]> git.donarmstrong.com Git - rsem.git/blob - PairedEndQModel.h
Fixed a bug in perl scripts for printing error messages
[rsem.git] / PairedEndQModel.h
1 #ifndef PAIREDENDQMODEL_H_
2 #define PAIREDENDQMODEL_H_
3
4 #include<cmath>
5 #include<cstdio>
6 #include<cassert>
7 #include<cstring>
8 #include<string>
9 #include<algorithm>
10 #include<sstream>
11 #include<iostream>
12
13 #include "utils.h"
14 #include "my_assert.h"
15 #include "Orientation.h"
16 #include "LenDist.h"
17 #include "RSPD.h"
18 #include "QualDist.h"
19 #include "QProfile.h"
20 #include "NoiseQProfile.h"
21
22 #include "ModelParams.h"
23 #include "RefSeq.h"
24 #include "Refs.h"
25 #include "SingleReadQ.h"
26 #include "PairedEndReadQ.h"
27 #include "PairedEndHit.h"
28 #include "ReadReader.h"
29
30 #include "simul.h"
31
32 class PairedEndQModel {
33 public:
34         PairedEndQModel(Refs* refs = NULL) {
35                 this->refs = refs;
36                 M = (refs != NULL ? refs->getM() : 0);
37                 memset(N, 0, sizeof(N));
38                 estRSPD = false;
39                 needCalcConPrb = true;
40
41                 ori = new Orientation();
42                 gld = new LenDist();
43                 rspd = new RSPD(estRSPD);
44                 qd = new QualDist();
45                 qpro = new QProfile();
46                 nqpro = new NoiseQProfile();
47                 mld = new LenDist();
48
49                 mw = NULL;
50                 seedLen = 0;
51         }
52
53         //If it is not a master node, only init & update can be used!
54         PairedEndQModel(ModelParams& params, bool isMaster = true) {
55                 M = params.M;
56                 memcpy(N, params.N, sizeof(params.N));
57                 refs = params.refs;
58                 estRSPD = params.estRSPD;
59                 seedLen = params.seedLen;
60                 needCalcConPrb = true;
61
62                 ori = NULL; gld = NULL; rspd = NULL; qd = NULL; qpro = NULL; nqpro = NULL; mld = NULL;
63                 mw = NULL;
64
65                 if (isMaster) {
66                         if (!estRSPD) rspd = new RSPD(estRSPD);
67                         qd = new QualDist();
68                         mld = new LenDist(params.mate_minL, params.mate_maxL);
69                 }
70
71                 ori = new Orientation(params.probF);
72                 gld = new LenDist(params.minL, params.maxL);
73                 if (estRSPD) rspd = new RSPD(estRSPD, params.B);
74                 qpro = new QProfile();
75                 nqpro = new NoiseQProfile();
76         }
77
78         ~PairedEndQModel() {
79                 refs = NULL;
80                 if (ori != NULL) delete ori;
81                 if (gld != NULL) delete gld;
82                 if (rspd != NULL) delete rspd;
83                 if (qd != NULL) delete qd;
84                 if (qpro != NULL) delete qpro;
85                 if (nqpro != NULL) delete nqpro;
86                 if (mld != NULL) delete mld;
87                 if (mw != NULL) delete mw;
88         }
89
90         void estimateFromReads(const char*);
91
92         //if prob is too small, just make it 0
93         double getConPrb(const PairedEndReadQ& read, const PairedEndHit& hit) {
94                 if (read.isLowQuality()) return 0.0;
95
96                 double prob;
97                 int sid = hit.getSid();
98                 RefSeq &ref = refs->getRef(sid);
99                 int dir = hit.getDir();
100                 int pos = hit.getPos();
101                 int fullLen = ref.getFullLen();
102                 int totLen = ref.getTotLen();
103                 int insertLen = hit.getInsertL();
104
105                 int fpos = (dir == 0 ? pos : totLen - pos - insertLen); // the aligned position reported in SAM file, should be a coordinate in forward strand
106                 int effL = std::min(fullLen, totLen - insertLen + 1);
107
108                 general_assert(fpos >= 0, "The alignment of fragment " + read.getName() + " to transcript " + itos(sid) + " starts at " + itos(fpos) + \
109                                 " from the forward direction, which should be a non-negative number! " + \
110                                 "It is possible that the aligner you use gave different read lengths for a same read in SAM file.");
111                 general_assert(fpos + insertLen <= totLen,"Fragment " + read.getName() + " is hung over the end of transcript " + itos(sid) + "! " \
112                                 + "It is possible that the aligner you use gave different read lengths for a same read in SAM file.");
113                 general_assert(insertLen <= totLen, "Fragment " + read.getName() + " has length " + itos(insertLen) + ", but it is aligned to transcript " \
114                                 + itos(sid) + ", whose length (" + itos(totLen) + ") is shorter than the fragment's length!");
115
116                 if (fpos >= fullLen || ref.getMask(fpos)) return 0.0; // For paired-end model, fpos is the seedPos
117
118                 prob = ori->getProb(dir) * gld->getAdjustedProb(insertLen, totLen) *
119                        rspd->getAdjustedProb(fpos, effL, fullLen);
120
121                 const SingleReadQ& mate1 = read.getMate1();
122                 prob *= mld->getAdjustedProb(mate1.getReadLength(), insertLen) *
123                         qpro->getProb(mate1.getReadSeq(), mate1.getQScore(), ref, pos, dir);
124
125                 const SingleReadQ& mate2 = read.getMate2();
126                 int m2pos = totLen - pos - insertLen;
127                 int m2dir = !dir;
128                 prob *= mld->getAdjustedProb(mate2.getReadLength(), hit.getInsertL()) *
129                         qpro->getProb(mate2.getReadSeq(), mate2.getQScore(), ref, m2pos, m2dir);
130
131                 if (prob < EPSILON) { prob = 0.0; }
132
133                 prob = (mw[sid] < EPSILON ? 0.0 : prob / mw[sid]);
134
135                 return prob;
136         }
137
138         double getNoiseConPrb(const PairedEndReadQ& read) {
139                 if (read.isLowQuality()) return 0.0;
140
141                 double prob;
142                 const SingleReadQ& mate1 = read.getMate1();
143                 const SingleReadQ& mate2 = read.getMate2();
144
145                 prob = mld->getProb(mate1.getReadLength()) * nqpro->getProb(mate1.getReadSeq(), mate1.getQScore());
146                 prob *= mld->getProb(mate2.getReadLength()) * nqpro->getProb(mate2.getReadSeq(), mate2.getQScore());
147
148                 if (prob < EPSILON) { prob = 0.0; }
149
150                 prob = (mw[0] < EPSILON ? 0.0: prob / mw[0]);
151
152                 return prob;
153         }
154
155         double getLogP() { return nqpro->getLogP(); }
156
157         void init();
158
159         void update(const PairedEndReadQ& read, const PairedEndHit& hit, double frac) {
160                 if (read.isLowQuality() || frac < EPSILON) return;
161
162                 RefSeq& ref = refs->getRef(hit.getSid());
163                 const SingleReadQ& mate1 = read.getMate1();
164                 const SingleReadQ& mate2 = read.getMate2();
165
166                 gld->update(hit.getInsertL(), frac);
167                 if (estRSPD) {
168                         int fpos = (hit.getDir() == 0 ? hit.getPos() : ref.getTotLen() - hit.getPos() - hit.getInsertL());
169                         rspd->update(fpos, ref.getFullLen(), frac);
170                 }
171                 qpro->update(mate1.getReadSeq(), mate1.getQScore(), ref, hit.getPos(), hit.getDir(), frac);
172
173                 int m2pos = ref.getTotLen() - hit.getPos() - hit.getInsertL();
174                 int m2dir = !hit.getDir();
175                 qpro->update(mate2.getReadSeq(), mate2.getQScore(), ref, m2pos, m2dir, frac);
176         }
177
178         void updateNoise(const PairedEndReadQ& read, double frac) {
179                 if (read.isLowQuality() || frac < EPSILON) return;
180
181                 const SingleReadQ& mate1 = read.getMate1();
182                 const SingleReadQ& mate2 = read.getMate2();
183
184                 nqpro->update(mate1.getReadSeq(), mate1.getQScore(), frac);
185                 nqpro->update(mate2.getReadSeq(), mate2.getQScore(), frac);
186         }
187
188         void finish();
189
190         void collect(const PairedEndQModel&);
191
192         bool getNeedCalcConPrb() { return needCalcConPrb; }
193         void setNeedCalcConPrb(bool value) { needCalcConPrb = value; }
194
195         void read(const char*);
196         void write(const char*);
197
198         const LenDist& getGLD() { return *gld; }
199
200         void startSimulation(simul*, double*);
201         bool simulate(READ_INT_TYPE, PairedEndReadQ&, int&);
202         void finishSimulation();
203
204         //Use it after function 'read' or 'estimateFromReads'
205         double* getMW() { 
206           assert(mw != NULL);
207           return mw;
208         }
209
210         int getModelType() const { return model_type; }
211  
212 private:
213         static const int model_type = 3;
214         static const int read_type = 3;
215
216         int M;
217         READ_INT_TYPE N[3];
218         Refs *refs;
219         int seedLen;
220
221         bool estRSPD;
222         bool needCalcConPrb; //true need, false does not need
223
224         Orientation *ori;
225         LenDist *gld, *mld; //mld1 mate_length_dist
226         RSPD *rspd;
227         QualDist *qd;
228         QProfile *qpro;
229         NoiseQProfile *nqpro;
230
231         simul *sampler; // for simulation
232         double *theta_cdf; // for simulation
233
234         double *mw; // for masking
235
236         void calcMW();
237 };
238
239 void PairedEndQModel::estimateFromReads(const char* readFN) {
240         int s;
241         char readFs[2][STRLEN];
242     PairedEndReadQ read;
243
244     mld->init();
245     for (int i = 0; i < 3; i++)
246         if (N[i] > 0) {
247                 genReadFileNames(readFN, i, read_type, s, readFs);
248                 ReadReader<PairedEndReadQ> reader(s, readFs, refs->hasPolyA(), seedLen); // allow calculation of calc_lq() function
249
250                 READ_INT_TYPE cnt = 0;
251                 while (reader.next(read)) {
252                         SingleReadQ mate1 = read.getMate1();
253                         SingleReadQ mate2 = read.getMate2();
254
255                         if (!read.isLowQuality()) {
256                                 mld->update(mate1.getReadLength(), 1.0);
257                                 mld->update(mate2.getReadLength(), 1.0);
258
259                                 qd->update(mate1.getQScore());
260                                 qd->update(mate2.getQScore());
261                           
262                                 if (i == 0) {
263                                         nqpro->updateC(mate1.getReadSeq(), mate1.getQScore());
264                                         nqpro->updateC(mate2.getReadSeq(), mate2.getQScore());
265                                 }
266                         }
267                         else if (verbose && (mate1.getReadLength() < seedLen || mate2.getReadLength() < seedLen)) {
268                                 std::cout<< "Warning: Read "<< read.getName()<< " is ignored due to at least one of the mates' length < seed length "<< seedLen<< "!"<< std::endl;
269                         }
270
271                         ++cnt;
272                         if (verbose && cnt % 1000000 == 0) { std::cout<< cnt<< " READS PROCESSED"<< std::endl; }
273                 }
274
275                 if (verbose) { std::cout<<"estimateFromReads, N"<< i<<" finished."<< std::endl; }
276         }
277
278     mld->finish();
279     qd->finish();
280     nqpro->calcInitParams();
281
282     mw = new double[M + 1];
283     calcMW();
284 }
285
286 void PairedEndQModel::init() {
287         gld->init();
288         if (estRSPD) rspd->init();
289         qpro->init();
290         nqpro->init();
291 }
292
293 void PairedEndQModel::finish() {
294         gld->finish();
295         if (estRSPD) rspd->finish();
296         qpro->finish();
297         nqpro->finish();
298         needCalcConPrb = true;
299         calcMW();
300 }
301
302 void PairedEndQModel::collect(const PairedEndQModel& o) {
303         gld->collect(*(o.gld));
304         if (estRSPD) rspd->collect(*(o.rspd));
305         qpro->collect(*(o.qpro));
306         nqpro->collect(*(o.nqpro));
307 }
308
309 //Only master node can call
310 void PairedEndQModel::read(const char* inpF) {
311         int val;
312         FILE *fi = fopen(inpF, "r");
313         if (fi == NULL) { fprintf(stderr, "Cannot open %s! It may not exist.\n", inpF); exit(-1); }
314
315         assert(fscanf(fi, "%d", &val) == 1);
316         assert(val == model_type);
317
318         ori->read(fi);
319         gld->read(fi);
320         mld->read(fi);
321         rspd->read(fi);
322         qd->read(fi);
323         qpro->read(fi);
324         nqpro->read(fi);
325
326         if (fscanf(fi, "%d", &val) == 1) {
327                 if (M == 0) M = val;
328                 if (M == val) {
329                         mw = new double[M + 1];
330                         for (int i = 0; i <= M; i++) assert(fscanf(fi, "%lf", &mw[i]) == 1);
331                 }
332         }
333
334
335         fclose(fi);
336 }
337
338 //Only master node can call. Only be called at EM.cpp
339 void PairedEndQModel::write(const char* outF) {
340         FILE *fo = fopen(outF, "w");
341
342         fprintf(fo, "%d\n", model_type);
343         fprintf(fo, "\n");
344
345         ori->write(fo);  fprintf(fo, "\n");
346         gld->write(fo);  fprintf(fo, "\n");
347         mld->write(fo);  fprintf(fo, "\n");
348         rspd->write(fo); fprintf(fo, "\n");
349         qd->write(fo);   fprintf(fo, "\n");
350         qpro->write(fo); fprintf(fo, "\n");
351         nqpro->write(fo);
352
353         if (mw != NULL) {
354           fprintf(fo, "\n%d\n", M);
355           for (int i = 0; i < M; i++) {
356             fprintf(fo, "%.15g ", mw[i]);
357           }
358           fprintf(fo, "%.15g\n", mw[M]);
359         }
360
361         fclose(fo);
362 }
363
364 void PairedEndQModel::startSimulation(simul* sampler, double* theta) {
365         this->sampler = sampler;
366
367         theta_cdf = new double[M + 1];
368         for (int i = 0; i <= M; i++) {
369                 theta_cdf[i] = theta[i];
370                 if (i > 0) theta_cdf[i] += theta_cdf[i - 1];
371         }
372
373         rspd->startSimulation(M, refs);
374         qd->startSimulation();
375         qpro->startSimulation();
376         nqpro->startSimulation();
377 }
378
379 bool PairedEndQModel::simulate(READ_INT_TYPE rid, PairedEndReadQ& read, int& sid) {
380         int dir, pos;
381         int insertL, mateL1, mateL2;
382         std::string name;
383         std::string qual1, qual2, readseq1, readseq2;
384         std::ostringstream strout;
385
386         sid = sampler->sample(theta_cdf, M + 1);
387
388         if (sid == 0) {
389                 dir = pos = insertL = 0;
390                 mateL1 = mld->simulate(sampler, -1);
391                 qual1 = qd->simulate(sampler, mateL1);
392                 readseq1 = nqpro->simulate(sampler, mateL1, qual1);
393
394                 mateL2 = mld->simulate(sampler, -1);
395                 qual2 = qd->simulate(sampler, mateL2);
396                 readseq2 = nqpro->simulate(sampler, mateL2, qual2);
397         }
398         else {
399                 RefSeq &ref = refs->getRef(sid);
400                 dir = ori->simulate(sampler);
401                 insertL = gld->simulate(sampler, ref.getTotLen());
402                 if (insertL < 0) return false;
403                 int effL = std::min(ref.getFullLen(), ref.getTotLen() - insertL + 1);
404                 pos = rspd->simulate(sampler, sid, effL);
405                 if (pos < 0) return false;
406                 if (dir > 0) pos = ref.getTotLen() - pos - insertL;
407
408                 mateL1 = mld->simulate(sampler, insertL);
409                 qual1 = qd->simulate(sampler, mateL1);
410                 readseq1 = qpro->simulate(sampler, mateL1, pos, dir, qual1, ref);
411
412                 int m2pos = ref.getTotLen() - pos - insertL;
413                 int m2dir = !dir;
414
415                 mateL2 = mld->simulate(sampler, insertL);
416                 qual2 = qd->simulate(sampler, mateL2);
417                 readseq2 = qpro->simulate(sampler, mateL2, m2pos, m2dir, qual2, ref);
418         }
419
420         strout<<rid<<"_"<<dir<<"_"<<sid<<"_"<<pos<<"_"<<insertL;
421         name = strout.str();
422
423         read = PairedEndReadQ(SingleReadQ(name + "/1", readseq1, qual1), SingleReadQ(name + "/2", readseq2, qual2));
424
425         return true;
426 }
427
428 void PairedEndQModel::finishSimulation() {
429         delete[] theta_cdf;
430
431         rspd->finishSimulation();
432         qd->finishSimulation();
433         qpro->finishSimulation();
434         nqpro->finishSimulation();
435 }
436
437
438 void PairedEndQModel::calcMW() {
439         assert(mld->getMinL() >= seedLen);
440
441         memset(mw, 0, sizeof(double) * (M + 1));
442         mw[0] = 1.0;
443
444         for (int i = 1; i <= M; i++) {
445                 RefSeq& ref = refs->getRef(i);
446                 int totLen = ref.getTotLen();
447                 int fullLen = ref.getFullLen();
448                 int end = std::min(fullLen, totLen - gld->getMinL() + 1);
449                 double value = 0.0;
450                 int minL, maxL;
451                 int effL, pfpos;
452
453                 //seedPos is fpos here
454                 for (int seedPos = 0; seedPos < end; seedPos++)
455                         if (ref.getMask(seedPos)) {
456                                 minL = gld->getMinL();
457                                 maxL = std::min(gld->getMaxL(), totLen - seedPos);
458                                 pfpos = seedPos;
459                                 for (int fragLen = minL; fragLen <= maxL; fragLen++) {
460                                         effL = std::min(fullLen, totLen - fragLen + 1);
461                                         value += gld->getAdjustedProb(fragLen, totLen) * rspd->getAdjustedProb(pfpos, effL, fullLen);
462                                 }
463                         }
464     
465                 mw[i] = 1.0 - value;
466
467                 if (mw[i] < 1e-8) {
468                         //      fprintf(stderr, "Warning: %dth reference sequence is masked for almost all positions!\n", i);
469                         mw[i] = 0.0;
470                 }
471         }
472 }
473
474 #endif /* PAIREDENDQMODEL_H_ */