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);
229 outputNames.push_back(outputFileName); outputTypes["accnos"].push_back(outputFileName);
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->setAccnosFile(outputFileName);
244 m->mothurOutEndLine();
245 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
246 m->mothurOut(outputFileName); m->mothurOutEndLine();
247 m->mothurOutEndLine();
249 //set accnos file as new current accnosfile
251 itTypes = outputTypes.find("accnos");
252 if (itTypes != outputTypes.end()) {
253 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setAccnosFile(current); }
259 catch(exception& e) {
260 m->errorOut(e, "ListSeqsCommand", "execute");
265 //**********************************************************************************************************************
266 int ListSeqsCommand::readFasta(){
270 m->openInputFile(fastafile, in);
275 if (m->control_pressed) { in.close(); return 0; }
277 Sequence currSeq(in);
278 name = currSeq.getName();
280 if (name != "") { names.push_back(name); }
289 catch(exception& e) {
290 m->errorOut(e, "ListSeqsCommand", "readFasta");
294 //**********************************************************************************************************************
295 int ListSeqsCommand::readList(){
298 m->openInputFile(listfile, in);
301 //read in list vector
305 for (int i = 0; i < list.getNumBins(); i++) {
306 string binnames = list.get(i);
308 if (m->control_pressed) { in.close(); return 0; }
310 while (binnames.find_first_of(',') != -1) {
311 string name = binnames.substr(0,binnames.find_first_of(','));
312 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
313 names.push_back(name);
316 names.push_back(binnames);
324 catch(exception& e) {
325 m->errorOut(e, "ListSeqsCommand", "readList");
330 //**********************************************************************************************************************
331 int ListSeqsCommand::readName(){
335 m->openInputFile(namefile, in);
336 string name, firstCol, secondCol;
340 if (m->control_pressed) { in.close(); return 0; }
345 //parse second column saving each name
346 while (secondCol.find_first_of(',') != -1) {
347 name = secondCol.substr(0,secondCol.find_first_of(','));
348 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
349 names.push_back(name);
352 //get name after last ,
353 names.push_back(secondCol);
361 catch(exception& e) {
362 m->errorOut(e, "ListSeqsCommand", "readName");
367 //**********************************************************************************************************************
368 int ListSeqsCommand::readGroup(){
372 m->openInputFile(groupfile, in);
377 if (m->control_pressed) { in.close(); return 0; }
379 in >> name; m->gobble(in); //read from first column
380 in >> group; //read from second column
382 names.push_back(name);
390 catch(exception& e) {
391 m->errorOut(e, "ListSeqsCommand", "readGroup");
396 //**********************************************************************************************************************
397 //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
398 int ListSeqsCommand::readAlign(){
402 m->openInputFile(alignfile, in);
405 //read column headers
406 for (int i = 0; i < 16; i++) {
407 if (!in.eof()) { in >> junk; }
414 if (m->control_pressed) { in.close(); return 0; }
416 in >> name; //read from first column
419 for (int i = 0; i < 15; i++) {
420 if (!in.eof()) { in >> junk; }
424 names.push_back(name);
434 catch(exception& e) {
435 m->errorOut(e, "ListSeqsCommand", "readAlign");
439 //**********************************************************************************************************************
440 int ListSeqsCommand::readTax(){
444 m->openInputFile(taxfile, in);
445 string name, firstCol, secondCol;
449 if (m->control_pressed) { in.close(); return 0; }
454 names.push_back(firstCol);
464 catch(exception& e) {
465 m->errorOut(e, "ListSeqsCommand", "readTax");
469 //**********************************************************************************************************************