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 if (outputNames.size() != 0) {
267 m->mothurOutEndLine();
268 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
269 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
270 m->mothurOutEndLine();
276 catch(exception& e) {
277 m->errorOut(e, "GetSeqsCommand", "execute");
282 //**********************************************************************************************************************
283 int GetSeqsCommand::readFasta(){
285 string thisOutputDir = outputDir;
286 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
287 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
289 m->openOutputFile(outputFileName, out);
293 m->openInputFile(fastafile, in);
296 bool wroteSomething = false;
300 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
302 Sequence currSeq(in);
303 name = currSeq.getName();
306 //if this name is in the accnos file
307 if (names.count(name) != 0) {
308 wroteSomething = true;
310 currSeq.printSequence(out);
319 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
320 outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
325 catch(exception& e) {
326 m->errorOut(e, "GetSeqsCommand", "readFasta");
330 //**********************************************************************************************************************
331 int GetSeqsCommand::readQual(){
333 string thisOutputDir = outputDir;
334 if (outputDir == "") { thisOutputDir += m->hasPath(qualfile); }
335 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(qualfile)) + "pick" + m->getExtension(qualfile);
337 m->openOutputFile(outputFileName, out);
341 m->openInputFile(qualfile, in);
344 bool wroteSomething = false;
348 string saveName = "";
354 if (name.length() != 0) {
355 saveName = name.substr(1);
358 if (c == 10 || c == 13){ break; }
365 char letter= in.get();
366 if(letter == '>'){ in.putback(letter); break; }
367 else{ scores += letter; }
372 if (names.count(saveName) != 0) {
373 wroteSomething = true;
375 out << name << endl << scores;
384 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
385 outputNames.push_back(outputFileName); outputTypes["qfile"].push_back(outputFileName);
390 catch(exception& e) {
391 m->errorOut(e, "GetSeqsCommand", "readQual");
395 //**********************************************************************************************************************
396 int GetSeqsCommand::readList(){
398 string thisOutputDir = outputDir;
399 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
400 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
402 m->openOutputFile(outputFileName, out);
405 m->openInputFile(listfile, in);
407 bool wroteSomething = false;
411 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
413 //read in list vector
416 //make a new list vector
418 newList.setLabel(list.getLabel());
421 for (int i = 0; i < list.getNumBins(); i++) {
423 //parse out names that are in accnos file
424 string binnames = list.get(i);
426 string newNames = "";
427 while (binnames.find_first_of(',') != -1) {
428 string name = binnames.substr(0,binnames.find_first_of(','));
429 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
431 //if that name is in the .accnos file, add it
432 if (names.count(name) != 0) { newNames += name + ","; }
436 if (names.count(binnames) != 0) { newNames += binnames + ","; }
438 //if there are names in this bin add to new list
439 if (newNames != "") {
440 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
441 newList.push_back(newNames);
445 //print new listvector
446 if (newList.getNumBins() != 0) {
447 wroteSomething = true;
456 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
457 outputNames.push_back(outputFileName); outputTypes["list"].push_back(outputFileName);
462 catch(exception& e) {
463 m->errorOut(e, "GetSeqsCommand", "readList");
467 //**********************************************************************************************************************
468 int GetSeqsCommand::readName(){
470 string thisOutputDir = outputDir;
471 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
472 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
474 m->openOutputFile(outputFileName, out);
478 m->openInputFile(namefile, in);
479 string name, firstCol, secondCol;
481 bool wroteSomething = false;
486 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
492 if (dups) { hold = secondCol; }
494 vector<string> parsedNames;
495 //parse second column saving each name
496 while (secondCol.find_first_of(',') != -1) {
497 name = secondCol.substr(0,secondCol.find_first_of(','));
498 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
499 parsedNames.push_back(name);
502 //get name after last ,
503 parsedNames.push_back(secondCol);
505 vector<string> validSecond;
506 for (int i = 0; i < parsedNames.size(); i++) {
507 if (names.count(parsedNames[i]) != 0) {
508 validSecond.push_back(parsedNames[i]);
512 if ((dups) && (validSecond.size() != 0)) { //dups = true and we want to add someone, then add everyone
513 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
514 out << firstCol << '\t' << hold << endl;
515 wroteSomething = true;
517 //if the name in the first column is in the set then print it and any other names in second column also in set
518 if (names.count(firstCol) != 0) {
520 wroteSomething = true;
522 out << firstCol << '\t';
524 //you know you have at least one valid second since first column is valid
525 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
526 out << validSecond[validSecond.size()-1] << endl;
529 //make first name in set you come to first column and then add the remaining names to second column
531 //you want part of this row
532 if (validSecond.size() != 0) {
534 wroteSomething = true;
536 out << validSecond[0] << '\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;
549 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
550 outputNames.push_back(outputFileName); outputTypes["name"].push_back(outputFileName);
555 catch(exception& e) {
556 m->errorOut(e, "GetSeqsCommand", "readName");
561 //**********************************************************************************************************************
562 int GetSeqsCommand::readGroup(){
564 string thisOutputDir = outputDir;
565 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
566 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
568 m->openOutputFile(outputFileName, out);
572 m->openInputFile(groupfile, in);
575 bool wroteSomething = false;
579 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
582 in >> name; //read from first column
583 in >> group; //read from second column
585 //if this name is in the accnos file
586 if (names.count(name) != 0) {
587 wroteSomething = true;
589 out << name << '\t' << group << endl;
597 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
598 outputNames.push_back(outputFileName); outputTypes["group"].push_back(outputFileName);
603 catch(exception& e) {
604 m->errorOut(e, "GetSeqsCommand", "readGroup");
608 //**********************************************************************************************************************
609 int GetSeqsCommand::readTax(){
611 string thisOutputDir = outputDir;
612 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
613 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
615 m->openOutputFile(outputFileName, out);
618 m->openInputFile(taxfile, in);
621 bool wroteSomething = false;
625 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
627 in >> name; //read from first column
628 in >> tax; //read from second column
630 //if this name is in the accnos file
631 if (names.count(name) != 0) {
632 wroteSomething = true;
634 out << name << '\t' << tax << endl;
642 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
643 outputNames.push_back(outputFileName); outputTypes["taxonomy"].push_back(outputFileName);
648 catch(exception& e) {
649 m->errorOut(e, "GetSeqsCommand", "readTax");
653 //**********************************************************************************************************************
654 //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
655 int GetSeqsCommand::readAlign(){
657 string thisOutputDir = outputDir;
658 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
659 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
661 m->openOutputFile(outputFileName, out);
665 m->openInputFile(alignfile, in);
668 bool wroteSomething = false;
670 //read column headers
671 for (int i = 0; i < 16; i++) {
672 if (!in.eof()) { in >> junk; out << junk << '\t'; }
679 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
682 in >> name; //read from first column
684 //if this name is in the accnos file
685 if (names.count(name) != 0) {
686 wroteSomething = true;
691 for (int i = 0; i < 15; i++) {
692 if (!in.eof()) { in >> junk; out << junk << '\t'; }
697 }else {//still read just don't do anything with it
699 for (int i = 0; i < 15; i++) {
700 if (!in.eof()) { in >> junk; }
710 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
711 outputNames.push_back(outputFileName); outputTypes["alignreport"].push_back(outputFileName);
716 catch(exception& e) {
717 m->errorOut(e, "GetSeqsCommand", "readAlign");
721 //**********************************************************************************************************************
723 int GetSeqsCommand::readAccnos(){
727 m->openInputFile(accnosfile, in);
742 catch(exception& e) {
743 m->errorOut(e, "GetSeqsCommand", "readAccnos");
748 //**********************************************************************************************************************