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 = ""; }
154 else { m->setFastaFile(fastafile); }
156 namefile = validParameter.validFile(parameters, "name", true);
157 if (namefile == "not open") { abort = true; }
158 else if (namefile == "not found") { namefile = ""; }
159 else { m->setNameFile(namefile); }
161 groupfile = validParameter.validFile(parameters, "group", true);
162 if (groupfile == "not open") { abort = true; }
163 else if (groupfile == "not found") { groupfile = ""; }
164 else { m->setGroupFile(groupfile); }
166 alignfile = validParameter.validFile(parameters, "alignreport", true);
167 if (alignfile == "not open") { abort = true; }
168 else if (alignfile == "not found") { alignfile = ""; }
170 listfile = validParameter.validFile(parameters, "list", true);
171 if (listfile == "not open") { abort = true; }
172 else if (listfile == "not found") { listfile = ""; }
173 else { m->setListFile(listfile); }
175 taxfile = validParameter.validFile(parameters, "taxonomy", true);
176 if (taxfile == "not open") { abort = true; }
177 else if (taxfile == "not found") { taxfile = ""; }
178 else { m->setTaxonomyFile(taxfile); }
180 if ((fastafile == "") && (namefile == "") && (listfile == "") && (groupfile == "") && (alignfile == "") && (taxfile == "")) { m->mothurOut("You must provide a file."); m->mothurOutEndLine(); abort = true; }
183 if (outputDir != "") { okay++; }
184 if (inputDir != "") { okay++; }
186 if (parameters.size() > okay) { m->mothurOut("You may only enter one file."); m->mothurOutEndLine(); abort = true; }
190 catch(exception& e) {
191 m->errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
195 //**********************************************************************************************************************
197 int ListSeqsCommand::execute(){
200 if (abort == true) { if (calledHelp) { return 0; } return 2; }
202 //read functions fill names vector
203 if (fastafile != "") { inputFileName = fastafile; readFasta(); }
204 else if (namefile != "") { inputFileName = namefile; readName(); }
205 else if (groupfile != "") { inputFileName = groupfile; readGroup(); }
206 else if (alignfile != "") { inputFileName = alignfile; readAlign(); }
207 else if (listfile != "") { inputFileName = listfile; readList(); }
208 else if (taxfile != "") { inputFileName = taxfile; readTax(); }
210 if (m->control_pressed) { outputTypes.clear(); return 0; }
212 //sort in alphabetical order
213 sort(names.begin(), names.end());
215 if (outputDir == "") { outputDir += m->hasPath(inputFileName); }
217 string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + "accnos";
220 m->openOutputFile(outputFileName, out);
221 outputNames.push_back(outputFileName); outputTypes["accnos"].push_back(outputFileName);
223 //output to .accnos file
224 for (int i = 0; i < names.size(); i++) {
226 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(outputFileName.c_str()); return 0; }
228 out << names[i] << endl;
232 if (m->control_pressed) { outputTypes.clear(); remove(outputFileName.c_str()); return 0; }
234 m->setAccnosFile(outputFileName);
236 m->mothurOutEndLine();
237 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
238 m->mothurOut(outputFileName); m->mothurOutEndLine();
239 m->mothurOutEndLine();
241 //set accnos file as new current accnosfile
243 itTypes = outputTypes.find("accnos");
244 if (itTypes != outputTypes.end()) {
245 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setAccnosFile(current); }
251 catch(exception& e) {
252 m->errorOut(e, "ListSeqsCommand", "execute");
257 //**********************************************************************************************************************
258 int ListSeqsCommand::readFasta(){
262 m->openInputFile(fastafile, in);
266 //string newFastaName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "numsAdded.fasta";
267 //m->openOutputFile(newFastaName, out);
269 //string lastName = "";
273 if (m->control_pressed) { in.close(); return 0; }
275 Sequence currSeq(in);
276 name = currSeq.getName();
277 //if (lastName == "") { lastName = name; }
278 //if (name != lastName) { count = 1; }
281 //Sequence newSeq(name+"_"+toString(count), currSeq.getAligned());
282 //newSeq.printSequence(out);
284 if (name != "") { names.push_back(name); }
295 catch(exception& e) {
296 m->errorOut(e, "ListSeqsCommand", "readFasta");
300 //**********************************************************************************************************************
301 int ListSeqsCommand::readList(){
304 m->openInputFile(listfile, in);
307 //read in list vector
311 for (int i = 0; i < list.getNumBins(); i++) {
312 string binnames = list.get(i);
314 if (m->control_pressed) { in.close(); return 0; }
316 while (binnames.find_first_of(',') != -1) {
317 string name = binnames.substr(0,binnames.find_first_of(','));
318 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
319 names.push_back(name);
322 names.push_back(binnames);
330 catch(exception& e) {
331 m->errorOut(e, "ListSeqsCommand", "readList");
336 //**********************************************************************************************************************
337 int ListSeqsCommand::readName(){
341 m->openInputFile(namefile, in);
342 string name, firstCol, secondCol;
346 if (m->control_pressed) { in.close(); return 0; }
351 //parse second column saving each name
352 while (secondCol.find_first_of(',') != -1) {
353 name = secondCol.substr(0,secondCol.find_first_of(','));
354 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
355 names.push_back(name);
358 //get name after last ,
359 names.push_back(secondCol);
367 catch(exception& e) {
368 m->errorOut(e, "ListSeqsCommand", "readName");
373 //**********************************************************************************************************************
374 int ListSeqsCommand::readGroup(){
378 m->openInputFile(groupfile, in);
383 if (m->control_pressed) { in.close(); return 0; }
385 in >> name; m->gobble(in); //read from first column
386 in >> group; //read from second column
388 names.push_back(name);
396 catch(exception& e) {
397 m->errorOut(e, "ListSeqsCommand", "readGroup");
402 //**********************************************************************************************************************
403 //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
404 int ListSeqsCommand::readAlign(){
408 m->openInputFile(alignfile, in);
411 //read column headers
412 for (int i = 0; i < 16; i++) {
413 if (!in.eof()) { in >> junk; }
420 if (m->control_pressed) { in.close(); return 0; }
422 in >> name; //read from first column
425 for (int i = 0; i < 15; i++) {
426 if (!in.eof()) { in >> junk; }
430 names.push_back(name);
440 catch(exception& e) {
441 m->errorOut(e, "ListSeqsCommand", "readAlign");
445 //**********************************************************************************************************************
446 int ListSeqsCommand::readTax(){
450 m->openInputFile(taxfile, in);
451 string name, firstCol, secondCol;
455 if (m->control_pressed) { in.close(); return 0; }
460 names.push_back(firstCol);
470 catch(exception& e) {
471 m->errorOut(e, "ListSeqsCommand", "readTax");
475 //**********************************************************************************************************************