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 if (accnosfile2 == "not found") { accnosfile2 = ""; }
193 fastafile = validParameter.validFile(parameters, "fasta", true);
194 if (fastafile == "not open") { abort = true; }
195 else if (fastafile == "not found") { fastafile = ""; }
197 namefile = validParameter.validFile(parameters, "name", true);
198 if (namefile == "not open") { abort = true; }
199 else if (namefile == "not found") { namefile = ""; }
201 groupfile = validParameter.validFile(parameters, "group", true);
202 if (groupfile == "not open") { abort = true; }
203 else if (groupfile == "not found") { groupfile = ""; }
205 alignfile = validParameter.validFile(parameters, "alignreport", true);
206 if (alignfile == "not open") { abort = true; }
207 else if (alignfile == "not found") { alignfile = ""; }
209 listfile = validParameter.validFile(parameters, "list", true);
210 if (listfile == "not open") { abort = true; }
211 else if (listfile == "not found") { listfile = ""; }
213 taxfile = validParameter.validFile(parameters, "taxonomy", true);
214 if (taxfile == "not open") { abort = true; }
215 else if (taxfile == "not found") { taxfile = ""; }
217 qualfile = validParameter.validFile(parameters, "qfile", true);
218 if (qualfile == "not open") { abort = true; }
219 else if (qualfile == "not found") { qualfile = ""; }
221 string usedDups = "true";
222 string temp = validParameter.validFile(parameters, "dups", false); if (temp == "not found") { temp = "false"; usedDups = ""; }
223 dups = m->isTrue(temp);
225 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; }
227 if ((usedDups != "") && (namefile == "")) { m->mothurOut("You may only use dups with the name option."); m->mothurOutEndLine(); abort = true; }
232 catch(exception& e) {
233 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
237 //**********************************************************************************************************************
239 void GetSeqsCommand::help(){
241 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");
242 m->mothurOut("It outputs a file containing only the sequences in the .accnos file.\n");
243 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");
244 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");
245 m->mothurOut("The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
246 m->mothurOut("Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
247 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
249 catch(exception& e) {
250 m->errorOut(e, "GetSeqsCommand", "help");
255 //**********************************************************************************************************************
257 int GetSeqsCommand::execute(){
260 if (abort == true) { return 0; }
262 //get names you want to keep
265 if (m->control_pressed) { return 0; }
267 //read through the correct file and output lines you want to keep
268 if (namefile != "") { readName(); }
269 if (fastafile != "") { readFasta(); }
270 if (groupfile != "") { readGroup(); }
271 if (alignfile != "") { readAlign(); }
272 if (listfile != "") { readList(); }
273 if (taxfile != "") { readTax(); }
274 if (qualfile != "") { readQual(); }
275 if (accnosfile2 != "") { compareAccnos(); }
277 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
279 m->mothurOut("Selected " + toString(names.size()) + " sequences."); m->mothurOutEndLine();
281 if (outputNames.size() != 0) {
282 m->mothurOutEndLine();
283 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
284 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
285 m->mothurOutEndLine();
291 catch(exception& e) {
292 m->errorOut(e, "GetSeqsCommand", "execute");
297 //**********************************************************************************************************************
298 int GetSeqsCommand::readFasta(){
300 string thisOutputDir = outputDir;
301 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
302 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
304 m->openOutputFile(outputFileName, out);
308 m->openInputFile(fastafile, in);
311 bool wroteSomething = false;
315 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
317 Sequence currSeq(in);
318 name = currSeq.getName();
321 //if this name is in the accnos file
322 if (names.count(name) != 0) {
323 wroteSomething = true;
325 currSeq.printSequence(out);
334 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
335 outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
340 catch(exception& e) {
341 m->errorOut(e, "GetSeqsCommand", "readFasta");
345 //**********************************************************************************************************************
346 int GetSeqsCommand::readQual(){
348 string thisOutputDir = outputDir;
349 if (outputDir == "") { thisOutputDir += m->hasPath(qualfile); }
350 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(qualfile)) + "pick" + m->getExtension(qualfile);
352 m->openOutputFile(outputFileName, out);
356 m->openInputFile(qualfile, in);
359 bool wroteSomething = false;
363 string saveName = "";
369 if (name.length() != 0) {
370 saveName = name.substr(1);
373 if (c == 10 || c == 13){ break; }
380 char letter= in.get();
381 if(letter == '>'){ in.putback(letter); break; }
382 else{ scores += letter; }
387 if (names.count(saveName) != 0) {
388 wroteSomething = true;
390 out << name << endl << scores;
399 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
400 outputNames.push_back(outputFileName); outputTypes["qfile"].push_back(outputFileName);
405 catch(exception& e) {
406 m->errorOut(e, "GetSeqsCommand", "readQual");
410 //**********************************************************************************************************************
411 int GetSeqsCommand::readList(){
413 string thisOutputDir = outputDir;
414 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
415 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
417 m->openOutputFile(outputFileName, out);
420 m->openInputFile(listfile, in);
422 bool wroteSomething = false;
426 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
428 //read in list vector
431 //make a new list vector
433 newList.setLabel(list.getLabel());
436 for (int i = 0; i < list.getNumBins(); i++) {
438 //parse out names that are in accnos file
439 string binnames = list.get(i);
441 string newNames = "";
442 while (binnames.find_first_of(',') != -1) {
443 string name = binnames.substr(0,binnames.find_first_of(','));
444 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
446 //if that name is in the .accnos file, add it
447 if (names.count(name) != 0) { newNames += name + ","; }
451 if (names.count(binnames) != 0) { newNames += binnames + ","; }
453 //if there are names in this bin add to new list
454 if (newNames != "") {
455 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
456 newList.push_back(newNames);
460 //print new listvector
461 if (newList.getNumBins() != 0) {
462 wroteSomething = true;
471 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
472 outputNames.push_back(outputFileName); outputTypes["list"].push_back(outputFileName);
477 catch(exception& e) {
478 m->errorOut(e, "GetSeqsCommand", "readList");
482 //**********************************************************************************************************************
483 int GetSeqsCommand::readName(){
485 string thisOutputDir = outputDir;
486 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
487 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
489 m->openOutputFile(outputFileName, out);
493 m->openInputFile(namefile, in);
494 string name, firstCol, secondCol;
496 bool wroteSomething = false;
501 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
507 if (dups) { hold = secondCol; }
509 vector<string> parsedNames;
510 //parse second column saving each name
511 while (secondCol.find_first_of(',') != -1) {
512 name = secondCol.substr(0,secondCol.find_first_of(','));
513 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
514 parsedNames.push_back(name);
517 //get name after last ,
518 parsedNames.push_back(secondCol);
520 vector<string> validSecond;
521 for (int i = 0; i < parsedNames.size(); i++) {
522 if (names.count(parsedNames[i]) != 0) {
523 validSecond.push_back(parsedNames[i]);
527 if ((dups) && (validSecond.size() != 0)) { //dups = true and we want to add someone, then add everyone
528 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
529 out << firstCol << '\t' << hold << endl;
530 wroteSomething = true;
532 //if the name in the first column is in the set then print it and any other names in second column also in set
533 if (names.count(firstCol) != 0) {
535 wroteSomething = true;
537 out << firstCol << '\t';
539 //you know you have at least one valid second since first column is valid
540 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
541 out << validSecond[validSecond.size()-1] << endl;
544 //make first name in set you come to first column and then add the remaining names to second column
546 //you want part of this row
547 if (validSecond.size() != 0) {
549 wroteSomething = true;
551 out << validSecond[0] << '\t';
553 //you know you have at least one valid second since first column is valid
554 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
555 out << validSecond[validSecond.size()-1] << endl;
564 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
565 outputNames.push_back(outputFileName); outputTypes["name"].push_back(outputFileName);
570 catch(exception& e) {
571 m->errorOut(e, "GetSeqsCommand", "readName");
576 //**********************************************************************************************************************
577 int GetSeqsCommand::readGroup(){
579 string thisOutputDir = outputDir;
580 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
581 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
583 m->openOutputFile(outputFileName, out);
587 m->openInputFile(groupfile, in);
590 bool wroteSomething = false;
594 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
597 in >> name; //read from first column
598 in >> group; //read from second column
600 //if this name is in the accnos file
601 if (names.count(name) != 0) {
602 wroteSomething = true;
604 out << name << '\t' << group << endl;
612 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
613 outputNames.push_back(outputFileName); outputTypes["group"].push_back(outputFileName);
618 catch(exception& e) {
619 m->errorOut(e, "GetSeqsCommand", "readGroup");
623 //**********************************************************************************************************************
624 int GetSeqsCommand::readTax(){
626 string thisOutputDir = outputDir;
627 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
628 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
630 m->openOutputFile(outputFileName, out);
633 m->openInputFile(taxfile, in);
636 bool wroteSomething = false;
640 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
642 in >> name; //read from first column
643 in >> tax; //read from second column
645 //if this name is in the accnos file
646 if (names.count(name) != 0) {
647 wroteSomething = true;
649 out << name << '\t' << tax << endl;
657 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
658 outputNames.push_back(outputFileName); outputTypes["taxonomy"].push_back(outputFileName);
663 catch(exception& e) {
664 m->errorOut(e, "GetSeqsCommand", "readTax");
668 //**********************************************************************************************************************
669 //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
670 int GetSeqsCommand::readAlign(){
672 string thisOutputDir = outputDir;
673 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
674 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
676 m->openOutputFile(outputFileName, out);
680 m->openInputFile(alignfile, in);
683 bool wroteSomething = false;
685 //read column headers
686 for (int i = 0; i < 16; i++) {
687 if (!in.eof()) { in >> junk; out << junk << '\t'; }
694 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
697 in >> name; //read from first column
699 //if this name is in the accnos file
700 if (names.count(name) != 0) {
701 wroteSomething = true;
706 for (int i = 0; i < 15; i++) {
707 if (!in.eof()) { in >> junk; out << junk << '\t'; }
712 }else {//still read just don't do anything with it
714 for (int i = 0; i < 15; i++) {
715 if (!in.eof()) { in >> junk; }
725 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
726 outputNames.push_back(outputFileName); outputTypes["alignreport"].push_back(outputFileName);
731 catch(exception& e) {
732 m->errorOut(e, "GetSeqsCommand", "readAlign");
736 //**********************************************************************************************************************
738 int GetSeqsCommand::readAccnos(){
742 m->openInputFile(accnosfile, in);
757 catch(exception& e) {
758 m->errorOut(e, "GetSeqsCommand", "readAccnos");
762 //**********************************************************************************************************************
764 int GetSeqsCommand::compareAccnos(){
767 string thisOutputDir = outputDir;
768 if (outputDir == "") { thisOutputDir += m->hasPath(accnosfile); }
769 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(accnosfile)) + "accnos.report";
771 m->openOutputFile(outputFileName, out);
774 m->openInputFile(accnosfile2, in);
777 set<string> namesAccnos2;
778 set<string> namesDups;
779 set<string> namesAccnos = names;
781 map<string, int> nameCount;
783 if (namefile != "") {
785 m->openInputFile(namefile, inName);
788 while(!inName.eof()){
790 if (m->control_pressed) { inName.close(); return 0; }
792 string thisname, repnames;
794 inName >> thisname; m->gobble(inName); //read from first column
795 inName >> repnames; //read from second column
797 int num = m->getNumNames(repnames);
798 nameCount[thisname] = num;
808 if (namesAccnos.count(name) == 0){ //name unique to accnos2
809 namesAccnos2.insert(name);
810 }else { //you are in both so erase
811 namesAccnos.erase(name);
812 namesDups.insert(name);
819 out << "Names in both files : " + toString(namesDups.size()) << endl;
820 m->mothurOut("Names in both files : " + toString(namesDups.size())); m->mothurOutEndLine();
822 for (set<string>::iterator it = namesDups.begin(); it != namesDups.end(); it++) {
824 if (namefile != "") { out << '\t' << nameCount[(*it)]; }
828 out << "Names unique to " + accnosfile + " : " + toString(namesAccnos.size()) << endl;
829 m->mothurOut("Names unique to " + accnosfile + " : " + toString(namesAccnos.size())); m->mothurOutEndLine();
831 for (set<string>::iterator it = namesAccnos.begin(); it != namesAccnos.end(); it++) {
833 if (namefile != "") { out << '\t' << nameCount[(*it)]; }
837 out << "Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size()) << endl;
838 m->mothurOut("Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size())); m->mothurOutEndLine();
840 for (set<string>::iterator it = namesAccnos2.begin(); it != namesAccnos2.end(); it++) {
842 if (namefile != "") { out << '\t' << nameCount[(*it)]; }
848 outputNames.push_back(outputFileName); outputTypes["accnosreport"].push_back(outputFileName);
853 catch(exception& e) {
854 m->errorOut(e, "GetSeqsCommand", "readAccnos");
860 //**********************************************************************************************************************