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 //**********************************************************************************************************************
16 GetSeqsCommand::GetSeqsCommand(string option) {
20 //allow user to run help
21 if(option == "help") { help(); abort = true; }
24 //valid paramters for this command
25 string Array[] = {"fasta","name", "group", "alignreport", "accnos", "dups", "list","taxonomy","outputdir","inputdir"};
26 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
28 OptionParser parser(option);
29 map<string,string> parameters = parser.getParameters();
31 ValidParameters validParameter;
32 map<string,string>::iterator it;
34 //check to make sure all parameters are valid for command
35 for (it = parameters.begin(); it != parameters.end(); it++) {
36 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
39 //if the user changes the output directory command factory will send this info to us in the output parameter
40 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
42 //if the user changes the input directory command factory will send this info to us in the output parameter
43 string inputDir = validParameter.validFile(parameters, "inputdir", false);
44 if (inputDir == "not found"){ inputDir = ""; }
47 it = parameters.find("alignreport");
48 //user has given a template file
49 if(it != parameters.end()){
50 path = m->hasPath(it->second);
51 //if the user has not given a path then, add inputdir. else leave path alone.
52 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
55 it = parameters.find("fasta");
56 //user has given a template file
57 if(it != parameters.end()){
58 path = m->hasPath(it->second);
59 //if the user has not given a path then, add inputdir. else leave path alone.
60 if (path == "") { parameters["fasta"] = inputDir + it->second; }
63 it = parameters.find("accnos");
64 //user has given a template file
65 if(it != parameters.end()){
66 path = m->hasPath(it->second);
67 //if the user has not given a path then, add inputdir. else leave path alone.
68 if (path == "") { parameters["accnos"] = inputDir + it->second; }
71 it = parameters.find("list");
72 //user has given a template file
73 if(it != parameters.end()){
74 path = m->hasPath(it->second);
75 //if the user has not given a path then, add inputdir. else leave path alone.
76 if (path == "") { parameters["list"] = inputDir + it->second; }
79 it = parameters.find("name");
80 //user has given a template file
81 if(it != parameters.end()){
82 path = m->hasPath(it->second);
83 //if the user has not given a path then, add inputdir. else leave path alone.
84 if (path == "") { parameters["name"] = inputDir + it->second; }
87 it = parameters.find("group");
88 //user has given a template file
89 if(it != parameters.end()){
90 path = m->hasPath(it->second);
91 //if the user has not given a path then, add inputdir. else leave path alone.
92 if (path == "") { parameters["group"] = inputDir + it->second; }
95 it = parameters.find("taxonomy");
96 //user has given a template file
97 if(it != parameters.end()){
98 path = m->hasPath(it->second);
99 //if the user has not given a path then, add inputdir. else leave path alone.
100 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
105 //check for required parameters
106 accnosfile = validParameter.validFile(parameters, "accnos", true);
107 if (accnosfile == "not open") { abort = true; }
108 else if (accnosfile == "not found") { accnosfile = ""; m->mothurOut("You must provide an accnos file."); m->mothurOutEndLine(); abort = true; }
110 fastafile = validParameter.validFile(parameters, "fasta", true);
111 if (fastafile == "not open") { abort = true; }
112 else if (fastafile == "not found") { fastafile = ""; }
114 namefile = validParameter.validFile(parameters, "name", true);
115 if (namefile == "not open") { abort = true; }
116 else if (namefile == "not found") { namefile = ""; }
118 groupfile = validParameter.validFile(parameters, "group", true);
119 if (groupfile == "not open") { abort = true; }
120 else if (groupfile == "not found") { groupfile = ""; }
122 alignfile = validParameter.validFile(parameters, "alignreport", true);
123 if (alignfile == "not open") { abort = true; }
124 else if (alignfile == "not found") { alignfile = ""; }
126 listfile = validParameter.validFile(parameters, "list", true);
127 if (listfile == "not open") { abort = true; }
128 else if (listfile == "not found") { listfile = ""; }
130 taxfile = validParameter.validFile(parameters, "taxonomy", true);
131 if (taxfile == "not open") { abort = true; }
132 else if (taxfile == "not found") { taxfile = ""; }
134 string usedDups = "true";
135 string temp = validParameter.validFile(parameters, "dups", false); if (temp == "not found") { temp = "false"; usedDups = ""; }
136 dups = m->isTrue(temp);
138 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; }
140 if ((usedDups != "") && (namefile == "")) { m->mothurOut("You may only use dups with the name option."); m->mothurOutEndLine(); abort = true; }
145 catch(exception& e) {
146 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
150 //**********************************************************************************************************************
152 void GetSeqsCommand::help(){
154 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");
155 m->mothurOut("It outputs a file containing only the sequences in the .accnos file.\n");
156 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");
157 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");
158 m->mothurOut("The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
159 m->mothurOut("Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
160 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
162 catch(exception& e) {
163 m->errorOut(e, "GetSeqsCommand", "help");
168 //**********************************************************************************************************************
170 int GetSeqsCommand::execute(){
173 if (abort == true) { return 0; }
175 //get names you want to keep
178 if (m->control_pressed) { return 0; }
180 //read through the correct file and output lines you want to keep
181 if (namefile != "") { readName(); }
182 if (fastafile != "") { readFasta(); }
183 if (groupfile != "") { readGroup(); }
184 if (alignfile != "") { readAlign(); }
185 if (listfile != "") { readList(); }
186 if (taxfile != "") { readTax(); }
188 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
190 if (outputNames.size() != 0) {
191 m->mothurOutEndLine();
192 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
193 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
194 m->mothurOutEndLine();
200 catch(exception& e) {
201 m->errorOut(e, "GetSeqsCommand", "execute");
206 //**********************************************************************************************************************
207 int GetSeqsCommand::readFasta(){
209 string thisOutputDir = outputDir;
210 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
211 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
213 m->openOutputFile(outputFileName, out);
217 m->openInputFile(fastafile, in);
220 bool wroteSomething = false;
224 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
226 Sequence currSeq(in);
227 name = currSeq.getName();
230 //if this name is in the accnos file
231 if (names.count(name) != 0) {
232 wroteSomething = true;
234 currSeq.printSequence(out);
242 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
243 outputNames.push_back(outputFileName);
248 catch(exception& e) {
249 m->errorOut(e, "GetSeqsCommand", "readFasta");
253 //**********************************************************************************************************************
254 int GetSeqsCommand::readList(){
256 string thisOutputDir = outputDir;
257 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
258 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
260 m->openOutputFile(outputFileName, out);
263 m->openInputFile(listfile, in);
265 bool wroteSomething = false;
269 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
271 //read in list vector
274 //make a new list vector
276 newList.setLabel(list.getLabel());
279 for (int i = 0; i < list.getNumBins(); i++) {
281 //parse out names that are in accnos file
282 string binnames = list.get(i);
284 string newNames = "";
285 while (binnames.find_first_of(',') != -1) {
286 string name = binnames.substr(0,binnames.find_first_of(','));
287 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
289 //if that name is in the .accnos file, add it
290 if (names.count(name) != 0) { newNames += name + ","; }
294 if (names.count(binnames) != 0) { newNames += binnames + ","; }
296 //if there are names in this bin add to new list
297 if (newNames != "") {
298 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
299 newList.push_back(newNames);
303 //print new listvector
304 if (newList.getNumBins() != 0) {
305 wroteSomething = true;
314 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
315 outputNames.push_back(outputFileName);
320 catch(exception& e) {
321 m->errorOut(e, "GetSeqsCommand", "readList");
325 //**********************************************************************************************************************
326 int GetSeqsCommand::readName(){
328 string thisOutputDir = outputDir;
329 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
330 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
332 m->openOutputFile(outputFileName, out);
336 m->openInputFile(namefile, in);
337 string name, firstCol, secondCol;
339 bool wroteSomething = false;
344 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
350 if (dups) { hold = secondCol; }
352 vector<string> parsedNames;
353 //parse second column saving each name
354 while (secondCol.find_first_of(',') != -1) {
355 name = secondCol.substr(0,secondCol.find_first_of(','));
356 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
357 parsedNames.push_back(name);
360 //get name after last ,
361 parsedNames.push_back(secondCol);
363 vector<string> validSecond;
364 for (int i = 0; i < parsedNames.size(); i++) {
365 if (names.count(parsedNames[i]) != 0) {
366 validSecond.push_back(parsedNames[i]);
370 if ((dups) && (validSecond.size() != 0)) { //dups = true and we want to add someone, then add everyone
371 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
372 out << firstCol << '\t' << hold << endl;
373 wroteSomething = true;
375 //if the name in the first column is in the set then print it and any other names in second column also in set
376 if (names.count(firstCol) != 0) {
378 wroteSomething = true;
380 out << firstCol << '\t';
382 //you know you have at least one valid second since first column is valid
383 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
384 out << validSecond[validSecond.size()-1] << endl;
387 //make first name in set you come to first column and then add the remaining names to second column
389 //you want part of this row
390 if (validSecond.size() != 0) {
392 wroteSomething = true;
394 out << validSecond[0] << '\t';
396 //you know you have at least one valid second since first column is valid
397 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
398 out << validSecond[validSecond.size()-1] << endl;
407 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
408 outputNames.push_back(outputFileName);
413 catch(exception& e) {
414 m->errorOut(e, "GetSeqsCommand", "readName");
419 //**********************************************************************************************************************
420 int GetSeqsCommand::readGroup(){
422 string thisOutputDir = outputDir;
423 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
424 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
426 m->openOutputFile(outputFileName, out);
430 m->openInputFile(groupfile, in);
433 bool wroteSomething = false;
437 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
440 in >> name; //read from first column
441 in >> group; //read from second column
443 //if this name is in the accnos file
444 if (names.count(name) != 0) {
445 wroteSomething = true;
447 out << name << '\t' << group << endl;
455 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
456 outputNames.push_back(outputFileName);
461 catch(exception& e) {
462 m->errorOut(e, "GetSeqsCommand", "readGroup");
466 //**********************************************************************************************************************
467 int GetSeqsCommand::readTax(){
469 string thisOutputDir = outputDir;
470 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
471 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
473 m->openOutputFile(outputFileName, out);
476 m->openInputFile(taxfile, in);
479 bool wroteSomething = false;
483 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
485 in >> name; //read from first column
486 in >> tax; //read from second column
488 //if this name is in the accnos file
489 if (names.count(name) != 0) {
490 wroteSomething = true;
492 out << name << '\t' << tax << endl;
500 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
501 outputNames.push_back(outputFileName);
506 catch(exception& e) {
507 m->errorOut(e, "GetSeqsCommand", "readTax");
511 //**********************************************************************************************************************
512 //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
513 int GetSeqsCommand::readAlign(){
515 string thisOutputDir = outputDir;
516 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
517 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
519 m->openOutputFile(outputFileName, out);
523 m->openInputFile(alignfile, in);
526 bool wroteSomething = false;
528 //read column headers
529 for (int i = 0; i < 16; i++) {
530 if (!in.eof()) { in >> junk; out << junk << '\t'; }
537 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
540 in >> name; //read from first column
542 //if this name is in the accnos file
543 if (names.count(name) != 0) {
544 wroteSomething = true;
549 for (int i = 0; i < 15; i++) {
550 if (!in.eof()) { in >> junk; out << junk << '\t'; }
555 }else {//still read just don't do anything with it
557 for (int i = 0; i < 15; i++) {
558 if (!in.eof()) { in >> junk; }
568 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
569 outputNames.push_back(outputFileName);
574 catch(exception& e) {
575 m->errorOut(e, "GetSeqsCommand", "readAlign");
579 //**********************************************************************************************************************
581 int GetSeqsCommand::readAccnos(){
585 m->openInputFile(accnosfile, in);
600 catch(exception& e) {
601 m->errorOut(e, "GetSeqsCommand", "readAccnos");
606 //**********************************************************************************************************************