1 #ifndef PAIREDENDMODEL_H_
2 #define PAIREDENDMODEL_H_
13 #include "my_assert.h"
14 #include "Orientation.h"
18 #include "NoiseProfile.h"
20 #include "ModelParams.h"
23 #include "SingleRead.h"
24 #include "PairedEndRead.h"
25 #include "PairedEndHit.h"
26 #include "ReadReader.h"
30 class PairedEndModel {
32 PairedEndModel(Refs* refs = NULL) {
34 M = (refs != NULL ? refs->getM() : 0);
35 memset(N, 0, sizeof(N));
37 needCalcConPrb = true;
39 ori = new Orientation();
41 rspd = new RSPD(estRSPD);
43 npro = new NoiseProfile();
50 //If it is not a master node, only init & update can be used!
51 PairedEndModel(ModelParams& params, bool isMaster = true) {
53 memcpy(N, params.N, sizeof(params.N));
55 estRSPD = params.estRSPD;
56 seedLen = params.seedLen;
57 needCalcConPrb = true;
59 ori = NULL; gld = NULL; rspd = NULL; pro = NULL; npro = NULL; mld = NULL;
63 if (!estRSPD) rspd = new RSPD(estRSPD);
64 mld = new LenDist(params.mate_minL, params.mate_maxL);
67 ori = new Orientation(params.probF);
68 gld = new LenDist(params.minL, params.maxL);
69 if (estRSPD) rspd = new RSPD(estRSPD, params.B);
70 pro = new Profile(params.maxL);
71 npro = new NoiseProfile();
76 if (ori != NULL) delete ori;
77 if (gld != NULL) delete gld;
78 if (rspd != NULL) delete rspd;
79 if (pro != NULL) delete pro;
80 if (npro != NULL) delete npro;
81 if (mld != NULL) delete mld;
82 if (mw != NULL) delete mw;
85 void estimateFromReads(const char*);
87 //if prob is too small, just make it 0
88 double getConPrb(const PairedEndRead& read, const PairedEndHit& hit) {
89 if (read.isLowQuality()) return 0.0;
92 int sid = hit.getSid();
93 RefSeq &ref = refs->getRef(sid);
94 int dir = hit.getDir();
95 int pos = hit.getPos();
96 int fullLen = ref.getFullLen();
97 int totLen = ref.getTotLen();
98 int insertLen = hit.getInsertL();
100 int fpos = (dir == 0 ? pos : totLen - pos - insertLen); // the aligned position reported in SAM file, should be a coordinate in forward strand
101 int effL = std::min(fullLen, totLen - insertLen + 1);
103 general_assert(fpos >= 0, "The alignment of fragment " + read.getName() + " to transcript " + itos(sid) + " starts at " + itos(fpos) + \
104 " from the forward direction, which should be a non-negative number! " + \
105 "It is possible that the aligner you use gave different read lengths for a same read in SAM file.");
106 general_assert(fpos + insertLen <= totLen,"Fragment " + read.getName() + " is hung over the end of transcript " + itos(sid) + "! " \
107 + "It is possible that the aligner you use gave different read lengths for a same read in SAM file.");
108 general_assert(insertLen <= totLen, "Fragment " + read.getName() + " has length " + itos(insertLen) + ", but it is aligned to transcript " \
109 + itos(sid) + ", whose length (" + itos(totLen) + ") is shorter than the fragment's length!");
112 if (fpos >= fullLen || ref.getMask(fpos)) return 0.0; // For paired-end model, fpos is the seedPos
114 prob = ori->getProb(dir) * gld->getAdjustedProb(insertLen, totLen) *
115 rspd->getAdjustedProb(fpos, effL, fullLen);
117 const SingleRead& mate1 = read.getMate1();
118 prob *= mld->getAdjustedProb(mate1.getReadLength(), insertLen) *
119 pro->getProb(mate1.getReadSeq(), ref, pos, dir);
121 const SingleRead& mate2 = read.getMate2();
122 int m2pos = totLen - pos - insertLen;
124 prob *= mld->getAdjustedProb(mate2.getReadLength(), insertLen) *
125 pro->getProb(mate2.getReadSeq(), ref, m2pos, m2dir);
127 if (prob < EPSILON) { prob = 0.0; }
129 prob = (mw[sid] < EPSILON ? 0.0 : prob / mw[sid]);
134 double getNoiseConPrb(const PairedEndRead& read) {
135 if (read.isLowQuality()) return 0.0;
137 const SingleRead& mate1 = read.getMate1();
138 const SingleRead& mate2 = read.getMate2();
140 prob = mld->getProb(mate1.getReadLength()) * npro->getProb(mate1.getReadSeq());
141 prob *= mld->getProb(mate2.getReadLength()) * npro->getProb(mate2.getReadSeq());
143 if (prob < EPSILON) { prob = 0.0; }
145 prob = (mw[0] < EPSILON ? 0.0: prob / mw[0]);
150 double getLogP() { return npro->getLogP(); }
154 void update(const PairedEndRead& read, const PairedEndHit& hit, double frac) {
155 if (read.isLowQuality() || frac < EPSILON) return;
157 RefSeq& ref = refs->getRef(hit.getSid());
158 const SingleRead& mate1 = read.getMate1();
159 const SingleRead& mate2 = read.getMate2();
161 gld->update(hit.getInsertL(), frac);
163 int fpos = (hit.getDir() == 0 ? hit.getPos() : ref.getTotLen() - hit.getPos() - hit.getInsertL());
164 rspd->update(fpos, ref.getFullLen(), frac);
166 pro->update(mate1.getReadSeq(), ref, hit.getPos(), hit.getDir(), frac);
168 int m2pos = ref.getTotLen() - hit.getPos() - hit.getInsertL();
169 int m2dir = !hit.getDir();
170 pro->update(mate2.getReadSeq(), ref, m2pos, m2dir, frac);
173 void updateNoise(const PairedEndRead& read, double frac) {
174 if (read.isLowQuality() || frac < EPSILON) return;
176 const SingleRead& mate1 = read.getMate1();
177 const SingleRead& mate2 = read.getMate2();
179 npro->update(mate1.getReadSeq(), frac);
180 npro->update(mate2.getReadSeq(), frac);
185 void collect(const PairedEndModel&);
187 bool getNeedCalcConPrb() { return needCalcConPrb; }
188 void setNeedCalcConPrb(bool value) { needCalcConPrb = value; }
190 void read(const char*);
191 void write(const char*);
193 const LenDist& getGLD() { return *gld; }
195 void startSimulation(simul*, double*);
196 bool simulate(int, PairedEndRead&, int&);
197 void finishSimulation();
199 //Use it after function 'read' or 'estimateFromReads'
205 int getModelType() const { return model_type; }
208 static const int model_type = 2;
209 static const int read_type = 2;
217 bool needCalcConPrb; //true need, false does not need
220 LenDist *gld, *mld; //mld1 mate_length_dist
225 simul *sampler; // for simulation
226 double *theta_cdf; // for simulation
228 double *mw; // for masking
233 void PairedEndModel::estimateFromReads(const char* readFN) {
235 char readFs[2][STRLEN];
239 for (int i = 0; i < 3; i++)
241 genReadFileNames(readFN, i, read_type, s, readFs);
242 ReadReader<PairedEndRead> reader(s, readFs, refs->hasPolyA(), seedLen); // allow calculation of calc_lq() function
245 while (reader.next(read)) {
246 SingleRead mate1 = read.getMate1();
247 SingleRead mate2 = read.getMate2();
249 if (!read.isLowQuality()) {
250 mld->update(mate1.getReadLength(), 1.0);
251 mld->update(mate2.getReadLength(), 1.0);
254 npro->updateC(mate1.getReadSeq());
255 npro->updateC(mate2.getReadSeq());
258 else if (verbose && (mate1.getReadLength() < seedLen || mate2.getReadLength() < seedLen)) {
259 printf("Warning: Read %s is ignored due to at least one of the mates' length < seed length %d!\n", read.getName().c_str(), seedLen);
263 if (verbose && cnt % 1000000 == 0) { printf("%d READS PROCESSED\n", cnt); }
266 if (verbose) { printf("estimateFromReads, N%d finished.\n", i); }
270 npro->calcInitParams();
272 mw = new double[M + 1];
276 void PairedEndModel::init() {
278 if (estRSPD) rspd->init();
283 void PairedEndModel::finish() {
285 if (estRSPD) rspd->finish();
288 needCalcConPrb = true;
292 void PairedEndModel::collect(const PairedEndModel& o) {
293 gld->collect(*(o.gld));
294 if (estRSPD) rspd->collect(*(o.rspd));
295 pro->collect(*(o.pro));
296 npro->collect(*(o.npro));
299 //Only master node can call
300 void PairedEndModel::read(const char* inpF) {
302 FILE *fi = fopen(inpF, "r");
303 if (fi == NULL) { fprintf(stderr, "Cannot open %s! It may not exist.\n", inpF); exit(-1); }
305 assert(fscanf(fi, "%d", &val) == 1);
306 assert(val == model_type);
315 if (fscanf(fi, "%d", &val) == 1) {
318 mw = new double[M + 1];
319 for (int i = 0; i <= M; i++) assert(fscanf(fi, "%lf", &mw[i]) == 1);
326 //Only master node can call. Only be called at EM.cpp
327 void PairedEndModel::write(const char* outF) {
328 FILE *fo = fopen(outF, "w");
330 fprintf(fo, "%d\n", model_type);
333 ori->write(fo); fprintf(fo, "\n");
334 gld->write(fo); fprintf(fo, "\n");
335 mld->write(fo); fprintf(fo, "\n");
336 rspd->write(fo); fprintf(fo, "\n");
337 pro->write(fo); fprintf(fo, "\n");
341 fprintf(fo, "\n%d\n", M);
342 for (int i = 0; i < M; i++) {
343 fprintf(fo, "%.15g ", mw[i]);
345 fprintf(fo, "%.15g\n", mw[M]);
351 void PairedEndModel::startSimulation(simul* sampler, double* theta) {
352 this->sampler = sampler;
354 theta_cdf = new double[M + 1];
355 for (int i = 0; i <= M; i++) {
356 theta_cdf[i] = theta[i];
357 if (i > 0) theta_cdf[i] += theta_cdf[i - 1];
360 rspd->startSimulation(M, refs);
361 pro->startSimulation();
362 npro->startSimulation();
365 bool PairedEndModel::simulate(int rid, PairedEndRead& read, int& sid) {
367 int insertL, mateL1, mateL2;
369 std::string readseq1, readseq2;
370 std::ostringstream strout;
372 sid = sampler->sample(theta_cdf, M + 1);
375 dir = pos = insertL = 0;
376 mateL1 = mld->simulate(sampler, -1);
377 readseq1 = npro->simulate(sampler, mateL1);
379 mateL2 = mld->simulate(sampler, -1);
380 readseq2 = npro->simulate(sampler, mateL2);
383 RefSeq &ref = refs->getRef(sid);
384 dir = ori->simulate(sampler);
385 insertL = gld->simulate(sampler, ref.getTotLen());
386 if (insertL < 0) return false;
387 int effL = std::min(ref.getFullLen(), ref.getTotLen() - insertL + 1);
388 pos = rspd->simulate(sampler, sid, effL);
389 if (pos < 0) return false;
390 if (dir > 0) pos = ref.getTotLen() - pos - insertL;
392 mateL1 = mld->simulate(sampler, insertL);
393 readseq1 = pro->simulate(sampler, mateL1, pos, dir, ref);
395 int m2pos = ref.getTotLen() - pos - insertL;
398 mateL2 = mld->simulate(sampler, insertL);
399 readseq2 = pro->simulate(sampler, mateL2, m2pos, m2dir, ref);
402 strout<<rid<<"_"<<dir<<"_"<<sid<<"_"<<pos<<"_"<<insertL;
405 read = PairedEndRead(SingleRead(name + "/1", readseq1), SingleRead(name + "/2", readseq2));
410 void PairedEndModel::finishSimulation() {
413 rspd->finishSimulation();
414 pro->finishSimulation();
415 npro->finishSimulation();
418 void PairedEndModel::calcMW() {
419 assert(mld->getMinL() >= seedLen);
421 memset(mw, 0, sizeof(double) * (M + 1));
424 for (int i = 1; i <= M; i++) {
425 RefSeq& ref = refs->getRef(i);
426 int totLen = ref.getTotLen();
427 int fullLen = ref.getFullLen();
428 int end = std::min(fullLen, totLen - gld->getMinL() + 1);
433 //seedPos is fpos here
434 for (int seedPos = 0; seedPos < end; seedPos++)
435 if (ref.getMask(seedPos)) {
436 minL = gld->getMinL();
437 maxL = std::min(gld->getMaxL(), totLen - seedPos);
439 for (int fragLen = minL; fragLen <= maxL; fragLen++) {
440 effL = std::min(fullLen, totLen - fragLen + 1);
441 value += gld->getAdjustedProb(fragLen, totLen) * rspd->getAdjustedProb(pfpos, effL, fullLen);
448 //fprintf(stderr, "Warning: %dth reference sequence is masked for almost all positions!\n", i);
454 #endif /* PAIREDENDMODEL_H_ */