]> git.donarmstrong.com Git - mothur.git/blob - getseqscommand.cpp
fixed read.tree so that it can read trees generated by fasttree
[mothur.git] / getseqscommand.cpp
1 /*
2  *  getseqscommand.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 "getseqscommand.h"
11 #include "sequence.hpp"
12 #include "listvector.hpp"
13
14 //**********************************************************************************************************************
15
16 GetSeqsCommand::GetSeqsCommand(string option)  {
17         try {
18                 abort = false;
19                                 
20                 //allow user to run help
21                 if(option == "help") { help(); abort = true; }
22                 
23                 else {
24                         //valid paramters for this command
25                         string Array[] =  {"fasta","name", "group", "alignreport", "accnos", "list","taxonomy","outputdir","inputdir"};
26                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27                         
28                         OptionParser parser(option);
29                         map<string,string> parameters = parser.getParameters();
30                         
31                         ValidParameters validParameter;
32                         map<string,string>::iterator it;
33                         
34                         //check to make sure all parameters are valid for command
35                         for (it = parameters.begin(); it != parameters.end(); it++) { 
36                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
37                         }
38                         
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 = "";         }
41                         
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 = "";          }
45                         else {
46                                 string path;
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;              }
53                                 }
54                                 
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;            }
61                                 }
62                                 
63                                 it = parameters.find("accnos");
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["accnos"] = inputDir + it->second;           }
69                                 }
70                                 
71                                 it = parameters.find("list");
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["list"] = inputDir + it->second;             }
77                                 }
78                                 
79                                 it = parameters.find("name");
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["name"] = inputDir + it->second;             }
85                                 }
86                                 
87                                 it = parameters.find("group");
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["group"] = inputDir + it->second;            }
93                                 }
94                                 
95                                 it = parameters.find("taxonomy");
96                                 //user has given a template file
97                                 if(it != parameters.end()){ 
98                                         path = hasPath(it->second);
99                                         //if the user has not given a path then, add inputdir. else leave path alone.
100                                         if (path == "") {       parameters["taxonomy"] = inputDir + it->second;         }
101                                 }
102                         }
103
104                         
105                         //check for required parameters
106                         accnosfile = validParameter.validFile(parameters, "accnos", true);
107                         if (accnosfile == "not open") { abort = true; }
108                         else if (accnosfile == "not found") {  accnosfile = "";  m->mothurOut("You must provide an accnos file."); m->mothurOutEndLine(); abort = true; }       
109                         
110                         fastafile = validParameter.validFile(parameters, "fasta", true);
111                         if (fastafile == "not open") { abort = true; }
112                         else if (fastafile == "not found") {  fastafile = "";  }        
113                         
114                         namefile = validParameter.validFile(parameters, "name", true);
115                         if (namefile == "not open") { abort = true; }
116                         else if (namefile == "not found") {  namefile = "";  }  
117                         
118                         groupfile = validParameter.validFile(parameters, "group", true);
119                         if (groupfile == "not open") { abort = true; }
120                         else if (groupfile == "not found") {  groupfile = "";  }        
121                         
122                         alignfile = validParameter.validFile(parameters, "alignreport", true);
123                         if (alignfile == "not open") { abort = true; }
124                         else if (alignfile == "not found") {  alignfile = "";  }
125                         
126                         listfile = validParameter.validFile(parameters, "list", true);
127                         if (listfile == "not open") { abort = true; }
128                         else if (listfile == "not found") {  listfile = "";  }
129                         
130                         taxfile = validParameter.validFile(parameters, "taxonomy", true);
131                         if (taxfile == "not open") { abort = true; }
132                         else if (taxfile == "not found") {  taxfile = "";  }
133                         
134                         string usedDups = "true";
135                         string temp = validParameter.validFile(parameters, "dups", false);      if (temp == "not found") { temp = "false"; usedDups = ""; }
136                         dups = isTrue(temp);
137                         
138                         if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == "") && (taxfile == ""))  { m->mothurOut("You must provide one of the following: fasta, name, group, alignreport, taxonomy or listfile."); m->mothurOutEndLine(); abort = true; }
139                 
140                         if ((usedDups != "") && (namefile == "")) {  m->mothurOut("You may only use dups with the name option."); m->mothurOutEndLine();  abort = true; }                       
141
142                 }
143
144         }
145         catch(exception& e) {
146                 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
147                 exit(1);
148         }
149 }
150 //**********************************************************************************************************************
151
152 void GetSeqsCommand::help(){
153         try {
154                 m->mothurOut("The get.seqs command reads an .accnos file and any of the following file types: fasta, name, group, list, taxonomy or alignreport file.\n");
155                 m->mothurOut("It outputs a file containing only the sequences in the .accnos file.\n");
156                 m->mothurOut("The get.seqs command parameters are accnos, fasta, name, group, list, taxonomy, alignreport and dups.  You must provide accnos and at least one of the other parameters.\n");
157                 m->mothurOut("The dups parameter allows you to add the entire line from a name file if you add any name from the line. default=false. \n");
158                 m->mothurOut("The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
159                 m->mothurOut("Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
160                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
161         }
162         catch(exception& e) {
163                 m->errorOut(e, "GetSeqsCommand", "help");
164                 exit(1);
165         }
166 }
167
168 //**********************************************************************************************************************
169
170 int GetSeqsCommand::execute(){
171         try {
172                 
173                 if (abort == true) { return 0; }
174                 
175                 //get names you want to keep
176                 readAccnos();
177                 
178                 if (m->control_pressed) { return 0; }
179                 
180                 //read through the correct file and output lines you want to keep
181                 if (namefile != "")                     {               readName();             }
182                 if (fastafile != "")            {               readFasta();    }
183                 if (groupfile != "")            {               readGroup();    }
184                 if (alignfile != "")            {               readAlign();    }
185                 if (listfile != "")                     {               readList();             }
186                 if (taxfile != "")                      {               readTax();              }
187                 
188                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
189                 
190                 if (outputNames.size() != 0) {
191                         m->mothurOutEndLine();
192                         m->mothurOut("Output File Names: "); m->mothurOutEndLine();
193                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
194                         m->mothurOutEndLine();
195                 }
196                 
197                 return 0;               
198         }
199
200         catch(exception& e) {
201                 m->errorOut(e, "GetSeqsCommand", "execute");
202                 exit(1);
203         }
204 }
205
206 //**********************************************************************************************************************
207 int GetSeqsCommand::readFasta(){
208         try {
209                 if (outputDir == "") { outputDir += hasPath(fastafile); }
210                 string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + "pick" +  getExtension(fastafile);
211                 ofstream out;
212                 openOutputFile(outputFileName, out);
213                 
214                 
215                 ifstream in;
216                 openInputFile(fastafile, in);
217                 string name;
218                 
219                 bool wroteSomething = false;
220                 
221                 while(!in.eof()){
222                 
223                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
224                         
225                         Sequence currSeq(in);
226                         name = currSeq.getName();
227                         
228                         if (name != "") {
229                                 //if this name is in the accnos file
230                                 if (names.count(name) != 0) {
231                                         wroteSomething = true;
232                                         
233                                         currSeq.printSequence(out);
234                                 }
235                         }
236                         gobble(in);
237                 }
238                 in.close();     
239                 out.close();
240                 
241                 if (wroteSomething == false) {
242                         m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
243                         remove(outputFileName.c_str()); 
244                 }else {  outputNames.push_back(outputFileName); }
245                 
246                 return 0;
247
248         }
249         catch(exception& e) {
250                 m->errorOut(e, "GetSeqsCommand", "readFasta");
251                 exit(1);
252         }
253 }
254 //**********************************************************************************************************************
255 int GetSeqsCommand::readList(){
256         try {
257                 if (outputDir == "") { outputDir += hasPath(listfile); }
258                 string outputFileName = outputDir + getRootName(getSimpleName(listfile)) + "pick" +  getExtension(listfile);
259                 ofstream out;
260                 openOutputFile(outputFileName, out);
261                 
262                 ifstream in;
263                 openInputFile(listfile, in);
264                 
265                 bool wroteSomething = false;
266                 
267                 while(!in.eof()){
268                         
269                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
270
271                         //read in list vector
272                         ListVector list(in);
273                         
274                         //make a new list vector
275                         ListVector newList;
276                         newList.setLabel(list.getLabel());
277                         
278                         //for each bin
279                         for (int i = 0; i < list.getNumBins(); i++) {
280                         
281                                 //parse out names that are in accnos file
282                                 string binnames = list.get(i);
283                                 
284                                 string newNames = "";
285                                 while (binnames.find_first_of(',') != -1) { 
286                                         string name = binnames.substr(0,binnames.find_first_of(','));
287                                         binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
288                                         
289                                         //if that name is in the .accnos file, add it
290                                         if (names.count(name) != 0) {  newNames += name + ",";  }
291                                 }
292                         
293                                 //get last name
294                                 if (names.count(binnames) != 0) {  newNames += binnames + ",";  }
295
296                                 //if there are names in this bin add to new list
297                                 if (newNames != "") { 
298                                         newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
299                                         newList.push_back(newNames);    
300                                 }
301                         }
302                                 
303                         //print new listvector
304                         if (newList.getNumBins() != 0) {
305                                 wroteSomething = true;
306                                 newList.print(out);
307                         }
308                         
309                         gobble(in);
310                 }
311                 in.close();     
312                 out.close();
313                 
314                 if (wroteSomething == false) {
315                         m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
316                         remove(outputFileName.c_str()); 
317                 }else {  outputNames.push_back(outputFileName); }
318                 
319                 return 0;
320
321         }
322         catch(exception& e) {
323                 m->errorOut(e, "GetSeqsCommand", "readList");
324                 exit(1);
325         }
326 }
327 //**********************************************************************************************************************
328 int GetSeqsCommand::readName(){
329         try {
330                 if (outputDir == "") { outputDir += hasPath(namefile); }
331                 string outputFileName = outputDir + getRootName(getSimpleName(namefile)) + "pick" +  getExtension(namefile);
332                 ofstream out;
333                 openOutputFile(outputFileName, out);
334                 
335
336                 ifstream in;
337                 openInputFile(namefile, in);
338                 string name, firstCol, secondCol;
339                 
340                 bool wroteSomething = false;
341                 
342                 
343                 while(!in.eof()){
344                 
345                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
346
347                         in >> firstCol;                         
348                         in >> secondCol;
349                         
350                         string hold = "";
351                         if (dups) { hold = secondCol; }
352                         
353                         vector<string> parsedNames;
354                         //parse second column saving each name
355                         while (secondCol.find_first_of(',') != -1) { 
356                                 name = secondCol.substr(0,secondCol.find_first_of(','));
357                                 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
358                                 parsedNames.push_back(name);
359                         }
360                         
361                         //get name after last ,
362                         parsedNames.push_back(secondCol);
363                         
364                         vector<string> validSecond;
365                         for (int i = 0; i < parsedNames.size(); i++) {
366                                 if (names.count(parsedNames[i]) != 0) {
367                                         validSecond.push_back(parsedNames[i]);
368                                 }
369                         }
370
371                         if ((dups) && (validSecond.size() != 0)) { //dups = true and we want to add someone, then add everyone
372                                 for (int i = 0; i < parsedNames.size(); i++) {  names.insert(parsedNames[i]);  }
373                                 out << firstCol << '\t' << hold << endl;
374                                 wroteSomething = true;
375                         }else {
376                                 //if the name in the first column is in the set then print it and any other names in second column also in set
377                                 if (names.count(firstCol) != 0) {
378                                 
379                                         wroteSomething = true;
380                                         
381                                         out << firstCol << '\t';
382                                         
383                                         //you know you have at least one valid second since first column is valid
384                                         for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
385                                         out << validSecond[validSecond.size()-1] << endl;
386                                         
387                                 
388                                 //make first name in set you come to first column and then add the remaining names to second column
389                                 }else {
390                                         //you want part of this row
391                                         if (validSecond.size() != 0) {
392                                         
393                                                 wroteSomething = true;
394                                                 
395                                                 out << validSecond[0] << '\t';
396                                         
397                                                 //you know you have at least one valid second since first column is valid
398                                                 for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
399                                                 out << validSecond[validSecond.size()-1] << endl;
400                                         }
401                                 }
402                         }
403                         gobble(in);
404                 }
405                 in.close();
406                 out.close();
407                 
408                 if (wroteSomething == false) {
409                         m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
410                         remove(outputFileName.c_str()); 
411                 }else {  outputNames.push_back(outputFileName); }
412                 
413                 return 0;
414                 
415         }
416         catch(exception& e) {
417                 m->errorOut(e, "GetSeqsCommand", "readName");
418                 exit(1);
419         }
420 }
421
422 //**********************************************************************************************************************
423 int GetSeqsCommand::readGroup(){
424         try {
425                 if (outputDir == "") { outputDir += hasPath(groupfile); }
426                 string outputFileName = outputDir + getRootName(getSimpleName(groupfile)) + "pick" + getExtension(groupfile);
427                 ofstream out;
428                 openOutputFile(outputFileName, out);
429                 
430
431                 ifstream in;
432                 openInputFile(groupfile, in);
433                 string name, group;
434                 
435                 bool wroteSomething = false;
436                 
437                 while(!in.eof()){
438
439                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
440
441
442                         in >> name;                             //read from first column
443                         in >> group;                    //read from second column
444                         
445                         //if this name is in the accnos file
446                         if (names.count(name) != 0) {
447                                 wroteSomething = true;
448                                 
449                                 out << name << '\t' << group << endl;
450                         }
451                                         
452                         gobble(in);
453                 }
454                 in.close();
455                 out.close();
456                 
457                 if (wroteSomething == false) {
458                         m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
459                         remove(outputFileName.c_str()); 
460                 }else {  outputNames.push_back(outputFileName); }
461                 
462                 return 0;
463
464         }
465         catch(exception& e) {
466                 m->errorOut(e, "GetSeqsCommand", "readGroup");
467                 exit(1);
468         }
469 }
470 //**********************************************************************************************************************
471 int GetSeqsCommand::readTax(){
472         try {
473                 if (outputDir == "") { outputDir += hasPath(taxfile); }
474                 string outputFileName = outputDir + getRootName(getSimpleName(taxfile)) + "pick" + getExtension(taxfile);
475                 ofstream out;
476                 openOutputFile(outputFileName, out);
477                 
478                 ifstream in;
479                 openInputFile(taxfile, in);
480                 string name, tax;
481                 
482                 bool wroteSomething = false;
483                 
484                 while(!in.eof()){
485
486                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
487
488                         in >> name;                             //read from first column
489                         in >> tax;                      //read from second column
490                         
491                         //if this name is in the accnos file
492                         if (names.count(name) != 0) {
493                                 wroteSomething = true;
494                                 
495                                 out << name << '\t' << tax << endl;
496                         }
497                                         
498                         gobble(in);
499                 }
500                 in.close();
501                 out.close();
502                 
503                 if (wroteSomething == false) {
504                         m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
505                         remove(outputFileName.c_str()); 
506                 }else {  outputNames.push_back(outputFileName); }
507                 
508                 return 0;
509
510         }
511         catch(exception& e) {
512                 m->errorOut(e, "GetSeqsCommand", "readTax");
513                 exit(1);
514         }
515 }
516 //**********************************************************************************************************************
517 //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
518 int GetSeqsCommand::readAlign(){
519         try {
520                 if (outputDir == "") { outputDir += hasPath(alignfile); }
521                 string outputFileName = outputDir + getRootName(getSimpleName(alignfile)) + "pick.align.report";
522                 ofstream out;
523                 openOutputFile(outputFileName, out);
524                 
525
526                 ifstream in;
527                 openInputFile(alignfile, in);
528                 string name, junk;
529                 
530                 bool wroteSomething = false;
531                 
532                 //read column headers
533                 for (int i = 0; i < 16; i++) {  
534                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
535                         else                    {       break;                  }
536                 }
537                 out << endl;
538                 
539                 while(!in.eof()){
540                 
541                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
542
543
544                         in >> name;                             //read from first column
545                         
546                         //if this name is in the accnos file
547                         if (names.count(name) != 0) {
548                                 wroteSomething = true;
549                                 
550                                 out << name << '\t';
551                                 
552                                 //read rest
553                                 for (int i = 0; i < 15; i++) {  
554                                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
555                                         else                    {       break;                  }
556                                 }
557                                 out << endl;
558                                 
559                         }else {//still read just don't do anything with it
560                                 //read rest
561                                 for (int i = 0; i < 15; i++) {  
562                                         if (!in.eof())  {       in >> junk;             }
563                                         else                    {       break;                  }
564                                 }
565                         }
566                         
567                         gobble(in);
568                 }
569                 in.close();
570                 out.close();
571                 
572                 if (wroteSomething == false) {
573                         m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
574                         remove(outputFileName.c_str()); 
575                 }else {  outputNames.push_back(outputFileName); }
576                 
577                 return 0;
578                 
579         }
580         catch(exception& e) {
581                 m->errorOut(e, "GetSeqsCommand", "readAlign");
582                 exit(1);
583         }
584 }
585 //**********************************************************************************************************************
586
587 int GetSeqsCommand::readAccnos(){
588         try {
589                 
590                 ifstream in;
591                 openInputFile(accnosfile, in);
592                 string name;
593                 
594                 while(!in.eof()){
595                         in >> name;
596                                                 
597                         names.insert(name);
598                         
599                         gobble(in);
600                 }
601                 in.close();     
602                 
603                 return 0;
604
605         }
606         catch(exception& e) {
607                 m->errorOut(e, "GetSeqsCommand", "readAccnos");
608                 exit(1);
609         }
610 }
611
612 //**********************************************************************************************************************
613