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