]> git.donarmstrong.com Git - mothur.git/blob - getseqscommand.cpp
added taxonomy file to list of files list.seqs, get.seqs and remove.seqs can process
[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                         
135                         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; }
136                 }
137
138         }
139         catch(exception& e) {
140                 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
141                 exit(1);
142         }
143 }
144 //**********************************************************************************************************************
145
146 void GetSeqsCommand::help(){
147         try {
148                 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");
149                 m->mothurOut("It outputs a file containing only the sequences in the .accnos file.\n");
150                 m->mothurOut("The get.seqs command parameters are accnos, fasta, name, group, list, taxonomy and alignreport.  You must provide accnos and at least one of the other parameters.\n");
151                 m->mothurOut("The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
152                 m->mothurOut("Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
153                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
154         }
155         catch(exception& e) {
156                 m->errorOut(e, "GetSeqsCommand", "help");
157                 exit(1);
158         }
159 }
160
161 //**********************************************************************************************************************
162
163 int GetSeqsCommand::execute(){
164         try {
165                 
166                 if (abort == true) { return 0; }
167                 
168                 //get names you want to keep
169                 readAccnos();
170                 
171                 if (m->control_pressed) { return 0; }
172                 
173                 //read through the correct file and output lines you want to keep
174                 if (fastafile != "")            {               readFasta();    }
175                 if (namefile != "")                     {               readName();             }
176                 if (groupfile != "")            {               readGroup();    }
177                 if (alignfile != "")            {               readAlign();    }
178                 if (listfile != "")                     {               readList();             }
179                 if (taxfile != "")                      {               readTax();              }
180                 
181                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
182                 
183                 if (outputNames.size() != 0) {
184                         m->mothurOutEndLine();
185                         m->mothurOut("Output File Names: "); m->mothurOutEndLine();
186                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
187                         m->mothurOutEndLine();
188                 }
189                 
190                 return 0;               
191         }
192
193         catch(exception& e) {
194                 m->errorOut(e, "GetSeqsCommand", "execute");
195                 exit(1);
196         }
197 }
198
199 //**********************************************************************************************************************
200 int GetSeqsCommand::readFasta(){
201         try {
202                 if (outputDir == "") { outputDir += hasPath(fastafile); }
203                 string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + "pick" +  getExtension(fastafile);
204                 ofstream out;
205                 openOutputFile(outputFileName, out);
206                 
207                 
208                 ifstream in;
209                 openInputFile(fastafile, in);
210                 string name;
211                 
212                 bool wroteSomething = false;
213                 
214                 while(!in.eof()){
215                 
216                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
217                         
218                         Sequence currSeq(in);
219                         name = currSeq.getName();
220                         
221                         if (name != "") {
222                                 //if this name is in the accnos file
223                                 if (names.count(name) == 1) {
224                                         wroteSomething = true;
225                                         
226                                         currSeq.printSequence(out);
227                                 }
228                         }
229                         gobble(in);
230                 }
231                 in.close();     
232                 out.close();
233                 
234                 if (wroteSomething == false) {
235                         m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
236                         remove(outputFileName.c_str()); 
237                 }else {  outputNames.push_back(outputFileName); }
238                 
239                 return 0;
240
241         }
242         catch(exception& e) {
243                 m->errorOut(e, "GetSeqsCommand", "readFasta");
244                 exit(1);
245         }
246 }
247 //**********************************************************************************************************************
248 int GetSeqsCommand::readList(){
249         try {
250                 if (outputDir == "") { outputDir += hasPath(listfile); }
251                 string outputFileName = outputDir + getRootName(getSimpleName(listfile)) + "pick" +  getExtension(listfile);
252                 ofstream out;
253                 openOutputFile(outputFileName, out);
254                 
255                 ifstream in;
256                 openInputFile(listfile, in);
257                 
258                 bool wroteSomething = false;
259                 
260                 while(!in.eof()){
261                         
262                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
263
264                         //read in list vector
265                         ListVector list(in);
266                         
267                         //make a new list vector
268                         ListVector newList;
269                         newList.setLabel(list.getLabel());
270                         
271                         //for each bin
272                         for (int i = 0; i < list.getNumBins(); i++) {
273                         
274                                 //parse out names that are in accnos file
275                                 string binnames = list.get(i);
276                                 
277                                 string newNames = "";
278                                 while (binnames.find_first_of(',') != -1) { 
279                                         string name = binnames.substr(0,binnames.find_first_of(','));
280                                         binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
281                                         
282                                         //if that name is in the .accnos file, add it
283                                         if (names.count(name) == 1) {  newNames += name + ",";  }
284                                 }
285                         
286                                 //get last name
287                                 if (names.count(binnames) == 1) {  newNames += binnames + ",";  }
288
289                                 //if there are names in this bin add to new list
290                                 if (newNames != "") { 
291                                         newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
292                                         newList.push_back(newNames);    
293                                 }
294                         }
295                                 
296                         //print new listvector
297                         if (newList.getNumBins() != 0) {
298                                 wroteSomething = true;
299                                 newList.print(out);
300                         }
301                         
302                         gobble(in);
303                 }
304                 in.close();     
305                 out.close();
306                 
307                 if (wroteSomething == false) {
308                         m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
309                         remove(outputFileName.c_str()); 
310                 }else {  outputNames.push_back(outputFileName); }
311                 
312                 return 0;
313
314         }
315         catch(exception& e) {
316                 m->errorOut(e, "GetSeqsCommand", "readList");
317                 exit(1);
318         }
319 }
320 //**********************************************************************************************************************
321 int GetSeqsCommand::readName(){
322         try {
323                 if (outputDir == "") { outputDir += hasPath(namefile); }
324                 string outputFileName = outputDir + getRootName(getSimpleName(namefile)) + "pick" +  getExtension(namefile);
325                 ofstream out;
326                 openOutputFile(outputFileName, out);
327                 
328
329                 ifstream in;
330                 openInputFile(namefile, in);
331                 string name, firstCol, secondCol;
332                 
333                 bool wroteSomething = false;
334                 
335                 
336                 while(!in.eof()){
337                 
338                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
339
340                         in >> firstCol;                         
341                         in >> secondCol;                        
342                         
343                         vector<string> parsedNames;
344                         //parse second column saving each name
345                         while (secondCol.find_first_of(',') != -1) { 
346                                 name = secondCol.substr(0,secondCol.find_first_of(','));
347                                 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
348                                 parsedNames.push_back(name);
349                         }
350                         
351                         //get name after last ,
352                         parsedNames.push_back(secondCol);
353                         
354                         vector<string> validSecond;
355                         for (int i = 0; i < parsedNames.size(); i++) {
356                                 if (names.count(parsedNames[i]) == 1) {
357                                         validSecond.push_back(parsedNames[i]);
358                                 }
359                         }
360
361                         
362                         //if the name in the first column is in the set then print it and any other names in second column also in set
363                         if (names.count(firstCol) == 1) {
364                         
365                                 wroteSomething = true;
366                                 
367                                 out << firstCol << '\t';
368                                 
369                                 //you know you have at least one valid second since first column is valid
370                                 for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
371                                 out << validSecond[validSecond.size()-1] << endl;
372                                 
373                         
374                         //make first name in set you come to first column and then add the remaining names to second column
375                         }else {
376                                 //you want part of this row
377                                 if (validSecond.size() != 0) {
378                                 
379                                         wroteSomething = true;
380                                         
381                                         out << validSecond[0] << '\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                         
389                         gobble(in);
390                 }
391                 in.close();
392                 out.close();
393                 
394                 if (wroteSomething == false) {
395                         m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
396                         remove(outputFileName.c_str()); 
397                 }else {  outputNames.push_back(outputFileName); }
398                 
399                 return 0;
400                 
401         }
402         catch(exception& e) {
403                 m->errorOut(e, "GetSeqsCommand", "readName");
404                 exit(1);
405         }
406 }
407
408 //**********************************************************************************************************************
409 int GetSeqsCommand::readGroup(){
410         try {
411                 if (outputDir == "") { outputDir += hasPath(groupfile); }
412                 string outputFileName = outputDir + getRootName(getSimpleName(groupfile)) + "pick" + getExtension(groupfile);
413                 ofstream out;
414                 openOutputFile(outputFileName, out);
415                 
416
417                 ifstream in;
418                 openInputFile(groupfile, in);
419                 string name, group;
420                 
421                 bool wroteSomething = false;
422                 
423                 while(!in.eof()){
424
425                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
426
427
428                         in >> name;                             //read from first column
429                         in >> group;                    //read from second column
430                         
431                         //if this name is in the accnos file
432                         if (names.count(name) == 1) {
433                                 wroteSomething = true;
434                                 
435                                 out << name << '\t' << group << endl;
436                         }
437                                         
438                         gobble(in);
439                 }
440                 in.close();
441                 out.close();
442                 
443                 if (wroteSomething == false) {
444                         m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
445                         remove(outputFileName.c_str()); 
446                 }else {  outputNames.push_back(outputFileName); }
447                 
448                 return 0;
449
450         }
451         catch(exception& e) {
452                 m->errorOut(e, "GetSeqsCommand", "readGroup");
453                 exit(1);
454         }
455 }
456 //**********************************************************************************************************************
457 int GetSeqsCommand::readTax(){
458         try {
459                 if (outputDir == "") { outputDir += hasPath(taxfile); }
460                 string outputFileName = outputDir + getRootName(getSimpleName(taxfile)) + "pick" + getExtension(taxfile);
461                 ofstream out;
462                 openOutputFile(outputFileName, out);
463                 
464                 ifstream in;
465                 openInputFile(taxfile, in);
466                 string name, tax;
467                 
468                 bool wroteSomething = false;
469                 
470                 while(!in.eof()){
471
472                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
473
474                         in >> name;                             //read from first column
475                         in >> tax;                      //read from second column
476                         
477                         //if this name is in the accnos file
478                         if (names.count(name) == 1) {
479                                 wroteSomething = true;
480                                 
481                                 out << name << '\t' << tax << endl;
482                         }
483                                         
484                         gobble(in);
485                 }
486                 in.close();
487                 out.close();
488                 
489                 if (wroteSomething == false) {
490                         m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
491                         remove(outputFileName.c_str()); 
492                 }else {  outputNames.push_back(outputFileName); }
493                 
494                 return 0;
495
496         }
497         catch(exception& e) {
498                 m->errorOut(e, "GetSeqsCommand", "readTax");
499                 exit(1);
500         }
501 }
502 //**********************************************************************************************************************
503 //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
504 int GetSeqsCommand::readAlign(){
505         try {
506                 if (outputDir == "") { outputDir += hasPath(alignfile); }
507                 string outputFileName = outputDir + getRootName(getSimpleName(alignfile)) + "pick.align.report";
508                 ofstream out;
509                 openOutputFile(outputFileName, out);
510                 
511
512                 ifstream in;
513                 openInputFile(alignfile, in);
514                 string name, junk;
515                 
516                 bool wroteSomething = false;
517                 
518                 //read column headers
519                 for (int i = 0; i < 16; i++) {  
520                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
521                         else                    {       break;                  }
522                 }
523                 out << endl;
524                 
525                 while(!in.eof()){
526                 
527                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
528
529
530                         in >> name;                             //read from first column
531                         
532                         //if this name is in the accnos file
533                         if (names.count(name) == 1) {
534                                 wroteSomething = true;
535                                 
536                                 out << name << '\t';
537                                 
538                                 //read rest
539                                 for (int i = 0; i < 15; i++) {  
540                                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
541                                         else                    {       break;                  }
542                                 }
543                                 out << endl;
544                                 
545                         }else {//still read just don't do anything with it
546                                 //read rest
547                                 for (int i = 0; i < 15; i++) {  
548                                         if (!in.eof())  {       in >> junk;             }
549                                         else                    {       break;                  }
550                                 }
551                         }
552                         
553                         gobble(in);
554                 }
555                 in.close();
556                 out.close();
557                 
558                 if (wroteSomething == false) {
559                         m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
560                         remove(outputFileName.c_str()); 
561                 }else {  outputNames.push_back(outputFileName); }
562                 
563                 return 0;
564                 
565         }
566         catch(exception& e) {
567                 m->errorOut(e, "GetSeqsCommand", "readAlign");
568                 exit(1);
569         }
570 }
571 //**********************************************************************************************************************
572
573 int GetSeqsCommand::readAccnos(){
574         try {
575                 
576                 ifstream in;
577                 openInputFile(accnosfile, in);
578                 string name;
579                 
580                 while(!in.eof()){
581                         in >> name;
582                                                 
583                         names.insert(name);
584                         
585                         gobble(in);
586                 }
587                 in.close();     
588                 
589                 return 0;
590
591         }
592         catch(exception& e) {
593                 m->errorOut(e, "GetSeqsCommand", "readAccnos");
594                 exit(1);
595         }
596 }
597
598 //**********************************************************************************************************************
599