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