]> git.donarmstrong.com Git - mothur.git/blob - preclustercommand.cpp
pat's mods to morisitahorn and pre.cluster
[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 PreClusterCommand::PreClusterCommand(string option){
17         try {
18                 abort = false;
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 Array[] =  {"fasta", "name", "diffs"};
26                         vector<string> myArray (Array, Array+(sizeof(Array)/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 it2 = parameters.begin(); it2 != parameters.end(); it2++) { 
35                                 if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) {  abort = true;  }
36                         }
37                         
38                         //check for required parameters
39                         fastafile = validParameter.validFile(parameters, "fasta", true);
40                         if (fastafile == "not found") { mothurOut("fasta is a required parameter for the pre.cluster command."); mothurOutEndLine(); abort = true; }
41                         else if (fastafile == "not open") { abort = true; }     
42                         
43                         //check for optional parameter and set defaults
44                         // ...at some point should added some additional type checking...
45                         namefile = validParameter.validFile(parameters, "name", true);
46
47                         if (namefile == "not found") { namefile =  "";  }
48                         else if (namefile == "not open") { abort = true; }      
49 //                      else {  readNameFile();  }
50                         
51                         string temp     = validParameter.validFile(parameters, "diffs", false);                         if(temp == "not found"){        temp = "1"; }
52                         convert(temp, diffs); 
53                 }
54                                 
55         }
56         catch(exception& e) {
57                 errorOut(e, "PreClusterCommand", "PreClusterCommand");
58                 exit(1);
59         }
60 }
61
62 //**********************************************************************************************************************
63 PreClusterCommand::~PreClusterCommand(){}       
64 //**********************************************************************************************************************
65
66 void PreClusterCommand::help(){
67         try {
68                 mothurOut("The pre.cluster command groups sequences that are within a given number of base mismatches.\n");
69                 mothurOut("The pre.cluster command outputs a new fasta and name file.\n");
70                 mothurOut("The pre.cluster command parameters are fasta, names and diffs. The fasta parameter is required. \n");
71                 mothurOut("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");
72                 mothurOut("The diffs parameter allows you to specify maximum number of mismatched bases allowed between sequences in a grouping. The default is 1.\n");
73                 mothurOut("The pre.cluster command should be in the following format: \n");
74                 mothurOut("pre.cluster(fasta=yourFastaFile, names=yourNamesFile, diffs=yourMaxDiffs) \n");
75                 mothurOut("Example pre.cluster(fasta=amazon.fasta, diffs=2).\n");
76                 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
77         }
78         catch(exception& e) {
79                 errorOut(e, "PreClusterCommand", "help");
80                 exit(1);
81         }
82 }
83 //**********************************************************************************************************************
84
85 int PreClusterCommand::execute(){
86         try {
87                 
88                 if (abort == true) { return 0; }
89                 
90                 //reads fasta file and return number of seqs
91                 int numSeqs = readNamesFASTA(); //fills alignSeqs and makes all seqs active
92         
93                 if (numSeqs == 0) { mothurOut("Error reading fasta file...please correct."); mothurOutEndLine(); return 0;  }
94                 if (diffs > length) { mothurOut("Error: diffs is greater than your sequence length."); mothurOutEndLine(); return 0;  }
95                 
96                 //clear sizes since you only needed this info to build the alignSeqs seqPNode structs
97 //              sizes.clear();
98         
99                 //sort seqs by number of identical seqs
100                 sort(alignSeqs.begin(), alignSeqs.end(), comparePriority);
101         
102                 //go through active list and cluster everthing you can, until all nodes are inactive
103                 //taking advantage of the fact that maps are already sorted
104 //              map<string, bool>::iterator itActive;
105 //              map<string, bool>::iterator it2Active;
106                 int count = 0;
107                 
108                 for (int i = 0; i < numSeqs; i++) {
109                         
110                         //are you active
111                         //                      itActive = active.find(alignSeqs[i].seq.getName());
112                         
113                         if (alignSeqs[i].active) {  //this sequence has not been merged yet
114                                 
115                                 //try to merge it with all smaller seqs
116                                 for (int j = i+1; j < numSeqs; j++) {
117                                         if (alignSeqs[j].active) {  //this sequence has not been merged yet
118                                                 //are you within "diff" bases
119                                                 int mismatch = calcMisMatches(alignSeqs[i].seq.getAligned(), alignSeqs[j].seq.getAligned());
120                                                 
121                                                 if (mismatch <= diffs) {
122                                                         //merge
123                                                         alignSeqs[i].names += ',' + alignSeqs[j].names;
124                                                         alignSeqs[i].numIdentical += alignSeqs[j].numIdentical;
125
126                                                         alignSeqs[j].active = 0;
127                                                         alignSeqs[j].numIdentical = 0;
128                                                         count++;
129                                                 }
130                                         }//end if j active
131                                 }//end if i != j
132                         
133                         //remove from active list 
134                                 alignSeqs[i].active = 0;
135                         }//end if active i
136                         if(i % 100 == 0)        { cout << i << '\t' << numSeqs - count << '\t' << count << endl;        }
137                 }
138         
139                 string newFastaFile = getRootName(fastafile) + "precluster" + getExtension(fastafile);
140                 string newNamesFile = getRootName(fastafile) + "precluster.names";
141                 
142                 
143                 mothurOut("Total number of sequences before precluster was " + toString(alignSeqs.size()) + "."); mothurOutEndLine();
144                 mothurOut("pre.cluster removed " + toString(count) + " sequences."); mothurOutEndLine(); 
145                 printData(newFastaFile, newNamesFile);
146
147                 return 0;
148                 
149         }
150         catch(exception& e) {
151                 errorOut(e, "PreClusterCommand", "execute");
152                 exit(1);
153         }
154 }
155 /**************************************************************************************************/
156 int PreClusterCommand::readFASTA(){
157         try {
158 //              ifstream inFasta;
159 //              openInputFile(fastafile, inFasta);
160 //              length = 0;
161 ////            map<string, string>::iterator it;
162 //
163 //              while (!inFasta.eof()) {
164 //                      Sequence temp(inFasta);  //read seq
165 //                      
166 //                      if (temp.getName() != "") {  
167 //                              if (namefile != "") {
168 //                                      //make sure fasta and name files match
169 //                                      it = names.find(temp.getName());
170 //                                      if (it == names.end()) { mothurOut(temp.getName() + " is not in your names file, please correct."); mothurOutEndLine(); exit(1); }
171 //                              }else { sizes[temp.getName()] = 1; }
172 //                              
173 //                              seqPNode tempNode(sizes[temp.getName()], temp);
174 //                              alignSeqs.push_back(tempNode); 
175 //                              active[temp.getName()] = true;
176 //                      }
177 //                      gobble(inFasta);
178 //              }
179 //              inFasta.close();
180 //              
181 //              if (alignSeqs.size() != 0) {  length = alignSeqs[0].seq.getAligned().length();  }
182 //              
183                 return alignSeqs.size();
184         }
185         catch(exception& e) {
186                 errorOut(e, "PreClusterCommand", "readFASTA");
187                 exit(1);
188         }
189 }
190 /**************************************************************************************************/
191
192 int PreClusterCommand::readNamesFASTA(){
193         try {
194                 ifstream inNames;
195                 ifstream inFasta;
196                 
197                 openInputFile(namefile, inNames);
198                 openInputFile(fastafile, inFasta);
199                 
200                 string firstCol, secondCol, nameString;
201                 length = 0;
202                 
203                 while (inFasta && inNames) {
204         
205                         inNames >> firstCol >> secondCol;
206                         nameString = secondCol;
207                         
208                         gobble(inNames);
209                         int size = 1;
210                         while (secondCol.find_first_of(',') != -1) { 
211                                 size++;
212                                 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
213                         }
214                         Sequence seq(inFasta);
215                         if (seq.getName() != firstCol) { mothurOut(seq.getName() + " is not in your names file, please correct."); mothurOutEndLine(); exit(1); }
216                         else{
217                                 seqPNode tempNode(size, seq, nameString);
218                                 alignSeqs.push_back(tempNode);
219                                 if (seq.getAligned().length() > length) {  length = alignSeqs[0].seq.getAligned().length();  }
220                         }                       
221                 }
222                 inFasta.close();
223                 inNames.close();
224                 return alignSeqs.size();
225         }
226         
227         catch(exception& e) {
228                 errorOut(e, "PreClusterCommand", "readNamesFASTA");
229                 exit(1);
230         }
231 }
232
233 /**************************************************************************************************/
234
235 int PreClusterCommand::calcMisMatches(string seq1, string seq2){
236         try {
237                 int numBad = 0;
238                 
239                 for (int i = 0; i < seq1.length(); i++) {
240                         //do they match
241                         if (seq1[i] != seq2[i]) { numBad++; }
242                         if (numBad > diffs) { return length;  } //to far to cluster
243                 }
244                 
245                 return numBad;
246         }
247         catch(exception& e) {
248                 errorOut(e, "PreClusterCommand", "calcMisMatches");
249                 exit(1);
250         }
251 }
252
253 /**************************************************************************************************/
254
255 void PreClusterCommand::printData(string newfasta, string newname){
256         try {
257                 ofstream outFasta;
258                 ofstream outNames;
259                 
260                 openOutputFile(newfasta, outFasta);
261                 openOutputFile(newname, outNames);
262                                 
263                 
264                 for (int i = 0; i < alignSeqs.size(); i++) {
265                         if (alignSeqs[i].numIdentical != 0) {
266                                 alignSeqs[i].seq.printSequence(outFasta); 
267                                 outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl;
268                         }
269                 }
270                 
271                 outFasta.close();
272                 outNames.close();
273                 
274         }
275         catch(exception& e) {
276                 errorOut(e, "PreClusterCommand", "printData");
277                 exit(1);
278         }
279 }
280
281 /**************************************************************************************************/
282
283