forked from gustavo11/GFFLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGFFGene.pm
288 lines (231 loc) · 5.56 KB
/
GFFGene.pm
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
package GFFGene;
use strict 'vars';
use strict 'refs';
use GFFTranscript;
sub new {
my ( $gene_id, $gene_name, $chrom, $start_coord, $end_coord, $strand, $file ) = @_;
my $self = {
id => $gene_id,
name => $gene_name,
chrom => $chrom,
start => $start_coord,
end => $end_coord,
strand => $strand,
file => $file
};
bless $self, GFFGene;
return $self;
}
sub get_file{
my $self = shift;
return $self->{file};
}
sub set_filename {
my $self = shift;
my ($filename) = @_;
$self->{filename} = $filename;
}
sub get_filename {
my $self = shift;
return $self->{filename};
}
sub overlaps {
my $self = shift;
# $strand_based = [op_strand, same_strand, strandness]
my ( $other, $strand_based, $buffer ) = @_;
return 0 if $self->get_chrom() ne $other->get_chrom();
return 0 if $self->get_strand() ne $other->get_strand() && $strand_based eq "same_strand";
return 0 if $self->get_strand() eq $other->get_strand() && $strand_based eq "op_strand";
if (
(
$self->get_start() >= ( $other->get_start - $buffer )
&& $self->get_start() <= ( $other->get_end() + $buffer )
)
|| ( $other->get_start() >= ( $self->get_start - $buffer )
&& $other->get_start() <= ( $self->get_end() + $buffer ) )
)
{
return 1;
}
else {
return 0;
}
}
sub add_transcript {
my $self = shift;
my ( $transcript_name, $transcript_type, $start_coord, $end_coord ) = @_;
$self->{transcript}->{$transcript_name} =
GFFTranscript::new( $transcript_name, $transcript_type, $start_coord, $end_coord, $self );
return $self->{transcript}->{$transcript_name};
}
sub delete_transcript {
my $self = shift;
my ($id) = @_;
undef $self->{transcript}->{$id};
delete $self->{transcript}->{$id};
$self->adjust_boundaries_transcript_based();
}
sub get_name {
my $self = shift;
return $self->{name};
}
sub set_name {
my $self = shift;
my ($new_name) = @_;
$self->{name} = $new_name;
}
sub get_id {
my $self = shift;
return $self->{id};
}
sub set_id {
my $self = shift;
my ($new_id) = @_;
$self->{id} = $new_id;
}
sub set_description {
my $self = shift;
my ($description) = @_;
return $self->{description} = $description;
}
sub get_description {
my $self = shift;
return $self->{description};
}
sub get_transcript {
my $self = shift;
my ($transcript_name) = @_;
return $self->{transcript}->{$transcript_name};
}
sub get_start {
my $self = shift;
return $self->{start};
}
sub get_end {
my $self = shift;
return $self->{end};
}
sub set_start {
my $self = shift;
my ($start_coord) = @_;
$self->{start} = $start_coord;
}
sub set_end {
my $self = shift;
my ($end_coord) = @_;
$self->{end} = $end_coord;
}
sub get_chrom {
my $self = shift;
return $self->{chrom};
}
sub set_chrom {
my $self = shift;
my ($chrom) = @_;
$self->{chrom} = $chrom;
# TODO
#$self->{chrom_index}->{$chrom}->{$gene_id} =
# $gene_temp_feature;
}
sub get_strand {
my $self = shift;
return $self->{strand};
}
sub set_strand {
my $self = shift;
my ($strand) = @_;
$self->{strand} = $strand;
}
sub get_transcripts_hash {
my $self = shift;
$self->{transcript};
}
sub get_transcripts_array {
my $self = shift;
return values %{ $self->{transcript} };
}
sub adjust_boundaries_transcript_based {
my $self = shift;
$self->set_start( $self->get_transcript_span_start() );
$self->set_end( $self->get_transcript_span_end() );
}
sub get_transcript_span_start {
my $self = shift;
my $lower_start_value = -1;
foreach my $ref_transcript ( values %{ $self->{transcript} } ) {
my $transcript_start = $ref_transcript->get_start();
$lower_start_value = $transcript_start if ( $transcript_start < $lower_start_value || $lower_start_value == -1 );
}
return $lower_start_value;
}
sub get_transcript_span_end {
my $self = shift;
my $higher_end_value = 0;
foreach my $ref_transcript ( values %{ $self->{transcript} } ) {
my $transcript_end = $ref_transcript->get_end();
$higher_end_value = $transcript_end if $transcript_end > $higher_end_value;
}
return $higher_end_value;
}
sub toGFF {
my $self = shift;
my $str =
$self->{chrom} . "\t."
. "\tgene\t"
. $self->{start} . "\t"
. $self->{end} . "\t.\t"
. $self->{strand} . "\t.\t" . "ID="
. $self->{id} . ";" . "Name="
. $self->{name} . ";";
# Additional attributes
foreach my $attribName ( keys %{ $self->{attrib} } ) {
next if( $attribName eq "Name" );
next if( $attribName eq "ID" );
$str .= "$attribName=" . $self->{attrib}{$attribName} . ";";
}
$str =~ s/$/\n/;
foreach my $ref_transcript ( values %{ $self->{transcript} } ) {
$str .= $ref_transcript->toGFF( $self->{chrom}, $self->{id}, $self->{strand} );
}
return $str;
}
sub toGTF {
my $self = shift;
my $str;
foreach my $ref_transcript ( values %{ $self->{transcript} } ) {
$str .= $ref_transcript->toGTF( $self->{chrom}, $self->{id}, $self->{strand} );
}
return $str;
}
sub get_num_transcripts {
my $self = shift;
return scalar( values %{ $self->{transcript} } );
}
sub set_attribute {
my $self = shift;
my ( $key, $value ) = @_;
return $self->{attrib}{$key} = $value;
}
sub check_attribute {
my $self = shift;
my ($key) = @_;
return defined $self->{attrib}{$key};
}
sub get_attribute {
my $self = shift;
my ($key) = @_;
if( not defined $self->{attrib}{$key} ){
die "Gene attribute $key does not exist for gene "
. $self->get_id() . " on file " . $self->get_filename() . "\n";
}
return $self->{attrib}{$key};
}
sub num_exons {
my $self = shift;
my $num_exons = 0;
foreach my $ref_transcript ( values %{ $self->{transcript} } ) {
$num_exons += $ref_transcript->num_exons();
}
return $num_exons;
}
return 1;