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