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