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