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; }
193 if (accnosfile2 == "not found") { accnosfile2 = ""; }
195 fastafile = validParameter.validFile(parameters, "fasta", true);
196 if (fastafile == "not open") { abort = true; }
197 else if (fastafile == "not found") { fastafile = ""; }
199 namefile = validParameter.validFile(parameters, "name", true);
200 if (namefile == "not open") { abort = true; }
201 else if (namefile == "not found") { namefile = ""; }
203 groupfile = validParameter.validFile(parameters, "group", true);
204 if (groupfile == "not open") { abort = true; }
205 else if (groupfile == "not found") { groupfile = ""; }
207 alignfile = validParameter.validFile(parameters, "alignreport", true);
208 if (alignfile == "not open") { abort = true; }
209 else if (alignfile == "not found") { alignfile = ""; }
211 listfile = validParameter.validFile(parameters, "list", true);
212 if (listfile == "not open") { abort = true; }
213 else if (listfile == "not found") { listfile = ""; }
215 taxfile = validParameter.validFile(parameters, "taxonomy", true);
216 if (taxfile == "not open") { abort = true; }
217 else if (taxfile == "not found") { taxfile = ""; }
219 qualfile = validParameter.validFile(parameters, "qfile", true);
220 if (qualfile == "not open") { abort = true; }
221 else if (qualfile == "not found") { qualfile = ""; }
223 string usedDups = "true";
224 string temp = validParameter.validFile(parameters, "dups", false); if (temp == "not found") { temp = "false"; usedDups = ""; }
225 dups = m->isTrue(temp);
227 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; }
229 if ((usedDups != "") && (namefile == "")) { m->mothurOut("You may only use dups with the name option."); m->mothurOutEndLine(); abort = true; }
234 catch(exception& e) {
235 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
239 //**********************************************************************************************************************
241 void GetSeqsCommand::help(){
243 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");
244 m->mothurOut("It outputs a file containing only the sequences in the .accnos file.\n");
245 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");
246 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");
247 m->mothurOut("The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
248 m->mothurOut("Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
249 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
251 catch(exception& e) {
252 m->errorOut(e, "GetSeqsCommand", "help");
257 //**********************************************************************************************************************
259 int GetSeqsCommand::execute(){
262 if (abort == true) { return 0; }
264 //get names you want to keep
267 if (m->control_pressed) { return 0; }
269 //read through the correct file and output lines you want to keep
270 if (namefile != "") { readName(); }
271 if (fastafile != "") { readFasta(); }
272 if (groupfile != "") { readGroup(); }
273 if (alignfile != "") { readAlign(); }
274 if (listfile != "") { readList(); }
275 if (taxfile != "") { readTax(); }
276 if (qualfile != "") { readQual(); }
277 if (accnosfile2 != "") { compareAccnos(); }
279 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
281 m->mothurOut("Selected " + toString(names.size()) + " sequences."); m->mothurOutEndLine();
283 if (outputNames.size() != 0) {
284 m->mothurOutEndLine();
285 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
286 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
287 m->mothurOutEndLine();
293 catch(exception& e) {
294 m->errorOut(e, "GetSeqsCommand", "execute");
299 //**********************************************************************************************************************
300 int GetSeqsCommand::readFasta(){
302 string thisOutputDir = outputDir;
303 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
304 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
306 m->openOutputFile(outputFileName, out);
310 m->openInputFile(fastafile, in);
313 bool wroteSomething = false;
317 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
319 Sequence currSeq(in);
320 name = currSeq.getName();
323 //if this name is in the accnos file
324 if (names.count(name) != 0) {
325 wroteSomething = true;
327 currSeq.printSequence(out);
336 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
337 outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
342 catch(exception& e) {
343 m->errorOut(e, "GetSeqsCommand", "readFasta");
347 //**********************************************************************************************************************
348 int GetSeqsCommand::readQual(){
350 string thisOutputDir = outputDir;
351 if (outputDir == "") { thisOutputDir += m->hasPath(qualfile); }
352 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(qualfile)) + "pick" + m->getExtension(qualfile);
354 m->openOutputFile(outputFileName, out);
358 m->openInputFile(qualfile, in);
361 bool wroteSomething = false;
365 string saveName = "";
371 if (name.length() != 0) {
372 saveName = name.substr(1);
375 if (c == 10 || c == 13){ break; }
382 char letter= in.get();
383 if(letter == '>'){ in.putback(letter); break; }
384 else{ scores += letter; }
389 if (names.count(saveName) != 0) {
390 wroteSomething = true;
392 out << name << endl << scores;
401 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
402 outputNames.push_back(outputFileName); outputTypes["qfile"].push_back(outputFileName);
407 catch(exception& e) {
408 m->errorOut(e, "GetSeqsCommand", "readQual");
412 //**********************************************************************************************************************
413 int GetSeqsCommand::readList(){
415 string thisOutputDir = outputDir;
416 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
417 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
419 m->openOutputFile(outputFileName, out);
422 m->openInputFile(listfile, in);
424 bool wroteSomething = false;
428 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
430 //read in list vector
433 //make a new list vector
435 newList.setLabel(list.getLabel());
438 for (int i = 0; i < list.getNumBins(); i++) {
440 //parse out names that are in accnos file
441 string binnames = list.get(i);
443 string newNames = "";
444 while (binnames.find_first_of(',') != -1) {
445 string name = binnames.substr(0,binnames.find_first_of(','));
446 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
448 //if that name is in the .accnos file, add it
449 if (names.count(name) != 0) { newNames += name + ","; }
453 if (names.count(binnames) != 0) { newNames += binnames + ","; }
455 //if there are names in this bin add to new list
456 if (newNames != "") {
457 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
458 newList.push_back(newNames);
462 //print new listvector
463 if (newList.getNumBins() != 0) {
464 wroteSomething = true;
473 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
474 outputNames.push_back(outputFileName); outputTypes["list"].push_back(outputFileName);
479 catch(exception& e) {
480 m->errorOut(e, "GetSeqsCommand", "readList");
484 //**********************************************************************************************************************
485 int GetSeqsCommand::readName(){
487 string thisOutputDir = outputDir;
488 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
489 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
491 m->openOutputFile(outputFileName, out);
495 m->openInputFile(namefile, in);
496 string name, firstCol, secondCol;
498 bool wroteSomething = false;
503 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
509 if (dups) { hold = secondCol; }
511 vector<string> parsedNames;
512 //parse second column saving each name
513 while (secondCol.find_first_of(',') != -1) {
514 name = secondCol.substr(0,secondCol.find_first_of(','));
515 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
516 parsedNames.push_back(name);
519 //get name after last ,
520 parsedNames.push_back(secondCol);
522 vector<string> validSecond;
523 for (int i = 0; i < parsedNames.size(); i++) {
524 if (names.count(parsedNames[i]) != 0) {
525 validSecond.push_back(parsedNames[i]);
529 if ((dups) && (validSecond.size() != 0)) { //dups = true and we want to add someone, then add everyone
530 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
531 out << firstCol << '\t' << hold << endl;
532 wroteSomething = true;
534 //if the name in the first column is in the set then print it and any other names in second column also in set
535 if (names.count(firstCol) != 0) {
537 wroteSomething = true;
539 out << firstCol << '\t';
541 //you know you have at least one valid second since first column is valid
542 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
543 out << validSecond[validSecond.size()-1] << endl;
546 //make first name in set you come to first column and then add the remaining names to second column
548 //you want part of this row
549 if (validSecond.size() != 0) {
551 wroteSomething = true;
553 out << validSecond[0] << '\t';
555 //you know you have at least one valid second since first column is valid
556 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
557 out << validSecond[validSecond.size()-1] << endl;
566 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
567 outputNames.push_back(outputFileName); outputTypes["name"].push_back(outputFileName);
572 catch(exception& e) {
573 m->errorOut(e, "GetSeqsCommand", "readName");
578 //**********************************************************************************************************************
579 int GetSeqsCommand::readGroup(){
581 string thisOutputDir = outputDir;
582 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
583 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
585 m->openOutputFile(outputFileName, out);
589 m->openInputFile(groupfile, in);
592 bool wroteSomething = false;
596 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
599 in >> name; //read from first column
600 in >> group; //read from second column
602 //if this name is in the accnos file
603 if (names.count(name) != 0) {
604 wroteSomething = true;
606 out << name << '\t' << group << endl;
614 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
615 outputNames.push_back(outputFileName); outputTypes["group"].push_back(outputFileName);
620 catch(exception& e) {
621 m->errorOut(e, "GetSeqsCommand", "readGroup");
625 //**********************************************************************************************************************
626 int GetSeqsCommand::readTax(){
628 string thisOutputDir = outputDir;
629 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
630 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
632 m->openOutputFile(outputFileName, out);
635 m->openInputFile(taxfile, in);
638 bool wroteSomething = false;
642 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
644 in >> name; //read from first column
645 in >> tax; //read from second column
647 //if this name is in the accnos file
648 if (names.count(name) != 0) {
649 wroteSomething = true;
651 out << name << '\t' << tax << endl;
659 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
660 outputNames.push_back(outputFileName); outputTypes["taxonomy"].push_back(outputFileName);
665 catch(exception& e) {
666 m->errorOut(e, "GetSeqsCommand", "readTax");
670 //**********************************************************************************************************************
671 //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
672 int GetSeqsCommand::readAlign(){
674 string thisOutputDir = outputDir;
675 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
676 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
678 m->openOutputFile(outputFileName, out);
682 m->openInputFile(alignfile, in);
685 bool wroteSomething = false;
687 //read column headers
688 for (int i = 0; i < 16; i++) {
689 if (!in.eof()) { in >> junk; out << junk << '\t'; }
696 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
699 in >> name; //read from first column
701 //if this name is in the accnos file
702 if (names.count(name) != 0) {
703 wroteSomething = true;
708 for (int i = 0; i < 15; i++) {
709 if (!in.eof()) { in >> junk; out << junk << '\t'; }
714 }else {//still read just don't do anything with it
716 for (int i = 0; i < 15; i++) {
717 if (!in.eof()) { in >> junk; }
727 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
728 outputNames.push_back(outputFileName); outputTypes["alignreport"].push_back(outputFileName);
733 catch(exception& e) {
734 m->errorOut(e, "GetSeqsCommand", "readAlign");
738 //**********************************************************************************************************************
740 int GetSeqsCommand::readAccnos(){
744 m->openInputFile(accnosfile, in);
759 catch(exception& e) {
760 m->errorOut(e, "GetSeqsCommand", "readAccnos");
764 //**********************************************************************************************************************
766 int GetSeqsCommand::compareAccnos(){
769 string thisOutputDir = outputDir;
770 if (outputDir == "") { thisOutputDir += m->hasPath(accnosfile); }
771 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(accnosfile)) + "accnos.report";
773 m->openOutputFile(outputFileName, out);
776 m->openInputFile(accnosfile2, in);
779 set<string> namesAccnos2;
780 set<string> namesDups;
781 set<string> namesAccnos = names;
783 map<string, int> nameCount;
785 if (namefile != "") {
787 m->openInputFile(namefile, inName);
790 while(!inName.eof()){
792 if (m->control_pressed) { inName.close(); return 0; }
794 string thisname, repnames;
796 inName >> thisname; m->gobble(inName); //read from first column
797 inName >> repnames; //read from second column
799 int num = m->getNumNames(repnames);
800 nameCount[thisname] = num;
810 if (namesAccnos.count(name) == 0){ //name unique to accnos2
811 namesAccnos2.insert(name);
812 }else { //you are in both so erase
813 namesAccnos.erase(name);
814 namesDups.insert(name);
821 out << "Names in both files : " + toString(namesDups.size()) << endl;
822 m->mothurOut("Names in both files : " + toString(namesDups.size())); m->mothurOutEndLine();
824 for (set<string>::iterator it = namesDups.begin(); it != namesDups.end(); it++) {
826 if (namefile != "") { out << '\t' << nameCount[(*it)]; }
830 out << "Names unique to " + accnosfile + " : " + toString(namesAccnos.size()) << endl;
831 m->mothurOut("Names unique to " + accnosfile + " : " + toString(namesAccnos.size())); m->mothurOutEndLine();
833 for (set<string>::iterator it = namesAccnos.begin(); it != namesAccnos.end(); it++) {
835 if (namefile != "") { out << '\t' << nameCount[(*it)]; }
839 out << "Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size()) << endl;
840 m->mothurOut("Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size())); m->mothurOutEndLine();
842 for (set<string>::iterator it = namesAccnos2.begin(); it != namesAccnos2.end(); it++) {
844 if (namefile != "") { out << '\t' << nameCount[(*it)]; }
850 outputNames.push_back(outputFileName); outputTypes["accnosreport"].push_back(outputFileName);
855 catch(exception& e) {
856 m->errorOut(e, "GetSeqsCommand", "readAccnos");
862 //**********************************************************************************************************************