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 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; }
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; }
47 //************************************************************/
49 class PreClusterCommand : public Command {
52 PreClusterCommand(string);
54 ~PreClusterCommand(){}
56 vector<string> setParameters();
57 string getCommandName() { return "pre.cluster"; }
58 string getCommandCategory() { return "Sequence Processing"; }
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"; }
67 void help() { m->mothurOut(getHelpString()); }
74 linePair(int i, int j) : start(i), end(j) {}
77 SequenceParser* parser;
78 SequenceCountParser* cparser;
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;
93 //int readNamesFASTA();
94 int calcMisMatches(string, string);
95 void printData(string, string, string); //fasta filename, names file name
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);
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 {
110 string groupfile, countfile;
111 string newFName, newNName, newMName;
116 vector<string> groups;
117 vector<string> mapFileNames;
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) {
139 /**************************************************************************************************/
140 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
142 static DWORD WINAPI MyPreclusterThreadFunction(LPVOID lpParam){
143 preClusterData* pDataArray;
144 pDataArray = (preClusterData*)lpParam;
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);
154 if (pDataArray->namefile != "") { parser = new SequenceParser(pDataArray->groupfile, pDataArray->fastafile, pDataArray->namefile); }
155 else { parser = new SequenceParser(pDataArray->groupfile, pDataArray->fastafile); }
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();
164 //precluster each group
165 for (int k = pDataArray->start; k < pDataArray->end; k++) {
167 int start = time(NULL);
169 if (pDataArray->m->control_pressed) { delete parser; return 0; }
171 pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOut("Processing group " + pDataArray->groups[k] + ":"); pDataArray->m->mothurOutEndLine();
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]);
180 if (pDataArray->namefile != "") { thisNameMap = parser->getNameMap(pDataArray->groups[k]); }
182 //fill alignSeqs with this groups info.
183 ////////////////////////////////////////////////////
184 //numSeqs = loadSeqs(thisNameMap, thisSeqs); same function below
188 map<string, string>::iterator it;
190 map<string, int> thisCount;
191 if (pDataArray->countfile != "") { thisCount = cparser->getCountTable(pDataArray->groups[k]); }
194 for (int i = 0; i < thisSeqs.size(); i++) {
196 if (pDataArray->m->control_pressed) { delete parser; return 0; }
198 if (pDataArray->namefile != "") {
199 it = thisNameMap.find(thisSeqs[i].getName());
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; }
206 for(int j=0;j<(it->second).length();j++){
207 if((it->second)[j] == ','){ numReps++; }
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(); }
214 }else { //no names file, you are identical to yourself
216 if (pDataArray->countfile != "") {
217 map<string, int>::iterator it2 = thisCount.find(thisSeqs[i].getName());
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; }
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(); }
230 if (error) { pDataArray->m->control_pressed = true; }
233 numSeqs = alignSeqs.size();
235 ////////////////////////////////////////////////////
237 if (pDataArray->m->control_pressed) { delete parser; return 0; }
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; }
241 ////////////////////////////////////////////////////
242 //int count = process(); - same function below
245 pDataArray->m->openOutputFile(pDataArray->newMName+pDataArray->groups[k]+".map", out);
246 pDataArray->mapFileNames.push_back(pDataArray->newMName+pDataArray->groups[k]+".map");
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); }
254 //think about running through twice...
255 for (int i = 0; i < numSeqs; i++) {
258 // itActive = active.find(alignSeqs[i].seq.getName());
260 if (alignSeqs[i].active) { //this sequence has not been merged yet
262 string chunk = alignSeqs[i].seq.getName() + "\t" + toString(alignSeqs[i].numIdentical) + "\t" + toString(0) + "\t" + alignSeqs[i].seq.getAligned() + "\n";
264 //try to merge it with all smaller seqs
265 for (int j = i+1; j < numSeqs; j++) {
267 if (pDataArray->m->control_pressed) { delete parser; return 0; }
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());
274 for (int k = 0; k < alignSeqs[i].seq.getAligned().length(); k++) {
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
280 if (mismatch <= pDataArray->diffs) {
282 alignSeqs[i].names += ',' + alignSeqs[j].names;
283 alignSeqs[i].numIdentical += alignSeqs[j].numIdentical;
285 alignSeqs[j].active = 0;
286 alignSeqs[j].numIdentical = 0;
287 alignSeqs[j].diffs = mismatch;
289 chunk += alignSeqs[j].seq.getName() + "\t" + toString(alignSeqs[j].numIdentical) + "\t" + toString(mismatch) + "\t" + alignSeqs[j].seq.getAligned() + "\n";
294 //remove from active list
295 alignSeqs[i].active = 0;
297 out << "ideal_seq_" << (i+1) << '\t' << alignSeqs[i].numIdentical << endl << chunk << endl;
300 if(i % 100 == 0) { pDataArray->m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); pDataArray->m->mothurOutEndLine(); }
303 if(numSeqs % 100 != 0) { pDataArray->m->mothurOut(toString(numSeqs) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); pDataArray->m->mothurOutEndLine(); }
304 ////////////////////////////////////////////////////
306 if (pDataArray->m->control_pressed) { delete parser; return 0; }
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();
311 ////////////////////////////////////////////////////
312 //printData(pDataArray->newFFile, pDataArray->newNFile); - same as below
316 pDataArray->m->openOutputFileAppend(pDataArray->newFName, outFasta);
317 pDataArray->m->openOutputFileAppend(pDataArray->newNName, outNames);
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; }
330 ////////////////////////////////////////////////////
332 pDataArray->m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); pDataArray->m->mothurOutEndLine();
340 catch(exception& e) {
341 pDataArray->m->errorOut(e, "PreClusterCommand", "MyPreclusterThreadFunction");
347 /**************************************************************************************************/