File: shhhseqscommand.h

package info (click to toggle)
mothur 1.33.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,248 kB
  • ctags: 12,231
  • sloc: cpp: 152,046; fortran: 665; makefile: 74; sh: 34
file content (337 lines) | stat: -rw-r--r-- 12,465 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#ifndef SHHHSEQSCOMMAND_H
#define SHHHSEQSCOMMAND_H

/*
 *  shhhseqscommand.h
 *  Mothur
 *
 *  Created by westcott on 11/8/11.
 *  Copyright 2011 Schloss Lab. All rights reserved.
 *
 */

#include "command.hpp"
#include "myseqdist.h"
#include "seqnoise.h"
#include "sequenceparser.h"
#include "deconvolutecommand.h"
#include "clustercommand.h"

//**********************************************************************************************************************

class ShhhSeqsCommand : public Command {
	
public:
	ShhhSeqsCommand(string);
	ShhhSeqsCommand();
	~ShhhSeqsCommand() {}
	
	vector<string> setParameters();
	string getCommandName()			{ return "shhh.seqs";	}
	string getCommandCategory()		{ return "Sequence Processing";		}
	
	string getHelpString();	
    string getOutputPattern(string);	
	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"; }
	string getDescription()		{ return "shhh.seqs"; }
	
	
	int execute(); 
	void help() { m->mothurOut(getHelpString()); }		
	
private:
	
	struct linePair {
		int start;
		int end;
		linePair(int i, int j) : start(i), end(j) {}
	};
	
	bool abort;
	string outputDir, fastafile, namefile, groupfile;
	int processors;
	double sigma;
	vector<string> outputNames;
	
	int readData(correctDist*, seqNoise&, vector<string>&, vector<string>&, vector<string>&, vector<int>&);
	int loadData(correctDist*, seqNoise&, vector<string>&, vector<string>&, vector<string>&, vector<int>&, map<string, string>&, vector<Sequence>&);

	int driver(seqNoise&, vector<string>&, vector<string>&, vector<string>&, vector<int>&, string, string, string, string);
	vector<string> driverGroups(SequenceParser&, string, string, string, int, int, vector<string>);
	vector<string> createProcessesGroups(SequenceParser&, string, string, string, vector<string>);
	int deconvoluteResults(string, string);



};

/**************************************************************************************************/
//custom data structure for threads to use.
// This is passed by void pointer so it can be any data type
// that can be passed using a single void pointer (LPVOID).
struct shhhseqsData {
	string fastafile; 
	string namefile; 
	string groupfile;
	string newFName, newNName, newMName;
	MothurOut* m;
	int start;
	int end;
	int sigma, threadID, count;
	vector<string> groups;
	vector<string> mapfileNames;
	
	shhhseqsData(){}
	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) {
		fastafile = f;
		namefile = n;
		groupfile = g;
		newFName = nff;
		newNName = nnf;
		newMName = nmf;
		m = mout;
		start = st;
		end = en;
		sigma = s;
		threadID = tid;
		groups = gr;
        count=0;
	}
};

/**************************************************************************************************/
#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
#else
static DWORD WINAPI MyShhhSeqsThreadFunction(LPVOID lpParam){ 
	shhhseqsData* pDataArray;
	pDataArray = (shhhseqsData*)lpParam;
	
	try {
		
		//parse fasta and name file by group
		SequenceParser parser(pDataArray->groupfile, pDataArray->fastafile, pDataArray->namefile);
						
		//precluster each group
		for (int k = pDataArray->start; k < pDataArray->end; k++) {
			
            pDataArray->count++;
            
			int start = time(NULL);
			
			if (pDataArray->m->control_pressed) {  return 0; }
			
			pDataArray->m->mothurOutEndLine(); pDataArray->m->mothurOut("Processing group " + pDataArray->groups[k] + ":"); pDataArray->m->mothurOutEndLine();

			map<string, string> thisNameMap;
			thisNameMap = parser.getNameMap(pDataArray->groups[k]); 
			vector<Sequence> thisSeqs = parser.getSeqs(pDataArray->groups[k]);
			
			if (pDataArray->m->control_pressed) {  return 0; }
			
			vector<string> sequences;
			vector<string> uniqueNames;
			vector<string> redundantNames;
			vector<int> seqFreq;
			
			seqNoise noise;
			correctDist* correct = new correctDist(1); //we use one processor since we already split up the work load.
			
			//load this groups info in order
			//loadData(correct, noise, sequences, uniqueNames, redundantNames, seqFreq, thisNameMap, thisSeqs);
			//////////////////////////////////////////////////////////////////////////////////////////////////
			bool error = false;
			map<string, string>::iterator it;
			
			for (int i = 0; i < thisSeqs.size(); i++) {
				
				if (pDataArray->m->control_pressed) { return 0; }
				
				if (thisSeqs[i].getName() != "") {
					correct->addSeq(thisSeqs[i].getName(), thisSeqs[i].getAligned());
					
					it = thisNameMap.find(thisSeqs[i].getName());
					if (it != thisNameMap.end()) {
						noise.addSeq(thisSeqs[i].getAligned(), sequences);
						noise.addRedundantName(it->first, it->second, uniqueNames, redundantNames, seqFreq);
					}else {
						pDataArray->m->mothurOut("[ERROR]: " + thisSeqs[i].getName() + " is in your fasta file and not in your namefile, please correct.");
						error = true;
					}
				}
			}
			
			if (error) { return 0; }
			//////////////////////////////////////////////////////////////////////////////////////////////////
			
			if (pDataArray->m->control_pressed) { return 0; }
			
			//calc distances for cluster
			string distFileName = pDataArray->m->getRootName(pDataArray->m->getSimpleName(pDataArray->fastafile)) + pDataArray->groups[k] + ".shhh.dist";
			correct->execute(distFileName);
			delete correct;
			
			if (pDataArray->m->control_pressed) { pDataArray->m->mothurRemove(distFileName); return 0; }
			
			//driver(noise, sequences, uniqueNames, redundantNames, seqFreq, distFileName, newFFile+groups[i], newNFile+groups[i], newMFile+groups[i]+".map"); 
			///////////////////////////////////////////////////////////////////////////////////////////////////
			double cutOff = 0.08;
			int minIter = 10;
			int maxIter = 1000;
			double minDelta = 1e-6;
			int numIters = 0;
			double maxDelta = 1e6;
			int numSeqs = sequences.size();
			
			//run cluster command
			string inputString = "phylip=" + distFileName + ", method=furthest, cutoff=0.08";
			pDataArray->m->mothurOut("/******************************************/"); pDataArray->m->mothurOutEndLine(); 
			pDataArray->m->mothurOut("Running command: cluster(" + inputString + ")"); pDataArray->m->mothurOutEndLine(); 
			
			Command* clusterCommand = new ClusterCommand(inputString);
			clusterCommand->execute();
			
			map<string, vector<string> > filenames = clusterCommand->getOutputFiles();
			string listFileName = filenames["list"][0];
			string rabundFileName = filenames["rabund"][0]; pDataArray->m->mothurRemove(rabundFileName);
			string sabundFileName = filenames["sabund"][0]; pDataArray->m->mothurRemove(sabundFileName);
			
			delete clusterCommand;
			pDataArray->m->mothurOut("/******************************************/"); pDataArray->m->mothurOutEndLine(); 
			
			if (pDataArray->m->control_pressed) { pDataArray->m->mothurRemove(distFileName); pDataArray->m->mothurRemove(listFileName); return 0; }
			
			vector<double> distances(numSeqs * numSeqs);
			noise.getDistanceData(distFileName, distances);
			pDataArray->m->mothurRemove(distFileName); 
			if (pDataArray->m->control_pressed) { pDataArray->m->mothurRemove(listFileName); return 0; }
			
			vector<int> otuData(numSeqs);
			vector<int> otuFreq;
			vector<vector<int> > otuBySeqLookUp;
			noise.getListData(listFileName, cutOff, otuData, otuFreq, otuBySeqLookUp);
			pDataArray->m->mothurRemove(listFileName);
			if (pDataArray->m->control_pressed) { return 0; }
			
			int numOTUs = otuFreq.size();
			
			vector<double> weights(numOTUs, 0);
			vector<int> change(numOTUs, 1);
			vector<int> centroids(numOTUs, -1);
			vector<int> cumCount(numOTUs, 0);
			
			vector<double> tau(numSeqs, 1);
			vector<int> anP(numSeqs, 0);
			vector<int> anI(numSeqs, 0);
			vector<int> anN(numSeqs, 0);
			vector<vector<int> > aanI = otuBySeqLookUp;
			
			while(numIters < minIter || ((maxDelta > minDelta) && (numIters < maxIter))){
				
				if (pDataArray->m->control_pressed) { return 0; }
				
				noise.updateOTUCountData(otuFreq, otuBySeqLookUp, aanI, anP, anI, cumCount); if (pDataArray->m->control_pressed) { return 0; }
				maxDelta = noise.calcNewWeights(weights, seqFreq, anI, cumCount, anP, otuFreq, tau);  if (pDataArray->m->control_pressed) { return 0; }
				
				noise.calcCentroids(anI, anP, change, centroids, cumCount, distances, seqFreq, otuFreq, tau); if (pDataArray->m->control_pressed) { return 0; }
				noise.checkCentroids(weights, centroids); if (pDataArray->m->control_pressed) { return 0; }
				
				otuFreq.assign(numOTUs, 0);
				
				int total = 0;
				
				for(int i=0;i<numSeqs;i++){
					if (pDataArray->m->control_pressed) { return 0; }
					
					double offset = 1e6;
					double norm = 0.0000;
					double minWeight = 0.1;
					vector<double> currentTau(numOTUs);
					
					for(int j=0;j<numOTUs;j++){
						if (pDataArray->m->control_pressed) { return 0; }
						if(weights[j] > minWeight && distances[i * numSeqs+centroids[j]] < offset){
							offset = distances[i * numSeqs+centroids[j]];
						}
					}
					
					for(int j=0;j<numOTUs;j++){
						if (pDataArray->m->control_pressed) { return 0; }
						if(weights[j] > minWeight){
							currentTau[j] = exp(pDataArray->sigma * (-distances[(i * numSeqs + centroids[j])] + offset)) * weights[j];
							norm += currentTau[j];
						}
						else{
							currentTau[j] = 0.0000;
						}
					}			
					
					for(int j=0;j<numOTUs;j++){
						if (pDataArray->m->control_pressed) { return 0; }
						currentTau[j] /= norm;
					}
					
					for(int j=0;j<numOTUs;j++){
						if (pDataArray->m->control_pressed) { return 0; }
						
						if(currentTau[j] > 1.0e-4){
							int oldTotal = total;
							total++;
							
							tau.resize(oldTotal+1);
							tau[oldTotal] = currentTau[j];
							otuBySeqLookUp[j][otuFreq[j]] = oldTotal;
							aanI[j][otuFreq[j]] = i;
							otuFreq[j]++;
							
						}
					}
					
					anP.resize(total);
					anI.resize(total);
				}
				
				numIters++;
			}
			
			noise.updateOTUCountData(otuFreq, otuBySeqLookUp, aanI, anP, anI, cumCount);  if (pDataArray->m->control_pressed) { return 0; }
			
			vector<double> percentage(numSeqs);
			noise.setUpOTUData(otuData, percentage, cumCount, tau, otuFreq, anP, anI);  if (pDataArray->m->control_pressed) { return 0; }
			noise.finishOTUData(otuData, otuFreq, anP, anI, cumCount, otuBySeqLookUp, aanI, tau);  if (pDataArray->m->control_pressed) { return 0; }
			
			change.assign(numOTUs, 1);
			noise.calcCentroids(anI, anP, change, centroids, cumCount, distances, seqFreq, otuFreq, tau); if (pDataArray->m->control_pressed) { return 0; }
			
			
			vector<int> finalTau(numOTUs, 0);
			for(int i=0;i<numSeqs;i++){
				if (pDataArray->m->control_pressed) { return 0; }
				finalTau[otuData[i]] += int(seqFreq[i]);
			}
			
			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);
			
			///////////////////////////////////////////////////////////////////////////////////////////////////
			
			if (pDataArray->m->control_pressed) { return 0; }
			
			pDataArray->m->appendFiles(pDataArray->newFName+pDataArray->groups[k], pDataArray->newFName); pDataArray->m->mothurRemove(pDataArray->newFName+pDataArray->groups[k]);
			pDataArray->m->appendFiles(pDataArray->newNName+pDataArray->groups[k], pDataArray->newNName); pDataArray->m->mothurRemove(pDataArray->newNName+pDataArray->groups[k]);
			pDataArray->mapfileNames.push_back(pDataArray->newMName+pDataArray->groups[k]+".map");
			
			pDataArray->m->mothurOut("It took " + toString(time(NULL) - start) + " secs to process group " + pDataArray->groups[k] + "."); pDataArray->m->mothurOutEndLine(); 
		}
		
		return 0;
		
		
	}
	catch(exception& e) {
		pDataArray->m->errorOut(e, "ShhhSeqsCommand", "MyShhhSeqsThreadFunction");
		exit(1);
	}
} 
#endif
/**************************************************************************************************/

#endif