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