-
Notifications
You must be signed in to change notification settings - Fork 1
/
MethylFreq2wig.pl
47 lines (40 loc) · 1.74 KB
/
MethylFreq2wig.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/perl -w
use strict;
my $sampleID = $ARGV[0];
$sampleID = "Sample" if (!$sampleID);
my $minDepth = $ARGV[1];
$minDepth = 10 if(!$minDepth);
print "track type=wiggle_0 name=\"$sampleID:methRatio\" visibility=full color=20,150,20 altColor=150,20,20 windowingFunction=mean\n";
my %methylTable;
sub main(){
while(my $line = <STDIN>){
chomp($line);
my @fields = split(/\t/, $line);
my $strand = $fields[2] eq 'W' ? '+' : '-';
my %alleleCounts;
my $CT_counts;
for(my $i=5; $i<scalar(@fields); $i+=2){
$alleleCounts{$fields[$i]}=$fields[$i+1];
$CT_counts += $fields[$i+1] if($fields[$i]=~ /[CT]/);
}
next if(!$CT_counts || $CT_counts/$fields[3] < 0.9);
my $index=$fields[0] . ":" . $fields[1];
$alleleCounts{'C'} =0 if(!$alleleCounts{'C'});
$methylTable{$fields[0]}->{$fields[1]}->{'C'} += $alleleCounts{'C'} ;
$methylTable{$fields[0]}->{$fields[1]}->{'CT'} += $CT_counts;
}
report_methylFreqBED();
}
sub report_methylFreqBED(){
my $cur_chr = "NA";
foreach my $chr(sort keys(%methylTable)){
print "variableStep chrom=$chr\n";
foreach my $pos(sort {$a<=>$b} keys %{$methylTable{$chr}}){
next if($methylTable{$chr}->{$pos}->{'CT'}<$minDepth);
my $methylLevel = sprintf("%4.3f", $methylTable{$chr}->{$pos}->{'C'}/$methylTable{$chr}->{$pos}->{'CT'});
print $pos-1, "\t", $methylLevel, "\n";
print $pos, "\t", $methylLevel, "\n";
}
}
}
main();