]> git.donarmstrong.com Git - mothur.git/blob - binsequencecommand.cpp
broke up globaldata and moved error checking and help into commands
[mothur.git] / binsequencecommand.cpp
1 /*
2  *  binsequencecommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 4/3/09.
6  *  Copyright 2009 Schloss Lab UMASS Amhers. All rights reserved.
7  *
8  */
9
10 #include "binsequencecommand.h"
11
12 //**********************************************************************************************************************
13 BinSeqCommand::BinSeqCommand(string option){
14         try {
15                 globaldata = GlobalData::getInstance();
16                 abort = false;
17                 allLines = 1;
18                 lines.clear();
19                 labels.clear();
20                 
21                 //allow user to run help
22                 if(option == "help") { help(); abort = true; }
23                 
24                 else {
25                         //valid paramters for this command
26                         string AlignArray[] =  {"fasta","line","label","name", "group"};
27                         vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
28                         
29                         parser = new OptionParser();
30                         parser->parse(option, parameters);  delete parser;
31                         
32                         ValidParameters* validParameter = new ValidParameters();
33                 
34                         //check to make sure all parameters are valid for command
35                         for (it = parameters.begin(); it != parameters.end(); it++) { 
36                                 if (validParameter->isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
37                         }
38                         
39                         //make sure the user has already run the read.otu command
40                         if (globaldata->getListFile() == "") { cout << "You must read a listfile before running the bin.seqs command." << endl; abort = true; }
41                         
42                         
43                         //check for required parameters
44                         fastafile = validParameter->validFile(parameters, "fasta", true);
45                         if (fastafile == "not found") { cout << "fasta is a required parameter for the bin.seqs command." << endl; abort = true; }
46                         else if (fastafile == "not open") { abort = true; }     
47                         else { 
48                                 globaldata->setFastaFile(fastafile);
49                                 openInputFile(fastafile, in);
50                                 fasta = new FastaMap();
51                         }
52                 
53                 
54                         //check for optional parameter and set defaults
55                         // ...at some point should added some additional type checking...
56                         line = validParameter->validFile(parameters, "line", false);                            
57                         if (line == "not found") { line = "";  }
58                         else { 
59                                 if(line != "all") {  splitAtDash(line, lines);  allLines = 0;  }
60                                 else { allLines = 1;  }
61                         }
62                         
63                         label = validParameter->validFile(parameters, "label", false);                  
64                         if (label == "not found") { label = ""; }
65                         else { 
66                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
67                                 else { allLines = 1;  }
68                         }
69                         
70                         //make sure user did not use both the line and label parameters
71                         if ((line != "") && (label != "")) { cout << "You cannot use both the line and label parameters at the same time. " << endl; abort = true; }
72                         //if the user has not specified any line or labels use the ones from read.otu
73                         else if ((line == "") && (label == "")) {  
74                                 allLines = globaldata->allLines; 
75                                 labels = globaldata->labels; 
76                                 lines = globaldata->lines;
77                         }
78                         
79                         namesfile = validParameter->validFile(parameters, "name", true);
80                         if (namesfile == "not open") { abort = true; }  
81                         else if (namesfile == "not found") { namesfile = ""; }
82
83                         groupfile = validParameter->validFile(parameters, "group", true);
84                         if (groupfile == "not open") { abort = true; }
85                         else if (groupfile == "not found") { groupfile = ""; }
86                         else {
87                                 //read in group map info.
88                                 groupMap = new GroupMap(groupfile);
89                                 groupMap->readMap();
90                         }
91         
92                         delete validParameter;
93                 }
94         }
95         catch(exception& e) {
96                 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function BinSeqCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
97                 exit(1);
98         }
99         catch(...) {
100                 cout << "An unknown error has occurred in the BinSeqCommand class function BinSeqCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
101                 exit(1);
102         }       
103 }
104 //**********************************************************************************************************************
105
106 void BinSeqCommand::help(){
107         try {
108                 cout << "The bin.seqs command can only be executed after a successful read.otu command of a listfile." << "\n";
109                 cout << "The bin.seqs command parameters are fasta, name, line, label and group.  The fasta parameter is required, and you may not use line and label at the same time." << "\n";
110                 cout << "The line and label allow you to select what distance levels you would like a output files created for, and are separated by dashes." << "\n";
111                 cout << "The bin.seqs command should be in the following format: bin.seqs(fasta=yourFastaFile, name=yourNamesFile, group=yourGroupFile, line=yourLines, label=yourLabels)." << "\n";
112                 cout << "Example bin.seqs(fasta=amazon.fasta, group=amazon.groups, line=1-3-5, name=amazon.names)." << "\n";
113                 cout << "The default value for line and label are all lines in your inputfile." << "\n";
114                 cout << "The bin.seqs command outputs a .fasta file for each distance you specify appending the OTU number to each name." << "\n";
115                 cout << "If you provide a groupfile, then it also appends the sequences group to the name." << "\n";
116                 cout << "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile)." << "\n" << "\n";
117         }
118         catch(exception& e) {
119                 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
120                 exit(1);
121         }
122         catch(...) {
123                 cout << "An unknown error has occurred in the BinSeqCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
124                 exit(1);
125         }       
126 }
127
128
129
130 //**********************************************************************************************************************
131
132 BinSeqCommand::~BinSeqCommand(){
133         //made new in execute
134         if (abort == false) {
135                 delete input;
136                 delete read;
137                 delete list;
138         }
139         
140         //made new in constructor
141         delete fasta;
142         if (groupfile != "") {
143                         delete groupMap;
144         }
145
146 }
147
148 //**********************************************************************************************************************
149
150 int BinSeqCommand::execute(){
151         try {
152         
153                 if (abort == true) {    return 0;       }
154         
155                 int count = 1;
156                 int error = 0;
157                 
158                 //read fastafile
159                 fasta->readFastaFile(in);
160                 
161                 //set format to list so input can get listvector
162                 globaldata->setFormat("list");
163                 
164                 //if user gave a namesfile then use it
165                 if (namesfile != "") {
166                         readNamesFile();
167                 }
168                 
169                 //read list file
170                 read = new ReadOTUFile(globaldata->getListFile());      
171                 read->read(&*globaldata); 
172                 
173                 input = globaldata->ginput;
174                 list = globaldata->gListVector;
175                 ListVector* lastList = list;
176                 
177                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
178                 set<string> processedLabels;
179                 set<string> userLabels = labels;
180                 set<int> userLines = lines;
181
182                                 
183                 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
184                         
185                         if(allLines == 1 || lines.count(count) == 1 || labels.count(list->getLabel()) == 1){
186                                 
187                                 error = process(list, count);   
188                                 if (error == 1) { return 0; }   
189                                                         
190                                 processedLabels.insert(list->getLabel());
191                                 userLabels.erase(list->getLabel());
192                                 userLines.erase(count);
193                         }
194                         
195                         if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastList->getLabel()) != 1)) {
196                                 
197                                 error = process(lastList, count);       
198                                 if (error == 1) { return 0; }
199                                                                                                         
200                                 processedLabels.insert(lastList->getLabel());
201                                 userLabels.erase(lastList->getLabel());
202                                 
203                         }
204                         
205                         if (count != 1) { delete lastList; }
206                         lastList = list;                        
207
208                         list = input->getListVector();
209                         count++;
210                 }
211                 
212                 
213                 //output error messages about any remaining user labels
214                 set<string>::iterator it;
215                 bool needToRun = false;
216                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
217                         cout << "Your file does not include the label "<< *it; 
218                         if (processedLabels.count(lastList->getLabel()) != 1) {
219                                 cout << ". I will use " << lastList->getLabel() << "." << endl;
220                                 needToRun = true;
221                         }else {
222                                 cout << ". Please refer to " << lastList->getLabel() << "." << endl;
223                         }
224                 }
225                 
226                 //run last line if you need to
227                 if (needToRun == true)  {
228                         error = process(lastList, count);       
229                         if (error == 1) { return 0; }                   
230                 }
231                 
232                 delete lastList;
233                 return 0;
234         }
235         catch(exception& e) {
236                 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
237                 exit(1);
238         }
239         catch(...) {
240                 cout << "An unknown error has occurred in the BinSeqCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
241                 exit(1);
242         }       
243 }
244
245 //**********************************************************************************************************************
246 void BinSeqCommand::readNamesFile() {
247         try {
248                 vector<string> dupNames;
249                 openInputFile(namesfile, inNames);
250                 
251                 string name, names, sequence;
252         
253                 while(inNames){
254                         inNames >> name;                        //read from first column  A
255                         inNames >> names;               //read from second column  A,B,C,D
256                         
257                         dupNames.clear();
258                         
259                         //parse names into vector
260                         splitAtComma(names, dupNames);
261                         
262                         //store names in fasta map
263                         sequence = fasta->getSequence(name);
264                         for (int i = 0; i < dupNames.size(); i++) {
265                                 fasta->push_back(dupNames[i], sequence);
266                         }
267                 
268                         gobble(inNames);
269                 }
270                 inNames.close();
271
272         }
273         catch(exception& e) {
274                 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
275                 exit(1);
276         }
277         catch(...) {
278                 cout << "An unknown error has occurred in the BinSeqCommand class function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
279                 exit(1);
280         }       
281 }
282 //**********************************************************************************************************************
283 //return 1 if error, 0 otherwise
284 int BinSeqCommand::process(ListVector* list, int count) {
285         try {
286                                 string binnames, name, sequence;
287                                 string outputFileName = getRootName(globaldata->getListFile()) + list->getLabel() + ".fasta";
288                                 openOutputFile(outputFileName, out);
289
290                                 cout << list->getLabel() << '\t' << count << endl;
291                                 
292                                 //for each bin in the list vector
293                                 for (int i = 0; i < list->size(); i++) {
294
295                                         binnames = list->get(i);
296                                         while (binnames.find_first_of(',') != -1) { 
297                                                 name = binnames.substr(0,binnames.find_first_of(','));
298                                                 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
299                                                 
300                                                 //do work for that name
301                                                 sequence = fasta->getSequence(name);
302                                                 if (sequence != "not found") {
303                                                         //if you don't have groups
304                                                         if (groupfile == "") {
305                                                                 name = name + "|" + toString(i+1);
306                                                                 out << ">" << name << endl;
307                                                                 out << sequence << endl;
308                                                         }else {//if you do have groups
309                                                                 string group = groupMap->getGroup(name);
310                                                                 if (group == "not found") {  
311                                                                         cout << name << " is missing from your group file. Please correct. " << endl;
312                                                                         remove(outputFileName.c_str());
313                                                                         return 1;
314                                                                 }else{
315                                                                         name = name + "|" + group + "|" + toString(i+1);
316                                                                         out << ">" << name << endl;
317                                                                         out << sequence << endl;
318                                                                 }
319                                                         }
320                                                 }else { 
321                                                         cout << name << " is missing from your fasta or name file. Please correct. " << endl; 
322                                                         remove(outputFileName.c_str());
323                                                         return 1;
324                                                 }
325                                                 
326                                         }
327                                         
328                                         //get last name
329                                         sequence = fasta->getSequence(binnames);
330                                         if (sequence != "not found") {
331                                                 //if you don't have groups
332                                                 if (groupfile == "") {
333                                                         binnames = binnames + "|" + toString(i+1);
334                                                         out << ">" << binnames << endl;
335                                                         out << sequence << endl;
336                                                 }else {//if you do have groups
337                                                         string group = groupMap->getGroup(binnames);
338                                                         if (group == "not found") {  
339                                                                 cout << binnames << " is missing from your group file. Please correct. " << endl;
340                                                                 remove(outputFileName.c_str());
341                                                                 return 1;
342                                                         }else{
343                                                                 binnames = binnames + "|" + group + "|" + toString(i+1);
344                                                                 out << ">" << binnames << endl;
345                                                                 out << sequence << endl;
346                                                         }
347                                                 }
348                                         }else { 
349                                                 cout << binnames << " is missing from your fasta or name file. Please correct. " << endl; 
350                                                 remove(outputFileName.c_str());
351                                                 return 1;
352                                         }
353                                 }
354                                         
355                                 out.close();
356                                 return 0;
357
358         }
359         catch(exception& e) {
360                 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
361                 exit(1);
362         }
363         catch(...) {
364                 cout << "An unknown error has occurred in the BinSeqCommand class function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
365                 exit(1);
366         }       
367 }
368 //**********************************************************************************************************************
369
370