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","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; }
97 //check for required parameters
98 accnosfile = validParameter.validFile(parameters, "accnos", true);
99 if (accnosfile == "not open") { abort = true; }
100 else if (accnosfile == "not found") { accnosfile = ""; m->mothurOut("You must provide an accnos file."); m->mothurOutEndLine(); abort = true; }
102 fastafile = validParameter.validFile(parameters, "fasta", true);
103 if (fastafile == "not open") { abort = true; }
104 else if (fastafile == "not found") { fastafile = ""; }
106 namefile = validParameter.validFile(parameters, "name", true);
107 if (namefile == "not open") { abort = true; }
108 else if (namefile == "not found") { namefile = ""; }
110 groupfile = validParameter.validFile(parameters, "group", true);
111 if (groupfile == "not open") { abort = true; }
112 else if (groupfile == "not found") { groupfile = ""; }
114 alignfile = validParameter.validFile(parameters, "alignreport", true);
115 if (alignfile == "not open") { abort = true; }
116 else if (alignfile == "not found") { alignfile = ""; }
118 listfile = validParameter.validFile(parameters, "list", true);
119 if (listfile == "not open") { abort = true; }
120 else if (listfile == "not found") { listfile = ""; }
122 if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == "")) { m->mothurOut("You must provide one of the following: fasta, name, group, alignreport or listfile."); m->mothurOutEndLine(); abort = true; }
125 if (outputDir != "") { okay++; }
127 if (parameters.size() > okay) { m->mothurOut("You may only enter one of the following: fasta, name, group, alignreport or listfile."); m->mothurOutEndLine(); abort = true; }
131 catch(exception& e) {
132 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
136 //**********************************************************************************************************************
138 void GetSeqsCommand::help(){
140 m->mothurOut("The get.seqs command reads an .accnos file and one of the following file types: fasta, name, group, list or alignreport file.\n");
141 m->mothurOut("It outputs a file containing only the sequences in the .accnos file.\n");
142 m->mothurOut("The get.seqs command parameters are accnos, fasta, name, group, list and alignreport. You must provide accnos and one of the other parameters.\n");
143 m->mothurOut("The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
144 m->mothurOut("Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
145 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
147 catch(exception& e) {
148 m->errorOut(e, "GetSeqsCommand", "help");
153 //**********************************************************************************************************************
155 int GetSeqsCommand::execute(){
158 if (abort == true) { return 0; }
160 //get names you want to keep
163 //read through the correct file and output lines you want to keep
164 if (fastafile != "") { readFasta(); }
165 else if (namefile != "") { readName(); }
166 else if (groupfile != "") { readGroup(); }
167 else if (alignfile != "") { readAlign(); }
168 else if (listfile != "") { readList(); }
170 if (outputNames.size() != 0) {
171 m->mothurOutEndLine();
172 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
173 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
174 m->mothurOutEndLine();
180 catch(exception& e) {
181 m->errorOut(e, "GetSeqsCommand", "execute");
186 //**********************************************************************************************************************
187 void GetSeqsCommand::readFasta(){
189 if (outputDir == "") { outputDir += hasPath(fastafile); }
190 string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + "pick" + getExtension(fastafile);
192 openOutputFile(outputFileName, out);
196 openInputFile(fastafile, in);
199 bool wroteSomething = false;
202 Sequence currSeq(in);
203 name = currSeq.getName();
206 //if this name is in the accnos file
207 if (names.count(name) == 1) {
208 wroteSomething = true;
210 currSeq.printSequence(out);
220 if (wroteSomething == false) {
221 m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
222 remove(outputFileName.c_str());
223 }else { outputNames.push_back(outputFileName); }
226 catch(exception& e) {
227 m->errorOut(e, "GetSeqsCommand", "readFasta");
231 //**********************************************************************************************************************
232 void GetSeqsCommand::readList(){
234 if (outputDir == "") { outputDir += hasPath(listfile); }
235 string outputFileName = outputDir + getRootName(getSimpleName(listfile)) + "pick" + getExtension(listfile);
237 openOutputFile(outputFileName, out);
240 openInputFile(listfile, in);
242 bool wroteSomething = false;
245 //read in list vector
248 //make a new list vector
250 newList.setLabel(list.getLabel());
253 for (int i = 0; i < list.getNumBins(); i++) {
255 //parse out names that are in accnos file
256 string binnames = list.get(i);
258 string newNames = "";
259 while (binnames.find_first_of(',') != -1) {
260 string name = binnames.substr(0,binnames.find_first_of(','));
261 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
263 //if that name is in the .accnos file, add it
264 if (names.count(name) == 1) { newNames += name + ","; }
268 if (names.count(binnames) == 1) { newNames += binnames; }
270 //if there are names in this bin add to new list
271 if (newNames != "") { newList.push_back(newNames); }
274 //print new listvector
275 if (newList.getNumBins() != 0) {
276 wroteSomething = true;
285 if (wroteSomething == false) {
286 m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
287 remove(outputFileName.c_str());
288 }else { outputNames.push_back(outputFileName); }
291 catch(exception& e) {
292 m->errorOut(e, "GetSeqsCommand", "readList");
296 //**********************************************************************************************************************
297 void GetSeqsCommand::readName(){
299 if (outputDir == "") { outputDir += hasPath(namefile); }
300 string outputFileName = outputDir + getRootName(getSimpleName(namefile)) + "pick" + getExtension(namefile);
302 openOutputFile(outputFileName, out);
306 openInputFile(namefile, in);
307 string name, firstCol, secondCol;
309 bool wroteSomething = false;
317 vector<string> parsedNames;
318 //parse second column saving each name
319 while (secondCol.find_first_of(',') != -1) {
320 name = secondCol.substr(0,secondCol.find_first_of(','));
321 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
322 parsedNames.push_back(name);
325 //get name after last ,
326 parsedNames.push_back(secondCol);
328 vector<string> validSecond;
329 for (int i = 0; i < parsedNames.size(); i++) {
330 if (names.count(parsedNames[i]) == 1) {
331 validSecond.push_back(parsedNames[i]);
336 //if the name in the first column is in the set then print it and any other names in second column also in set
337 if (names.count(firstCol) == 1) {
339 wroteSomething = true;
341 out << firstCol << '\t';
343 //you know you have at least one valid second since first column is valid
344 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
345 out << validSecond[validSecond.size()-1] << endl;
348 //make first name in set you come to first column and then add the remaining names to second column
350 //you want part of this row
351 if (validSecond.size() != 0) {
353 wroteSomething = true;
355 out << validSecond[0] << '\t';
357 //you know you have at least one valid second since first column is valid
358 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
359 out << validSecond[validSecond.size()-1] << endl;
368 if (wroteSomething == false) {
369 m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
370 remove(outputFileName.c_str());
371 }else { outputNames.push_back(outputFileName); }
374 catch(exception& e) {
375 m->errorOut(e, "GetSeqsCommand", "readName");
380 //**********************************************************************************************************************
381 void GetSeqsCommand::readGroup(){
383 if (outputDir == "") { outputDir += hasPath(groupfile); }
384 string outputFileName = outputDir + getRootName(getSimpleName(groupfile)) + "pick" + getExtension(groupfile);
386 openOutputFile(outputFileName, out);
390 openInputFile(groupfile, in);
393 bool wroteSomething = false;
397 in >> name; //read from first column
398 in >> group; //read from second column
400 //if this name is in the accnos file
401 if (names.count(name) == 1) {
402 wroteSomething = true;
404 out << name << '\t' << group << endl;
414 if (wroteSomething == false) {
415 m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
416 remove(outputFileName.c_str());
417 }else { outputNames.push_back(outputFileName); }
420 catch(exception& e) {
421 m->errorOut(e, "GetSeqsCommand", "readGroup");
426 //**********************************************************************************************************************
427 //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
428 void GetSeqsCommand::readAlign(){
430 if (outputDir == "") { outputDir += hasPath(alignfile); }
431 string outputFileName = outputDir + getRootName(getSimpleName(alignfile)) + "pick.align.report";
433 openOutputFile(outputFileName, out);
437 openInputFile(alignfile, in);
440 bool wroteSomething = false;
442 //read column headers
443 for (int i = 0; i < 16; i++) {
444 if (!in.eof()) { in >> junk; out << junk << '\t'; }
451 in >> name; //read from first column
453 //if this name is in the accnos file
454 if (names.count(name) == 1) {
455 wroteSomething = true;
460 for (int i = 0; i < 15; i++) {
461 if (!in.eof()) { in >> junk; out << junk << '\t'; }
468 }else {//still read just don't do anything with it
470 for (int i = 0; i < 15; i++) {
471 if (!in.eof()) { in >> junk; }
481 if (wroteSomething == false) {
482 m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
483 remove(outputFileName.c_str());
484 }else { outputNames.push_back(outputFileName); }
487 catch(exception& e) {
488 m->errorOut(e, "GetSeqsCommand", "readAlign");
492 //**********************************************************************************************************************
494 void GetSeqsCommand::readAccnos(){
498 openInputFile(accnosfile, in);
511 catch(exception& e) {
512 m->errorOut(e, "GetSeqsCommand", "readAccnos");
517 //**********************************************************************************************************************