ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/tophat_cpp/inserts.h
Revision: 154
Committed: Tue Jan 24 02:29:21 2012 UTC (7 years, 8 months ago) by gpertea
File size: 4742 byte(s)
Log Message:
massive update with Daehwan's work

Line File contents
1 #ifndef INSERTS_H
2 #define INSERTS_H
3 /*
4 * inserts.h
5 * TopHat
6 *
7 * Created by Cole Trapnell on 1/14/09.
8 * Copyright 2009 Cole Trapnell. All rights reserved.
9 *
10 */
11
12 #include "bwt_map.h"
13
14 struct InsertAlignment
15 {
16 InsertAlignment(uint64_t _refid,
17 BowtieHit* _left_alignment,
18 BowtieHit* _right_alignment) :
19 refid(_refid),
20 left_alignment(_left_alignment),
21 right_alignment(_right_alignment) {}
22
23 uint64_t refid;
24 BowtieHit* left_alignment;
25 BowtieHit* right_alignment;
26 };
27
28 pair<int, int> pair_distances(const BowtieHit& h1, const BowtieHit& h2);
29
30 bool gap_lt(const pair<int, int>& lhs, const pair<int, int>& rhs);
31
32 struct InsertAlignmentGrade
33 {
34 InsertAlignmentGrade() :
35 too_close(false),
36 too_far(false),
37 num_spliced(0),
38 num_mapped(0),
39 opposite_strands(false),
40 consistent_splices(false),
41 longest_ref_skip(0x7FFFFu),
42 edit_dist(0x1F),
43 fusion(true),
44 inner_dist(99999999){}
45
46 InsertAlignmentGrade(const BowtieHit& h1, bool fusion = false) :
47 too_close(false),
48 too_far(false),
49 num_spliced(0),
50 num_mapped(0),
51 opposite_strands(false),
52 consistent_splices(false),
53 edit_dist(0x1F),
54 fusion(fusion),
55 num_alignments(0),
56 inner_dist(99999999)
57 {
58 if (!h1.contiguous())
59 num_spliced++;
60
61 num_mapped = 1;
62
63 longest_ref_skip = min(0x3FFFFu, (unsigned int)get_longest_ref_skip(h1) / 100);
64 edit_dist = h1.edit_dist();
65 num_alignments = 1;
66 }
67
68 InsertAlignmentGrade(const BowtieHit& h1,
69 const BowtieHit& h2,
70 int min_inner_distance,
71 int max_inner_distance,
72 bool fusion = false) :
73 too_close(false),
74 too_far(false),
75 num_spliced(0),
76 num_mapped(0),
77 opposite_strands(false),
78 consistent_splices(false),
79 edit_dist(0x1F),
80 fusion(fusion),
81 num_alignments(0)
82 {
83 pair<int, int> distances = pair_distances(h1, h2);
84 inner_dist = distances.second;
85
86 num_mapped = 2;
87
88 if (!h1.contiguous())
89 num_spliced++;
90 if (!h2.contiguous())
91 num_spliced++;
92
93 too_far = (inner_dist > max_inner_distance);
94 too_close = (inner_dist < min_inner_distance);
95 opposite_strands = (h1.antisense_align() != h2.antisense_align());
96 consistent_splices = (num_spliced == 2 && h1.antisense_splice() == h2.antisense_splice());
97
98 uint32_t ls = max(get_longest_ref_skip(h1), get_longest_ref_skip(h2));
99 longest_ref_skip = min (ls / 100, 0x3FFFFu);
100 edit_dist = h1.edit_dist() + h2.edit_dist();
101 num_alignments = 1;
102 assert(!(too_far && too_close));
103 }
104
105 InsertAlignmentGrade& operator=(const InsertAlignmentGrade& rhs)
106 {
107 too_close = rhs.too_close;
108 too_far = rhs.too_far;
109 num_spliced = rhs.num_spliced;
110 num_mapped = rhs.num_mapped;
111 opposite_strands = rhs.opposite_strands;
112 consistent_splices = rhs.consistent_splices;
113 longest_ref_skip = rhs.longest_ref_skip;
114 edit_dist = rhs.edit_dist;
115 fusion = rhs.fusion;
116 num_alignments = rhs.num_alignments;
117 inner_dist = rhs.inner_dist;
118 return *this;
119 }
120
121 static int get_longest_ref_skip(const BowtieHit& h1)
122 {
123 vector<pair<int, int> > gaps;
124 h1.gaps(gaps);
125 if (gaps.empty())
126 return 0;
127
128 vector<pair<int, int> >::iterator max_itr = max_element(gaps.begin(), gaps.end(), gap_lt);
129 return abs(max_itr->second - max_itr->first);
130 }
131
132 // Returns true if rhs is a "happier" alignment for the ends of this insert
133 // than this InsertStatus.
134 bool operator<(const InsertAlignmentGrade& rhs);
135
136 bool happy() const
137 {
138 return num_mapped == 2 && opposite_strands && (num_spliced != 2 || consistent_splices) && !too_far;
139 }
140
141 bool too_close;
142 bool too_far;
143
144 uint8_t num_spliced;
145 uint8_t num_mapped;
146
147 bool opposite_strands;
148 bool consistent_splices;
149 uint32_t longest_ref_skip; // in 100s of bp
150 unsigned char edit_dist;
151 bool fusion;
152 int num_alignments; // number of equally good alignments for the insert
153 int inner_dist; // distance between inner edges of mates
154 };
155
156 typedef vector<pair<InsertAlignmentGrade, vector<InsertAlignment> > > BestInsertAlignmentTable;
157 void accept_valid_hits(BestInsertAlignmentTable& best_status_for_inserts);
158 void accept_all_best_hits(BestInsertAlignmentTable& best_status_for_inserts);
159
160 void best_insert_mappings(uint64_t refid,
161 ReadTable& it,
162 HitList& hits1_in_ref,
163 HitList& hits2_in_ref,
164 BestInsertAlignmentTable& best_insert_alignments,
165 bool prefer_shorter_pairs = false);
166
167
168 void insert_best_pairings(RefSequenceTable& rt,
169 ReadTable& it,
170 HitTable& hits1,
171 HitTable& hits2,
172 BestInsertAlignmentTable& best_pairings,
173 bool prefer_shorter_pairs = false);
174 #endif