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