root/lib/compare.pm

Revision 281, 8.9 kB (checked in by cholt, 2 weeks ago)

no longer thread dependant, fixed overhang bug, and evidence clustering bug

Line 
1 #------------------------------------------------------------------------
2 #----                            compare                             ----
3 #------------------------------------------------------------------------
4 package compare;
5 use strict;
6 use vars qw(@ISA @EXPORT $VERSION);
7 use Exporter;
8 use PostData;
9 use FileHandle;
10 use PostData;
11 use Exporter;
12 use PhatHit_utils;
13 use cluster;
14 use clean;
15
16 @ISA = qw(
17        );
18 #------------------------------------------------------------------------
19 #--------------------------- FUNCTIONS ----------------------------------
20 #------------------------------------------------------------------------
21 sub is_same_alt_form {
22         my $a     = shift;
23         my $b     = shift;
24         my $seq   = shift;
25         my $flank = shift;
26
27         die "only one feature in is_same_alt_form!\n"
28         unless defined($a) && defined($b);
29
30         my ($s_to_a_str, $s_to_b_str) = compare_by_shadow($a, $b, $seq, $flank);
31
32         my $a_to_b_str = compare_phat_hits($a, $b, 'query', $flank);
33
34         if     ($s_to_a_str eq $s_to_b_str && $a_to_b_str !~ /^0+$/){
35                 return 1;
36         }
37
38         elsif ($s_to_a_str =~ /[^0]0+[^0]/){
39                 #print STDERR "QAAAA s_to_a_str:$s_to_a_str s_to_b_str:$s_to_b_str\n";
40                 #print STDERR $a->name." ".$b->name."\n";
41                 #sleep 3;
42                 return 0;
43         }
44         elsif ($s_to_b_str =~ /[^0]0+[^0]/){
45                 #print STDERR "QBBBB s_to_a_str:$s_to_a_str s_to_b_str:$s_to_b_str\n";
46                 #print STDERR $a->name." ".$b->name."\n";
47                 #sleep 3;
48
49                 return 0;
50         }
51         else {
52                 #print STDERR "not caught s_to_a_str:$s_to_a_str s_to_b_str:$s_to_b_str".$a->name." ".$b->name."\n";
53                 #sleep 3;
54                 return 1;
55         }
56 }
57 #------------------------------------------------------------------------
58 sub is_redundant_alt_form {
59         my $a     = shift;
60         my $b     = shift;
61         my $seq   = shift;
62         my $flank = shift;
63
64        die "only one feature in is_redundant_form!\n"
65         unless defined($a) && defined($b);
66
67         # note that b will always have fewer exons or be shorter...
68
69         my ($s_to_a_str, $s_to_b_str) = compare_by_shadow($a, $b, $seq, $flank);
70
71         my $a_to_b_str = compare_phat_hits($a, $b, 'query', $flank);
72         my $b_to_a_str = compare_phat_hits($b, $a, 'query', $flank);
73         if     ($s_to_a_str eq $s_to_b_str && $a_to_b_str !~ /^0*$/){
74                 return 1;
75         }
76         elsif ($b_to_a_str =~ /^B?1+b?$/ && $a_to_b_str =~ /^0*A?1+a?$/){
77                 #print STDERR "RAAAA s_to_a_str:$s_to_a_str s_to_b_str:$s_to_b_str\n";
78                 #sleep 3;
79                 return 1;
80         }
81         elsif ($s_to_a_str =~ /^1+$/ && $s_to_b_str =~ /^0*A?1+a?$/){
82                 #print STDERR "RBBBB s_to_a_str:$s_to_a_str s_to_b_str:$s_to_b_str\n";
83                 sleep 3;
84                 #return 1;
85         }
86
87         else {
88                 #print STDERR "redun not caught s_to_a_str:$s_to_a_str s_to_b_str:$s_to_b_str a_to_b_str:$a_to_b_str b_to_a_str:$b_to_a_str".$a->name." ".$b->name."\n";
89                 #sleep 3;
90                 return 0;
91         }
92 }
93 #------------------------------------------------------------------------
94 sub hsps_overlap {
95        my $hit_a = shift;
96        my $hit_b = shift;
97        my $what  = shift;
98        my $r     = shift || 0;
99
100
101         my $sorted_a = PhatHit_utils::sort_hits($hit_a, $what);
102         my $sorted_b = PhatHit_utils::sort_hits($hit_b, $what);
103
104         foreach my $hsp_a (@{$sorted_a}){
105
106                 my $aB = $hsp_a->nB($what);
107                 my $aE = $hsp_a->nE($what);
108
109                 ($aB, $aE) = ($aE, $aB) if $aB > $aE;
110
111                 foreach my $hsp_b (@{$sorted_b}){
112                         my $bB = $hsp_b->nB($what);
113                         my $bE = $hsp_b->nE($what);
114
115                         ($bB, $bE) = ($bE, $bB) if $bB > $bE;
116                         my $class = compare($aB, $aE, $bB, $bE, $r);
117
118                         return 1 if $class ne '0'
119                        
120                 }
121         }
122         return 0;
123 }
124 #------------------------------------------------------------------------
125 sub overlap {
126        my $hit_a = shift;
127        my $hit_b = shift;
128        my $what  = shift;
129        my $r     = shift || 0;
130
131        my $aB = $hit_a->nB($what);
132        my $aE = $hit_a->nE($what);
133        
134        ($aB, $aE) = ($aE, $aB) if $aB > $aE;
135        
136        my $bB = $hit_b->nB($what);
137        my $bE = $hit_b->nE($what);
138        
139        ($bB, $bE) = ($bE, $bB) if $bB > $bE;
140        my $class = compare($aB, $aE, $bB, $bE, $r);
141        
142        return 1 if $class ne '0';
143            
144        return 0;
145 }
146 #------------------------------------------------------------------------
147 sub compare_by_shadow {
148         my $a     = shift;
149         my $b     = shift;
150         my $seq   = shift;
151         my $flank = shift;
152
153         my $a_coors  = PhatHit_utils::get_hsp_coors_from_hit($a, 'query');
154         my $b_coors  = PhatHit_utils::get_hsp_coors_from_hit($b, 'query');
155
156         my @t_coors = (@{$a_coors}, @{$b_coors});
157
158         my $pieces  = Shadower::getPieces($seq, \@t_coors, 0);
159
160         my $s_code_a = get_shadow_code($pieces, $a_coors, $flank);
161         my $s_code_b = get_shadow_code($pieces, $b_coors, $flank);
162
163         return ($s_code_a, $s_code_b);
164 }
165 #------------------------------------------------------------------------
166 sub get_shadow_code {
167         my $pieces = shift;
168         my $coors  = shift;
169         my $flank  = shift;
170
171         my @codes;
172         my $i = 0;
173         foreach my $p (@{$pieces}){
174                 my $pB = $p->{b};
175                 my $pE = $p->{e};
176
177                 foreach my $pair (@{$coors}){
178                         my $cB = $pair->[0];
179                         my $cE = $pair->[1];
180
181                         ($cB, $cE) = ($cE, $cB) if $cB > $cE;
182                        
183                         my $class = compare($pB, $pE, $cB, $cE, $flank);
184
185                         push(@{$codes[$i]}, $class);
186                 }
187                 $i++;
188         }
189
190         my $code = simplify_comparison_code(\@codes);
191
192
193         return $code;
194 }
195 #------------------------------------------------------------------------
196 sub simplify_comparison_code {
197         my $codes = shift;
198
199         my $code = '';
200         foreach my $exon (@{$codes}){
201                 my @sorted  =
202                 sort {code_values($b) <=> code_values($a)} @{$exon};
203
204                 $code .= shift @sorted;
205         }
206         return $code;
207 }
208 #------------------------------------------------------------------------
209 sub code_values {
210         my $v = shift;
211
212         if    ($v eq '0'){
213                 return 0;
214         }
215         elsif ($v eq  '1'){
216                 return 10;
217         }
218         elsif ($v =~  /[ABab]/) {
219                 return 5;
220         }
221         else {
222                 return 1;
223         }
224
225 }
226 #------------------------------------------------------------------------
227 sub same_strand {
228         my $aB = shift;
229         my $aE = shift;
230         my $bB = shift;
231         my $bE = shift;
232
233         if    ($aB <= $aE && $bB <= $bE){
234                 return 1;
235         }
236         elsif ($aB >= $aE && $bB >= $bE){
237                 return 1;
238         }
239         else {
240                 return 0;
241         }
242 }
243 #------------------------------------------------------------------------
244 sub compare_phat_hits {
245         my $hit_a = shift;
246         my $hit_b = shift;
247         my $what  = shift;
248         my $r     = shift || 0;
249
250         my $sorted_a = PhatHit_utils::sort_hits($hit_a, $what);
251         my $sorted_b = PhatHit_utils::sort_hits($hit_b, $what);
252
253         my $alpha = 0;
254         my $omega = @{$sorted_a} - 1;
255
256         my @codes;
257         my $i = 0;
258         foreach my $hsp_a (@{$sorted_a}){
259                        
260                 my $aB = $hsp_a->nB($what);
261                 my $aE = $hsp_a->nE($what);
262
263
264                 my $j = 0;
265                 foreach my $hsp_b (@{$sorted_b}){
266                         my $bB = $hsp_b->nB($what);
267                         my $bE = $hsp_b->nE($what);
268                        
269                         my $class = compare($aB, $aE, $bB, $bE, $r);
270
271                         push(@{$codes[$i]}, $class);
272
273                 }
274                 $i++;
275         }
276         my $code = simplify_comparison_code(\@codes);
277
278         return $code;
279 }
280 #------------------------------------------------------------------------
281 sub compare {
282         my $aB = shift;
283         my $aE = shift;
284         my $bB = shift;
285         my $bE = shift;
286         my $r  = shift || 0;
287
288         my $class;
289         if(same_strand($aB, $aE, $bB, $bE)){
290
291         }
292         else {
293                 return 'R';
294         }
295
296         my $s = 1;
297         if ($aB > $aE && $bB > $bE){
298                 ($aB, $aE) = ($aE, $aB);
299                 ($bB, $bE) = ($bE, $bB);
300                 $s = -1;
301         }
302                
303
304         if     ($bE < $aB || $bB > $aE){
305                 # a        ----
306                 # b  ----
307                 $class = 0;
308         }
309         elsif (abs($aB - $bB) <= $r && abs($aE - $bE) <= $r){
310                 #  a -----
311                 #  b -----
312                 $class = 1;
313         }
314         elsif ($bB < $aB && abs($aE - $bE) <= $r){
315                 # a   ----
316                 # b ------
317                 $class = $s == 1 ? 'B' : 'b';
318         }
319         elsif ($aB < $bB && abs($aE - $bE) <= $r){
320                 # a ------
321                 # b   ----
322                 $class = $s == 1 ? 'A' : 'a';
323         }
324         elsif (abs($aB - $bB) <= $r && $aE < $bE){
325                 # a ----
326                 # b ------
327                 $class = $s == 1 ? 'b' : 'B';
328         }
329         elsif (abs($aB - $bB) <= $r && $aE > $bE){
330                 # a ------
331                 # b ----
332                 $class = $s == 1 ? 'a' : 'A';
333         }
334         elsif ($aB > $bB && $aE < $bE){
335                 # a   ----
336                 # b --------
337                 $class = 'I'; # I for In
338         }
339         elsif ($aB < $bB && $aE > $bE){
340                 # a --------
341                 # b   ----
342                 $class = 'i'; # i for in
343         }
344         elsif ($aB < $bB && $aE < $bE){
345                 # a ------
346                 # b   -------
347                 $class = $s == 1 ? 'Z' : 'z'; ; # Z for zig-zag
348         }
349         elsif ($aB > $bB && $aE > $bE){
350                 # a      ------
351                 # b -------
352                 $class = $s == 1 ? 'z' : 'Z'; # z for Zig-zag
353         }
354
355         return $class; 
356 }
357 #------------------------------------------------------------------------
358 1;
359
360
Note: See TracBrowser for help on using the browser.