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"
19 /************************************************************/
27 seqPNode(int n, Sequence s, string nm) : numIdentical(n), seq(s), names(nm), active(1) { diffs = 0; }
30 /************************************************************/
31 inline bool comparePriority(seqPNode first, seqPNode second) { return (first.numIdentical > second.numIdentical); }
32 //************************************************************/
34 class PreClusterCommand : public Command {
37 PreClusterCommand(string);
39 ~PreClusterCommand(){}
41 vector<string> setParameters();
42 string getCommandName() { return "pre.cluster"; }
43 string getCommandCategory() { return "Sequence Processing"; }
44 string getHelpString();
45 string getCitation() { return "http://www.mothur.org/wiki/Pre.cluster"; }
46 string getDescription() { return "implements a pseudo-single linkage algorithm with the goal of removing sequences that are likely due to pyrosequencing errors"; }
50 void help() { m->mothurOut(getHelpString()); }
57 linePair(int i, int j) : start(i), end(j) {}
60 int diffs, length, processors;
62 string fastafile, namefile, outputDir, groupfile;
63 vector<seqPNode> alignSeqs; //maps the number of identical seqs to a sequence
64 map<string, string> names; //represents the names file first column maps to second column
65 map<string, int> sizes; //this map a seq name to the number of identical seqs in the names file
66 map<string, int>::iterator itSize;
67 // map<string, bool> active; //maps sequence name to whether it has already been merged or not.
68 vector<string> outputNames;
69 map<string, vector<string> > outputTypes;
73 //int readNamesFASTA();
74 int calcMisMatches(string, string);
75 void printData(string, string); //fasta filename, names file name
77 int loadSeqs(map<string, string>&, vector<Sequence>&);
78 int driverGroups(SequenceParser*, string, string, string, int, int, vector<string> groups);
79 int createProcessesGroups(SequenceParser*, string, string, string, vector<string>);
82 /**************************************************************************************************/
83 //custom data structure for threads to use.
84 // This is passed by void pointer so it can be any data type
85 // that can be passed using a single void pointer (LPVOID).
86 struct preClusterData {
90 string newFName, newNName, newMName;
95 vector<string> groups;
96 vector<string> mapFileNames;
99 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) {
115 /**************************************************************************************************/
116 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
118 static DWORD WINAPI MyPreclusterThreadFunction(LPVOID lpParam){
119 preClusterData* pDataArray;
120 pDataArray = (preClusterData*)lpParam;
124 //parse fasta and name file by group
125 SequenceParser* parser;
126 if (pDataArray->namefile != "") { parser = new SequenceParser(pDataArray->groupfile, pDataArray->fastafile, pDataArray->namefile); }
127 else { parser = new SequenceParser(pDataArray->groupfile, pDataArray->fastafile); }
130 vector<seqPNode> alignSeqs;
131 //clear out old files
132 ofstream outF; pDataArray->m->openOutputFile(pDataArray->newFName, outF); outF.close();
133 ofstream outN; pDataArray->m->openOutputFile(pDataArray->newNName, outN); outN.close();
135 //precluster each group
136 for (int k = pDataArray->start; k < pDataArray->end; k++) {
138 int start = time(NULL);
140 if (pDataArray->m->control_pressed) { delete parser; return 0; }
142 pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOut("Processing group " + pDataArray->groups[k] + ":"); pDataArray->m->mothurOutEndLine();
144 map<string, string> thisNameMap;
145 if (pDataArray->namefile != "") { thisNameMap = parser->getNameMap(pDataArray->groups[k]); }
146 vector<Sequence> thisSeqs = parser->getSeqs(pDataArray->groups[k]);
148 //fill alignSeqs with this groups info.
149 ////////////////////////////////////////////////////
150 //numSeqs = loadSeqs(thisNameMap, thisSeqs); same function below
154 map<string, string>::iterator it;
157 for (int i = 0; i < thisSeqs.size(); i++) {
159 if (pDataArray->m->control_pressed) { delete parser; return 0; }
161 if (pDataArray->namefile != "") {
162 it = thisNameMap.find(thisSeqs[i].getName());
164 //should never be true since parser checks for this
165 if (it == thisNameMap.end()) { pDataArray->m->mothurOut(thisSeqs[i].getName() + " is not in your names file, please correct."); pDataArray->m->mothurOutEndLine(); error = true; }
169 for(int j=0;j<(it->second).length();j++){
170 if((it->second)[j] == ','){ numReps++; }
173 seqPNode tempNode(numReps, thisSeqs[i], it->second);
174 alignSeqs.push_back(tempNode);
175 if (thisSeqs[i].getAligned().length() > length) { length = thisSeqs[i].getAligned().length(); }
177 }else { //no names file, you are identical to yourself
178 seqPNode tempNode(1, thisSeqs[i], thisSeqs[i].getName());
179 alignSeqs.push_back(tempNode);
180 if (thisSeqs[i].getAligned().length() > length) { length = thisSeqs[i].getAligned().length(); }
185 if (error) { pDataArray->m->control_pressed = true; }
188 numSeqs = alignSeqs.size();
190 ////////////////////////////////////////////////////
192 if (pDataArray->m->control_pressed) { delete parser; return 0; }
194 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 //int count = process(); - same function below
200 pDataArray->m->openOutputFile(pDataArray->newMName+pDataArray->groups[k]+".map", out);
201 pDataArray->mapFileNames.push_back(pDataArray->newMName+pDataArray->groups[k]+".map");
203 //sort seqs by number of identical seqs
204 sort(alignSeqs.begin(), alignSeqs.end(), comparePriority);
208 //think about running through twice...
209 for (int i = 0; i < numSeqs; i++) {
212 // itActive = active.find(alignSeqs[i].seq.getName());
214 if (alignSeqs[i].active) { //this sequence has not been merged yet
216 string chunk = alignSeqs[i].seq.getName() + "\t" + toString(alignSeqs[i].numIdentical) + "\t" + toString(0) + "\t" + alignSeqs[i].seq.getAligned() + "\n";
218 //try to merge it with all smaller seqs
219 for (int j = i+1; j < numSeqs; j++) {
221 if (pDataArray->m->control_pressed) { delete parser; return 0; }
223 if (alignSeqs[j].active) { //this sequence has not been merged yet
224 //are you within "diff" bases
225 //int mismatch = calcMisMatches(alignSeqs[i].seq.getAligned(), alignSeqs[j].seq.getAligned());
228 for (int k = 0; k < alignSeqs[i].seq.getAligned().length(); k++) {
230 if (alignSeqs[i].seq.getAligned()[k] != alignSeqs[j].seq.getAligned()[k]) { mismatch++; }
231 if (mismatch > pDataArray->diffs) { mismatch = length; break; } //to far to cluster
234 if (mismatch <= pDataArray->diffs) {
236 alignSeqs[i].names += ',' + alignSeqs[j].names;
237 alignSeqs[i].numIdentical += alignSeqs[j].numIdentical;
239 alignSeqs[j].active = 0;
240 alignSeqs[j].numIdentical = 0;
241 alignSeqs[j].diffs = mismatch;
243 chunk += alignSeqs[j].seq.getName() + "\t" + toString(alignSeqs[j].numIdentical) + "\t" + toString(mismatch) + "\t" + alignSeqs[j].seq.getAligned() + "\n";
248 //remove from active list
249 alignSeqs[i].active = 0;
251 out << "ideal_seq_" << (i+1) << '\t' << alignSeqs[i].numIdentical << endl << chunk << endl;
254 if(i % 100 == 0) { pDataArray->m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); pDataArray->m->mothurOutEndLine(); }
257 if(numSeqs % 100 != 0) { pDataArray->m->mothurOut(toString(numSeqs) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); pDataArray->m->mothurOutEndLine(); }
258 ////////////////////////////////////////////////////
260 if (pDataArray->m->control_pressed) { delete parser; return 0; }
262 pDataArray->m->mothurOut("Total number of sequences before pre.cluster was " + toString(alignSeqs.size()) + ".");pDataArray-> m->mothurOutEndLine();
263 pDataArray->m->mothurOut("pre.cluster removed " + toString(count) + " sequences."); pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOutEndLine();
265 ////////////////////////////////////////////////////
266 //printData(pDataArray->newFFile, pDataArray->newNFile); - same as below
270 pDataArray->m->openOutputFileAppend(pDataArray->newFName, outFasta);
271 pDataArray->m->openOutputFileAppend(pDataArray->newNName, outNames);
273 for (int i = 0; i < alignSeqs.size(); i++) {
274 if (alignSeqs[i].numIdentical != 0) {
275 alignSeqs[i].seq.printSequence(outFasta);
276 outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl;
282 ////////////////////////////////////////////////////
284 pDataArray->m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); pDataArray->m->mothurOutEndLine();
292 catch(exception& e) {
293 pDataArray->m->errorOut(e, "PreClusterCommand", "MyPreclusterThreadFunction");
299 /**************************************************************************************************/