| 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__ |