]> git.donarmstrong.com Git - mothur.git/blob - listseqscommand.cpp
parsimony calculator changed order of groups to reflect change in shared Utils made...
[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                 abort = true;
30                 //initialize outputTypes
31                 vector<string> tempOutNames;
32                 outputTypes["accnos"] = tempOutNames;
33         }
34         catch(exception& e) {
35                 m->errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
36                 exit(1);
37         }
38 }
39 //**********************************************************************************************************************
40 vector<string> ListSeqsCommand::getRequiredParameters(){        
41         try {
42                 string Array[] =  {"fasta","name", "group", "alignreport","list","taxonomy","or"};
43                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
44                 return myArray;
45         }
46         catch(exception& e) {
47                 m->errorOut(e, "ListSeqsCommand", "getRequiredParameters");
48                 exit(1);
49         }
50 }
51 //**********************************************************************************************************************
52 vector<string> ListSeqsCommand::getRequiredFiles(){     
53         try {
54                 vector<string> myArray;
55                 return myArray;
56         }
57         catch(exception& e) {
58                 m->errorOut(e, "ListSeqsCommand", "getRequiredFiles");
59                 exit(1);
60         }
61 }
62 //**********************************************************************************************************************
63
64 ListSeqsCommand::ListSeqsCommand(string option)  {
65         try {
66                 abort = false;
67                 
68                 //allow user to run help
69                 if(option == "help") { help(); abort = true; }
70                 
71                 else {
72                         //valid paramters for this command
73                         string Array[] =  {"fasta","name", "group", "alignreport","list","taxonomy","outputdir","inputdir"};
74                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
75                         
76                         OptionParser parser(option);
77                         map<string,string> parameters = parser.getParameters();
78                         
79                         ValidParameters validParameter;
80                         map<string,string>::iterator it;
81                         
82                         //check to make sure all parameters are valid for command
83                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
84                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
85                         }
86                         
87                         //initialize outputTypes
88                         vector<string> tempOutNames;
89                         outputTypes["accnos"] = tempOutNames;
90                         
91                         //if the user changes the output directory command factory will send this info to us in the output parameter 
92                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
93                         
94                         //if the user changes the input directory command factory will send this info to us in the output parameter 
95                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
96                         if (inputDir == "not found"){   inputDir = "";          }
97                         else {
98                                 string path;
99                                 it = parameters.find("alignreport");
100                                 //user has given a template file
101                                 if(it != parameters.end()){ 
102                                         path = m->hasPath(it->second);
103                                         //if the user has not given a path then, add inputdir. else leave path alone.
104                                         if (path == "") {       parameters["alignreport"] = inputDir + it->second;              }
105                                 }
106                                 
107                                 it = parameters.find("fasta");
108                                 //user has given a template file
109                                 if(it != parameters.end()){ 
110                                         path = m->hasPath(it->second);
111                                         //if the user has not given a path then, add inputdir. else leave path alone.
112                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
113                                 }
114                                 
115                                 it = parameters.find("list");
116                                 //user has given a template file
117                                 if(it != parameters.end()){ 
118                                         path = m->hasPath(it->second);
119                                         //if the user has not given a path then, add inputdir. else leave path alone.
120                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
121                                 }
122                                 
123                                 it = parameters.find("name");
124                                 //user has given a template file
125                                 if(it != parameters.end()){ 
126                                         path = m->hasPath(it->second);
127                                         //if the user has not given a path then, add inputdir. else leave path alone.
128                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
129                                 }
130                                 
131                                 it = parameters.find("group");
132                                 //user has given a template file
133                                 if(it != parameters.end()){ 
134                                         path = m->hasPath(it->second);
135                                         //if the user has not given a path then, add inputdir. else leave path alone.
136                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
137                                 }
138                                 
139                                 it = parameters.find("taxonomy");
140                                 //user has given a template file
141                                 if(it != parameters.end()){ 
142                                         path = m->hasPath(it->second);
143                                         //if the user has not given a path then, add inputdir. else leave path alone.
144                                         if (path == "") {       parameters["taxonomy"] = inputDir + it->second;         }
145                                 }
146                         }
147
148                         //check for required parameters
149                         fastafile = validParameter.validFile(parameters, "fasta", true);
150                         if (fastafile == "not open") { abort = true; }
151                         else if (fastafile == "not found") {  fastafile = "";  }        
152                         
153                         namefile = validParameter.validFile(parameters, "name", true);
154                         if (namefile == "not open") { abort = true; }
155                         else if (namefile == "not found") {  namefile = "";  }  
156                         
157                         groupfile = validParameter.validFile(parameters, "group", true);
158                         if (groupfile == "not open") { abort = true; }
159                         else if (groupfile == "not found") {  groupfile = "";  }        
160                         
161                         alignfile = validParameter.validFile(parameters, "alignreport", true);
162                         if (alignfile == "not open") { abort = true; }
163                         else if (alignfile == "not found") {  alignfile = "";  }
164                         
165                         listfile = validParameter.validFile(parameters, "list", true);
166                         if (listfile == "not open") { abort = true; }
167                         else if (listfile == "not found") {  listfile = "";  }
168
169                         taxfile = validParameter.validFile(parameters, "taxonomy", true);
170                         if (taxfile == "not open") { abort = true; }
171                         else if (taxfile == "not found") {  taxfile = "";  }
172                         
173                         if ((fastafile == "") && (namefile == "") && (listfile == "") && (groupfile == "") && (alignfile == "") && (taxfile == ""))  { m->mothurOut("You must provide a file."); m->mothurOutEndLine(); abort = true; }
174                         
175                         int okay = 1;
176                         if (outputDir != "") { okay++; }
177                         if (inputDir != "") { okay++; }
178                         
179                         if (parameters.size() > okay) { m->mothurOut("You may only enter one file."); m->mothurOutEndLine(); abort = true;  }
180                 }
181
182         }
183         catch(exception& e) {
184                 m->errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
185                 exit(1);
186         }
187 }
188 //**********************************************************************************************************************
189
190 void ListSeqsCommand::help(){
191         try {
192                 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");
193                 m->mothurOut("The list.seqs command parameters are fasta, name, group, list, taxonomy and alignreport.  You must provide one of these parameters.\n");
194                 m->mothurOut("The list.seqs command should be in the following format: list.seqs(fasta=yourFasta).\n");
195                 m->mothurOut("Example list.seqs(fasta=amazon.fasta).\n");
196                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
197         }
198         catch(exception& e) {
199                 m->errorOut(e, "ListSeqsCommand", "help");
200                 exit(1);
201         }
202 }
203
204 //**********************************************************************************************************************
205
206 int ListSeqsCommand::execute(){
207         try {
208                 
209                 if (abort == true) { return 0; }
210                 
211                 //read functions fill names vector
212                 if (fastafile != "")            {       inputFileName = fastafile;      readFasta();    }
213                 else if (namefile != "")        {       inputFileName = namefile;       readName();             }
214                 else if (groupfile != "")       {       inputFileName = groupfile;      readGroup();    }
215                 else if (alignfile != "")       {       inputFileName = alignfile;      readAlign();    }
216                 else if (listfile != "")        {       inputFileName = listfile;       readList();             }
217                 else if (taxfile != "")         {       inputFileName = taxfile;        readTax();              }
218                 
219                 if (m->control_pressed) { outputTypes.clear();  return 0; }
220                 
221                 //sort in alphabetical order
222                 sort(names.begin(), names.end());
223                 
224                 if (outputDir == "") {  outputDir += m->hasPath(inputFileName);  }
225                 
226                 string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + "accnos";
227
228                 ofstream out;
229                 m->openOutputFile(outputFileName, out);
230                 
231                 //output to .accnos file
232                 for (int i = 0; i < names.size(); i++) {
233                         
234                         if (m->control_pressed) { outputTypes.clear(); out.close(); remove(outputFileName.c_str()); return 0; }
235                         
236                         out << names[i] << endl;
237                 }
238                 out.close();
239                 
240                 if (m->control_pressed) { outputTypes.clear();  remove(outputFileName.c_str()); return 0; }
241
242                 m->mothurOutEndLine();
243                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
244                 m->mothurOut(outputFileName); m->mothurOutEndLine();    outputNames.push_back(outputFileName); outputTypes["accnos"].push_back(outputFileName);
245                 m->mothurOutEndLine();
246                 
247                 return 0;               
248         }
249
250         catch(exception& e) {
251                 m->errorOut(e, "ListSeqsCommand", "execute");
252                 exit(1);
253         }
254 }
255
256 //**********************************************************************************************************************
257 int ListSeqsCommand::readFasta(){
258         try {
259                 
260                 ifstream in;
261                 m->openInputFile(fastafile, in);
262                 string name;
263         
264                 while(!in.eof()){
265                         
266                         if (m->control_pressed) { in.close(); return 0; }
267                         
268                         Sequence currSeq(in);
269                         name = currSeq.getName();
270                         
271                         if (name != "") {  names.push_back(name);  }
272                         
273                         m->gobble(in);
274                 }
275                 in.close();     
276                 
277                 return 0;
278
279         }
280         catch(exception& e) {
281                 m->errorOut(e, "ListSeqsCommand", "readFasta");
282                 exit(1);
283         }
284 }
285 //**********************************************************************************************************************
286 int ListSeqsCommand::readList(){
287         try {
288                 ifstream in;
289                 m->openInputFile(listfile, in);
290                 
291                 if(!in.eof()){
292                         //read in list vector
293                         ListVector list(in);
294                         
295                         //for each bin
296                         for (int i = 0; i < list.getNumBins(); i++) {
297                                 string binnames = list.get(i);
298                                 
299                                 if (m->control_pressed) { in.close(); return 0; }
300                                 
301                                 while (binnames.find_first_of(',') != -1) { 
302                                         string name = binnames.substr(0,binnames.find_first_of(','));
303                                         binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
304                                         names.push_back(name);
305                                 }
306                         
307                                 names.push_back(binnames);
308                         }
309                 }
310                 in.close();     
311                 
312                 return 0;
313                 
314         }
315         catch(exception& e) {
316                 m->errorOut(e, "ListSeqsCommand", "readList");
317                 exit(1);
318         }
319 }
320
321 //**********************************************************************************************************************
322 int ListSeqsCommand::readName(){
323         try {
324                 
325                 ifstream in;
326                 m->openInputFile(namefile, in);
327                 string name, firstCol, secondCol;
328                 
329                 while(!in.eof()){
330                 
331                         if (m->control_pressed) { in.close(); return 0; }
332
333                         in >> firstCol;                         
334                         in >> secondCol;                        
335                         
336                         //parse second column saving each name
337                         while (secondCol.find_first_of(',') != -1) { 
338                                 name = secondCol.substr(0,secondCol.find_first_of(','));
339                                 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
340                                 names.push_back(name);
341                         }
342                         
343                         //get name after last ,
344                         names.push_back(secondCol);
345                         
346                         m->gobble(in);
347                 }
348                 in.close();
349                 return 0;
350                 
351         }
352         catch(exception& e) {
353                 m->errorOut(e, "ListSeqsCommand", "readName");
354                 exit(1);
355         }
356 }
357
358 //**********************************************************************************************************************
359 int ListSeqsCommand::readGroup(){
360         try {
361         
362                 ifstream in;
363                 m->openInputFile(groupfile, in);
364                 string name, group;
365                 
366                 while(!in.eof()){
367                         
368                         if (m->control_pressed) { in.close(); return 0; }
369                         
370                         in >> name;     m->gobble(in);                  //read from first column
371                         in >> group;                    //read from second column
372                         
373                         names.push_back(name);
374                                         
375                         m->gobble(in);
376                 }
377                 in.close();
378                 return 0;
379
380         }
381         catch(exception& e) {
382                 m->errorOut(e, "ListSeqsCommand", "readGroup");
383                 exit(1);
384         }
385 }
386
387 //**********************************************************************************************************************
388 //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
389 int ListSeqsCommand::readAlign(){
390         try {
391         
392                 ifstream in;
393                 m->openInputFile(alignfile, in);
394                 string name, junk;
395                 
396                 //read column headers
397                 for (int i = 0; i < 16; i++) {  
398                         if (!in.eof())  {       in >> junk;             }
399                         else                    {       break;                  }
400                 }
401                 
402                 
403                 while(!in.eof()){
404                 
405                         if (m->control_pressed) { in.close(); return 0; }
406
407                         in >> name;                             //read from first column
408                         
409                         //read rest
410                         for (int i = 0; i < 15; i++) {  
411                                 if (!in.eof())  {       in >> junk;             }
412                                 else                    {       break;                  }
413                         }
414                         
415                         names.push_back(name);
416                                         
417                         m->gobble(in);
418                 }
419                 in.close();
420                 
421                 return 0;
422
423                 
424         }
425         catch(exception& e) {
426                 m->errorOut(e, "ListSeqsCommand", "readAlign");
427                 exit(1);
428         }
429 }
430 //**********************************************************************************************************************
431 int ListSeqsCommand::readTax(){
432         try {
433                 
434                 ifstream in;
435                 m->openInputFile(taxfile, in);
436                 string name, firstCol, secondCol;
437                 
438                 while(!in.eof()){
439                 
440                         if (m->control_pressed) { in.close(); return 0; }
441
442                         in >> firstCol;                         
443                         in >> secondCol;                        
444                         
445                         names.push_back(firstCol);
446                         
447                         m->gobble(in);
448                 }
449                 in.close();
450                 
451                 return 0;
452                 
453         }
454         catch(exception& e) {
455                 m->errorOut(e, "ListSeqsCommand", "readTax");
456                 exit(1);
457         }
458 }
459 //**********************************************************************************************************************