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