-
Notifications
You must be signed in to change notification settings - Fork 16
/
extractPeak.pl
executable file
·307 lines (239 loc) · 7.24 KB
/
extractPeak.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env perl
use strict;
use warnings;
use File::Basename;
use Getopt::Long;
use Carp;
use Data::Dumper;
use Bed;
use Common;
my $verbose = 0;
my $prog = basename ($0);
my $sameStrand = 0;
#my $step = 1;
my $noMatchScore = -1e10;
my $outputFormat = "bed"; # bed, detail
my @ARGV0 = @ARGV;
GetOptions ("s"=>\$sameStrand,
#"step:i"=>\$step,
"nms|no-match-score:f"=>\$noMatchScore,
"of:s"=>\$outputFormat,
"v"=>\$verbose);
if (@ARGV != 3)
{
print "extract peak of wiggle values in specified region\n";
print "Usage: $prog [options] <region.bed> <in.wig> <out.bed>\n";
print " <in.wig> : use - to use stdin; gz file is accepted\n";
print " -s : same strand required\n";
print " --no-match-score [float] : the score assigned when no match is found ($noMatchScore)\n";
print " -of [string] : output format ([bed]|detail)\n";
print " -v : verbose\n";
exit (1);
}
print "CMD=$prog ", join(" ", @ARGV0), "\n" if $verbose;
my ($regionBedFile, $inWiggleFile, $outBedFile) = @ARGV;
print "load bed regions from $regionBedFile ...\n" if $verbose;
my $regions = readBedFile ($regionBedFile, $verbose);
my $n = @$regions;
print "$n regions loaded\n" if $verbose;
my %regionHash;
foreach my $r (@$regions)
{
my $chrom = $r->{"chrom"};
Carp::croak "no strand info in ", Dumper ($r), "\n" if $sameStrand && not exists $r->{"strand"};
my $strand = exists $r->{"strand"} && $sameStrand ? $r->{"strand"} : 'b';
$r->{"peakScore"} = $noMatchScore;
$r->{"peakStart"} = $r->{"chromStart"};
$r->{"peakEnd"} = $r->{"chromEnd"};
$r->{"halfPeakStart"} = $r->{"chromStart"};
$r->{"halfPeakEnd"} = $r->{"chromEnd"};
#for (my $i = 0; $i < $r->{"chromEnd"} - $r->{"chromStart"} + 1; $i++)
#{
# $r->{"score2"}->[$i] = 0;
#}
push @{$regionHash{$strand}->{$chrom}}, $r;
}
my $fin;
my $strand = 'b';
my $chrom = "";
my $currRegions = [];
my @currScores;
if ( $inWiggleFile eq "-")
{
$fin = *STDIN;
}
else
{
if ($inWiggleFile =~/\.gz$/)
{
open ($fin, "gunzip -c $inWiggleFile | ") || Carp::croak "cannot open file $inWiggleFile to read\n";
}
else
{
open ($fin, "<$inWiggleFile") || Carp::croak "cannot open file $inWiggleFile to read\n";
}
}
while (my $line = <$fin>)
{
chomp $line;
next if $line=~/^\s*$/;
my $firstTrack = 0;
if ($line=~/^track\s/ || $firstTrack)
{
$firstTrack = 0;
print "a new track found ($line)...\n" if $verbose;
if (@$currRegions > 0 && @currScores > 0)
{
my $nr = @$currRegions;
my $ns = @currScores;
print "extracting scores for chrom=$chrom, strand=$strand ($nr regions, $ns scores) ...\n" if $verbose;
extractPeak ($currRegions, \@currScores); # if @$currRegions > 0 && @currScores > 0;
}
$chrom = "";
$currRegions = [];
@currScores = ();
$strand = 'b';
if ($sameStrand)
{
if($line=~/\(([\+|\-])\)/)
{
$strand = $1;
}
else
{
Carp::croak "no strand information found in the track\n";
}
}
next if $line =~/^track\s/;
}
my ($chr, $chromStart, $chromEnd, $score) = split (/\t/, $line);
Carp::croak "the score in the wiggle can not be less than $noMatchScore\n" if $score <= $noMatchScore;
#make sure the scores are reasonable
$chromEnd -= 1;
if ($chr ne $chrom)
{
if (@$currRegions > 0 && @currScores > 0)
{
my $nr = @$currRegions;
my $ns = @currScores;
print "extracting scores for chrom=$chrom, strand=$strand ($nr regions, $ns scores) ...\n" if $verbose;
extractPeak ($currRegions, \@currScores); # if @$currRegions > 0 && @currScores > 0;
}
print "processing $chr ...\n" if $verbose;
$chrom = $chr;
$currRegions = [];
$currRegions = $regionHash{$strand}->{$chrom} if exists $regionHash{$strand} && exists $regionHash{$strand}->{$chrom};
@currScores = ();
}
push @currScores, {chrom=>$chr, chromStart=>$chromStart, chromEnd=>$chromEnd, score=>$score};
}
if (@$currRegions > 0 && @currScores > 0)
{
my $nr = @$currRegions;
my $ns = @currScores;
print "extracting scores for chrom=$chrom, strand=$strand ($nr regions, $ns scores) ...\n" if $verbose;
extractPeak ($currRegions, \@currScores); # if @$currRegions > 0 && @currScores > 0;
}
close ($fin) if $inWiggleFile ne '-';
print "dumping results to $outBedFile ...\n" if $verbose;
if ($outputFormat eq 'bed')
{
foreach my $r (@$regions)
{
#$r->{"chromStart"} = $r->{"peakStart"};
#$r->{"chromEnd"} = $r->{"peakEnd"};
$r->{"score"} = $r->{"peakScore"};
}
writeBedFile ($regions, $outBedFile);
}
elsif ($outputFormat eq 'detail')
{
my $fout;
open ($fout, ">$outBedFile") || Carp::croak "cannot open file $outBedFile to write\n";
foreach my $r (@$regions)
{
print $fout join ("\t", bedToLine ($r),
$r->{"peakScore"}, $r->{"peakStart"}, $r->{"peakEnd"} + 1, $r->{"halfPeakStart"}, $r->{"halfPeakEnd"} + 1), "\n";
}
close ($fout);
}
else
{
Carp::croak "wrong output format: $outputFormat\n";
}
sub extractPeak
{
my ($regions, $scores) = @_;
my @regionsSorted = sort {$a->{"chromStart"} <=> $b->{"chromStart"}} @$regions;
my $firstScoreIdx = 0;
print "determine peak height ...\n" if $verbose;
my $k = 0;
foreach my $r (@regionsSorted)
{
print "$k ...\n" if $verbose && $k % 5000 == 0;
$k++;
my $chromStart = $r->{"chromStart"};
my $chromEnd = $r->{"chromEnd"};
while ($firstScoreIdx < @$scores && $scores->[$firstScoreIdx]->{"chromEnd"} < $chromStart)
{
$firstScoreIdx++;
}
#print "firstIdx = $firstScoreIdx\n" if $verbose;
my $i = $firstScoreIdx;
while ($i < @$scores && $scores->[$i]->{"chromStart"} <= $chromEnd)
{
my $overlapStart = max ($chromStart, $scores->[$i]->{"chromStart"});
my $overlapEnd = min ($chromEnd, $scores->[$i]->{"chromEnd"});
if ($overlapEnd >= $overlapStart)
{
if ($r->{"peakScore"} < $scores->[$i]->{"score"})
{
#use the new region
$r->{"peakScore"} = $scores->[$i]->{"score"};
$r->{"peakStart"} = $scores->[$i]->{"chromStart"};
$r->{"peakEnd"} = $scores->[$i]->{"chromEnd"};
}
elsif ($r->{"peakScore"} == $scores->[$i]->{"score"})
{
#expand the region
$r->{"peakStart"} = min ($r->{"peakStart"}, $scores->[$i]->{"chromStart"});
$r->{"peakEnd"} = max ($r->{"peakEnd"}, $scores->[$i]->{"chromEnd"});
}
}
$i++;
}
}
print "determine half peak width ...\n" if $verbose;
$firstScoreIdx = 0;
$k = 0;
foreach my $r (@regionsSorted)
{
print "$k ...\n" if $verbose && $k % 5000 == 0;
$k++;
my $chromStart = $r->{"chromStart"};
my $chromEnd = $r->{"chromEnd"};
next unless $r->{"peakScore"} > $noMatchScore;
my $halfPeakStartMatch = 0;
while ($firstScoreIdx < @$scores && $scores->[$firstScoreIdx]->{"chromEnd"} < $chromStart)
{
$firstScoreIdx++;
}
#print "firstIdx = $firstScoreIdx\n" if $verbose;
my $i = $firstScoreIdx;
while ($i < @$scores && $scores->[$i]->{"chromStart"} <= $chromEnd)
{
my $overlapStart = max ($chromStart, $scores->[$i]->{"chromStart"});
my $overlapEnd = min ($chromEnd, $scores->[$i]->{"chromEnd"});
if ($overlapEnd >= $overlapStart && $scores->[$i]->{"score"} > $r->{"peakScore"} / 2)
{
if ($halfPeakStartMatch == 0)
{
$r->{"halfPeakStart"} = $scores->[$i]->{"chromStart"};
$halfPeakStartMatch = 1;
}
$r->{"halfPeakEnd"} = $scores->[$i]->{"chromEnd"};
}
$i++;
}
}
}