1 #ifndef PRECLUSTERCOMMAND_H
2 #define PRECLUSTERCOMMAND_H
9 * Created by westcott on 12/21/09.
10 * Copyright 2009 Schloss Lab. All rights reserved.
15 #include "command.hpp"
16 #include "sequence.hpp"
17 #include "sequenceparser.h"
18 #include "sequencecountparser.h"
20 /************************************************************/
28 seqPNode(int n, Sequence s, string nm) : numIdentical(n), seq(s), names(nm), active(1) { diffs = 0; }
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; }
39 //************************************************************/
41 class PreClusterCommand : public Command {
44 PreClusterCommand(string);
46 ~PreClusterCommand(){}
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 "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"; }
54 string getDescription() { return "implements a pseudo-single linkage algorithm with the goal of removing sequences that are likely due to pyrosequencing errors"; }
58 void help() { m->mothurOut(getHelpString()); }
65 linePair(int i, int j) : start(i), end(j) {}
68 SequenceParser* parser;
69 SequenceCountParser* cparser;
72 int diffs, length, processors;
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;
85 //int readNamesFASTA();
86 int calcMisMatches(string, string);
87 void printData(string, string, string); //fasta filename, names file name
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);
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 {
102 string groupfile, countfile;
103 string newFName, newNName, newMName;
108 vector<string> groups;
109 vector<string> mapFileNames;
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) {
129 /**************************************************************************************************/
130 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
132 static DWORD WINAPI MyPreclusterThreadFunction(LPVOID lpParam){
133 preClusterData* pDataArray;
134 pDataArray = (preClusterData*)lpParam;
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);
144 if (pDataArray->namefile != "") { parser = new SequenceParser(pDataArray->groupfile, pDataArray->fastafile, pDataArray->namefile); }
145 else { parser = new SequenceParser(pDataArray->groupfile, pDataArray->fastafile); }
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();
154 //precluster each group
155 for (int k = pDataArray->start; k < pDataArray->end; k++) {
157 int start = time(NULL);
159 if (pDataArray->m->control_pressed) { delete parser; return 0; }
161 pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOut("Processing group " + pDataArray->groups[k] + ":"); pDataArray->m->mothurOutEndLine();
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]);
170 if (pDataArray->namefile != "") { thisNameMap = parser->getNameMap(pDataArray->groups[k]); }
172 //fill alignSeqs with this groups info.
173 ////////////////////////////////////////////////////
174 //numSeqs = loadSeqs(thisNameMap, thisSeqs); same function below
178 map<string, string>::iterator it;
180 map<string, int> thisCount;
181 if (pDataArray->countfile != "") { thisCount = cparser->getCountTable(pDataArray->groups[k]); }
184 for (int i = 0; i < thisSeqs.size(); i++) {
186 if (pDataArray->m->control_pressed) { delete parser; return 0; }
188 if (pDataArray->namefile != "") {
189 it = thisNameMap.find(thisSeqs[i].getName());
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; }
196 for(int j=0;j<(it->second).length();j++){
197 if((it->second)[j] == ','){ numReps++; }
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(); }
204 }else { //no names file, you are identical to yourself
206 if (pDataArray->countfile != "") {
207 map<string, int>::iterator it2 = thisCount.find(thisSeqs[i].getName());
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; }
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(); }
220 if (error) { pDataArray->m->control_pressed = true; }
223 numSeqs = alignSeqs.size();
225 ////////////////////////////////////////////////////
227 if (pDataArray->m->control_pressed) { delete parser; return 0; }
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; }
231 ////////////////////////////////////////////////////
232 //int count = process(); - same function below
235 pDataArray->m->openOutputFile(pDataArray->newMName+pDataArray->groups[k]+".map", out);
236 pDataArray->mapFileNames.push_back(pDataArray->newMName+pDataArray->groups[k]+".map");
238 //sort seqs by number of identical seqs
239 sort(alignSeqs.begin(), alignSeqs.end(), comparePriority);
243 //think about running through twice...
244 for (int i = 0; i < numSeqs; i++) {
247 // itActive = active.find(alignSeqs[i].seq.getName());
249 if (alignSeqs[i].active) { //this sequence has not been merged yet
251 string chunk = alignSeqs[i].seq.getName() + "\t" + toString(alignSeqs[i].numIdentical) + "\t" + toString(0) + "\t" + alignSeqs[i].seq.getAligned() + "\n";
253 //try to merge it with all smaller seqs
254 for (int j = i+1; j < numSeqs; j++) {
256 if (pDataArray->m->control_pressed) { delete parser; return 0; }
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());
263 for (int k = 0; k < alignSeqs[i].seq.getAligned().length(); k++) {
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
269 if (mismatch <= pDataArray->diffs) {
271 alignSeqs[i].names += ',' + alignSeqs[j].names;
272 alignSeqs[i].numIdentical += alignSeqs[j].numIdentical;
274 alignSeqs[j].active = 0;
275 alignSeqs[j].numIdentical = 0;
276 alignSeqs[j].diffs = mismatch;
278 chunk += alignSeqs[j].seq.getName() + "\t" + toString(alignSeqs[j].numIdentical) + "\t" + toString(mismatch) + "\t" + alignSeqs[j].seq.getAligned() + "\n";
283 //remove from active list
284 alignSeqs[i].active = 0;
286 out << "ideal_seq_" << (i+1) << '\t' << alignSeqs[i].numIdentical << endl << chunk << endl;
289 if(i % 100 == 0) { pDataArray->m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); pDataArray->m->mothurOutEndLine(); }
292 if(numSeqs % 100 != 0) { pDataArray->m->mothurOut(toString(numSeqs) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); pDataArray->m->mothurOutEndLine(); }
293 ////////////////////////////////////////////////////
295 if (pDataArray->m->control_pressed) { delete parser; return 0; }
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();
300 ////////////////////////////////////////////////////
301 //printData(pDataArray->newFFile, pDataArray->newNFile); - same as below
305 pDataArray->m->openOutputFileAppend(pDataArray->newFName, outFasta);
306 pDataArray->m->openOutputFileAppend(pDataArray->newNName, outNames);
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; }
319 ////////////////////////////////////////////////////
321 pDataArray->m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); pDataArray->m->mothurOutEndLine();
329 catch(exception& e) {
330 pDataArray->m->errorOut(e, "PreClusterCommand", "MyPreclusterThreadFunction");
336 /**************************************************************************************************/