-
Notifications
You must be signed in to change notification settings - Fork 0
/
flatten_subtelomere_results.pl
215 lines (185 loc) · 6.15 KB
/
flatten_subtelomere_results.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#! /usr/bin/perl
use strict;
use warnings;
# die with stack trace
use Carp;
$SIG{__DIE__} = sub { confess $_[0] };
use Data::Dump qw(dump);
use List::Util qw( min max );
STDOUT->autoflush(1);
STDERR->autoflush(1);
my $NHMMER_IN = $ARGV[0];
main();
sub main {
if(!defined($NHMMER_IN) or $NHMMER_IN eq ''){
die("Input NHMMER report tblout file is required!");
}
open(my $FH, "<$NHMMER_IN") or die("Unable to open $NHMMER_IN");
my @lines = <$FH>;
chomp(@lines);
my $curr_target = "";
my $curr_query = "";
my @curr_hmmfrom = ();
my @curr_hmmto = ();
my $curr_alifrom;
my $curr_alito;
my $curr_seqlen;
my $curr_strand;
my @curr_evalue = ();
my @curr_score = ();
my @curr_bias = ();
foreach my $li (@lines){
#56b84e33-119a-40d3-863f-2e765a6c27c2 25838 25847 CACACACCA 0 +
my($target,$taccession,$query,$qaccession,$hmmfrom,$hmmto,$alifrom,$alito,$envfrom,$envto,$seqlen,$strand,$evalue,$score,$bias,$desc) = split("\t",$li);
# print "new_id=$new_id\n";
if($target ne $curr_target){
# new target, print last stretch
print
$curr_target . "\t-\t"
$curr_query . "\t-\t"
min(@curr_hmmfrom) . "\t"
max(@curr_hmmto) . "\t"
$curr_alifrom . "\t"
$curr_alito . "\t"
($curr_alifrom + 1). "\t"
($curr_alito + 1) . "\t"
$curr_seqlen . "\t"
$curr_strand . "\t"
max(@curr_evalue) . "\t"
max(@curr_score) . "\t"
max(@curr_bias) . "\t-\n";
$curr_target = $curr_target;
$curr_query = $curr_query;
@curr_hmmfrom = ($hmmfrom);
@curr_hmmto = ($hmmto);
$curr_alifrom = $alifrom;
$curr_alito = $alito;
$curr_seqlen = $seqlen;
$curr_strand = $strand;
@curr_evalue = ($evalue);
@curr_score = ($score);
@curr_bias = ($bias);
}
elsif($strand ne $curr_strand){
# same target but change strand
# not the same stretch, print last stretch
print
$curr_target . "\t-\t"
$curr_query . "\t-\t"
min(@curr_hmmfrom) . "\t"
max(@curr_hmmto) . "\t"
$curr_alifrom . "\t"
$curr_alito . "\t"
($curr_alifrom + 1). "\t"
($curr_alito + 1) . "\t"
$curr_seqlen . "\t"
$curr_strand . "\t"
max(@curr_evalue) . "\t"
max(@curr_score) . "\t"
max(@curr_bias) . "\t-\n";
$curr_target = $curr_target;
$curr_query = $curr_query;
@curr_hmmfrom = ($hmmfrom);
@curr_hmmto = ($hmmto);
$curr_alifrom = $alifrom;
$curr_alito = $alito;
$curr_seqlen = $seqlen;
$curr_strand = $strand;
@curr_evalue = ($evalue);
@curr_score = ($score);
@curr_bias = ($bias);
}
elsif($strand eq '+' and $alifrom < $curr_alifrom){
# overlapping stretch
push(@curr_hmmfrom,$hmmfrom);
push(@curr_hmmto,$hmmto);
$curr_alito = $alito
push(@curr_evalue,$evalue);
push(@curr_score,$score);
push(@curr_bias,$bias);
}
elsif($strand eq '-' and $alito < $curr_alifrom){
# overlapping stretch
push(@curr_hmmfrom,$hmmfrom);
push(@curr_hmmto,$hmmto);
$curr_alito = $alito
push(@curr_evalue,$evalue);
push(@curr_score,$score);
push(@curr_bias,$bias);
}
}
}
sub flatten_telo_match{
my($all_telo_match) = @_;
my %struct;
foreach my $id (keys(%$all_telo_match)){
my @sorted = @{$all_telo_match->{$id}};
# my @sorted = reverse(sort { $a <=> $b } @coords);
my $is_ongoing_stretch = 0;
my $stretch_start = 0;
my $stretch_end = 0;
$struct{$id} = [];
for(my $i=0; $i < scalar(@sorted); $i++){
my $curr_start = $sorted[$i];
my $curr_end = $curr_start + $SEED_LEN + 1;
if($i == 0){
# $is_ongoing_stretch = 1;
$stretch_start = $curr_start;
$stretch_end = $curr_end;
}
# check if this is last pos in array
if($i == scalar(@sorted) - 1){
# last item
# if($is_ongoing_stretch){
push(@{$struct{$id}}, [$stretch_start,$curr_end]);
# }
last;
}
my $next_start = $sorted[$i+1];
my $next_end = $sorted[$i+1] + $SEED_LEN + 1;
if($next_start > $curr_end + $SEED_LEN + 1){
# end of stretch
# $is_ongoing_stretch = 0;
push(@{$struct{$id}}, [$stretch_start,$curr_end]);
$stretch_start = $next_start;
$next_end = $curr_end;
}
else{
# steetch keep going
# $is_ongoing_stretch = 1;
$stretch_end = $curr_end;
}
}
}
return \%struct
}
sub read_seqkit{
my($SEQKIT_IN) = @_;
open(my $FH, "<$SEQKIT_IN") or die("Unable to open $SEQKIT_IN");
my @lines = <$FH>;
chomp(@lines);
my $curr_id = "";
my $curr_start;
my $curr_end;
my %struct;
foreach my $li (@lines){
#56b84e33-119a-40d3-863f-2e765a6c27c2 25838 25847 CACACACCA 0 +
my($new_id,$new_start,$new_end,$new_motif,$new_x,$new_strand) = split("\t",$li);
# print "new_id=$new_id\n";
if($new_id ne $curr_id){
# init entry
$curr_id = $new_id;
$struct{$curr_id} = [];
}
push(@{$struct{$curr_id}},$new_start);
$curr_start = $new_start;
$curr_end = $new_end;
}
# sort coords
foreach my $k (keys(%struct)){
my @vals = @{$struct{$k}};
my @s = sort { $a <=> $b } @vals;
$struct{$k} = \@s;
}
return \%struct;
}