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 if (m->control_pressed) { return 0; }
165 //read through the correct file and output lines you want to keep
166 if (fastafile != "") { readFasta(); }
167 else if (namefile != "") { readName(); }
168 else if (groupfile != "") { readGroup(); }
169 else if (alignfile != "") { readAlign(); }
170 else if (listfile != "") { readList(); }
172 if (m->control_pressed) { return 0; }
174 if (outputNames.size() != 0) {
175 m->mothurOutEndLine();
176 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
177 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
178 m->mothurOutEndLine();
184 catch(exception& e) {
185 m->errorOut(e, "GetSeqsCommand", "execute");
190 //**********************************************************************************************************************
191 int GetSeqsCommand::readFasta(){
193 if (outputDir == "") { outputDir += hasPath(fastafile); }
194 string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + "pick" + getExtension(fastafile);
196 openOutputFile(outputFileName, out);
200 openInputFile(fastafile, in);
203 bool wroteSomething = false;
207 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
209 Sequence currSeq(in);
210 name = currSeq.getName();
213 //if this name is in the accnos file
214 if (names.count(name) == 1) {
215 wroteSomething = true;
217 currSeq.printSequence(out);
227 if (wroteSomething == false) {
228 m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
229 remove(outputFileName.c_str());
230 }else { outputNames.push_back(outputFileName); }
235 catch(exception& e) {
236 m->errorOut(e, "GetSeqsCommand", "readFasta");
240 //**********************************************************************************************************************
241 int GetSeqsCommand::readList(){
243 if (outputDir == "") { outputDir += hasPath(listfile); }
244 string outputFileName = outputDir + getRootName(getSimpleName(listfile)) + "pick" + getExtension(listfile);
246 openOutputFile(outputFileName, out);
249 openInputFile(listfile, in);
251 bool wroteSomething = false;
255 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
257 //read in list vector
260 //make a new list vector
262 newList.setLabel(list.getLabel());
265 for (int i = 0; i < list.getNumBins(); i++) {
267 //parse out names that are in accnos file
268 string binnames = list.get(i);
270 string newNames = "";
271 while (binnames.find_first_of(',') != -1) {
272 string name = binnames.substr(0,binnames.find_first_of(','));
273 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
275 //if that name is in the .accnos file, add it
276 if (names.count(name) == 1) { newNames += name + ","; }
280 if (names.count(binnames) == 1) { newNames += binnames + ","; }
282 //if there are names in this bin add to new list
283 if (newNames != "") {
284 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
285 newList.push_back(newNames);
289 //print new listvector
290 if (newList.getNumBins() != 0) {
291 wroteSomething = true;
300 if (wroteSomething == false) {
301 m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
302 remove(outputFileName.c_str());
303 }else { outputNames.push_back(outputFileName); }
308 catch(exception& e) {
309 m->errorOut(e, "GetSeqsCommand", "readList");
313 //**********************************************************************************************************************
314 int GetSeqsCommand::readName(){
316 if (outputDir == "") { outputDir += hasPath(namefile); }
317 string outputFileName = outputDir + getRootName(getSimpleName(namefile)) + "pick" + getExtension(namefile);
319 openOutputFile(outputFileName, out);
323 openInputFile(namefile, in);
324 string name, firstCol, secondCol;
326 bool wroteSomething = false;
331 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
336 vector<string> parsedNames;
337 //parse second column saving each name
338 while (secondCol.find_first_of(',') != -1) {
339 name = secondCol.substr(0,secondCol.find_first_of(','));
340 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
341 parsedNames.push_back(name);
344 //get name after last ,
345 parsedNames.push_back(secondCol);
347 vector<string> validSecond;
348 for (int i = 0; i < parsedNames.size(); i++) {
349 if (names.count(parsedNames[i]) == 1) {
350 validSecond.push_back(parsedNames[i]);
355 //if the name in the first column is in the set then print it and any other names in second column also in set
356 if (names.count(firstCol) == 1) {
358 wroteSomething = true;
360 out << firstCol << '\t';
362 //you know you have at least one valid second since first column is valid
363 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
364 out << validSecond[validSecond.size()-1] << endl;
367 //make first name in set you come to first column and then add the remaining names to second column
369 //you want part of this row
370 if (validSecond.size() != 0) {
372 wroteSomething = true;
374 out << validSecond[0] << '\t';
376 //you know you have at least one valid second since first column is valid
377 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
378 out << validSecond[validSecond.size()-1] << endl;
387 if (wroteSomething == false) {
388 m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
389 remove(outputFileName.c_str());
390 }else { outputNames.push_back(outputFileName); }
395 catch(exception& e) {
396 m->errorOut(e, "GetSeqsCommand", "readName");
401 //**********************************************************************************************************************
402 int GetSeqsCommand::readGroup(){
404 if (outputDir == "") { outputDir += hasPath(groupfile); }
405 string outputFileName = outputDir + getRootName(getSimpleName(groupfile)) + "pick" + getExtension(groupfile);
407 openOutputFile(outputFileName, out);
411 openInputFile(groupfile, in);
414 bool wroteSomething = false;
418 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
421 in >> name; //read from first column
422 in >> group; //read from second column
424 //if this name is in the accnos file
425 if (names.count(name) == 1) {
426 wroteSomething = true;
428 out << name << '\t' << group << endl;
438 if (wroteSomething == false) {
439 m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
440 remove(outputFileName.c_str());
441 }else { outputNames.push_back(outputFileName); }
446 catch(exception& e) {
447 m->errorOut(e, "GetSeqsCommand", "readGroup");
452 //**********************************************************************************************************************
453 //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
454 int GetSeqsCommand::readAlign(){
456 if (outputDir == "") { outputDir += hasPath(alignfile); }
457 string outputFileName = outputDir + getRootName(getSimpleName(alignfile)) + "pick.align.report";
459 openOutputFile(outputFileName, out);
463 openInputFile(alignfile, in);
466 bool wroteSomething = false;
468 //read column headers
469 for (int i = 0; i < 16; i++) {
470 if (!in.eof()) { in >> junk; out << junk << '\t'; }
477 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
480 in >> name; //read from first column
482 //if this name is in the accnos file
483 if (names.count(name) == 1) {
484 wroteSomething = true;
489 for (int i = 0; i < 15; i++) {
490 if (!in.eof()) { in >> junk; out << junk << '\t'; }
497 }else {//still read just don't do anything with it
499 for (int i = 0; i < 15; i++) {
500 if (!in.eof()) { in >> junk; }
510 if (wroteSomething == false) {
511 m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
512 remove(outputFileName.c_str());
513 }else { outputNames.push_back(outputFileName); }
518 catch(exception& e) {
519 m->errorOut(e, "GetSeqsCommand", "readAlign");
523 //**********************************************************************************************************************
525 int GetSeqsCommand::readAccnos(){
529 openInputFile(accnosfile, in);
544 catch(exception& e) {
545 m->errorOut(e, "GetSeqsCommand", "readAccnos");
550 //**********************************************************************************************************************