1 #ifndef SHHHSEQSCOMMAND_H
2 #define SHHHSEQSCOMMAND_H
8 * Created by westcott on 11/8/11.
9 * Copyright 2011 Schloss Lab. All rights reserved.
13 #include "command.hpp"
14 #include "myseqdist.h"
16 #include "sequenceparser.h"
17 #include "deconvolutecommand.h"
18 #include "clustercommand.h"
20 //**********************************************************************************************************************
22 class ShhhSeqsCommand : public Command {
25 ShhhSeqsCommand(string);
29 vector<string> setParameters();
30 string getCommandName() { return "shhh.seqs"; }
31 string getCommandCategory() { return "Sequence Processing"; }
33 string getHelpString();
34 string getOutputPattern(string);
35 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.\nQuince C, Lanzen A, Davenport RJ, Turnbaugh PJ (2011). Removing noise from pyrosequenced amplicons. BMC Bioinformatics 12:38.\nhttp://www.mothur.org/wiki/Shhh.seqs"; }
36 string getDescription() { return "shhh.seqs"; }
40 void help() { m->mothurOut(getHelpString()); }
47 linePair(int i, int j) : start(i), end(j) {}
51 string outputDir, fastafile, namefile, groupfile;
54 vector<string> outputNames;
56 int readData(correctDist*, seqNoise&, vector<string>&, vector<string>&, vector<string>&, vector<int>&);
57 int loadData(correctDist*, seqNoise&, vector<string>&, vector<string>&, vector<string>&, vector<int>&, map<string, string>&, vector<Sequence>&);
59 int driver(seqNoise&, vector<string>&, vector<string>&, vector<string>&, vector<int>&, string, string, string, string);
60 vector<string> driverGroups(SequenceParser&, string, string, string, int, int, vector<string>);
61 vector<string> createProcessesGroups(SequenceParser&, string, string, string, vector<string>);
62 int deconvoluteResults(string, string);
68 /**************************************************************************************************/
69 //custom data structure for threads to use.
70 // This is passed by void pointer so it can be any data type
71 // that can be passed using a single void pointer (LPVOID).
76 string newFName, newNName, newMName;
81 vector<string> groups;
82 vector<string> mapfileNames;
85 shhhseqsData(string f, string n, string g, string nff, string nnf, string nmf, vector<string> gr, MothurOut* mout, int st, int en, int s, int tid) {
101 /**************************************************************************************************/
102 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
104 static DWORD WINAPI MyShhhSeqsThreadFunction(LPVOID lpParam){
105 shhhseqsData* pDataArray;
106 pDataArray = (shhhseqsData*)lpParam;
110 //parse fasta and name file by group
111 SequenceParser parser(pDataArray->groupfile, pDataArray->fastafile, pDataArray->namefile);
113 //precluster each group
114 for (int k = pDataArray->start; k < pDataArray->end; k++) {
116 int start = time(NULL);
118 if (pDataArray->m->control_pressed) { return 0; }
120 pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOut("Processing group " + pDataArray->groups[k] + ":"); pDataArray->m->mothurOutEndLine();
122 map<string, string> thisNameMap;
123 thisNameMap = parser.getNameMap(pDataArray->groups[k]);
124 vector<Sequence> thisSeqs = parser.getSeqs(pDataArray->groups[k]);
126 if (pDataArray->m->control_pressed) { return 0; }
128 vector<string> sequences;
129 vector<string> uniqueNames;
130 vector<string> redundantNames;
134 correctDist* correct = new correctDist(1); //we use one processor since we already split up the work load.
136 //load this groups info in order
137 //loadData(correct, noise, sequences, uniqueNames, redundantNames, seqFreq, thisNameMap, thisSeqs);
138 //////////////////////////////////////////////////////////////////////////////////////////////////
140 map<string, string>::iterator it;
142 for (int i = 0; i < thisSeqs.size(); i++) {
144 if (pDataArray->m->control_pressed) { return 0; }
146 if (thisSeqs[i].getName() != "") {
147 correct->addSeq(thisSeqs[i].getName(), thisSeqs[i].getAligned());
149 it = thisNameMap.find(thisSeqs[i].getName());
150 if (it != thisNameMap.end()) {
151 noise.addSeq(thisSeqs[i].getAligned(), sequences);
152 noise.addRedundantName(it->first, it->second, uniqueNames, redundantNames, seqFreq);
154 pDataArray->m->mothurOut("[ERROR]: " + thisSeqs[i].getName() + " is in your fasta file and not in your namefile, please correct.");
160 if (error) { return 0; }
161 //////////////////////////////////////////////////////////////////////////////////////////////////
163 if (pDataArray->m->control_pressed) { return 0; }
165 //calc distances for cluster
166 string distFileName = pDataArray->m->getRootName(pDataArray->m->getSimpleName(pDataArray->fastafile)) + pDataArray->groups[k] + ".shhh.dist";
167 correct->execute(distFileName);
170 if (pDataArray->m->control_pressed) { pDataArray->m->mothurRemove(distFileName); return 0; }
172 //driver(noise, sequences, uniqueNames, redundantNames, seqFreq, distFileName, newFFile+groups[i], newNFile+groups[i], newMFile+groups[i]+".map");
173 ///////////////////////////////////////////////////////////////////////////////////////////////////
174 double cutOff = 0.08;
177 double minDelta = 1e-6;
179 double maxDelta = 1e6;
180 int numSeqs = sequences.size();
182 //run cluster command
183 string inputString = "phylip=" + distFileName + ", method=furthest, cutoff=0.08";
184 pDataArray->m->mothurOut("/******************************************/"); pDataArray->m->mothurOutEndLine();
185 pDataArray->m->mothurOut("Running command: cluster(" + inputString + ")"); pDataArray->m->mothurOutEndLine();
187 Command* clusterCommand = new ClusterCommand(inputString);
188 clusterCommand->execute();
190 map<string, vector<string> > filenames = clusterCommand->getOutputFiles();
191 string listFileName = filenames["list"][0];
192 string rabundFileName = filenames["rabund"][0]; pDataArray->m->mothurRemove(rabundFileName);
193 string sabundFileName = filenames["sabund"][0]; pDataArray->m->mothurRemove(sabundFileName);
195 delete clusterCommand;
196 pDataArray->m->mothurOut("/******************************************/"); pDataArray->m->mothurOutEndLine();
198 if (pDataArray->m->control_pressed) { pDataArray->m->mothurRemove(distFileName); pDataArray->m->mothurRemove(listFileName); return 0; }
200 vector<double> distances(numSeqs * numSeqs);
201 noise.getDistanceData(distFileName, distances);
202 pDataArray->m->mothurRemove(distFileName);
203 if (pDataArray->m->control_pressed) { pDataArray->m->mothurRemove(listFileName); return 0; }
205 vector<int> otuData(numSeqs);
207 vector<vector<int> > otuBySeqLookUp;
208 noise.getListData(listFileName, cutOff, otuData, otuFreq, otuBySeqLookUp);
209 pDataArray->m->mothurRemove(listFileName);
210 if (pDataArray->m->control_pressed) { return 0; }
212 int numOTUs = otuFreq.size();
214 vector<double> weights(numOTUs, 0);
215 vector<int> change(numOTUs, 1);
216 vector<int> centroids(numOTUs, -1);
217 vector<int> cumCount(numOTUs, 0);
219 vector<double> tau(numSeqs, 1);
220 vector<int> anP(numSeqs, 0);
221 vector<int> anI(numSeqs, 0);
222 vector<int> anN(numSeqs, 0);
223 vector<vector<int> > aanI = otuBySeqLookUp;
225 while(numIters < minIter || ((maxDelta > minDelta) && (numIters < maxIter))){
227 if (pDataArray->m->control_pressed) { return 0; }
229 noise.updateOTUCountData(otuFreq, otuBySeqLookUp, aanI, anP, anI, cumCount); if (pDataArray->m->control_pressed) { return 0; }
230 maxDelta = noise.calcNewWeights(weights, seqFreq, anI, cumCount, anP, otuFreq, tau); if (pDataArray->m->control_pressed) { return 0; }
232 noise.calcCentroids(anI, anP, change, centroids, cumCount, distances, seqFreq, otuFreq, tau); if (pDataArray->m->control_pressed) { return 0; }
233 noise.checkCentroids(weights, centroids); if (pDataArray->m->control_pressed) { return 0; }
235 otuFreq.assign(numOTUs, 0);
239 for(int i=0;i<numSeqs;i++){
240 if (pDataArray->m->control_pressed) { return 0; }
243 double norm = 0.0000;
244 double minWeight = 0.1;
245 vector<double> currentTau(numOTUs);
247 for(int j=0;j<numOTUs;j++){
248 if (pDataArray->m->control_pressed) { return 0; }
249 if(weights[j] > minWeight && distances[i * numSeqs+centroids[j]] < offset){
250 offset = distances[i * numSeqs+centroids[j]];
254 for(int j=0;j<numOTUs;j++){
255 if (pDataArray->m->control_pressed) { return 0; }
256 if(weights[j] > minWeight){
257 currentTau[j] = exp(pDataArray->sigma * (-distances[(i * numSeqs + centroids[j])] + offset)) * weights[j];
258 norm += currentTau[j];
261 currentTau[j] = 0.0000;
265 for(int j=0;j<numOTUs;j++){
266 if (pDataArray->m->control_pressed) { return 0; }
267 currentTau[j] /= norm;
270 for(int j=0;j<numOTUs;j++){
271 if (pDataArray->m->control_pressed) { return 0; }
273 if(currentTau[j] > 1.0e-4){
274 int oldTotal = total;
277 tau.resize(oldTotal+1);
278 tau[oldTotal] = currentTau[j];
279 otuBySeqLookUp[j][otuFreq[j]] = oldTotal;
280 aanI[j][otuFreq[j]] = i;
293 noise.updateOTUCountData(otuFreq, otuBySeqLookUp, aanI, anP, anI, cumCount); if (pDataArray->m->control_pressed) { return 0; }
295 vector<double> percentage(numSeqs);
296 noise.setUpOTUData(otuData, percentage, cumCount, tau, otuFreq, anP, anI); if (pDataArray->m->control_pressed) { return 0; }
297 noise.finishOTUData(otuData, otuFreq, anP, anI, cumCount, otuBySeqLookUp, aanI, tau); if (pDataArray->m->control_pressed) { return 0; }
299 change.assign(numOTUs, 1);
300 noise.calcCentroids(anI, anP, change, centroids, cumCount, distances, seqFreq, otuFreq, tau); if (pDataArray->m->control_pressed) { return 0; }
303 vector<int> finalTau(numOTUs, 0);
304 for(int i=0;i<numSeqs;i++){
305 if (pDataArray->m->control_pressed) { return 0; }
306 finalTau[otuData[i]] += int(seqFreq[i]);
309 noise.writeOutput(pDataArray->newFName+pDataArray->groups[k], pDataArray->newNName+pDataArray->groups[k], pDataArray->newMName+pDataArray->groups[k]+".map", finalTau, centroids, otuData, sequences, uniqueNames, redundantNames, seqFreq, distances);
311 ///////////////////////////////////////////////////////////////////////////////////////////////////
313 if (pDataArray->m->control_pressed) { return 0; }
315 pDataArray->m->appendFiles(pDataArray->newFName+pDataArray->groups[k], pDataArray->newFName); pDataArray->m->mothurRemove(pDataArray->newFName+pDataArray->groups[k]);
316 pDataArray->m->appendFiles(pDataArray->newNName+pDataArray->groups[k], pDataArray->newNName); pDataArray->m->mothurRemove(pDataArray->newNName+pDataArray->groups[k]);
317 pDataArray->mapfileNames.push_back(pDataArray->newMName+pDataArray->groups[k]+".map");
319 pDataArray->m->mothurOut("It took " + toString(time(NULL) - start) + " secs to process group " + pDataArray->groups[k] + "."); pDataArray->m->mothurOutEndLine();
326 catch(exception& e) {
327 pDataArray->m->errorOut(e, "ShhhSeqsCommand", "MyShhhSeqsThreadFunction");
332 /**************************************************************************************************/