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", "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(){
29 //initialize outputTypes
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;
39 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
43 //**********************************************************************************************************************
44 vector<string> GetSeqsCommand::getRequiredParameters(){
46 string Array[] = {"accnos"};
47 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
51 m->errorOut(e, "GetSeqsCommand", "getRequiredParameters");
55 //**********************************************************************************************************************
56 vector<string> GetSeqsCommand::getRequiredFiles(){
58 vector<string> myArray;
62 m->errorOut(e, "GetSeqsCommand", "getRequiredFiles");
66 //**********************************************************************************************************************
67 GetSeqsCommand::GetSeqsCommand(string option) {
71 //allow user to run help
72 if(option == "help") { help(); abort = true; }
75 //valid paramters for this command
76 string Array[] = {"fasta","name", "group", "alignreport", "accnos", "dups", "list","taxonomy","outputdir","inputdir"};
77 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
79 OptionParser parser(option);
80 map<string,string> parameters = parser.getParameters();
82 ValidParameters validParameter;
83 map<string,string>::iterator it;
85 //check to make sure all parameters are valid for command
86 for (it = parameters.begin(); it != parameters.end(); it++) {
87 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
90 //initialize outputTypes
91 vector<string> tempOutNames;
92 outputTypes["fasta"] = tempOutNames;
93 outputTypes["taxonomy"] = tempOutNames;
94 outputTypes["name"] = tempOutNames;
95 outputTypes["group"] = tempOutNames;
96 outputTypes["alignreport"] = tempOutNames;
97 outputTypes["list"] = tempOutNames;
99 //if the user changes the output directory command factory will send this info to us in the output parameter
100 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
102 //if the user changes the input directory command factory will send this info to us in the output parameter
103 string inputDir = validParameter.validFile(parameters, "inputdir", false);
104 if (inputDir == "not found"){ inputDir = ""; }
107 it = parameters.find("alignreport");
108 //user has given a template file
109 if(it != parameters.end()){
110 path = m->hasPath(it->second);
111 //if the user has not given a path then, add inputdir. else leave path alone.
112 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
115 it = parameters.find("fasta");
116 //user has given a template file
117 if(it != parameters.end()){
118 path = m->hasPath(it->second);
119 //if the user has not given a path then, add inputdir. else leave path alone.
120 if (path == "") { parameters["fasta"] = inputDir + it->second; }
123 it = parameters.find("accnos");
124 //user has given a template file
125 if(it != parameters.end()){
126 path = m->hasPath(it->second);
127 //if the user has not given a path then, add inputdir. else leave path alone.
128 if (path == "") { parameters["accnos"] = inputDir + it->second; }
131 it = parameters.find("list");
132 //user has given a template file
133 if(it != parameters.end()){
134 path = m->hasPath(it->second);
135 //if the user has not given a path then, add inputdir. else leave path alone.
136 if (path == "") { parameters["list"] = inputDir + it->second; }
139 it = parameters.find("name");
140 //user has given a template file
141 if(it != parameters.end()){
142 path = m->hasPath(it->second);
143 //if the user has not given a path then, add inputdir. else leave path alone.
144 if (path == "") { parameters["name"] = inputDir + it->second; }
147 it = parameters.find("group");
148 //user has given a template file
149 if(it != parameters.end()){
150 path = m->hasPath(it->second);
151 //if the user has not given a path then, add inputdir. else leave path alone.
152 if (path == "") { parameters["group"] = inputDir + it->second; }
155 it = parameters.find("taxonomy");
156 //user has given a template file
157 if(it != parameters.end()){
158 path = m->hasPath(it->second);
159 //if the user has not given a path then, add inputdir. else leave path alone.
160 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
165 //check for required parameters
166 accnosfile = validParameter.validFile(parameters, "accnos", true);
167 if (accnosfile == "not open") { abort = true; }
168 else if (accnosfile == "not found") { accnosfile = ""; m->mothurOut("You must provide an accnos file."); m->mothurOutEndLine(); abort = true; }
170 fastafile = validParameter.validFile(parameters, "fasta", true);
171 if (fastafile == "not open") { abort = true; }
172 else if (fastafile == "not found") { fastafile = ""; }
174 namefile = validParameter.validFile(parameters, "name", true);
175 if (namefile == "not open") { abort = true; }
176 else if (namefile == "not found") { namefile = ""; }
178 groupfile = validParameter.validFile(parameters, "group", true);
179 if (groupfile == "not open") { abort = true; }
180 else if (groupfile == "not found") { groupfile = ""; }
182 alignfile = validParameter.validFile(parameters, "alignreport", true);
183 if (alignfile == "not open") { abort = true; }
184 else if (alignfile == "not found") { alignfile = ""; }
186 listfile = validParameter.validFile(parameters, "list", true);
187 if (listfile == "not open") { abort = true; }
188 else if (listfile == "not found") { listfile = ""; }
190 taxfile = validParameter.validFile(parameters, "taxonomy", true);
191 if (taxfile == "not open") { abort = true; }
192 else if (taxfile == "not found") { taxfile = ""; }
194 string usedDups = "true";
195 string temp = validParameter.validFile(parameters, "dups", false); if (temp == "not found") { temp = "false"; usedDups = ""; }
196 dups = m->isTrue(temp);
198 if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == "") && (taxfile == "")) { m->mothurOut("You must provide one of the following: fasta, name, group, alignreport, taxonomy or listfile."); m->mothurOutEndLine(); abort = true; }
200 if ((usedDups != "") && (namefile == "")) { m->mothurOut("You may only use dups with the name option."); m->mothurOutEndLine(); abort = true; }
205 catch(exception& e) {
206 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
210 //**********************************************************************************************************************
212 void GetSeqsCommand::help(){
214 m->mothurOut("The get.seqs command reads an .accnos file and any of the following file types: fasta, name, group, list, taxonomy or alignreport file.\n");
215 m->mothurOut("It outputs a file containing only the sequences in the .accnos file.\n");
216 m->mothurOut("The get.seqs command parameters are accnos, fasta, name, group, list, taxonomy, alignreport and dups. You must provide accnos and at least one of the other parameters.\n");
217 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");
218 m->mothurOut("The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
219 m->mothurOut("Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
220 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
222 catch(exception& e) {
223 m->errorOut(e, "GetSeqsCommand", "help");
228 //**********************************************************************************************************************
230 int GetSeqsCommand::execute(){
233 if (abort == true) { return 0; }
235 //get names you want to keep
238 if (m->control_pressed) { return 0; }
240 //read through the correct file and output lines you want to keep
241 if (namefile != "") { readName(); }
242 if (fastafile != "") { readFasta(); }
243 if (groupfile != "") { readGroup(); }
244 if (alignfile != "") { readAlign(); }
245 if (listfile != "") { readList(); }
246 if (taxfile != "") { readTax(); }
248 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
250 if (outputNames.size() != 0) {
251 m->mothurOutEndLine();
252 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
253 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
254 m->mothurOutEndLine();
260 catch(exception& e) {
261 m->errorOut(e, "GetSeqsCommand", "execute");
266 //**********************************************************************************************************************
267 int GetSeqsCommand::readFasta(){
269 string thisOutputDir = outputDir;
270 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
271 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
273 m->openOutputFile(outputFileName, out);
277 m->openInputFile(fastafile, in);
280 bool wroteSomething = false;
284 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
286 Sequence currSeq(in);
287 name = currSeq.getName();
290 //if this name is in the accnos file
291 if (names.count(name) != 0) {
292 wroteSomething = true;
294 currSeq.printSequence(out);
302 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
303 outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
308 catch(exception& e) {
309 m->errorOut(e, "GetSeqsCommand", "readFasta");
313 //**********************************************************************************************************************
314 int GetSeqsCommand::readList(){
316 string thisOutputDir = outputDir;
317 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
318 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
320 m->openOutputFile(outputFileName, out);
323 m->openInputFile(listfile, in);
325 bool wroteSomething = false;
329 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
331 //read in list vector
334 //make a new list vector
336 newList.setLabel(list.getLabel());
339 for (int i = 0; i < list.getNumBins(); i++) {
341 //parse out names that are in accnos file
342 string binnames = list.get(i);
344 string newNames = "";
345 while (binnames.find_first_of(',') != -1) {
346 string name = binnames.substr(0,binnames.find_first_of(','));
347 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
349 //if that name is in the .accnos file, add it
350 if (names.count(name) != 0) { newNames += name + ","; }
354 if (names.count(binnames) != 0) { newNames += binnames + ","; }
356 //if there are names in this bin add to new list
357 if (newNames != "") {
358 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
359 newList.push_back(newNames);
363 //print new listvector
364 if (newList.getNumBins() != 0) {
365 wroteSomething = true;
374 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
375 outputNames.push_back(outputFileName); outputTypes["list"].push_back(outputFileName);
380 catch(exception& e) {
381 m->errorOut(e, "GetSeqsCommand", "readList");
385 //**********************************************************************************************************************
386 int GetSeqsCommand::readName(){
388 string thisOutputDir = outputDir;
389 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
390 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
392 m->openOutputFile(outputFileName, out);
396 m->openInputFile(namefile, in);
397 string name, firstCol, secondCol;
399 bool wroteSomething = false;
404 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
410 if (dups) { hold = secondCol; }
412 vector<string> parsedNames;
413 //parse second column saving each name
414 while (secondCol.find_first_of(',') != -1) {
415 name = secondCol.substr(0,secondCol.find_first_of(','));
416 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
417 parsedNames.push_back(name);
420 //get name after last ,
421 parsedNames.push_back(secondCol);
423 vector<string> validSecond;
424 for (int i = 0; i < parsedNames.size(); i++) {
425 if (names.count(parsedNames[i]) != 0) {
426 validSecond.push_back(parsedNames[i]);
430 if ((dups) && (validSecond.size() != 0)) { //dups = true and we want to add someone, then add everyone
431 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
432 out << firstCol << '\t' << hold << endl;
433 wroteSomething = true;
435 //if the name in the first column is in the set then print it and any other names in second column also in set
436 if (names.count(firstCol) != 0) {
438 wroteSomething = true;
440 out << firstCol << '\t';
442 //you know you have at least one valid second since first column is valid
443 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
444 out << validSecond[validSecond.size()-1] << endl;
447 //make first name in set you come to first column and then add the remaining names to second column
449 //you want part of this row
450 if (validSecond.size() != 0) {
452 wroteSomething = true;
454 out << validSecond[0] << '\t';
456 //you know you have at least one valid second since first column is valid
457 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
458 out << validSecond[validSecond.size()-1] << endl;
467 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
468 outputNames.push_back(outputFileName); outputTypes["name"].push_back(outputFileName);
473 catch(exception& e) {
474 m->errorOut(e, "GetSeqsCommand", "readName");
479 //**********************************************************************************************************************
480 int GetSeqsCommand::readGroup(){
482 string thisOutputDir = outputDir;
483 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
484 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
486 m->openOutputFile(outputFileName, out);
490 m->openInputFile(groupfile, in);
493 bool wroteSomething = false;
497 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
500 in >> name; //read from first column
501 in >> group; //read from second column
503 //if this name is in the accnos file
504 if (names.count(name) != 0) {
505 wroteSomething = true;
507 out << name << '\t' << group << endl;
515 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
516 outputNames.push_back(outputFileName); outputTypes["group"].push_back(outputFileName);
521 catch(exception& e) {
522 m->errorOut(e, "GetSeqsCommand", "readGroup");
526 //**********************************************************************************************************************
527 int GetSeqsCommand::readTax(){
529 string thisOutputDir = outputDir;
530 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
531 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
533 m->openOutputFile(outputFileName, out);
536 m->openInputFile(taxfile, in);
539 bool wroteSomething = false;
543 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
545 in >> name; //read from first column
546 in >> tax; //read from second column
548 //if this name is in the accnos file
549 if (names.count(name) != 0) {
550 wroteSomething = true;
552 out << name << '\t' << tax << endl;
560 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
561 outputNames.push_back(outputFileName); outputTypes["taxonomy"].push_back(outputFileName);
566 catch(exception& e) {
567 m->errorOut(e, "GetSeqsCommand", "readTax");
571 //**********************************************************************************************************************
572 //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
573 int GetSeqsCommand::readAlign(){
575 string thisOutputDir = outputDir;
576 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
577 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
579 m->openOutputFile(outputFileName, out);
583 m->openInputFile(alignfile, in);
586 bool wroteSomething = false;
588 //read column headers
589 for (int i = 0; i < 16; i++) {
590 if (!in.eof()) { in >> junk; out << junk << '\t'; }
597 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
600 in >> name; //read from first column
602 //if this name is in the accnos file
603 if (names.count(name) != 0) {
604 wroteSomething = true;
609 for (int i = 0; i < 15; i++) {
610 if (!in.eof()) { in >> junk; out << junk << '\t'; }
615 }else {//still read just don't do anything with it
617 for (int i = 0; i < 15; i++) {
618 if (!in.eof()) { in >> junk; }
628 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
629 outputNames.push_back(outputFileName); outputTypes["alignreport"].push_back(outputFileName);
634 catch(exception& e) {
635 m->errorOut(e, "GetSeqsCommand", "readAlign");
639 //**********************************************************************************************************************
641 int GetSeqsCommand::readAccnos(){
645 m->openInputFile(accnosfile, in);
660 catch(exception& e) {
661 m->errorOut(e, "GetSeqsCommand", "readAccnos");
666 //**********************************************************************************************************************