-
Notifications
You must be signed in to change notification settings - Fork 16
/
clip_difference_analysis.pl
executable file
·178 lines (126 loc) · 4.73 KB
/
clip_difference_analysis.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env perl
use strict;
use warnings;
use Carp;
use Getopt::Long;
use File::Basename;
use Math::CDF qw(:all);
use Bed;
use Common;
my $verbose = 0;
my $prog = basename ($0);
my $filterIDFile = "";
my $pseudoCount = 1e-7;
my $exprFile = "";
GetOptions ("v|verbose"=>\$verbose,
'filter:s'=>\$filterIDFile,
'pseudo-count:f'=>\$pseudoCount,
'expr:s'=>\$exprFile,
);
if (@ARGV != 3)
{
print "CLIP cluster difference analysis\n";
print "Usage: $prog <clip1.bed> <clip2.bed> <out.txt>\n";
print " -filter [string] : list of clusters to estiamte expected ratio\n";
print " -pseudo-count [float] : pseudo count of tag number ($pseudoCount)\n";
print " -expr [string] : expression file (cluster id<\\t>log2fc\n";
print " -v : verbose\n";
exit (1);
}
my ($clusterBedFile1, $clusterBedFile2, $outFile) = @ARGV;
print "read clusters from $clusterBedFile1 ...\n" if $verbose;
my $clustersCondition1 = readBedFile ($clusterBedFile1, $verbose);
print "read clusters from $clusterBedFile2 ...\n" if $verbose;
my $clustersCondition2 = readBedFile ($clusterBedFile2, $verbose);
my $n = @$clustersCondition1;
Carp::croak "the two input BED files have different number of rows\n" unless $n == @$clustersCondition2;
print "$n clusters loaded\n" if $verbose;
my %filterIDHash;
if ($filterIDFile ne '')
{
Carp::croak "$filterIDFile does not exist\n" unless -f $filterIDFile;
print "reading cluster ids to estimate expected ratio...\n" if $verbose;
my $fin;
open ($fin, "<$filterIDFile") || Carp::croak "cannot open file $filterIDFile ...\n";
while (my $line = <$fin>)
{
chomp $line;
next if $line=~/^\s*$/;
my @cols = split(/\s/, $line);
$filterIDHash{$cols[0]} = 1;
}
close ($fin);
my $n = keys %filterIDHash;
print "$n clusters loaded\n" if $verbose;
}
my $totalTagNumCondition1 = 0;
my $totalTagNumCondition2 = 0;
for (my $i = 0; $i < $n; $i++)
{
my $name = $clustersCondition1->[$i]->{'name'};
Carp::croak "cluster $i in the two condition does not have the same name:\n" , Dumper ($clustersCondition1->[$i]), Dumper ($clustersCondition2->[$i]), "\n"
unless $clustersCondition2->[$i]->{'name'} eq $name;
if ($filterIDFile ne '')
{
next unless exists $filterIDHash{$name};
}
$totalTagNumCondition1 += $clustersCondition1->[$i]->{'score'};
$totalTagNumCondition2 += $clustersCondition2->[$i]->{'score'};
}
my $expectedRatio = $totalTagNumCondition1 / $totalTagNumCondition2;
print "total tag number N1=$totalTagNumCondition1, N2=$totalTagNumCondition2, N1/N2=$expectedRatio\n" if $verbose;
my @exprLog2FC;
if ($exprFile ne '' && (-f $exprFile))
{
print "reading gene expression fold change ...\n" if $verbose;
my $fin;
open ($fin, "<$exprFile") || Carp::croak "cannot open file $exprFile to read\n";
my $iter = 0;
while (my $line = <$fin>)
{
chomp $line;
next if $line=~/^\s*$/;
print "$iter ...\n" if $verbose && $iter % 100000 == 0;
my ($clusterId, $log2FC) = split (/\s/, $line);
Carp::croak "inconsistency detected in cluster ids of input files\n" unless $clusterId eq $clustersCondition1->[$iter]->{'name'} && $clusterId eq $clustersCondition2->[$iter]->{'name'};
push @exprLog2FC, $log2FC;
$iter++;
}
close ($fin);
}
if (@exprLog2FC > 0)
{
Carp::croak "expression file and cluster bed file must have the same number of lines in the same order\n" unless @exprLog2FC == $n;
}
print "performing binomial test of difference\n" if $verbose;
#&R::initR("--silent");
#&R::library("RSPerl");
my $fout;
open ($fout, ">$outFile") || Carp::croak "cannot open file $outFile\n";
print $fout join ("\t", "chrom", "chromStart", "chromEnd", "name", "score", "strand", "n1", "n2", "log2FC", "p"), "\n";
for (my $i = 0; $i < $n; $i++)
{
print "$i ...\n" if $verbose && $i % 100000 == 0;
my $n1 = $clustersCondition1->[$i]->{'score'} > 0 ? $clustersCondition1->[$i]->{'score'} : $clustersCondition1->[$i]->{'score'} + $pseudoCount; #1 is the pseudo count
my $n2 = $clustersCondition2->[$i]->{'score'} > 0 ? $clustersCondition2->[$i]->{'score'} : $clustersCondition2->[$i]->{'score'} + $pseudoCount;
my $r = $expectedRatio;
if (@exprLog2FC == $n)
{
my $log2FC = $exprLog2FC[$i];
my $fc = 2**$log2FC;
$r *= $fc;
}
my $p = $r / ($r + 1);
my $pvalue = binomTest (int($n1+0.5), int($n1+$n2+0.5), $p);
#my $ret = &R::call ("binom.test", int($n1+0.5)+0, int($n1+$n2+0.5)+0, $p + 0);
#my $pvalue = $ret->getEl ('p.value');
#$pvalue = 1- $pvalue if $pvalue > 0.5;
#$pvalue *= 2; #make it two tails
$pvalue = 1e-100 if $pvalue <= 0;
$pvalue = 1 if $pvalue > 1;
my $log2FC = log ($n1 / $n2 / $r) / log(2);
my %c = %{$clustersCondition1->[$i]};
$c{'score'} = sprintf ("%.2f", -log ($pvalue) / log(10) * 10);
print $fout join ("\t", bedToLine (\%c), $n1, $n2, $log2FC, $pvalue), "\n";
}
close ($fout);