-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcoverageBedParser.pl
executable file
·56 lines (49 loc) · 1.52 KB
/
coverageBedParser.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/env perl
=hey
Author: Shijian Sky Zhang
E-mail: [email protected]
=cut
use 5.010;
use warnings;
use strict;
use Getopt::Long;
use File::Basename;
my $bedFormat = 3;
sub usage{
my $scriptName = basename $0;
print <<HELP;
Usage: perl $scriptName coverageBedOutput.tsv >OUTPUT.tsv
If coverageBedOutput.tsv isn't specified, input from STDIN
Option:
-b --bedFormat INT Bed format ([$bedFormat], 6)
-h --help Print this help information
HELP
}
GetOptions(
'b|bedFormat=s' => \$bedFormat,
'h|help' => sub{usage(); exit}
) || usage();
$ARGV[0] = '-' unless defined $ARGV[0];
open IN, "$ARGV[0]" or die "Can't read file ($ARGV[0]): $!";
my %regions;
while(<IN>){
chomp;
next if /^all/;
my @fields = split "\t";
my ($key, $depth, $siteCount, $regionSize, $fraction);
if($bedFormat == 3){
$key = join "\t", @fields[0..2];
}elsif($bedFormat == 6){
$key = join "\t", @fields[0..5];
}
($depth, $siteCount, $regionSize, $fraction) = @fields[($#fields-3)..$#fields];
$regions{$key}{depth} += $depth * $siteCount;
$regions{$key}{size} = $regionSize;
$regions{$key}{coveredSize} += $siteCount if $depth > 0;
}
for my $region(keys %regions){
my $meanDepth = $regions{$region}{depth}/$regions{$region}{size};
my $coveredSite = exists $regions{$region}{coveredSize} ? $regions{$region}{coveredSize} : 0;
my $coveredFraction = $coveredSite / $regions{$region}{size};
say join "\t", ($region, $meanDepth, $coveredFraction);
}