forked from drchriscole/ensemblUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotate_promoters.pl
executable file
·201 lines (143 loc) · 5.25 KB
/
annotate_promoters.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
#!/usr/bin/perl
=head1 NAME
annotate_promoters.pl - annotate promoters of given genes with putative binding sites
=cut
use strict;
use warnings;
use Getopt::Long qw(:config auto_version);
use Pod::Usage;
use File::Basename;
use FindBin qw($Bin);
use lib "$Bin/lib";
use ensembl;
use Bio::EnsEMBL::ApiVersion;
my $file;
my $qMotif = '[AG]CGTG'; # HRE motif
my $promoterLength = 20;
my $genome = 'GRCh37';
my $out = 'motif_matches.bed';
my $VERBOSE = 1;
my $DEBUG = 0;
my $help;
my $man;
our $VERSION = '1.0';
GetOptions (
'in=s' => \$file,
'motif=s' => \$qMotif,
'length=i' => \$promoterLength,
'genome=s' => \$genome,
'out=s' => \$out,
'verbose!' => \$VERBOSE,
'debug!' => \$DEBUG,
'man' => \$man,
'help|?' => \$help,
) or pod2usage();
pod2usage(-verbose => 2) if ($man);
pod2usage(-verbose => 1) if ($help);
pod2usage(-msg => 'Please supply a valid filename.') unless ($file && -s $file);
# get gene list - HGNC symbols, one gene per line
my @genes = getGeneList($file);
die "ERROR - no genes found\n" unless (scalar @genes);
printf "Read %d genes in file '$file'\n", scalar @genes if $VERBOSE;
# load ensembl object
printf "NOTE: using Ensembl API version %s\n", software_version() if $VERBOSE;
my $ens = ensembl->new(genomeBuild => $genome, VERBOSE => $VERBOSE);
print "Species: ", $ens->species, "\n" if $VERBOSE;
# connect to ensembl and do some checks
my $registry = $ens->connect();
print "Genome version: ".$registry->get_adaptor('human', 'core', 'genomecontainer')->get_version()."\n" if $DEBUG;
my $sliceAdaptor = $registry->get_adaptor('human', 'core', 'slice');
my $geneAdaptor = $registry->get_adaptor('human', 'core', 'gene');
# for each gene get upstream sequence via ensembl
open(my $OUT, ">", $out) or die "ERROR - unable to open '$out' for write: ${!}\nDied";
print $OUT "track name=\"Motif_BS\"\n";
foreach my $g (@genes) {
my ($slice, $strand) = getPromoterSeq($geneAdaptor->fetch_by_display_label($g), $promoterLength);
# check strand and revComp if on reverse strand
# only look at promoter part of seq - ensembl returns whole gene + up/down stream region
my $seq = '';
if ($strand eq '-1' ) {
$seq = substr(reverseComplement($slice->seq()),0,$promoterLength);
} else {
$seq = substr($slice->seq(),0,$promoterLength);
}
# capture all motif sequence regions
while ($seq =~ /($qMotif)/g) {
my $motif = $1;
my $len = length($motif);
print "Match $1 found for gene '$g'\n" if $VERBOSE;
my $idx = index($seq,$motif); # find where the match is
# report matches is BED format: chr start end name score strand
if ($strand eq '-1') {
printf $OUT "%s\t%d\t%d\t$g:$motif\t100\t-\n", $slice->seq_region_name(), $slice->end()-$idx-$len, $slice->end()-$idx
} else {
printf $OUT "%s\t%d\t%d\t$g:$motif\t100\t+\n", $slice->seq_region_name(), $slice->start()+$idx, $slice->start()+$idx+$len
}
}
}
close($OUT);
## for a given ensembl gene object and +/- padding region,
## return slice object and strand
sub getPromoterSeq {
my $gene = shift;
my $length = shift;
die "ERROR - no ensembl gene found for '$gene'\n" unless (defined $gene);
printf "Found %s stableID for '$gene'\n", $gene->stable_id() if $DEBUG;
my $slice = $sliceAdaptor->fetch_by_gene_stable_id($gene->stable_id(), $length);
printf "Locus for '$gene' is %s:%d:%d\n", $slice->seq_region_name(), $slice->start(), $slice->end() if $DEBUG;
return($slice, $gene->strand());
}
## parse gene list
sub getGeneList {
my $file = shift;
my @data;
open(my $fh, "<", $file) or die "ERROR - unable to open '$file': ${!}\nDied";
while(<$fh>) {
chomp();
next unless(length($_));
push @data, $_;
}
close($fh);
return(@data);
}
## generate reverse complement sequence
sub reverseComplement {
my $seq = shift;
my $rev = reverse $seq;
$rev =~ tr/ACGT/TGCA/;
return($rev);
}
=head1 SYNOPSIS
annotate_promoters.pl --in <file> [--length <int>] [--genome <str>] [--out <file>] [--verbose|--no-verbose] [--version] [--debug|--no-debug] [--man] [--help]
=head1 DESCRIPTION
This script identifies putative motifs & binding sites in the upstream/promoter region for a given list of genes.
All identified sites are reported in BED format.
=head1 KNOWN ISSUES
When using a version of the ensembl perl API which is more than two versions older than the most recent, all output will be GRCh38 specific regardless of which assembly specified via I<--version>.
=head1 OPTIONS
=over 5
=item B<--in>
Input file with list of genes.
=item B<--length>
Length of upstream region to search.
=item B<--genome>
Specify the genome build to use (GRCh37|GRCh38). [default: GRCh37]
=item B<--out>
Output filename. [default: motif_matches.bed]
=item B<--version>
Report version information and exit
=item B<--verbose|--no-verbose>
Toggle verbosity. [default:none]
=item B<--debug|--no-debug>
Toggle debugging output. [default:none]
=item B<--help>
Brief help.
=item B<--man>
Full manpage of program.
=back
=head1 AUTHOR
Chris Cole <[email protected]>
=head1 COPYRIGHT
Copyright 2015, Chris Cole. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
=cut