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", "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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = ""; }
135 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; }
139 catch(exception& e) {
140 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
144 //**********************************************************************************************************************
146 void GetSeqsCommand::help(){
148 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");
149 m->mothurOut("It outputs a file containing only the sequences in the .accnos file.\n");
150 m->mothurOut("The get.seqs command parameters are accnos, fasta, name, group, list, taxonomy and alignreport. You must provide accnos and at least one of the other parameters.\n");
151 m->mothurOut("The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
152 m->mothurOut("Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
153 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
155 catch(exception& e) {
156 m->errorOut(e, "GetSeqsCommand", "help");
161 //**********************************************************************************************************************
163 int GetSeqsCommand::execute(){
166 if (abort == true) { return 0; }
168 //get names you want to keep
171 if (m->control_pressed) { return 0; }
173 //read through the correct file and output lines you want to keep
174 if (fastafile != "") { readFasta(); }
175 if (namefile != "") { readName(); }
176 if (groupfile != "") { readGroup(); }
177 if (alignfile != "") { readAlign(); }
178 if (listfile != "") { readList(); }
179 if (taxfile != "") { readTax(); }
181 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
183 if (outputNames.size() != 0) {
184 m->mothurOutEndLine();
185 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
186 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
187 m->mothurOutEndLine();
193 catch(exception& e) {
194 m->errorOut(e, "GetSeqsCommand", "execute");
199 //**********************************************************************************************************************
200 int GetSeqsCommand::readFasta(){
202 if (outputDir == "") { outputDir += hasPath(fastafile); }
203 string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + "pick" + getExtension(fastafile);
205 openOutputFile(outputFileName, out);
209 openInputFile(fastafile, in);
212 bool wroteSomething = false;
216 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
218 Sequence currSeq(in);
219 name = currSeq.getName();
222 //if this name is in the accnos file
223 if (names.count(name) == 1) {
224 wroteSomething = true;
226 currSeq.printSequence(out);
234 if (wroteSomething == false) {
235 m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
236 remove(outputFileName.c_str());
237 }else { outputNames.push_back(outputFileName); }
242 catch(exception& e) {
243 m->errorOut(e, "GetSeqsCommand", "readFasta");
247 //**********************************************************************************************************************
248 int GetSeqsCommand::readList(){
250 if (outputDir == "") { outputDir += hasPath(listfile); }
251 string outputFileName = outputDir + getRootName(getSimpleName(listfile)) + "pick" + getExtension(listfile);
253 openOutputFile(outputFileName, out);
256 openInputFile(listfile, in);
258 bool wroteSomething = false;
262 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
264 //read in list vector
267 //make a new list vector
269 newList.setLabel(list.getLabel());
272 for (int i = 0; i < list.getNumBins(); i++) {
274 //parse out names that are in accnos file
275 string binnames = list.get(i);
277 string newNames = "";
278 while (binnames.find_first_of(',') != -1) {
279 string name = binnames.substr(0,binnames.find_first_of(','));
280 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
282 //if that name is in the .accnos file, add it
283 if (names.count(name) == 1) { newNames += name + ","; }
287 if (names.count(binnames) == 1) { newNames += binnames + ","; }
289 //if there are names in this bin add to new list
290 if (newNames != "") {
291 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
292 newList.push_back(newNames);
296 //print new listvector
297 if (newList.getNumBins() != 0) {
298 wroteSomething = true;
307 if (wroteSomething == false) {
308 m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
309 remove(outputFileName.c_str());
310 }else { outputNames.push_back(outputFileName); }
315 catch(exception& e) {
316 m->errorOut(e, "GetSeqsCommand", "readList");
320 //**********************************************************************************************************************
321 int GetSeqsCommand::readName(){
323 if (outputDir == "") { outputDir += hasPath(namefile); }
324 string outputFileName = outputDir + getRootName(getSimpleName(namefile)) + "pick" + getExtension(namefile);
326 openOutputFile(outputFileName, out);
330 openInputFile(namefile, in);
331 string name, firstCol, secondCol;
333 bool wroteSomething = false;
338 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
343 vector<string> parsedNames;
344 //parse second column saving each name
345 while (secondCol.find_first_of(',') != -1) {
346 name = secondCol.substr(0,secondCol.find_first_of(','));
347 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
348 parsedNames.push_back(name);
351 //get name after last ,
352 parsedNames.push_back(secondCol);
354 vector<string> validSecond;
355 for (int i = 0; i < parsedNames.size(); i++) {
356 if (names.count(parsedNames[i]) == 1) {
357 validSecond.push_back(parsedNames[i]);
362 //if the name in the first column is in the set then print it and any other names in second column also in set
363 if (names.count(firstCol) == 1) {
365 wroteSomething = true;
367 out << firstCol << '\t';
369 //you know you have at least one valid second since first column is valid
370 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
371 out << validSecond[validSecond.size()-1] << endl;
374 //make first name in set you come to first column and then add the remaining names to second column
376 //you want part of this row
377 if (validSecond.size() != 0) {
379 wroteSomething = true;
381 out << validSecond[0] << '\t';
383 //you know you have at least one valid second since first column is valid
384 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
385 out << validSecond[validSecond.size()-1] << endl;
394 if (wroteSomething == false) {
395 m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
396 remove(outputFileName.c_str());
397 }else { outputNames.push_back(outputFileName); }
402 catch(exception& e) {
403 m->errorOut(e, "GetSeqsCommand", "readName");
408 //**********************************************************************************************************************
409 int GetSeqsCommand::readGroup(){
411 if (outputDir == "") { outputDir += hasPath(groupfile); }
412 string outputFileName = outputDir + getRootName(getSimpleName(groupfile)) + "pick" + getExtension(groupfile);
414 openOutputFile(outputFileName, out);
418 openInputFile(groupfile, in);
421 bool wroteSomething = false;
425 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
428 in >> name; //read from first column
429 in >> group; //read from second column
431 //if this name is in the accnos file
432 if (names.count(name) == 1) {
433 wroteSomething = true;
435 out << name << '\t' << group << endl;
443 if (wroteSomething == false) {
444 m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
445 remove(outputFileName.c_str());
446 }else { outputNames.push_back(outputFileName); }
451 catch(exception& e) {
452 m->errorOut(e, "GetSeqsCommand", "readGroup");
456 //**********************************************************************************************************************
457 int GetSeqsCommand::readTax(){
459 if (outputDir == "") { outputDir += hasPath(taxfile); }
460 string outputFileName = outputDir + getRootName(getSimpleName(taxfile)) + "pick" + getExtension(taxfile);
462 openOutputFile(outputFileName, out);
465 openInputFile(taxfile, in);
468 bool wroteSomething = false;
472 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
474 in >> name; //read from first column
475 in >> tax; //read from second column
477 //if this name is in the accnos file
478 if (names.count(name) == 1) {
479 wroteSomething = true;
481 out << name << '\t' << tax << endl;
489 if (wroteSomething == false) {
490 m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
491 remove(outputFileName.c_str());
492 }else { outputNames.push_back(outputFileName); }
497 catch(exception& e) {
498 m->errorOut(e, "GetSeqsCommand", "readTax");
502 //**********************************************************************************************************************
503 //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
504 int GetSeqsCommand::readAlign(){
506 if (outputDir == "") { outputDir += hasPath(alignfile); }
507 string outputFileName = outputDir + getRootName(getSimpleName(alignfile)) + "pick.align.report";
509 openOutputFile(outputFileName, out);
513 openInputFile(alignfile, in);
516 bool wroteSomething = false;
518 //read column headers
519 for (int i = 0; i < 16; i++) {
520 if (!in.eof()) { in >> junk; out << junk << '\t'; }
527 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
530 in >> name; //read from first column
532 //if this name is in the accnos file
533 if (names.count(name) == 1) {
534 wroteSomething = true;
539 for (int i = 0; i < 15; i++) {
540 if (!in.eof()) { in >> junk; out << junk << '\t'; }
545 }else {//still read just don't do anything with it
547 for (int i = 0; i < 15; i++) {
548 if (!in.eof()) { in >> junk; }
558 if (wroteSomething == false) {
559 m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
560 remove(outputFileName.c_str());
561 }else { outputNames.push_back(outputFileName); }
566 catch(exception& e) {
567 m->errorOut(e, "GetSeqsCommand", "readAlign");
571 //**********************************************************************************************************************
573 int GetSeqsCommand::readAccnos(){
577 openInputFile(accnosfile, in);
592 catch(exception& e) {
593 m->errorOut(e, "GetSeqsCommand", "readAccnos");
598 //**********************************************************************************************************************