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"
14 //**********************************************************************************************************************
15 vector<string> ListSeqsCommand::getValidParameters(){
17 string Array[] = {"fasta","name", "group", "alignreport","list","taxonomy","outputdir","inputdir"};
18 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
22 m->errorOut(e, "ListSeqsCommand", "getValidParameters");
26 //**********************************************************************************************************************
27 ListSeqsCommand::ListSeqsCommand(){
30 //initialize outputTypes
31 vector<string> tempOutNames;
32 outputTypes["accnos"] = tempOutNames;
35 m->errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
39 //**********************************************************************************************************************
40 vector<string> ListSeqsCommand::getRequiredParameters(){
42 string Array[] = {"fasta","name", "group", "alignreport","list","taxonomy","or"};
43 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
47 m->errorOut(e, "ListSeqsCommand", "getRequiredParameters");
51 //**********************************************************************************************************************
52 vector<string> ListSeqsCommand::getRequiredFiles(){
54 vector<string> myArray;
58 m->errorOut(e, "ListSeqsCommand", "getRequiredFiles");
62 //**********************************************************************************************************************
64 ListSeqsCommand::ListSeqsCommand(string option) {
68 //allow user to run help
69 if(option == "help") { help(); abort = true; }
72 //valid paramters for this command
73 string Array[] = {"fasta","name", "group", "alignreport","list","taxonomy","outputdir","inputdir"};
74 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
76 OptionParser parser(option);
77 map<string,string> parameters = parser.getParameters();
79 ValidParameters validParameter;
80 map<string,string>::iterator it;
82 //check to make sure all parameters are valid for command
83 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
84 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
87 //initialize outputTypes
88 vector<string> tempOutNames;
89 outputTypes["accnos"] = tempOutNames;
91 //if the user changes the output directory command factory will send this info to us in the output parameter
92 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
94 //if the user changes the input directory command factory will send this info to us in the output parameter
95 string inputDir = validParameter.validFile(parameters, "inputdir", false);
96 if (inputDir == "not found"){ inputDir = ""; }
99 it = parameters.find("alignreport");
100 //user has given a template file
101 if(it != parameters.end()){
102 path = m->hasPath(it->second);
103 //if the user has not given a path then, add inputdir. else leave path alone.
104 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
107 it = parameters.find("fasta");
108 //user has given a template file
109 if(it != parameters.end()){
110 path = m->hasPath(it->second);
111 //if the user has not given a path then, add inputdir. else leave path alone.
112 if (path == "") { parameters["fasta"] = inputDir + it->second; }
115 it = parameters.find("list");
116 //user has given a template file
117 if(it != parameters.end()){
118 path = m->hasPath(it->second);
119 //if the user has not given a path then, add inputdir. else leave path alone.
120 if (path == "") { parameters["list"] = inputDir + it->second; }
123 it = parameters.find("name");
124 //user has given a template file
125 if(it != parameters.end()){
126 path = m->hasPath(it->second);
127 //if the user has not given a path then, add inputdir. else leave path alone.
128 if (path == "") { parameters["name"] = inputDir + it->second; }
131 it = parameters.find("group");
132 //user has given a template file
133 if(it != parameters.end()){
134 path = m->hasPath(it->second);
135 //if the user has not given a path then, add inputdir. else leave path alone.
136 if (path == "") { parameters["group"] = inputDir + it->second; }
139 it = parameters.find("taxonomy");
140 //user has given a template file
141 if(it != parameters.end()){
142 path = m->hasPath(it->second);
143 //if the user has not given a path then, add inputdir. else leave path alone.
144 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
148 //check for required parameters
149 fastafile = validParameter.validFile(parameters, "fasta", true);
150 if (fastafile == "not open") { abort = true; }
151 else if (fastafile == "not found") { fastafile = ""; }
153 namefile = validParameter.validFile(parameters, "name", true);
154 if (namefile == "not open") { abort = true; }
155 else if (namefile == "not found") { namefile = ""; }
157 groupfile = validParameter.validFile(parameters, "group", true);
158 if (groupfile == "not open") { abort = true; }
159 else if (groupfile == "not found") { groupfile = ""; }
161 alignfile = validParameter.validFile(parameters, "alignreport", true);
162 if (alignfile == "not open") { abort = true; }
163 else if (alignfile == "not found") { alignfile = ""; }
165 listfile = validParameter.validFile(parameters, "list", true);
166 if (listfile == "not open") { abort = true; }
167 else if (listfile == "not found") { listfile = ""; }
169 taxfile = validParameter.validFile(parameters, "taxonomy", true);
170 if (taxfile == "not open") { abort = true; }
171 else if (taxfile == "not found") { taxfile = ""; }
173 if ((fastafile == "") && (namefile == "") && (listfile == "") && (groupfile == "") && (alignfile == "") && (taxfile == "")) { m->mothurOut("You must provide a file."); m->mothurOutEndLine(); abort = true; }
176 if (outputDir != "") { okay++; }
177 if (inputDir != "") { okay++; }
179 if (parameters.size() > okay) { m->mothurOut("You may only enter one file."); m->mothurOutEndLine(); abort = true; }
183 catch(exception& e) {
184 m->errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
188 //**********************************************************************************************************************
190 void ListSeqsCommand::help(){
192 m->mothurOut("The list.seqs command reads a fasta, name, group, list, taxonomy or alignreport file and outputs a .accnos file containing sequence names.\n");
193 m->mothurOut("The list.seqs command parameters are fasta, name, group, list, taxonomy and alignreport. You must provide one of these parameters.\n");
194 m->mothurOut("The list.seqs command should be in the following format: list.seqs(fasta=yourFasta).\n");
195 m->mothurOut("Example list.seqs(fasta=amazon.fasta).\n");
196 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
198 catch(exception& e) {
199 m->errorOut(e, "ListSeqsCommand", "help");
204 //**********************************************************************************************************************
206 int ListSeqsCommand::execute(){
209 if (abort == true) { return 0; }
211 //read functions fill names vector
212 if (fastafile != "") { inputFileName = fastafile; readFasta(); }
213 else if (namefile != "") { inputFileName = namefile; readName(); }
214 else if (groupfile != "") { inputFileName = groupfile; readGroup(); }
215 else if (alignfile != "") { inputFileName = alignfile; readAlign(); }
216 else if (listfile != "") { inputFileName = listfile; readList(); }
217 else if (taxfile != "") { inputFileName = taxfile; readTax(); }
219 if (m->control_pressed) { outputTypes.clear(); return 0; }
221 //sort in alphabetical order
222 sort(names.begin(), names.end());
224 if (outputDir == "") { outputDir += m->hasPath(inputFileName); }
226 string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + "accnos";
229 m->openOutputFile(outputFileName, out);
231 //output to .accnos file
232 for (int i = 0; i < names.size(); i++) {
234 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(outputFileName.c_str()); return 0; }
236 out << names[i] << endl;
240 if (m->control_pressed) { outputTypes.clear(); remove(outputFileName.c_str()); return 0; }
242 m->mothurOutEndLine();
243 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
244 m->mothurOut(outputFileName); m->mothurOutEndLine(); outputNames.push_back(outputFileName); outputTypes["accnos"].push_back(outputFileName);
245 m->mothurOutEndLine();
250 catch(exception& e) {
251 m->errorOut(e, "ListSeqsCommand", "execute");
256 //**********************************************************************************************************************
257 int ListSeqsCommand::readFasta(){
261 m->openInputFile(fastafile, in);
266 if (m->control_pressed) { in.close(); return 0; }
268 Sequence currSeq(in);
269 name = currSeq.getName();
271 if (name != "") { names.push_back(name); }
280 catch(exception& e) {
281 m->errorOut(e, "ListSeqsCommand", "readFasta");
285 //**********************************************************************************************************************
286 int ListSeqsCommand::readList(){
289 m->openInputFile(listfile, in);
292 //read in list vector
296 for (int i = 0; i < list.getNumBins(); i++) {
297 string binnames = list.get(i);
299 if (m->control_pressed) { in.close(); return 0; }
301 while (binnames.find_first_of(',') != -1) {
302 string name = binnames.substr(0,binnames.find_first_of(','));
303 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
304 names.push_back(name);
307 names.push_back(binnames);
315 catch(exception& e) {
316 m->errorOut(e, "ListSeqsCommand", "readList");
321 //**********************************************************************************************************************
322 int ListSeqsCommand::readName(){
326 m->openInputFile(namefile, in);
327 string name, firstCol, secondCol;
331 if (m->control_pressed) { in.close(); return 0; }
336 //parse second column saving each name
337 while (secondCol.find_first_of(',') != -1) {
338 name = secondCol.substr(0,secondCol.find_first_of(','));
339 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
340 names.push_back(name);
343 //get name after last ,
344 names.push_back(secondCol);
352 catch(exception& e) {
353 m->errorOut(e, "ListSeqsCommand", "readName");
358 //**********************************************************************************************************************
359 int ListSeqsCommand::readGroup(){
363 m->openInputFile(groupfile, in);
368 if (m->control_pressed) { in.close(); return 0; }
370 in >> name; m->gobble(in); //read from first column
371 in >> group; //read from second column
373 names.push_back(name);
381 catch(exception& e) {
382 m->errorOut(e, "ListSeqsCommand", "readGroup");
387 //**********************************************************************************************************************
388 //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
389 int ListSeqsCommand::readAlign(){
393 m->openInputFile(alignfile, in);
396 //read column headers
397 for (int i = 0; i < 16; i++) {
398 if (!in.eof()) { in >> junk; }
405 if (m->control_pressed) { in.close(); return 0; }
407 in >> name; //read from first column
410 for (int i = 0; i < 15; i++) {
411 if (!in.eof()) { in >> junk; }
415 names.push_back(name);
425 catch(exception& e) {
426 m->errorOut(e, "ListSeqsCommand", "readAlign");
430 //**********************************************************************************************************************
431 int ListSeqsCommand::readTax(){
435 m->openInputFile(taxfile, in);
436 string name, firstCol, secondCol;
440 if (m->control_pressed) { in.close(); return 0; }
445 names.push_back(firstCol);
454 catch(exception& e) {
455 m->errorOut(e, "ListSeqsCommand", "readTax");
459 //**********************************************************************************************************************