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