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