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