]> git.donarmstrong.com Git - mothur.git/blob - preclustercommand.cpp
rewrote metastats command in c++, added mothurRemove function to handle ~ error....
[mothur.git] / preclustercommand.cpp
1 /*
2  *  preclustercommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 12/21/09.
6  *  Copyright 2009 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "preclustercommand.h"
11
12 //**********************************************************************************************************************
13 inline bool comparePriority(seqPNode first, seqPNode second) {  return (first.numIdentical > second.numIdentical); }
14
15 //**********************************************************************************************************************
16 vector<string> PreClusterCommand::setParameters(){      
17         try {
18                 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
19                 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
20                 CommandParameter pdiffs("diffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(pdiffs);
21                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
22                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
23                 
24                 vector<string> myArray;
25                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
26                 return myArray;
27         }
28         catch(exception& e) {
29                 m->errorOut(e, "PreClusterCommand", "setParameters");
30                 exit(1);
31         }
32 }
33 //**********************************************************************************************************************
34 string PreClusterCommand::getHelpString(){      
35         try {
36                 string helpString = "";
37                 helpString += "The pre.cluster command groups sequences that are within a given number of base mismatches.\n";
38                 helpString += "The pre.cluster command outputs a new fasta and name file.\n";
39                 helpString += "The pre.cluster command parameters are fasta, names and diffs. The fasta parameter is required. \n";
40                 helpString += "The names parameter allows you to give a list of seqs that are identical. This file is 2 columns, first column is name or representative sequence, second column is a list of its identical sequences separated by commas.\n";
41                 helpString += "The diffs parameter allows you to specify maximum number of mismatched bases allowed between sequences in a grouping. The default is 1.\n";
42                 helpString += "The pre.cluster command should be in the following format: \n";
43                 helpString += "pre.cluster(fasta=yourFastaFile, names=yourNamesFile, diffs=yourMaxDiffs) \n";
44                 helpString += "Example pre.cluster(fasta=amazon.fasta, diffs=2).\n";
45                 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
46                 return helpString;
47         }
48         catch(exception& e) {
49                 m->errorOut(e, "PreClusterCommand", "getHelpString");
50                 exit(1);
51         }
52 }
53
54 //**********************************************************************************************************************
55 PreClusterCommand::PreClusterCommand(){ 
56         try {
57                 abort = true; calledHelp = true; 
58                 setParameters();
59                 vector<string> tempOutNames;
60                 outputTypes["fasta"] = tempOutNames;
61                 outputTypes["name"] = tempOutNames;
62         }
63         catch(exception& e) {
64                 m->errorOut(e, "PreClusterCommand", "PreClusterCommand");
65                 exit(1);
66         }
67 }
68 //**********************************************************************************************************************
69
70 PreClusterCommand::PreClusterCommand(string option) {
71         try {
72                 abort = false; calledHelp = false;   
73                 
74                 //allow user to run help
75                 if(option == "help") { help(); abort = true; calledHelp = true; }
76                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
77                 
78                 else {
79                         vector<string> myArray = setParameters();
80                         
81                         OptionParser parser(option);
82                         map<string, string> parameters = parser.getParameters();
83                         
84                         ValidParameters validParameter;
85                         map<string, string>::iterator it;
86                 
87                         //check to make sure all parameters are valid for command
88                         for (map<string, string>::iterator it2 = parameters.begin(); it2 != parameters.end(); it2++) { 
89                                 if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) {  abort = true;  }
90                         }
91                         
92                         //initialize outputTypes
93                         vector<string> tempOutNames;
94                         outputTypes["fasta"] = tempOutNames;
95                         outputTypes["name"] = tempOutNames;
96                 
97                         //if the user changes the input directory command factory will send this info to us in the output parameter 
98                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
99                         if (inputDir == "not found"){   inputDir = "";          }
100                         else {
101                                 string path;
102                                 it = parameters.find("fasta");
103                                 //user has given a template file
104                                 if(it != parameters.end()){ 
105                                         path = m->hasPath(it->second);
106                                         //if the user has not given a path then, add inputdir. else leave path alone.
107                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
108                                 }
109                                 
110                                 it = parameters.find("name");
111                                 //user has given a template file
112                                 if(it != parameters.end()){ 
113                                         path = m->hasPath(it->second);
114                                         //if the user has not given a path then, add inputdir. else leave path alone.
115                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
116                                 }
117                         }
118
119                         //check for required parameters
120                         fastafile = validParameter.validFile(parameters, "fasta", true);
121                         if (fastafile == "not found") {                                 
122                                 fastafile = m->getFastaFile(); 
123                                 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
124                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
125                         }
126                         else if (fastafile == "not open") { abort = true; }     
127                         else { m->setFastaFile(fastafile); }
128                         
129                         //if the user changes the output directory command factory will send this info to us in the output parameter 
130                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
131                                 outputDir = ""; 
132                                 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it       
133                         }
134
135                         //check for optional parameter and set defaults
136                         // ...at some point should added some additional type checking...
137                         namefile = validParameter.validFile(parameters, "name", true);
138                         if (namefile == "not found") { namefile =  "";  }
139                         else if (namefile == "not open") { abort = true; }      
140                         else {  readNameFile();  m->setNameFile(namefile); }
141                         
142                         string temp     = validParameter.validFile(parameters, "diffs", false);                         if(temp == "not found"){        temp = "1"; }
143                         convert(temp, diffs); 
144                 }
145                                 
146         }
147         catch(exception& e) {
148                 m->errorOut(e, "PreClusterCommand", "PreClusterCommand");
149                 exit(1);
150         }
151 }
152 //**********************************************************************************************************************
153
154 int PreClusterCommand::execute(){
155         try {
156                 
157                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
158                 
159                 int start = time(NULL);
160                 
161                 //reads fasta file and return number of seqs
162                 int numSeqs = readFASTA(); //fills alignSeqs and makes all seqs active
163                 
164                 if (m->control_pressed) { return 0; }
165         
166                 if (numSeqs == 0) { m->mothurOut("Error reading fasta file...please correct."); m->mothurOutEndLine(); return 0;  }
167                 if (diffs > length) { m->mothurOut("Error: diffs is greater than your sequence length."); m->mothurOutEndLine(); return 0;  }
168                 
169                 //clear sizes since you only needed this info to build the alignSeqs seqPNode structs
170 //              sizes.clear();
171         
172                 //sort seqs by number of identical seqs
173                 sort(alignSeqs.begin(), alignSeqs.end(), comparePriority);
174         
175                 int count = 0;
176
177                 //think about running through twice...
178                 for (int i = 0; i < numSeqs; i++) {
179                         
180                         //are you active
181                         //                      itActive = active.find(alignSeqs[i].seq.getName());
182                         
183                         if (alignSeqs[i].active) {  //this sequence has not been merged yet
184                                 
185                                 //try to merge it with all smaller seqs
186                                 for (int j = i+1; j < numSeqs; j++) {
187                                         
188                                         if (m->control_pressed) { return 0; }
189                                         
190                                         if (alignSeqs[j].active) {  //this sequence has not been merged yet
191                                                 //are you within "diff" bases
192                                                 int mismatch = calcMisMatches(alignSeqs[i].seq.getAligned(), alignSeqs[j].seq.getAligned());
193                                                 
194                                                 if (mismatch <= diffs) {
195                                                         //merge
196                                                         alignSeqs[i].names += ',' + alignSeqs[j].names;
197                                                         alignSeqs[i].numIdentical += alignSeqs[j].numIdentical;
198
199                                                         alignSeqs[j].active = 0;
200                                                         alignSeqs[j].numIdentical = 0;
201                                                         count++;
202                                                 }
203                                         }//end if j active
204                                 }//end if i != j
205                         
206                         //remove from active list 
207                                 alignSeqs[i].active = 0;
208                                 
209                         }//end if active i
210                         if(i % 100 == 0)        { m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); }
211                 }
212                 
213                 if(numSeqs % 100 != 0)  { m->mothurOut(toString(numSeqs) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine();   }
214         
215                 
216                 string fileroot = outputDir + m->getRootName(m->getSimpleName(fastafile));
217                 
218                 string newFastaFile = fileroot + "precluster" + m->getExtension(fastafile);
219                 string newNamesFile = fileroot + "precluster.names";
220                 
221                 if (m->control_pressed) { return 0; }
222                 
223                 m->mothurOut("Total number of sequences before precluster was " + toString(alignSeqs.size()) + "."); m->mothurOutEndLine();
224                 m->mothurOut("pre.cluster removed " + toString(count) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine(); 
225                 printData(newFastaFile, newNamesFile);
226                 
227                 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); 
228                 
229                 if (m->control_pressed) { m->mothurRemove(newFastaFile); m->mothurRemove(newNamesFile); return 0; }
230                 
231                 m->mothurOutEndLine();
232                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
233                 m->mothurOut(newFastaFile); m->mothurOutEndLine();      outputNames.push_back(newFastaFile); outputTypes["fasta"].push_back(newFastaFile);
234                 m->mothurOut(newNamesFile); m->mothurOutEndLine();      outputNames.push_back(newNamesFile); outputTypes["name"].push_back(newNamesFile);
235                 m->mothurOutEndLine();
236                 
237                 //set fasta file as new current fastafile
238                 string current = "";
239                 itTypes = outputTypes.find("fasta");
240                 if (itTypes != outputTypes.end()) {
241                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
242                 }
243                 
244                 itTypes = outputTypes.find("name");
245                 if (itTypes != outputTypes.end()) {
246                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
247                 }
248                 
249                 return 0;
250                 
251         }
252         catch(exception& e) {
253                 m->errorOut(e, "PreClusterCommand", "execute");
254                 exit(1);
255         }
256 }
257
258 /**************************************************************************************************/
259
260 //this requires the names and fasta file to be in the same order
261
262 int PreClusterCommand::readFASTA(){
263         try {
264                 //ifstream inNames;
265                 ifstream inFasta;
266                 
267                 //m->openInputFile(namefile, inNames);
268                 m->openInputFile(fastafile, inFasta);
269                 
270                 //string firstCol, secondCol, nameString;
271                 length = 0;
272                 
273                 while (!inFasta.eof()) {
274                         
275                         if (m->control_pressed) { inFasta.close(); return 0; }
276                         
277                         //inNames >> firstCol >> secondCol;
278                         //nameString = secondCol;
279                         
280                         //m->gobble(inNames);
281                         //int size = 1;
282                         //while (secondCol.find_first_of(',') != -1) { 
283                         //      size++;
284                         //      secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
285                         //}
286                         
287                         Sequence seq(inFasta);  m->gobble(inFasta);
288                         
289                         if (seq.getName() != "") {  //can get "" if commented line is at end of fasta file
290                                 if (namefile != "") {
291                                         itSize = sizes.find(seq.getName());
292                                         
293                                         if (itSize == sizes.end()) { m->mothurOut(seq.getName() + " is not in your names file, please correct."); m->mothurOutEndLine(); exit(1); }
294                                         else{
295                                                 seqPNode tempNode(itSize->second, seq, names[seq.getName()]);
296                                                 alignSeqs.push_back(tempNode);
297                                                 if (seq.getAligned().length() > length) {  length = seq.getAligned().length();  }
298                                         }       
299                                 }else { //no names file, you are identical to yourself 
300                                         seqPNode tempNode(1, seq, seq.getName());
301                                         alignSeqs.push_back(tempNode);
302                                         if (seq.getAligned().length() > length) {  length = seq.getAligned().length();  }
303                                 }
304                         }
305                 }
306                 inFasta.close();
307                 //inNames.close();
308                 return alignSeqs.size();
309         }
310         
311         catch(exception& e) {
312                 m->errorOut(e, "PreClusterCommand", "readFASTA");
313                 exit(1);
314         }
315 }
316
317 /**************************************************************************************************/
318
319 int PreClusterCommand::calcMisMatches(string seq1, string seq2){
320         try {
321                 int numBad = 0;
322                 
323                 for (int i = 0; i < seq1.length(); i++) {
324                         //do they match
325                         if (seq1[i] != seq2[i]) { numBad++; }
326                         if (numBad > diffs) { return length;  } //to far to cluster
327                 }
328                 
329                 return numBad;
330         }
331         catch(exception& e) {
332                 m->errorOut(e, "PreClusterCommand", "calcMisMatches");
333                 exit(1);
334         }
335 }
336
337 /**************************************************************************************************/
338
339 void PreClusterCommand::printData(string newfasta, string newname){
340         try {
341                 ofstream outFasta;
342                 ofstream outNames;
343                 
344                 m->openOutputFile(newfasta, outFasta);
345                 m->openOutputFile(newname, outNames);
346                                 
347                 
348                 for (int i = 0; i < alignSeqs.size(); i++) {
349                         if (alignSeqs[i].numIdentical != 0) {
350                                 alignSeqs[i].seq.printSequence(outFasta); 
351                                 outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl;
352                         }
353                 }
354                 
355                 outFasta.close();
356                 outNames.close();
357                 
358         }
359         catch(exception& e) {
360                 m->errorOut(e, "PreClusterCommand", "printData");
361                 exit(1);
362         }
363 }
364 /**************************************************************************************************/
365
366 void PreClusterCommand::readNameFile(){
367         try {
368                 ifstream in;
369                 m->openInputFile(namefile, in);
370                 string firstCol, secondCol;
371                                 
372                 while (!in.eof()) {
373                         in >> firstCol >> secondCol; m->gobble(in);
374                         names[firstCol] = secondCol;
375                         int size = 1;
376
377                         for(int i=0;i<secondCol.size();i++){
378                                 if(secondCol[i] == ','){        size++; }
379                         }
380                         sizes[firstCol] = size;
381                 }
382                 in.close();
383         }
384         catch(exception& e) {
385                 m->errorOut(e, "PreClusterCommand", "readNameFile");
386                 exit(1);
387         }
388 }
389
390 /**************************************************************************************************/
391
392