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