1 #ifndef PAIREDENDQMODEL_H_
2 #define PAIREDENDQMODEL_H_
13 #include "my_assert.h"
14 #include "Orientation.h"
19 #include "NoiseQProfile.h"
21 #include "ModelParams.h"
24 #include "SingleReadQ.h"
25 #include "PairedEndReadQ.h"
26 #include "PairedEndHit.h"
27 #include "ReadReader.h"
31 class PairedEndQModel {
33 PairedEndQModel(Refs* refs = NULL) {
35 M = (refs != NULL ? refs->getM() : 0);
36 memset(N, 0, sizeof(N));
38 needCalcConPrb = true;
40 ori = new Orientation();
42 rspd = new RSPD(estRSPD);
44 qpro = new QProfile();
45 nqpro = new NoiseQProfile();
52 //If it is not a master node, only init & update can be used!
53 PairedEndQModel(ModelParams& params, bool isMaster = true) {
55 memcpy(N, params.N, sizeof(params.N));
57 estRSPD = params.estRSPD;
58 seedLen = params.seedLen;
59 needCalcConPrb = true;
61 ori = NULL; gld = NULL; rspd = NULL; qd = NULL; qpro = NULL; nqpro = NULL; mld = NULL;
65 if (!estRSPD) rspd = new RSPD(estRSPD);
67 mld = new LenDist(params.mate_minL, params.mate_maxL);
70 ori = new Orientation(params.probF);
71 gld = new LenDist(params.minL, params.maxL);
72 if (estRSPD) rspd = new RSPD(estRSPD, params.B);
73 qpro = new QProfile();
74 nqpro = new NoiseQProfile();
79 if (ori != NULL) delete ori;
80 if (gld != NULL) delete gld;
81 if (rspd != NULL) delete rspd;
82 if (qd != NULL) delete qd;
83 if (qpro != NULL) delete qpro;
84 if (nqpro != NULL) delete nqpro;
85 if (mld != NULL) delete mld;
86 if (mw != NULL) delete mw;
89 void estimateFromReads(const char*);
91 //if prob is too small, just make it 0
92 double getConPrb(const PairedEndReadQ& read, const PairedEndHit& hit) {
93 if (read.isLowQuality()) return 0.0;
96 int sid = hit.getSid();
97 RefSeq &ref = refs->getRef(sid);
98 int dir = hit.getDir();
99 int pos = hit.getPos();
100 int fullLen = ref.getFullLen();
101 int totLen = ref.getTotLen();
102 int insertLen = hit.getInsertL();
104 int fpos = (dir == 0 ? pos : totLen - pos - insertLen); // the aligned position reported in SAM file, should be a coordinate in forward strand
105 int effL = std::min(fullLen, totLen - insertLen + 1);
107 general_assert(fpos >= 0, "The alignment of fragment " + read.getName() + " to transcript " + itos(sid) + " starts at " + itos(fpos) + \
108 " from the forward direction, which should be a non-negative number! " + \
109 "It is possible that the aligner you use gave different read lengths for a same read in SAM file.");
110 general_assert(fpos + insertLen <= totLen,"Fragment " + read.getName() + " is hung over the end of transcript " + itos(sid) + "! " \
111 + "It is possible that the aligner you use gave different read lengths for a same read in SAM file.");
112 general_assert(insertLen <= totLen, "Fragment " + read.getName() + " has length " + itos(insertLen) + ", but it is aligned to transcript " \
113 + itos(sid) + ", whose length (" + itos(totLen) + ") is shorter than the fragment's length!");
115 if (fpos >= fullLen || ref.getMask(fpos)) return 0.0; // For paired-end model, fpos is the seedPos
117 prob = ori->getProb(dir) * gld->getAdjustedProb(insertLen, totLen) *
118 rspd->getAdjustedProb(fpos, effL, fullLen);
120 const SingleReadQ& mate1 = read.getMate1();
121 prob *= mld->getAdjustedProb(mate1.getReadLength(), insertLen) *
122 qpro->getProb(mate1.getReadSeq(), mate1.getQScore(), ref, pos, dir);
124 const SingleReadQ& mate2 = read.getMate2();
125 int m2pos = totLen - pos - insertLen;
127 prob *= mld->getAdjustedProb(mate2.getReadLength(), hit.getInsertL()) *
128 qpro->getProb(mate2.getReadSeq(), mate2.getQScore(), ref, m2pos, m2dir);
130 if (prob < EPSILON) { prob = 0.0; }
132 prob = (mw[sid] < EPSILON ? 0.0 : prob / mw[sid]);
137 double getNoiseConPrb(const PairedEndReadQ& read) {
138 if (read.isLowQuality()) return 0.0;
141 const SingleReadQ& mate1 = read.getMate1();
142 const SingleReadQ& mate2 = read.getMate2();
144 prob = mld->getProb(mate1.getReadLength()) * nqpro->getProb(mate1.getReadSeq(), mate1.getQScore());
145 prob *= mld->getProb(mate2.getReadLength()) * nqpro->getProb(mate2.getReadSeq(), mate2.getQScore());
147 if (prob < EPSILON) { prob = 0.0; }
149 prob = (mw[0] < EPSILON ? 0.0: prob / mw[0]);
154 double getLogP() { return nqpro->getLogP(); }
158 void update(const PairedEndReadQ& read, const PairedEndHit& hit, double frac) {
159 if (read.isLowQuality() || frac < EPSILON) return;
161 RefSeq& ref = refs->getRef(hit.getSid());
162 const SingleReadQ& mate1 = read.getMate1();
163 const SingleReadQ& mate2 = read.getMate2();
165 gld->update(hit.getInsertL(), frac);
167 int fpos = (hit.getDir() == 0 ? hit.getPos() : ref.getTotLen() - hit.getPos() - hit.getInsertL());
168 rspd->update(fpos, ref.getFullLen(), frac);
170 qpro->update(mate1.getReadSeq(), mate1.getQScore(), ref, hit.getPos(), hit.getDir(), frac);
172 int m2pos = ref.getTotLen() - hit.getPos() - hit.getInsertL();
173 int m2dir = !hit.getDir();
174 qpro->update(mate2.getReadSeq(), mate2.getQScore(), ref, m2pos, m2dir, frac);
177 void updateNoise(const PairedEndReadQ& read, double frac) {
178 if (read.isLowQuality() || frac < EPSILON) return;
180 const SingleReadQ& mate1 = read.getMate1();
181 const SingleReadQ& mate2 = read.getMate2();
183 nqpro->update(mate1.getReadSeq(), mate1.getQScore(), frac);
184 nqpro->update(mate2.getReadSeq(), mate2.getQScore(), frac);
189 void collect(const PairedEndQModel&);
191 bool getNeedCalcConPrb() { return needCalcConPrb; }
192 void setNeedCalcConPrb(bool value) { needCalcConPrb = value; }
194 void read(const char*);
195 void write(const char*);
197 const LenDist& getGLD() { return *gld; }
199 void startSimulation(simul*, double*);
200 bool simulate(int, PairedEndReadQ&, int&);
201 void finishSimulation();
203 //Use it after function 'read' or 'estimateFromReads'
209 int getModelType() const { return model_type; }
212 static const int model_type = 3;
213 static const int read_type = 3;
221 bool needCalcConPrb; //true need, false does not need
224 LenDist *gld, *mld; //mld1 mate_length_dist
228 NoiseQProfile *nqpro;
230 simul *sampler; // for simulation
231 double *theta_cdf; // for simulation
233 double *mw; // for masking
238 void PairedEndQModel::estimateFromReads(const char* readFN) {
240 char readFs[2][STRLEN];
244 for (int i = 0; i < 3; i++)
246 genReadFileNames(readFN, i, read_type, s, readFs);
247 ReadReader<PairedEndReadQ> reader(s, readFs, refs->hasPolyA(), seedLen); // allow calculation of calc_lq() function
250 while (reader.next(read)) {
251 SingleReadQ mate1 = read.getMate1();
252 SingleReadQ mate2 = read.getMate2();
254 if (!read.isLowQuality()) {
255 mld->update(mate1.getReadLength(), 1.0);
256 mld->update(mate2.getReadLength(), 1.0);
258 qd->update(mate1.getQScore());
259 qd->update(mate2.getQScore());
262 nqpro->updateC(mate1.getReadSeq(), mate1.getQScore());
263 nqpro->updateC(mate2.getReadSeq(), mate2.getQScore());
266 else if (verbose && (mate1.getReadLength() < seedLen || mate2.getReadLength() < seedLen)) {
267 printf("Warning: Read %s is ignored due to at least one of the mates' length < seed length %d!\n", read.getName().c_str(), seedLen);
271 if (verbose && cnt % 1000000 == 0) { printf("%d READS PROCESSED\n", cnt); }
274 if (verbose) { printf("estimateFromReads, N%d finished.\n", i); }
279 nqpro->calcInitParams();
281 mw = new double[M + 1];
285 void PairedEndQModel::init() {
287 if (estRSPD) rspd->init();
292 void PairedEndQModel::finish() {
294 if (estRSPD) rspd->finish();
297 needCalcConPrb = true;
301 void PairedEndQModel::collect(const PairedEndQModel& o) {
302 gld->collect(*(o.gld));
303 if (estRSPD) rspd->collect(*(o.rspd));
304 qpro->collect(*(o.qpro));
305 nqpro->collect(*(o.nqpro));
308 //Only master node can call
309 void PairedEndQModel::read(const char* inpF) {
311 FILE *fi = fopen(inpF, "r");
312 if (fi == NULL) { fprintf(stderr, "Cannot open %s! It may not exist.\n", inpF); exit(-1); }
314 assert(fscanf(fi, "%d", &val) == 1);
315 assert(val == model_type);
325 if (fscanf(fi, "%d", &val) == 1) {
328 mw = new double[M + 1];
329 for (int i = 0; i <= M; i++) assert(fscanf(fi, "%lf", &mw[i]) == 1);
337 //Only master node can call. Only be called at EM.cpp
338 void PairedEndQModel::write(const char* outF) {
339 FILE *fo = fopen(outF, "w");
341 fprintf(fo, "%d\n", model_type);
344 ori->write(fo); fprintf(fo, "\n");
345 gld->write(fo); fprintf(fo, "\n");
346 mld->write(fo); fprintf(fo, "\n");
347 rspd->write(fo); fprintf(fo, "\n");
348 qd->write(fo); fprintf(fo, "\n");
349 qpro->write(fo); fprintf(fo, "\n");
353 fprintf(fo, "\n%d\n", M);
354 for (int i = 0; i < M; i++) {
355 fprintf(fo, "%.15g ", mw[i]);
357 fprintf(fo, "%.15g\n", mw[M]);
363 void PairedEndQModel::startSimulation(simul* sampler, double* theta) {
364 this->sampler = sampler;
366 theta_cdf = new double[M + 1];
367 for (int i = 0; i <= M; i++) {
368 theta_cdf[i] = theta[i];
369 if (i > 0) theta_cdf[i] += theta_cdf[i - 1];
372 rspd->startSimulation(M, refs);
373 qd->startSimulation();
374 qpro->startSimulation();
375 nqpro->startSimulation();
378 bool PairedEndQModel::simulate(int rid, PairedEndReadQ& read, int& sid) {
380 int insertL, mateL1, mateL2;
382 std::string qual1, qual2, readseq1, readseq2;
383 std::ostringstream strout;
385 sid = sampler->sample(theta_cdf, M + 1);
388 dir = pos = insertL = 0;
389 mateL1 = mld->simulate(sampler, -1);
390 qual1 = qd->simulate(sampler, mateL1);
391 readseq1 = nqpro->simulate(sampler, mateL1, qual1);
393 mateL2 = mld->simulate(sampler, -1);
394 qual2 = qd->simulate(sampler, mateL2);
395 readseq2 = nqpro->simulate(sampler, mateL2, qual2);
398 RefSeq &ref = refs->getRef(sid);
399 dir = ori->simulate(sampler);
400 insertL = gld->simulate(sampler, ref.getTotLen());
401 if (insertL < 0) return false;
402 int effL = std::min(ref.getFullLen(), ref.getTotLen() - insertL + 1);
403 pos = rspd->simulate(sampler, sid, effL);
404 if (pos < 0) return false;
405 if (dir > 0) pos = ref.getTotLen() - pos - insertL;
407 mateL1 = mld->simulate(sampler, insertL);
408 qual1 = qd->simulate(sampler, mateL1);
409 readseq1 = qpro->simulate(sampler, mateL1, pos, dir, qual1, ref);
411 int m2pos = ref.getTotLen() - pos - insertL;
414 mateL2 = mld->simulate(sampler, insertL);
415 qual2 = qd->simulate(sampler, mateL2);
416 readseq2 = qpro->simulate(sampler, mateL2, m2pos, m2dir, qual2, ref);
419 strout<<rid<<"_"<<dir<<"_"<<sid<<"_"<<pos<<"_"<<insertL;
422 read = PairedEndReadQ(SingleReadQ(name + "/1", readseq1, qual1), SingleReadQ(name + "/2", readseq2, qual2));
427 void PairedEndQModel::finishSimulation() {
430 rspd->finishSimulation();
431 qd->finishSimulation();
432 qpro->finishSimulation();
433 nqpro->finishSimulation();
437 void PairedEndQModel::calcMW() {
438 assert(mld->getMinL() >= seedLen);
440 memset(mw, 0, sizeof(double) * (M + 1));
443 for (int i = 1; i <= M; i++) {
444 RefSeq& ref = refs->getRef(i);
445 int totLen = ref.getTotLen();
446 int fullLen = ref.getFullLen();
447 int end = std::min(fullLen, totLen - gld->getMinL() + 1);
452 //seedPos is fpos here
453 for (int seedPos = 0; seedPos < end; seedPos++)
454 if (ref.getMask(seedPos)) {
455 minL = gld->getMinL();
456 maxL = std::min(gld->getMaxL(), totLen - seedPos);
458 for (int fragLen = minL; fragLen <= maxL; fragLen++) {
459 effL = std::min(fullLen, totLen - fragLen + 1);
460 value += gld->getAdjustedProb(fragLen, totLen) * rspd->getAdjustedProb(pfpos, effL, fullLen);
467 // fprintf(stderr, "Warning: %dth reference sequence is masked for almost all positions!\n", i);
473 #endif /* PAIREDENDQMODEL_H_ */