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