]> git.donarmstrong.com Git - mothur.git/blob - preclustercommand.h
added topdown parameter to pre.cluster. added more debugging output to bayesian...
[mothur.git] / preclustercommand.h
1 #ifndef PRECLUSTERCOMMAND_H
2 #define PRECLUSTERCOMMAND_H
3
4
5 /*
6  *  preclustercommand.h
7  *  Mothur
8  *
9  *  Created by westcott on 12/21/09.
10  *  Copyright 2009 Schloss Lab. All rights reserved.
11  *
12  */
13
14
15 #include "command.hpp"
16 #include "sequence.hpp"
17 #include "sequenceparser.h"
18 #include "sequencecountparser.h"
19
20 /************************************************************/
21 struct seqPNode {
22         int numIdentical;
23         Sequence seq;
24         string names;
25         bool active;
26         int diffs;
27         seqPNode() {}
28         seqPNode(int n, Sequence s, string nm) : numIdentical(n), seq(s), names(nm), active(1) { diffs = 0; }
29         ~seqPNode() {}
30 };
31 /************************************************************/
32 inline bool comparePriorityTopDown(seqPNode first, seqPNode second) {  
33     if (first.numIdentical > second.numIdentical) { return true;  }
34     else if (first.numIdentical == second.numIdentical) { 
35         if (first.seq.getName() > second.seq.getName()) { return true; }
36     }
37     return false; 
38 }
39 /************************************************************/
40 inline bool comparePriorityDownTop(seqPNode first, seqPNode second) {  
41     if (first.numIdentical < second.numIdentical) { return true;  }
42     else if (first.numIdentical == second.numIdentical) { 
43         if (first.seq.getName() > second.seq.getName()) { return true; }
44     }
45     return false; 
46 }
47 //************************************************************/
48
49 class PreClusterCommand : public Command {
50         
51 public:
52         PreClusterCommand(string);
53         PreClusterCommand();
54         ~PreClusterCommand(){}
55         
56         vector<string> setParameters();
57         string getCommandName()                 { return "pre.cluster";                         }
58         string getCommandCategory()             { return "Sequence Processing";         }
59         
60         string getHelpString(); 
61     string getOutputPattern(string);    
62         string getCitation() { return "Schloss PD, Gevers D, Westcott SL (2011).  Reducing the effects of PCR amplification and sequencing artifacts on 16S rRNA-based studies.  PLoS ONE.  6:e27310.\nhttp://www.mothur.org/wiki/Pre.cluster"; }
63         string getDescription()         { return "implements a pseudo-single linkage algorithm with the goal of removing sequences that are likely due to pyrosequencing errors"; }
64
65         
66         int execute(); 
67         void help() { m->mothurOut(getHelpString()); }  
68         
69 private:
70         
71         struct linePair {
72                 int start;
73                 int end;
74                 linePair(int i, int j) : start(i), end(j) {}
75         };
76         
77     SequenceParser* parser;
78     SequenceCountParser* cparser;
79     CountTable ct;
80     
81         int diffs, length, processors;
82         bool abort, bygroup, topdown;
83         string fastafile, namefile, outputDir, groupfile, countfile;
84         vector<seqPNode> alignSeqs; //maps the number of identical seqs to a sequence
85         map<string, string> names; //represents the names file first column maps to second column
86         map<string, int> sizes;  //this map a seq name to the number of identical seqs in the names file
87         map<string, int>::iterator itSize; 
88 //      map<string, bool> active; //maps sequence name to whether it has already been merged or not.
89         vector<string> outputNames;
90         
91         int readFASTA();
92         void readNameFile();
93         //int readNamesFASTA();
94         int calcMisMatches(string, string);
95         void printData(string, string, string); //fasta filename, names file name
96         int process(string);
97         int loadSeqs(map<string, string>&, vector<Sequence>&, string);
98         int driverGroups(string, string, string, int, int, vector<string> groups);
99         int createProcessesGroups(string, string, string, vector<string>);
100     int mergeGroupCounts(string, string, string);
101 };
102
103 /**************************************************************************************************/
104 //custom data structure for threads to use.
105 // This is passed by void pointer so it can be any data type
106 // that can be passed using a single void pointer (LPVOID).
107 struct preClusterData {
108         string fastafile; 
109         string namefile; 
110         string groupfile, countfile;
111         string newFName, newNName, newMName;
112         MothurOut* m;
113         int start;
114         int end;
115         int diffs, threadID;
116         vector<string> groups;
117         vector<string> mapFileNames;
118     bool topdown;
119         
120         preClusterData(){}
121         preClusterData(string f, string n, string g, string c, string nff,  string nnf, string nmf, vector<string> gr, MothurOut* mout, int st, int en, int d, bool td, int tid) {
122                 fastafile = f;
123                 namefile = n;
124                 groupfile = g;
125                 newFName = nff;
126                 newNName = nnf;
127                 newMName = nmf;
128                 m = mout;
129                 start = st;
130                 end = en;
131                 diffs = d;
132                 threadID = tid;
133                 groups = gr;
134         countfile = c;
135         topdown = td;
136         }
137 };
138
139 /**************************************************************************************************/
140 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
141 #else
142 static DWORD WINAPI MyPreclusterThreadFunction(LPVOID lpParam){ 
143         preClusterData* pDataArray;
144         pDataArray = (preClusterData*)lpParam;
145         
146         try {
147                 
148                 //parse fasta and name file by group
149                 SequenceParser* parser;
150         SequenceCountParser* cparser;
151         if (pDataArray->countfile != "") {
152             cparser = new SequenceCountParser(pDataArray->countfile, pDataArray->fastafile);
153         }else {
154             if (pDataArray->namefile != "") { parser = new SequenceParser(pDataArray->groupfile, pDataArray->fastafile, pDataArray->namefile);  }
155             else                                { parser = new SequenceParser(pDataArray->groupfile, pDataArray->fastafile);                    }
156         }
157         
158                 int numSeqs = 0;
159                 vector<seqPNode> alignSeqs;
160                 //clear out old files
161                 ofstream outF; pDataArray->m->openOutputFile(pDataArray->newFName, outF); outF.close();
162                 ofstream outN; pDataArray->m->openOutputFile(pDataArray->newNName, outN);  outN.close();
163                 
164                 //precluster each group
165                 for (int k = pDataArray->start; k < pDataArray->end; k++) {
166                         
167                         int start = time(NULL);
168                         
169                         if (pDataArray->m->control_pressed) {  delete parser; return 0; }
170                         
171                         pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOut("Processing group " + pDataArray->groups[k] + ":"); pDataArray->m->mothurOutEndLine();
172                         
173                         map<string, string> thisNameMap;
174             vector<Sequence> thisSeqs;
175                         if (pDataArray->groupfile != "") { 
176                 thisSeqs = parser->getSeqs(pDataArray->groups[k]);
177             }else if (pDataArray->countfile != "") {
178                 thisSeqs = cparser->getSeqs(pDataArray->groups[k]);
179             }
180                         if (pDataArray->namefile != "") {  thisNameMap = parser->getNameMap(pDataArray->groups[k]); }
181                         
182                         //fill alignSeqs with this groups info.
183                         ////////////////////////////////////////////////////
184                         //numSeqs = loadSeqs(thisNameMap, thisSeqs); same function below
185                         
186                         int length = 0;
187                         alignSeqs.clear();
188                         map<string, string>::iterator it;
189                         bool error = false;
190             map<string, int> thisCount;
191             if (pDataArray->countfile != "") { thisCount = cparser->getCountTable(pDataArray->groups[k]);  }
192
193                         
194                         for (int i = 0; i < thisSeqs.size(); i++) {
195                                 
196                                 if (pDataArray->m->control_pressed) { delete parser; return 0; }
197                                 
198                                 if (pDataArray->namefile != "") {
199                                         it = thisNameMap.find(thisSeqs[i].getName());
200                                         
201                                         //should never be true since parser checks for this
202                                         if (it == thisNameMap.end()) { pDataArray->m->mothurOut(thisSeqs[i].getName() + " is not in your names file, please correct."); pDataArray->m->mothurOutEndLine(); error = true; }
203                                         else{
204                                                 //get number of reps
205                                                 int numReps = 1;
206                                                 for(int j=0;j<(it->second).length();j++){
207                                                         if((it->second)[j] == ','){     numReps++;      }
208                                                 }
209                                                 
210                                                 seqPNode tempNode(numReps, thisSeqs[i], it->second);
211                                                 alignSeqs.push_back(tempNode);
212                                                 if (thisSeqs[i].getAligned().length() > length) {  length = thisSeqs[i].getAligned().length();  }
213                                         }       
214                                 }else { //no names file, you are identical to yourself 
215                                         int numRep = 1;
216                     if (pDataArray->countfile != "") { 
217                         map<string, int>::iterator it2 = thisCount.find(thisSeqs[i].getName());
218                         
219                         //should never be true since parser checks for this
220                         if (it2 == thisCount.end()) { pDataArray->m->mothurOut(thisSeqs[i].getName() + " is not in your count file, please correct."); pDataArray->m->mothurOutEndLine(); error = true; }
221                         else { numRep = it2->second;  }
222                     }
223                     seqPNode tempNode(numRep, thisSeqs[i], thisSeqs[i].getName());
224                     alignSeqs.push_back(tempNode);
225                                         if (thisSeqs[i].getAligned().length() > length) {  length = thisSeqs[i].getAligned().length();  }
226                                 }
227                         }
228                         
229                         //sanity check
230                         if (error) { pDataArray->m->control_pressed = true; }
231                         
232                         thisSeqs.clear();
233                         numSeqs = alignSeqs.size();
234                         
235                         ////////////////////////////////////////////////////
236                         
237                         if (pDataArray->m->control_pressed) {   delete parser; return 0; }
238                         
239                         if (pDataArray->diffs > length) { pDataArray->m->mothurOut("Error: diffs is greater than your sequence length."); pDataArray->m->mothurOutEndLine(); pDataArray->m->control_pressed = true; return 0;  }
240                         
241                         ////////////////////////////////////////////////////
242                         //int count = process(); - same function below
243                         
244                         ofstream out;
245                         pDataArray->m->openOutputFile(pDataArray->newMName+pDataArray->groups[k]+".map", out);
246                         pDataArray->mapFileNames.push_back(pDataArray->newMName+pDataArray->groups[k]+".map");
247                         
248             //sort seqs by number of identical seqs
249             if (pDataArray->topdown) { sort(alignSeqs.begin(), alignSeqs.end(), comparePriorityTopDown);  }
250             else {  sort(alignSeqs.begin(), alignSeqs.end(), comparePriorityDownTop);  }
251             
252                         int count = 0;
253                         
254                         //think about running through twice...
255                         for (int i = 0; i < numSeqs; i++) {
256                                 
257                                 //are you active
258                                 //                      itActive = active.find(alignSeqs[i].seq.getName());
259                                 
260                                 if (alignSeqs[i].active) {  //this sequence has not been merged yet
261                                         
262                                         string chunk = alignSeqs[i].seq.getName() + "\t" + toString(alignSeqs[i].numIdentical) + "\t" + toString(0) + "\t" + alignSeqs[i].seq.getAligned() + "\n";
263
264                                         //try to merge it with all smaller seqs
265                                         for (int j = i+1; j < numSeqs; j++) {
266                                                 
267                                                 if (pDataArray->m->control_pressed) { delete parser; return 0; }
268                                                 
269                                                 if (alignSeqs[j].active) {  //this sequence has not been merged yet
270                                                         //are you within "diff" bases
271                                                         //int mismatch = calcMisMatches(alignSeqs[i].seq.getAligned(), alignSeqs[j].seq.getAligned());
272                                                         int mismatch = 0;
273                                                         
274                                                         for (int k = 0; k < alignSeqs[i].seq.getAligned().length(); k++) {
275                                                                 //do they match
276                                                                 if (alignSeqs[i].seq.getAligned()[k] != alignSeqs[j].seq.getAligned()[k]) { mismatch++; }
277                                                                 if (mismatch > pDataArray->diffs) { mismatch = length; break; } //to far to cluster
278                                                         }
279                                                         
280                                                         if (mismatch <= pDataArray->diffs) {
281                                                                 //merge
282                                                                 alignSeqs[i].names += ',' + alignSeqs[j].names;
283                                                                 alignSeqs[i].numIdentical += alignSeqs[j].numIdentical;
284                                                                 
285                                                                 alignSeqs[j].active = 0;
286                                                                 alignSeqs[j].numIdentical = 0;
287                                                                 alignSeqs[j].diffs = mismatch;
288                                                                 count++;
289                                                                 chunk += alignSeqs[j].seq.getName() + "\t" + toString(alignSeqs[j].numIdentical) + "\t" + toString(mismatch) + "\t" + alignSeqs[j].seq.getAligned() + "\n";
290                                                         }
291                                                 }//end if j active
292                                         }//end for loop j
293                                         
294                                         //remove from active list 
295                                         alignSeqs[i].active = 0;
296                                         
297                                         out << "ideal_seq_" << (i+1) << '\t' << alignSeqs[i].numIdentical << endl << chunk << endl;
298                                         
299                                 }//end if active i
300                                 if(i % 100 == 0)        { pDataArray->m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); pDataArray->m->mothurOutEndLine(); }
301                         }
302                         out.close();
303                         if(numSeqs % 100 != 0)  { pDataArray->m->mothurOut(toString(numSeqs) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); pDataArray->m->mothurOutEndLine();   }       
304                         ////////////////////////////////////////////////////
305                         
306                         if (pDataArray->m->control_pressed) {  delete parser; return 0; }
307                         
308                         pDataArray->m->mothurOut("Total number of sequences before pre.cluster was " + toString(alignSeqs.size()) + ".");pDataArray-> m->mothurOutEndLine();
309                         pDataArray->m->mothurOut("pre.cluster removed " + toString(count) + " sequences."); pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOutEndLine(); 
310                         
311                         ////////////////////////////////////////////////////
312                         //printData(pDataArray->newFFile, pDataArray->newNFile); - same as below
313                         ofstream outFasta;
314                         ofstream outNames;
315                         
316                         pDataArray->m->openOutputFileAppend(pDataArray->newFName, outFasta);
317                         pDataArray->m->openOutputFileAppend(pDataArray->newNName, outNames);
318                                                 
319                         for (int i = 0; i < alignSeqs.size(); i++) {
320                                 if (alignSeqs[i].numIdentical != 0) {
321                                         alignSeqs[i].seq.printSequence(outFasta); 
322                                         if (pDataArray->countfile != "") {  outNames << pDataArray->groups[k] << '\t' << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl; 
323                     }else {  outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl;  }
324
325                                 }
326                         }
327                         
328                         outFasta.close();
329                         outNames.close();
330                         ////////////////////////////////////////////////////
331                         
332                         pDataArray->m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); pDataArray->m->mothurOutEndLine(); 
333                         
334                 }
335                 
336                 return numSeqs;
337                 
338
339         }
340         catch(exception& e) {
341                 pDataArray->m->errorOut(e, "PreClusterCommand", "MyPreclusterThreadFunction");
342                 exit(1);
343         }
344
345 #endif
346
347 /**************************************************************************************************/
348
349
350 #endif
351
352