blib/script/bed2bedGraph.pl untested

filter: Statementstmt 0.0% 0 / 107 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 31 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod n/a totaltotal 0.0% 0 / 170 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/bin/env perl
2 # -*-CPerl-*-
3 # Last changed Time-stamp: <2014-12-13 01:03:41 mtw>
4  
5 ###############
6 ###Use stuff
7 ###############
8 use strict;
9 use warnings;
10 use PerlIO::gzip;
11 use Cwd;
12 use File::Path qw (make_path);
13 use Pod::Usage;
14 use Getopt::Long qw( :config posix_default bundling no_ignore_case );
15 use Bio::ViennaNGS::Util qw(unique_array);
16  
17 ###############
18 ###Variables
19 ###############
20  
21 my $VERBOSE=0;
22 my ( $file, $type, $peak, $chromsize, $track, $conv, $anno );
23  
24 ###############
25 ###Command Line Options
26 ###############
27  
28 pod2usage(-verbose => 0)
29 unless GetOptions(
30 "file|f=s" => \$file,
31 "type|t=s" => \$type,
32 "chrom|c=s" => \$chromsize,
33 "anno|a=s" => \$anno,
34 "help|h" => sub{pod2usage(-verbose => 1)},
35 "man|m" => sub{pod2usage(-verbose => 2)},
36 "verbose" => sub{ $VERBOSE++ }
37 );
38  
39  
40 my $dir = cwd();
41  
42 my $file = file("Bedgraph", $file);
43  
44 make_path($file) unless (-d $file);
45  
46 die "You must provide filename and cell-type, and file with chrom-sizes\n"
47 unless ($file && $type && $chromsize);
48  
49 ###############
50 ###MAIN
51 ###############
52  
53 my $pid = $$;
54 (my $job = `cat /proc/$pid/cmdline`)=~ s/\0/ /g;
55 print STDERR $job,"\n";
56 print STDERR "Adding annotation information\n" if ($anno && $anno eq "extended");
57 my %sizes=();
58  
59 open (SIZE, "<:gzip(autopop)", $chromsize);
60 while (<SIZE>){
61 chomp (my $raw = $_);
62 my @line = split("\t",$raw);
63 $sizes{"$line[0]"}=$line[1];
64 }
65 close (SIZE);
66  
67 open (IN, "<:gzip(autopop)" , $file);
68 chdir("Bedgraph/$file") or die "Could not move to Bedgraph $!\n";
69  
70 my %covplus = ();
71 my %covminus = ();
72 my %anno = ();
73 my %annop = ();
74 my %annom = ();
75  
76 while (<IN>){
77 chomp (my $raw = $_);
78 push my @line , split (/\t/,$raw);
79 push @line, "\." if ( !$line[5] );
80 my $chrom = $line[0];#)=~ s/chr//g;
81 my $start = $line[1];
82 my $end = $line[2];
83 my $strand = $line[5];
84 my $annotation = join("\t",$line[3],$line[4]);
85 for (5..$#line){
86 $annotation .= "\t".$line[$_];
87 }
88 for (my $i=$start;$i<=$end;$i++){
89 my $a=$i+1;
90 next if ($a > ($sizes{"$chrom"}));
91 if ($strand eq "+" or $strand eq "1"){
92 $covplus{"$chrom\t$i\t$a"}+=1;
93 push @{$annop{"$chrom\t$i\t$a"}}, $annotation if ($anno && $anno eq "extended");
94 }
95 elsif($strand eq "-" or $strand eq "-1"){
96 $covminus{"$chrom\t$i\t$a"}+=1;
97 push @{$annom{"$chrom\t$i\t$a"}}, $annotation if ($anno && $anno eq "extended");
98 }
99 }
100 }
101 close (IN);
102  
103 foreach my $key (keys %covplus){
104 my $cov=$covplus{$key};
105 my @an = unique_array(\@{$annop{$key}});
106 my $annotation = join("\t",@an);
107 my @tmp=split(/\t/,$key);
108 my $chrom=$tmp[0];
109 open (OUT1, ">>:gzip", "$chrom\.$type\.fw.gz");
110 if ($anno && $anno eq "extended"){
111 print OUT1 "$key\t$cov\t$annotation\n";
112 }
113 else{
114 print OUT1 "$key\t$cov\n";
115 }
116 close (OUT1);
117 }
118  
119 foreach my $key (keys %covminus){
120 my $cov=$covminus{$key}; # at least one occurence
121 my @an = unique_array(\@{$annom{$key}});
122 my $annotation = join("\t",@an);
123 my @tmp=split(/\t/,$key);
124 my $chrom=$tmp[0];
125 open (OUT2, ">>:gzip", "$chrom\.$type\.re.gz");
126 if ($anno && $anno eq "extended"){
127 print OUT2 "$key\t-$cov\t$annotation\n";
128 }
129 else{
130 print OUT2 "$key\t-$cov\n";
131 }
132 close (OUT2);
133 }
134  
135 `for i in \`ls chr\*\.fw.gz\`;do zcat \$i|sort -k1,1 -k2,2 -n|gzip > \$i\.bedg.gz;done`;
136 `for i in \`ls chr\*\.re.gz\`;do zcat \$i|sort -k1,1 -k2,2 -n|gzip > \$i\.bedg.gz;done`;
137  
138 chdir ($dir) or die "Could not move to $dir $!\n";
139  
140  
141 ###############
142 ###POD
143 ###############
144  
145 __END__
146  
147 =head1 NAME
148  
149 bed2bedGraph.pl - Convert BED or extended BED files to
150 bedGraph format
151  
152 =head1 SYNOPSIS
153  
154 bed2bedGraph.pl [-f I<FILE>] [-c I<FILE>] [-t I<STRING>] [-a
155 I<STRING>] [options]
156  
157 =head1 DESCRIPTION
158  
159 This program converts BED files to strand specific bedGraph files,
160 allowing additional annotation and automatic generation of bedGraph
161 files which can easily be converted to big-type files for UCSC
162 visualization.
163  
164 =head1 OPTIONS
165  
166 =over
167  
168 =item B<-f>
169  
170 BED file for conversion.
171  
172 =item B<-c>
173  
174 File containing chromosome sizes
175  
176 =item B<-t>
177  
178 Type of bed file (e.g. sample name, replicate name, condition, ...)
179  
180 =item B<-a>
181  
182 Whether file is a standard bed or extended bed, 'extended' for extended bed
183  
184 =item B<--help -h>
185  
186 Print short help
187  
188 =item B<--man>
189  
190 Prints the manual page and exits
191  
192 =back
193  
194  
195  
196 =head1 AUTHOR
197  
198 Joerg Fallmann E<lt>joerg.fallmann@univie.ac.atE<gt>
199  
200 =cut
201  
202  
203 ##################################END################################
204