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(){
29 abort = true; calledHelp = true;
30 vector<string> tempOutNames;
31 outputTypes["fasta"] = tempOutNames;
32 outputTypes["taxonomy"] = tempOutNames;
33 outputTypes["name"] = tempOutNames;
34 outputTypes["group"] = tempOutNames;
35 outputTypes["alignreport"] = tempOutNames;
36 outputTypes["list"] = tempOutNames;
37 outputTypes["qfile"] = tempOutNames;
38 outputTypes["accnosreport"] = tempOutNames;
41 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
45 //**********************************************************************************************************************
46 vector<string> GetSeqsCommand::getRequiredParameters(){
48 string Array[] = {"accnos"};
49 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
53 m->errorOut(e, "GetSeqsCommand", "getRequiredParameters");
57 //**********************************************************************************************************************
58 vector<string> GetSeqsCommand::getRequiredFiles(){
60 vector<string> myArray;
64 m->errorOut(e, "GetSeqsCommand", "getRequiredFiles");
68 //**********************************************************************************************************************
69 GetSeqsCommand::GetSeqsCommand(string option) {
71 abort = false; calledHelp = false;
73 //allow user to run help
74 if(option == "help") { help(); abort = true; calledHelp = true; }
77 //valid paramters for this command
78 string Array[] = {"fasta","name", "group", "alignreport", "qfile", "accnos", "accnos2","dups", "list","taxonomy","outputdir","inputdir"};
79 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
81 OptionParser parser(option);
82 map<string,string> parameters = parser.getParameters();
84 ValidParameters validParameter;
85 map<string,string>::iterator it;
87 //check to make sure all parameters are valid for command
88 for (it = parameters.begin(); it != parameters.end(); it++) {
89 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
92 //initialize outputTypes
93 vector<string> tempOutNames;
94 outputTypes["fasta"] = tempOutNames;
95 outputTypes["taxonomy"] = tempOutNames;
96 outputTypes["name"] = tempOutNames;
97 outputTypes["group"] = tempOutNames;
98 outputTypes["alignreport"] = tempOutNames;
99 outputTypes["list"] = tempOutNames;
100 outputTypes["qfile"] = tempOutNames;
101 outputTypes["accnosreport"] = tempOutNames;
103 //if the user changes the output directory command factory will send this info to us in the output parameter
104 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
106 //if the user changes the input directory command factory will send this info to us in the output parameter
107 string inputDir = validParameter.validFile(parameters, "inputdir", false);
108 if (inputDir == "not found"){ inputDir = ""; }
111 it = parameters.find("alignreport");
112 //user has given a template file
113 if(it != parameters.end()){
114 path = m->hasPath(it->second);
115 //if the user has not given a path then, add inputdir. else leave path alone.
116 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
119 it = parameters.find("fasta");
120 //user has given a template file
121 if(it != parameters.end()){
122 path = m->hasPath(it->second);
123 //if the user has not given a path then, add inputdir. else leave path alone.
124 if (path == "") { parameters["fasta"] = inputDir + it->second; }
127 it = parameters.find("accnos");
128 //user has given a template file
129 if(it != parameters.end()){
130 path = m->hasPath(it->second);
131 //if the user has not given a path then, add inputdir. else leave path alone.
132 if (path == "") { parameters["accnos"] = inputDir + it->second; }
135 it = parameters.find("accnos2");
136 //user has given a template file
137 if(it != parameters.end()){
138 path = m->hasPath(it->second);
139 //if the user has not given a path then, add inputdir. else leave path alone.
140 if (path == "") { parameters["accnos2"] = inputDir + it->second; }
143 it = parameters.find("list");
144 //user has given a template file
145 if(it != parameters.end()){
146 path = m->hasPath(it->second);
147 //if the user has not given a path then, add inputdir. else leave path alone.
148 if (path == "") { parameters["list"] = inputDir + it->second; }
151 it = parameters.find("name");
152 //user has given a template file
153 if(it != parameters.end()){
154 path = m->hasPath(it->second);
155 //if the user has not given a path then, add inputdir. else leave path alone.
156 if (path == "") { parameters["name"] = inputDir + it->second; }
159 it = parameters.find("group");
160 //user has given a template file
161 if(it != parameters.end()){
162 path = m->hasPath(it->second);
163 //if the user has not given a path then, add inputdir. else leave path alone.
164 if (path == "") { parameters["group"] = inputDir + it->second; }
167 it = parameters.find("taxonomy");
168 //user has given a template file
169 if(it != parameters.end()){
170 path = m->hasPath(it->second);
171 //if the user has not given a path then, add inputdir. else leave path alone.
172 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
175 it = parameters.find("qfile");
176 //user has given a template file
177 if(it != parameters.end()){
178 path = m->hasPath(it->second);
179 //if the user has not given a path then, add inputdir. else leave path alone.
180 if (path == "") { parameters["qfile"] = inputDir + it->second; }
185 //check for required parameters
186 accnosfile = validParameter.validFile(parameters, "accnos", true);
187 if (accnosfile == "not open") { abort = true; }
188 else if (accnosfile == "not found") { accnosfile = ""; m->mothurOut("You must provide an accnos file."); m->mothurOutEndLine(); abort = true; }
190 if (accnosfile2 == "not found") { accnosfile2 = ""; }
192 fastafile = validParameter.validFile(parameters, "fasta", true);
193 if (fastafile == "not open") { abort = true; }
194 else if (fastafile == "not found") { fastafile = ""; }
196 namefile = validParameter.validFile(parameters, "name", true);
197 if (namefile == "not open") { abort = true; }
198 else if (namefile == "not found") { namefile = ""; }
200 groupfile = validParameter.validFile(parameters, "group", true);
201 if (groupfile == "not open") { abort = true; }
202 else if (groupfile == "not found") { groupfile = ""; }
204 alignfile = validParameter.validFile(parameters, "alignreport", true);
205 if (alignfile == "not open") { abort = true; }
206 else if (alignfile == "not found") { alignfile = ""; }
208 listfile = validParameter.validFile(parameters, "list", true);
209 if (listfile == "not open") { abort = true; }
210 else if (listfile == "not found") { listfile = ""; }
212 taxfile = validParameter.validFile(parameters, "taxonomy", true);
213 if (taxfile == "not open") { abort = true; }
214 else if (taxfile == "not found") { taxfile = ""; }
216 qualfile = validParameter.validFile(parameters, "qfile", true);
217 if (qualfile == "not open") { abort = true; }
218 else if (qualfile == "not found") { qualfile = ""; }
220 string usedDups = "true";
221 string temp = validParameter.validFile(parameters, "dups", false); if (temp == "not found") { temp = "false"; usedDups = ""; }
222 dups = m->isTrue(temp);
224 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; }
226 if ((usedDups != "") && (namefile == "")) { m->mothurOut("You may only use dups with the name option."); m->mothurOutEndLine(); abort = true; }
231 catch(exception& e) {
232 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
236 //**********************************************************************************************************************
238 void GetSeqsCommand::help(){
240 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");
241 m->mothurOut("It outputs a file containing only the sequences in the .accnos file.\n");
242 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");
243 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");
244 m->mothurOut("The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
245 m->mothurOut("Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
246 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
248 catch(exception& e) {
249 m->errorOut(e, "GetSeqsCommand", "help");
254 //**********************************************************************************************************************
256 int GetSeqsCommand::execute(){
259 if (abort == true) { if (calledHelp) { return 0; } return 2; }
261 //get names you want to keep
264 if (m->control_pressed) { return 0; }
266 //read through the correct file and output lines you want to keep
267 if (namefile != "") { readName(); }
268 if (fastafile != "") { readFasta(); }
269 if (groupfile != "") { readGroup(); }
270 if (alignfile != "") { readAlign(); }
271 if (listfile != "") { readList(); }
272 if (taxfile != "") { readTax(); }
273 if (qualfile != "") { readQual(); }
274 if (accnosfile2 != "") { compareAccnos(); }
276 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
278 m->mothurOut("Selected " + toString(names.size()) + " sequences."); m->mothurOutEndLine();
280 if (outputNames.size() != 0) {
281 m->mothurOutEndLine();
282 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
283 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
284 m->mothurOutEndLine();
290 catch(exception& e) {
291 m->errorOut(e, "GetSeqsCommand", "execute");
296 //**********************************************************************************************************************
297 int GetSeqsCommand::readFasta(){
299 string thisOutputDir = outputDir;
300 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
301 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
303 m->openOutputFile(outputFileName, out);
307 m->openInputFile(fastafile, in);
310 bool wroteSomething = false;
314 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
316 Sequence currSeq(in);
317 name = currSeq.getName();
320 //if this name is in the accnos file
321 if (names.count(name) != 0) {
322 wroteSomething = true;
324 currSeq.printSequence(out);
333 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
334 outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
339 catch(exception& e) {
340 m->errorOut(e, "GetSeqsCommand", "readFasta");
344 //**********************************************************************************************************************
345 int GetSeqsCommand::readQual(){
347 string thisOutputDir = outputDir;
348 if (outputDir == "") { thisOutputDir += m->hasPath(qualfile); }
349 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(qualfile)) + "pick" + m->getExtension(qualfile);
351 m->openOutputFile(outputFileName, out);
355 m->openInputFile(qualfile, in);
358 bool wroteSomething = false;
362 string saveName = "";
368 if (name.length() != 0) {
369 saveName = name.substr(1);
372 if (c == 10 || c == 13){ break; }
379 char letter= in.get();
380 if(letter == '>'){ in.putback(letter); break; }
381 else{ scores += letter; }
386 if (names.count(saveName) != 0) {
387 wroteSomething = true;
389 out << name << endl << scores;
398 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
399 outputNames.push_back(outputFileName); outputTypes["qfile"].push_back(outputFileName);
404 catch(exception& e) {
405 m->errorOut(e, "GetSeqsCommand", "readQual");
409 //**********************************************************************************************************************
410 int GetSeqsCommand::readList(){
412 string thisOutputDir = outputDir;
413 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
414 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
416 m->openOutputFile(outputFileName, out);
419 m->openInputFile(listfile, in);
421 bool wroteSomething = false;
425 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
427 //read in list vector
430 //make a new list vector
432 newList.setLabel(list.getLabel());
435 for (int i = 0; i < list.getNumBins(); i++) {
437 //parse out names that are in accnos file
438 string binnames = list.get(i);
440 string newNames = "";
441 while (binnames.find_first_of(',') != -1) {
442 string name = binnames.substr(0,binnames.find_first_of(','));
443 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
445 //if that name is in the .accnos file, add it
446 if (names.count(name) != 0) { newNames += name + ","; }
450 if (names.count(binnames) != 0) { newNames += binnames + ","; }
452 //if there are names in this bin add to new list
453 if (newNames != "") {
454 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
455 newList.push_back(newNames);
459 //print new listvector
460 if (newList.getNumBins() != 0) {
461 wroteSomething = true;
470 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
471 outputNames.push_back(outputFileName); outputTypes["list"].push_back(outputFileName);
476 catch(exception& e) {
477 m->errorOut(e, "GetSeqsCommand", "readList");
481 //**********************************************************************************************************************
482 int GetSeqsCommand::readName(){
484 string thisOutputDir = outputDir;
485 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
486 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
488 m->openOutputFile(outputFileName, out);
492 m->openInputFile(namefile, in);
493 string name, firstCol, secondCol;
495 bool wroteSomething = false;
500 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
506 if (dups) { hold = secondCol; }
508 vector<string> parsedNames;
509 //parse second column saving each name
510 while (secondCol.find_first_of(',') != -1) {
511 name = secondCol.substr(0,secondCol.find_first_of(','));
512 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
513 parsedNames.push_back(name);
516 //get name after last ,
517 parsedNames.push_back(secondCol);
519 vector<string> validSecond;
520 for (int i = 0; i < parsedNames.size(); i++) {
521 if (names.count(parsedNames[i]) != 0) {
522 validSecond.push_back(parsedNames[i]);
526 if ((dups) && (validSecond.size() != 0)) { //dups = true and we want to add someone, then add everyone
527 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
528 out << firstCol << '\t' << hold << endl;
529 wroteSomething = true;
531 //if the name in the first column is in the set then print it and any other names in second column also in set
532 if (names.count(firstCol) != 0) {
534 wroteSomething = true;
536 out << firstCol << '\t';
538 //you know you have at least one valid second since first column is valid
539 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
540 out << validSecond[validSecond.size()-1] << endl;
543 //make first name in set you come to first column and then add the remaining names to second column
545 //you want part of this row
546 if (validSecond.size() != 0) {
548 wroteSomething = true;
550 out << validSecond[0] << '\t';
552 //you know you have at least one valid second since first column is valid
553 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
554 out << validSecond[validSecond.size()-1] << endl;
563 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
564 outputNames.push_back(outputFileName); outputTypes["name"].push_back(outputFileName);
569 catch(exception& e) {
570 m->errorOut(e, "GetSeqsCommand", "readName");
575 //**********************************************************************************************************************
576 int GetSeqsCommand::readGroup(){
578 string thisOutputDir = outputDir;
579 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
580 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
582 m->openOutputFile(outputFileName, out);
586 m->openInputFile(groupfile, in);
589 bool wroteSomething = false;
593 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
596 in >> name; //read from first column
597 in >> group; //read from second column
599 //if this name is in the accnos file
600 if (names.count(name) != 0) {
601 wroteSomething = true;
603 out << name << '\t' << group << endl;
611 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
612 outputNames.push_back(outputFileName); outputTypes["group"].push_back(outputFileName);
617 catch(exception& e) {
618 m->errorOut(e, "GetSeqsCommand", "readGroup");
622 //**********************************************************************************************************************
623 int GetSeqsCommand::readTax(){
625 string thisOutputDir = outputDir;
626 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
627 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
629 m->openOutputFile(outputFileName, out);
632 m->openInputFile(taxfile, in);
635 bool wroteSomething = false;
639 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
641 in >> name; //read from first column
642 in >> tax; //read from second column
644 //if this name is in the accnos file
645 if (names.count(name) != 0) {
646 wroteSomething = true;
648 out << name << '\t' << tax << endl;
656 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
657 outputNames.push_back(outputFileName); outputTypes["taxonomy"].push_back(outputFileName);
662 catch(exception& e) {
663 m->errorOut(e, "GetSeqsCommand", "readTax");
667 //**********************************************************************************************************************
668 //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
669 int GetSeqsCommand::readAlign(){
671 string thisOutputDir = outputDir;
672 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
673 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
675 m->openOutputFile(outputFileName, out);
679 m->openInputFile(alignfile, in);
682 bool wroteSomething = false;
684 //read column headers
685 for (int i = 0; i < 16; i++) {
686 if (!in.eof()) { in >> junk; out << junk << '\t'; }
693 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
696 in >> name; //read from first column
698 //if this name is in the accnos file
699 if (names.count(name) != 0) {
700 wroteSomething = true;
705 for (int i = 0; i < 15; i++) {
706 if (!in.eof()) { in >> junk; out << junk << '\t'; }
711 }else {//still read just don't do anything with it
713 for (int i = 0; i < 15; i++) {
714 if (!in.eof()) { in >> junk; }
724 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
725 outputNames.push_back(outputFileName); outputTypes["alignreport"].push_back(outputFileName);
730 catch(exception& e) {
731 m->errorOut(e, "GetSeqsCommand", "readAlign");
735 //**********************************************************************************************************************
737 int GetSeqsCommand::readAccnos(){
741 m->openInputFile(accnosfile, in);
756 catch(exception& e) {
757 m->errorOut(e, "GetSeqsCommand", "readAccnos");
761 //**********************************************************************************************************************
763 int GetSeqsCommand::compareAccnos(){
766 string thisOutputDir = outputDir;
767 if (outputDir == "") { thisOutputDir += m->hasPath(accnosfile); }
768 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(accnosfile)) + "accnos.report";
770 m->openOutputFile(outputFileName, out);
773 m->openInputFile(accnosfile2, in);
776 set<string> namesAccnos2;
777 set<string> namesDups;
778 set<string> namesAccnos = names;
780 map<string, int> nameCount;
782 if (namefile != "") {
784 m->openInputFile(namefile, inName);
787 while(!inName.eof()){
789 if (m->control_pressed) { inName.close(); return 0; }
791 string thisname, repnames;
793 inName >> thisname; m->gobble(inName); //read from first column
794 inName >> repnames; //read from second column
796 int num = m->getNumNames(repnames);
797 nameCount[thisname] = num;
807 if (namesAccnos.count(name) == 0){ //name unique to accnos2
808 namesAccnos2.insert(name);
809 }else { //you are in both so erase
810 namesAccnos.erase(name);
811 namesDups.insert(name);
818 out << "Names in both files : " + toString(namesDups.size()) << endl;
819 m->mothurOut("Names in both files : " + toString(namesDups.size())); m->mothurOutEndLine();
821 for (set<string>::iterator it = namesDups.begin(); it != namesDups.end(); it++) {
823 if (namefile != "") { out << '\t' << nameCount[(*it)]; }
827 out << "Names unique to " + accnosfile + " : " + toString(namesAccnos.size()) << endl;
828 m->mothurOut("Names unique to " + accnosfile + " : " + toString(namesAccnos.size())); m->mothurOutEndLine();
830 for (set<string>::iterator it = namesAccnos.begin(); it != namesAccnos.end(); it++) {
832 if (namefile != "") { out << '\t' << nameCount[(*it)]; }
836 out << "Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size()) << endl;
837 m->mothurOut("Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size())); m->mothurOutEndLine();
839 for (set<string>::iterator it = namesAccnos2.begin(); it != namesAccnos2.end(); it++) {
841 if (namefile != "") { out << '\t' << nameCount[(*it)]; }
847 outputNames.push_back(outputFileName); outputTypes["accnosreport"].push_back(outputFileName);
852 catch(exception& e) {
853 m->errorOut(e, "GetSeqsCommand", "readAccnos");
859 //**********************************************************************************************************************