-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmake_intron_gff.pl
executable file
·154 lines (118 loc) · 3.58 KB
/
make_intron_gff.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
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
use feature 'say';
use autodie;
use FindBin;
use lib "$FindBin::Bin/lib";
use GFF::Parser;
use Log::Log4perl qw/:easy/;
use Getopt::Euclid qw( :vars<opt_> );
use Pod::Usage;
pod2usage(-verbose => 99,-sections => [qw/NAME SYNOPSIS OPTIONS/]) if !$opt_input;
Log::Log4perl->easy_init({
level => $INFO,
layout => "%d{HH:mm:ss} %p> (%L) %M - %m%n",
#file => ">log4perl.log",
});
my $gene_tag = $opt_gene_tag;
my $exon_tag = $opt_exon_tag;
my $single = $opt_single;
my $in_file = $opt_input;
my $out_file = $opt_output;
my %genes = ();
# {
# seq => {
# locus => {
# gff => $gff,
# isoform => {
# isoform1 => [$gff, ...],
# isoform2 => [$gff, ...]
# }
# }
# }
# }
open my $out, '>', $out_file;
my $parser = GFF::Parser->new(file => $in_file);
my $counter=0;
while (my $gff = $parser->next){
if (++$counter % 5000 == 0) { INFO("$counter"); }
if ($gff->feature eq 'gene'){
if (my $gene_locus = $gff->get_attribute($gene_tag)){
$genes{$gff->sequence}{$gene_locus}{gff} = $gff;
}
}
elsif ($gff->feature eq 'exon'){
if (my $exon_locus = $gff->get_attribute($exon_tag)){
my ($gene_locus, $id) = $gff->parse_locus($exon_tag);
if ($gene_locus && (!$single || $id == 1)){
push @{$genes{$gff->sequence}{$gene_locus}{isoform}{$exon_locus}}, $gff;
}
}
}
}
for my $seq (sort {$a cmp $b} keys %genes) {
for my $locus (sort {
$genes{$seq}{$a}{gff}->start <=> $genes{$seq}{$b}{gff}->start
} grep {
exists $genes{$seq}{$_}{gff} && exists $genes{$seq}{$_}{isoform}
} keys %{$genes{$seq}}) {
# {gff => $gff, isoform => {isoform1 => $gff, ..}}
my $locus_hash = $genes{$seq}{$locus};
my $gene_gff = $locus_hash->{gff};
my $start = $gene_gff->start;
my $end = $gene_gff->end;
DEBUG($gene_gff->to_string);
for my $exon_locus (sort keys %{$locus_hash->{isoform}}){
my @exons = sort {$a->start <=> $b->start} @{$locus_hash->{isoform}{$exon_locus}};
for my $exon (@exons){
DEBUG($exon->to_string);
}
for my $exon (@exons){
if ($start < $exon->start){
#DEBUG(
say $out (
join "\t",
$gene_gff->sequence,
$gene_gff->source,
'intron',
$start,
($exon->start-1),
q{.},
$gene_gff->frame // q{.},
q{.},
$exon->attribute_string,
);
}
$start = $exon->end + 1;
}
}
}
}
=head1 NAME
make_intron_gff.pl - Create intron file from exon and gene annotation file.
=head1 SYNOPSIS
make_intron_gff.pl --input TAIR8_gmod.gff --output introns.gff --single
=head1 OPTIONS
=over
=item --gene-tag <tag> | -g <tag>
Gene's locus key. (default: ID)
=for Euclid
tag.default: 'ID'
=item --exon-tag <tag> | -g <tag>
Exon's locus key. (default: Parent)
=for Euclid
tag.default: 'Parent'
=item --single
grab only one isoform per gene.
=item --input <file>
Input GFF File with exons and gffs.
=for Euclid
file.type: readable
=item --output <file>
Output file (stdout by default)
=for Euclid
file.default: '-'
=back
=cut