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