]> git.donarmstrong.com Git - mothur.git/blob - removeseqscommand.cpp
added checks for ^C to quit command instead of program
[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 = getRootName(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 = getRootName(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 != "") {  newList.push_back(newNames);     }
286                         }
287                                 
288                         //print new listvector
289                         if (newList.getNumBins() != 0) {
290                                 wroteSomething = true;
291                                 newList.print(out);
292                         }
293                         
294                         gobble(in);
295                 }
296                 in.close();     
297                 out.close();
298                 
299                 if (wroteSomething == false) {
300                         m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();
301                         remove(outputFileName.c_str()); 
302                 }else { outputNames.push_back(outputFileName); }
303                 
304                 return 0;
305
306         }
307         catch(exception& e) {
308                 m->errorOut(e, "RemoveSeqsCommand", "readList");
309                 exit(1);
310         }
311 }
312 //**********************************************************************************************************************
313 int RemoveSeqsCommand::readName(){
314         try {
315                 if (outputDir == "") {  outputDir += hasPath(namefile);  }
316                 string outputFileName = getRootName(namefile) + "pick" + getExtension(namefile);
317                 string outputFileName2 = getRootName(namefile) + "dups.accnos";
318
319                 ofstream out2;
320                 if (dups) {      openOutputFile(outputFileName2, out2); }
321                 bool wroteDups = false;
322                 
323                 ofstream out;
324                 openOutputFile(outputFileName, out);
325
326                 ifstream in;
327                 openInputFile(namefile, in);
328                 string name, firstCol, secondCol;
329                 
330                 bool wroteSomething = false;
331                 
332                 while(!in.eof()){
333                         if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str()); if (dups) { out2.close(); remove(outputFileName2.c_str()); } return 0; }
334
335                         in >> firstCol;                         
336                         in >> secondCol;                        
337
338                         vector<string> parsedNames;
339                         //parse second column saving each name
340                         while (secondCol.find_first_of(',') != -1) { 
341                                 name = secondCol.substr(0,secondCol.find_first_of(','));
342                                 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
343                                 parsedNames.push_back(name);
344
345                         }
346                         
347                         //get name after last ,
348                         parsedNames.push_back(secondCol);
349
350                         vector<string> validSecond;  validSecond.clear();
351                         for (int i = 0; i < parsedNames.size(); i++) {
352                                 if (names.count(parsedNames[i]) == 0) {
353                                         validSecond.push_back(parsedNames[i]);
354                                 }
355                         }
356                         
357                         if ((dups) && (validSecond.size() != parsedNames.size())) { 
358                                 wroteDups = true;
359                                 for (int i = 0; i < parsedNames.size(); i++) {  out2 << parsedNames[i] << endl;  }
360                         }else {
361                                         //if the name in the first column is in the set then print it and any other names in second column also in set
362                                 if (names.count(firstCol) == 0) {
363                                         
364                                         wroteSomething = true;
365                                         
366                                         out << firstCol << '\t';
367                                         
368                                         //you know you have at least one valid second since first column is valid
369                                         for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
370                                         out << validSecond[validSecond.size()-1] << endl;
371                                         
372                                         //make first name in set you come to first column and then add the remaining names to second column
373                                 }else {
374                                         
375                                         //you want part of this row
376                                         if (validSecond.size() != 0) {
377                                                 
378                                                 wroteSomething = true;
379                                                 
380                                                 out << validSecond[0] << '\t';
381                                                 
382                                                 //you know you have at least one valid second since first column is valid
383                                                 for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
384                                                 out << validSecond[validSecond.size()-1] << endl;
385                                         }
386                                 }
387                         }
388                         gobble(in);
389                 }
390                 in.close();
391                 out.close();
392                 
393                 if (dups) { out2.close();  }
394                 if (wroteDups == false) {
395                         remove(outputFileName2.c_str()); 
396                 }else { outputNames.push_back(outputFileName2); }
397                 
398                 if (wroteSomething == false) {
399                         m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();
400                         remove(outputFileName.c_str()); 
401                 }else { outputNames.push_back(outputFileName); }
402                 
403                 return 0;
404         }
405         catch(exception& e) {
406                 m->errorOut(e, "RemoveSeqsCommand", "readName");
407                 exit(1);
408         }
409 }
410
411 //**********************************************************************************************************************
412 int RemoveSeqsCommand::readGroup(){
413         try {
414                 if (outputDir == "") {  outputDir += hasPath(groupfile);  }
415                 string outputFileName = getRootName(groupfile) + "pick" + getExtension(groupfile);
416                 ofstream out;
417                 openOutputFile(outputFileName, out);
418
419                 ifstream in;
420                 openInputFile(groupfile, in);
421                 string name, group;
422                 
423                 bool wroteSomething = false;
424                 
425                 while(!in.eof()){
426                         if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
427                         
428                         in >> name;                             //read from first column
429                         in >> group;                    //read from second column
430                         
431                         //if this name is in the accnos file
432                         if (names.count(name) == 0) {
433                                 wroteSomething = true;
434                                 out << name << '\t' << group << endl;
435                         }else {         names.erase(name);              }
436                                         
437                         gobble(in);
438                 }
439                 in.close();
440                 out.close();
441                 
442                 if (wroteSomething == false) {
443                         m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();
444                         remove(outputFileName.c_str()); 
445                 }else { outputNames.push_back(outputFileName); }
446                 
447                 return 0;
448         }
449         catch(exception& e) {
450                 m->errorOut(e, "RemoveSeqsCommand", "readGroup");
451                 exit(1);
452         }
453 }
454
455 //**********************************************************************************************************************
456 //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
457 int RemoveSeqsCommand::readAlign(){
458         try {
459                 if (outputDir == "") {  outputDir += hasPath(alignfile);  }
460                 string outputFileName = getRootName(getRootName(alignfile)) + "pick.align.report";
461                 ofstream out;
462                 openOutputFile(outputFileName, out);
463
464                 ifstream in;
465                 openInputFile(alignfile, in);
466                 string name, junk;
467                 
468                 bool wroteSomething = false;
469                 
470                 //read column headers
471                 for (int i = 0; i < 16; i++) {  
472                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
473                         else                    {       break;                  }
474                 }
475                 out << endl;
476                 
477                 while(!in.eof()){
478                         if (m->control_pressed) { in.close();  out.close();  remove(outputFileName.c_str());  return 0; }
479                         
480                         in >> name;                             //read from first column
481                         
482                         //if this name is in the accnos file
483                         if (names.count(name) == 0) {
484                                 wroteSomething = true;
485                                 
486                                 out << name << '\t';
487                                 
488                                 //read rest
489                                 for (int i = 0; i < 15; i++) {  
490                                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
491                                         else                    {       break;                  }
492                                 }
493                                 out << endl;
494                                 
495                         }else {//still read just don't do anything with it
496                                 names.erase(name);      
497                                 
498                                 //read rest
499                                 for (int i = 0; i < 15; i++) {  
500                                         if (!in.eof())  {       in >> junk;             }
501                                         else                    {       break;                  }
502                                 }
503                         }
504                         
505                         gobble(in);
506                 }
507                 in.close();
508                 out.close();
509                 
510                 if (wroteSomething == false) {
511                         m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();
512                         remove(outputFileName.c_str()); 
513                 }else { outputNames.push_back(outputFileName); }
514                 
515                 return 0;
516                 
517         }
518         catch(exception& e) {
519                 m->errorOut(e, "RemoveSeqsCommand", "readAlign");
520                 exit(1);
521         }
522 }
523 //**********************************************************************************************************************
524 void RemoveSeqsCommand::readAccnos(){
525         try {
526                 
527                 ifstream in;
528                 openInputFile(accnosfile, in);
529                 string name;
530                 
531                 while(!in.eof()){
532                         in >> name;
533                                                 
534                         names.insert(name);
535                         
536                         gobble(in);
537                 }
538                 in.close();             
539
540         }
541         catch(exception& e) {
542                 m->errorOut(e, "RemoveSeqsCommand", "readAccnos");
543                 exit(1);
544         }
545 }
546
547 //**********************************************************************************************************************
548
549