ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/scripts/wigfix2bin.pl
Revision: 24
Committed: Tue Jul 26 21:46:39 2011 UTC (8 years, 1 month ago) by gpertea
File size: 1521 byte(s)
Log Message:
Line File contents
1 #!/usr/bin/perl
2 use strict;
3 my $usage=q/Usage:
4 wigfix2bin.pl <wigFixstream>
5 Converts wigFix to a fixed binary format:
6
7 bytes 00-08 : 'WIGB'<32bit-start-offset>
8 bytes 09... : 2-byte int = value*1000 for each base
9
10 /;
11 $_=<STDIN>;
12 die("Error: wigFix header not recognized!\n")
13 unless (m/^fixedStep\s/);
14 my ($chr, $cpos)=(m/chrom=(\S+)\s+start=(\d+)/);
15 die("Error: wigFix header not recognized!\n") unless $chr && $cpos;
16
17 open(BIN, ">$chr.wigfixbin") || die("Error creating file $chr.wigfixbin!\n");
18 binmode(BIN);
19 print STDERR "Writing $chr.wigfixbin ..\n";
20 print BIN "WIGB";
21 print BIN pack('I', $cpos);
22 while (<STDIN>) {
23 if (m/start=(\d+)/) {
24 my $skipto=$1;
25 my $skiplen=$skipto-$cpos;
26 for (my $i=0;$i<$skiplen;$i++) {
27 print BIN pack('s',0);
28 }
29 $cpos=$skipto;
30 next;
31 }
32 my ($v)=(m/^([\d\.\-]+)$/);
33 die("Error parsing value for line $. : $_\n") unless length($v)>0;
34 my $n=int($v*1000);
35 if ($n>32767 || $n<-32767) {
36 die("Error: value out of range ($n)!\n");
37 }
38 print BIN pack('s',$n);
39 $cpos++;
40 }
41 close(BIN);
42 print STDERR " ..done.\n";
43 #print STDERR "Waiting for 4s..\n";
44 #sleep(4);
45 #print STDERR "Now reading data back..\n";
46
47 # read back (testing):
48 # open(BIN, "$chr.wigfixbin");
49 # my ($tag, $r);
50 # read(BIN, $tag, 4);
51 # read(BIN, $r, 4);
52 # my @v=unpack('I',$r);
53 # print STDERR "Header tag=$tag, offset=$v[0]\n";
54 # while (read(BIN,$r,2)==2) {
55 # @v=unpack('s',$r);
56 # my $n=sprintf('%.3f', $v[0]/1000);
57 # print STDERR "read value: $n\n";
58 # }
59 # close(BIN);

Properties

Name Value
svn:executable *