blib/script/coverage_to_topoview.pl untested

filter: Statementstmt 0.0% 0 / 150 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 269 CC 23 SCAR 43.0
CC
23
Cov
0%
indexCoverageFile
132.0
is_bed4
90.0
get_zcat
20.0
CRAP 73.9
1 #!/usr/local/bin/perl -w
2 use strict;
3 use BerkeleyDB;
4 use Data::Dumper;
5 use Getopt::Long;
6  
7 # Based on http:/flybase.org/static_pages/docs/software/index_cov_files.pl
8 # This script will process the output of bam_coverage_windows.pl
9 # to the data structure needed by the topoview glyph
10 # Sheldon McKay (sheldon.mckay@gmail.com)
11  
12 my ($log,$outdir,$help);
13 GetOptions (
14 "log" => \$log,
15 "output-dir=s" => \$outdir,
16 "help" => \$help
17 );
18  
19 my $usage = q(
20 Usage: perl coverage_to_topoview.pl [-o output_dir] [-h] [-l] file1.wig.gz file2.wig.gz ...
21 -o output directory (default 'topoview')
22 -l use log2 for read counts (recommended)
23 -h this help message
24 );
25  
26 die $usage if !@ARGV || $help;
27  
28 $outdir ||= 'topoview';
29  
30 my (%bdb_hash,$max_signal,@SubsetNames);
31  
32 system "mkdir -p $outdir";
33  
34 my $outfile = "$outdir/data.cat";
35 unlink $outfile if -e $outfile;
36  
37 open( COV, '>' . $outfile ) || die "Cannot open $outfile!";
38 my $hashfile = "$outdir/index.bdbhash";
39 unlink $hashfile if -e $hashfile;
40  
41 %bdb_hash = ();
42 tie(%bdb_hash, "BerkeleyDB::Hash",
43 -Filename => $hashfile,
44 -Flags => DB_CREATE);
45  
46 $max_signal = 0;
47 @SubsetNames = ();
48  
49 for my $file ( sort @ARGV ) {
50 next unless is_bed4($file);
51 indexCoverageFile($file);
52 }
53  
54 $bdb_hash{'subsets'} = join( "\t", @SubsetNames );
55 $bdb_hash{'max_signal'} = $max_signal;
56  
57 my @all_keys = keys %bdb_hash;
58  
59 for my $kkey ( sort @all_keys ) {
60 print "\t$kkey => " . $bdb_hash{$kkey} . "\n";
61 }
62  
63 if ( $max_signal > 10000 ) {
64 warn "WARNING: max_signal=$max_signal - TOO HIGH. Consider log2?\n";
65 }
66  
67 untie %bdb_hash;
68 chmod( 0666, $hashfile ); # ! sometimes very important
69  
70 close COV;
71  
72  
73 sub is_bed4 {
74 my $file = shift;
75 my $cat = $file =~ /gz$/ ? 'zcat'
76 : $file =~ /bz2/ ? 'bzcat'
77 : 'cat';
78 open WIG, "$cat $file |" or die "could not open $file: $!";
79
80 my $idx;
81 while (<WIG>) {
82 next if /^track/;
83 last if ++$idx > 9;
84  
85 my ($ref,$start,$end,$score,@other) = split "\t";
86 if (@other > 0) {
87 die "Extra fields, I was expecting BED4";
88 }
89 unless ($ref && $start && $end && $score) {
90 die "Not enough fields, I was expecting BED4";
91 }
92 unless (is_numeric($start) && is_numeric($end)) {
93 die "start ($start) and end ($end) are supposed to be numbers";
94 }
95 unless (is_numeric($score)) {
96 die "score ($score) is not numeric"
97 }
98 }
99
100 return 1;
101 }
102  
103 sub is_numeric {
104 no warnings;
105 return defined eval { $_[ 0] == 0 };
106 }
107  
108 sub indexCoverageFile {
109 my $file = shift;
110 my $zcat = get_zcat($file);
111  
112 open( INF, "$zcat $file |" ) || die "Can't open $file";
113
114 chomp(my $SubsetName = `basename $file .wig.gz`);
115  
116 print STDERR "Subset=$SubsetName\n";
117  
118 push( @SubsetNames, $SubsetName );
119  
120 my $old_ref = "";
121 my @offsets = ();
122  
123 my $step = 1000;
124 my $coordstep = 5000;
125 my $counter = 0;
126 my $offset = tell(COV);
127  
128 $bdb_hash{$SubsetName} = $offset; # record offset where new subset data starts
129  
130 my $old_signal = 0;
131 my $old_coord = -200000;
132 my $start = 0;
133 my $lastRecordedCoord = -200000;
134  
135 my @signals;
136 while (<INF>) {
137 $offset = tell(COV);
138  
139 next if /^$/ || /^\#/;
140 my ($ref,$start,$end,$signal) = split;
141  
142 $signal = log($signal)/log(2) if $log;
143  
144 $start += 1; # zero-based, half-open coords in BED
145
146 $signal = 0 if $signal < 0;
147  
148 # New chromosome
149 if ( $ref ne $old_ref ) {
150 print STDERR "chromosome = $ref\n";
151 dumpOffsets( $start, $SubsetName . ':' . $old_ref, @offsets )
152 unless $old_ref eq ""; # previous subset:arm
153 $old_ref = $ref;
154 print COV "# subset=$SubsetName chromosome=$old_ref\n";
155 $offset = tell(COV);
156 $bdb_hash{ $SubsetName . ':' . $old_ref } =
157 $offset; # record offset where new subset:arm data starts
158 @offsets = ("-200000\t$offset");
159 print COV "-200000\t0\n"; # insert one fictive zero read
160 $offset = tell(COV);
161 print COV "0\t0\n"; # insert one more fictive zero read
162 push( @offsets, "0\t$offset" );
163 $counter = 0;
164 $old_signal = 0;
165 $old_coord = 0;
166 $lastRecordedCoord = 0;
167 }
168  
169
170 # fill in holes in coverage with 0
171 if ($start > $old_coord+1 && $old_signal > 0) {
172 print COV join("\t",++$old_coord,0), "\n";
173 $old_coord++;
174 $counter++;
175 $offset = tell(COV);
176 $old_signal = 0;
177 }
178
179 if ( $signal == $old_signal) {
180 $old_coord = $end;
181 next;
182 }
183
184 $max_signal = $signal if $max_signal < $signal;
185 if ( $counter++ > $step
186 || $start - $lastRecordedCoord > $coordstep )
187 {
188 push( @offsets, "$start\t$offset" );
189 $counter = 0;
190 $lastRecordedCoord = $start;
191 }
192  
193 $old_coord = $end;
194 $old_signal = $signal;
195
196 print COV join("\t",$start,$signal), "\n";
197 }
198  
199 # don't forget to dump offsets data on file end..
200 dumpOffsets( $start,$SubsetName . ':' . $old_ref, @offsets )
201 unless $old_ref eq ""; # previous subset:arm
202 close(INF);
203 return;
204 }
205  
206 sub dumpOffsets {
207 my ( $start, $key, @offsetlines ) = @_;
208 print COV "# offsets for $key\n";
209 my $offset = tell(COV);
210 my $prevoffset = $offset;
211 $bdb_hash{ $key . ':offsets' } = $offset
212 ; # record offset where offsets VALUES for subset:arm data start (skip header)
213 my $oldbigstep = 0;
214 foreach my $str (@offsetlines) {
215 print COV $str . "\n";
216 my ( $start, $floffset ) = split( /[ \t]+/, $str );
217  
218 # following wasn't working properly..
219 my $newbigstep = int( $start / 1000000.0 );
220 if ( $newbigstep > $oldbigstep ) {
221 $bdb_hash{ $key . ':offsets:' . $newbigstep } =
222 $prevoffset; # one before is the right start
223 $oldbigstep = $newbigstep;
224 }
225 $prevoffset = $offset;
226 $offset = tell(COV);
227 }
228 return;
229 }
230  
231 #***********************************************************
232 #
233 #***********************************************************
234  
235 sub get_zcat {
236 my $fullfile = shift;
237 if ( $fullfile =~ /\.gz$/i ) {
238 my $zcat = `which zcat`;
239 if ( $? != 0 ) { $zcat = `which gzcat`; }
240 chomp($zcat);
241 return ($zcat);
242 }
243 elsif ( $fullfile =~ /\.bz2$/i ) { return ('bzcat'); }
244 return ('/bin/cat');
245 }
246  
247 #*******