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", "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;
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) {
73 //allow user to run help
74 if(option == "help") { help(); abort = true; }
77 //valid paramters for this command
78 string Array[] = {"fasta","name", "group", "alignreport", "qfile", "accnos", "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;
102 //if the user changes the output directory command factory will send this info to us in the output parameter
103 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
105 //if the user changes the input directory command factory will send this info to us in the output parameter
106 string inputDir = validParameter.validFile(parameters, "inputdir", false);
107 if (inputDir == "not found"){ inputDir = ""; }
110 it = parameters.find("alignreport");
111 //user has given a template file
112 if(it != parameters.end()){
113 path = m->hasPath(it->second);
114 //if the user has not given a path then, add inputdir. else leave path alone.
115 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
118 it = parameters.find("fasta");
119 //user has given a template file
120 if(it != parameters.end()){
121 path = m->hasPath(it->second);
122 //if the user has not given a path then, add inputdir. else leave path alone.
123 if (path == "") { parameters["fasta"] = inputDir + it->second; }
126 it = parameters.find("accnos");
127 //user has given a template file
128 if(it != parameters.end()){
129 path = m->hasPath(it->second);
130 //if the user has not given a path then, add inputdir. else leave path alone.
131 if (path == "") { parameters["accnos"] = inputDir + it->second; }
134 it = parameters.find("list");
135 //user has given a template file
136 if(it != parameters.end()){
137 path = m->hasPath(it->second);
138 //if the user has not given a path then, add inputdir. else leave path alone.
139 if (path == "") { parameters["list"] = inputDir + it->second; }
142 it = parameters.find("name");
143 //user has given a template file
144 if(it != parameters.end()){
145 path = m->hasPath(it->second);
146 //if the user has not given a path then, add inputdir. else leave path alone.
147 if (path == "") { parameters["name"] = inputDir + it->second; }
150 it = parameters.find("group");
151 //user has given a template file
152 if(it != parameters.end()){
153 path = m->hasPath(it->second);
154 //if the user has not given a path then, add inputdir. else leave path alone.
155 if (path == "") { parameters["group"] = inputDir + it->second; }
158 it = parameters.find("taxonomy");
159 //user has given a template file
160 if(it != parameters.end()){
161 path = m->hasPath(it->second);
162 //if the user has not given a path then, add inputdir. else leave path alone.
163 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
166 it = parameters.find("qfile");
167 //user has given a template file
168 if(it != parameters.end()){
169 path = m->hasPath(it->second);
170 //if the user has not given a path then, add inputdir. else leave path alone.
171 if (path == "") { parameters["qfile"] = inputDir + it->second; }
176 //check for required parameters
177 accnosfile = validParameter.validFile(parameters, "accnos", true);
178 if (accnosfile == "not open") { abort = true; }
179 else if (accnosfile == "not found") { accnosfile = ""; m->mothurOut("You must provide an accnos file."); m->mothurOutEndLine(); abort = true; }
181 fastafile = validParameter.validFile(parameters, "fasta", true);
182 if (fastafile == "not open") { abort = true; }
183 else if (fastafile == "not found") { fastafile = ""; }
185 namefile = validParameter.validFile(parameters, "name", true);
186 if (namefile == "not open") { abort = true; }
187 else if (namefile == "not found") { namefile = ""; }
189 groupfile = validParameter.validFile(parameters, "group", true);
190 if (groupfile == "not open") { abort = true; }
191 else if (groupfile == "not found") { groupfile = ""; }
193 alignfile = validParameter.validFile(parameters, "alignreport", true);
194 if (alignfile == "not open") { abort = true; }
195 else if (alignfile == "not found") { alignfile = ""; }
197 listfile = validParameter.validFile(parameters, "list", true);
198 if (listfile == "not open") { abort = true; }
199 else if (listfile == "not found") { listfile = ""; }
201 taxfile = validParameter.validFile(parameters, "taxonomy", true);
202 if (taxfile == "not open") { abort = true; }
203 else if (taxfile == "not found") { taxfile = ""; }
205 qualfile = validParameter.validFile(parameters, "qfile", true);
206 if (qualfile == "not open") { abort = true; }
207 else if (qualfile == "not found") { qualfile = ""; }
209 string usedDups = "true";
210 string temp = validParameter.validFile(parameters, "dups", false); if (temp == "not found") { temp = "false"; usedDups = ""; }
211 dups = m->isTrue(temp);
213 if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == "") && (taxfile == "") && (qualfile == "")) { m->mothurOut("You must provide one of the following: fasta, name, group, alignreport, taxonomy, quality or listfile."); m->mothurOutEndLine(); abort = true; }
215 if ((usedDups != "") && (namefile == "")) { m->mothurOut("You may only use dups with the name option."); m->mothurOutEndLine(); abort = true; }
220 catch(exception& e) {
221 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
225 //**********************************************************************************************************************
227 void GetSeqsCommand::help(){
229 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");
230 m->mothurOut("It outputs a file containing only the sequences in the .accnos file.\n");
231 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");
232 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");
233 m->mothurOut("The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
234 m->mothurOut("Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
235 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
237 catch(exception& e) {
238 m->errorOut(e, "GetSeqsCommand", "help");
243 //**********************************************************************************************************************
245 int GetSeqsCommand::execute(){
248 if (abort == true) { return 0; }
250 //get names you want to keep
253 if (m->control_pressed) { return 0; }
255 //read through the correct file and output lines you want to keep
256 if (namefile != "") { readName(); }
257 if (fastafile != "") { readFasta(); }
258 if (groupfile != "") { readGroup(); }
259 if (alignfile != "") { readAlign(); }
260 if (listfile != "") { readList(); }
261 if (taxfile != "") { readTax(); }
262 if (qualfile != "") { readQual(); }
264 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
266 m->mothurOut("Selected " + toString(names.size()) + " sequences."); m->mothurOutEndLine();
268 if (outputNames.size() != 0) {
269 m->mothurOutEndLine();
270 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
271 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
272 m->mothurOutEndLine();
278 catch(exception& e) {
279 m->errorOut(e, "GetSeqsCommand", "execute");
284 //**********************************************************************************************************************
285 int GetSeqsCommand::readFasta(){
287 string thisOutputDir = outputDir;
288 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
289 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
291 m->openOutputFile(outputFileName, out);
295 m->openInputFile(fastafile, in);
298 bool wroteSomething = false;
302 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
304 Sequence currSeq(in);
305 name = currSeq.getName();
308 //if this name is in the accnos file
309 if (names.count(name) != 0) {
310 wroteSomething = true;
312 currSeq.printSequence(out);
321 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
322 outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
327 catch(exception& e) {
328 m->errorOut(e, "GetSeqsCommand", "readFasta");
332 //**********************************************************************************************************************
333 int GetSeqsCommand::readQual(){
335 string thisOutputDir = outputDir;
336 if (outputDir == "") { thisOutputDir += m->hasPath(qualfile); }
337 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(qualfile)) + "pick" + m->getExtension(qualfile);
339 m->openOutputFile(outputFileName, out);
343 m->openInputFile(qualfile, in);
346 bool wroteSomething = false;
350 string saveName = "";
356 if (name.length() != 0) {
357 saveName = name.substr(1);
360 if (c == 10 || c == 13){ break; }
367 char letter= in.get();
368 if(letter == '>'){ in.putback(letter); break; }
369 else{ scores += letter; }
374 if (names.count(saveName) != 0) {
375 wroteSomething = true;
377 out << name << endl << scores;
386 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
387 outputNames.push_back(outputFileName); outputTypes["qfile"].push_back(outputFileName);
392 catch(exception& e) {
393 m->errorOut(e, "GetSeqsCommand", "readQual");
397 //**********************************************************************************************************************
398 int GetSeqsCommand::readList(){
400 string thisOutputDir = outputDir;
401 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
402 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
404 m->openOutputFile(outputFileName, out);
407 m->openInputFile(listfile, in);
409 bool wroteSomething = false;
413 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
415 //read in list vector
418 //make a new list vector
420 newList.setLabel(list.getLabel());
423 for (int i = 0; i < list.getNumBins(); i++) {
425 //parse out names that are in accnos file
426 string binnames = list.get(i);
428 string newNames = "";
429 while (binnames.find_first_of(',') != -1) {
430 string name = binnames.substr(0,binnames.find_first_of(','));
431 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
433 //if that name is in the .accnos file, add it
434 if (names.count(name) != 0) { newNames += name + ","; }
438 if (names.count(binnames) != 0) { newNames += binnames + ","; }
440 //if there are names in this bin add to new list
441 if (newNames != "") {
442 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
443 newList.push_back(newNames);
447 //print new listvector
448 if (newList.getNumBins() != 0) {
449 wroteSomething = true;
458 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
459 outputNames.push_back(outputFileName); outputTypes["list"].push_back(outputFileName);
464 catch(exception& e) {
465 m->errorOut(e, "GetSeqsCommand", "readList");
469 //**********************************************************************************************************************
470 int GetSeqsCommand::readName(){
472 string thisOutputDir = outputDir;
473 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
474 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
476 m->openOutputFile(outputFileName, out);
480 m->openInputFile(namefile, in);
481 string name, firstCol, secondCol;
483 bool wroteSomething = false;
488 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
494 if (dups) { hold = secondCol; }
496 vector<string> parsedNames;
497 //parse second column saving each name
498 while (secondCol.find_first_of(',') != -1) {
499 name = secondCol.substr(0,secondCol.find_first_of(','));
500 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
501 parsedNames.push_back(name);
504 //get name after last ,
505 parsedNames.push_back(secondCol);
507 vector<string> validSecond;
508 for (int i = 0; i < parsedNames.size(); i++) {
509 if (names.count(parsedNames[i]) != 0) {
510 validSecond.push_back(parsedNames[i]);
514 if ((dups) && (validSecond.size() != 0)) { //dups = true and we want to add someone, then add everyone
515 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
516 out << firstCol << '\t' << hold << endl;
517 wroteSomething = true;
519 //if the name in the first column is in the set then print it and any other names in second column also in set
520 if (names.count(firstCol) != 0) {
522 wroteSomething = true;
524 out << firstCol << '\t';
526 //you know you have at least one valid second since first column is valid
527 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
528 out << validSecond[validSecond.size()-1] << endl;
531 //make first name in set you come to first column and then add the remaining names to second column
533 //you want part of this row
534 if (validSecond.size() != 0) {
536 wroteSomething = true;
538 out << validSecond[0] << '\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;
551 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
552 outputNames.push_back(outputFileName); outputTypes["name"].push_back(outputFileName);
557 catch(exception& e) {
558 m->errorOut(e, "GetSeqsCommand", "readName");
563 //**********************************************************************************************************************
564 int GetSeqsCommand::readGroup(){
566 string thisOutputDir = outputDir;
567 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
568 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
570 m->openOutputFile(outputFileName, out);
574 m->openInputFile(groupfile, in);
577 bool wroteSomething = false;
581 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
584 in >> name; //read from first column
585 in >> group; //read from second column
587 //if this name is in the accnos file
588 if (names.count(name) != 0) {
589 wroteSomething = true;
591 out << name << '\t' << group << endl;
599 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
600 outputNames.push_back(outputFileName); outputTypes["group"].push_back(outputFileName);
605 catch(exception& e) {
606 m->errorOut(e, "GetSeqsCommand", "readGroup");
610 //**********************************************************************************************************************
611 int GetSeqsCommand::readTax(){
613 string thisOutputDir = outputDir;
614 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
615 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
617 m->openOutputFile(outputFileName, out);
620 m->openInputFile(taxfile, in);
623 bool wroteSomething = false;
627 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
629 in >> name; //read from first column
630 in >> tax; //read from second column
632 //if this name is in the accnos file
633 if (names.count(name) != 0) {
634 wroteSomething = true;
636 out << name << '\t' << tax << endl;
644 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
645 outputNames.push_back(outputFileName); outputTypes["taxonomy"].push_back(outputFileName);
650 catch(exception& e) {
651 m->errorOut(e, "GetSeqsCommand", "readTax");
655 //**********************************************************************************************************************
656 //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
657 int GetSeqsCommand::readAlign(){
659 string thisOutputDir = outputDir;
660 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
661 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
663 m->openOutputFile(outputFileName, out);
667 m->openInputFile(alignfile, in);
670 bool wroteSomething = false;
672 //read column headers
673 for (int i = 0; i < 16; i++) {
674 if (!in.eof()) { in >> junk; out << junk << '\t'; }
681 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
684 in >> name; //read from first column
686 //if this name is in the accnos file
687 if (names.count(name) != 0) {
688 wroteSomething = true;
693 for (int i = 0; i < 15; i++) {
694 if (!in.eof()) { in >> junk; out << junk << '\t'; }
699 }else {//still read just don't do anything with it
701 for (int i = 0; i < 15; i++) {
702 if (!in.eof()) { in >> junk; }
712 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
713 outputNames.push_back(outputFileName); outputTypes["alignreport"].push_back(outputFileName);
718 catch(exception& e) {
719 m->errorOut(e, "GetSeqsCommand", "readAlign");
723 //**********************************************************************************************************************
725 int GetSeqsCommand::readAccnos(){
729 m->openInputFile(accnosfile, in);
744 catch(exception& e) {
745 m->errorOut(e, "GetSeqsCommand", "readAccnos");
750 //**********************************************************************************************************************