annotate PsiCLASS-1.0.2/Constraints.hpp @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 #ifndef _MOURISL_CLASSES_CONSTRAINTS_HEADER
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2 #define _MOURISL_CLASSES_CONSTRAINTS_HEADER
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 #include <vector>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 #include <map>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 #include <algorithm>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 #include <string>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 #include <string.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 #include "BitTable.hpp"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 #include "alignments.hpp"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 #include "SubexonGraph.hpp"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 struct _constraint
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 BitTable vector ; // subexon vector
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 double weight ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 double normAbund ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 double abundance ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 int support ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 int uniqSupport ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 int maxReadLen ; // the longest read length support this constraint.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 int info ; // other usages.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 int first, last ; // indicate the first and last index of the subexons.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 } ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 struct _matePairConstraint
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 int i, j ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 int support ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 int uniqSupport ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 double abundance ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 double normAbund ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 int effectiveCount ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 int type ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 } ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 struct _readIdHeap
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 char *readId ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 int pos ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 int matePos ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 int idx ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 } ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 //----------------------------------------------------------------------------------
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 // We assume the access to the data structure is sorted by matePos.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 // So we can "pre-cache" the ids with the same matePos.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 class MateReadIds
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 private:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 std::vector< struct _readIdHeap > heap ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 void HeapifyUp( int tag )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 while ( tag > 1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 if ( heap[tag / 2].matePos < heap[tag].matePos )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 return ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 struct _readIdHeap tmp ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 tmp = heap[tag / 2] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 heap[tag / 2] = heap[tag] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 heap[tag] = tmp ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 tag /= 2 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 void HeapifyDown( int tag )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 int size = heap.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 while ( 2 * tag < size )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 int choose = 2 * tag ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 if ( 2 * tag + 1 < size &&
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 heap[ 2 * tag + 1].matePos < heap[2 * tag ].matePos )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 choose = 2 * tag + 1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 if ( heap[tag].matePos < heap[choose].matePos )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 return ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 struct _readIdHeap tmp ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 tmp = heap[choose] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 heap[choose] = heap[tag] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 heap[tag] = tmp ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 tag = choose ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 struct _readIdHeap Pop()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 struct _readIdHeap ret ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 int size = heap.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 if ( size < 2 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 ret.readId = NULL ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 return ret ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 ret = heap[1] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 heap[1] = heap[ heap.size() - 1] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 heap.pop_back() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 HeapifyDown( 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 return ret ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 int cachedMatePos ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 std::map<std::string, int> cachedIdx ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 bool hasMateReadIdSuffix ; // ignore the last ".{1,2}" or "/{1,2}" .
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 public:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 MateReadIds()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 // Push a dummy element so the vector becomes 1-based.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 struct _readIdHeap nh ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 nh.readId = NULL ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 nh.pos = nh.idx = nh.matePos = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 heap.push_back( nh ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 cachedMatePos = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 hasMateReadIdSuffix = false ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 ~MateReadIds()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 int size = heap.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 std::map<std::string, int>().swap( cachedIdx ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 for ( int i = 0 ; i < size ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 if ( heap[i].readId != NULL )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 free( heap[i].readId ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 void Clear()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 std::map<std::string, int>().swap( cachedIdx ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 int size = heap.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 for ( int i = 0 ; i < size ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 if ( heap[i].readId != NULL )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 free( heap[i].readId ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 std::vector<struct _readIdHeap>().swap( heap ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 struct _readIdHeap nh ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 nh.readId = NULL ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 nh.pos = nh.idx = nh.matePos = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 heap.push_back( nh ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 cachedMatePos = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 void Insert( char *id, int pos, int idx, int matePos )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 struct _readIdHeap nh ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 nh.readId = strdup( id ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 nh.pos = pos ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 nh.idx = idx ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 nh.matePos = matePos ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 heap.push_back( nh ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 HeapifyUp( heap.size() - 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 // If the id does not exist, return -1.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 int Query( char *id, int matePos )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 int size ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 size = heap.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 if ( matePos > cachedMatePos )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 std::map<std::string, int>().swap( cachedIdx ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 while ( size >= 2 && heap[1].matePos < matePos )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 struct _readIdHeap r = Pop() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 if ( r.readId )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 free( r.readId ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 --size ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 while ( size >= 2 && heap[1].matePos == matePos )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 struct _readIdHeap r = Pop() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 cachedIdx[ std::string( r.readId ) ] = r.idx ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190 if ( r.readId )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 free( r.readId ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 --size ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 cachedMatePos = matePos ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196 std::string s( id ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 if ( hasMateReadIdSuffix )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199 int len = s.length() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 if ( len >= 2 && ( s[len - 1] == '1' || s[len - 1] == '2' )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201 && ( s[len - 2] == '.' || s[len - 2] == '/' ) )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203 s[len - 1] = '2' - s[len - 1] + '1' ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207 if ( cachedIdx.find( s ) != cachedIdx.end() )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 return cachedIdx[s] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211 return -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
213
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
214 void UpdateIdx( std::vector<int> &newIdx )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
215 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
216 int size = heap.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
217 int i ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
218 for ( i = 1 ; i < size ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
219 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
220 heap[i].idx = newIdx[ heap[i].idx ] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
221 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
222
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
223 for ( std::map<std::string, int>::iterator it = cachedIdx.begin() ; it != cachedIdx.end() ; ++it )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
224 it->second = newIdx[ it->second ] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
225 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
226
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
227 void SetHasMateReadIdSuffix( bool in )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
228 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
229 hasMateReadIdSuffix = true ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
230 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
231 } ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
232
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
233
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
234 //--------------------------------------------------------------------------
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
235 class Constraints
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
236 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
237 private:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
238 int prevStart, prevEnd ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
239 bool usePrimaryAsUnique ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
240 MateReadIds mateReadIds ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
241
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
242 Alignments *pAlignments ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
243
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
244 //@return: whether this alignment is compatible with the subexons or not.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
245 bool ConvertAlignmentToBitTable( struct _pair *segments, int segCnt, struct _subexon *subexons, int seCnt, int seStart, struct _constraint &ct ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
246
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
247 // Sort to increasing order. Since the first subexon occupies the least important digit.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
248 static bool CompSortConstraints( const struct _constraint &a, const struct _constraint &b )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
249 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
250 //int k
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
251 if ( a.first < b.first )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
252 return true ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
253 else if ( a.first > b.first )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
254 return false ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
255
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
256 int diffPos = a.vector.GetFirstDifference( b.vector ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
257 if ( diffPos == -1 ) // case of equal.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
258 return false ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
259
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
260 if ( a.vector.Test( diffPos ))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
261 return false ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
262 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
263 return true ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
264 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
265
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
266 static bool CompSortMatePairs( const struct _matePairConstraint &a, const struct _matePairConstraint &b )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
267 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
268 if ( a.i < b.i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
269 return true ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
270 else if ( a.i > b.i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
271 return false ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
272 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
273 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
274 if ( a.j < b.j )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
275 return true ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
276 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
277 return false ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
278 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
279 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
280
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
281 void CoalesceSameConstraints() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
282 void ComputeNormAbund( struct _subexon *subexons ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
283 public:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
284 std::vector<struct _constraint> constraints ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
285 std::vector<struct _matePairConstraint> matePairs ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
286
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
287 Constraints()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
288 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
289 usePrimaryAsUnique = false ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
290 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
291
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
292 Constraints( Alignments *a ): pAlignments( a )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
293 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
294 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
295
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
296 ~Constraints()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
297 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
298 int i ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
299 int size = constraints.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
300 for ( i = 0 ; i < size ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
301 constraints[i].vector.Release() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
302 constraints.clear() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
303 std::vector<struct _constraint>().swap( constraints ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
304 matePairs.clear() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
305 std::vector<struct _matePairConstraint>().swap( matePairs ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
306 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
307
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
308 void Clear()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
309 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
310 //TODO: do I need to release the memory from BitTable?
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
311 constraints.clear() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
312 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
313
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
314 void SetAlignments( Alignments *a )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
315 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
316 pAlignments = a ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
317 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
318
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
319 void SetUsePrimaryAsUnique( bool in )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
320 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
321 usePrimaryAsUnique = in ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
322 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
323
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
324 void Assign( Constraints &c )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
325 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
326 int i ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
327 int size = constraints.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
328 if ( size > 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
329 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
330 for ( i = 0 ; i < size ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
331 constraints[i].vector.Release() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
332 constraints.clear() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
333 std::vector<struct _constraint>().swap( constraints ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
334 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
335 matePairs.clear() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
336 std::vector<struct _matePairConstraint>().swap( matePairs ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
337
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
338 //constraints.resize( c.constraints.size() ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
339 constraints = c.constraints ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
340 size = c.constraints.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
341 for ( i = 0 ; i < size ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
342 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
343 /*struct _constraint nc ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
344 nc.weight = c.constraints[i].weight ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
345 nc.normAbund = c.constraints[i].normAbund ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
346 nc.abundance = c.constraints[i].abundance ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
347 nc.support = c.constraints[i].support ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
348 nc.uniqSupport = c.constraints[i].uniqSupport ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
349 nc.maxReadLen = c.constraints[i].maxReadLen ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
350 nc.info = c.constraints[i].info ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
351 nc.first = c.constraints[i].first ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
352 nc.last = c.constraints[i].last ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
353 nc.vector.Duplicate( c.constraints[i].vector ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
354 constraints[i] = ( nc ) ; */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
355 constraints[i].vector.Nullify() ; // so that it won't affect the BitTable in "c"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
356 constraints[i].vector.Duplicate( c.constraints[i].vector ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
357 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
358 matePairs = c.matePairs ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
359 pAlignments = c.pAlignments ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
360 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
361
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
362 void DownsampleConstraintsFrom( Constraints &c, int stride = 10 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
363 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
364 int i ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
365 int size = constraints.size(), k ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
366
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
367 if ( size > 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
368 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
369 for ( i = 0 ; i < size ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
370 constraints[i].vector.Release() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
371 constraints.clear() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
372 std::vector<struct _constraint>().swap( constraints ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
373 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
374 matePairs.clear() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
375 std::vector<struct _matePairConstraint>().swap( matePairs ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
376
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
377 //constraints.resize( c.constraints.size() ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
378 //constraints = c.constraints ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
379 k = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
380 size = c.constraints.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
381 for ( i = 0 ; i < size ; i += stride, ++k )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
382 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
383 constraints.push_back( c.constraints[i] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
384 constraints[k].vector.Nullify() ; // so that it won't affect the BitTable in "c"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
385 constraints[k].vector.Duplicate( c.constraints[i].vector ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
386
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
387 /*std::vector<int> seIdx ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
388 constraints[k].vector.GetOnesIndices( seIdx ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
389 int j, l = seIdx.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
390 for ( j = 2 ; j < l ; ++j )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
391 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
392 constraints[k].vector.Unset( seIdx[j] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
393 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
394 constraints[k].last = seIdx[1] ;*/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
395 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
396 // mate pairs is not used. if we down-sampling
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
397 pAlignments = c.pAlignments ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
398 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
399
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
400 void TruncateConstraintsCoverFrom( Constraints &c, int seCnt, int maxConstraintSize )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
401 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
402 int i ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
403 int size = constraints.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
404
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
405 if ( size > 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
406 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
407 for ( i = 0 ; i < size ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
408 constraints[i].vector.Release() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
409 constraints.clear() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
410 std::vector<struct _constraint>().swap( constraints ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
411 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
412 matePairs.clear() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
413 std::vector<struct _matePairConstraint>().swap( matePairs ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
414
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
415 //constraints.resize( c.constraints.size() ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
416 //constraints = c.constraints ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
417 size = c.constraints.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
418 for ( i = 0 ; i < size ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
419 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
420 constraints.push_back( c.constraints[i] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
421 constraints[i].vector.Nullify() ; // so that it won't affect the BitTable in "c"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
422 constraints[i].vector.Init( seCnt ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
423 std::vector<int> seIdx ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
424 c.constraints[i].vector.GetOnesIndices( seIdx ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
425 int j, l = seIdx.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
426 for ( j = 0 ; j < maxConstraintSize && j < l ; ++j )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
427 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
428 constraints[i].vector.Set( seIdx[j] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
429 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
430 constraints[i].last = seIdx[j - 1] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
431 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
432 // mate pairs is not used. if we down-sampling
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
433 pAlignments = c.pAlignments ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
434 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
435
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
436 void SetHasMateReadIdSuffix( bool in )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
437 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
438 mateReadIds.SetHasMateReadIdSuffix( in ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
439 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
440
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
441 int BuildConstraints( struct _subexon *subexons, int seCnt, int start, int end ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
442
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
443 } ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
444
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
445 #endif