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