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