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 //**********************************************************************************************************************
16 ListSeqsCommand::ListSeqsCommand(string option) {
20 //allow user to run help
21 if(option == "help") { help(); abort = true; }
24 //valid paramters for this command
25 string Array[] = {"fasta","name", "group", "alignreport","list","taxonomy","outputdir","inputdir"};
26 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
28 OptionParser parser(option);
29 map<string,string> parameters = parser.getParameters();
31 ValidParameters validParameter;
32 map<string,string>::iterator it;
34 //check to make sure all parameters are valid for command
35 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
36 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
39 //if the user changes the output directory command factory will send this info to us in the output parameter
40 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
42 //if the user changes the input directory command factory will send this info to us in the output parameter
43 string inputDir = validParameter.validFile(parameters, "inputdir", false);
44 if (inputDir == "not found"){ inputDir = ""; }
47 it = parameters.find("alignreport");
48 //user has given a template file
49 if(it != parameters.end()){
50 path = hasPath(it->second);
51 //if the user has not given a path then, add inputdir. else leave path alone.
52 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
55 it = parameters.find("fasta");
56 //user has given a template file
57 if(it != parameters.end()){
58 path = hasPath(it->second);
59 //if the user has not given a path then, add inputdir. else leave path alone.
60 if (path == "") { parameters["fasta"] = inputDir + it->second; }
63 it = parameters.find("list");
64 //user has given a template file
65 if(it != parameters.end()){
66 path = hasPath(it->second);
67 //if the user has not given a path then, add inputdir. else leave path alone.
68 if (path == "") { parameters["list"] = inputDir + it->second; }
71 it = parameters.find("name");
72 //user has given a template file
73 if(it != parameters.end()){
74 path = hasPath(it->second);
75 //if the user has not given a path then, add inputdir. else leave path alone.
76 if (path == "") { parameters["name"] = inputDir + it->second; }
79 it = parameters.find("group");
80 //user has given a template file
81 if(it != parameters.end()){
82 path = hasPath(it->second);
83 //if the user has not given a path then, add inputdir. else leave path alone.
84 if (path == "") { parameters["group"] = inputDir + it->second; }
87 it = parameters.find("taxonomy");
88 //user has given a template file
89 if(it != parameters.end()){
90 path = hasPath(it->second);
91 //if the user has not given a path then, add inputdir. else leave path alone.
92 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
96 //check for required parameters
97 fastafile = validParameter.validFile(parameters, "fasta", true);
98 if (fastafile == "not open") { abort = true; }
99 else if (fastafile == "not found") { fastafile = ""; }
101 namefile = validParameter.validFile(parameters, "name", true);
102 if (namefile == "not open") { abort = true; }
103 else if (namefile == "not found") { namefile = ""; }
105 groupfile = validParameter.validFile(parameters, "group", true);
106 if (groupfile == "not open") { abort = true; }
107 else if (groupfile == "not found") { groupfile = ""; }
109 alignfile = validParameter.validFile(parameters, "alignreport", true);
110 if (alignfile == "not open") { abort = true; }
111 else if (alignfile == "not found") { alignfile = ""; }
113 listfile = validParameter.validFile(parameters, "list", true);
114 if (listfile == "not open") { abort = true; }
115 else if (listfile == "not found") { listfile = ""; }
117 taxfile = validParameter.validFile(parameters, "taxonomy", true);
118 if (taxfile == "not open") { abort = true; }
119 else if (taxfile == "not found") { taxfile = ""; }
121 if ((fastafile == "") && (namefile == "") && (listfile == "") && (groupfile == "") && (alignfile == "") && (taxfile == "")) { m->mothurOut("You must provide a file."); m->mothurOutEndLine(); abort = true; }
124 if (outputDir != "") { okay++; }
125 if (inputDir != "") { okay++; }
127 if (parameters.size() > okay) { m->mothurOut("You may only enter one file."); m->mothurOutEndLine(); abort = true; }
131 catch(exception& e) {
132 m->errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
136 //**********************************************************************************************************************
138 void ListSeqsCommand::help(){
140 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");
141 m->mothurOut("The list.seqs command parameters are fasta, name, group, list, taxonomy and alignreport. You must provide one of these parameters.\n");
142 m->mothurOut("The list.seqs command should be in the following format: list.seqs(fasta=yourFasta).\n");
143 m->mothurOut("Example list.seqs(fasta=amazon.fasta).\n");
144 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
146 catch(exception& e) {
147 m->errorOut(e, "ListSeqsCommand", "help");
152 //**********************************************************************************************************************
154 int ListSeqsCommand::execute(){
157 if (abort == true) { return 0; }
159 //read functions fill names vector
160 if (fastafile != "") { inputFileName = fastafile; readFasta(); }
161 else if (namefile != "") { inputFileName = namefile; readName(); }
162 else if (groupfile != "") { inputFileName = groupfile; readGroup(); }
163 else if (alignfile != "") { inputFileName = alignfile; readAlign(); }
164 else if (listfile != "") { inputFileName = listfile; readList(); }
165 else if (taxfile != "") { inputFileName = taxfile; readTax(); }
167 if (m->control_pressed) { return 0; }
169 //sort in alphabetical order
170 sort(names.begin(), names.end());
172 if (outputDir == "") { outputDir += hasPath(inputFileName); }
174 string outputFileName = outputDir + getRootName(getSimpleName(inputFileName)) + "accnos";
177 openOutputFile(outputFileName, out);
179 //output to .accnos file
180 for (int i = 0; i < names.size(); i++) {
182 if (m->control_pressed) { out.close(); remove(outputFileName.c_str()); return 0; }
184 out << names[i] << endl;
188 if (m->control_pressed) { remove(outputFileName.c_str()); return 0; }
190 m->mothurOutEndLine();
191 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
192 m->mothurOut(outputFileName); m->mothurOutEndLine();
193 m->mothurOutEndLine();
198 catch(exception& e) {
199 m->errorOut(e, "ListSeqsCommand", "execute");
204 //**********************************************************************************************************************
205 int ListSeqsCommand::readFasta(){
209 openInputFile(fastafile, in);
214 if (m->control_pressed) { in.close(); return 0; }
216 Sequence currSeq(in);
217 name = currSeq.getName();
219 if (name != "") { names.push_back(name); }
228 catch(exception& e) {
229 m->errorOut(e, "ListSeqsCommand", "readFasta");
233 //**********************************************************************************************************************
234 int ListSeqsCommand::readList(){
237 openInputFile(listfile, in);
240 //read in list vector
244 for (int i = 0; i < list.getNumBins(); i++) {
245 string binnames = list.get(i);
247 if (m->control_pressed) { in.close(); return 0; }
249 while (binnames.find_first_of(',') != -1) {
250 string name = binnames.substr(0,binnames.find_first_of(','));
251 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
252 names.push_back(name);
255 names.push_back(binnames);
263 catch(exception& e) {
264 m->errorOut(e, "ListSeqsCommand", "readList");
269 //**********************************************************************************************************************
270 int ListSeqsCommand::readName(){
274 openInputFile(namefile, in);
275 string name, firstCol, secondCol;
279 if (m->control_pressed) { in.close(); return 0; }
284 //parse second column saving each name
285 while (secondCol.find_first_of(',') != -1) {
286 name = secondCol.substr(0,secondCol.find_first_of(','));
287 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
288 names.push_back(name);
291 //get name after last ,
292 names.push_back(secondCol);
300 catch(exception& e) {
301 m->errorOut(e, "ListSeqsCommand", "readName");
306 //**********************************************************************************************************************
307 int ListSeqsCommand::readGroup(){
311 openInputFile(groupfile, in);
316 if (m->control_pressed) { in.close(); return 0; }
318 in >> name; //read from first column
319 in >> group; //read from second column
321 names.push_back(name);
329 catch(exception& e) {
330 m->errorOut(e, "ListSeqsCommand", "readGroup");
335 //**********************************************************************************************************************
336 //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
337 int ListSeqsCommand::readAlign(){
341 openInputFile(alignfile, in);
344 //read column headers
345 for (int i = 0; i < 16; i++) {
346 if (!in.eof()) { in >> junk; }
353 if (m->control_pressed) { in.close(); return 0; }
355 in >> name; //read from first column
358 for (int i = 0; i < 15; i++) {
359 if (!in.eof()) { in >> junk; }
363 names.push_back(name);
373 catch(exception& e) {
374 m->errorOut(e, "ListSeqsCommand", "readAlign");
378 //**********************************************************************************************************************
379 int ListSeqsCommand::readTax(){
383 openInputFile(taxfile, in);
384 string name, firstCol, secondCol;
388 if (m->control_pressed) { in.close(); return 0; }
393 names.push_back(firstCol);
402 catch(exception& e) {
403 m->errorOut(e, "ListSeqsCommand", "readTax");
407 //**********************************************************************************************************************