annotate methylation_analysis/methylation_by_region_converter.pl @ 5:23fed0cf09f4 draft

Uploaded
author fcaramia
date Mon, 03 Dec 2012 18:27:11 -0500
parents 282edadee017
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
282edadee017 Uploaded
fcaramia
parents:
diff changeset
1 #!/usr/bin/perl
282edadee017 Uploaded
fcaramia
parents:
diff changeset
2
282edadee017 Uploaded
fcaramia
parents:
diff changeset
3 # script to take a bed file of target regions and a series of bedgraphs from bismark and create a bedgrapgh with methylation percentages aggregated by region
282edadee017 Uploaded
fcaramia
parents:
diff changeset
4
282edadee017 Uploaded
fcaramia
parents:
diff changeset
5 # Created by Jason Ellul, Oct 2012
282edadee017 Uploaded
fcaramia
parents:
diff changeset
6
282edadee017 Uploaded
fcaramia
parents:
diff changeset
7
282edadee017 Uploaded
fcaramia
parents:
diff changeset
8 use strict;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
9 use warnings;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
10 use Getopt::Std;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
11 use File::Basename;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
12 use Data::Dumper;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
13 $| = 1;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
14
282edadee017 Uploaded
fcaramia
parents:
diff changeset
15 # Grab and set all options
282edadee017 Uploaded
fcaramia
parents:
diff changeset
16 my %OPTIONS = (s => "MethylationData");
282edadee017 Uploaded
fcaramia
parents:
diff changeset
17 getopts('l:L:o:s:', \%OPTIONS);
282edadee017 Uploaded
fcaramia
parents:
diff changeset
18
282edadee017 Uploaded
fcaramia
parents:
diff changeset
19 die qq(
282edadee017 Uploaded
fcaramia
parents:
diff changeset
20 Usage: methylation_by_region_converter.pl [OPTIONS] <bed file> <bedGraph 1> [<bedGraph 2> ...]
282edadee017 Uploaded
fcaramia
parents:
diff changeset
21
282edadee017 Uploaded
fcaramia
parents:
diff changeset
22 OPTIONS: -o STR the name of the output file
282edadee017 Uploaded
fcaramia
parents:
diff changeset
23 -l STR filename of the log file [null]
282edadee017 Uploaded
fcaramia
parents:
diff changeset
24 -L STR append to an existing log file [null]
282edadee017 Uploaded
fcaramia
parents:
diff changeset
25 -s STR Sample Name [$OPTIONS{s}]
282edadee017 Uploaded
fcaramia
parents:
diff changeset
26 ) if(@ARGV < 2);
282edadee017 Uploaded
fcaramia
parents:
diff changeset
27
282edadee017 Uploaded
fcaramia
parents:
diff changeset
28 my $version = 0.1;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
29 my $bed = shift @ARGV;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
30 my @graphs = @ARGV;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
31 my $Script = "methylation_by_region_converter.pl";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
32 my $now;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
33
282edadee017 Uploaded
fcaramia
parents:
diff changeset
34 # if log file specified
282edadee017 Uploaded
fcaramia
parents:
diff changeset
35 if(defined $OPTIONS{l}) {
282edadee017 Uploaded
fcaramia
parents:
diff changeset
36 open (FH, ">$OPTIONS{l}") or die "Couldn't create log file $OPTIONS{l}!\n";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
37 close (FH);
282edadee017 Uploaded
fcaramia
parents:
diff changeset
38 # Open the log file and redirect output to it
282edadee017 Uploaded
fcaramia
parents:
diff changeset
39 open (STDERR, ">>$OPTIONS{l}") or die "Couldn't write to log file $OPTIONS{l}!\n";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
40 my $now = localtime time;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
41 print "Log File Created $now\n";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
42 } elsif(defined $OPTIONS{L}) {
282edadee017 Uploaded
fcaramia
parents:
diff changeset
43 #append to a log file
282edadee017 Uploaded
fcaramia
parents:
diff changeset
44 # Open the log file and redirect output to it
282edadee017 Uploaded
fcaramia
parents:
diff changeset
45 open (STDERR, ">>$OPTIONS{L}") or die "Couldn't append to log file $OPTIONS{L}!\n";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
46 my $now = localtime time;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
47 print "Appending To Log File $now\n\n";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
48 }
282edadee017 Uploaded
fcaramia
parents:
diff changeset
49
282edadee017 Uploaded
fcaramia
parents:
diff changeset
50 # print version of this script.
282edadee017 Uploaded
fcaramia
parents:
diff changeset
51 print STDERR "Using $Script version $version\n\n";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
52 print STDERR "Using region file: $bed\n";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
53 print STDERR "And bedgraphs: @graphs\n\n";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
54 my %Regions;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
55 my @chr_order;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
56
282edadee017 Uploaded
fcaramia
parents:
diff changeset
57 # read in regions file
282edadee017 Uploaded
fcaramia
parents:
diff changeset
58 print STDERR "Reading Regions Bed File $bed ... ";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
59 open(BED, "$bed") || die "$bed: $!";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
60 while(my $line = <BED>) {
282edadee017 Uploaded
fcaramia
parents:
diff changeset
61 chomp $line;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
62 my @line_sp = split("\t", $line);
282edadee017 Uploaded
fcaramia
parents:
diff changeset
63 $line_sp[2]--;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
64
282edadee017 Uploaded
fcaramia
parents:
diff changeset
65 push @chr_order, $line_sp[0] unless defined $Regions{$line_sp[0]};
282edadee017 Uploaded
fcaramia
parents:
diff changeset
66 push @{ $Regions{$line_sp[0]}{Start} }, $line_sp[1];
282edadee017 Uploaded
fcaramia
parents:
diff changeset
67 push @{ $Regions{$line_sp[0]}{End} }, $line_sp[2];
282edadee017 Uploaded
fcaramia
parents:
diff changeset
68 push @{ $Regions{$line_sp[0]}{Methylated} }, 0;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
69 push @{ $Regions{$line_sp[0]}{Unmethylated} }, 0;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
70 }
282edadee017 Uploaded
fcaramia
parents:
diff changeset
71 close(BED);
282edadee017 Uploaded
fcaramia
parents:
diff changeset
72 print STDERR "Done.\n";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
73
282edadee017 Uploaded
fcaramia
parents:
diff changeset
74 # read in bedgraphs
282edadee017 Uploaded
fcaramia
parents:
diff changeset
75 foreach my $bedGraph (@graphs) {
282edadee017 Uploaded
fcaramia
parents:
diff changeset
76 print STDERR "Loading bedGraph File $bedGraph ... ";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
77 open(GRAPH, $bedGraph) || die "$bedGraph: $!";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
78 my @lines = <GRAPH>;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
79 close(GRAPH);
282edadee017 Uploaded
fcaramia
parents:
diff changeset
80 print STDERR "Done.\n\n";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
81
282edadee017 Uploaded
fcaramia
parents:
diff changeset
82 print STDERR "Processing bedGraph File ... ";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
83 foreach my $line (@lines) {
282edadee017 Uploaded
fcaramia
parents:
diff changeset
84 chomp $line;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
85 my @line_sp = split("\t", $line);
282edadee017 Uploaded
fcaramia
parents:
diff changeset
86 $line_sp[1]++;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
87
282edadee017 Uploaded
fcaramia
parents:
diff changeset
88 # if the chromosome is in the regions file
282edadee017 Uploaded
fcaramia
parents:
diff changeset
89 if(defined $Regions{$line_sp[0]}) {
282edadee017 Uploaded
fcaramia
parents:
diff changeset
90 my $pos = binsearchregion($line_sp[1], \&cmpFunc, \@{ $Regions{$line_sp[0]}{Start} }, \@{ $Regions{$line_sp[0]}{End} });
282edadee017 Uploaded
fcaramia
parents:
diff changeset
91
282edadee017 Uploaded
fcaramia
parents:
diff changeset
92 # if the position is within a region
282edadee017 Uploaded
fcaramia
parents:
diff changeset
93 if($pos >= 0) {
282edadee017 Uploaded
fcaramia
parents:
diff changeset
94 ${ $Regions{$line_sp[0]}{Methylated} }[$pos] += $line_sp[4];
282edadee017 Uploaded
fcaramia
parents:
diff changeset
95 ${ $Regions{$line_sp[0]}{Unmethylated} }[$pos] += $line_sp[5];
282edadee017 Uploaded
fcaramia
parents:
diff changeset
96 }
282edadee017 Uploaded
fcaramia
parents:
diff changeset
97 }
282edadee017 Uploaded
fcaramia
parents:
diff changeset
98 }
282edadee017 Uploaded
fcaramia
parents:
diff changeset
99 print STDERR "Done.\n\n";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
100 }
282edadee017 Uploaded
fcaramia
parents:
diff changeset
101
282edadee017 Uploaded
fcaramia
parents:
diff changeset
102 if(defined $OPTIONS{o}) {
282edadee017 Uploaded
fcaramia
parents:
diff changeset
103 open (STDOUT, ">$OPTIONS{o}") || die "$OPTIONS{o}: $!";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
104 }
282edadee017 Uploaded
fcaramia
parents:
diff changeset
105
282edadee017 Uploaded
fcaramia
parents:
diff changeset
106 # calculate percent methylated and print
282edadee017 Uploaded
fcaramia
parents:
diff changeset
107 print STDERR "Printing output ... ";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
108 print "#type=DNA_METHYLATION\n";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
109 print "'ID\tchrom\tloc.start\tloc.end\tMethylated\tUnmethylated\tTotal\tFractionMethylated\n";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
110 foreach my $chr (@chr_order) {
282edadee017 Uploaded
fcaramia
parents:
diff changeset
111 for(my $region = 0; $region < @{ $Regions{$chr}{Start} }; $region++) {
282edadee017 Uploaded
fcaramia
parents:
diff changeset
112 my $total = ${ $Regions{$chr}{Methylated} }[$region] + ${ $Regions{$chr}{Unmethylated} }[$region];
282edadee017 Uploaded
fcaramia
parents:
diff changeset
113 my $fract = sprintf("%.3f", ${ $Regions{$chr}{Methylated} }[$region] / $total) if $total;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
114 print "$OPTIONS{s}\t$chr\t${ $Regions{$chr}{Start} }[$region]\t${ $Regions{$chr}{End} }[$region]\t" if $total;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
115 print "${ $Regions{$chr}{Methylated} }[$region]\t${ $Regions{$chr}{Unmethylated} }[$region]\t$total\t$fract\n" if $total;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
116 }
282edadee017 Uploaded
fcaramia
parents:
diff changeset
117 }
282edadee017 Uploaded
fcaramia
parents:
diff changeset
118 close(STDERR) if defined $OPTIONS{o};
282edadee017 Uploaded
fcaramia
parents:
diff changeset
119 print STDERR "Done.\n";
282edadee017 Uploaded
fcaramia
parents:
diff changeset
120
282edadee017 Uploaded
fcaramia
parents:
diff changeset
121 sub binsearchregion {
282edadee017 Uploaded
fcaramia
parents:
diff changeset
122 my ($target, $cmp, $start, $end) = @_;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
123
282edadee017 Uploaded
fcaramia
parents:
diff changeset
124 my $posmin = 0;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
125 my $posmax = $#{ $start };
282edadee017 Uploaded
fcaramia
parents:
diff changeset
126
282edadee017 Uploaded
fcaramia
parents:
diff changeset
127 return -1 if &$cmp (0, $start, $target) > 0;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
128 return -1 if &$cmp ($#{ $end }, $end, $target) < 0;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
129
282edadee017 Uploaded
fcaramia
parents:
diff changeset
130 while (1) {
282edadee017 Uploaded
fcaramia
parents:
diff changeset
131 my $mid = int (($posmin + $posmax) / 2);
282edadee017 Uploaded
fcaramia
parents:
diff changeset
132 my $result = &$cmp ($mid, $start, $target);
282edadee017 Uploaded
fcaramia
parents:
diff changeset
133
282edadee017 Uploaded
fcaramia
parents:
diff changeset
134 if ($result < 0) {
282edadee017 Uploaded
fcaramia
parents:
diff changeset
135 $posmin = $posmax, next if $mid == $posmin && $posmax != $posmin;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
136 if($mid == $posmin) {
282edadee017 Uploaded
fcaramia
parents:
diff changeset
137 return -1 if &$cmp ($mid, $end, $target) < 0;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
138 return $mid;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
139 }
282edadee017 Uploaded
fcaramia
parents:
diff changeset
140 $posmin = $mid;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
141 } elsif ($result > 0) {
282edadee017 Uploaded
fcaramia
parents:
diff changeset
142 $posmax = $posmin, next if $mid == $posmax && $posmax != $posmin;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
143 if($mid == $posmax) {
282edadee017 Uploaded
fcaramia
parents:
diff changeset
144 $mid--;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
145 return -1 if &$cmp ($mid, $end, $target) < 0;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
146 return $mid;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
147 }
282edadee017 Uploaded
fcaramia
parents:
diff changeset
148 $posmax = $mid;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
149 } else {
282edadee017 Uploaded
fcaramia
parents:
diff changeset
150 return $mid;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
151 }
282edadee017 Uploaded
fcaramia
parents:
diff changeset
152 }
282edadee017 Uploaded
fcaramia
parents:
diff changeset
153 }
282edadee017 Uploaded
fcaramia
parents:
diff changeset
154
282edadee017 Uploaded
fcaramia
parents:
diff changeset
155 sub cmpFunc {
282edadee017 Uploaded
fcaramia
parents:
diff changeset
156 my ($index, $arrayRef, $target) = @_;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
157 my $item = $$arrayRef[$index];
282edadee017 Uploaded
fcaramia
parents:
diff changeset
158
282edadee017 Uploaded
fcaramia
parents:
diff changeset
159 return $item <=> $target;
282edadee017 Uploaded
fcaramia
parents:
diff changeset
160 }
282edadee017 Uploaded
fcaramia
parents:
diff changeset
161