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