-
Notifications
You must be signed in to change notification settings - Fork 0
/
defineRegions.pl
205 lines (186 loc) · 4.55 KB
/
defineRegions.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#defines regions by sliding a sliding window of size $windowSize (number of CpGs). If the average number of significant CpGs is > $minSigCount, the window is added to the region.
use strict;
my $file = shift;
my $windowSize = shift || 5;#number of things to remember
my $minSigCount = shift || 3;
my $diffCutoff = shift || 0.5;
my $pvalCutoff = shift || 0.01;
my $resetDistance = 1000; #reset if cpgs are this far apart
if ($file =~ /\.gz$/)
{
open(IN, "gunzip -c $file |") or die "can't open pipe to file $file";
}
else
{
open IN, $file or die "Could not open file $file\n";
}
my $head = <IN>;
my @windowStarts = (0) x $windowSize;
my @windowVals = (0) x $windowSize;
my $root = "$file.w$windowSize.s$minSigCount.d$diffCutoff.p$pvalCutoff";
open OUT, ">$root.reg.unsorted";
open UNMERGED, ">$root.unmerged";
my $totalCpGs = 0;
my @regs = ();
my $thisLine = 0;
my $thisIndex = 0;
my $goodRegStart = 0;
my $inGoodReg = 0;
my $printedPeaks = 0;
my $lastChr = 0;
my $lastStart = 0;
while (my $line = <IN>)
{
$totalCpGs++;
chomp $line;
my @lineEls = split "\t", $line;
my $chr = $lineEls[0];
next if $chr eq "chrX";
next if $chr eq "chrY";
my $start = $lineEls[1];
my $pval = $lineEls[4];
my $diff = $lineEls[5];
#if we're on a new chromosome or if we are too far away from previous start
if ($start < $lastStart || $start - $lastStart > $resetDistance)
{
printValidRegs();
}
$windowStarts[$thisIndex] = $start;
my $val = 0;
if ($pval <= $pvalCutoff)
{
if ($diffCutoff > 0 && $diff >= $diffCutoff)
{
$val = 1;
}
elsif ($diffCutoff < 0 && $diff <= $diffCutoff)
{
$val = 1;
}
}
$windowVals[$thisIndex] = $val;
my $countGood = sum(@windowVals);
# print "stp: $chr\t$start\tp:$pval\td:$diff\t@windowVals\tc:$countGood\n";
if (!$inGoodReg && $countGood >= $minSigCount)
{
$goodRegStart = 0;
for (my $i = $thisIndex+1; $i < $thisIndex + $windowSize; $i++)
{
# print "tI: $thisIndex Checking $i & $windowSize ($windowVals[$i % $windowSize]) == 1)\n";
if ($windowVals[$i % $windowSize] == 1)
{
$goodRegStart = $windowStarts[$i % $windowSize];
# print "set good start to $goodRegStart\n";
last;
}
}
#if the whole window has not been covered, the last good start will be 0
if ($goodRegStart > 0)
{
$inGoodReg = 1;
}
# print "lws: @windowStarts\nlwc: @windowVals\nlastGoodStart: $goodRegStart\n";
}
elsif ($inGoodReg && $countGood < $minSigCount)
{
my $goodRegEnd = 0;
for (my $i = ($thisIndex-1) + $windowSize; $i > $thisIndex; $i--)
{
if ($windowVals[$i % $windowSize] == 1)
{
$goodRegEnd = $windowStarts[$i % $windowSize];
last;
}
}
print UNMERGED "$chr\t$goodRegStart\t$goodRegEnd\n";
push @regs, "$chr\t$goodRegStart\t$goodRegEnd";
$inGoodReg = 0;
}
$lastChr = $chr;
$lastStart = $start;
$thisIndex++;
$thisIndex = 0 if $thisIndex == $windowSize;
$thisLine++;
}
#print from last chr
printValidRegs();
print `sort -k 1,1 -k2,2n $root.reg.unsorted > $root.reg`;
print `rm $root.reg.unsorted`;
print "read $totalCpGs total CpGs\n";
print "printed $printedPeaks peaks\n";
sub mean
{
my $sum = 0;
for (my $i = 0; $i < $windowSize; $i++)
{
$sum += $_[$i];
}
return 0 if $sum == 0;
return $sum/@_;
}
sub median
{
my @vals = sort {$a <=> $b} @_;
my $len = @vals;
if($len%2) #odd?
{
return $vals[int($len/2)];
}
else #even
{
return ($vals[int($len/2)-1] + $vals[int($len/2)])/2;
}
}
sub sum
{
my $sum = 0;
for (my $i = 0; $i < $windowSize; $i++)
{
$sum += $_[$i];
}
return $sum;
}
sub printValidRegs
{
# print "printing regs\n===".join("\n",@regs)."\n===\n";
#add last region if above cutoff
my $countGood = sum(@windowVals);
if ($inGoodReg && $countGood >= $minSigCount)
{
print UNMERGED "$lastChr\t$goodRegStart\t$lastStart\n";
push @regs, "$lastChr\t$goodRegStart\t$lastStart";
}
#print overlapped regions
while (@regs)
{
#for each region, either print it or merge it with the next one
my $currReg = shift @regs;
#if there is a next region
if (@regs)
{
my $nextReg = @regs[0];
my ($currRegChr,$currRegStart,$currRegEnd) = split "\t", $currReg;
my ($nextRegChr,$nextRegStart,$nextRegEnd) = split "\t", $nextReg;
if ($currRegEnd >= $nextRegStart)
{
my $newNext = "$nextRegChr\t$currRegStart\t$nextRegEnd";
$regs[0] = $newNext;
# print "cre: $currRegEnd < nre $nextRegStart\n";
}
else
{
print OUT "$currReg\n";
$printedPeaks++;
}
}
else
{
print OUT "$currReg\n";
$printedPeaks++;
}
}
@regs = ();
$inGoodReg = 0;
@windowStarts = (0) x $windowSize;
@windowVals = (0) x $windowSize;
}