5 * Created by Sarah Westcott on 7/8/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "getseqscommand.h"
11 #include "sequence.hpp"
12 #include "listvector.hpp"
14 //**********************************************************************************************************************
15 vector<string> GetSeqsCommand::getValidParameters(){
17 string Array[] = {"fasta","name", "group", "alignreport", "accnos", "dups", "list","taxonomy","outputdir","inputdir"};
18 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
22 m->errorOut(e, "GetSeqsCommand", "getValidParameters");
26 //**********************************************************************************************************************
27 GetSeqsCommand::GetSeqsCommand(){
30 //initialize outputTypes
31 vector<string> tempOutNames;
32 outputTypes["fasta"] = tempOutNames;
33 outputTypes["taxonomy"] = tempOutNames;
34 outputTypes["name"] = tempOutNames;
35 outputTypes["group"] = tempOutNames;
36 outputTypes["alignreport"] = tempOutNames;
37 outputTypes["list"] = tempOutNames;
40 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
44 //**********************************************************************************************************************
45 vector<string> GetSeqsCommand::getRequiredParameters(){
47 string Array[] = {"accnos"};
48 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
52 m->errorOut(e, "GetSeqsCommand", "getRequiredParameters");
56 //**********************************************************************************************************************
57 vector<string> GetSeqsCommand::getRequiredFiles(){
59 vector<string> myArray;
63 m->errorOut(e, "GetSeqsCommand", "getRequiredFiles");
67 //**********************************************************************************************************************
68 GetSeqsCommand::GetSeqsCommand(string option) {
72 //allow user to run help
73 if(option == "help") { help(); abort = true; }
76 //valid paramters for this command
77 string Array[] = {"fasta","name", "group", "alignreport", "accnos", "dups", "list","taxonomy","outputdir","inputdir"};
78 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
80 OptionParser parser(option);
81 map<string,string> parameters = parser.getParameters();
83 ValidParameters validParameter;
84 map<string,string>::iterator it;
86 //check to make sure all parameters are valid for command
87 for (it = parameters.begin(); it != parameters.end(); it++) {
88 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
91 //initialize outputTypes
92 vector<string> tempOutNames;
93 outputTypes["fasta"] = tempOutNames;
94 outputTypes["taxonomy"] = tempOutNames;
95 outputTypes["name"] = tempOutNames;
96 outputTypes["group"] = tempOutNames;
97 outputTypes["alignreport"] = tempOutNames;
98 outputTypes["list"] = tempOutNames;
100 //if the user changes the output directory command factory will send this info to us in the output parameter
101 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
103 //if the user changes the input directory command factory will send this info to us in the output parameter
104 string inputDir = validParameter.validFile(parameters, "inputdir", false);
105 if (inputDir == "not found"){ inputDir = ""; }
108 it = parameters.find("alignreport");
109 //user has given a template file
110 if(it != parameters.end()){
111 path = m->hasPath(it->second);
112 //if the user has not given a path then, add inputdir. else leave path alone.
113 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
116 it = parameters.find("fasta");
117 //user has given a template file
118 if(it != parameters.end()){
119 path = m->hasPath(it->second);
120 //if the user has not given a path then, add inputdir. else leave path alone.
121 if (path == "") { parameters["fasta"] = inputDir + it->second; }
124 it = parameters.find("accnos");
125 //user has given a template file
126 if(it != parameters.end()){
127 path = m->hasPath(it->second);
128 //if the user has not given a path then, add inputdir. else leave path alone.
129 if (path == "") { parameters["accnos"] = inputDir + it->second; }
132 it = parameters.find("list");
133 //user has given a template file
134 if(it != parameters.end()){
135 path = m->hasPath(it->second);
136 //if the user has not given a path then, add inputdir. else leave path alone.
137 if (path == "") { parameters["list"] = inputDir + it->second; }
140 it = parameters.find("name");
141 //user has given a template file
142 if(it != parameters.end()){
143 path = m->hasPath(it->second);
144 //if the user has not given a path then, add inputdir. else leave path alone.
145 if (path == "") { parameters["name"] = inputDir + it->second; }
148 it = parameters.find("group");
149 //user has given a template file
150 if(it != parameters.end()){
151 path = m->hasPath(it->second);
152 //if the user has not given a path then, add inputdir. else leave path alone.
153 if (path == "") { parameters["group"] = inputDir + it->second; }
156 it = parameters.find("taxonomy");
157 //user has given a template file
158 if(it != parameters.end()){
159 path = m->hasPath(it->second);
160 //if the user has not given a path then, add inputdir. else leave path alone.
161 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
166 //check for required parameters
167 accnosfile = validParameter.validFile(parameters, "accnos", true);
168 if (accnosfile == "not open") { abort = true; }
169 else if (accnosfile == "not found") { accnosfile = ""; m->mothurOut("You must provide an accnos file."); m->mothurOutEndLine(); abort = true; }
171 fastafile = validParameter.validFile(parameters, "fasta", true);
172 if (fastafile == "not open") { abort = true; }
173 else if (fastafile == "not found") { fastafile = ""; }
175 namefile = validParameter.validFile(parameters, "name", true);
176 if (namefile == "not open") { abort = true; }
177 else if (namefile == "not found") { namefile = ""; }
179 groupfile = validParameter.validFile(parameters, "group", true);
180 if (groupfile == "not open") { abort = true; }
181 else if (groupfile == "not found") { groupfile = ""; }
183 alignfile = validParameter.validFile(parameters, "alignreport", true);
184 if (alignfile == "not open") { abort = true; }
185 else if (alignfile == "not found") { alignfile = ""; }
187 listfile = validParameter.validFile(parameters, "list", true);
188 if (listfile == "not open") { abort = true; }
189 else if (listfile == "not found") { listfile = ""; }
191 taxfile = validParameter.validFile(parameters, "taxonomy", true);
192 if (taxfile == "not open") { abort = true; }
193 else if (taxfile == "not found") { taxfile = ""; }
195 string usedDups = "true";
196 string temp = validParameter.validFile(parameters, "dups", false); if (temp == "not found") { temp = "false"; usedDups = ""; }
197 dups = m->isTrue(temp);
199 if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == "") && (taxfile == "")) { m->mothurOut("You must provide one of the following: fasta, name, group, alignreport, taxonomy or listfile."); m->mothurOutEndLine(); abort = true; }
201 if ((usedDups != "") && (namefile == "")) { m->mothurOut("You may only use dups with the name option."); m->mothurOutEndLine(); abort = true; }
206 catch(exception& e) {
207 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
211 //**********************************************************************************************************************
213 void GetSeqsCommand::help(){
215 m->mothurOut("The get.seqs command reads an .accnos file and any of the following file types: fasta, name, group, list, taxonomy or alignreport file.\n");
216 m->mothurOut("It outputs a file containing only the sequences in the .accnos file.\n");
217 m->mothurOut("The get.seqs command parameters are accnos, fasta, name, group, list, taxonomy, alignreport and dups. You must provide accnos and at least one of the other parameters.\n");
218 m->mothurOut("The dups parameter allows you to add the entire line from a name file if you add any name from the line. default=false. \n");
219 m->mothurOut("The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
220 m->mothurOut("Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
221 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
223 catch(exception& e) {
224 m->errorOut(e, "GetSeqsCommand", "help");
229 //**********************************************************************************************************************
231 int GetSeqsCommand::execute(){
234 if (abort == true) { return 0; }
236 //get names you want to keep
239 if (m->control_pressed) { return 0; }
241 //read through the correct file and output lines you want to keep
242 if (namefile != "") { readName(); }
243 if (fastafile != "") { readFasta(); }
244 if (groupfile != "") { readGroup(); }
245 if (alignfile != "") { readAlign(); }
246 if (listfile != "") { readList(); }
247 if (taxfile != "") { readTax(); }
249 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
251 if (outputNames.size() != 0) {
252 m->mothurOutEndLine();
253 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
254 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
255 m->mothurOutEndLine();
261 catch(exception& e) {
262 m->errorOut(e, "GetSeqsCommand", "execute");
267 //**********************************************************************************************************************
268 int GetSeqsCommand::readFasta(){
270 string thisOutputDir = outputDir;
271 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
272 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
274 m->openOutputFile(outputFileName, out);
278 m->openInputFile(fastafile, in);
281 bool wroteSomething = false;
285 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
287 Sequence currSeq(in);
288 name = currSeq.getName();
291 //if this name is in the accnos file
292 if (names.count(name) != 0) {
293 wroteSomething = true;
295 currSeq.printSequence(out);
304 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
305 outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
310 catch(exception& e) {
311 m->errorOut(e, "GetSeqsCommand", "readFasta");
315 //**********************************************************************************************************************
316 int GetSeqsCommand::readList(){
318 string thisOutputDir = outputDir;
319 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
320 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
322 m->openOutputFile(outputFileName, out);
325 m->openInputFile(listfile, in);
327 bool wroteSomething = false;
331 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
333 //read in list vector
336 //make a new list vector
338 newList.setLabel(list.getLabel());
341 for (int i = 0; i < list.getNumBins(); i++) {
343 //parse out names that are in accnos file
344 string binnames = list.get(i);
346 string newNames = "";
347 while (binnames.find_first_of(',') != -1) {
348 string name = binnames.substr(0,binnames.find_first_of(','));
349 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
351 //if that name is in the .accnos file, add it
352 if (names.count(name) != 0) { newNames += name + ","; }
356 if (names.count(binnames) != 0) { newNames += binnames + ","; }
358 //if there are names in this bin add to new list
359 if (newNames != "") {
360 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
361 newList.push_back(newNames);
365 //print new listvector
366 if (newList.getNumBins() != 0) {
367 wroteSomething = true;
376 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
377 outputNames.push_back(outputFileName); outputTypes["list"].push_back(outputFileName);
382 catch(exception& e) {
383 m->errorOut(e, "GetSeqsCommand", "readList");
387 //**********************************************************************************************************************
388 int GetSeqsCommand::readName(){
390 string thisOutputDir = outputDir;
391 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
392 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
394 m->openOutputFile(outputFileName, out);
398 m->openInputFile(namefile, in);
399 string name, firstCol, secondCol;
401 bool wroteSomething = false;
406 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
412 if (dups) { hold = secondCol; }
414 vector<string> parsedNames;
415 //parse second column saving each name
416 while (secondCol.find_first_of(',') != -1) {
417 name = secondCol.substr(0,secondCol.find_first_of(','));
418 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
419 parsedNames.push_back(name);
422 //get name after last ,
423 parsedNames.push_back(secondCol);
425 vector<string> validSecond;
426 for (int i = 0; i < parsedNames.size(); i++) {
427 if (names.count(parsedNames[i]) != 0) {
428 validSecond.push_back(parsedNames[i]);
432 if ((dups) && (validSecond.size() != 0)) { //dups = true and we want to add someone, then add everyone
433 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
434 out << firstCol << '\t' << hold << endl;
435 wroteSomething = true;
437 //if the name in the first column is in the set then print it and any other names in second column also in set
438 if (names.count(firstCol) != 0) {
440 wroteSomething = true;
442 out << firstCol << '\t';
444 //you know you have at least one valid second since first column is valid
445 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
446 out << validSecond[validSecond.size()-1] << endl;
449 //make first name in set you come to first column and then add the remaining names to second column
451 //you want part of this row
452 if (validSecond.size() != 0) {
454 wroteSomething = true;
456 out << validSecond[0] << '\t';
458 //you know you have at least one valid second since first column is valid
459 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
460 out << validSecond[validSecond.size()-1] << endl;
469 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
470 outputNames.push_back(outputFileName); outputTypes["name"].push_back(outputFileName);
475 catch(exception& e) {
476 m->errorOut(e, "GetSeqsCommand", "readName");
481 //**********************************************************************************************************************
482 int GetSeqsCommand::readGroup(){
484 string thisOutputDir = outputDir;
485 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
486 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
488 m->openOutputFile(outputFileName, out);
492 m->openInputFile(groupfile, in);
495 bool wroteSomething = false;
499 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
502 in >> name; //read from first column
503 in >> group; //read from second column
505 //if this name is in the accnos file
506 if (names.count(name) != 0) {
507 wroteSomething = true;
509 out << name << '\t' << group << endl;
517 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
518 outputNames.push_back(outputFileName); outputTypes["group"].push_back(outputFileName);
523 catch(exception& e) {
524 m->errorOut(e, "GetSeqsCommand", "readGroup");
528 //**********************************************************************************************************************
529 int GetSeqsCommand::readTax(){
531 string thisOutputDir = outputDir;
532 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
533 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
535 m->openOutputFile(outputFileName, out);
538 m->openInputFile(taxfile, in);
541 bool wroteSomething = false;
545 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
547 in >> name; //read from first column
548 in >> tax; //read from second column
550 //if this name is in the accnos file
551 if (names.count(name) != 0) {
552 wroteSomething = true;
554 out << name << '\t' << tax << endl;
562 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
563 outputNames.push_back(outputFileName); outputTypes["taxonomy"].push_back(outputFileName);
568 catch(exception& e) {
569 m->errorOut(e, "GetSeqsCommand", "readTax");
573 //**********************************************************************************************************************
574 //alignreport file has a column header line then all other lines contain 16 columns. we just want the first column since that contains the name
575 int GetSeqsCommand::readAlign(){
577 string thisOutputDir = outputDir;
578 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
579 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
581 m->openOutputFile(outputFileName, out);
585 m->openInputFile(alignfile, in);
588 bool wroteSomething = false;
590 //read column headers
591 for (int i = 0; i < 16; i++) {
592 if (!in.eof()) { in >> junk; out << junk << '\t'; }
599 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
602 in >> name; //read from first column
604 //if this name is in the accnos file
605 if (names.count(name) != 0) {
606 wroteSomething = true;
611 for (int i = 0; i < 15; i++) {
612 if (!in.eof()) { in >> junk; out << junk << '\t'; }
617 }else {//still read just don't do anything with it
619 for (int i = 0; i < 15; i++) {
620 if (!in.eof()) { in >> junk; }
630 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
631 outputNames.push_back(outputFileName); outputTypes["alignreport"].push_back(outputFileName);
636 catch(exception& e) {
637 m->errorOut(e, "GetSeqsCommand", "readAlign");
641 //**********************************************************************************************************************
643 int GetSeqsCommand::readAccnos(){
647 m->openInputFile(accnosfile, in);
662 catch(exception& e) {
663 m->errorOut(e, "GetSeqsCommand", "readAccnos");
668 //**********************************************************************************************************************