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","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; }
88 //check for required parameters
89 fastafile = validParameter.validFile(parameters, "fasta", true);
90 if (fastafile == "not open") { abort = true; }
91 else if (fastafile == "not found") { fastafile = ""; }
93 namefile = validParameter.validFile(parameters, "name", true);
94 if (namefile == "not open") { abort = true; }
95 else if (namefile == "not found") { namefile = ""; }
97 groupfile = validParameter.validFile(parameters, "group", true);
98 if (groupfile == "not open") { abort = true; }
99 else if (groupfile == "not found") { groupfile = ""; }
101 alignfile = validParameter.validFile(parameters, "alignreport", true);
102 if (alignfile == "not open") { abort = true; }
103 else if (alignfile == "not found") { alignfile = ""; }
105 listfile = validParameter.validFile(parameters, "list", true);
106 if (listfile == "not open") { abort = true; }
107 else if (listfile == "not found") { listfile = ""; }
110 if ((fastafile == "") && (namefile == "") && (listfile == "") && (groupfile == "") && (alignfile == "")) { m->mothurOut("You must provide a file."); m->mothurOutEndLine(); abort = true; }
113 if (outputDir != "") { okay++; }
115 if (parameters.size() > okay) { m->mothurOut("You may only enter one file."); m->mothurOutEndLine(); abort = true; }
119 catch(exception& e) {
120 m->errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
124 //**********************************************************************************************************************
126 void ListSeqsCommand::help(){
128 m->mothurOut("The list.seqs command reads a fasta, name, group, list or alignreport file and outputs a .accnos file containing sequence names.\n");
129 m->mothurOut("The list.seqs command parameters are fasta, name, group and alignreport. You must provide one of these parameters.\n");
130 m->mothurOut("The list.seqs command should be in the following format: list.seqs(fasta=yourFasta).\n");
131 m->mothurOut("Example list.seqs(fasta=amazon.fasta).\n");
132 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
134 catch(exception& e) {
135 m->errorOut(e, "ListSeqsCommand", "help");
140 //**********************************************************************************************************************
142 int ListSeqsCommand::execute(){
145 if (abort == true) { return 0; }
147 //read functions fill names vector
148 if (fastafile != "") { inputFileName = fastafile; readFasta(); }
149 else if (namefile != "") { inputFileName = namefile; readName(); }
150 else if (groupfile != "") { inputFileName = groupfile; readGroup(); }
151 else if (alignfile != "") { inputFileName = alignfile; readAlign(); }
152 else if (listfile != "") { inputFileName = listfile; readList(); }
154 if (m->control_pressed) { return 0; }
156 //sort in alphabetical order
157 sort(names.begin(), names.end());
159 if (outputDir == "") { outputDir += hasPath(inputFileName); }
161 string outputFileName = outputDir + getRootName(getSimpleName(inputFileName)) + "accnos";
164 openOutputFile(outputFileName, out);
166 //output to .accnos file
167 for (int i = 0; i < names.size(); i++) {
169 if (m->control_pressed) { out.close(); remove(outputFileName.c_str()); return 0; }
171 out << names[i] << endl;
175 if (m->control_pressed) { remove(outputFileName.c_str()); return 0; }
177 m->mothurOutEndLine();
178 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
179 m->mothurOut(outputFileName); m->mothurOutEndLine();
180 m->mothurOutEndLine();
185 catch(exception& e) {
186 m->errorOut(e, "ListSeqsCommand", "execute");
191 //**********************************************************************************************************************
192 int ListSeqsCommand::readFasta(){
196 openInputFile(fastafile, in);
201 if (m->control_pressed) { in.close(); return 0; }
203 Sequence currSeq(in);
204 name = currSeq.getName();
206 if (name != "") { names.push_back(name); }
215 catch(exception& e) {
216 m->errorOut(e, "ListSeqsCommand", "readFasta");
220 //**********************************************************************************************************************
221 int ListSeqsCommand::readList(){
224 openInputFile(listfile, in);
227 //read in list vector
231 for (int i = 0; i < list.getNumBins(); i++) {
232 string binnames = list.get(i);
234 if (m->control_pressed) { in.close(); return 0; }
236 while (binnames.find_first_of(',') != -1) {
237 string name = binnames.substr(0,binnames.find_first_of(','));
238 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
239 names.push_back(name);
242 names.push_back(binnames);
250 catch(exception& e) {
251 m->errorOut(e, "ListSeqsCommand", "readList");
256 //**********************************************************************************************************************
257 int ListSeqsCommand::readName(){
261 openInputFile(namefile, in);
262 string name, firstCol, secondCol;
266 if (m->control_pressed) { in.close(); return 0; }
271 //parse second column saving each name
272 while (secondCol.find_first_of(',') != -1) {
273 name = secondCol.substr(0,secondCol.find_first_of(','));
274 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
275 names.push_back(name);
278 //get name after last ,
279 names.push_back(secondCol);
287 catch(exception& e) {
288 m->errorOut(e, "ListSeqsCommand", "readName");
293 //**********************************************************************************************************************
294 int ListSeqsCommand::readGroup(){
298 openInputFile(groupfile, in);
303 if (m->control_pressed) { in.close(); return 0; }
305 in >> name; //read from first column
306 in >> group; //read from second column
308 names.push_back(name);
316 catch(exception& e) {
317 m->errorOut(e, "ListSeqsCommand", "readGroup");
322 //**********************************************************************************************************************
323 //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
324 int ListSeqsCommand::readAlign(){
328 openInputFile(alignfile, in);
331 //read column headers
332 for (int i = 0; i < 16; i++) {
333 if (!in.eof()) { in >> junk; }
340 if (m->control_pressed) { in.close(); return 0; }
342 in >> name; //read from first column
345 for (int i = 0; i < 15; i++) {
346 if (!in.eof()) { in >> junk; }
350 names.push_back(name);
360 catch(exception& e) {
361 m->errorOut(e, "ListSeqsCommand", "readAlign");
365 //**********************************************************************************************************************