File: splice_site.h

package info (click to toggle)
hisat2 2.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,756 kB
  • sloc: cpp: 86,309; python: 12,230; sh: 2,171; perl: 936; makefile: 375
file content (615 lines) | stat: -rw-r--r-- 18,023 bytes parent folder | download | duplicates (2)
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*
 * Copyright 2013, Daehwan Kim <infphilo@gmail.com>
 *
 * This file is part of HISAT.
 *
 * HISAT is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * HISAT is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with HISAT.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef SPLICE_SITE_H_
#define SPLICE_SITE_H_

#include <stdint.h>
#include <iostream>
#include <fstream>
#include <limits>
#include "assert_helpers.h"
#include "mem_ids.h"
#include "ref_coord.h"
#include "ds.h"
#include "read.h"
#include "reference.h"
#include "hier_idx_common.h"
#include "gfm.h"
#include "alt.h"

enum {
    SPL_UNKNOWN = 1,
    SPL_FW,
    SPL_RC,
    SPL_SEMI_FW,
    SPL_SEMI_RC,
};

using namespace std;

// #define NEW_PROB_MODEL
// the following parameters are borrowed from Yeo and Burge 2004 in Journal of Computational Biology
const size_t donor_exonic_len = 3;
const size_t donor_intronic_len = 6;
const size_t donor_len = donor_exonic_len + donor_intronic_len;

#if defined(NEW_PROB_MODEL)
const size_t acceptor_intronic_len = 20;
const size_t acceptor_exonic_len = 3;
const size_t acceptor_len = acceptor_intronic_len + acceptor_exonic_len;
#else
// the following parameters are borrowed from Ch 3. in Bioinformatics - From Genomes to Drugs. Volume I: Basic Technologies (Victor Solovyev)
const size_t acceptor_intronic_len = 14;
const size_t acceptor_exonic_len = 1;
const size_t acceptor_len = acceptor_intronic_len + acceptor_exonic_len;
const size_t acceptor_len1 = acceptor_len / 2;
const size_t acceptor_len2 = acceptor_len - acceptor_len1;

// the following parameters are borrowed from Yeo and Burge 2004 in Journal of Computational Biology
const float background_prob[4] = {0.27f, 0.23f, 0.23f, 0.27f};

extern float donor_prob[4][donor_len];
extern float acceptor_prob[4][acceptor_len];

extern float donor_prob_sum[1 << (donor_len << 1)];
extern float acceptor_prob_sum1[1 << (acceptor_len1 << 1)];
extern float acceptor_prob_sum2[1 << (acceptor_len2 << 1)];

#endif
const size_t intronic_len = max<size_t>(donor_intronic_len, acceptor_intronic_len);

extern void init_junction_prob();

/**
 *
 */
class SpliceSitePos {
    
public:
    
	SpliceSitePos() { reset(); }
    
	SpliceSitePos(const SpliceSitePos& c) { init(c); }
	
	SpliceSitePos(uint32_t ref, uint32_t left, uint32_t right, uint8_t splDir, bool exon = false)
    {
        init(ref, left, right, splDir, exon);
    }
    
	/**
	 * Copy given fields into this Coord.
	 */
    void init(uint32_t ref, uint32_t left, uint32_t right, uint8_t splDir, bool exon = false) {
		_ref = ref;
		_left = left;
        _right = right;
		_splDir = splDir;
        _exon = exon;
	}
    
	/**
	 * Copy contents of given Coord into this one.
	 */
	void init(const SpliceSitePos& c) {
		_ref = c._ref;
		_left = c._left;
		_right = c._right;
        _splDir = c._splDir;
        _exon = c._exon;
	}
	
	/**
	 * Return true iff this Coord is identical to the given Coord.
	 */
	bool operator==(const SpliceSitePos& o) const {
		assert(inited());
		assert(o.inited());
		return _ref == o._ref &&
        _left == o._left &&
        _right == o._right &&
        _splDir == o._splDir &&
        _exon == o._exon;
        
	}
    
	/**
	 * Return true iff this Coord is less than the given Coord.  One Coord is
	 * less than another if (a) its reference id is less, (b) its orientation is
	 * less, or (c) its offset is less.
	 */
	bool operator<(const SpliceSitePos& o) const {
		if(_ref < o._ref) return true;
		if(_ref > o._ref) return false;
		if(_left < o._left) return true;
		if(_left > o._left) return false;
        if(_right < o._right) return true;
		if(_right > o._right) return false;
        if(_splDir < o._splDir) return true;
        if(_splDir > o._splDir) return false;
        if(_exon != o._exon) return _exon;
		return false;
	}
	
	/**
	 * Return the opposite result from operator<.
	 */
	bool operator>=(const SpliceSitePos& o) const {
		return !((*this) < o);
	}
	
	/**
	 * Return true iff this Coord is greater than the given Coord.  One Coord
	 * is greater than another if (a) its reference id is greater, (b) its
	 * orientation is greater, or (c) its offset is greater.
	 */
	bool operator>(const SpliceSitePos& o) const {
		if(_ref > o._ref) return true;
		if(_ref < o._ref) return false;
		if(_left > o._left) return true;
		if(_left < o._left) return false;
        if(_right > o._right) return true;
		if(_right < o._right) return false;
        if(_splDir > o._splDir) return true;
        if(_splDir < o._splDir) return false;
        if(_exon != o._exon) return !_exon;
		return false;
	}
	
	/**
	 * Return the opposite result from operator>.
	 */
	bool operator<=(const SpliceSitePos& o) const {
		return !((*this) > o);
	}
	
	/**
	 * Reset this coord to uninitialized state.
	 */
	virtual void reset() {
		_ref = std::numeric_limits<uint32_t>::max();
		_left = std::numeric_limits<uint32_t>::max();
        _right = std::numeric_limits<uint32_t>::max();
        _splDir = SPL_UNKNOWN;
        _exon = false;
	}
	
	/**
	 * Return true iff this Coord is initialized (i.e. ref and off have both
	 * been set since the last call to reset()).
	 */
	bool inited() const {
		if(_ref != std::numeric_limits<uint32_t>::max() &&
		   _left != std::numeric_limits<uint32_t>::max() &&
           _right != std::numeric_limits<uint32_t>::max())
		{
			return true;
		}
		return false;
	}
	
#ifndef NDEBUG
	/**
	 * Check that coord is internally consistent.
	 */
	bool repOk() const {
		if(_ref == std::numeric_limits<uint32_t>::max() ||
		   _left == std::numeric_limits<uint32_t>::max() ||
           _right == std::numeric_limits<uint32_t>::max()) {
            return false;
		}
		return true;
	}
#endif
	
	/**
	 * Check whether an interval defined by this coord and having
	 * length 'len' is contained within an interval defined by
	 * 'inbegin' and 'inend'.
	 */
#if 0
	bool within(int64_t len, int64_t inbegin, int64_t inend) const {
		return off_ >= inbegin && off_ + len <= inend;
	}
#endif
	
	uint32_t ref()          const { return _ref; }
	uint32_t left()         const { return _left; }
    uint32_t right()        const { return _right; }
	uint8_t  splDir()       const { return _splDir; }
    bool     canonical()    const { return _splDir == SPL_FW || _splDir == SPL_RC; }
    uint32_t intron_len()   const { return _right - _left - 1; }
    bool     exon() const { return _exon; }
    
protected:
    
	uint32_t  _ref;             // which reference?
	uint32_t  _left;            // 0-based offset of the right most base of the left flanking exon
    uint32_t  _right;           // 0-based offset of the left most base of the right flanking exon
    uint8_t   _splDir;
    bool      _exon;
};

/**
 *
 */
class SpliceSite : public SpliceSitePos {

public:

	SpliceSite() { reset(); }

	SpliceSite(const SpliceSite& c) { init(c); }
	
	SpliceSite(uint32_t ref,
               uint32_t left,
               uint32_t right,
               uint8_t splDir,
               bool exon = false,
               bool fromFile = false,
               bool known = false)
    {
        init(ref, left, right, splDir, exon, fromFile, known);
    }

	/**
	 * Copy given fields into this Coord.
	 */
	void init(uint32_t ref,
              uint32_t left,
              uint32_t right,
              uint8_t splDir,
              bool exon = false,
              bool fromFile = false,
              bool known = false)
    {
        SpliceSitePos::init(ref, left, right, splDir, exon);
    
        // _donordint = 0;
        // _acceptordint = 0;
        // _leftseq = 0;
        // _rightseq = 0;
        _leftext = 0;
        _rightext = 0;
        _numreads = 0;
        _editdist = 0;
        // _probscore = 0.0f;
        _readid = 0;
        _exon = exon;
        _fromfile = fromFile;
        _known = known;
	}

	/**
	 * Copy contents of given Coord into this one.
	 */
	void init(const SpliceSite& c) {
        SpliceSitePos::init(c);
        
        // _donordint = c._donordint;
        // _acceptordint = c._acceptordint;
        // _leftseq = c._leftseq;
        // _rightseq = c._rightseq;
        _leftext = c._leftext;
        _rightext = c._rightext;
        _numreads = c._numreads;
        _editdist = c._editdist;
        // _probscore = c._probscore;
        _readid = c._readid;
        _fromfile = c._fromfile;
        _known = c._known;
	}
	
	/**
	 * Reset this coord to uninitialized state.
	 */
	virtual void reset() {
        SpliceSitePos::reset();
        
        // _donordint = 0;
        // _acceptordint = 0;
        // _leftseq = 0;
        // _rightseq = 0;
        _leftext = 0;
        _rightext = 0;
        _numreads = 0;
        _editdist = 0;
        // _probscore = 0.0;
        _readid = 0;
        _fromfile = false;
        _known = false;
	}
	
    // uint8_t  donordint()    const { return _donordint; }
    // uint8_t  acceptordint() const { return _acceptordint; }
    // uint64_t leftseq()      const { return _leftseq; }
    // uint64_t rightseq()     const { return _rightseq; }
    
    uint32_t leftext()      const { return _leftext; }
    uint32_t rightext()     const { return _rightext; }
    
    uint64_t numreads()     const { return _numreads; }
    uint32_t editdist()     const { return _editdist; }
    // float    probscore()    const { return _probscore; }

public:

    // uint8_t   _donordint;       // 3' dinucleotide on Watson strand
    // uint8_t   _acceptordint;    // 5' dinucleotide on Watson strand
    // uint64_t  _leftseq;         // left 32bp flanking seq on Watson strand
    // uint64_t  _rightseq;        // right 32bp flanking seq on Watson strand
    
    uint32_t  _leftext;
    uint32_t  _rightext;
    
    uint64_t  _numreads;        // number of reads spanning this splice site
    
    uint32_t  _editdist;
    
    // float     _probscore;
    
    uint64_t _readid;
    bool     _fromfile;
    bool     _known;
};

std::ostream& operator<<(std::ostream& out, const SpliceSite& c);

/**
 *
 */
class Exon {
public:
    
    Exon() { reset(); }
    
    Exon(const Exon& c) { init(c); }
    
    Exon(uint32_t ref, uint32_t left, uint32_t right, bool fw)
    {
        init(ref, left, right, fw);
    }
    
    /**
     * Copy given fields into this Coord.
     */
    void init(uint32_t ref, uint32_t left, uint32_t right, bool fw) {
        _ref = ref;
        _left = left;
        _right = right;
        _fw = fw;
    }
    
    /**
     * Copy contents of given Coord into this one.
     */
    void init(const Exon& c) {
        _ref = c._ref;
        _left = c._left;
        _right = c._right;
        _fw = c._fw;
    }
    
    /**
     * Return true iff this Coord is identical to the given Coord.
     */
    bool operator==(const Exon& o) const {
        assert(inited());
        assert(o.inited());
        return _ref == o._ref &&
        _left == o._left &&
        _right == o._right &&
        _fw == o._fw;
    }
    
    /**
     * Return true iff this Coord is less than the given Coord.  One Coord is
     * less than another if (a) its reference id is less, (b) its orientation is
     * less, or (c) its offset is less.
     */
    bool operator<(const Exon& o) const {
        if(_ref < o._ref) return true;
        if(_ref > o._ref) return false;
        if(_left < o._left) return true;
        if(_left > o._left) return false;
        if(_right < o._right) return true;
        if(_right > o._right) return false;
        if(_fw != o._fw) return _fw;
        return false;
    }
    
    /**
     * Return the opposite result from operator<.
     */
    bool operator>=(const Exon& o) const {
        return !((*this) < o);
    }
    
    /**
     * Return true iff this Coord is greater than the given Coord.  One Coord
     * is greater than another if (a) its reference id is greater, (b) its
     * orientation is greater, or (c) its offset is greater.
     */
    bool operator>(const Exon& o) const {
        if(_ref > o._ref) return true;
        if(_ref < o._ref) return false;
        if(_left > o._left) return true;
        if(_left < o._left) return false;
        if(_right > o._right) return true;
        if(_right < o._right) return false;
        if(_fw != o._fw) return !_fw;
        return false;
    }
    
    /**
     * Return the opposite result from operator>.
     */
    bool operator<=(const Exon& o) const {
        return !((*this) > o);
    }
    
    /**
     * Reset this coord to uninitialized state.
     */
    void reset() {
        _ref = std::numeric_limits<uint32_t>::max();
        _left = std::numeric_limits<uint32_t>::max();
        _right = std::numeric_limits<uint32_t>::max();
        _fw = true;
    }
    
    /**
     * Return true iff this Coord is initialized (i.e. ref and off have both
     * been set since the last call to reset()).
     */
    bool inited() const {
        if(_ref != std::numeric_limits<uint32_t>::max() &&
           _left != std::numeric_limits<uint32_t>::max() &&
           _right != std::numeric_limits<uint32_t>::max())
        {
            return true;
        }
        return false;
    }
    
#ifndef NDEBUG
    /**
     * Check that coord is internally consistent.
     */
    bool repOk() const {
        if(_ref == std::numeric_limits<uint32_t>::max() ||
           _left == std::numeric_limits<uint32_t>::max() ||
           _right == std::numeric_limits<uint32_t>::max()) {
            return false;
        }
        return true;
    }
#endif
    
    uint32_t ref()          const { return _ref; }
    uint32_t left()         const { return _left; }
    uint32_t right()        const { return _right; }
    bool     fw()           const { return _fw; }
    
protected:
    uint32_t  _ref;             // which reference?
    uint32_t  _left;            // 0-based offset of the right most base of the left flanking exon
    uint32_t  _right;           // 0-based offset of the left most base of the right flanking exon
    bool      _fw;              // true -> Watson strand
};

class AlnRes;

class SpliceSiteDB {
public:
    typedef RedBlackNode<SpliceSitePos, uint32_t> Node;
    
public:
    SpliceSiteDB(
                 const BitPairReference& refs,
                 const EList<string>& refnames,
                 bool threadSafe = true,
                 bool write = false,
                 bool read = false);
    ~SpliceSiteDB();
    
    bool addSpliceSite(
                       const Read& rd,
                       const AlnRes& rs,
                       uint32_t minAnchorLen = 15);
    
    static float probscore(
                           int64_t donor_seq,
                           int64_t acceptor_seq);
    
    size_t size(uint64_t ref) const;
    bool empty(uint64_t ref) const;
    
    bool empty() { return _empty; }
    
    bool write() const { return _write; }
    bool read() const { return _read; }
    
    bool getSpliceSite(SpliceSite& ss) const;
    void getLeftSpliceSites(uint32_t ref, uint32_t left, uint32_t range, EList<SpliceSite>& spliceSites) const;
    void getRightSpliceSites(uint32_t ref, uint32_t right, uint32_t range, EList<SpliceSite>& spliceSites) const;
    bool hasSpliceSites(uint32_t ref, uint32_t left1, uint32_t right1, uint32_t left2, uint32_t right2, bool includeNovel = false) const;
    bool insideExon(uint32_t ref, uint32_t left, uint32_t right) const;
    
    void print(ofstream& out);
    void read(const GFM<TIndexOffU>& gfm, const EList<ALT<TIndexOffU> >& alts);
    void read(ifstream& in, bool known = false);
    
private:
    void getSpliceSites_recur(
                              const RedBlackNode<SpliceSitePos, uint32_t> *node,
                              uint32_t left,
                              uint32_t right,
                              EList<SpliceSite>& spliceSites) const;
    
    bool hasSpliceSites_recur(
                              const RedBlackNode<SpliceSitePos, uint32_t> *node,
                              uint32_t left,
                              uint32_t right,
                              bool includeNovel) const;    
    
    const RedBlackNode<SpliceSitePos, uint32_t>* getSpliceSite_temp(const SpliceSitePos& ssp) const;
    
    void print_recur(
                     const RedBlackNode<SpliceSitePos, uint32_t> *node,
                     ofstream& out,
                     const uint32_t numreads_cutoff,
                     const uint32_t numreads_cutoff2,
                     EList<SpliceSite>& ss_list);
    
    Pool& pool(uint64_t ref);
    
    void print_impl(
                    ofstream& out,
                    EList<SpliceSite>& ss_list,
                    const SpliceSite* ss = NULL);
    
private:
    uint64_t                            _numRefs;
    EList<string>                       _refnames;
    
    EList<RedBlack<SpliceSitePos, uint32_t>* >   _fwIndex;
    EList<RedBlack<SpliceSitePos, uint32_t>* >   _bwIndex;
    
    ELList<SpliceSite>                   _spliceSites;
    
    ELList<Pool*>                        _pool;   // dispenses memory pages
    
    bool                                _write;
    bool                                _read;
    
    EList<MUTEX_T>                      _mutex;
    bool                                _threadSafe;
    
    SStringExpandable<char>             raw_refbuf;
    ASSERT_ONLY(SStringExpandable<uint32_t> destU32);
    BTDnaString                         donorstr;
    BTDnaString                         acceptorstr;
    
    bool                                _empty;
    
    EList<Exon>                         _exons;
};

#endif /*ifndef SPLICE_SITE_H_*/