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