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"; }
32 string getHelpString();
33 string getCitation() { return "http://www.mothur.org/wiki/Shhh.seqs"; }
34 string getDescription() { return "shhh.seqs"; }
38 void help() { m->mothurOut(getHelpString()); }
45 linePair(int i, int j) : start(i), end(j) {}
49 string outputDir, fastafile, namefile, groupfile;
52 vector<string> outputNames;
54 int readData(correctDist*, seqNoise&, vector<string>&, vector<string>&, vector<string>&, vector<int>&);
55 int loadData(correctDist*, seqNoise&, vector<string>&, vector<string>&, vector<string>&, vector<int>&, map<string, string>&, vector<Sequence>&);
57 int driver(seqNoise&, vector<string>&, vector<string>&, vector<string>&, vector<int>&, string, string, string, string);
58 int driverGroups(SequenceParser&, string, string, string, int, int, vector<string>);
59 int createProcessesGroups(SequenceParser&, string, string, string, vector<string>);
60 int deconvoluteResults(string, string);
66 /**************************************************************************************************/
67 //custom data structure for threads to use.
68 // This is passed by void pointer so it can be any data type
69 // that can be passed using a single void pointer (LPVOID).
74 string newFName, newNName, newMName;
79 vector<string> groups;
82 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) {
98 /**************************************************************************************************/
99 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
101 static DWORD WINAPI MyShhhSeqsThreadFunction(LPVOID lpParam){
102 shhhseqsData* pDataArray;
103 pDataArray = (shhhseqsData*)lpParam;
107 //parse fasta and name file by group
108 SequenceParser parser(pDataArray->groupfile, pDataArray->fastafile, pDataArray->namefile);
110 //precluster each group
111 for (int k = pDataArray->start; k < pDataArray->end; k++) {
113 int start = time(NULL);
115 if (pDataArray->m->control_pressed) { return 0; }
117 pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOut("Processing group " + pDataArray->groups[k] + ":"); pDataArray->m->mothurOutEndLine();
119 map<string, string> thisNameMap;
120 thisNameMap = parser.getNameMap(pDataArray->groups[k]);
121 vector<Sequence> thisSeqs = parser.getSeqs(pDataArray->groups[k]);
123 if (pDataArray->m->control_pressed) { return 0; }
125 vector<string> sequences;
126 vector<string> uniqueNames;
127 vector<string> redundantNames;
131 correctDist* correct = new correctDist(1); //we use one processor since we already split up the work load.
133 //load this groups info in order
134 //loadData(correct, noise, sequences, uniqueNames, redundantNames, seqFreq, thisNameMap, thisSeqs);
135 //////////////////////////////////////////////////////////////////////////////////////////////////
137 map<string, string>::iterator it;
139 for (int i = 0; i < thisSeqs.size(); i++) {
141 if (pDataArray->m->control_pressed) { return 0; }
143 if (thisSeqs[i].getName() != "") {
144 correct->addSeq(thisSeqs[i].getName(), thisSeqs[i].getAligned());
146 it = thisNameMap.find(thisSeqs[i].getName());
147 if (it != thisNameMap.end()) {
148 noise.addSeq(thisSeqs[i].getAligned(), sequences);
149 noise.addRedundantName(it->first, it->second, uniqueNames, redundantNames, seqFreq);
151 pDataArray->m->mothurOut("[ERROR]: " + thisSeqs[i].getName() + " is in your fasta file and not in your namefile, please correct.");
157 if (error) { return 0; }
158 //////////////////////////////////////////////////////////////////////////////////////////////////
160 if (pDataArray->m->control_pressed) { return 0; }
162 //calc distances for cluster
163 string distFileName = pDataArray->m->getRootName(pDataArray->m->getSimpleName(pDataArray->fastafile)) + pDataArray->groups[k] + ".shhh.dist";
164 correct->execute(distFileName);
167 if (pDataArray->m->control_pressed) { pDataArray->m->mothurRemove(distFileName); return 0; }
169 //driver(noise, sequences, uniqueNames, redundantNames, seqFreq, distFileName, newFFile+groups[i], newNFile+groups[i], newMFile+groups[i]+".map");
170 ///////////////////////////////////////////////////////////////////////////////////////////////////
171 double cutOff = 0.08;
174 double minDelta = 1e-6;
176 double maxDelta = 1e6;
177 int numSeqs = sequences.size();
179 //run cluster command
180 string inputString = "phylip=" + distFileName + ", method=furthest, cutoff=0.08";
181 pDataArray->m->mothurOut("/******************************************/"); pDataArray->m->mothurOutEndLine();
182 pDataArray->m->mothurOut("Running command: cluster(" + inputString + ")"); pDataArray->m->mothurOutEndLine();
184 Command* clusterCommand = new ClusterCommand(inputString);
185 clusterCommand->execute();
187 map<string, vector<string> > filenames = clusterCommand->getOutputFiles();
188 string listFileName = filenames["list"][0];
189 string rabundFileName = filenames["rabund"][0]; pDataArray->m->mothurRemove(rabundFileName);
190 string sabundFileName = filenames["sabund"][0]; pDataArray->m->mothurRemove(sabundFileName);
192 delete clusterCommand;
193 pDataArray->m->mothurOut("/******************************************/"); pDataArray->m->mothurOutEndLine();
195 if (pDataArray->m->control_pressed) { pDataArray->m->mothurRemove(distFileName); pDataArray->m->mothurRemove(listFileName); return 0; }
197 vector<double> distances(numSeqs * numSeqs);
198 noise.getDistanceData(distFileName, distances);
199 pDataArray->m->mothurRemove(distFileName);
200 if (pDataArray->m->control_pressed) { pDataArray->m->mothurRemove(listFileName); return 0; }
202 vector<int> otuData(numSeqs);
204 vector<vector<int> > otuBySeqLookUp;
205 noise.getListData(listFileName, cutOff, otuData, otuFreq, otuBySeqLookUp);
206 pDataArray->m->mothurRemove(listFileName);
207 if (pDataArray->m->control_pressed) { return 0; }
209 int numOTUs = otuFreq.size();
211 vector<double> weights(numOTUs, 0);
212 vector<int> change(numOTUs, 1);
213 vector<int> centroids(numOTUs, -1);
214 vector<int> cumCount(numOTUs, 0);
216 vector<double> tau(numSeqs, 1);
217 vector<int> anP(numSeqs, 0);
218 vector<int> anI(numSeqs, 0);
219 vector<int> anN(numSeqs, 0);
220 vector<vector<int> > aanI = otuBySeqLookUp;
222 while(numIters < minIter || ((maxDelta > minDelta) && (numIters < maxIter))){
224 if (pDataArray->m->control_pressed) { return 0; }
226 noise.updateOTUCountData(otuFreq, otuBySeqLookUp, aanI, anP, anI, cumCount); if (pDataArray->m->control_pressed) { return 0; }
227 maxDelta = noise.calcNewWeights(weights, seqFreq, anI, cumCount, anP, otuFreq, tau); if (pDataArray->m->control_pressed) { return 0; }
229 noise.calcCentroids(anI, anP, change, centroids, cumCount, distances, seqFreq, otuFreq, tau); if (pDataArray->m->control_pressed) { return 0; }
230 noise.checkCentroids(weights, centroids); if (pDataArray->m->control_pressed) { return 0; }
232 otuFreq.assign(numOTUs, 0);
236 for(int i=0;i<numSeqs;i++){
237 if (pDataArray->m->control_pressed) { return 0; }
240 double norm = 0.0000;
241 double minWeight = 0.1;
242 vector<double> currentTau(numOTUs);
244 for(int j=0;j<numOTUs;j++){
245 if (pDataArray->m->control_pressed) { return 0; }
246 if(weights[j] > minWeight && distances[i * numSeqs+centroids[j]] < offset){
247 offset = distances[i * numSeqs+centroids[j]];
251 for(int j=0;j<numOTUs;j++){
252 if (pDataArray->m->control_pressed) { return 0; }
253 if(weights[j] > minWeight){
254 currentTau[j] = exp(pDataArray->sigma * (-distances[(i * numSeqs + centroids[j])] + offset)) * weights[j];
255 norm += currentTau[j];
258 currentTau[j] = 0.0000;
262 for(int j=0;j<numOTUs;j++){
263 if (pDataArray->m->control_pressed) { return 0; }
264 currentTau[j] /= norm;
267 for(int j=0;j<numOTUs;j++){
268 if (pDataArray->m->control_pressed) { return 0; }
270 if(currentTau[j] > 1.0e-4){
271 int oldTotal = total;
274 tau.resize(oldTotal+1);
275 tau[oldTotal] = currentTau[j];
276 otuBySeqLookUp[j][otuFreq[j]] = oldTotal;
277 aanI[j][otuFreq[j]] = i;
290 noise.updateOTUCountData(otuFreq, otuBySeqLookUp, aanI, anP, anI, cumCount); if (pDataArray->m->control_pressed) { return 0; }
292 vector<double> percentage(numSeqs);
293 noise.setUpOTUData(otuData, percentage, cumCount, tau, otuFreq, anP, anI); if (pDataArray->m->control_pressed) { return 0; }
294 noise.finishOTUData(otuData, otuFreq, anP, anI, cumCount, otuBySeqLookUp, aanI, tau); if (pDataArray->m->control_pressed) { return 0; }
296 change.assign(numOTUs, 1);
297 noise.calcCentroids(anI, anP, change, centroids, cumCount, distances, seqFreq, otuFreq, tau); if (pDataArray->m->control_pressed) { return 0; }
300 vector<int> finalTau(numOTUs, 0);
301 for(int i=0;i<numSeqs;i++){
302 if (pDataArray->m->control_pressed) { return 0; }
303 finalTau[otuData[i]] += int(seqFreq[i]);
306 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);
308 ///////////////////////////////////////////////////////////////////////////////////////////////////
310 if (pDataArray->m->control_pressed) { return 0; }
312 pDataArray->m->appendFiles(pDataArray->newFName+pDataArray->groups[k], pDataArray->newFName); pDataArray->m->mothurRemove(pDataArray->newFName+pDataArray->groups[k]);
313 pDataArray->m->appendFiles(pDataArray->newNName+pDataArray->groups[k], pDataArray->newNName); pDataArray->m->mothurRemove(pDataArray->newNName+pDataArray->groups[k]);
315 pDataArray->m->mothurOut("It took " + toString(time(NULL) - start) + " secs to process group " + pDataArray->groups[k] + "."); pDataArray->m->mothurOutEndLine();
324 catch(exception& e) {
325 pDataArray->m->errorOut(e, "PreClusterCommand", "MyPreclusterThreadFunction");
330 /**************************************************************************************************/