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", "qfile","alignreport", "accnos", "accnos2","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;
38 outputTypes["qfile"] = tempOutNames;
39 outputTypes["accnosreport"] = tempOutNames;
42 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
46 //**********************************************************************************************************************
47 vector<string> GetSeqsCommand::getRequiredParameters(){
49 string Array[] = {"accnos"};
50 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
54 m->errorOut(e, "GetSeqsCommand", "getRequiredParameters");
58 //**********************************************************************************************************************
59 vector<string> GetSeqsCommand::getRequiredFiles(){
61 vector<string> myArray;
65 m->errorOut(e, "GetSeqsCommand", "getRequiredFiles");
69 //**********************************************************************************************************************
70 GetSeqsCommand::GetSeqsCommand(string option) {
74 //allow user to run help
75 if(option == "help") { help(); abort = true; }
78 //valid paramters for this command
79 string Array[] = {"fasta","name", "group", "alignreport", "qfile", "accnos", "accnos2","dups", "list","taxonomy","outputdir","inputdir"};
80 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
82 OptionParser parser(option);
83 map<string,string> parameters = parser.getParameters();
85 ValidParameters validParameter;
86 map<string,string>::iterator it;
88 //check to make sure all parameters are valid for command
89 for (it = parameters.begin(); it != parameters.end(); it++) {
90 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
93 //initialize outputTypes
94 vector<string> tempOutNames;
95 outputTypes["fasta"] = tempOutNames;
96 outputTypes["taxonomy"] = tempOutNames;
97 outputTypes["name"] = tempOutNames;
98 outputTypes["group"] = tempOutNames;
99 outputTypes["alignreport"] = tempOutNames;
100 outputTypes["list"] = tempOutNames;
101 outputTypes["qfile"] = tempOutNames;
102 outputTypes["accnosreport"] = tempOutNames;
104 //if the user changes the output directory command factory will send this info to us in the output parameter
105 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
107 //if the user changes the input directory command factory will send this info to us in the output parameter
108 string inputDir = validParameter.validFile(parameters, "inputdir", false);
109 if (inputDir == "not found"){ inputDir = ""; }
112 it = parameters.find("alignreport");
113 //user has given a template file
114 if(it != parameters.end()){
115 path = m->hasPath(it->second);
116 //if the user has not given a path then, add inputdir. else leave path alone.
117 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
120 it = parameters.find("fasta");
121 //user has given a template file
122 if(it != parameters.end()){
123 path = m->hasPath(it->second);
124 //if the user has not given a path then, add inputdir. else leave path alone.
125 if (path == "") { parameters["fasta"] = inputDir + it->second; }
128 it = parameters.find("accnos");
129 //user has given a template file
130 if(it != parameters.end()){
131 path = m->hasPath(it->second);
132 //if the user has not given a path then, add inputdir. else leave path alone.
133 if (path == "") { parameters["accnos"] = inputDir + it->second; }
136 it = parameters.find("accnos2");
137 //user has given a template file
138 if(it != parameters.end()){
139 path = m->hasPath(it->second);
140 //if the user has not given a path then, add inputdir. else leave path alone.
141 if (path == "") { parameters["accnos2"] = inputDir + it->second; }
144 it = parameters.find("list");
145 //user has given a template file
146 if(it != parameters.end()){
147 path = m->hasPath(it->second);
148 //if the user has not given a path then, add inputdir. else leave path alone.
149 if (path == "") { parameters["list"] = inputDir + it->second; }
152 it = parameters.find("name");
153 //user has given a template file
154 if(it != parameters.end()){
155 path = m->hasPath(it->second);
156 //if the user has not given a path then, add inputdir. else leave path alone.
157 if (path == "") { parameters["name"] = inputDir + it->second; }
160 it = parameters.find("group");
161 //user has given a template file
162 if(it != parameters.end()){
163 path = m->hasPath(it->second);
164 //if the user has not given a path then, add inputdir. else leave path alone.
165 if (path == "") { parameters["group"] = inputDir + it->second; }
168 it = parameters.find("taxonomy");
169 //user has given a template file
170 if(it != parameters.end()){
171 path = m->hasPath(it->second);
172 //if the user has not given a path then, add inputdir. else leave path alone.
173 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
176 it = parameters.find("qfile");
177 //user has given a template file
178 if(it != parameters.end()){
179 path = m->hasPath(it->second);
180 //if the user has not given a path then, add inputdir. else leave path alone.
181 if (path == "") { parameters["qfile"] = inputDir + it->second; }
186 //check for required parameters
187 accnosfile = validParameter.validFile(parameters, "accnos", true);
188 if (accnosfile == "not open") { abort = true; }
189 else if (accnosfile == "not found") { accnosfile = ""; m->mothurOut("You must provide an accnos file."); m->mothurOutEndLine(); abort = true; }
191 accnosfile2 = validParameter.validFile(parameters, "accnos2", true);
192 if (accnosfile2 == "not open") { abort = true; }
194 fastafile = validParameter.validFile(parameters, "fasta", true);
195 if (fastafile == "not open") { abort = true; }
196 else if (fastafile == "not found") { fastafile = ""; }
198 namefile = validParameter.validFile(parameters, "name", true);
199 if (namefile == "not open") { abort = true; }
200 else if (namefile == "not found") { namefile = ""; }
202 groupfile = validParameter.validFile(parameters, "group", true);
203 if (groupfile == "not open") { abort = true; }
204 else if (groupfile == "not found") { groupfile = ""; }
206 alignfile = validParameter.validFile(parameters, "alignreport", true);
207 if (alignfile == "not open") { abort = true; }
208 else if (alignfile == "not found") { alignfile = ""; }
210 listfile = validParameter.validFile(parameters, "list", true);
211 if (listfile == "not open") { abort = true; }
212 else if (listfile == "not found") { listfile = ""; }
214 taxfile = validParameter.validFile(parameters, "taxonomy", true);
215 if (taxfile == "not open") { abort = true; }
216 else if (taxfile == "not found") { taxfile = ""; }
218 qualfile = validParameter.validFile(parameters, "qfile", true);
219 if (qualfile == "not open") { abort = true; }
220 else if (qualfile == "not found") { qualfile = ""; }
222 string usedDups = "true";
223 string temp = validParameter.validFile(parameters, "dups", false); if (temp == "not found") { temp = "false"; usedDups = ""; }
224 dups = m->isTrue(temp);
226 if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == "") && (taxfile == "") && (qualfile == "") && (accnosfile2 == "")) { m->mothurOut("You must provide one of the following: fasta, name, group, alignreport, taxonomy, quality or listfile."); m->mothurOutEndLine(); abort = true; }
228 if ((usedDups != "") && (namefile == "")) { m->mothurOut("You may only use dups with the name option."); m->mothurOutEndLine(); abort = true; }
233 catch(exception& e) {
234 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
238 //**********************************************************************************************************************
240 void GetSeqsCommand::help(){
242 m->mothurOut("The get.seqs command reads an .accnos file and any of the following file types: fasta, name, group, list, taxonomy, quality or alignreport file.\n");
243 m->mothurOut("It outputs a file containing only the sequences in the .accnos file.\n");
244 m->mothurOut("The get.seqs command parameters are accnos, fasta, name, group, list, taxonomy, qfile, alignreport and dups. You must provide accnos and at least one of the other parameters.\n");
245 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");
246 m->mothurOut("The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
247 m->mothurOut("Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
248 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
250 catch(exception& e) {
251 m->errorOut(e, "GetSeqsCommand", "help");
256 //**********************************************************************************************************************
258 int GetSeqsCommand::execute(){
261 if (abort == true) { return 0; }
263 //get names you want to keep
266 if (m->control_pressed) { return 0; }
268 //read through the correct file and output lines you want to keep
269 if (namefile != "") { readName(); }
270 if (fastafile != "") { readFasta(); }
271 if (groupfile != "") { readGroup(); }
272 if (alignfile != "") { readAlign(); }
273 if (listfile != "") { readList(); }
274 if (taxfile != "") { readTax(); }
275 if (qualfile != "") { readQual(); }
276 if (accnosfile2 != "") { compareAccnos(); }
278 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
280 m->mothurOut("Selected " + toString(names.size()) + " sequences."); m->mothurOutEndLine();
282 if (outputNames.size() != 0) {
283 m->mothurOutEndLine();
284 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
285 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
286 m->mothurOutEndLine();
292 catch(exception& e) {
293 m->errorOut(e, "GetSeqsCommand", "execute");
298 //**********************************************************************************************************************
299 int GetSeqsCommand::readFasta(){
301 string thisOutputDir = outputDir;
302 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
303 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
305 m->openOutputFile(outputFileName, out);
309 m->openInputFile(fastafile, in);
312 bool wroteSomething = false;
316 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
318 Sequence currSeq(in);
319 name = currSeq.getName();
322 //if this name is in the accnos file
323 if (names.count(name) != 0) {
324 wroteSomething = true;
326 currSeq.printSequence(out);
335 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
336 outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
341 catch(exception& e) {
342 m->errorOut(e, "GetSeqsCommand", "readFasta");
346 //**********************************************************************************************************************
347 int GetSeqsCommand::readQual(){
349 string thisOutputDir = outputDir;
350 if (outputDir == "") { thisOutputDir += m->hasPath(qualfile); }
351 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(qualfile)) + "pick" + m->getExtension(qualfile);
353 m->openOutputFile(outputFileName, out);
357 m->openInputFile(qualfile, in);
360 bool wroteSomething = false;
364 string saveName = "";
370 if (name.length() != 0) {
371 saveName = name.substr(1);
374 if (c == 10 || c == 13){ break; }
381 char letter= in.get();
382 if(letter == '>'){ in.putback(letter); break; }
383 else{ scores += letter; }
388 if (names.count(saveName) != 0) {
389 wroteSomething = true;
391 out << name << endl << scores;
400 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
401 outputNames.push_back(outputFileName); outputTypes["qfile"].push_back(outputFileName);
406 catch(exception& e) {
407 m->errorOut(e, "GetSeqsCommand", "readQual");
411 //**********************************************************************************************************************
412 int GetSeqsCommand::readList(){
414 string thisOutputDir = outputDir;
415 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
416 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
418 m->openOutputFile(outputFileName, out);
421 m->openInputFile(listfile, in);
423 bool wroteSomething = false;
427 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
429 //read in list vector
432 //make a new list vector
434 newList.setLabel(list.getLabel());
437 for (int i = 0; i < list.getNumBins(); i++) {
439 //parse out names that are in accnos file
440 string binnames = list.get(i);
442 string newNames = "";
443 while (binnames.find_first_of(',') != -1) {
444 string name = binnames.substr(0,binnames.find_first_of(','));
445 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
447 //if that name is in the .accnos file, add it
448 if (names.count(name) != 0) { newNames += name + ","; }
452 if (names.count(binnames) != 0) { newNames += binnames + ","; }
454 //if there are names in this bin add to new list
455 if (newNames != "") {
456 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
457 newList.push_back(newNames);
461 //print new listvector
462 if (newList.getNumBins() != 0) {
463 wroteSomething = true;
472 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
473 outputNames.push_back(outputFileName); outputTypes["list"].push_back(outputFileName);
478 catch(exception& e) {
479 m->errorOut(e, "GetSeqsCommand", "readList");
483 //**********************************************************************************************************************
484 int GetSeqsCommand::readName(){
486 string thisOutputDir = outputDir;
487 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
488 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
490 m->openOutputFile(outputFileName, out);
494 m->openInputFile(namefile, in);
495 string name, firstCol, secondCol;
497 bool wroteSomething = false;
502 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
508 if (dups) { hold = secondCol; }
510 vector<string> parsedNames;
511 //parse second column saving each name
512 while (secondCol.find_first_of(',') != -1) {
513 name = secondCol.substr(0,secondCol.find_first_of(','));
514 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
515 parsedNames.push_back(name);
518 //get name after last ,
519 parsedNames.push_back(secondCol);
521 vector<string> validSecond;
522 for (int i = 0; i < parsedNames.size(); i++) {
523 if (names.count(parsedNames[i]) != 0) {
524 validSecond.push_back(parsedNames[i]);
528 if ((dups) && (validSecond.size() != 0)) { //dups = true and we want to add someone, then add everyone
529 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
530 out << firstCol << '\t' << hold << endl;
531 wroteSomething = true;
533 //if the name in the first column is in the set then print it and any other names in second column also in set
534 if (names.count(firstCol) != 0) {
536 wroteSomething = true;
538 out << firstCol << '\t';
540 //you know you have at least one valid second since first column is valid
541 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
542 out << validSecond[validSecond.size()-1] << endl;
545 //make first name in set you come to first column and then add the remaining names to second column
547 //you want part of this row
548 if (validSecond.size() != 0) {
550 wroteSomething = true;
552 out << validSecond[0] << '\t';
554 //you know you have at least one valid second since first column is valid
555 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
556 out << validSecond[validSecond.size()-1] << endl;
565 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
566 outputNames.push_back(outputFileName); outputTypes["name"].push_back(outputFileName);
571 catch(exception& e) {
572 m->errorOut(e, "GetSeqsCommand", "readName");
577 //**********************************************************************************************************************
578 int GetSeqsCommand::readGroup(){
580 string thisOutputDir = outputDir;
581 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
582 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
584 m->openOutputFile(outputFileName, out);
588 m->openInputFile(groupfile, in);
591 bool wroteSomething = false;
595 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
598 in >> name; //read from first column
599 in >> group; //read from second column
601 //if this name is in the accnos file
602 if (names.count(name) != 0) {
603 wroteSomething = true;
605 out << name << '\t' << group << endl;
613 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
614 outputNames.push_back(outputFileName); outputTypes["group"].push_back(outputFileName);
619 catch(exception& e) {
620 m->errorOut(e, "GetSeqsCommand", "readGroup");
624 //**********************************************************************************************************************
625 int GetSeqsCommand::readTax(){
627 string thisOutputDir = outputDir;
628 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
629 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
631 m->openOutputFile(outputFileName, out);
634 m->openInputFile(taxfile, in);
637 bool wroteSomething = false;
641 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
643 in >> name; //read from first column
644 in >> tax; //read from second column
646 //if this name is in the accnos file
647 if (names.count(name) != 0) {
648 wroteSomething = true;
650 out << name << '\t' << tax << endl;
658 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
659 outputNames.push_back(outputFileName); outputTypes["taxonomy"].push_back(outputFileName);
664 catch(exception& e) {
665 m->errorOut(e, "GetSeqsCommand", "readTax");
669 //**********************************************************************************************************************
670 //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
671 int GetSeqsCommand::readAlign(){
673 string thisOutputDir = outputDir;
674 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
675 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
677 m->openOutputFile(outputFileName, out);
681 m->openInputFile(alignfile, in);
684 bool wroteSomething = false;
686 //read column headers
687 for (int i = 0; i < 16; i++) {
688 if (!in.eof()) { in >> junk; out << junk << '\t'; }
695 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
698 in >> name; //read from first column
700 //if this name is in the accnos file
701 if (names.count(name) != 0) {
702 wroteSomething = true;
707 for (int i = 0; i < 15; i++) {
708 if (!in.eof()) { in >> junk; out << junk << '\t'; }
713 }else {//still read just don't do anything with it
715 for (int i = 0; i < 15; i++) {
716 if (!in.eof()) { in >> junk; }
726 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
727 outputNames.push_back(outputFileName); outputTypes["alignreport"].push_back(outputFileName);
732 catch(exception& e) {
733 m->errorOut(e, "GetSeqsCommand", "readAlign");
737 //**********************************************************************************************************************
739 int GetSeqsCommand::readAccnos(){
743 m->openInputFile(accnosfile, in);
758 catch(exception& e) {
759 m->errorOut(e, "GetSeqsCommand", "readAccnos");
763 //**********************************************************************************************************************
765 int GetSeqsCommand::compareAccnos(){
768 string thisOutputDir = outputDir;
769 if (outputDir == "") { thisOutputDir += m->hasPath(accnosfile); }
770 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(accnosfile)) + "accnos.report";
772 m->openOutputFile(outputFileName, out);
775 m->openInputFile(accnosfile2, in);
778 set<string> namesAccnos2;
779 set<string> namesDups;
780 set<string> namesAccnos = names;
785 if (namesAccnos.count(name) == 0){ //name unique to accnos2
786 namesAccnos2.insert(name);
787 }else { //you are in both so erase
788 namesAccnos.erase(name);
789 namesDups.insert(name);
796 out << "Names in both files : " + toString(namesDups.size()) << endl;
797 m->mothurOut("Names in both files : " + toString(namesDups.size())); m->mothurOutEndLine();
799 for (set<string>::iterator it = namesDups.begin(); it != namesDups.end(); it++) {
800 out << (*it) << endl;
803 out << "Names unique to " + accnosfile + " : " + toString(namesAccnos.size()) << endl;
804 m->mothurOut("Names unique to " + accnosfile + " : " + toString(namesAccnos.size())); m->mothurOutEndLine();
806 for (set<string>::iterator it = namesAccnos.begin(); it != namesAccnos.end(); it++) {
807 out << (*it) << endl;
810 out << "Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size()) << endl;
811 m->mothurOut("Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size())); m->mothurOutEndLine();
813 for (set<string>::iterator it = namesAccnos2.begin(); it != namesAccnos2.end(); it++) {
814 out << (*it) << endl;
819 outputNames.push_back(outputFileName); outputTypes["accnosreport"].push_back(outputFileName);
824 catch(exception& e) {
825 m->errorOut(e, "GetSeqsCommand", "readAccnos");
831 //**********************************************************************************************************************