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(){
29 abort = true; calledHelp = true;
30 vector<string> tempOutNames;
31 outputTypes["accnos"] = tempOutNames;
34 m->errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
38 //**********************************************************************************************************************
39 vector<string> ListSeqsCommand::getRequiredParameters(){
41 string Array[] = {"fasta","name", "group", "alignreport","list","taxonomy","or"};
42 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
46 m->errorOut(e, "ListSeqsCommand", "getRequiredParameters");
50 //**********************************************************************************************************************
51 vector<string> ListSeqsCommand::getRequiredFiles(){
53 vector<string> myArray;
57 m->errorOut(e, "ListSeqsCommand", "getRequiredFiles");
61 //**********************************************************************************************************************
63 ListSeqsCommand::ListSeqsCommand(string option) {
65 abort = false; calledHelp = false;
67 //allow user to run help
68 if(option == "help") { help(); abort = true; calledHelp = true; }
71 //valid paramters for this command
72 string Array[] = {"fasta","name", "group", "alignreport","list","taxonomy","outputdir","inputdir"};
73 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
75 OptionParser parser(option);
76 map<string,string> parameters = parser.getParameters();
78 ValidParameters validParameter;
79 map<string,string>::iterator it;
81 //check to make sure all parameters are valid for command
82 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
83 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
86 //initialize outputTypes
87 vector<string> tempOutNames;
88 outputTypes["accnos"] = tempOutNames;
90 //if the user changes the output directory command factory will send this info to us in the output parameter
91 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
93 //if the user changes the input directory command factory will send this info to us in the output parameter
94 string inputDir = validParameter.validFile(parameters, "inputdir", false);
95 if (inputDir == "not found"){ inputDir = ""; }
98 it = parameters.find("alignreport");
99 //user has given a template file
100 if(it != parameters.end()){
101 path = m->hasPath(it->second);
102 //if the user has not given a path then, add inputdir. else leave path alone.
103 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
106 it = parameters.find("fasta");
107 //user has given a template file
108 if(it != parameters.end()){
109 path = m->hasPath(it->second);
110 //if the user has not given a path then, add inputdir. else leave path alone.
111 if (path == "") { parameters["fasta"] = inputDir + it->second; }
114 it = parameters.find("list");
115 //user has given a template file
116 if(it != parameters.end()){
117 path = m->hasPath(it->second);
118 //if the user has not given a path then, add inputdir. else leave path alone.
119 if (path == "") { parameters["list"] = inputDir + it->second; }
122 it = parameters.find("name");
123 //user has given a template file
124 if(it != parameters.end()){
125 path = m->hasPath(it->second);
126 //if the user has not given a path then, add inputdir. else leave path alone.
127 if (path == "") { parameters["name"] = inputDir + it->second; }
130 it = parameters.find("group");
131 //user has given a template file
132 if(it != parameters.end()){
133 path = m->hasPath(it->second);
134 //if the user has not given a path then, add inputdir. else leave path alone.
135 if (path == "") { parameters["group"] = inputDir + it->second; }
138 it = parameters.find("taxonomy");
139 //user has given a template file
140 if(it != parameters.end()){
141 path = m->hasPath(it->second);
142 //if the user has not given a path then, add inputdir. else leave path alone.
143 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
147 //check for required parameters
148 fastafile = validParameter.validFile(parameters, "fasta", true);
149 if (fastafile == "not open") { abort = true; }
150 else if (fastafile == "not found") { fastafile = ""; }
152 namefile = validParameter.validFile(parameters, "name", true);
153 if (namefile == "not open") { abort = true; }
154 else if (namefile == "not found") { namefile = ""; }
156 groupfile = validParameter.validFile(parameters, "group", true);
157 if (groupfile == "not open") { abort = true; }
158 else if (groupfile == "not found") { groupfile = ""; }
160 alignfile = validParameter.validFile(parameters, "alignreport", true);
161 if (alignfile == "not open") { abort = true; }
162 else if (alignfile == "not found") { alignfile = ""; }
164 listfile = validParameter.validFile(parameters, "list", true);
165 if (listfile == "not open") { abort = true; }
166 else if (listfile == "not found") { listfile = ""; }
168 taxfile = validParameter.validFile(parameters, "taxonomy", true);
169 if (taxfile == "not open") { abort = true; }
170 else if (taxfile == "not found") { taxfile = ""; }
172 if ((fastafile == "") && (namefile == "") && (listfile == "") && (groupfile == "") && (alignfile == "") && (taxfile == "")) { m->mothurOut("You must provide a file."); m->mothurOutEndLine(); abort = true; }
175 if (outputDir != "") { okay++; }
176 if (inputDir != "") { okay++; }
178 if (parameters.size() > okay) { m->mothurOut("You may only enter one file."); m->mothurOutEndLine(); abort = true; }
182 catch(exception& e) {
183 m->errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
187 //**********************************************************************************************************************
189 void ListSeqsCommand::help(){
191 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");
192 m->mothurOut("The list.seqs command parameters are fasta, name, group, list, taxonomy and alignreport. You must provide one of these parameters.\n");
193 m->mothurOut("The list.seqs command should be in the following format: list.seqs(fasta=yourFasta).\n");
194 m->mothurOut("Example list.seqs(fasta=amazon.fasta).\n");
195 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
197 catch(exception& e) {
198 m->errorOut(e, "ListSeqsCommand", "help");
203 //**********************************************************************************************************************
205 int ListSeqsCommand::execute(){
208 if (abort == true) { if (calledHelp) { return 0; } return 2; }
210 //read functions fill names vector
211 if (fastafile != "") { inputFileName = fastafile; readFasta(); }
212 else if (namefile != "") { inputFileName = namefile; readName(); }
213 else if (groupfile != "") { inputFileName = groupfile; readGroup(); }
214 else if (alignfile != "") { inputFileName = alignfile; readAlign(); }
215 else if (listfile != "") { inputFileName = listfile; readList(); }
216 else if (taxfile != "") { inputFileName = taxfile; readTax(); }
218 if (m->control_pressed) { outputTypes.clear(); return 0; }
220 //sort in alphabetical order
221 sort(names.begin(), names.end());
223 if (outputDir == "") { outputDir += m->hasPath(inputFileName); }
225 string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + "accnos";
228 m->openOutputFile(outputFileName, out);
230 //output to .accnos file
231 for (int i = 0; i < names.size(); i++) {
233 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(outputFileName.c_str()); return 0; }
235 out << names[i] << endl;
239 if (m->control_pressed) { outputTypes.clear(); remove(outputFileName.c_str()); return 0; }
241 m->mothurOutEndLine();
242 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
243 m->mothurOut(outputFileName); m->mothurOutEndLine(); outputNames.push_back(outputFileName); outputTypes["accnos"].push_back(outputFileName);
244 m->mothurOutEndLine();
249 catch(exception& e) {
250 m->errorOut(e, "ListSeqsCommand", "execute");
255 //**********************************************************************************************************************
256 int ListSeqsCommand::readFasta(){
260 m->openInputFile(fastafile, in);
265 if (m->control_pressed) { in.close(); return 0; }
267 Sequence currSeq(in);
268 name = currSeq.getName();
270 if (name != "") { names.push_back(name); }
279 catch(exception& e) {
280 m->errorOut(e, "ListSeqsCommand", "readFasta");
284 //**********************************************************************************************************************
285 int ListSeqsCommand::readList(){
288 m->openInputFile(listfile, in);
291 //read in list vector
295 for (int i = 0; i < list.getNumBins(); i++) {
296 string binnames = list.get(i);
298 if (m->control_pressed) { in.close(); return 0; }
300 while (binnames.find_first_of(',') != -1) {
301 string name = binnames.substr(0,binnames.find_first_of(','));
302 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
303 names.push_back(name);
306 names.push_back(binnames);
314 catch(exception& e) {
315 m->errorOut(e, "ListSeqsCommand", "readList");
320 //**********************************************************************************************************************
321 int ListSeqsCommand::readName(){
325 m->openInputFile(namefile, in);
326 string name, firstCol, secondCol;
330 if (m->control_pressed) { in.close(); return 0; }
335 //parse second column saving each name
336 while (secondCol.find_first_of(',') != -1) {
337 name = secondCol.substr(0,secondCol.find_first_of(','));
338 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
339 names.push_back(name);
342 //get name after last ,
343 names.push_back(secondCol);
351 catch(exception& e) {
352 m->errorOut(e, "ListSeqsCommand", "readName");
357 //**********************************************************************************************************************
358 int ListSeqsCommand::readGroup(){
362 m->openInputFile(groupfile, in);
367 if (m->control_pressed) { in.close(); return 0; }
369 in >> name; m->gobble(in); //read from first column
370 in >> group; //read from second column
372 names.push_back(name);
380 catch(exception& e) {
381 m->errorOut(e, "ListSeqsCommand", "readGroup");
386 //**********************************************************************************************************************
387 //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
388 int ListSeqsCommand::readAlign(){
392 m->openInputFile(alignfile, in);
395 //read column headers
396 for (int i = 0; i < 16; i++) {
397 if (!in.eof()) { in >> junk; }
404 if (m->control_pressed) { in.close(); return 0; }
406 in >> name; //read from first column
409 for (int i = 0; i < 15; i++) {
410 if (!in.eof()) { in >> junk; }
414 names.push_back(name);
424 catch(exception& e) {
425 m->errorOut(e, "ListSeqsCommand", "readAlign");
429 //**********************************************************************************************************************
430 int ListSeqsCommand::readTax(){
434 m->openInputFile(taxfile, in);
435 string name, firstCol, secondCol;
439 if (m->control_pressed) { in.close(); return 0; }
444 names.push_back(firstCol);
453 catch(exception& e) {
454 m->errorOut(e, "ListSeqsCommand", "readTax");
458 //**********************************************************************************************************************