]> git.donarmstrong.com Git - mothur.git/blob - removeseqscommand.cpp
added set.dir command and modified commands to redirect input and output, removed...
[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" };
26                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27                         
28                         OptionParser parser(option);
29                         map<string,string> parameters = parser.getParameters();
30                         
31                         ValidParameters validParameter;
32                         map<string,string>::iterator it;
33                         
34                         //check to make sure all parameters are valid for command
35                         for (it = parameters.begin(); it != parameters.end(); it++) { 
36                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
37                         }
38                         
39                         //if the user changes the output directory command factory will send this info to us in the output parameter 
40                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
41                         
42                         //if the user changes the input directory command factory will send this info to us in the output parameter 
43                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
44                         if (inputDir == "not found"){   inputDir = "";          }
45                         else {
46                                 string path;
47                                 it = parameters.find("alignreport");
48                                 //user has given a template file
49                                 if(it != parameters.end()){ 
50                                         path = hasPath(it->second);
51                                         //if the user has not given a path then, add inputdir. else leave path alone.
52                                         if (path == "") {       parameters["alignreport"] = inputDir + it->second;              }
53                                 }
54                                 
55                                 it = parameters.find("fasta");
56                                 //user has given a template file
57                                 if(it != parameters.end()){ 
58                                         path = hasPath(it->second);
59                                         //if the user has not given a path then, add inputdir. else leave path alone.
60                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
61                                 }
62                                 
63                                 it = parameters.find("accnos");
64                                 //user has given a template file
65                                 if(it != parameters.end()){ 
66                                         path = hasPath(it->second);
67                                         //if the user has not given a path then, add inputdir. else leave path alone.
68                                         if (path == "") {       parameters["accnos"] = inputDir + it->second;           }
69                                 }
70                                 
71                                 it = parameters.find("list");
72                                 //user has given a template file
73                                 if(it != parameters.end()){ 
74                                         path = hasPath(it->second);
75                                         //if the user has not given a path then, add inputdir. else leave path alone.
76                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
77                                 }
78                                 
79                                 it = parameters.find("name");
80                                 //user has given a template file
81                                 if(it != parameters.end()){ 
82                                         path = hasPath(it->second);
83                                         //if the user has not given a path then, add inputdir. else leave path alone.
84                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
85                                 }
86                                 
87                                 it = parameters.find("group");
88                                 //user has given a template file
89                                 if(it != parameters.end()){ 
90                                         path = hasPath(it->second);
91                                         //if the user has not given a path then, add inputdir. else leave path alone.
92                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
93                                 }
94                         }
95
96                         
97                         //check for required parameters
98                         accnosfile = validParameter.validFile(parameters, "accnos", true);
99                         if (accnosfile == "not open") { abort = true; }
100                         else if (accnosfile == "not found") {  accnosfile = "";  mothurOut("You must provide an accnos file."); mothurOutEndLine(); abort = true; }     
101                         
102                         fastafile = validParameter.validFile(parameters, "fasta", true);
103                         if (fastafile == "not open") { abort = true; }
104                         else if (fastafile == "not found") {  fastafile = "";  }        
105                         
106                         namefile = validParameter.validFile(parameters, "name", true);
107                         if (namefile == "not open") { abort = true; }
108                         else if (namefile == "not found") {  namefile = "";  }  
109                         
110                         groupfile = validParameter.validFile(parameters, "group", true);
111                         if (groupfile == "not open") { abort = true; }
112                         else if (groupfile == "not found") {  groupfile = "";  }        
113                         
114                         alignfile = validParameter.validFile(parameters, "alignreport", true);
115                         if (alignfile == "not open") { abort = true; }
116                         else if (alignfile == "not found") {  alignfile = "";  }
117                         
118                         listfile = validParameter.validFile(parameters, "list", true);
119                         if (listfile == "not open") { abort = true; }
120                         else if (listfile == "not found") {  listfile = "";  }
121                         
122                         if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == ""))  { mothurOut("You must provide one of the following: fasta, name, group, alignreport or list."); mothurOutEndLine(); abort = true; }
123                         
124                         int okay = 2;
125                         if (outputDir != "") { okay++; }
126                         
127                         if (parameters.size() > okay) { mothurOut("You may only enter one of the following: fasta, name, group, alignreport, or list."); mothurOutEndLine(); abort = true;  }
128                 }
129
130         }
131         catch(exception& e) {
132                 errorOut(e, "RemoveSeqsCommand", "RemoveSeqsCommand");
133                 exit(1);
134         }
135 }
136 //**********************************************************************************************************************
137
138 void RemoveSeqsCommand::help(){
139         try {
140                 mothurOut("The remove.seqs command reads an .accnos file and one of the following file types: fasta, name, group, list or alignreport file.\n");
141                 mothurOut("It outputs a file containing the sequences NOT in the .accnos file.\n");
142                 mothurOut("The remove.seqs command parameters are accnos, fasta, name, group, list and alignreport.  You must provide accnos and one of the other parameters.\n");
143                 mothurOut("The remove.seqs command should be in the following format: remove.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
144                 mothurOut("Example remove.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
145                 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
146         }
147         catch(exception& e) {
148                 errorOut(e, "RemoveSeqsCommand", "help");
149                 exit(1);
150         }
151 }
152
153 //**********************************************************************************************************************
154
155 int RemoveSeqsCommand::execute(){
156         try {
157                 
158                 if (abort == true) { return 0; }
159                 
160                 //get names you want to keep
161                 readAccnos();
162                 
163                 //read through the correct file and output lines you want to keep
164                 if (fastafile != "")            {               readFasta();    }
165                 else if (namefile != "")        {               readName();             }
166                 else if (groupfile != "")       {               readGroup();    }
167                 else if (alignfile != "")       {               readAlign();    }
168                 else if (listfile != "")        {               readList();             }
169                 
170                 return 0;               
171         }
172
173         catch(exception& e) {
174                 errorOut(e, "RemoveSeqsCommand", "execute");
175                 exit(1);
176         }
177 }
178
179 //**********************************************************************************************************************
180 void RemoveSeqsCommand::readFasta(){
181         try {
182                 if (outputDir == "") {  outputDir += hasPath(fastafile);  }
183                 string outputFileName = getRootName(fastafile) + "pick" + getExtension(fastafile);
184                 ofstream out;
185                 openOutputFile(outputFileName, out);
186                 
187                 ifstream in;
188                 openInputFile(fastafile, in);
189                 string name;
190                 
191                 bool wroteSomething = false;
192                 
193                 while(!in.eof()){
194                         Sequence currSeq(in);
195                         name = currSeq.getName();
196                         
197                         if (name != "") {
198                                 //if this name is in the accnos file
199                                 if (names.count(name) == 0) {
200                                         wroteSomething = true;
201                                         
202                                         currSeq.printSequence(out);
203                                 }else {         names.erase(name);              }
204                         }
205                         gobble(in);
206                 }
207                 in.close();     
208                 out.close();
209                 
210                 if (wroteSomething == false) {
211                         mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
212                         remove(outputFileName.c_str()); 
213                 }
214
215         }
216         catch(exception& e) {
217                 errorOut(e, "RemoveSeqsCommand", "readFasta");
218                 exit(1);
219         }
220 }
221 //**********************************************************************************************************************
222 void RemoveSeqsCommand::readList(){
223         try {
224                 if (outputDir == "") {  outputDir += hasPath(listfile);  }
225                 string outputFileName = getRootName(listfile) + "pick" +  getExtension(listfile);
226                 ofstream out;
227                 openOutputFile(outputFileName, out);
228                 
229                 ifstream in;
230                 openInputFile(listfile, in);
231                 
232                 bool wroteSomething = false;
233                 
234                 while(!in.eof()){
235                         //read in list vector
236                         ListVector list(in);
237                         
238                         //make a new list vector
239                         ListVector newList;
240                         newList.setLabel(list.getLabel());
241                         
242                         //for each bin
243                         for (int i = 0; i < list.getNumBins(); i++) {
244                         
245                                 //parse out names that are in accnos file
246                                 string binnames = list.get(i);
247                                 
248                                 string newNames = "";
249                                 while (binnames.find_first_of(',') != -1) { 
250                                         string name = binnames.substr(0,binnames.find_first_of(','));
251                                         binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
252                                         
253                                         //if that name is in the .accnos file, add it
254                                         if (names.count(name) == 0) {  newNames += name + ",";  }
255                                 }
256                         
257                                 //get last name
258                                 if (names.count(binnames) == 0) {  newNames += binnames;  }
259
260                                 //if there are names in this bin add to new list
261                                 if (newNames != "") {  newList.push_back(newNames);     }
262                         }
263                                 
264                         //print new listvector
265                         if (newList.getNumBins() != 0) {
266                                 wroteSomething = true;
267                                 newList.print(out);
268                         }
269                         
270                         gobble(in);
271                 }
272                 in.close();     
273                 out.close();
274                 
275                 if (wroteSomething == false) {
276                         mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
277                         remove(outputFileName.c_str()); 
278                 }
279
280         }
281         catch(exception& e) {
282                 errorOut(e, "RemoveSeqsCommand", "readList");
283                 exit(1);
284         }
285 }
286 //**********************************************************************************************************************
287 void RemoveSeqsCommand::readName(){
288         try {
289                 if (outputDir == "") {  outputDir += hasPath(namefile);  }
290                 string outputFileName = getRootName(namefile) + "pick" + getExtension(namefile);
291
292                 ofstream out;
293                 openOutputFile(outputFileName, out);
294
295                 ifstream in;
296                 openInputFile(namefile, in);
297                 string name, firstCol, secondCol;
298                 
299                 bool wroteSomething = false;
300                 
301                 while(!in.eof()){
302
303                         in >> firstCol;                         
304                         in >> secondCol;                        
305
306                         vector<string> parsedNames;
307                         //parse second column saving each name
308                         while (secondCol.find_first_of(',') != -1) { 
309                                 name = secondCol.substr(0,secondCol.find_first_of(','));
310                                 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
311                                 parsedNames.push_back(name);
312
313                         }
314                         
315                         //get name after last ,
316                         parsedNames.push_back(secondCol);
317
318                         vector<string> validSecond;  validSecond.clear();
319                         for (int i = 0; i < parsedNames.size(); i++) {
320                                 if (names.count(parsedNames[i]) == 0) {
321                                         validSecond.push_back(parsedNames[i]);
322                                 }
323                         }
324                         
325                         //if the name in the first column is in the set then print it and any other names in second column also in set
326                         if (names.count(firstCol) == 0) {
327                         
328                                 wroteSomething = true;
329                                 
330                                 out << firstCol << '\t';
331                                 
332                                 //you know you have at least one valid second since first column is valid
333                                 for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
334                                 out << validSecond[validSecond.size()-1] << endl;
335                         
336                         //make first name in set you come to first column and then add the remaining names to second column
337                         }else {
338                                 
339                                 //you want part of this row
340                                 if (validSecond.size() != 0) {
341                                 
342                                         wroteSomething = true;
343                                         
344                                         out << validSecond[0] << '\t';
345                                 
346                                         //you know you have at least one valid second since first column is valid
347                                         for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
348                                         out << validSecond[validSecond.size()-1] << endl;
349                                 }
350                         }
351                         
352                         gobble(in);
353                 }
354                 in.close();
355                 out.close();
356                 
357                 if (wroteSomething == false) {
358                         mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
359                         remove(outputFileName.c_str()); 
360                 }
361                 
362         }
363         catch(exception& e) {
364                 errorOut(e, "RemoveSeqsCommand", "readName");
365                 exit(1);
366         }
367 }
368
369 //**********************************************************************************************************************
370 void RemoveSeqsCommand::readGroup(){
371         try {
372                 if (outputDir == "") {  outputDir += hasPath(groupfile);  }
373                 string outputFileName = getRootName(groupfile) + "pick" + getExtension(groupfile);
374                 ofstream out;
375                 openOutputFile(outputFileName, out);
376
377                 ifstream in;
378                 openInputFile(groupfile, in);
379                 string name, group;
380                 
381                 bool wroteSomething = false;
382                 
383                 while(!in.eof()){
384
385                         in >> name;                             //read from first column
386                         in >> group;                    //read from second column
387                         
388                         //if this name is in the accnos file
389                         if (names.count(name) == 0) {
390                                 wroteSomething = true;
391                                 out << name << '\t' << group << endl;
392                         }else {         names.erase(name);              }
393                                         
394                         gobble(in);
395                 }
396                 in.close();
397                 out.close();
398                 
399                 if (wroteSomething == false) {
400                         mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
401                         remove(outputFileName.c_str()); 
402                 }
403
404         }
405         catch(exception& e) {
406                 errorOut(e, "RemoveSeqsCommand", "readGroup");
407                 exit(1);
408         }
409 }
410
411 //**********************************************************************************************************************
412 //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
413 void RemoveSeqsCommand::readAlign(){
414         try {
415                 if (outputDir == "") {  outputDir += hasPath(alignfile);  }
416                 string outputFileName = getRootName(getRootName(alignfile)) + "pick.align.report";
417                 ofstream out;
418                 openOutputFile(outputFileName, out);
419
420                 ifstream in;
421                 openInputFile(alignfile, in);
422                 string name, junk;
423                 
424                 bool wroteSomething = false;
425                 
426                 //read column headers
427                 for (int i = 0; i < 16; i++) {  
428                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
429                         else                    {       break;                  }
430                 }
431                 out << endl;
432                 
433                 while(!in.eof()){
434
435                         in >> name;                             //read from first column
436                         
437                         //if this name is in the accnos file
438                         if (names.count(name) == 0) {
439                                 wroteSomething = true;
440                                 
441                                 out << name << '\t';
442                                 
443                                 //read rest
444                                 for (int i = 0; i < 15; i++) {  
445                                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
446                                         else                    {       break;                  }
447                                 }
448                                 out << endl;
449                                 
450                         }else {//still read just don't do anything with it
451                                 names.erase(name);      
452                                 
453                                 //read rest
454                                 for (int i = 0; i < 15; i++) {  
455                                         if (!in.eof())  {       in >> junk;             }
456                                         else                    {       break;                  }
457                                 }
458                         }
459                         
460                         gobble(in);
461                 }
462                 in.close();
463                 out.close();
464                 
465                 if (wroteSomething == false) {
466                         mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
467                         remove(outputFileName.c_str()); 
468                 }
469                 
470         }
471         catch(exception& e) {
472                 errorOut(e, "RemoveSeqsCommand", "readAlign");
473                 exit(1);
474         }
475 }
476 //**********************************************************************************************************************
477 void RemoveSeqsCommand::readAccnos(){
478         try {
479                 
480                 ifstream in;
481                 openInputFile(accnosfile, in);
482                 string name;
483                 
484                 while(!in.eof()){
485                         in >> name;
486                                                 
487                         names.insert(name);
488                         
489                         gobble(in);
490                 }
491                 in.close();             
492
493         }
494         catch(exception& e) {
495                 errorOut(e, "RemoveSeqsCommand", "readAccnos");
496                 exit(1);
497         }
498 }
499
500 //**********************************************************************************************************************
501
502