]> git.donarmstrong.com Git - mothur.git/blob - binsequencecommand.cpp
removed line option
[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                                 delete list;
170                                 list = input->getListVector(lastLabel);
171                                 
172                                 error = process(list);  
173                                 if (error == 1) { return 0; }
174                                                                                                         
175                                 processedLabels.insert(list->getLabel());
176                                 userLabels.erase(list->getLabel());
177                         }
178                         
179                         lastLabel = list->getLabel();                   
180                         
181                         delete list;
182                         list = input->getListVector();
183                 }
184                 
185                 
186                 //output error messages about any remaining user labels
187                 set<string>::iterator it;
188                 bool needToRun = false;
189                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
190                         mothurOut("Your file does not include the label " + *it); 
191                         if (processedLabels.count(lastLabel) != 1) {
192                                 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
193                                 needToRun = true;
194                         }else {
195                                 mothurOut(". Please refer to " + lastLabel + ".");  mothurOutEndLine();
196                         }
197                 }
198                 
199                 //run last label if you need to
200                 if (needToRun == true)  {
201                         if (list != NULL) {             delete list;    }
202                         list = input->getListVector(lastLabel);
203                                 
204                         error = process(list);  
205                         if (error == 1) { return 0; }
206                         
207                         delete list;  
208                 }
209                 
210                 return 0;
211         }
212         catch(exception& e) {
213                 errorOut(e, "BinSeqCommand", "execute");
214                 exit(1);
215         }
216 }
217
218 //**********************************************************************************************************************
219 void BinSeqCommand::readNamesFile() {
220         try {
221                 vector<string> dupNames;
222                 openInputFile(namesfile, inNames);
223                 
224                 string name, names, sequence;
225         
226                 while(inNames){
227                         inNames >> name;                        //read from first column  A
228                         inNames >> names;               //read from second column  A,B,C,D
229                         
230                         dupNames.clear();
231                         
232                         //parse names into vector
233                         splitAtComma(names, dupNames);
234                         
235                         //store names in fasta map
236                         sequence = fasta->getSequence(name);
237                         for (int i = 0; i < dupNames.size(); i++) {
238                                 fasta->push_back(dupNames[i], sequence);
239                         }
240                 
241                         gobble(inNames);
242                 }
243                 inNames.close();
244
245         }
246         catch(exception& e) {
247                 errorOut(e, "BinSeqCommand", "readNamesFile");
248                 exit(1);
249         }
250 }
251 //**********************************************************************************************************************
252 //return 1 if error, 0 otherwise
253 int BinSeqCommand::process(ListVector* list) {
254         try {
255                                 string binnames, name, sequence;
256                                 string outputFileName = getRootName(globaldata->getListFile()) + list->getLabel() + ".fasta";
257                                 openOutputFile(outputFileName, out);
258
259                                 mothurOut(list->getLabel()); mothurOutEndLine();
260                                 
261                                 //for each bin in the list vector
262                                 for (int i = 0; i < list->size(); i++) {
263
264                                         binnames = list->get(i);
265                                         while (binnames.find_first_of(',') != -1) { 
266                                                 name = binnames.substr(0,binnames.find_first_of(','));
267                                                 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
268                                                 
269                                                 //do work for that name
270                                                 sequence = fasta->getSequence(name);
271                                                 if (sequence != "not found") {
272                                                         //if you don't have groups
273                                                         if (groupfile == "") {
274                                                                 name = name + "|" + toString(i+1);
275                                                                 out << ">" << name << endl;
276                                                                 out << sequence << endl;
277                                                         }else {//if you do have groups
278                                                                 string group = groupMap->getGroup(name);
279                                                                 if (group == "not found") {  
280                                                                         mothurOut(name + " is missing from your group file. Please correct. ");  mothurOutEndLine();
281                                                                         remove(outputFileName.c_str());
282                                                                         return 1;
283                                                                 }else{
284                                                                         name = name + "|" + group + "|" + toString(i+1);
285                                                                         out << ">" << name << endl;
286                                                                         out << sequence << endl;
287                                                                 }
288                                                         }
289                                                 }else { 
290                                                         mothurOut(name + " is missing from your fasta or name file. Please correct. "); mothurOutEndLine();
291                                                         remove(outputFileName.c_str());
292                                                         return 1;
293                                                 }
294                                                 
295                                         }
296                                         
297                                         //get last name
298                                         sequence = fasta->getSequence(binnames);
299                                         if (sequence != "not found") {
300                                                 //if you don't have groups
301                                                 if (groupfile == "") {
302                                                         binnames = binnames + "|" + toString(i+1);
303                                                         out << ">" << binnames << endl;
304                                                         out << sequence << endl;
305                                                 }else {//if you do have groups
306                                                         string group = groupMap->getGroup(binnames);
307                                                         if (group == "not found") {  
308                                                                 mothurOut(binnames + " is missing from your group file. Please correct. "); mothurOutEndLine();
309                                                                 remove(outputFileName.c_str());
310                                                                 return 1;
311                                                         }else{
312                                                                 binnames = binnames + "|" + group + "|" + toString(i+1);
313                                                                 out << ">" << binnames << endl;
314                                                                 out << sequence << endl;
315                                                         }
316                                                 }
317                                         }else { 
318                                                 mothurOut(binnames + " is missing from your fasta or name file. Please correct. "); mothurOutEndLine();
319                                                 remove(outputFileName.c_str());
320                                                 return 1;
321                                         }
322                                 }
323                                         
324                                 out.close();
325                                 return 0;
326
327         }
328         catch(exception& e) {
329                 errorOut(e, "BinSeqCommand", "process");
330                 exit(1);
331         }
332 }
333 //**********************************************************************************************************************
334
335