]> git.donarmstrong.com Git - mothur.git/blob - listseqscommand.cpp
added pipeline commands which involved change to command factory and command class...
[mothur.git] / listseqscommand.cpp
1 /*
2  *  listseqscommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 7/8/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "listseqscommand.h"
11 #include "sequence.hpp"
12 #include "listvector.hpp"
13
14 //**********************************************************************************************************************
15 vector<string> ListSeqsCommand::getValidParameters(){   
16         try {
17                 string Array[] =  {"fasta","name", "group", "alignreport","list","taxonomy","outputdir","inputdir"};
18                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
19                 return myArray;
20         }
21         catch(exception& e) {
22                 m->errorOut(e, "ListSeqsCommand", "getValidParameters");
23                 exit(1);
24         }
25 }
26 //**********************************************************************************************************************
27 ListSeqsCommand::ListSeqsCommand(){     
28         try {
29                 //initialize outputTypes
30                 vector<string> tempOutNames;
31                 outputTypes["accnos"] = tempOutNames;
32         }
33         catch(exception& e) {
34                 m->errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
35                 exit(1);
36         }
37 }
38 //**********************************************************************************************************************
39 vector<string> ListSeqsCommand::getRequiredParameters(){        
40         try {
41                 string Array[] =  {"fasta","name", "group", "alignreport","list","taxonomy","or"};
42                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
43                 return myArray;
44         }
45         catch(exception& e) {
46                 m->errorOut(e, "ListSeqsCommand", "getRequiredParameters");
47                 exit(1);
48         }
49 }
50 //**********************************************************************************************************************
51 vector<string> ListSeqsCommand::getRequiredFiles(){     
52         try {
53                 vector<string> myArray;
54                 return myArray;
55         }
56         catch(exception& e) {
57                 m->errorOut(e, "ListSeqsCommand", "getRequiredFiles");
58                 exit(1);
59         }
60 }
61 //**********************************************************************************************************************
62
63 ListSeqsCommand::ListSeqsCommand(string option)  {
64         try {
65                 abort = false;
66                 
67                 //allow user to run help
68                 if(option == "help") { help(); abort = true; }
69                 
70                 else {
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)));
74                         
75                         OptionParser parser(option);
76                         map<string,string> parameters = parser.getParameters();
77                         
78                         ValidParameters validParameter;
79                         map<string,string>::iterator it;
80                         
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;  }
84                         }
85                         
86                         //initialize outputTypes
87                         vector<string> tempOutNames;
88                         outputTypes["accnos"] = tempOutNames;
89                         
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 = "";         }
92                         
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 = "";          }
96                         else {
97                                 string path;
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;              }
104                                 }
105                                 
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;            }
112                                 }
113                                 
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;             }
120                                 }
121                                 
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;             }
128                                 }
129                                 
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;            }
136                                 }
137                                 
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;         }
144                                 }
145                         }
146
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 = "";  }        
151                         
152                         namefile = validParameter.validFile(parameters, "name", true);
153                         if (namefile == "not open") { abort = true; }
154                         else if (namefile == "not found") {  namefile = "";  }  
155                         
156                         groupfile = validParameter.validFile(parameters, "group", true);
157                         if (groupfile == "not open") { abort = true; }
158                         else if (groupfile == "not found") {  groupfile = "";  }        
159                         
160                         alignfile = validParameter.validFile(parameters, "alignreport", true);
161                         if (alignfile == "not open") { abort = true; }
162                         else if (alignfile == "not found") {  alignfile = "";  }
163                         
164                         listfile = validParameter.validFile(parameters, "list", true);
165                         if (listfile == "not open") { abort = true; }
166                         else if (listfile == "not found") {  listfile = "";  }
167
168                         taxfile = validParameter.validFile(parameters, "taxonomy", true);
169                         if (taxfile == "not open") { abort = true; }
170                         else if (taxfile == "not found") {  taxfile = "";  }
171                         
172                         if ((fastafile == "") && (namefile == "") && (listfile == "") && (groupfile == "") && (alignfile == "") && (taxfile == ""))  { m->mothurOut("You must provide a file."); m->mothurOutEndLine(); abort = true; }
173                         
174                         int okay = 1;
175                         if (outputDir != "") { okay++; }
176                         if (inputDir != "") { okay++; }
177                         
178                         if (parameters.size() > okay) { m->mothurOut("You may only enter one file."); m->mothurOutEndLine(); abort = true;  }
179                 }
180
181         }
182         catch(exception& e) {
183                 m->errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
184                 exit(1);
185         }
186 }
187 //**********************************************************************************************************************
188
189 void ListSeqsCommand::help(){
190         try {
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");
196         }
197         catch(exception& e) {
198                 m->errorOut(e, "ListSeqsCommand", "help");
199                 exit(1);
200         }
201 }
202
203 //**********************************************************************************************************************
204
205 int ListSeqsCommand::execute(){
206         try {
207                 
208                 if (abort == true) { return 0; }
209                 
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();              }
217                 
218                 if (m->control_pressed) { outputTypes.clear();  return 0; }
219                 
220                 //sort in alphabetical order
221                 sort(names.begin(), names.end());
222                 
223                 if (outputDir == "") {  outputDir += m->hasPath(inputFileName);  }
224                 
225                 string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + "accnos";
226
227                 ofstream out;
228                 m->openOutputFile(outputFileName, out);
229                 
230                 //output to .accnos file
231                 for (int i = 0; i < names.size(); i++) {
232                         
233                         if (m->control_pressed) { outputTypes.clear(); out.close(); remove(outputFileName.c_str()); return 0; }
234                         
235                         out << names[i] << endl;
236                 }
237                 out.close();
238                 
239                 if (m->control_pressed) { outputTypes.clear();  remove(outputFileName.c_str()); return 0; }
240
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();
245                 
246                 return 0;               
247         }
248
249         catch(exception& e) {
250                 m->errorOut(e, "ListSeqsCommand", "execute");
251                 exit(1);
252         }
253 }
254
255 //**********************************************************************************************************************
256 int ListSeqsCommand::readFasta(){
257         try {
258                 
259                 ifstream in;
260                 m->openInputFile(fastafile, in);
261                 string name;
262         
263                 while(!in.eof()){
264                         
265                         if (m->control_pressed) { in.close(); return 0; }
266                         
267                         Sequence currSeq(in);
268                         name = currSeq.getName();
269                         
270                         if (name != "") {  names.push_back(name);  }
271                         
272                         m->gobble(in);
273                 }
274                 in.close();     
275                 
276                 return 0;
277
278         }
279         catch(exception& e) {
280                 m->errorOut(e, "ListSeqsCommand", "readFasta");
281                 exit(1);
282         }
283 }
284 //**********************************************************************************************************************
285 int ListSeqsCommand::readList(){
286         try {
287                 ifstream in;
288                 m->openInputFile(listfile, in);
289                 
290                 if(!in.eof()){
291                         //read in list vector
292                         ListVector list(in);
293                         
294                         //for each bin
295                         for (int i = 0; i < list.getNumBins(); i++) {
296                                 string binnames = list.get(i);
297                                 
298                                 if (m->control_pressed) { in.close(); return 0; }
299                                 
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);
304                                 }
305                         
306                                 names.push_back(binnames);
307                         }
308                 }
309                 in.close();     
310                 
311                 return 0;
312                 
313         }
314         catch(exception& e) {
315                 m->errorOut(e, "ListSeqsCommand", "readList");
316                 exit(1);
317         }
318 }
319
320 //**********************************************************************************************************************
321 int ListSeqsCommand::readName(){
322         try {
323                 
324                 ifstream in;
325                 m->openInputFile(namefile, in);
326                 string name, firstCol, secondCol;
327                 
328                 while(!in.eof()){
329                 
330                         if (m->control_pressed) { in.close(); return 0; }
331
332                         in >> firstCol;                         
333                         in >> secondCol;                        
334                         
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);
340                         }
341                         
342                         //get name after last ,
343                         names.push_back(secondCol);
344                         
345                         m->gobble(in);
346                 }
347                 in.close();
348                 return 0;
349                 
350         }
351         catch(exception& e) {
352                 m->errorOut(e, "ListSeqsCommand", "readName");
353                 exit(1);
354         }
355 }
356
357 //**********************************************************************************************************************
358 int ListSeqsCommand::readGroup(){
359         try {
360         
361                 ifstream in;
362                 m->openInputFile(groupfile, in);
363                 string name, group;
364                 
365                 while(!in.eof()){
366                         
367                         if (m->control_pressed) { in.close(); return 0; }
368                         
369                         in >> name;                             //read from first column
370                         in >> group;                    //read from second column
371                         
372                         names.push_back(name);
373                                         
374                         m->gobble(in);
375                 }
376                 in.close();
377                 return 0;
378
379         }
380         catch(exception& e) {
381                 m->errorOut(e, "ListSeqsCommand", "readGroup");
382                 exit(1);
383         }
384 }
385
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(){
389         try {
390         
391                 ifstream in;
392                 m->openInputFile(alignfile, in);
393                 string name, junk;
394                 
395                 //read column headers
396                 for (int i = 0; i < 16; i++) {  
397                         if (!in.eof())  {       in >> junk;             }
398                         else                    {       break;                  }
399                 }
400                 
401                 
402                 while(!in.eof()){
403                 
404                         if (m->control_pressed) { in.close(); return 0; }
405
406                         in >> name;                             //read from first column
407                         
408                         //read rest
409                         for (int i = 0; i < 15; i++) {  
410                                 if (!in.eof())  {       in >> junk;             }
411                                 else                    {       break;                  }
412                         }
413                         
414                         names.push_back(name);
415                                         
416                         m->gobble(in);
417                 }
418                 in.close();
419                 
420                 return 0;
421
422                 
423         }
424         catch(exception& e) {
425                 m->errorOut(e, "ListSeqsCommand", "readAlign");
426                 exit(1);
427         }
428 }
429 //**********************************************************************************************************************
430 int ListSeqsCommand::readTax(){
431         try {
432                 
433                 ifstream in;
434                 m->openInputFile(taxfile, in);
435                 string name, firstCol, secondCol;
436                 
437                 while(!in.eof()){
438                 
439                         if (m->control_pressed) { in.close(); return 0; }
440
441                         in >> firstCol;                         
442                         in >> secondCol;                        
443                         
444                         names.push_back(firstCol);
445                         
446                         m->gobble(in);
447                 }
448                 in.close();
449                 
450                 return 0;
451                 
452         }
453         catch(exception& e) {
454                 m->errorOut(e, "ListSeqsCommand", "readTax");
455                 exit(1);
456         }
457 }
458 //**********************************************************************************************************************