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