forked from wang-q/withncbi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gff2runlist.pl
363 lines (314 loc) · 10.6 KB
/
gff2runlist.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
#!/usr/bin/perl
use strict;
use warnings;
use autodie;
use Getopt::Long;
use FindBin;
use YAML::Syck qw(Dump Load DumpFile LoadFile);
use Path::Tiny;
use IO::Zlib;
use Set::Scalar;
use AlignDB::IntSpan;
use App::RL::Common;
#----------------------------------------------------------#
# GetOpt section
#----------------------------------------------------------#
=head1 NAME
gff2runlist.pl - Convert gff3 file to chromosome runlists
=head1 SYNOPSIS
perl gff2runlist.pl [options]
Options:
--help -? brief help message
--file -f STR one gff3 file, gzipped file is supported
--size -s STR chr.sizes
--range -r INT range of up/down-stream. Default is [1000]
--clean -c up/down-stream don't contain any regions of other genes
--remove -r remove 'chr0' from chromosome names
=cut
GetOptions(
'help|?' => sub { Getopt::Long::HelpMessage(0) },
'file|f=s' => \( my $infile ),
'size|s=s' => \( my $size ),
'range|r=i' => \( my $range = 1000 ),
'clean|c' => \( my $clean ),
'remove|r' => \( my $remove_chr ),
) or Getopt::Long::HelpMessage(1);
#----------------------------------------------------------#
# Processing
#----------------------------------------------------------#
# only keep chromosomes listed in chr.sizes
# avoid out of chromosome for up/down-stream
my $chr_set_of = {};
if ( defined $size ) {
if ( path($size)->is_file ) {
printf "==> Load chr.sizes\n";
my $length_of = App::RL::Common::read_sizes($size);
for my $chr ( keys %{$length_of} ) {
my $set = AlignDB::IntSpan->new;
$set->add_pair( 1, $length_of->{$chr} );
$chr_set_of->{$chr} = $set;
}
}
else {
die "*** [$size] doesn't exist!\n";
}
}
printf "==> Load gff3 file\n";
my $in_fh = IO::Zlib->new( $infile, "rb" );
# runlists
my $all_gene = {}; # all genes combined
my $sep_gene = {}; # seperated genes
my $all_repeat = {}; # dust and trf, repeatmasker in rmout2runlist.pl
# chromosome names
my $all_name_set = Set::Scalar->new;
# current transcript in Ensembl gff3 or current mrna in JGI gff3
my $cur_transcript;
my $cache = {
exon => [],
five_prime_UTR => [],
three_prime_UTR => [],
CDS => [],
};
while (1) {
my $line = <$in_fh>;
last unless $line;
next if $line =~ /^#/;
chomp $line;
my @array = split( "\t", $line );
my $type = $array[2];
my $chr = $array[0];
$chr =~ s/chr0?//i if $remove_chr;
my $start = $array[3];
my $end = $array[4];
my $strand = $array[6]; # strand may be "."
if ( $strand ne "-" ) {
$strand = "+";
}
my @attrs = split ";", $array[8];
my %attr_of;
for my $item (@attrs) {
my @pair = split "=", $item;
if ( @pair == 2 ) {
$attr_of{ $pair[0] } = $pair[1];
}
}
# only keep chromosomes listed in chr.sizes
if ( defined $size ) {
next unless exists $chr_set_of->{$chr};
}
if ( $type eq 'gene' ) {
my $gene_id;
if ( exists $attr_of{gene_id} ) { # Ensembl
$gene_id = $attr_of{gene_id};
}
elsif ( exists $attr_of{Name} ) { # JGI
$gene_id = $attr_of{Name};
}
else {
$gene_id = $attr_of{ID};
print " " x 4 . "Can't get gene_id\n";
print " " x 4 . "$line\n";
}
printf "gene: %s\n", $gene_id;
$all_name_set->insert($chr);
# gene and up/down-stream
my $set_of = {
gene => AlignDB::IntSpan->new,
upstream => AlignDB::IntSpan->new,
downstream => AlignDB::IntSpan->new,
};
$set_of->{gene}->add_pair( $start, $end );
$set_of->{upstream}->add_pair( $start - $range, $start - 1 );
$set_of->{downstream}->add_pair( $end + 1, $end + $range );
# swap up/down-stream for negtive strand
if ( $strand eq "-" ) {
( $set_of->{upstream}, $set_of->{downstream} )
= ( $set_of->{downstream}, $set_of->{upstream} );
}
# avoid out of chromosome for up/down-stream
if ( exists $chr_set_of->{$chr} ) {
$set_of->{upstream} = $set_of->{upstream}->intersect( $chr_set_of->{$chr} );
$set_of->{downstream} = $set_of->{downstream}->intersect( $chr_set_of->{$chr} );
}
for my $f (qw{gene upstream downstream}) {
# initialize sets
if ( !exists $all_gene->{$f} ) {
$all_gene->{$f} = {};
$sep_gene->{$f} = {};
}
if ( !exists $all_gene->{$f}{$chr} ) {
$all_gene->{$f}{$chr} = AlignDB::IntSpan->new;
}
# add sets
$all_gene->{$f}{$chr}->add( $set_of->{$f} );
$sep_gene->{$f}{$gene_id} = { $chr => $set_of->{$f} };
}
}
if ( $type eq 'repeat_region' ) {
my $f;
if ( exists $attr_of{description} ) {
$f = $attr_of{description};
next unless ( $f eq "dust" or $f eq "trf" );
}
# initialize sets
if ( !exists $all_repeat->{$f} ) {
$all_repeat->{$f} = {};
}
if ( !exists $all_repeat->{$f}{$chr} ) {
$all_repeat->{$f}{$chr} = AlignDB::IntSpan->new;
}
$all_name_set->insert($chr);
# add sets
$all_repeat->{$f}{$chr}->add_pair( $start, $end );
}
if ( ( defined $cur_transcript )
and ( $type =~ /transcript|mrna/i ) )
{
printf "transcript: %s\n", $cur_transcript;
process_transcript( $all_gene, $sep_gene, $cur_transcript, $cache );
# initialize the next transcript
my $transcript_id;
if ( exists $attr_of{transcript_id} ) { # Ensembl
$transcript_id = $attr_of{transcript_id};
}
elsif ( exists $attr_of{Name} ) { # JGI
$transcript_id = $attr_of{Name};
}
else {
$transcript_id = $attr_of{ID};
print " " x 4 . "Can't get transcript_id\n";
print " " x 4 . "$line\n";
}
$cur_transcript = $transcript_id;
# empty caches
$cache = {
exon => [],
five_prime_UTR => [],
three_prime_UTR => [],
CDS => [],
};
}
elsif ( $type =~ /transcript|mrna/i ) { # First transcript
my $transcript_id;
if ( exists $attr_of{transcript_id} ) {
$transcript_id = $attr_of{transcript_id};
}
elsif ( exists $attr_of{Name} ) { # JGI
$transcript_id = $attr_of{Name};
}
else {
$transcript_id = $attr_of{ID};
print " " x 4 . "Can't get transcript_id\n";
print " " x 4 . "$line\n";
}
$cur_transcript = $transcript_id;
}
elsif ( $type eq 'exon' ) {
push @{ $cache->{$type} }, [ $chr, $start, $end ];
}
elsif ( $type eq 'five_prime_UTR' ) {
push @{ $cache->{$type} }, [ $chr, $start, $end ];
}
elsif ( $type eq 'three_prime_UTR' ) {
push @{ $cache->{$type} }, [ $chr, $start, $end ];
}
elsif ( $type eq 'CDS' ) {
push @{ $cache->{$type} }, [ $chr, $start, $end ];
}
}
$in_fh->close;
# last transcript
if ( scalar @{ $cache->{exon} } > 0 ) {
printf "transcript: %s\n", $cur_transcript;
process_transcript( $all_gene, $sep_gene, $cur_transcript, $cache );
}
# intergenic regions
{
print "==> Intergenic\n";
$all_gene->{intergenic} = {};
for my $chr ( $all_name_set->members ) {
my $set = $chr_set_of->{$chr}->copy;
for my $f (qw{gene upstream downstream}) {
if ( exists $all_gene->{$f}{$chr} ) {
$set->remove( $all_gene->{$f}{$chr} );
}
}
$all_gene->{intergenic}{$chr} = $set->runlist;
}
}
# clean up/down-stream
if ($clean) {
for my $f (qw{upstream downstream}) {
print "==> Clean $f\n";
for my $chr ( $all_name_set->members ) {
$all_gene->{$f}{$chr}->remove( $all_gene->{gene}{$chr} );
}
for my $id ( keys %{ $sep_gene->{gene} } ) {
for my $chr ( keys %{ $sep_gene->{gene}{$id} } ) {
#print "$f $id $chr\n";
$sep_gene->{$f}{$id}{$chr}->remove( $all_gene->{gene}{$chr} );
}
}
}
}
#----------------------------------------------------------#
# Outputs
#----------------------------------------------------------#
print "==> Write Output files\n";
for my $f (qw{gene upstream downstream exon five_prime_UTR three_prime_UTR CDS intron}) {
for my $chr ( $all_name_set->members ) {
$all_gene->{$f}{$chr} = $all_gene->{$f}{$chr}->runlist;
}
for my $id ( keys %{ $sep_gene->{$f} } ) {
for my $chr ( keys %{ $sep_gene->{$f}{$id} } ) {
$sep_gene->{$f}{$id}{$chr} = $sep_gene->{$f}{$id}{$chr}->runlist;
}
}
DumpFile( "sep-$f.yml", $sep_gene->{$f} );
}
DumpFile( "all-gene.yml", $all_gene );
for my $f ( keys %{$all_repeat} ) {
for my $chr ( keys %{ $all_repeat->{$f} } ) {
$all_repeat->{$f}{$chr} = $all_repeat->{$f}{$chr}->runlist;
}
}
DumpFile( "all-repeat.yml", $all_repeat );
exit;
#----------------------------------------------------------#
# Subroutines
#----------------------------------------------------------#
sub process_transcript {
my $all_gene = shift;
my $sep_gene = shift;
my $cur_transcript = shift;
my $cache = shift;
# process cached features
my $set_of = {
exon => AlignDB::IntSpan->new,
five_prime_UTR => AlignDB::IntSpan->new,
three_prime_UTR => AlignDB::IntSpan->new,
CDS => AlignDB::IntSpan->new,
intron => AlignDB::IntSpan->new,
};
for my $f (qw{exon five_prime_UTR three_prime_UTR CDS}) {
for my $item ( @{ $cache->{$f} } ) {
$set_of->{$f}->add_pair( $item->[1], $item->[2] );
}
}
$set_of->{intron} = $set_of->{exon}->holes;
my $chr = $cache->{exon}[0][0];
for my $f (qw{exon five_prime_UTR three_prime_UTR CDS intron}) {
# initialize sets
if ( !exists $all_gene->{$f} ) {
$all_gene->{$f} = {};
$sep_gene->{$f} = {};
}
if ( !exists $all_gene->{$f}{$chr} ) {
$all_gene->{$f}{$chr} = AlignDB::IntSpan->new;
}
# add sets
$all_gene->{$f}{$chr}->add( $set_of->{$f} );
$sep_gene->{$f}{$cur_transcript} = { $chr => $set_of->{$f} };
}
return;
}