/usr/bin/coverage_to_topoview is in libbio-graphics-perl 2.40-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 | #!/usr/bin/perl -w
use strict;
use BerkeleyDB;
use Data::Dumper;
use Getopt::Long;
# Based on http:/flybase.org/static_pages/docs/software/index_cov_files.pl
# This script will process the output of bam_coverage_windows.pl
# to the data structure needed by the topoview glyph
# Sheldon McKay (sheldon.mckay@gmail.com)
my ($log,$outdir,$help);
GetOptions (
"log" => \$log,
"output-dir=s" => \$outdir,
"help" => \$help
);
my $usage = q(
Usage: perl coverage_to_topoview.pl [-o output_dir] [-h] [-l] file1.wig.gz file2.wig.gz ...
-o output directory (default 'topoview')
-l use log2 for read counts (recommended)
-h this help message
);
die $usage if !@ARGV || $help;
$outdir ||= 'topoview';
my (%bdb_hash,$max_signal,@SubsetNames);
system "mkdir -p $outdir";
my $outfile = "$outdir/data.cat";
unlink $outfile if -e $outfile;
open( COV, '>' . $outfile ) || die "Cannot open $outfile!";
my $hashfile = "$outdir/index.bdbhash";
unlink $hashfile if -e $hashfile;
%bdb_hash = ();
tie(%bdb_hash, "BerkeleyDB::Hash",
-Filename => $hashfile,
-Flags => DB_CREATE);
$max_signal = 0;
@SubsetNames = ();
for my $file ( sort @ARGV ) {
next unless is_bed4($file);
indexCoverageFile($file);
}
$bdb_hash{'subsets'} = join( "\t", @SubsetNames );
$bdb_hash{'max_signal'} = $max_signal;
my @all_keys = keys %bdb_hash;
for my $kkey ( sort @all_keys ) {
print "\t$kkey => " . $bdb_hash{$kkey} . "\n";
}
if ( $max_signal > 10000 ) {
warn "WARNING: max_signal=$max_signal - TOO HIGH. Consider log2?\n";
}
untie %bdb_hash;
chmod( 0666, $hashfile ); # ! sometimes very important
close COV;
sub is_bed4 {
my $file = shift;
my $cat = $file =~ /gz$/ ? 'zcat'
: $file =~ /bz2/ ? 'bzcat'
: 'cat';
open WIG, "$cat $file |" or die "could not open $file: $!";
my $idx;
while (<WIG>) {
next if /^track/;
last if ++$idx > 9;
my ($ref,$start,$end,$score,@other) = split "\t";
if (@other > 0) {
die "Extra fields, I was expecting BED4";
}
unless ($ref && $start && $end && $score) {
die "Not enough fields, I was expecting BED4";
}
unless (is_numeric($start) && is_numeric($end)) {
die "start ($start) and end ($end) are supposed to be numbers";
}
unless (is_numeric($score)) {
die "score ($score) is not numeric"
}
}
return 1;
}
sub is_numeric {
no warnings;
return defined eval { $_[ 0] == 0 };
}
sub indexCoverageFile {
my $file = shift;
my $zcat = get_zcat($file);
open( INF, "$zcat $file |" ) || die "Can't open $file";
chomp(my $SubsetName = `basename $file .wig.gz`);
print STDERR "Subset=$SubsetName\n";
push( @SubsetNames, $SubsetName );
my $old_ref = "";
my @offsets = ();
my $step = 1000;
my $coordstep = 5000;
my $counter = 0;
my $offset = tell(COV);
$bdb_hash{$SubsetName} = $offset; # record offset where new subset data starts
my $old_signal = 0;
my $old_coord = -200000;
my $start = 0;
my $lastRecordedCoord = -200000;
my @signals;
while (<INF>) {
$offset = tell(COV);
next if /^$/ || /^\#/;
my ($ref,$start,$end,$signal) = split;
$signal = log($signal)/log(2) if $log;
$start += 1; # zero-based, half-open coords in BED
$signal = 0 if $signal < 0;
# New chromosome
if ( $ref ne $old_ref ) {
print STDERR "chromosome = $ref\n";
dumpOffsets( $start, $SubsetName . ':' . $old_ref, @offsets )
unless $old_ref eq ""; # previous subset:arm
$old_ref = $ref;
print COV "# subset=$SubsetName chromosome=$old_ref\n";
$offset = tell(COV);
$bdb_hash{ $SubsetName . ':' . $old_ref } =
$offset; # record offset where new subset:arm data starts
@offsets = ("-200000\t$offset");
print COV "-200000\t0\n"; # insert one fictive zero read
$offset = tell(COV);
print COV "0\t0\n"; # insert one more fictive zero read
push( @offsets, "0\t$offset" );
$counter = 0;
$old_signal = 0;
$old_coord = 0;
$lastRecordedCoord = 0;
}
# fill in holes in coverage with 0
if ($start > $old_coord+1 && $old_signal > 0) {
print COV join("\t",++$old_coord,0), "\n";
$old_coord++;
$counter++;
$offset = tell(COV);
$old_signal = 0;
}
if ( $signal == $old_signal) {
$old_coord = $end;
next;
}
$max_signal = $signal if $max_signal < $signal;
if ( $counter++ > $step
|| $start - $lastRecordedCoord > $coordstep )
{
push( @offsets, "$start\t$offset" );
$counter = 0;
$lastRecordedCoord = $start;
}
$old_coord = $end;
$old_signal = $signal;
print COV join("\t",$start,$signal), "\n";
}
# don't forget to dump offsets data on file end..
dumpOffsets( $start,$SubsetName . ':' . $old_ref, @offsets )
unless $old_ref eq ""; # previous subset:arm
close(INF);
return;
}
sub dumpOffsets {
my ( $start, $key, @offsetlines ) = @_;
print COV "# offsets for $key\n";
my $offset = tell(COV);
my $prevoffset = $offset;
$bdb_hash{ $key . ':offsets' } = $offset
; # record offset where offsets VALUES for subset:arm data start (skip header)
my $oldbigstep = 0;
foreach my $str (@offsetlines) {
print COV $str . "\n";
my ( $start, $floffset ) = split( /[ \t]+/, $str );
# following wasn't working properly..
my $newbigstep = int( $start / 1000000.0 );
if ( $newbigstep > $oldbigstep ) {
$bdb_hash{ $key . ':offsets:' . $newbigstep } =
$prevoffset; # one before is the right start
$oldbigstep = $newbigstep;
}
$prevoffset = $offset;
$offset = tell(COV);
}
return;
}
#***********************************************************
#
#***********************************************************
sub get_zcat {
my $fullfile = shift;
if ( $fullfile =~ /\.gz$/i ) {
my $zcat = `which zcat`;
if ( $? != 0 ) { $zcat = `which gzcat`; }
chomp($zcat);
return ($zcat);
}
elsif ( $fullfile =~ /\.bz2$/i ) { return ('bzcat'); }
return ('/bin/cat');
}
#*******
|