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