-
Notifications
You must be signed in to change notification settings - Fork 4
/
peaks2genes
executable file
·380 lines (306 loc) · 7.77 KB
/
peaks2genes
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/perl -w
# Copyright © 2015, Owen Marshall
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
use strict;
use File::Basename;
use 5.010;
$|++;
my $version = "1.0.1";
print STDERR "\npeaks2genes v$version\nCopyright © 2015, Owen Marshall\n\n";
my %vars = (
'pad' => 0,
'genes_file' => '',
'gene_pad' => 1000,
'mouse' => 0,
'promoters_only'=>0,
'exclude_lincs' => 0,
);
my %vars_details = (
'pad' => 'Add padding around peaks (bp)',
'genes_file' => 'GFF file of annotated genes to use',
'gene_pad' => 'Consider a peak associated with a gene if it lies within this distance of the gene start or end',
'mouse' => 'Deals with Ensembl formatted GFFs for the mouse genome',
'promoters_only' => 'Only check promoters for overlap',
'exclude_lincs' => 'Ignore lncRNAs in output',
);
my $HOME = (getpwuid($<))[7];
my @in_files;
process_cli();
unless (@in_files) {
help();
}
my @genes;
my @genes_f;
my @genes_r;
my @peaks;
unless ($vars{'genes_file'}) {
die("Error: no genes file specified. Please specify a valid genes file with the --genes_file option ...\n\n")
}
unless (-e $vars{'genes_file'}) {
die("Error: Genes file does not exist. Please specify a valid genes file with the --genes_file option ...\n\n")
}
### pipeline
foreach my $fn (@in_files) {
my ($name,$dir,$ext) = fileparse($fn, qr/\.[^.]*/);
load_file($fn, \@peaks);
read_genes($vars{'genes_file'});
report_genes($name,$dir);
say '';
}
say "All done.\n";
exit 0;
sub load_file {
# Load peaks file (gff)
my ($fn, $aref) = @_;
say "Loading $fn ...";
open (IN, "<$fn") || die "Unable to read input file $fn: $!\n";
while (<IN>) {
chomp;
next if m/^#/;
my @tmp = split("\t");
if ($#tmp > 1) {
push @$aref, [@tmp[0,3,4,5]];
} else {
# allow a list of chr:start-end ...
my ($chr, $start, $end) = $tmp[0] =~ m/(.*?)\:(\d+)-(\d+)/;
push @$aref, [$chr,$start,$end,1];
}
}
close (IN);
map { @{$_}[0] =~ s/chr//g } @$aref;
}
sub read_genes {
my $fn = shift;
if ($fn =~ m/\.gz$/) {
# gzipped file
open (GENES, '-|', "gunzip -c $fn") || die "Error: cannot open genes file $fn: $!\n\n";
} else {
open(GENES, "<$fn")|| die "Error: cannot open genes file $fn: $!\n\n";
}
while (<GENES>) {
my ($chr, $a, $b, $start, $end, $c, $pol, $d, $info) = split(/\t/);
next unless $info;
my $name;
if ($vars{'mouse'}) {
($name) = $info =~ m/gene_name \"(.*?)\"/;
} else {
($name) = $info =~ m/Name=(.*?);/;
}
if ($vars{'exclude_lincs'}) {
next if $name =~ m/^CR\d+/;
}
$chr =~ s/chr//g;
push @genes, [$chr, $start, $end, $pol, $name];
if ($pol eq '+') {
push @genes_f, [$chr, $start, $end, $pol, $name];
} else {
push @genes_r, [$chr, $start, $end, $pol, $name];
}
}
close GENES;
}
sub report_genes {
my ($name,$dir) = @_;
$dir ||= '.';
say " Searching for peaks overlapping genes ...";
my ($p1inc, @genes_overlap) = find_genes(\@peaks);
say " Writing genes list ...";
write_gene_lists(\@genes_overlap, "$dir/$name.genes.in.peak.csv");
}
sub write_gene_lists {
my ($ar, $name) = @_;
open (GENEOUT, '>', $name) || die "Cannot open gene lists for writing: $!\n";
say GENEOUT join("\t",@$_) foreach @$ar;
close GENEOUT;
}
sub find_genes {
my $ar = shift;
my @matched;
my $overlap=0;
foreach (@$ar) {
my ($c,$s,$e,$sc) = @{$_};
if ($vars{'promoters_only'}) {
my @mgf = grep {
@{$_}[0] eq $c
&& $e >= @{$_}[1] - $vars{'gene_pad'}
&& $s <= @{$_}[1] + $vars{'gene_pad'}
} @genes_f;
if (@mgf) {
$overlap++;
push @matched, [@{$_}[4], $sc] foreach @mgf;
}
my @mgr = grep {
@{$_}[0] eq $c
&& $e >= @{$_}[2] - $vars{'gene_pad'}
&& $s <= @{$_}[2] + $vars{'gene_pad'}
} @genes_r;
if (@mgr) {
$overlap++;
push @matched, [@{$_}[4], $sc] foreach @mgr;
}
} else {
my @mg = grep {
@{$_}[0] eq $c
&& $e >= @{$_}[1] - $vars{'gene_pad'}
&& $s <= @{$_}[2] + $vars{'gene_pad'}
} @genes;
if (@mg) {
$overlap++;
push @matched, [@{$_}[4], $sc] foreach @mg;
}
}
}
return ($overlap, sort {@$b[1] <=> @$a[1]} uniq(@matched));
}
sub printout {
my ($hr, $fh, $ar) = @_;
foreach (keys %$hr) {
my ($c,$s,$e,$sc) = split(/-/);
push @$ar, [$c,$s,$e,$sc] if $ar;
print $fh join("\t",$c,'.','.',$s,$e,$sc,'.','.','.'),"\n";
}
}
sub excl {
my ($phr, $ar, $fh, $aer) = @_;
foreach (@$ar) {
my ($c,$a,$b,$s,$e,$sc) = @{$_};
next if $phr -> {"$c-$s-$e-$sc"};
push @$aer, [$c,$s,$e,$sc] if $aer;
print $fh join("\t",$c,'.','.',$s,$e,$sc,'.','.','.'),"\n";
}
}
sub peak_score {
my ($href, $aref) = @_;
my ($peaksum, @peakscore);
foreach (keys %$href) {
my ($chr, $start, $end, $score) = split("-");
$peaksum += $end-$start;
push @peakscore, $score;
}
my $avg_peak_score = mean(@peakscore);
return ($peaksum, $avg_peak_score);
}
sub max {
my ($max, @vars) = @_;
my $index=0;
$max||=0;
for my $i (0..$#vars) {
($max, $index) = ($vars[$i], $i+1) if $vars[$i] > $max;
}
return ($index, $max);
}
sub min {
my ($min, @vars) = @_;
my $index=0;
$min||=0;
for my $i (0..$#vars) {
($min, $index) = ($vars[$i],$i+1) if $vars[$i] < $min;
}
return ($index, $min);
}
sub uniq {
return unless @_;
my %seen;
return grep { !$seen{$_}++ } @_;
}
sub mean {
my @a = @_;
my $n = @a;
return $a[0] if $n<2;
my $total;
map {$total += $_} @a;
my $mean = $total/$n;
return $mean;
}
sub median {
my @a = sort {$a <=> $b} @_;
my $index = int($#a/2);
return $a[$index];
}
sub stdev {
my @a = @_;
my $n = @a;
return (0, 0) if $n<2;
my $mean = mean(@a);
my $sum = 0;
map {$sum += abs($_-$mean)} @a;
my $sd= $sum/($n-1);
my $se = $sd/sqrt($n);
return $sd;
}
sub process_cli {
foreach (@ARGV) {
if (/--(.*)=(.*)/) {
unless (defined($vars{$1})) {
print STDERR "Did not understand $_ ...\n";
help();
}
my ($v, $opt) = ($1,$2);
$opt =~ s/~/$HOME/;
$vars{$v} = $opt;
next;
} elsif (/--h[elp]*/) {
help();
} elsif (/--(.*)/) {
if (defined($vars{$1})) {
$vars{$1} = 1;
} else {
print STDERR "Did not understand $_ ...\n";
help();
}
next;
}
push @in_files, $_;
}
}
sub help {
print STDOUT <<EOT;
peaks2genes.pl -- calls genes in close proximity to peaks
Usage: peaks2genes [options] [peak files in GFF format]
Options:
EOT
my $opt_len = 0;
foreach (keys %vars) {
my $l = length($_);
$opt_len = $l if $l > $opt_len;
}
$opt_len+=2;
my $cols= `tput cols` || 80;
my ($v, $val, $def, $def_format);
my $help_format = "format STDOUT =\n"
.' '.'^'.'<'x$opt_len . ' '. '^' . '<'x($cols-$opt_len-4) . "\n"
.'$v, $def_format'."\n"
.' '.'^'.'<'x$opt_len . ' '. '^' . '<'x($cols-$opt_len-6) . "~~\n"
.'$v, $def_format'."\n"
.".\n";
eval $help_format;
die $@ if $@;
foreach my $k (sort (keys %vars)) {
($v, $val, $def) = ($k, $vars{$k}, $vars_details{$k});
$def||="";
$def_format = $val ? "$def\n\r[Current value: $val]" : $def;
$v = "--$v";
# format =
# ^<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#$v, $def_format
# ^<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~
#$v, $def_format
#.
write();
}
print STDOUT "\n";
exit 1;
}