]> git.donarmstrong.com Git - mothur.git/blob - getseqscommand.cpp
added remove.seqs command
[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", "align", "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, "align", 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, align."); mothurOutEndLine(); abort = true; }
59                         
60                         if (parameters.size() > 2) { mothurOut("You may only enter one of the following: fasta, name, group, align."); 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 align.  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";
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";
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                                         names.erase(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                                 names.erase(firstCol);
206                         
207                         //make first name in set you come to first column and then add the remaining names to second column
208                         }else {
209                                 //you want part of this row
210                                 if (validSecond.size() != 0) {
211                                 
212                                         wroteSomething = true;
213                                         
214                                         out << validSecond[0] << '\t';
215                                 
216                                         //you know you have at least one valid second since first column is valid
217                                         for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
218                                         out << validSecond[validSecond.size()-1] << endl;
219                                 }
220                         }
221                         
222                         gobble(in);
223                 }
224                 in.close();
225                 out.close();
226                 
227                 if (wroteSomething == false) {
228                         mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
229                         remove(outputFileName.c_str()); 
230                 }
231                 
232         }
233         catch(exception& e) {
234                 errorOut(e, "GetSeqsCommand", "readName");
235                 exit(1);
236         }
237 }
238
239 //**********************************************************************************************************************
240 void GetSeqsCommand::readGroup(){
241         try {
242         
243                 string outputFileName = getRootName(groupfile) + "pick";
244                 ofstream out;
245                 openOutputFile(outputFileName, out);
246
247                 ifstream in;
248                 openInputFile(groupfile, in);
249                 string name, group;
250                 
251                 bool wroteSomething = false;
252                 
253                 while(!in.eof()){
254
255                         in >> name;                             //read from first column
256                         in >> group;                    //read from second column
257                         
258                         //if this name is in the accnos file
259                         if (names.count(name) == 1) {
260                                 wroteSomething = true;
261                                 
262                                 out << name << '\t' << group << endl;
263                                 
264                                 names.erase(name);
265                         }
266                                         
267                         gobble(in);
268                 }
269                 in.close();
270                 out.close();
271                 
272                 if (wroteSomething == false) {
273                         mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
274                         remove(outputFileName.c_str()); 
275                 }
276
277         }
278         catch(exception& e) {
279                 errorOut(e, "GetSeqsCommand", "readGroup");
280                 exit(1);
281         }
282 }
283
284 //**********************************************************************************************************************
285 //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
286 void GetSeqsCommand::readAlign(){
287         try {
288                 string outputFileName = getRootName(alignfile) + "pick";
289                 ofstream out;
290                 openOutputFile(outputFileName, out);
291
292                 ifstream in;
293                 openInputFile(alignfile, in);
294                 string name, junk;
295                 
296                 bool wroteSomething = false;
297                 
298                 //read column headers
299                 for (int i = 0; i < 16; i++) {  
300                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
301                         else                    {       break;                  }
302                 }
303                 out << endl;
304                 
305                 while(!in.eof()){
306
307                         in >> name;                             //read from first column
308                         
309                         //if this name is in the accnos file
310                         if (names.count(name) == 1) {
311                                 wroteSomething = true;
312                                 
313                                 out << name << '\t';
314                                 
315                                 //read rest
316                                 for (int i = 0; i < 15; i++) {  
317                                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
318                                         else                    {       break;                  }
319                                 }
320                                 out << endl;
321                                 
322                                 names.erase(name);
323                                 
324                         }else {//still read just don't do anything with it
325                                 //read rest
326                                 for (int i = 0; i < 15; i++) {  
327                                         if (!in.eof())  {       in >> junk;             }
328                                         else                    {       break;                  }
329                                 }
330                         }
331                         
332                         gobble(in);
333                 }
334                 in.close();
335                 out.close();
336                 
337                 if (wroteSomething == false) {
338                         mothurOut("Your file does not contain any sequence from the .accnos file."); mothurOutEndLine();
339                         remove(outputFileName.c_str()); 
340                 }
341                 
342         }
343         catch(exception& e) {
344                 errorOut(e, "GetSeqsCommand", "readAlign");
345                 exit(1);
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