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 getOutputFileNameTag(string, string);
45 string getHelpString();
46 string getCitation() { return "http://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"; }
51 void help() { m->mothurOut(getHelpString()); }
58 linePair(int i, int j) : start(i), end(j) {}
61 int diffs, length, processors;
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;
74 //int readNamesFASTA();
75 int calcMisMatches(string, string);
76 void printData(string, string); //fasta filename, names file name
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>);
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 {
91 string newFName, newNName, newMName;
96 vector<string> groups;
97 vector<string> mapFileNames;
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) {
116 /**************************************************************************************************/
117 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
119 static DWORD WINAPI MyPreclusterThreadFunction(LPVOID lpParam){
120 preClusterData* pDataArray;
121 pDataArray = (preClusterData*)lpParam;
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); }
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();
136 //precluster each group
137 for (int k = pDataArray->start; k < pDataArray->end; k++) {
139 int start = time(NULL);
141 if (pDataArray->m->control_pressed) { delete parser; return 0; }
143 pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOut("Processing group " + pDataArray->groups[k] + ":"); pDataArray->m->mothurOutEndLine();
145 map<string, string> thisNameMap;
146 if (pDataArray->namefile != "") { thisNameMap = parser->getNameMap(pDataArray->groups[k]); }
147 vector<Sequence> thisSeqs = parser->getSeqs(pDataArray->groups[k]);
149 //fill alignSeqs with this groups info.
150 ////////////////////////////////////////////////////
151 //numSeqs = loadSeqs(thisNameMap, thisSeqs); same function below
155 map<string, string>::iterator it;
158 for (int i = 0; i < thisSeqs.size(); i++) {
160 if (pDataArray->m->control_pressed) { delete parser; return 0; }
162 if (pDataArray->namefile != "") {
163 it = thisNameMap.find(thisSeqs[i].getName());
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; }
170 for(int j=0;j<(it->second).length();j++){
171 if((it->second)[j] == ','){ numReps++; }
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(); }
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(); }
186 if (error) { pDataArray->m->control_pressed = true; }
189 numSeqs = alignSeqs.size();
191 ////////////////////////////////////////////////////
193 if (pDataArray->m->control_pressed) { delete parser; return 0; }
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; }
197 ////////////////////////////////////////////////////
198 //int count = process(); - same function below
201 pDataArray->m->openOutputFile(pDataArray->newMName+pDataArray->groups[k]+".map", out);
202 pDataArray->mapFileNames.push_back(pDataArray->newMName+pDataArray->groups[k]+".map");
204 //sort seqs by number of identical seqs
205 sort(alignSeqs.begin(), alignSeqs.end(), comparePriority);
209 //think about running through twice...
210 for (int i = 0; i < numSeqs; i++) {
213 // itActive = active.find(alignSeqs[i].seq.getName());
215 if (alignSeqs[i].active) { //this sequence has not been merged yet
217 string chunk = alignSeqs[i].seq.getName() + "\t" + toString(alignSeqs[i].numIdentical) + "\t" + toString(0) + "\t" + alignSeqs[i].seq.getAligned() + "\n";
219 //try to merge it with all smaller seqs
220 for (int j = i+1; j < numSeqs; j++) {
222 if (pDataArray->m->control_pressed) { delete parser; return 0; }
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());
229 for (int k = 0; k < alignSeqs[i].seq.getAligned().length(); k++) {
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
235 if (mismatch <= pDataArray->diffs) {
237 alignSeqs[i].names += ',' + alignSeqs[j].names;
238 alignSeqs[i].numIdentical += alignSeqs[j].numIdentical;
240 alignSeqs[j].active = 0;
241 alignSeqs[j].numIdentical = 0;
242 alignSeqs[j].diffs = mismatch;
244 chunk += alignSeqs[j].seq.getName() + "\t" + toString(alignSeqs[j].numIdentical) + "\t" + toString(mismatch) + "\t" + alignSeqs[j].seq.getAligned() + "\n";
249 //remove from active list
250 alignSeqs[i].active = 0;
252 out << "ideal_seq_" << (i+1) << '\t' << alignSeqs[i].numIdentical << endl << chunk << endl;
255 if(i % 100 == 0) { pDataArray->m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); pDataArray->m->mothurOutEndLine(); }
258 if(numSeqs % 100 != 0) { pDataArray->m->mothurOut(toString(numSeqs) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); pDataArray->m->mothurOutEndLine(); }
259 ////////////////////////////////////////////////////
261 if (pDataArray->m->control_pressed) { delete parser; return 0; }
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();
266 ////////////////////////////////////////////////////
267 //printData(pDataArray->newFFile, pDataArray->newNFile); - same as below
271 pDataArray->m->openOutputFileAppend(pDataArray->newFName, outFasta);
272 pDataArray->m->openOutputFileAppend(pDataArray->newNName, outNames);
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;
283 ////////////////////////////////////////////////////
285 pDataArray->m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); pDataArray->m->mothurOutEndLine();
293 catch(exception& e) {
294 pDataArray->m->errorOut(e, "PreClusterCommand", "MyPreclusterThreadFunction");
300 /**************************************************************************************************/