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