annotate PsiCLASS-1.0.2/SubexonInfo.cpp @ 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 // report the total depth for the segment
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2 // Format: ./a.out alignments.bam splice_site
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 #include <stdio.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 #include <string.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 #include <stdlib.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 #define __STDC_FORMAT_MACROS
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 #include <inttypes.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 #include <algorithm>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 #include <vector>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 #include <math.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 #include "alignments.hpp"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 #include "blocks.hpp"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 #include "stats.hpp"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 #define ABS(x) ((x)<0?-(x):(x))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 char usage[] = "./subexon-info alignment.bam intron.splice [options]\n"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 "options:\n"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 "\t--minDepth INT: the minimum coverage depth considered as part of a subexon (default: 2)\n"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 "\t--noStats: do not compute the statistical scores (default: not used)\n" ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 char buffer[4096] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 int gMinDepth ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 bool CompSplitSite( struct _splitSite a, struct _splitSite b )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 if ( a.chrId < b.chrId )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 return true ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 else if ( a.chrId > b.chrId )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 return false ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 else if ( a.pos != b.pos )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 return a.pos < b.pos ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 else if ( a.type != b.type )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 return a.type < b.type ; // We want the start of exons comes first, since we are scanning the genome from left to right,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 // so we can terminate the extension early and create a single-base subexon later.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 return a.oppositePos < b.oppositePos ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 bool CompBlocksByAvgDepth( struct _block a, struct _block b )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 double avgA = a.depthSum / (double)( a.end - a.start + 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 double avgB = b.depthSum / (double)( b.end - b.start + 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 return avgA < avgB ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 bool CompBlocksByRatio( struct _block a, struct _block b )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 return a.ratio < b.ratio ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 int CompDouble( const void *p1, const void *p2 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 double a = *(double *)p1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 double b = *(double *)p2 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 if ( a > b )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 return 1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 else if ( a < b )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 return -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 return 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67 // Clean up the split sites;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 void FilterAndSortSplitSites( std::vector<struct _splitSite> &sites )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 std::sort( sites.begin(), sites.end(), CompSplitSite ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 int i, j, k, l ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 int size = sites.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 for ( i = 0 ; i < size ; )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 for ( j = i + 1 ; j < size ; ++j )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 if ( sites[j].chrId != sites[i].chrId || sites[j].pos != sites[i].pos || sites[j].type != sites[i].type )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 int maxSupport = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 for ( k = i ; k < j ; ++k )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 if ( sites[k].support > maxSupport )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 maxSupport = sites[k].support ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 int strandCnt[2] = {0, 0} ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 char strand = sites[i].strand ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 for ( k = i ; k < j ; ++k )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 if ( sites[k].strand == '-' )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 strandCnt[0] += sites[k].support ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 else if ( sites[k].strand == '+' )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 strandCnt[1] += sites[k].support ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 if ( strandCnt[0] > strandCnt[1] )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 strand = '-' ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 else if ( strandCnt[1] > strandCnt[0] )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 strand = '+' ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 bool allOneExceptMax = false ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 if ( maxSupport >= 20 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 allOneExceptMax = true ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 for ( k = i ; k < j ; ++k )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 if ( sites[k].support == maxSupport )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 continue ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 if ( sites[k].support > 1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 allOneExceptMax = false ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 for ( k = i ; k < j ; ++k )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 if ( ( sites[k].support < 0.01 * maxSupport && sites[k].support <= 3 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 || sites[k].support < 0.001 * maxSupport || sites[k].strand != strand // The introns from the different strand are filtered.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 || ( sites[k].support < 0.02 * maxSupport && sites[k].mismatchSum >= 2 * sites[k].support )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 || ( allOneExceptMax && sites[k].support == 1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 || ( sites[k].support <= 2 && sites[k].mismatchSum >= 2 * sites[k].support )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 || ( maxSupport >= 2 && sites[k].support == 1 && ( ABS( sites[k].oppositePos - sites[k].pos - 1 ) >= 10000 || sites[k].mismatchSum != 0 ) ) )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 for ( l = i - 1 ; l >= 0 && sites[l].chrId == sites[i].chrId && sites[l].pos >= sites[k].oppositePos ; --l )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 if ( sites[l].pos == sites[k].oppositePos && sites[l].oppositePos == sites[k].pos )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 sites[l].support = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 sites[k].support = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 for ( l = j ; l < size && sites[l].chrId == sites[i].chrId && sites[l].pos <= sites[k].oppositePos ; ++l )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 if ( sites[l].pos == sites[k].oppositePos && sites[l].oppositePos == sites[k].pos )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 sites[l].support = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 sites[k].support = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 /*else if ( sites[k].support <= 1 && sites[k].oppositePos - sites[k].pos + 1 >= 30000 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 for ( l = j ; l < size && sites[l].chrId == sites[i].chrId && sites[l].pos <= sites[k].oppositePos ; ++l )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 if ( sites[l].pos == sites[k].oppositePos && sites[l].oppositePos == sites[k].pos )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 if ( l - j >= 20 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 sites[l].support = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 sites[k].support = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 }*/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 i = j ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 k = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 for ( i = 0 ; i < size ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 if ( sites[i].support > 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 sites[k] = sites[i] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 if ( sites[k].strand == '?' )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 sites[k].strand = '.' ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 ++k ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 sites.resize( k ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 // Remove the same sites.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 void KeepUniqSplitSites( std::vector< struct _splitSite> &sites )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 int i, j ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 int size = sites.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 int k = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 for ( i = 0 ; i < size ; )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 for ( j = i + 1 ; j < size ; ++j )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 if ( sites[j].chrId != sites[i].chrId || sites[j].pos != sites[i].pos || sites[j].type != sites[i].type )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 sites[k] = sites[i] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190 ++k ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 /*else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193 if ( sites[i].type != sites[k-1].type )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 printf( "%d\n", sites[i].pos ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 }*/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 i = j ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 sites.resize( k ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 // For the sites that corresponds to the start of an exon, we remove the adjust to it and
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203 /*for ( i = 1 ; i < size ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 if ( sites[i].pos == sites[i - 1].pos && sites[i - 1].type == 2 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207 if ( sites[i].type == 1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208 ++sites[i].pos ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210 }*/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
213 // Filter split sites that are extremely close to each other but on different strand.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
214 void FilterNearSplitSites( std::vector< struct _splitSite> &sites )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
215 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
216 int i, j ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
217 int size = sites.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
218 int k = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
219 for ( i = 0 ; i < size - 1 ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
220 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
221 if ( sites[i].support < 0 || sites[i].type != sites[i + 1].type || sites[i].chrId != sites[i + 1].chrId )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
222 continue ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
223 if ( sites[i + 1].pos - sites[i].pos <= 7 &&
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
224 ( sites[i + 1].strand != sites[i].strand ||
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
225 sites[i].strand == '?' ) )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
226 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
227 int tag = i ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
228 if ( sites[i + 1].support < sites[i].support )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
229 tag = i + 1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
230 sites[tag].support = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
231 int direction ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
232 if ( sites[tag].oppositePos < sites[tag].pos )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
233 direction = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
234 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
235 direction = 1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
236
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
237 for ( j = tag ; j >= 0 && j < size ; j += direction )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
238 if ( sites[j].pos == sites[tag].oppositePos && sites[j].oppositePos == sites[tag].pos )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
239 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
240 sites[j].support = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
241 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
242 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
243 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
244 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
245
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
246 for ( i = 0 ; i < size ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
247 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
248 if ( sites[i].support > 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
249 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
250 sites[k] = sites[i] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
251 ++k ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
252 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
253 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
254 sites.resize( k ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
255 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
256
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
257 void FilterRepeatSplitSites( std::vector<struct _splitSite> &sites )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
258 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
259 int i, j ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
260 int size = sites.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
261 int k = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
262 for ( i = 0 ; i < size ; )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
263 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
264 for ( j = i + 1 ; j < size ; ++j )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
265 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
266 if ( sites[j].pos != sites[i].pos || sites[j].type != sites[i].type || sites[i].chrId != sites[j].chrId )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
267 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
268 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
269 int max = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
270 int maxtag = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
271 for ( k = i ; k < j ; ++k )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
272 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
273 if ( sites[k].uniqSupport > max )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
274 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
275 max = sites[k].uniqSupport ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
276 maxtag = k ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
277 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
278 else if ( sites[k].uniqSupport == max && sites[k].support > sites[maxtag].support )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
279 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
280 maxtag = k ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
281 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
282 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
283
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
284 if ( max > -1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
285 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
286 if ( sites[maxtag].uniqSupport > sites[maxtag].support * 0.1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
287 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
288 for ( k = i ; k < j ; ++k )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
289 if ( sites[k].uniqSupport < 0.05 * sites[k].support )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
290 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
291 sites[k].support = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
292
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
293 int direction ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
294 if ( sites[k].oppositePos < sites[k].pos )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
295 direction = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
296 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
297 direction = 1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
298 int l ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
299 for ( l = k ; l >= 0 && l < size ; l += direction )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
300 if ( sites[l].pos == sites[k].oppositePos && sites[l].oppositePos == sites[k].pos )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
301 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
302 sites[l].support = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
303 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
304 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
305 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
306 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
307 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
308 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
309 for ( k = i ; k < j ; ++k )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
310 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
311 if ( sites[k].support <= 10 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
312 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
313 sites[k].support = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
314
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
315 int direction ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
316 if ( sites[k].oppositePos < sites[k].pos )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
317 direction = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
318 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
319 direction = 1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
320 int l ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
321 for ( l = k ; l >= 0 && l < size ; l += direction )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
322 if ( sites[l].pos == sites[k].oppositePos && sites[l].oppositePos == sites[k].pos )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
323 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
324 sites[l].support = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
325 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
326 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
327 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
328 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
329 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
330 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
331
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
332 i = j ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
333 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
334
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
335 k = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
336 for ( i = 0 ; i < size ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
337 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
338 if ( sites[i].support > 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
339 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
340 sites[k] = sites[i] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
341 ++k ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
342 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
343 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
344 sites.resize( k ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
345
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
346 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
347
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
348
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
349 // When k=1, the gamma distribution becomes exponential distribution, and can be optimized analytically..
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
350 // Maximize: sum_i z_i( log( 1/theta e^{-x_i / theta} )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
351 /*double ThetaOfExponentialDistribution( double *x, double *z, int n )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
352 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
353 double sumZ = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
354 double sumZX = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
355 for ( i = 0 ; i < n ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
356 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
357 sumZ += z[i] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
358 sumZX += z[i] * x[i] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
359 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
360 }*/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
361
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
362 // for boundK, if it is positive, it represent the upper bound. If it is negative, -boundK will be the lower bound for k.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
363 // if boundK==0, there is no extra bound.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
364 // The same logic for boundProduct, which bounds k*theta
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
365 void GradientDescentGammaDistribution( double &k, double &theta, double initK, double initTheta, double lowerBoundK, double upperBoundK,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
366 double lowerBoundMean, double upperBoundMean, double *x, double *z, int n )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
367 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
368 int i ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
369 k = initK ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
370 theta = initTheta ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
371 double c = 0.5 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
372 int iterCnt = 1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
373
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
374 double sumZ = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
375 double sumZX = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
376 double sumZLogX = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
377 double Hessian[2][2] ; // 0 for k, 1 for theta
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
378 double inverseHessian[2][2] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
379 int tmp ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
380
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
381 double positiveKRecord = -5, positiveThetaRecord = -5 ; // record the value of k, theta when theta, k becomes non-positive.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
382
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
383 for ( i = 0 ; i < n ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
384 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
385 sumZ += z[i] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
386 sumZX += z[i] * x[i] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
387 sumZLogX += z[i] * log( x[i] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
388 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
389
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
390 while ( 1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
391 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
392 double gradK = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
393 double gradTheta = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
394
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
395 double prevK = k ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
396 double prevTheta = theta ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
397 double digammaK = digammal( k ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
398
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
399 gradK = sumZ * ( -log( theta ) - digammaK ) + sumZLogX ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
400 gradTheta = -sumZ * ( k / theta ) + sumZX / ( theta * theta ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
401
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
402 Hessian[0][0] = -sumZ * trigamma( k, &tmp ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
403 Hessian[0][1] = -sumZ / theta ; // \partial l / ( \partial k \partial theta)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
404 Hessian[1][0] = -sumZ / theta ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
405 Hessian[1][1] = sumZ * k / ( theta * theta ) - 2 * sumZX / ( theta * theta * theta ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
406
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
407 double det = Hessian[0][0] * Hessian[1][1] - Hessian[0][1] * Hessian[1][0] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
408 /*printf( "%s iter %d:\n", __func__, iterCnt ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
409 printf( "%lf %lf %lf %lf\n", sumZ, k, theta, sumZX ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
410 printf( "%lf %lf %lf\n", gradK, gradTheta, det ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
411 printf( "%lf %lf %lf %lf\n", Hessian[0][0], Hessian[0][1], Hessian[1][0], Hessian[1][1] ) ;*/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
412 if ( det <= 1e-4 && det >=-1e-4 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
413 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
414 k = k + c / iterCnt * gradK ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
415 theta = theta + c / iterCnt * gradTheta ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
416 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
417 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
418 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
419 inverseHessian[0][0] = Hessian[1][1] / det ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
420 inverseHessian[0][1] = -Hessian[0][1] / det ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
421 inverseHessian[1][0] = -Hessian[1][0] / det ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
422 inverseHessian[1][1] = Hessian[0][0] / det ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
423 //printf( "%lf %lf %lf %lf: %lf\n=====\n", inverseHessian[0][0], inverseHessian[0][1], inverseHessian[1][0], inverseHessian[1][1],
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
424 // Hessian[1][0] * inverseHessian[0][1] + Hessian[1][1] * inverseHessian[1][1] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
425 double step = 0.5 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
426 k = k - step * ( inverseHessian[0][0] * gradK + inverseHessian[0][1] * gradTheta ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
427 theta = theta - step * ( inverseHessian[1][0] * gradK + inverseHessian[1][1] * gradTheta ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
428
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
429 bool flag = false ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
430 if ( k <= 1e-6 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
431 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
432 step = ( prevK - 1e-6 ) / ( inverseHessian[0][0] * gradK + inverseHessian[0][1] * gradTheta ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
433
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
434 if ( ABS( theta - positiveThetaRecord ) < 1e-5 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
435 flag = true ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
436 positiveThetaRecord = theta ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
437 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
438 if ( theta <= 1e-6 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
439 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
440 double tmp = ( prevTheta - 1e-6 ) / ( inverseHessian[1][0] * gradK + inverseHessian[1][1] * gradTheta ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
441 if ( tmp < step )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
442 step = tmp ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
443
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
444 if ( ABS( k - positiveKRecord ) < 1e-5 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
445 flag = true ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
446 positiveKRecord = k ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
447 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
448
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
449 if ( step != 0.5 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
450 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
451 k = prevK - step * ( inverseHessian[0][0] * gradK + inverseHessian[0][1] * gradTheta ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
452 theta = prevTheta - step * ( inverseHessian[1][0] * gradK + inverseHessian[1][1] * gradTheta ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
453 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
454
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
455 /*if ( flag )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
456 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
457 k = prevK ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
458 theta = prevTheta ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
459 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
460 }*/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
461 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
462
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
463 if ( upperBoundK > 0 && k > upperBoundK )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
464 k = upperBoundK ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
465 else if ( lowerBoundK > 0 && k < lowerBoundK )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
466 k = lowerBoundK ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
467
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
468 if ( upperBoundMean > 0 && k * theta > upperBoundMean )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
469 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
470 theta = upperBoundMean / k ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
471 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
472 else if ( lowerBoundMean > 0 && k * theta < lowerBoundMean )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
473 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
474 theta = lowerBoundMean / k ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
475 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
476
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
477 if ( k <= 1e-6 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
478 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
479 k = 1e-6 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
480 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
481
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
482 if ( theta <= 1e-6 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
483 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
484 theta = 1e-6 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
485 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
486
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
487 double diff = ABS( prevK - k ) + ABS( prevTheta - theta ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
488 if ( diff < 1e-5 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
489 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
490
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
491 ++iterCnt ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
492 //if ( det <= 1e-4 && det >=-1e-4 && k >= 5000 ) //&& diff < 1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
493 if ( k >= 10000 ) //&& diff < 1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
494 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
495 k = prevK ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
496 theta = prevTheta ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
497 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
498 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
499 if ( iterCnt == 1000 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
500 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
501 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
502 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
503
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
504 double MixtureGammaEM( double *x, int n, double &pi, double *k, double *theta, int tries, double meanBound[2], int iter = 1000 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
505 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
506 int i ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
507 double *z = new double[n] ; // the expectation that it assigned to model 0.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
508 double *oneMinusZ = new double[n] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
509 int t = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
510 double history[5] = {-1, -1, -1, -1, -1} ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
511 double maxX = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
512 double sumX = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
513 if ( n <= 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
514 return 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
515
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
516 for ( i = 0 ; i < n ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
517 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
518 sumX += x[i] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
519 if ( x[i] > maxX )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
520 maxX = x[i] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
521 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
522 if ( maxX > meanBound[1] && meanBound[1] >= 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
523 maxX = meanBound[1] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
524
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
525 /*if ( meanBound[1] == -1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
526 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
527 // The EM for coverage
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
528 maxX = 10.0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
529 }*/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
530
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
531 while ( 1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
532 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
533 double npi, nk[2], ntheta[2] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
534 double sum = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
535 for ( i = 0 ; i < n ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
536 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
537 //double lf0 = -k[0] * log( theta[0] ) + ( k[0] - 1 ) * log( cov[i]) - cov[i] / theta[0] - lgamma( k[0] );
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
538 //double lf1 = -k[1] * log( theta[1] ) + ( k[1] - 1 ) * log( cov[i]) - cov[i] / theta[1] - lgamma( k[1] );
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
539 //z[i] = exp( lf0 + log( pi ) ) / ( exp( lf0 + log( pi ) ) + exp( lf1 + log( 1 - pi ) ) ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
540 if ( pi != 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
541 z[i] = MixtureGammaAssignment( x[i], pi, k, theta ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
542 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
543 z[i] = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
544 /*if ( isnan( z[i] ) )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
545 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
546 printf( "nan: %lf %lf %lf %lf\n", x[i], pi, k, theta ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
547 }*/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
548 oneMinusZ[i] = 1 - z[i] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
549 sum += z[i] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
550 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
551
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
552 // compute new pi.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
553 npi = sum / n ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
554
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
555 // Use gradient descent to compute new k and theta.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
556 if ( 1 ) //pi > 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
557 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
558 double bound ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
559 if ( meanBound[1] != -1 ) // the EM for ratio
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
560 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
561 bound = ( theta[1] * k[1] > 1 ) ? 1 : ( theta[1] * k[1] ) / ( 1 + tries );
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
562 GradientDescentGammaDistribution( nk[0], ntheta[0], k[0], theta[0], k[1], -1, -1, bound, x, z, n ) ; // It seems setting an upper bound 1 for k[0] is not a good idea.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
563 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
564 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
565 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
566 bound = ( theta[1] * k[1] > 1 ) ? 1 : ( theta[1] * k[1] ) / ( 1 + tries ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
567 GradientDescentGammaDistribution( nk[0], ntheta[0], k[0], theta[0], k[1], -1, meanBound[0], bound, x, z, n ) ; // It seems setting an upper bound 1 for k[0] is not a good idea.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
568 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
569 GradientDescentGammaDistribution( nk[1], ntheta[1], k[1], theta[1], -1, k[0], theta[0] * k[0], maxX, x, oneMinusZ, n ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
570 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
571 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
572 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
573 GradientDescentGammaDistribution( nk[1], ntheta[1], k[1], theta[1], 0, 0, 0, 0, x, oneMinusZ, n ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
574 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
575
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
576 double diff ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
577 if ( isnan( npi ) || isnan( nk[0] ) || isnan( nk[1] ) || isnan( ntheta[0] ) || isnan( ntheta[1] ) )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
578 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
579 delete[] z ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
580 delete[] oneMinusZ ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
581 return -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
582 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
583 diff = ABS( nk[0] - k[0] ) + ABS( nk[1] - k[1] )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
584 + ABS( ntheta[0] - theta[0] ) + ABS( ntheta[1] - theta[1] ) ; // pi is fully determined by these 4 parameters.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
585 if ( diff < 1e-4 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
586 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
587 diff = ABS( nk[0] - history[1] ) + ABS( nk[1] - history[2] )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
588 + ABS( ntheta[0] - history[3] ) + ABS( ntheta[1] - history[4] ) ; // pi is fully determined by these 4 parameters.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
589 if ( diff < 1e-4 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
590 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
591
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
592 history[0] = pi ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
593 history[1] = k[0] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
594 history[2] = k[1] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
595 history[3] = theta[0] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
596 history[4] = theta[1] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
597
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
598 pi = npi ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
599 k[0] = nk[0] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
600 k[1] = nk[1] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
601 theta[0] = ntheta[0] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
602 theta[1] = ntheta[1] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
603
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
604 /*double logLikelihood = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
605 for ( i = 0 ; i < n ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
606 logLikelihood += log( pi * exp( LogGammaDensity( x[i], k[0], theta[0]) ) +
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
607 (1 - pi ) * exp( LogGammaDensity( x[i], k[1], theta[1] ) ) ) ;*/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
608
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
609 //printf( "%d: %lf %lf %lf %lf %lf\n", t, pi, k[0], theta[0], k[1], theta[1] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
610
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
611 ++t ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
612 if ( iter != -1 && t >= iter )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
613 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
614 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
615 delete[] z ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
616 delete[] oneMinusZ ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
617 return 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
618 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
619
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
620 bool IsParametersTheSame( double *k, double *theta )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
621 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
622 if ( ABS( k[0] - k[1] ) < 1e-2 && ABS( theta[0] - theta[1] ) < 1e-2 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
623 return true ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
624 return false ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
625 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
626
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
627 int RatioAndCovEM( double *covRatio, double *cov, int n, double &piRatio, double kRatio[2],
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
628 double thetaRatio[2], double &piCov, double kCov[2], double thetaCov[2] )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
629 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
630 int i ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
631 piRatio = 0.6 ; // mixture coefficient for model 0 and 1
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
632 kRatio[0] = 0.9 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
633 kRatio[1] = 0.45 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
634 thetaRatio[0] = 0.05 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
635 thetaRatio[1] = 1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
636 double meanBound[2] = {-1, 1} ; // [0] is for the lower bound of the noise model, [1] is for the upper bound of the true model
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
637
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
638 /*double *filteredCovRatio = new double[n] ;// ignore the ratio that is greater than 5.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
639 int m = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
640 for ( i = 0 ; i < n ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
641 if ( covRatio[i] < 1.0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
642 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
643 filteredCovRatio[m] = covRatio[i] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
644 ++m ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
645 }*/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
646 srand( 17 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
647 int maxTries = 10 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
648 int t = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
649 double *buffer = new double[n] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
650 for ( i = 0 ; i < n ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
651 buffer[i] = covRatio[i] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
652 qsort( buffer, n, sizeof( double ), CompDouble ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
653 //covRatio = buffer ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
654 while ( 1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
655 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
656 //printf( "EM\n" ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
657 MixtureGammaEM( covRatio, n, piRatio, kRatio, thetaRatio, t, meanBound ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
658 //printf( "%lf %lf %lf %lf %lf\n", piRatio, kRatio[0], kRatio[1], thetaRatio[0], thetaRatio[1] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
659 if ( piRatio > 0.999 || piRatio < 0.001 || IsParametersTheSame( kRatio, thetaRatio ) )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
660 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
661 ++t ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
662 if ( t > maxTries )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
663 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
664 piRatio = 0.6 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
665 kRatio[0] += ( ( rand() * 0.5 - RAND_MAX ) / (double)RAND_MAX * 0.1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
666 if ( kRatio[0] <= 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
667 kRatio[0] = 0.9 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
668 kRatio[1] += ( ( rand() * 0.5 - RAND_MAX ) / (double)RAND_MAX * 0.1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
669 if ( kRatio[1] <= 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
670 kRatio[1] = 0.45 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
671 thetaRatio[0] += ( ( rand() * 0.5 - RAND_MAX ) / (double)RAND_MAX * 0.1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
672 if ( thetaRatio[0] <= 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
673 thetaRatio[0] = 0.05 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
674 thetaRatio[1] += ( ( rand() * 0.5 - RAND_MAX ) / (double)RAND_MAX * 0.1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
675 if ( thetaRatio[1] <= 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
676 thetaRatio[1] = 1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
677 if ( kRatio[0] < kRatio[1] )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
678 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
679 if ( rand() & 1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
680 kRatio[0] = kRatio[1] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
681 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
682 kRatio[1] = kRatio[0] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
683 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
684 if ( kRatio[0] * thetaRatio[0] > kRatio[1] * thetaRatio[1] )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
685 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
686 thetaRatio[0] = kRatio[1] * thetaRatio[1] / kRatio[0] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
687 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
688 //printf( "%lf %lf %lf %lf %lf\n", piRatio, kRatio[0], kRatio[1], thetaRatio[0], thetaRatio[1] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
689
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
690 continue ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
691 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
692
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
693 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
694 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
695 //delete[] filteredCovRatio ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
696 if ( t > maxTries && piRatio > 0.999 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
697 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
698 /*piRatio = 0.6 ; // mixture coefficient for model 0 and 1
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
699 kRatio[0] = 0.9 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
700 kRatio[1] = 0.45 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
701 thetaRatio[0] = 0.05 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
702 thetaRatio[1] = 1 ;*/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
703 piRatio = 0.999 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
704 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
705 if ( IsParametersTheSame( kRatio, thetaRatio ) || piRatio <= 1e-3 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
706 piRatio = 1e-3 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
707
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
708 piCov = piRatio ; // mixture coefficient for model 0 and 1
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
709 kCov[0] = 0.9 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
710 kCov[1] = 0.45 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
711 thetaCov[0] = 3 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
712 thetaCov[1] = 12 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
713
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
714 // only do one iteration of EM, so that pi does not change?
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
715 // But it seems it still better to run full EM.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
716 meanBound[0] = 1.01 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
717 meanBound[1] = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
718
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
719 //printf( "for coverage:\n" ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
720 //piCov = 0.001000 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
721 MixtureGammaEM( cov, n, piCov, kCov, thetaCov, 0, meanBound ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
722 //printf( "for coverage done\n" ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
723 piCov = piRatio ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
724
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
725 delete []buffer ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
726
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
727 return 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
728 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
729
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
730 double GetPValue( double x, double *k, double *theta )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
731 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
732 int fault ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
733 double p ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
734 p = 1 - gammad( x / theta[0], k[0], &fault ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
735 return p ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
736 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
737
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
738 // if x's value is less than the average of (k0-1)*theta0, then we force x=(k0-1)*theta0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
739 // the mode of the model 0. Of course, it does not affect when k0<=1 already.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
740 double MixtureGammaAssignmentAdjust( double x, double pi, double* k, double *theta )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
741 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
742 if ( x < ( k[0] - 1 ) * theta[0] )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
743 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
744 x = ( k[0] - 1 ) * theta[0] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
745 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
746 return MixtureGammaAssignment( x, pi, k, theta ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
747 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
748
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
749
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
750 // Transform the cov number for better fitting
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
751 double TransformCov( double c )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
752 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
753 double ret ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
754 // original it is c-1.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
755 // Use -2 instead of -1 is that many region covered to 1 reads will be filtered when
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
756 // build the subexons.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
757 //
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
758 //ret = sqrt( c ) - 1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
759 if ( c <= 2 + 1e-6 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
760 ret = 1e-6 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
761 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
762 ret = c - 2 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
763 return ret ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
764 //return log( c ) / log( 2.0 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
765 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
766
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
767 int main( int argc, char *argv[] )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
768 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
769 int i, j ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
770 bool noStats = false ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
771 if ( argc < 3 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
772 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
773 fprintf( stderr, usage ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
774 exit( 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
775 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
776
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
777 gMinDepth = 2 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
778
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
779 for ( i = 3 ; i < argc ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
780 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
781 if ( !strcmp( argv[i], "--noStats" ) )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
782 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
783 noStats = true ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
784 continue ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
785 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
786 else if ( !strcmp( argv[i], "--minDepth" ) )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
787 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
788 gMinDepth = atoi( argv[i + 1] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
789 ++i ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
790 continue ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
791 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
792 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
793 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
794 fprintf( stderr, "Unknown argument: %s\n", argv[i] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
795 return 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
796 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
797 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
798
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
799 Alignments alignments ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
800 alignments.Open( argv[1] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
801 std::vector<struct _splitSite> splitSites ; // only compromised the
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
802 std::vector<struct _splitSite> allSplitSites ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
803
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
804 // read in the splice site
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
805 FILE *fp ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
806 fp = fopen( argv[2], "r" ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
807 char chrom[50] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
808 int64_t start, end ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
809 int support ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
810 char strand[3] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
811 int uniqSupport, secondarySupport, uniqEditDistance, secondaryEditDistance ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
812 while ( fscanf( fp, "%s %" PRId64 " %" PRId64 " %d %s %d %d %d %d", chrom, &start, &end, &support, strand,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
813 &uniqSupport, &secondarySupport, &uniqEditDistance, &secondaryEditDistance ) != EOF )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
814 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
815 if ( support <= 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
816 continue ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
817 //if ( !( uniqSupport >= 1
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
818 // || secondarySupport > 10 ) )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
819 //if ( uniqSupport <= 0.01 * ( uniqSupport + secondarySupport ) || ( uniqSupport == 0 && secondarySupport < 20 ) )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
820 //if ( uniqSupport == 0 && secondarySupport <= 10 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
821 // continue ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
822 int chrId = alignments.GetChromIdFromName( chrom ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
823 struct _splitSite ss ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
824 --start ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
825 --end ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
826 ss.pos = start ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
827 ss.chrId = chrId ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
828 ss.type = 2 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
829 ss.oppositePos = end ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
830 ss.strand = strand[0] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
831 ss.support = support ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
832 ss.uniqSupport = uniqSupport ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
833 ss.mismatchSum = uniqEditDistance + secondaryEditDistance ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
834 splitSites.push_back( ss ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
835
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
836 ss.pos = end ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
837 ss.type = 1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
838 ss.oppositePos = start ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
839 ss.strand = strand[0] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
840 ss.support = support ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
841 ss.uniqSupport = uniqSupport ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
842 ss.mismatchSum = uniqEditDistance + secondaryEditDistance ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
843 splitSites.push_back( ss ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
844 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
845 fclose( fp ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
846 //printf( "ss:%d\n", splitSites.size() ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
847
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
848 //printf( "ss:%d\n", splitSites.size() ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
849
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
850 alignments.GetGeneralInfo( true ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
851 // Build the blocks
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
852 Blocks regions ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
853 alignments.Rewind() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
854 regions.BuildExonBlocks( alignments ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
855 //printf( "%d\n", regions.exonBlocks.size() ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
856
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
857 FilterAndSortSplitSites( splitSites ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
858 FilterNearSplitSites( splitSites ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
859 FilterRepeatSplitSites( splitSites ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
860 regions.FilterSplitSitesInRegions( splitSites ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
861 regions.FilterGeneMergeSplitSites( splitSites ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
862
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
863
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
864 allSplitSites = splitSites ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
865 KeepUniqSplitSites( splitSites ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
866
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
867 //for ( i = 0 ; i < splitSites.size() ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
868 // printf( "%d %d\n", splitSites[i].pos + 1, splitSites[i].oppositePos + 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
869 // Split the blocks using split site
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
870 regions.SplitBlocks( alignments, splitSites ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
871 //printf( "%d\n", regions.exonBlocks.size() ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
872 /*for ( i = 0 ; i < regions.exonBlocks.size() ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
873 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
874 struct _block &e = regions.exonBlocks[i] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
875 printf( "%s %" PRId64 " %" PRId64 " %d %d\n", alignments.GetChromName( e.chrId ), e.start + 1, e.end + 1, e.leftType, e.rightType ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
876 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
877 return 0 ;*/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
878 // Recompute the coverage for each block.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
879 alignments.Rewind() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
880 //printf( "Before computeDepth: %d\n", regions.exonBlocks.size() ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
881
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
882 regions.ComputeDepth( alignments ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
883 //printf( "After computeDepth: %d\n", regions.exonBlocks.size() ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
884
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
885 // Merge blocks that may have a hollow coverage by accident.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
886 regions.MergeNearBlocks() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
887 //printf( "After merge: %d\n", regions.exonBlocks.size() ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
888
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
889 // Put the intron informations
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
890 regions.AddIntronInformation( allSplitSites, alignments ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
891 //printf( "After add information.\n" ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
892
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
893 // Compute the average ratio against the left and right connected subexons.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
894 regions.ComputeRatios() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
895 //printf( "After compute ratios.\n" ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
896
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
897 //printf( "Finish building regions.\n" ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
898 if ( noStats )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
899 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
900 // just output the subexons.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
901 if ( realpath( argv[1], buffer ) == NULL )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
902 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
903 strcpy( buffer, argv[1] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
904 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
905 printf( "#%s\n", buffer ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
906 printf( "#fitted_ir_parameter_ratio: pi: -1 k0: -1 theta0: -1 k1: -1 theta1: -1\n" ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
907 printf( "#fitted_ir_parameter_cov: pi: -1 k0: -1 theta0: -1 k1: -1 theta1: -1\n" ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
908
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
909 int blockCnt = regions.exonBlocks.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
910 for ( int i = 0 ; i < blockCnt ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
911 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
912 struct _block &e = regions.exonBlocks[i] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
913 double avgDepth = (double)e.depthSum / ( e.end - e.start + 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
914 printf( "%s %" PRId64 " %" PRId64 " %d %d %lf -1 -1 -1 -1 ", alignments.GetChromName( e.chrId ), e.start + 1, e.end + 1, e.leftType, e.rightType, avgDepth ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
915 int prevCnt = e.prevCnt ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
916 if ( i > 0 && e.start == regions.exonBlocks[i - 1].end + 1 &&
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
917 e.leftType == regions.exonBlocks[i - 1].rightType )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
918 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
919 printf( "%d ", prevCnt + 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
920 for ( j = 0 ; j < prevCnt ; ++j )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
921 printf( "%" PRId64 " ", regions.exonBlocks[ e.prev[j] ].end + 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
922 printf( "%" PRId64 " ", regions.exonBlocks[i - 1].end + 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
923 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
924 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
925 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
926 printf( "%d ", prevCnt ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
927 for ( j = 0 ; j < prevCnt ; ++j )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
928 printf( "%" PRId64 " ", regions.exonBlocks[ e.prev[j] ].end + 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
929 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
930
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
931 int nextCnt = e.nextCnt ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
932 if ( i < blockCnt - 1 && e.end == regions.exonBlocks[i + 1].start - 1 &&
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
933 e.rightType == regions.exonBlocks[i + 1].leftType )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
934 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
935 printf( "%d %" PRId64 " ", nextCnt + 1, regions.exonBlocks[i + 1].start + 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
936 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
937 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
938 printf( "%d ", nextCnt ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
939 for ( j = 0 ; j < nextCnt ; ++j )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
940 printf( "%" PRId64 " ", regions.exonBlocks[ e.next[j] ].start + 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
941 printf( "\n" ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
942
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
943 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
944 return 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
945 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
946
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
947 // Extract the blocks for different events.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
948 int blockCnt = regions.exonBlocks.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
949 std::vector<struct _block> irBlocks ; // The regions corresponds to intron retention events.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
950 double *leftClassifier = new double[ blockCnt ] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
951 double *rightClassifier = new double[ blockCnt ] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
952 std::vector<struct _block> overhangBlocks ; //blocks like (...[ or ]...)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
953 std::vector<struct _block> islandBlocks ; // blocks like (....)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
954
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
955 for ( i = 0 ; i < blockCnt ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
956 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
957 int ltype = regions.exonBlocks[i].leftType ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
958 int rtype = regions.exonBlocks[i].rightType ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
959 leftClassifier[i] = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
960 rightClassifier[i] = -1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
961
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
962 //double avgDepth = (double)regions.exonBlocks[i].depthSum / ( regions.exonBlocks[i].end - regions.exonBlocks[i].start + 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
963
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
964 if ( ltype == 2 && rtype == 1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
965 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
966 // candidate intron retention.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
967 // Note that when I compute the ratio, it is already made sure that the avgDepth>1.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
968 double ratio = regions.PickLeftAndRightRatio( regions.exonBlocks[i] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
969
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
970 //printf( "%lf %lf\n", regions.exonBlocks[i].leftRatio, regions.exonBlocks[i].rightRatio ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
971 if ( ratio > 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
972 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
973 regions.exonBlocks[i].ratio = ratio ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
974 irBlocks.push_back( regions.exonBlocks[i] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
975 irBlocks[ irBlocks.size() - 1 ].contigId = i ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
976 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
977 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
978 else if ( ( ltype == 0 && rtype == 1 ) || ( ltype == 2 && rtype == 0 ) )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
979 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
980 // subexons like (...[ or ]...)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
981 double ratio ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
982 if ( ltype == 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
983 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
984 ratio = regions.exonBlocks[i].rightRatio ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
985 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
986 else if ( ltype == 2 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
987 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
988 ratio = regions.exonBlocks[i].leftRatio ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
989 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
990 if ( ratio > 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
991 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
992 regions.exonBlocks[i].ratio = ratio ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
993 overhangBlocks.push_back( regions.exonBlocks[i] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
994 overhangBlocks[ overhangBlocks.size() - 1].contigId = i ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
995 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
996 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
997 else if ( ltype == 0 && rtype == 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
998 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
999 islandBlocks.push_back( regions.exonBlocks[i] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1000 islandBlocks[ islandBlocks.size() - 1].contigId = i ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1001 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1002 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1003
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1004 // Compute the histogram for each intron.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1005 int irBlockCnt = irBlocks.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1006 double *cov = new double[irBlockCnt] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1007 double *covRatio = new double[ irBlockCnt ] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1008 double piRatio, kRatio[2], thetaRatio[2] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1009 double piCov, kCov[2], thetaCov[2] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1010 for ( i = 0 ; i < irBlockCnt ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1011 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1012 double avgDepth = regions.GetAvgDepth( irBlocks[i] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1013 //cov[i] = ( avgDepth - 1 ) / ( flankingAvg - 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1014 cov[i] = TransformCov( avgDepth ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1015
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1016 covRatio[i] = regions.PickLeftAndRightRatio( irBlocks[i] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1017 //cov[i] = avgDepth / anchorAvg ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1018 //printf( "%"PRId64" %d %d: %lf %lf\n", irBlocks[i].depthSum, irBlocks[i].start, irBlocks[i].end, avgDepth, cov[i] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1019 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1020
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1021 /*fp = fopen( "ratio.out", "r" ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1022 int irBlockCnt = 0 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1023 double *cov = new double[10000] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1024 while ( 1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1025 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1026 double r ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1027 if ( fscanf( fp, "%lf", &r ) == EOF )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1028 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1029 cov[ irBlockCnt ] = r ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1030 ++irBlockCnt ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1031 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1032 fclose( fp ) ;*/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1033 RatioAndCovEM( covRatio, cov, irBlockCnt, piRatio, kRatio, thetaRatio, piCov, kCov, thetaCov ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1034
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1035 for ( i = 0 ; i < irBlockCnt ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1036 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1037 //double lf0 = -k[0] * log( theta[0] ) + ( k[0] - 1 ) * log( cov[i]) - cov[i] / theta[0] - lgamma( k[0] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1038 //double lf1 = -k[1] * log( theta[1] ) + ( k[1] - 1 ) * log( cov[i]) - cov[i] / theta[1] - lgamma( k[1] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1039
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1040 double p1, p2, p ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1041
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1042 p1 = MixtureGammaAssignmentAdjust( covRatio[i], piRatio, kRatio, thetaRatio ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1043 p2 = MixtureGammaAssignmentAdjust( cov[i], piCov, kCov, thetaCov ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1044
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1045 /*p1 = GetPValue( covRatio[i], kRatio, thetaRatio ) ; //1 - gammad( covRatio[i] / thetaRatio[0], kRatio[0], &fault ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1046 if ( piRatio != 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1047 p2 = GetPValue( cov[i], kCov, thetaCov ) ;//1 - gammad( cov[i] / thetaCov[0], kCov[0], &fault ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1048 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1049 p2 = p1 ;*/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1050 //printf( "%lf %lf: %lf %lf\n", covRatio[i], cov[i], p1, p2 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1051 p = p1 > p2 ? p1 : p2 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1052
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1053
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1054 //printf( "%d %d: avg: %lf ratio: %lf p: %lf\n", irBlocks[i].start, irBlocks[i].end, irBlocks[i].depthSum / (double)( irBlocks[i].end - irBlocks[i].start + 1 ), covRatio[i],
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1055 // p ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1056 leftClassifier[ irBlocks[i].contigId ] = p ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1057 rightClassifier[ irBlocks[i].contigId ] = p ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1058 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1059
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1060 // Process the classifier for overhang subexons and the subexons to see whether we need soft boundary
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1061 int overhangBlockCnt = overhangBlocks.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1062 delete []cov ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1063 delete []covRatio ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1064
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1065 cov = new double[ overhangBlockCnt ] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1066 covRatio = new double[overhangBlockCnt] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1067 double overhangPiRatio, overhangKRatio[2], overhangThetaRatio[2] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1068 double overhangPiCov, overhangKCov[2], overhangThetaCov[2] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1069
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1070 for ( i = 0 ; i < overhangBlockCnt ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1071 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1072 covRatio[i] = overhangBlocks[i].ratio ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1073 cov[i] = TransformCov( regions.GetAvgDepth( overhangBlocks[i] ) ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1074 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1075 RatioAndCovEM( covRatio, cov, overhangBlockCnt, overhangPiRatio, overhangKRatio, overhangThetaRatio, overhangPiCov, overhangKCov, overhangThetaCov ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1076
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1077 for ( i = 0 ; i < overhangBlockCnt ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1078 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1079 //double lf0 = -k[0] * log( theta[0] ) + ( k[0] - 1 ) * log( cov[i]) - cov[i] / theta[0] - lgamma( k[0] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1080 //double lf1 = -k[1] * log( theta[1] ) + ( k[1] - 1 ) * log( cov[i]) - cov[i] / theta[1] - lgamma( k[1] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1081
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1082 double p1, p2, p ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1083 p1 = MixtureGammaAssignmentAdjust( covRatio[i], overhangPiRatio, overhangKRatio, overhangThetaRatio ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1084 p2 = MixtureGammaAssignmentAdjust( cov[i], overhangPiCov, overhangKCov, overhangThetaCov ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1085
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1086 /*p1 = GetPValue( covRatio[i], overhangKRatio, overhangThetaRatio ) ; //1 - gammad( covRatio[i] / thetaRatio[0], kRatio[0], &fault ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1087 if ( overhangPiRatio != 0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1088 p2 = GetPValue( cov[i], overhangKCov, overhangThetaCov ) ;//1 - gammad( cov[i] / thetaCov[0], kCov[0], &fault ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1089 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1090 p2 = p1 ;*/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1091
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1092 //p = p1 < p2 ? p1 : p2 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1093 p = sqrt( p1 * p2 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1094
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1095 int idx = overhangBlocks[i].contigId ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1096 if ( regions.exonBlocks[idx].rightType == 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1097 leftClassifier[ idx ] = rightClassifier[ idx ] = p ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1098 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1099 leftClassifier[ idx ] = rightClassifier[ idx ] = p ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1100 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1101
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1102 for ( i = 0 ; i < blockCnt ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1103 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1104 struct _block &e = regions.exonBlocks[i] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1105 //if ( ( e.leftType == 0 && e.rightType == 1 ) ||
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1106 // variance-stabailizing transformation of poisson distribution. But we are more conservative here.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1107 // The multiply 2 before that is because we ignore the region below 0, so we need to somehow renormalize the distribution.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1108 if ( e.leftType == 1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1109 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1110 if ( e.leftRatio >= 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1111 leftClassifier[i] = 2 * alnorm( e.leftRatio * 2.0 , true ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1112 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1113 leftClassifier[i] = 1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1114 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1115 /*else if ( e.leftType == 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1116 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1117 // If this region is a start of a gene, the other sample might introduce
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1118 // a new intron before it. So we want to test whether this region can still
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1119 // be a start of a gene even there is an intron before it.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1120 for ( j = i + 1 ; j < blockCnt ; ++j )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1121 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1122 if ( regions.exonBlocks[j].contigId != regions.exonBlocks[i].contigId )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1123 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1124 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1125
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1126 for ( k = i ; k < j ; ++k )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1127 if ( regions.exonBlocks[j].leftType == 1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1128 break ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1129 if ( k >= j )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1130 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1131 leftClassifier[i] = alnorm( )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1132 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1133 }*/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1134
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1135 //if ( ( e.rightType == 0 && e.leftType == 2 ) ||
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1136 if ( e.rightType == 2 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1137 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1138 if ( e.rightRatio >= 0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1139 rightClassifier[i] = 2 * alnorm( e.rightRatio * 2.0, true ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1140 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1141 rightClassifier[i] = 1 ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1142 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1143 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1144
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1145 // Process the result for subexons seems like single-exon transcript (...)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1146 int islandBlockCnt = islandBlocks.size() ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1147 //std::sort( islandBlocks.begin(), islandBlocks.end(), CompBlocksByAvgDepth ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1148 for ( i = 0, j = 0 ; i < islandBlockCnt ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1149 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1150 /*if ( regions.GetAvgDepth( islandBlocks[i] ) != regions.GetAvgDepth( islandBlocks[j] ) )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1151 j = i ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1152 leftClassifier[ islandBlocks[i].contigId ] = 1 - (j + 1) / (double)( islandBlockCnt + 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1153 rightClassifier[ islandBlocks[i].contigId ] = 1 - (j + 1) / (double)( islandBlockCnt + 1 ) ;*/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1154 double p = GetPValue( TransformCov( regions.GetAvgDepth( islandBlocks[i] ) ), kCov, thetaCov ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1155 leftClassifier[ islandBlocks[i].contigId ] = p ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1156 rightClassifier[ islandBlocks[i].contigId ] = p ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1157 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1158
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1159
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1160 // Output the result.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1161 if ( realpath( argv[1], buffer ) == NULL )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1162 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1163 strcpy( buffer, argv[1] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1164 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1165 printf( "#%s\n", buffer ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1166 // TODO: higher precision.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1167 printf( "#fitted_ir_parameter_ratio: pi: %lf k0: %lf theta0: %lf k1: %lf theta1: %lf\n", piRatio, kRatio[0], thetaRatio[0], kRatio[1], thetaRatio[1] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1168 printf( "#fitted_ir_parameter_cov: pi: %lf k0: %lf theta0: %lf k1: %lf theta1: %lf\n", piCov, kCov[0], thetaCov[0], kCov[1], thetaCov[1] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1169
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1170 printf( "#fitted_overhang_parameter_ratio: pi: %lf k0: %lf theta0: %lf k1: %lf theta1: %lf\n", overhangPiRatio, overhangKRatio[0], overhangThetaRatio[0], overhangKRatio[1], overhangThetaRatio[1] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1171 printf( "#fitted_overhang_parameter_cov: pi: %lf k0: %lf theta0: %lf k1: %lf theta1: %lf\n", overhangPiCov, overhangKCov[0], overhangThetaCov[0], overhangKCov[1], overhangThetaCov[1] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1172
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1173
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1174 for ( int i = 0 ; i < blockCnt ; ++i )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1175 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1176 struct _block &e = regions.exonBlocks[i] ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1177 double avgDepth = regions.GetAvgDepth( e ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1178 printf( "%s %" PRId64 " %" PRId64 " %d %d %c %c %lf %lf %lf %lf %lf ", alignments.GetChromName( e.chrId ), e.start + 1, e.end + 1, e.leftType, e.rightType,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1179 e.leftStrand, e.rightStrand, avgDepth,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1180 e.leftRatio, e.rightRatio, leftClassifier[i], rightClassifier[i] ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1181 int prevCnt = e.prevCnt ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1182 if ( i > 0 && e.start == regions.exonBlocks[i - 1].end + 1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1183 //&& e.leftType == regions.exonBlocks[i - 1].rightType )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1184 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1185 printf( "%d ", prevCnt + 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1186 for ( j = 0 ; j < prevCnt ; ++j )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1187 printf( "%" PRId64 " ", regions.exonBlocks[ e.prev[j] ].end + 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1188 printf( "%" PRId64 " ", regions.exonBlocks[i - 1].end + 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1189 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1190 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1191 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1192 printf( "%d ", prevCnt ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1193 for ( j = 0 ; j < prevCnt ; ++j )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1194 printf( "%" PRId64 " ", regions.exonBlocks[ e.prev[j] ].end + 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1195 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1196
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1197 int nextCnt = e.nextCnt ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1198 if ( i < blockCnt - 1 && e.end == regions.exonBlocks[i + 1].start - 1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1199 //&& e.rightType == regions.exonBlocks[i + 1].leftType )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1200 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1201 printf( "%d %" PRId64 " ", nextCnt + 1, regions.exonBlocks[i + 1].start + 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1202 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1203 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1204 printf( "%d ", nextCnt ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1205 for ( j = 0 ; j < nextCnt ; ++j )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1206 printf( "%" PRId64 " ", regions.exonBlocks[ e.next[j] ].start + 1 ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1207 printf( "\n" ) ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1208 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1209
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1210 delete[] cov ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1211 delete[] covRatio ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1212 delete[] leftClassifier ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1213 delete[] rightClassifier ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1214 }