]> git.donarmstrong.com Git - mothur.git/blob - removeseqscommand.cpp
modified sequence class to read fasta files with comments. this required modification...
[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 (name != "") {
129                                 //if this name is in the accnos file
130                                 if (names.count(name) == 0) {
131                                         wroteSomething = true;
132                                         
133                                         currSeq.printSequence(out);
134                                 }else {         names.erase(name);              }
135                         }
136                         gobble(in);
137                 }
138                 in.close();     
139                 out.close();
140                 
141                 if (wroteSomething == false) {
142                         mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
143                         remove(outputFileName.c_str()); 
144                 }
145
146         }
147         catch(exception& e) {
148                 errorOut(e, "RemoveSeqsCommand", "readFasta");
149                 exit(1);
150         }
151 }
152
153 //**********************************************************************************************************************
154 void RemoveSeqsCommand::readName(){
155         try {
156         
157                 string outputFileName = getRootName(namefile) + "pick" + getExtension(namefile);
158
159                 ofstream out;
160                 openOutputFile(outputFileName, out);
161
162                 ifstream in;
163                 openInputFile(namefile, in);
164                 string name, firstCol, secondCol;
165                 
166                 bool wroteSomething = false;
167                 
168                 while(!in.eof()){
169
170                         in >> firstCol;                         
171                         in >> secondCol;                        
172
173                         vector<string> parsedNames;
174                         //parse second column saving each name
175                         while (secondCol.find_first_of(',') != -1) { 
176                                 name = secondCol.substr(0,secondCol.find_first_of(','));
177                                 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
178                                 parsedNames.push_back(name);
179
180                         }
181                         
182                         //get name after last ,
183                         parsedNames.push_back(secondCol);
184
185                         vector<string> validSecond;  validSecond.clear();
186                         for (int i = 0; i < parsedNames.size(); i++) {
187                                 if (names.count(parsedNames[i]) == 0) {
188                                         validSecond.push_back(parsedNames[i]);
189                                 }
190                         }
191                         
192                         //if the name in the first column is in the set then print it and any other names in second column also in set
193                         if (names.count(firstCol) == 0) {
194                         
195                                 wroteSomething = true;
196                                 
197                                 out << firstCol << '\t';
198                                 
199                                 //you know you have at least one valid second since first column is valid
200                                 for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
201                                 out << validSecond[validSecond.size()-1] << endl;
202                         
203                         //make first name in set you come to first column and then add the remaining names to second column
204                         }else {
205                                 
206                                 //you want part of this row
207                                 if (validSecond.size() != 0) {
208                                 
209                                         wroteSomething = true;
210                                         
211                                         out << validSecond[0] << '\t';
212                                 
213                                         //you know you have at least one valid second since first column is valid
214                                         for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
215                                         out << validSecond[validSecond.size()-1] << endl;
216                                 }
217                         }
218                         
219                         gobble(in);
220                 }
221                 in.close();
222                 out.close();
223                 
224                 if (wroteSomething == false) {
225                         mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
226                         remove(outputFileName.c_str()); 
227                 }
228                 
229         }
230         catch(exception& e) {
231                 errorOut(e, "RemoveSeqsCommand", "readName");
232                 exit(1);
233         }
234 }
235
236 //**********************************************************************************************************************
237 void RemoveSeqsCommand::readGroup(){
238         try {
239         
240                 string outputFileName = getRootName(groupfile) + "pick" + getExtension(groupfile);
241                 ofstream out;
242                 openOutputFile(outputFileName, out);
243
244                 ifstream in;
245                 openInputFile(groupfile, in);
246                 string name, group;
247                 
248                 bool wroteSomething = false;
249                 
250                 while(!in.eof()){
251
252                         in >> name;                             //read from first column
253                         in >> group;                    //read from second column
254                         
255                         //if this name is in the accnos file
256                         if (names.count(name) == 0) {
257                                 wroteSomething = true;
258                                 out << name << '\t' << group << endl;
259                         }else {         names.erase(name);              }
260                                         
261                         gobble(in);
262                 }
263                 in.close();
264                 out.close();
265                 
266                 if (wroteSomething == false) {
267                         mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
268                         remove(outputFileName.c_str()); 
269                 }
270
271         }
272         catch(exception& e) {
273                 errorOut(e, "RemoveSeqsCommand", "readGroup");
274                 exit(1);
275         }
276 }
277
278 //**********************************************************************************************************************
279 //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
280 void RemoveSeqsCommand::readAlign(){
281         try {
282                 string outputFileName = getRootName(getRootName(alignfile)) + "pick.align.report";
283                 ofstream out;
284                 openOutputFile(outputFileName, out);
285
286                 ifstream in;
287                 openInputFile(alignfile, in);
288                 string name, junk;
289                 
290                 bool wroteSomething = false;
291                 
292                 //read column headers
293                 for (int i = 0; i < 16; i++) {  
294                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
295                         else                    {       break;                  }
296                 }
297                 out << endl;
298                 
299                 while(!in.eof()){
300
301                         in >> name;                             //read from first column
302                         
303                         //if this name is in the accnos file
304                         if (names.count(name) == 0) {
305                                 wroteSomething = true;
306                                 
307                                 out << name << '\t';
308                                 
309                                 //read rest
310                                 for (int i = 0; i < 15; i++) {  
311                                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
312                                         else                    {       break;                  }
313                                 }
314                                 out << endl;
315                                 
316                         }else {//still read just don't do anything with it
317                                 names.erase(name);      
318                                 
319                                 //read rest
320                                 for (int i = 0; i < 15; i++) {  
321                                         if (!in.eof())  {       in >> junk;             }
322                                         else                    {       break;                  }
323                                 }
324                         }
325                         
326                         gobble(in);
327                 }
328                 in.close();
329                 out.close();
330                 
331                 if (wroteSomething == false) {
332                         mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
333                         remove(outputFileName.c_str()); 
334                 }
335                 
336         }
337         catch(exception& e) {
338                 errorOut(e, "RemoveSeqsCommand", "readAlign");
339                 exit(1);
340         }
341 }
342 //**********************************************************************************************************************
343 void RemoveSeqsCommand::readAccnos(){
344         try {
345                 
346                 ifstream in;
347                 openInputFile(accnosfile, in);
348                 string name;
349                 
350                 while(!in.eof()){
351                         in >> name;
352                                                 
353                         names.insert(name);
354                         
355                         gobble(in);
356                 }
357                 in.close();             
358
359         }
360         catch(exception& e) {
361                 errorOut(e, "RemoveSeqsCommand", "readAccnos");
362                 exit(1);
363         }
364 }
365
366 //**********************************************************************************************************************
367
368