-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall_promoters_of.pl
423 lines (393 loc) · 14.3 KB
/
all_promoters_of.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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/perl
################################
#Compute all promoter positions#
#and promoter sequences #
#of a contig. #
################################
use List::Util ("max");
#assumed start and end of promoters related to transcription-start
my $from = 1000;
my $to = 50;
#contig of interest
my $chromosome = 7;
my $contig = "supercont2.7";
#parameters of annotation file
my $tabsBeforeLocus = 0;
my $tabsBeforeStart = 4;
my $tabsBeforeStop = 5;
my $tabsBeforeStrand = 6;
#information to extract from annotation file
my @loci;
my @starts;
my @stops;
my @strands;
#information to extract from genome file
my $sequence;
my $nucleotides;
#open genome file
open( my $genome, "<", "aspergillus_flavus_2_supercontigs.fasta" ) || die "genome input error: " . $!;
my $found = 0;
#we are not at the end of the file and
#not at a line after the selected contig
while ( !eof($genome) && !$found ) {
my $line = <$genome>;
#found the header of the selected contig
if ( index( $line, ">" ) != -1 && index( $line, $contig ) != -1 ) {
$found = 1;
$line = <$genome>;
#read the genome line by line
#until we get to the next contig > next header
while ( !eof && index( $line, ">" ) == -1 ) {
$sequence .= $line;
chomp($sequence);
$line = <$genome>;
}
}
}
close $genome;
$sequence = lc($sequence);
$nucleotides = length($sequence);
print "Contig " . $contig . " has " . $nucleotides . " nucleotides";
#open annotation file
open( my $annotation, "<", "aspergillus_flavus_2_genome_summary_per_gene.txt" )
|| die "annotation input error: " . $!;
#analyse each line of the annotation file
<$annotation>; #skip first line (header)
while (<$annotation>) {
my $line = $_;
#check if the current line belongs to the selected contig
if ( index( $line, "\t" . $chromosome . "\t" ) != -1 ) {
my $locus;
my $startPos;
my $stopPos;
my $strandPos;
#extract locus, start, stop and strand
my $noOfTab = 1;
my $tabPos = 0;
do {
$tabPos = index( $line, "\t", $tabPos + 1 );
if ( $noOfTab == $tabsBeforeLocus + 1 ) {
$locus = substr( $line, 0, $tabPos );
}
elsif ( $noOfTab == $tabsBeforeStart ) {
$startPos = $tabPos + 1;
}
elsif ( $noOfTab == $tabsBeforeStop ) {
$stopPos = $tabPos + 1;
}
elsif ( $noOfTab == $tabsBeforeStrand ) {
$strandPos = $tabPos + 1;
}
++$noOfTab;
} while ( $tabPos != -1 );
my $start = substr( $line, $startPos, $stopPos - $startPos - 1 );
my $stop = substr( $line, $stopPos, $strandPos - $stopPos - 1 );
my $strand = substr( $line, $strandPos, 1 );
push( @loci, $locus );
push( @starts, $start );
push( @stops, $stop );
push( @strands, $strand );
}
}
close $annotation;
print " with " . @loci . " loci (genes).\n";
#compute borders of promoters
my @positions;
my $skipNextLocus = 0;
for ( 0 .. $#loci ) {
#two genes share the same promoter > special case 9
#did computation with first gene > skip second gene
if ($skipNextLocus) {
$skipNextLocus = 0;
}
#first gene of the contig
#AND NOT special case 9
#[ $_ -1 ] not present
elsif (
$_ == 0
&& !( ( ( $strands[$_] eq '-' ) && ( $strands[ $_ + 1 ] eq '+' ) )
&& ( $stops[$_] + $from >= $starts[ $_ + 1 ] - $from ) )
)
{
if ( $strands[$_] eq '+' ) {
if ( ( ( $starts[$_] - $from ) >= 0 ) && ( $stops[$_] > $starts[$_] + $to ) ) { #1
push( @positions, $starts[$_] - $from );
push( @positions, $starts[$_] + $to );
push( @names, "around_ts_of_" . $loci[$_] );
}
elsif ( ( ( $starts[$_] - $from ) < 0 ) && ( $stops[$_] > $starts[$_] + $to ) ) { #2
push( @positions, 1 );
push( @positions, $starts[$_] + $to );
push( @names, "around_ts_of_" . $loci[$_] );
}
elsif ( ( ( $starts[$_] - $from ) >= 0 ) && ( $starts[$_] + $to >= $stops[$_] ) ) { #3
push( @positions, $starts[$_] - $from );
push( @positions, $stops[$_] );
push( @names, "around_ts_of_" . $loci[$_] );
}
elsif ( ( ( $starts[$_] - $from ) < 0 ) && ( $starts[$_] + $to >= $stops[$_] ) ) { #7
push( @positions, 1 );
push( @positions, $stops[$_] );
push( @names, "around_ts_of_" . $loci[$_] );
}
else {
print "problem with promoter at gene " . $loci[$_] . "!\n";
}
}
elsif ( $strands[$_] eq '-' ) {
if ( ( $starts[$_] < $stops[$_] - $to ) && ( $starts[ $_ + 1 ] > $stops[$_] + $from ) ) { #4
push( @positions, $stops[$_] - $to );
push( @positions, $stops[$_] + $from );
push( @names, "around_ts_of_" . $loci[$_] );
}
elsif ( ( $starts[$_] < $stops[$_] - $to ) && ( $starts[ $_ + 1 ] <= $stops[$_] + $from ) ) { #5
push( @positions, $stops[$_] - $to );
push( @positions, $starts[ $_ + 1 ] - 1 );
push( @names, "around_ts_of_" . $loci[$_] );
}
elsif ( ( $starts[$_] >= $stops[$_] - $to ) && ( $starts[ $_ + 1 ] > $stops[$_] + $from ) ) { #6
push( @positions, $starts[$_] );
push( @positions, $stops[$_] + $from );
push( @names, "around_ts_of_" . $loci[$_] );
}
elsif ( ( $starts[ $_ + 1 ] <= $stops[$_] + $from ) && ( $starts[$_] >= $stops[$_] - $to ) ) { #8
push( @positions, $starts[$_] );
push( @positions, $starts[ $_ + 1 ] - 1 );
push( @names, "around_ts_of_" . $loci[$_] );
}
else {
print "problem with promoter at gene " . $loci[$_] . "!\n";
}
}
else {
print "strand-problem at gene " . $loci[$_] . "!\n";
}
}
#last gene of contig
#[ $_ +1 ] not present
elsif ( $_ == $#loci && !$skipNextLocus ) {
if ( $strands[$_] eq '+' ) {
if ( ( $stops[ $_ - 1 ] < $starts[$_] - $from ) && ( $stops[$_] > $starts[$_] + $to ) ) { #1
push( @positions, $starts[$_] - $from );
push( @positions, $starts[$_] + $to );
push( @names, "around_ts_of_" . $loci[$_] );
}
elsif ( ( $stops[ $_ - 1 ] >= $starts[$_] - $from ) && ( $stops[$_] > $starts[$_] + $to ) ) { #2
push( @positions, $stops[ $_ - 1 ] + 1 );
push( @positions, $starts[$_] + $to );
push( @names, "around_ts_of_" . $loci[$_] );
}
elsif ( ( $stops[ $_ - 1 ] < $starts[$_] - $from ) && ( $starts[$_] + $to >= $stops[$_] ) ) { #3
push( @positions, $starts[$_] - $from );
push( @positions, $stops[$_] );
push( @names, "around_ts_of_" . $loci[$_] );
}
elsif ( ( $stops[ $_ - 1 ] >= $starts[$_] - $from ) && ( $starts[$_] + $to >= $stops[$_] ) ) { #7
push( @positions, $stops[ $_ - 1 ] + 1 );
push( @positions, $stops[$_] );
push( @names, "around_ts_of_" . $loci[$_] );
}
else {
print "problem with promoter at gene " . $loci[$_] . "!\n";
}
}
elsif ( $strands[$_] eq '-' ) {
if ( ( $starts[$_] < $stops[$_] - $to ) && ( ( $stops[$_] + $from ) <= $nucleotides ) ) { #4
push( @positions, $stops[$_] - $to );
push( @positions, $stops[$_] + $from );
push( @names, "around_ts_of_" . $loci[$_] );
}
elsif ( ( $starts[$_] < $stops[$_] - $to ) && ( ( $stops[$_] + $from ) > $nucleotides ) ) { #5
push( @positions, $stops[$_] - $to );
push( @positions, $nucleotides );
push( @names, "around_ts_of_" . $loci[$_] );
}
elsif ( ( $starts[$_] >= $stops[$_] - $to ) && ( ( $stops[$_] + $from ) <= $nucleotides ) ) { #6
push( @positions, $starts[$_] );
push( @positions, $stops[$_] + $from );
push( @names, "around_ts_of_" . $loci[$_] );
}
elsif ( ( $starts[ $_ + 1 ] <= $stops[$_] + $from ) && ( ( $stops[$_] + $from ) > $nucleotides ) )
{ #8
push( @positions, $starts[$_] );
push( @positions, $nucleotides );
push( @names, "around_ts_of_" . $loci[$_] );
}
else {
print "problem with promoter at gene " . $loci[$_] . "!\n";
}
}
else {
print "strand-problem at gene " . $loci[$_] . "!\n";
}
}
#special-case 9
elsif ( ( ( $strands[$_] eq '-' ) && ( $strands[ $_ + 1 ] eq '+' ) )
&& ( $stops[$_] + $from >= $starts[ $_ + 1 ] - $from ) )
{
if ( ( $stops[$_] > $starts[$_] + $to ) && ( $starts[$_] < $stops[$_] - $to ) ) { #9.1+4
push( @positions, $stops[$_] - $to );
push( @positions, $starts[ $_ + 1 ] + $to );
push( @names, "around_ts_of_" . $loci[$_] . "_and_" . $loci[ $_ + 1 ] );
}
elsif ( ( $starts[$_] < $stops[$_] - $to ) && ( $starts[ $_ + 1 ] + $to >= $stops[ $_ + 1 ] ) )
{ #9.3+4
push( @positions, $stops[$_] - $to );
push( @positions, $stops[ $_ + 1 ] );
push( @names, "around_ts_of_" . $loci[$_] . "_and_" . $loci[ $_ + 1 ] );
}
elsif ( ( $starts[$_] >= $stops[$_] - $to ) && ( $stops[ $_ + 1 ] > $starts[ $_ + 1 ] + $to ) )
{ #9.1+6
push( @positions, $starts[$_] );
push( @positions, $starts[ $_ + 1 ] + $to );
push( @names, "around_ts_of_" . $loci[$_] . "_and_" . $loci[ $_ + 1 ] );
}
elsif ( ( $starts[$_] >= $stops[$_] - $to ) && ( $starts[ $_ + 1 ] + $to >= $stops[ $_ + 1 ] ) )
{ #9.3+6
push( @positions, $starts[$_] );
push( @positions, $stops[ $_ + 1 ] );
push( @names, "around_ts_of_" . $loci[$_] . "_and_" . $loci[ $_ + 1 ] );
}
else {
print "problem with promoter at gene " . $loci[$_] . "!\n";
}
$skipNextLocus = 1;
}
#normal-cases
elsif ( !$skipNextLocus ) {
if ( $strands[$_] eq '+' ) {
if ( ( $stops[ $_ - 1 ] < $starts[$_] - $from ) && ( $stops[$_] > $starts[$_] + $to ) ) { #1
push( @positions, $starts[$_] - $from );
push( @positions, $starts[$_] + $to );
push( @names, "around_ts_of_" . $loci[$_] );
}
elsif ( ( $stops[ $_ - 1 ] >= $starts[$_] - $from ) && ( $stops[$_] > $starts[$_] + $to ) ) { #2
push( @positions, $stops[ $_ - 1 ] + 1 );
push( @positions, $starts[$_] + $to );
push( @names, "around_ts_of_" . $loci[$_] );
}
elsif ( ( $stops[ $_ - 1 ] < $starts[$_] - $from ) && ( $starts[$_] + $to >= $stops[$_] ) ) { #3
push( @positions, $starts[$_] - $from );
push( @positions, $stops[$_] );
push( @names, "around_ts_of_" . $loci[$_] );
}
elsif ( ( $stops[ $_ - 1 ] >= $starts[$_] - $from ) && ( $starts[$_] + $to >= $stops[$_] ) ) { #7
push( @positions, $stops[ $_ - 1 ] + 1 );
push( @positions, $stops[$_] );
push( @names, "around_ts_of_" . $loci[$_] );
}
else {
print "problem with promoter at gene " . $loci[$_] . "!\n";
}
}
elsif ( $strands[$_] eq '-' ) {
if ( ( $starts[$_] < $stops[$_] - $to ) && ( $starts[ $_ + 1 ] > $stops[$_] + $from ) ) { #4
push( @positions, $stops[$_] - $to );
push( @positions, $stops[$_] + $from );
push( @names, "around_ts_of_" . $loci[$_] );
}
elsif ( ( $starts[$_] < $stops[$_] - $to ) && ( $starts[ $_ + 1 ] <= $stops[$_] + $from ) ) { #5
push( @positions, $stops[$_] - $to );
push( @positions, $starts[ $_ + 1 ] - 1 );
push( @names, "around_ts_of_" . $loci[$_] );
}
elsif ( ( $starts[$_] >= $stops[$_] - $to ) && ( $starts[ $_ + 1 ] > $stops[$_] + $from ) ) { #6
push( @positions, $starts[$_] );
push( @positions, $stops[$_] + $from );
push( @names, "around_ts_of_" . $loci[$_] );
}
elsif ( ( $starts[ $_ + 1 ] <= $stops[$_] + $from ) && ( $starts[$_] >= $stops[$_] - $to ) ) { #8
push( @positions, $starts[$_] );
push( @positions, $starts[ $_ + 1 ] - 1 );
push( @names, "around_ts_of_" . $loci[$_] );
}
else {
print "problem with promoter at gene " . $loci[$_] . "!\n";
}
}
else {
print "strand-problem at gene " . $loci[$_] . "!\n";
}
}
}
#open output files
open( my $outputSequences, ">", "all_promoterSequences_of_" . $contig . ".fasta" )
|| die "outputSequences error: " . $!;
open( my $outputPositions, ">", "all_promoterPositions_of_" . $contig . ".csv" )
|| die "outputSequences error: " . $!;
print $outputPositions "name\tstart\tend\tlength\n"; #print head to positions file
my $skipNextPosition = 0;
my $currentRegion = 0;
for ( 0 .. $#positions ) {
#skip every other position
#@positions[start, end, start, end, start, ...]
#> go through all start positions only
if ($skipNextPosition) {
$skipNextPosition = 0;
}
else {
$length = $positions[ $_ + 1 ] - $positions[$_] + 1;
print $outputPositions $names[$currentRegion] . "\t"
. $positions[$_] . "\t"
. $positions[ $_ + 1 ] . "\t"
. $length . "\n";
my $comment = ">" . $names[$currentRegion] . "___" . $length . "bp\n";
my $sequence = substr( $sequence, $positions[$_] - 1, $length ) . "\n";
$sequence =~ s/(.{60})/$1\n/g; #maximum of 60 characters per line
print $outputSequences $comment;
print $outputSequences $sequence;
++$currentRegion;
$skipNextPosition = 1;
}
}
close $outputPositions;
close $outputSequences;
print $currentRegion. " promoters extracted.";
#############################
#error detection / debugging#
#############################
$skipNextPosition = 0;
for ( 0 .. $#positions ) {
if ( $_ < 0 ) {
print "\nError: Computed a negative position!";
}
if ( $_ > $nucleotides ) {
print "\nError: Computed a position \"after\" the last nucleotide!";
}
#skip every other position
#@positions[start, end, start, end, start, ...]
#> look at all start positions only
if ($skipNextPosition) {
$skipNextPosition = 0;
}
else {
$skipNextPosition = 1;
#no promoter length should be negative
if ( ( $positions[ $_ + 1 ] - $positions[$_] + 1 ) < 0 ) {
print "\nError: Computed a promoter with negative length: "
. ( $positions[ $_ + 1 ] - $positions[$_] + 1 ) . " "
. $_;
}
#no promoter length should be longer than twice the
#assumed start and end of a promoter related to the transcription-start
if ( ( $positions[ $_ + 1 ] - $positions[$_] + 1 ) > ( $from + $to ) * 2 + 1 ) {
print "\nError: Computed a promoter that is too long: "
. ( $positions[ $_ + 1 ] - $positions[$_] + 1 ) . " "
. $_;
}
#the start of a promoter should always be before the stop and not vice versa or equal
if ( $positions[$_] >= $positions[ $_ + 1 ] ) {
print "\nError: Computed twisted or equal positions: "
. $positions[$_] . ">="
. $positions[ $_ + 1 ] . " "
. $_;
}
#no overlapping of start and end should happen
if ( $_ != 0 && ( $positions[$_] <= $positions[ $_ - 1 ] ) ) {
print "\nError: Computed start of current promoter before end of last promtor: "
. $positions[$_] . "<="
. $positions[ $_ - 1 ] . " "
. $_;
}
}
}