]> git.donarmstrong.com Git - mothur.git/blob - getseqscommand.cpp
added checks for ^C to quit command instead of program
[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 != "") {  newList.push_back(newNames);     }
284                         }
285                                 
286                         //print new listvector
287                         if (newList.getNumBins() != 0) {
288                                 wroteSomething = true;
289                                 newList.print(out);
290                         }
291                         
292                         gobble(in);
293                 }
294                 in.close();     
295                 out.close();
296                 
297                 if (wroteSomething == false) {
298                         m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
299                         remove(outputFileName.c_str()); 
300                 }else {  outputNames.push_back(outputFileName); }
301                 
302                 return 0;
303
304         }
305         catch(exception& e) {
306                 m->errorOut(e, "GetSeqsCommand", "readList");
307                 exit(1);
308         }
309 }
310 //**********************************************************************************************************************
311 int GetSeqsCommand::readName(){
312         try {
313                 if (outputDir == "") { outputDir += hasPath(namefile); }
314                 string outputFileName = outputDir + getRootName(getSimpleName(namefile)) + "pick" +  getExtension(namefile);
315                 ofstream out;
316                 openOutputFile(outputFileName, out);
317                 
318
319                 ifstream in;
320                 openInputFile(namefile, in);
321                 string name, firstCol, secondCol;
322                 
323                 bool wroteSomething = false;
324                 
325                 
326                 while(!in.eof()){
327                 
328                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
329
330                         in >> firstCol;                         
331                         in >> secondCol;                        
332                         
333                         vector<string> parsedNames;
334                         //parse second column saving each name
335                         while (secondCol.find_first_of(',') != -1) { 
336                                 name = secondCol.substr(0,secondCol.find_first_of(','));
337                                 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
338                                 parsedNames.push_back(name);
339                         }
340                         
341                         //get name after last ,
342                         parsedNames.push_back(secondCol);
343                         
344                         vector<string> validSecond;
345                         for (int i = 0; i < parsedNames.size(); i++) {
346                                 if (names.count(parsedNames[i]) == 1) {
347                                         validSecond.push_back(parsedNames[i]);
348                                 }
349                         }
350
351                         
352                         //if the name in the first column is in the set then print it and any other names in second column also in set
353                         if (names.count(firstCol) == 1) {
354                         
355                                 wroteSomething = true;
356                                 
357                                 out << firstCol << '\t';
358                                 
359                                 //you know you have at least one valid second since first column is valid
360                                 for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
361                                 out << validSecond[validSecond.size()-1] << endl;
362                                 
363                         
364                         //make first name in set you come to first column and then add the remaining names to second column
365                         }else {
366                                 //you want part of this row
367                                 if (validSecond.size() != 0) {
368                                 
369                                         wroteSomething = true;
370                                         
371                                         out << validSecond[0] << '\t';
372                                 
373                                         //you know you have at least one valid second since first column is valid
374                                         for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
375                                         out << validSecond[validSecond.size()-1] << endl;
376                                 }
377                         }
378                         
379                         gobble(in);
380                 }
381                 in.close();
382                 out.close();
383                 
384                 if (wroteSomething == false) {
385                         m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
386                         remove(outputFileName.c_str()); 
387                 }else {  outputNames.push_back(outputFileName); }
388                 
389                 return 0;
390                 
391         }
392         catch(exception& e) {
393                 m->errorOut(e, "GetSeqsCommand", "readName");
394                 exit(1);
395         }
396 }
397
398 //**********************************************************************************************************************
399 int GetSeqsCommand::readGroup(){
400         try {
401                 if (outputDir == "") { outputDir += hasPath(groupfile); }
402                 string outputFileName = outputDir + getRootName(getSimpleName(groupfile)) + "pick" + getExtension(groupfile);
403                 ofstream out;
404                 openOutputFile(outputFileName, out);
405                 
406
407                 ifstream in;
408                 openInputFile(groupfile, in);
409                 string name, group;
410                 
411                 bool wroteSomething = false;
412                 
413                 while(!in.eof()){
414
415                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
416
417
418                         in >> name;                             //read from first column
419                         in >> group;                    //read from second column
420                         
421                         //if this name is in the accnos file
422                         if (names.count(name) == 1) {
423                                 wroteSomething = true;
424                                 
425                                 out << name << '\t' << group << endl;
426                                 
427                                 names.erase(name);
428                         }
429                                         
430                         gobble(in);
431                 }
432                 in.close();
433                 out.close();
434                 
435                 if (wroteSomething == false) {
436                         m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
437                         remove(outputFileName.c_str()); 
438                 }else {  outputNames.push_back(outputFileName); }
439                 
440                 return 0;
441
442         }
443         catch(exception& e) {
444                 m->errorOut(e, "GetSeqsCommand", "readGroup");
445                 exit(1);
446         }
447 }
448
449 //**********************************************************************************************************************
450 //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
451 int GetSeqsCommand::readAlign(){
452         try {
453                 if (outputDir == "") { outputDir += hasPath(alignfile); }
454                 string outputFileName = outputDir + getRootName(getSimpleName(alignfile)) + "pick.align.report";
455                 ofstream out;
456                 openOutputFile(outputFileName, out);
457                 
458
459                 ifstream in;
460                 openInputFile(alignfile, in);
461                 string name, junk;
462                 
463                 bool wroteSomething = false;
464                 
465                 //read column headers
466                 for (int i = 0; i < 16; i++) {  
467                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
468                         else                    {       break;                  }
469                 }
470                 out << endl;
471                 
472                 while(!in.eof()){
473                 
474                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
475
476
477                         in >> name;                             //read from first column
478                         
479                         //if this name is in the accnos file
480                         if (names.count(name) == 1) {
481                                 wroteSomething = true;
482                                 
483                                 out << name << '\t';
484                                 
485                                 //read rest
486                                 for (int i = 0; i < 15; i++) {  
487                                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
488                                         else                    {       break;                  }
489                                 }
490                                 out << endl;
491                                 
492                                 names.erase(name);
493                                 
494                         }else {//still read just don't do anything with it
495                                 //read rest
496                                 for (int i = 0; i < 15; i++) {  
497                                         if (!in.eof())  {       in >> junk;             }
498                                         else                    {       break;                  }
499                                 }
500                         }
501                         
502                         gobble(in);
503                 }
504                 in.close();
505                 out.close();
506                 
507                 if (wroteSomething == false) {
508                         m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();
509                         remove(outputFileName.c_str()); 
510                 }else {  outputNames.push_back(outputFileName); }
511                 
512                 return 0;
513                 
514         }
515         catch(exception& e) {
516                 m->errorOut(e, "GetSeqsCommand", "readAlign");
517                 exit(1);
518         }
519 }
520 //**********************************************************************************************************************
521
522 int GetSeqsCommand::readAccnos(){
523         try {
524                 
525                 ifstream in;
526                 openInputFile(accnosfile, in);
527                 string name;
528                 
529                 while(!in.eof()){
530                         in >> name;
531                                                 
532                         names.insert(name);
533                         
534                         gobble(in);
535                 }
536                 in.close();     
537                 
538                 return 0;
539
540         }
541         catch(exception& e) {
542                 m->errorOut(e, "GetSeqsCommand", "readAccnos");
543                 exit(1);
544         }
545 }
546
547 //**********************************************************************************************************************
548