-
Notifications
You must be signed in to change notification settings - Fork 5
/
hmmresult2windows.pl
56 lines (53 loc) · 1.36 KB
/
hmmresult2windows.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/perl
use warnings;
use strict;
my $sample = $ARGV[0];
my $current_chr = "NA";
my $previous_pos;
my $previous_state;
my $min_prob = $ARGV[1]; #minimum probability to pick state
print "sample\tchr\tstart\tend\tstate";
while(<STDIN>){
chomp;
if ($. == 1){next;}
my @a = split(/\t/,$_);
my $chr = $a[0];
my $pos = $a[1];
my $p1 = $a[2];
my $het = $a[3];
my $p2 = $a[4];
my $current_state = "Unknown";
if ($p1 >= $min_prob){
$current_state = "P1";
}elsif ($p2 >= $min_prob){
$current_state = "P2";
}elsif ($het >= $min_prob){
$current_state = "Het";
}
if ($chr ne $current_chr){
#Print end of previous chr
if ($previous_state){
print "\t$previous_pos\t$previous_state";
}
$current_chr = $chr;
undef($previous_pos);
undef($previous_state);
#Start new window;
$previous_pos = $pos;
$previous_state = $current_state;
print "\n$sample\t$chr\t$pos";
}else{
if ($previous_state eq $current_state){
$previous_pos = $pos;
}else{
my $midway_point = int($previous_pos + (($pos - $previous_pos)/2));
my $midway_point_plus = $midway_point+1;
print "\t$midway_point\t$previous_state";
print "\n$sample\t$chr\t$midway_point_plus";
$previous_state = $current_state;
$previous_pos = $pos;
}
}
}
#PRINT LAST WINDOW
print "\t$previous_pos\t$previous_state";