1 #ifndef PAIREDENDMODEL_H_
2 #define PAIREDENDMODEL_H_
13 #include "Orientation.h"
17 #include "NoiseProfile.h"
19 #include "ModelParams.h"
22 #include "SingleRead.h"
23 #include "PairedEndRead.h"
24 #include "PairedEndHit.h"
25 #include "ReadReader.h"
29 class PairedEndModel {
31 PairedEndModel(Refs* refs = NULL) {
33 M = (refs != NULL ? refs->getM() : 0);
34 memset(N, 0, sizeof(N));
36 needCalcConPrb = true;
38 ori = new Orientation();
40 rspd = new RSPD(estRSPD);
42 npro = new NoiseProfile();
49 //If it is not a master node, only init & update can be used!
50 PairedEndModel(ModelParams& params, bool isMaster = true) {
52 memcpy(N, params.N, sizeof(params.N));
54 estRSPD = params.estRSPD;
55 seedLen = params.seedLen;
56 needCalcConPrb = true;
58 ori = NULL; gld = NULL; rspd = NULL; pro = NULL; npro = NULL; mld = NULL;
62 if (!estRSPD) rspd = new RSPD(estRSPD);
63 mld = new LenDist(params.mate_minL, params.mate_maxL);
66 ori = new Orientation(params.probF);
67 gld = new LenDist(params.minL, params.maxL);
68 if (estRSPD) rspd = new RSPD(estRSPD, params.B);
69 pro = new Profile(params.maxL);
70 npro = new NoiseProfile();
75 if (ori != NULL) delete ori;
76 if (gld != NULL) delete gld;
77 if (rspd != NULL) delete rspd;
78 if (pro != NULL) delete pro;
79 if (npro != NULL) delete npro;
80 if (mld != NULL) delete mld;
81 if (mw != NULL) delete mw;
84 void estimateFromReads(const char*);
86 //if prob is too small, just make it 0
87 double getConPrb(const PairedEndRead& read, const PairedEndHit& hit) {
88 if (read.isLowQuality()) return 0.0;
91 int sid = hit.getSid();
92 RefSeq &ref = refs->getRef(sid);
93 int dir = hit.getDir();
94 int pos = hit.getPos();
95 int fullLen = ref.getFullLen();
96 int totLen = ref.getTotLen();
97 int insertLen = hit.getInsertL();
99 int fpos = (dir == 0 ? pos : totLen - pos - insertLen); // the aligned position reported in SAM file, should be a coordinate in forward strand
100 int effL = std::min(fullLen, totLen - insertLen + 1);
102 assert(fpos >= 0 && fpos + insertLen <= totLen && insertLen <= totLen);
103 if (fpos >= fullLen || ref.getMask(fpos)) return 0.0; // For paired-end model, fpos is the seedPos
105 prob = ori->getProb(dir) * gld->getAdjustedProb(insertLen, totLen) *
106 rspd->getAdjustedProb(fpos, effL, fullLen);
108 const SingleRead& mate1 = read.getMate1();
109 prob *= mld->getAdjustedProb(mate1.getReadLength(), insertLen) *
110 pro->getProb(mate1.getReadSeq(), ref, pos, dir);
112 const SingleRead& mate2 = read.getMate2();
113 int m2pos = totLen - pos - insertLen;
115 prob *= mld->getAdjustedProb(mate2.getReadLength(), insertLen) *
116 pro->getProb(mate2.getReadSeq(), ref, m2pos, m2dir);
118 if (prob < EPSILON) { prob = 0.0; }
120 prob = (mw[sid] < EPSILON ? 0.0 : prob / mw[sid]);
125 double getNoiseConPrb(const PairedEndRead& read) {
126 if (read.isLowQuality()) return 0.0;
128 const SingleRead& mate1 = read.getMate1();
129 const SingleRead& mate2 = read.getMate2();
131 prob = mld->getProb(mate1.getReadLength()) * npro->getProb(mate1.getReadSeq());
132 prob *= mld->getProb(mate2.getReadLength()) * npro->getProb(mate2.getReadSeq());
134 if (prob < EPSILON) { prob = 0.0; }
136 prob = (mw[0] < EPSILON ? 0.0: prob / mw[0]);
141 double getLogP() { return npro->getLogP(); }
145 void update(const PairedEndRead& read, const PairedEndHit& hit, double frac) {
146 if (read.isLowQuality() || frac < EPSILON) return;
148 RefSeq& ref = refs->getRef(hit.getSid());
149 const SingleRead& mate1 = read.getMate1();
150 const SingleRead& mate2 = read.getMate2();
152 gld->update(hit.getInsertL(), frac);
154 int fpos = (hit.getDir() == 0 ? hit.getPos() : ref.getTotLen() - hit.getPos() - hit.getInsertL());
155 rspd->update(fpos, ref.getFullLen(), frac);
157 pro->update(mate1.getReadSeq(), ref, hit.getPos(), hit.getDir(), frac);
159 int m2pos = ref.getTotLen() - hit.getPos() - hit.getInsertL();
160 int m2dir = !hit.getDir();
161 pro->update(mate2.getReadSeq(), ref, m2pos, m2dir, frac);
164 void updateNoise(const PairedEndRead& read, double frac) {
165 if (read.isLowQuality() || frac < EPSILON) return;
167 const SingleRead& mate1 = read.getMate1();
168 const SingleRead& mate2 = read.getMate2();
170 npro->update(mate1.getReadSeq(), frac);
171 npro->update(mate2.getReadSeq(), frac);
176 void collect(const PairedEndModel&);
178 bool getNeedCalcConPrb() { return needCalcConPrb; }
179 void setNeedCalcConPrb(bool value) { needCalcConPrb = value; }
181 void read(const char*);
182 void write(const char*);
184 const LenDist& getGLD() { return *gld; }
186 void startSimulation(simul*, double*);
187 bool simulate(int, PairedEndRead&, int&);
188 void finishSimulation();
190 //Use it after function 'read' or 'estimateFromReads'
196 int getModelType() const { return model_type; }
199 static const int model_type = 2;
200 static const int read_type = 2;
208 bool needCalcConPrb; //true need, false does not need
211 LenDist *gld, *mld; //mld1 mate_length_dist
216 simul *sampler; // for simulation
217 double *theta_cdf; // for simulation
219 double *mw; // for masking
224 void PairedEndModel::estimateFromReads(const char* readFN) {
226 char readFs[2][STRLEN];
230 for (int i = 0; i < 3; i++)
232 genReadFileNames(readFN, i, read_type, s, readFs);
233 ReadReader<PairedEndRead> reader(s, readFs, refs->hasPolyA(), seedLen); // allow calculation of calc_lq() function
236 while (reader.next(read)) {
237 SingleRead mate1 = read.getMate1();
238 SingleRead mate2 = read.getMate2();
240 if (!read.isLowQuality()) {
241 mld->update(mate1.getReadLength(), 1.0);
242 mld->update(mate2.getReadLength(), 1.0);
245 npro->updateC(mate1.getReadSeq());
246 npro->updateC(mate2.getReadSeq());
249 else if (verbose && (mate1.getReadLength() < seedLen || mate2.getReadLength() < seedLen)) {
250 printf("Warning: Read %s is ignored due to at least one of the mates' length < seed length %d!\n", read.getName().c_str(), seedLen);
254 if (verbose && cnt % 1000000 == 0) { printf("%d READS PROCESSED\n", cnt); }
257 if (verbose) { printf("estimateFromReads, N%d finished.\n", i); }
261 npro->calcInitParams();
263 mw = new double[M + 1];
267 void PairedEndModel::init() {
269 if (estRSPD) rspd->init();
274 void PairedEndModel::finish() {
276 if (estRSPD) rspd->finish();
279 needCalcConPrb = true;
283 void PairedEndModel::collect(const PairedEndModel& o) {
284 gld->collect(*(o.gld));
285 if (estRSPD) rspd->collect(*(o.rspd));
286 pro->collect(*(o.pro));
287 npro->collect(*(o.npro));
290 //Only master node can call
291 void PairedEndModel::read(const char* inpF) {
293 FILE *fi = fopen(inpF, "r");
294 if (fi == NULL) { fprintf(stderr, "Cannot open %s! It may not exist.\n", inpF); exit(-1); }
296 assert(fscanf(fi, "%d", &val) == 1);
297 assert(val == model_type);
306 if (fscanf(fi, "%d", &val) == 1) {
309 mw = new double[M + 1];
310 for (int i = 0; i <= M; i++) assert(fscanf(fi, "%lf", &mw[i]) == 1);
317 //Only master node can call. Only be called at EM.cpp
318 void PairedEndModel::write(const char* outF) {
319 FILE *fo = fopen(outF, "w");
321 fprintf(fo, "%d\n", model_type);
324 ori->write(fo); fprintf(fo, "\n");
325 gld->write(fo); fprintf(fo, "\n");
326 mld->write(fo); fprintf(fo, "\n");
327 rspd->write(fo); fprintf(fo, "\n");
328 pro->write(fo); fprintf(fo, "\n");
332 fprintf(fo, "\n%d\n", M);
333 for (int i = 0; i < M; i++) {
334 fprintf(fo, "%.15g ", mw[i]);
336 fprintf(fo, "%.15g\n", mw[M]);
342 void PairedEndModel::startSimulation(simul* sampler, double* theta) {
343 this->sampler = sampler;
345 theta_cdf = new double[M + 1];
346 for (int i = 0; i <= M; i++) {
347 theta_cdf[i] = theta[i];
348 if (i > 0) theta_cdf[i] += theta_cdf[i - 1];
351 rspd->startSimulation(M, refs);
352 pro->startSimulation();
353 npro->startSimulation();
356 bool PairedEndModel::simulate(int rid, PairedEndRead& read, int& sid) {
358 int insertL, mateL1, mateL2;
360 std::string readseq1, readseq2;
361 std::ostringstream strout;
363 sid = sampler->sample(theta_cdf, M + 1);
366 dir = pos = insertL = 0;
367 mateL1 = mld->simulate(sampler, -1);
368 readseq1 = npro->simulate(sampler, mateL1);
370 mateL2 = mld->simulate(sampler, -1);
371 readseq2 = npro->simulate(sampler, mateL2);
374 RefSeq &ref = refs->getRef(sid);
375 dir = ori->simulate(sampler);
376 insertL = gld->simulate(sampler, ref.getTotLen());
377 if (insertL < 0) return false;
378 int effL = std::min(ref.getFullLen(), ref.getTotLen() - insertL + 1);
379 pos = rspd->simulate(sampler, sid, effL);
380 if (pos < 0) return false;
381 if (dir > 0) pos = ref.getTotLen() - pos - insertL;
383 mateL1 = mld->simulate(sampler, insertL);
384 readseq1 = pro->simulate(sampler, mateL1, pos, dir, ref);
386 int m2pos = ref.getTotLen() - pos - insertL;
389 mateL2 = mld->simulate(sampler, insertL);
390 readseq2 = pro->simulate(sampler, mateL2, m2pos, m2dir, ref);
393 strout<<rid<<"_"<<dir<<"_"<<sid<<"_"<<pos<<"_"<<insertL;
396 read = PairedEndRead(SingleRead(name + "/1", readseq1), SingleRead(name + "/2", readseq2));
401 void PairedEndModel::finishSimulation() {
404 rspd->finishSimulation();
405 pro->finishSimulation();
406 npro->finishSimulation();
409 void PairedEndModel::calcMW() {
410 assert(mld->getMinL() >= seedLen);
412 memset(mw, 0, sizeof(double) * (M + 1));
415 for (int i = 1; i <= M; i++) {
416 RefSeq& ref = refs->getRef(i);
417 int totLen = ref.getTotLen();
418 int fullLen = ref.getFullLen();
419 int end = std::min(fullLen, totLen - gld->getMinL() + 1);
424 //seedPos is fpos here
425 for (int seedPos = 0; seedPos < end; seedPos++)
426 if (ref.getMask(seedPos)) {
427 minL = gld->getMinL();
428 maxL = std::min(gld->getMaxL(), totLen - seedPos);
430 for (int fragLen = minL; fragLen <= maxL; fragLen++) {
431 effL = std::min(fullLen, totLen - fragLen + 1);
432 value += gld->getAdjustedProb(fragLen, totLen) * rspd->getAdjustedProb(pfpos, effL, fullLen);
439 //fprintf(stderr, "Warning: %dth reference sequence is masked for almost all positions!\n", i);
445 #endif /* PAIREDENDMODEL_H_ */