5 * Created by Sarah Westcott on 7/8/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "listseqscommand.h"
11 #include "sequence.hpp"
12 #include "listvector.hpp"
15 //**********************************************************************************************************************
16 vector<string> ListSeqsCommand::setParameters(){
18 CommandParameter pfasta("fasta", "InputTypes", "", "", "FNGLT", "FNGLT", "none",false,false); parameters.push_back(pfasta);
19 CommandParameter pname("name", "InputTypes", "", "", "FNGLT", "FNGLT", "none",false,false); parameters.push_back(pname);
20 CommandParameter pgroup("group", "InputTypes", "", "", "FNGLT", "FNGLT", "none",false,false); parameters.push_back(pgroup);
21 CommandParameter plist("list", "InputTypes", "", "", "FNGLT", "FNGLT", "none",false,false); parameters.push_back(plist);
22 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "FNGLT", "FNGLT", "none",false,false); parameters.push_back(ptaxonomy);
23 CommandParameter palignreport("alignreport", "InputTypes", "", "", "FNGLT", "FNGLT", "none",false,false); parameters.push_back(palignreport);
24 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
25 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
27 vector<string> myArray;
28 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
32 m->errorOut(e, "ListSeqsCommand", "setParameters");
36 //**********************************************************************************************************************
37 string ListSeqsCommand::getHelpString(){
39 string helpString = "";
40 helpString += "The list.seqs command reads a fasta, name, group, list, taxonomy or alignreport file and outputs a .accnos file containing sequence names.\n";
41 helpString += "The list.seqs command parameters are fasta, name, group, list, taxonomy and alignreport. You must provide one of these parameters.\n";
42 helpString += "The list.seqs command should be in the following format: list.seqs(fasta=yourFasta).\n";
43 helpString += "Example list.seqs(fasta=amazon.fasta).\n";
44 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
48 m->errorOut(e, "ListSeqsCommand", "getHelpString");
53 //**********************************************************************************************************************
54 ListSeqsCommand::ListSeqsCommand(){
56 abort = true; calledHelp = true;
58 vector<string> tempOutNames;
59 outputTypes["accnos"] = tempOutNames;
62 m->errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
66 //**********************************************************************************************************************
68 ListSeqsCommand::ListSeqsCommand(string option) {
70 abort = false; calledHelp = false;
72 //allow user to run help
73 if(option == "help") { help(); abort = true; calledHelp = true; }
74 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
76 vector<string> myArray = setParameters();
78 OptionParser parser(option);
79 map<string,string> parameters = parser.getParameters();
81 ValidParameters validParameter;
82 map<string,string>::iterator it;
84 //check to make sure all parameters are valid for command
85 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
86 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
89 //initialize outputTypes
90 vector<string> tempOutNames;
91 outputTypes["accnos"] = tempOutNames;
93 //if the user changes the output directory command factory will send this info to us in the output parameter
94 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
96 //if the user changes the input directory command factory will send this info to us in the output parameter
97 string inputDir = validParameter.validFile(parameters, "inputdir", false);
98 if (inputDir == "not found"){ inputDir = ""; }
101 it = parameters.find("alignreport");
102 //user has given a template file
103 if(it != parameters.end()){
104 path = m->hasPath(it->second);
105 //if the user has not given a path then, add inputdir. else leave path alone.
106 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
109 it = parameters.find("fasta");
110 //user has given a template file
111 if(it != parameters.end()){
112 path = m->hasPath(it->second);
113 //if the user has not given a path then, add inputdir. else leave path alone.
114 if (path == "") { parameters["fasta"] = inputDir + it->second; }
117 it = parameters.find("list");
118 //user has given a template file
119 if(it != parameters.end()){
120 path = m->hasPath(it->second);
121 //if the user has not given a path then, add inputdir. else leave path alone.
122 if (path == "") { parameters["list"] = inputDir + it->second; }
125 it = parameters.find("name");
126 //user has given a template file
127 if(it != parameters.end()){
128 path = m->hasPath(it->second);
129 //if the user has not given a path then, add inputdir. else leave path alone.
130 if (path == "") { parameters["name"] = inputDir + it->second; }
133 it = parameters.find("group");
134 //user has given a template file
135 if(it != parameters.end()){
136 path = m->hasPath(it->second);
137 //if the user has not given a path then, add inputdir. else leave path alone.
138 if (path == "") { parameters["group"] = inputDir + it->second; }
141 it = parameters.find("taxonomy");
142 //user has given a template file
143 if(it != parameters.end()){
144 path = m->hasPath(it->second);
145 //if the user has not given a path then, add inputdir. else leave path alone.
146 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
150 //check for required parameters
151 fastafile = validParameter.validFile(parameters, "fasta", true);
152 if (fastafile == "not open") { abort = true; }
153 else if (fastafile == "not found") { fastafile = ""; }
155 namefile = validParameter.validFile(parameters, "name", true);
156 if (namefile == "not open") { abort = true; }
157 else if (namefile == "not found") { namefile = ""; }
159 groupfile = validParameter.validFile(parameters, "group", true);
160 if (groupfile == "not open") { abort = true; }
161 else if (groupfile == "not found") { groupfile = ""; }
163 alignfile = validParameter.validFile(parameters, "alignreport", true);
164 if (alignfile == "not open") { abort = true; }
165 else if (alignfile == "not found") { alignfile = ""; }
167 listfile = validParameter.validFile(parameters, "list", true);
168 if (listfile == "not open") { abort = true; }
169 else if (listfile == "not found") { listfile = ""; }
171 taxfile = validParameter.validFile(parameters, "taxonomy", true);
172 if (taxfile == "not open") { abort = true; }
173 else if (taxfile == "not found") { taxfile = ""; }
175 if ((fastafile == "") && (namefile == "") && (listfile == "") && (groupfile == "") && (alignfile == "") && (taxfile == "")) { m->mothurOut("You must provide a file."); m->mothurOutEndLine(); abort = true; }
178 if (outputDir != "") { okay++; }
179 if (inputDir != "") { okay++; }
181 if (parameters.size() > okay) { m->mothurOut("You may only enter one file."); m->mothurOutEndLine(); abort = true; }
185 catch(exception& e) {
186 m->errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
190 //**********************************************************************************************************************
192 int ListSeqsCommand::execute(){
195 if (abort == true) { if (calledHelp) { return 0; } return 2; }
197 //read functions fill names vector
198 if (fastafile != "") { inputFileName = fastafile; readFasta(); }
199 else if (namefile != "") { inputFileName = namefile; readName(); }
200 else if (groupfile != "") { inputFileName = groupfile; readGroup(); }
201 else if (alignfile != "") { inputFileName = alignfile; readAlign(); }
202 else if (listfile != "") { inputFileName = listfile; readList(); }
203 else if (taxfile != "") { inputFileName = taxfile; readTax(); }
205 if (m->control_pressed) { outputTypes.clear(); return 0; }
207 //sort in alphabetical order
208 sort(names.begin(), names.end());
210 if (outputDir == "") { outputDir += m->hasPath(inputFileName); }
212 string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + "accnos";
215 m->openOutputFile(outputFileName, out);
216 outputNames.push_back(outputFileName); outputTypes["accnos"].push_back(outputFileName);
218 //output to .accnos file
219 for (int i = 0; i < names.size(); i++) {
221 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(outputFileName.c_str()); return 0; }
223 out << names[i] << endl;
227 if (m->control_pressed) { outputTypes.clear(); remove(outputFileName.c_str()); return 0; }
229 m->setAccnosFile(outputFileName);
231 m->mothurOutEndLine();
232 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
233 m->mothurOut(outputFileName); m->mothurOutEndLine();
234 m->mothurOutEndLine();
236 //set accnos file as new current accnosfile
238 itTypes = outputTypes.find("accnos");
239 if (itTypes != outputTypes.end()) {
240 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setAccnosFile(current); }
246 catch(exception& e) {
247 m->errorOut(e, "ListSeqsCommand", "execute");
252 //**********************************************************************************************************************
253 int ListSeqsCommand::readFasta(){
257 m->openInputFile(fastafile, in);
261 //string newFastaName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "numsAdded.fasta";
262 //m->openOutputFile(newFastaName, out);
264 //string lastName = "";
268 if (m->control_pressed) { in.close(); return 0; }
270 Sequence currSeq(in);
271 name = currSeq.getName();
272 //if (lastName == "") { lastName = name; }
273 //if (name != lastName) { count = 1; }
276 //Sequence newSeq(name+"_"+toString(count), currSeq.getAligned());
277 //newSeq.printSequence(out);
279 if (name != "") { names.push_back(name); }
290 catch(exception& e) {
291 m->errorOut(e, "ListSeqsCommand", "readFasta");
295 //**********************************************************************************************************************
296 int ListSeqsCommand::readList(){
299 m->openInputFile(listfile, in);
302 //read in list vector
306 for (int i = 0; i < list.getNumBins(); i++) {
307 string binnames = list.get(i);
309 if (m->control_pressed) { in.close(); return 0; }
311 while (binnames.find_first_of(',') != -1) {
312 string name = binnames.substr(0,binnames.find_first_of(','));
313 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
314 names.push_back(name);
317 names.push_back(binnames);
325 catch(exception& e) {
326 m->errorOut(e, "ListSeqsCommand", "readList");
331 //**********************************************************************************************************************
332 int ListSeqsCommand::readName(){
336 m->openInputFile(namefile, in);
337 string name, firstCol, secondCol;
341 if (m->control_pressed) { in.close(); return 0; }
346 //parse second column saving each name
347 while (secondCol.find_first_of(',') != -1) {
348 name = secondCol.substr(0,secondCol.find_first_of(','));
349 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
350 names.push_back(name);
353 //get name after last ,
354 names.push_back(secondCol);
362 catch(exception& e) {
363 m->errorOut(e, "ListSeqsCommand", "readName");
368 //**********************************************************************************************************************
369 int ListSeqsCommand::readGroup(){
373 m->openInputFile(groupfile, in);
378 if (m->control_pressed) { in.close(); return 0; }
380 in >> name; m->gobble(in); //read from first column
381 in >> group; //read from second column
383 names.push_back(name);
391 catch(exception& e) {
392 m->errorOut(e, "ListSeqsCommand", "readGroup");
397 //**********************************************************************************************************************
398 //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
399 int ListSeqsCommand::readAlign(){
403 m->openInputFile(alignfile, in);
406 //read column headers
407 for (int i = 0; i < 16; i++) {
408 if (!in.eof()) { in >> junk; }
415 if (m->control_pressed) { in.close(); return 0; }
417 in >> name; //read from first column
420 for (int i = 0; i < 15; i++) {
421 if (!in.eof()) { in >> junk; }
425 names.push_back(name);
435 catch(exception& e) {
436 m->errorOut(e, "ListSeqsCommand", "readAlign");
440 //**********************************************************************************************************************
441 int ListSeqsCommand::readTax(){
445 m->openInputFile(taxfile, in);
446 string name, firstCol, secondCol;
450 if (m->control_pressed) { in.close(); return 0; }
455 names.push_back(firstCol);
465 catch(exception& e) {
466 m->errorOut(e, "ListSeqsCommand", "readTax");
470 //**********************************************************************************************************************