-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpileup2boxes.pl
executable file
·249 lines (172 loc) · 5.32 KB
/
pileup2boxes.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/perl
=head1 NAME
tab2boxes.pl
Tool to count sequence ocurrences by box size
=cut
=head1 SYPNOSIS
tab2boxes.pl [-h] -i <inputtabfile> -s <id_sizes_file> [-b <box_size>]
=head2 I<Flags:>
=over
=item -i
B<inputtabfile> tabular file (mandatory)
=item -s
B<id_sizes_file> id_sizes file (two columns: ID, SIZE) (mandatory)
=item -b
B<box_size> box size (1000000 by default)
=item -c
B<count coverage values> use this flag for a sum of coverage values.
Without it each line will be counted as 1, and the values for the coverage from column 3 will not be summed (default F)
=item -o
B<out_file> Output file for writing. Defaults to infile.bins
=item -h
B<help> print the help
=back
=cut
=head1 DESCRIPTION
This script count ocurrences from a tab delimited file (generally sequence coordinates). These could be read from a .vcf file (the first 2 columns will be used)
For example: pileup2boxes.pl -i myfile.tab -b 1000
chr1 2 chr1 1000 2
chr1 678 chr1 2000 1
chr1 1230 => chr1 3000 0
chr1 4536 chr1 4000 0
chr1 4690 chr1 5000 2
=cut
=head1 AUTHORS
Aureliano Bombarely Gomez <[email protected]>
Naama Menda <[email protected]>
=cut
=head1 METHODS
pileup2boxes.pl
=cut
use strict;
use warnings;
use Math::Round qw( nhimult );
use Getopt::Std;
use File::Slurp;
our ($opt_i, $opt_s, $opt_b, $opt_h, $opt_o, $opt_c);
getopts("i:s:b:o:ch");
if (!$opt_i && !$opt_s && !$opt_b && !$opt_h) {
print "There are n\'t any tags. Print help\n\n";
help();
}
if ($opt_h) {
help();
}
## Get the arguments and check them
my $in = $opt_i ||
die("ARGUMENTS ERROR: -i <inputtabfile> option was not supplied.\n");
my $insz = $opt_s ||
die("ARGUMENTS ERROR: -s <id_size_file> option was not supplied.\n");
my $box = $opt_b || 1000000;
my $out = $opt_o || $in . ".bins" ;
print STDERR "\n\n1) Parsing the ID sizes file.\n\n";
open my $sfh, '<', $insz or die("ERROR to open the file $insz: $!\n");
my %sizes = ();
while(<$sfh>) {
chomp($_);
my @data = split(/\t/, $_);
$sizes{$data[0]} = $data[1];
print STDERR "Size for $data[0] = $data[1] \n";
}
print STDERR "\n\n2) Parsing the $in file.\n\n";
open my $ifh, '<', $in or die("ERROR to open the file $in: $!\n");
my %id = ();
my $crr_box = $box;
my $l = 0;
if ($opt_c) {
print STDERR "opt_c : Counting sum of coverage values.\n\n";
}
while(<$ifh>) {
chomp($_);
$l++;
###print STDERR "\tParsing the line $l \r";
my @data = split(/\t/, $_);
my $pos = $data[1];
my $chr = $data[0];
my $value = $data[2];
#print STDERR "\n$chr \t $pos \t box = $crr_box\n" ;
if (exists $id{ $chr }) {
if ( nhimult($box, $pos) == $crr_box) {
#print STDERR "INCREMENTING box $crr_box ($chr \t $pos ) \n";
if ($opt_c) { $id{ $chr }->{$crr_box} += ( $value -1 ) ;}
$id{ $chr }->{$crr_box} += 1;
}
else {
$crr_box = nhimult($box , $pos);
#print STDERR "NEW BOX: $crr_box ( $chr \t $pos) \n";
if($opt_c) { $id{ $chr }->{$crr_box} = $value ; }
else { $id{ $chr }->{$crr_box} = 1; }
}
}
else {
$crr_box = nhimult($box , $pos);
print STDERR "new chromosome $chr. Box = $box , crr_box = $crr_box\n";
print STDERR "NEW BOX: $crr_box ( $chr \t $pos) value = $value \n";
if($opt_c) { $id{ $chr }->{$crr_box} = $value -1 ; }
else { $id{ $chr }->{ $crr_box } = 1; }
}
}
print STDERR "\n\n";
print STDERR "3) Producing output .\n\n";
## It will scan the sizes
foreach my $chr (sort keys %sizes) {
print STDERR "Binning chromosome $chr \n";
if (defined $id{$chr} ) {
my $max = $sizes{$chr};
my %feats = %{$id{$chr}};
my $c = $box;
while($c <= $max) {
if ($c < $max ) {
if (exists $feats{$c}) {
#print STDERR "Found box: $chr : $c\n";
write_file($out, { append => 1} , ( "$chr\t$c\t$feats{$c}\n") );
}
else {
write_file($out, { append => 1} , ( "$chr\t$c\t0\n") );
}
}
if ($c == $max) {
if (exists $feats{$c}) {
write_file($out, { append => 1} , ( "$chr\t$c\t$feats{$c}\n") );
}
else {
write_file($out, { append => 1} , ( "$chr\t$c\t0\n") );
}
}
$c += $box;
}
}
}
print STDERR "\n\nDONE\n\n";
=head2 help
Usage: help()
Desc: print help of this script
Ret: none
Args: none
Side_Effects: exit of the script
Example: if (!@ARGV) {
help();
}
=cut
sub help {
print STDERR <<EOF;
$0:
Description:
This script count ocurrences for a tab file (generally sequence coord.).
For example: pileup2boxes.pl -i myfile.tab -b 1000
chr1 2 chr1 0 2
chr1 678 chr1 1000 1
chr1 1230 => chr1 2000 0
chr1 4536 chr1 3000 0
chr1 4690 chr1 4000 2
Usage:
tab2boxes.pl [-h] -i <inputtabfile> -s <id_sizes_file> [-b <box_size>]
Flags:
-i <inputtabfile> tabular file (mandatory)
-s <id_sizes_file> id_sizes file (two columns: ID, SIZE) (mand.)
-b <box_size> box size (1000000 by default)
-o <out_file> output file. Defaults to input_file.bins
-h <help> print the help
EOF
exit (1);
}