-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-best-breaks
executable file
·94 lines (75 loc) · 1.95 KB
/
find-best-breaks
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#! /usr/bin/env perl
use strict;
use warnings FATAL => 'all';
#use Carp::Always;
# use FindBin;
# use lib "$FindBin::Bin";
# use Xyzzy;
use constant { TRUE => 1, FALSE => 0 };
# ------------------------------------------------------------------------
use constant { NO_VALUE => ";no-value;" };
sub parse_gff_attributes {
my ($raw_attributes) = @_;
my $attributes = {};
foreach my $key_val (split(/; */,$raw_attributes)) {
my ($key,$val);
if ( $key_val =~ /^([^=]+)=(.*)/ ) {
($key,$val) = ($1,$2);
} else {
($key,$val) = ($key_val, NO_VALUE);
}
$attributes->{$key} = $val;
}
return $attributes;
}
# ------------------------------------------------------------------------
my %lens;
my %vectors;
while (<STDIN>) {
chomp;
if ( /^#/ ) {
if ( /^##sequence-region (.+) 1 ([0-9]+)$/ ) {
my ($accession,$len) = ($1,$2);
(!defined($vectors{$accession})) || die;
my $v = ["bogus"];
for (my $i=1; $i<=$len; $i++) {
push @$v, 0;
}
$vectors{$accession} = $v;
$lens{$accession} = $len;
}
next;
}
my ($seqname,$source,$feature,$start,$end,
$score,$strand,$frame,$raw_attributes) = split(/\t/,$_);
my $attributes = parse_gff_attributes($raw_attributes);
my $v = $vectors{$seqname};
my $len = $lens{$seqname};
defined($v) || die;
my $n = $end - $start + 1;
for (my $i=$start; $i<=$end; $i++) {
my $j = $i;
if ( $j < 1 ) {
$j += $len;
} elsif ( $len < $j ) {
$j -= $len;
}
$v->[$j] += $n;
}
}
# ------------------------------------------------------------------------
foreach my $accession ( sort (keys %vectors) ) {
my $v = $vectors{$accession};
my $len = $#{$v};
my $min = $v->[1];
my @pos = (1);
for (my $i=2; $i<=$len; $i++) {
if ( $v->[$i] < $min ) {
$min = $v->[$i];
@pos = ($i);
} elsif ($v->[$i] == $min) {
push @pos, $i;
}
}
print $accession,": ", $min," (",join(",",@pos),")\n";
}