]> git.donarmstrong.com Git - mothur.git/blob - getseqscommand.cpp
added list parameter to get.seqs and remove.seqs and added readline library for inter...
[mothur.git] / getseqscommand.cpp
1 /*
2  *  getseqscommand.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 "getseqscommand.h"
11 #include "sequence.hpp"
12 #include "listvector.hpp"
13
14 //**********************************************************************************************************************
15
16 GetSeqsCommand::GetSeqsCommand(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 listfile."); mothurOutEndLine(); abort = true; }
64                         
65                         if (parameters.size() > 2) { mothurOut("You may only enter one of the following: fasta, name, group, alignreport or listfile."); mothurOutEndLine(); abort = true;  }
66                 }
67
68         }
69         catch(exception& e) {
70                 errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
71                 exit(1);
72         }
73 }
74 //**********************************************************************************************************************
75
76 void GetSeqsCommand::help(){
77         try {
78                 mothurOut("The get.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 only the sequences in the .accnos file.\n");
80                 mothurOut("The get.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 get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
82                 mothurOut("Example get.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, "GetSeqsCommand", "help");
87                 exit(1);
88         }
89 }
90
91 //**********************************************************************************************************************
92
93 int GetSeqsCommand::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, "GetSeqsCommand", "execute");
113                 exit(1);
114         }
115 }
116
117 //**********************************************************************************************************************
118 void GetSeqsCommand::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) == 1) {
137                                         wroteSomething = true;
138                                         
139                                         currSeq.printSequence(out);
140                                         
141                                         names.erase(name);
142                                 }
143                         }
144                         gobble(in);
145                 }
146                 in.close();     
147                 out.close();
148                 
149                 if (wroteSomething == false) {
150                         mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
151                         remove(outputFileName.c_str()); 
152                 }
153
154         }
155         catch(exception& e) {
156                 errorOut(e, "GetSeqsCommand", "readFasta");
157                 exit(1);
158         }
159 }
160 //**********************************************************************************************************************
161 void GetSeqsCommand::readList(){
162         try {
163                 string outputFileName = getRootName(listfile) + "pick" +  getExtension(listfile);
164                 ofstream out;
165                 openOutputFile(outputFileName, out);
166                 
167                 ifstream in;
168                 openInputFile(listfile, in);
169                 
170                 bool wroteSomething = false;
171                 
172                 while(!in.eof()){
173                         //read in list vector
174                         ListVector list(in);
175                         
176                         //make a new list vector
177                         ListVector newList;
178                         newList.setLabel(list.getLabel());
179                         
180                         //for each bin
181                         for (int i = 0; i < list.getNumBins(); i++) {
182                         
183                                 //parse out names that are in accnos file
184                                 string binnames = list.get(i);
185                                 
186                                 string newNames = "";
187                                 while (binnames.find_first_of(',') != -1) { 
188                                         string name = binnames.substr(0,binnames.find_first_of(','));
189                                         binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
190                                         
191                                         //if that name is in the .accnos file, add it
192                                         if (names.count(name) == 1) {  newNames += name + ",";  }
193                                 }
194                         
195                                 //get last name
196                                 if (names.count(binnames) == 1) {  newNames += binnames;  }
197
198                                 //if there are names in this bin add to new list
199                                 if (newNames != "") {  newList.push_back(newNames);     }
200                         }
201                                 
202                         //print new listvector
203                         if (newList.getNumBins() != 0) {
204                                 wroteSomething = true;
205                                 newList.print(out);
206                         }
207                         
208                         gobble(in);
209                 }
210                 in.close();     
211                 out.close();
212                 
213                 if (wroteSomething == false) {
214                         mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
215                         remove(outputFileName.c_str()); 
216                 }
217
218         }
219         catch(exception& e) {
220                 errorOut(e, "GetSeqsCommand", "readList");
221                 exit(1);
222         }
223 }
224 //**********************************************************************************************************************
225 void GetSeqsCommand::readName(){
226         try {
227         
228                 string outputFileName = getRootName(namefile) + "pick" +  getExtension(namefile);
229                 ofstream out;
230                 openOutputFile(outputFileName, out);
231
232                 ifstream in;
233                 openInputFile(namefile, in);
234                 string name, firstCol, secondCol;
235                 
236                 bool wroteSomething = false;
237                 
238                 
239                 while(!in.eof()){
240
241                         in >> firstCol;                         
242                         in >> secondCol;                        
243                         
244                         vector<string> parsedNames;
245                         //parse second column saving each name
246                         while (secondCol.find_first_of(',') != -1) { 
247                                 name = secondCol.substr(0,secondCol.find_first_of(','));
248                                 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
249                                 parsedNames.push_back(name);
250                         }
251                         
252                         //get name after last ,
253                         parsedNames.push_back(secondCol);
254                         
255                         vector<string> validSecond;
256                         for (int i = 0; i < parsedNames.size(); i++) {
257                                 if (names.count(parsedNames[i]) == 1) {
258                                         validSecond.push_back(parsedNames[i]);
259                                 }
260                         }
261
262                         
263                         //if the name in the first column is in the set then print it and any other names in second column also in set
264                         if (names.count(firstCol) == 1) {
265                         
266                                 wroteSomething = true;
267                                 
268                                 out << firstCol << '\t';
269                                 
270                                 //you know you have at least one valid second since first column is valid
271                                 for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
272                                 out << validSecond[validSecond.size()-1] << endl;
273                                 
274                         
275                         //make first name in set you come to first column and then add the remaining names to second column
276                         }else {
277                                 //you want part of this row
278                                 if (validSecond.size() != 0) {
279                                 
280                                         wroteSomething = true;
281                                         
282                                         out << validSecond[0] << '\t';
283                                 
284                                         //you know you have at least one valid second since first column is valid
285                                         for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
286                                         out << validSecond[validSecond.size()-1] << endl;
287                                 }
288                         }
289                         
290                         gobble(in);
291                 }
292                 in.close();
293                 out.close();
294                 
295                 if (wroteSomething == false) {
296                         mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
297                         remove(outputFileName.c_str()); 
298                 }
299                 
300         }
301         catch(exception& e) {
302                 errorOut(e, "GetSeqsCommand", "readName");
303                 exit(1);
304         }
305 }
306
307 //**********************************************************************************************************************
308 void GetSeqsCommand::readGroup(){
309         try {
310         
311                 string outputFileName = getRootName(groupfile) + "pick" + getExtension(groupfile);
312                 ofstream out;
313                 openOutputFile(outputFileName, out);
314
315                 ifstream in;
316                 openInputFile(groupfile, in);
317                 string name, group;
318                 
319                 bool wroteSomething = false;
320                 
321                 while(!in.eof()){
322
323                         in >> name;                             //read from first column
324                         in >> group;                    //read from second column
325                         
326                         //if this name is in the accnos file
327                         if (names.count(name) == 1) {
328                                 wroteSomething = true;
329                                 
330                                 out << name << '\t' << group << endl;
331                                 
332                                 names.erase(name);
333                         }
334                                         
335                         gobble(in);
336                 }
337                 in.close();
338                 out.close();
339                 
340                 if (wroteSomething == false) {
341                         mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
342                         remove(outputFileName.c_str()); 
343                 }
344
345         }
346         catch(exception& e) {
347                 errorOut(e, "GetSeqsCommand", "readGroup");
348                 exit(1);
349         }
350 }
351
352 //**********************************************************************************************************************
353 //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
354 void GetSeqsCommand::readAlign(){
355         try {
356                 string outputFileName = getRootName(getRootName(alignfile)) + "pick.align.report";
357                 ofstream out;
358                 openOutputFile(outputFileName, out);
359
360                 ifstream in;
361                 openInputFile(alignfile, in);
362                 string name, junk;
363                 
364                 bool wroteSomething = false;
365                 
366                 //read column headers
367                 for (int i = 0; i < 16; i++) {  
368                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
369                         else                    {       break;                  }
370                 }
371                 out << endl;
372                 
373                 while(!in.eof()){
374
375                         in >> name;                             //read from first column
376                         
377                         //if this name is in the accnos file
378                         if (names.count(name) == 1) {
379                                 wroteSomething = true;
380                                 
381                                 out << name << '\t';
382                                 
383                                 //read rest
384                                 for (int i = 0; i < 15; i++) {  
385                                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
386                                         else                    {       break;                  }
387                                 }
388                                 out << endl;
389                                 
390                                 names.erase(name);
391                                 
392                         }else {//still read just don't do anything with it
393                                 //read rest
394                                 for (int i = 0; i < 15; i++) {  
395                                         if (!in.eof())  {       in >> junk;             }
396                                         else                    {       break;                  }
397                                 }
398                         }
399                         
400                         gobble(in);
401                 }
402                 in.close();
403                 out.close();
404                 
405                 if (wroteSomething == false) {
406                         mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
407                         remove(outputFileName.c_str()); 
408                 }
409                 
410         }
411         catch(exception& e) {
412                 errorOut(e, "GetSeqsCommand", "readAlign");
413                 exit(1);
414         }
415 }
416 //**********************************************************************************************************************
417
418 void GetSeqsCommand::readAccnos(){
419         try {
420                 
421                 ifstream in;
422                 openInputFile(accnosfile, in);
423                 string name;
424                 
425                 while(!in.eof()){
426                         in >> name;
427                                                 
428                         names.insert(name);
429                         
430                         gobble(in);
431                 }
432                 in.close();             
433
434         }
435         catch(exception& e) {
436                 errorOut(e, "GetSeqsCommand", "readAccnos");
437                 exit(1);
438         }
439 }
440
441 //**********************************************************************************************************************
442