]> git.donarmstrong.com Git - mothur.git/blob - preclustercommand.cpp
added getCommandInfoCommand for gui
[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                 
77                 else {
78                         vector<string> myArray = setParameters();
79                         
80                         OptionParser parser(option);
81                         map<string, string> parameters = parser.getParameters();
82                         
83                         ValidParameters validParameter;
84                         map<string, string>::iterator it;
85                 
86                         //check to make sure all parameters are valid for command
87                         for (map<string, string>::iterator it2 = parameters.begin(); it2 != parameters.end(); it2++) { 
88                                 if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) {  abort = true;  }
89                         }
90                         
91                         //initialize outputTypes
92                         vector<string> tempOutNames;
93                         outputTypes["fasta"] = tempOutNames;
94                         outputTypes["name"] = tempOutNames;
95                 
96                         //if the user changes the input directory command factory will send this info to us in the output parameter 
97                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
98                         if (inputDir == "not found"){   inputDir = "";          }
99                         else {
100                                 string path;
101                                 it = parameters.find("fasta");
102                                 //user has given a template file
103                                 if(it != parameters.end()){ 
104                                         path = m->hasPath(it->second);
105                                         //if the user has not given a path then, add inputdir. else leave path alone.
106                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
107                                 }
108                                 
109                                 it = parameters.find("name");
110                                 //user has given a template file
111                                 if(it != parameters.end()){ 
112                                         path = m->hasPath(it->second);
113                                         //if the user has not given a path then, add inputdir. else leave path alone.
114                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
115                                 }
116                         }
117
118                         //check for required parameters
119                         fastafile = validParameter.validFile(parameters, "fasta", true);
120                         if (fastafile == "not found") {                                 
121                                 fastafile = m->getFastaFile(); 
122                                 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
123                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
124                         }
125                         else if (fastafile == "not open") { abort = true; }     
126                         
127                         //if the user changes the output directory command factory will send this info to us in the output parameter 
128                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
129                                 outputDir = ""; 
130                                 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it       
131                         }
132
133                         //check for optional parameter and set defaults
134                         // ...at some point should added some additional type checking...
135                         namefile = validParameter.validFile(parameters, "name", true);
136                         if (namefile == "not found") { namefile =  "";  }
137                         else if (namefile == "not open") { abort = true; }      
138                         else {  readNameFile();  }
139                         
140                         string temp     = validParameter.validFile(parameters, "diffs", false);                         if(temp == "not found"){        temp = "1"; }
141                         convert(temp, diffs); 
142                 }
143                                 
144         }
145         catch(exception& e) {
146                 m->errorOut(e, "PreClusterCommand", "PreClusterCommand");
147                 exit(1);
148         }
149 }
150 //**********************************************************************************************************************
151
152 int PreClusterCommand::execute(){
153         try {
154                 
155                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
156                 
157                 int start = time(NULL);
158                 
159                 //reads fasta file and return number of seqs
160                 int numSeqs = readFASTA(); //fills alignSeqs and makes all seqs active
161                 
162                 if (m->control_pressed) { return 0; }
163         
164                 if (numSeqs == 0) { m->mothurOut("Error reading fasta file...please correct."); m->mothurOutEndLine(); return 0;  }
165                 if (diffs > length) { m->mothurOut("Error: diffs is greater than your sequence length."); m->mothurOutEndLine(); return 0;  }
166                 
167                 //clear sizes since you only needed this info to build the alignSeqs seqPNode structs
168 //              sizes.clear();
169         
170                 //sort seqs by number of identical seqs
171                 sort(alignSeqs.begin(), alignSeqs.end(), comparePriority);
172         
173                 int count = 0;
174
175                 //think about running through twice...
176                 for (int i = 0; i < numSeqs; i++) {
177                         
178                         //are you active
179                         //                      itActive = active.find(alignSeqs[i].seq.getName());
180                         
181                         if (alignSeqs[i].active) {  //this sequence has not been merged yet
182                                 
183                                 //try to merge it with all smaller seqs
184                                 for (int j = i+1; j < numSeqs; j++) {
185                                         
186                                         if (m->control_pressed) { return 0; }
187                                         
188                                         if (alignSeqs[j].active) {  //this sequence has not been merged yet
189                                                 //are you within "diff" bases
190                                                 int mismatch = calcMisMatches(alignSeqs[i].seq.getAligned(), alignSeqs[j].seq.getAligned());
191                                                 
192                                                 if (mismatch <= diffs) {
193                                                         //merge
194                                                         alignSeqs[i].names += ',' + alignSeqs[j].names;
195                                                         alignSeqs[i].numIdentical += alignSeqs[j].numIdentical;
196
197                                                         alignSeqs[j].active = 0;
198                                                         alignSeqs[j].numIdentical = 0;
199                                                         count++;
200                                                 }
201                                         }//end if j active
202                                 }//end if i != j
203                         
204                         //remove from active list 
205                                 alignSeqs[i].active = 0;
206                                 
207                         }//end if active i
208                         if(i % 100 == 0)        { m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); }
209                 }
210                 
211                 if(numSeqs % 100 != 0)  { m->mothurOut(toString(numSeqs) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine();   }
212         
213                 
214                 string fileroot = outputDir + m->getRootName(m->getSimpleName(fastafile));
215                 
216                 string newFastaFile = fileroot + "precluster" + m->getExtension(fastafile);
217                 string newNamesFile = fileroot + "precluster.names";
218                 
219                 if (m->control_pressed) { return 0; }
220                 
221                 m->mothurOut("Total number of sequences before precluster was " + toString(alignSeqs.size()) + "."); m->mothurOutEndLine();
222                 m->mothurOut("pre.cluster removed " + toString(count) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine(); 
223                 printData(newFastaFile, newNamesFile);
224                 
225                 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); 
226                 
227                 if (m->control_pressed) { remove(newFastaFile.c_str()); remove(newNamesFile.c_str()); return 0; }
228                 
229                 m->mothurOutEndLine();
230                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
231                 m->mothurOut(newFastaFile); m->mothurOutEndLine();      outputNames.push_back(newFastaFile); outputTypes["fasta"].push_back(newFastaFile);
232                 m->mothurOut(newNamesFile); m->mothurOutEndLine();      outputNames.push_back(newNamesFile); outputTypes["name"].push_back(newNamesFile);
233                 m->mothurOutEndLine();
234                 
235                 //set fasta file as new current fastafile
236                 string current = "";
237                 itTypes = outputTypes.find("fasta");
238                 if (itTypes != outputTypes.end()) {
239                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
240                 }
241                 
242                 itTypes = outputTypes.find("name");
243                 if (itTypes != outputTypes.end()) {
244                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
245                 }
246                 
247                 return 0;
248                 
249         }
250         catch(exception& e) {
251                 m->errorOut(e, "PreClusterCommand", "execute");
252                 exit(1);
253         }
254 }
255
256 /**************************************************************************************************/
257
258 //this requires the names and fasta file to be in the same order
259
260 int PreClusterCommand::readFASTA(){
261         try {
262                 //ifstream inNames;
263                 ifstream inFasta;
264                 
265                 //m->openInputFile(namefile, inNames);
266                 m->openInputFile(fastafile, inFasta);
267                 
268                 //string firstCol, secondCol, nameString;
269                 length = 0;
270                 
271                 while (!inFasta.eof()) {
272                         
273                         if (m->control_pressed) { inFasta.close(); return 0; }
274                         
275                         //inNames >> firstCol >> secondCol;
276                         //nameString = secondCol;
277                         
278                         //m->gobble(inNames);
279                         //int size = 1;
280                         //while (secondCol.find_first_of(',') != -1) { 
281                         //      size++;
282                         //      secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
283                         //}
284                         
285                         Sequence seq(inFasta);  m->gobble(inFasta);
286                         
287                         if (seq.getName() != "") {  //can get "" if commented line is at end of fasta file
288                                 if (namefile != "") {
289                                         itSize = sizes.find(seq.getName());
290                                         
291                                         if (itSize == sizes.end()) { m->mothurOut(seq.getName() + " is not in your names file, please correct."); m->mothurOutEndLine(); exit(1); }
292                                         else{
293                                                 seqPNode tempNode(itSize->second, seq, names[seq.getName()]);
294                                                 alignSeqs.push_back(tempNode);
295                                                 if (seq.getAligned().length() > length) {  length = seq.getAligned().length();  }
296                                         }       
297                                 }else { //no names file, you are identical to yourself 
298                                         seqPNode tempNode(1, seq, seq.getName());
299                                         alignSeqs.push_back(tempNode);
300                                         if (seq.getAligned().length() > length) {  length = seq.getAligned().length();  }
301                                 }
302                         }
303                 }
304                 inFasta.close();
305                 //inNames.close();
306                 return alignSeqs.size();
307         }
308         
309         catch(exception& e) {
310                 m->errorOut(e, "PreClusterCommand", "readFASTA");
311                 exit(1);
312         }
313 }
314
315 /**************************************************************************************************/
316
317 int PreClusterCommand::calcMisMatches(string seq1, string seq2){
318         try {
319                 int numBad = 0;
320                 
321                 for (int i = 0; i < seq1.length(); i++) {
322                         //do they match
323                         if (seq1[i] != seq2[i]) { numBad++; }
324                         if (numBad > diffs) { return length;  } //to far to cluster
325                 }
326                 
327                 return numBad;
328         }
329         catch(exception& e) {
330                 m->errorOut(e, "PreClusterCommand", "calcMisMatches");
331                 exit(1);
332         }
333 }
334
335 /**************************************************************************************************/
336
337 void PreClusterCommand::printData(string newfasta, string newname){
338         try {
339                 ofstream outFasta;
340                 ofstream outNames;
341                 
342                 m->openOutputFile(newfasta, outFasta);
343                 m->openOutputFile(newname, outNames);
344                                 
345                 
346                 for (int i = 0; i < alignSeqs.size(); i++) {
347                         if (alignSeqs[i].numIdentical != 0) {
348                                 alignSeqs[i].seq.printSequence(outFasta); 
349                                 outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl;
350                         }
351                 }
352                 
353                 outFasta.close();
354                 outNames.close();
355                 
356         }
357         catch(exception& e) {
358                 m->errorOut(e, "PreClusterCommand", "printData");
359                 exit(1);
360         }
361 }
362 /**************************************************************************************************/
363
364 void PreClusterCommand::readNameFile(){
365         try {
366                 ifstream in;
367                 m->openInputFile(namefile, in);
368                 string firstCol, secondCol;
369                                 
370                 while (!in.eof()) {
371                         in >> firstCol >> secondCol; m->gobble(in);
372                         names[firstCol] = secondCol;
373                         int size = 1;
374
375                         for(int i=0;i<secondCol.size();i++){
376                                 if(secondCol[i] == ','){        size++; }
377                         }
378                         sizes[firstCol] = size;
379                 }
380                 in.close();
381         }
382         catch(exception& e) {
383                 m->errorOut(e, "PreClusterCommand", "readNameFile");
384                 exit(1);
385         }
386 }
387
388 /**************************************************************************************************/
389
390