forked from gustavo11/GFFLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGFFCDS.pm
284 lines (215 loc) · 4.44 KB
/
GFFCDS.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
package GFFCDS;
use strict 'vars';
use strict 'refs';
require GFFTranscript;
# use overload '==' => \&compare;
sub new {
my ( $exon_name, $start_coord, $end_coord, $phase, $parent ) = @_;
my $self = {
id => $exon_name,
start => $start_coord,
end => $end_coord,
phase => $phase,
parent => $parent
};
bless $self, GFFCDS;
return $self;
}
sub copy {
my ( $ref, $parent ) = @_;
my $self;
if ( defined $parent ) {
$self = {
id => $ref->{id},
start => $ref->{start},
end => $ref->{end},
phase => $ref->{phase},
parent => $parent
};
}
else {
$self = {
id => $ref->{id},
start => $ref->{start},
end => $ref->{end},
phase => $ref->{phase},
parent => $ref->{parent}
};
}
bless $self, GFFCDS;
# Copying other atrributes
$self->cpy_attrib($ref);
return $self;
}
sub cpy_attrib {
my $self = shift;
my ($ref) = @_;
foreach my $ckey ( keys %{ $ref->{attrib} } ) {
$self->{attrib}{$ckey} = $ref->{attrib}{$ckey};
}
}
sub get_parent {
my $self = shift;
return $self->{parent};
}
sub set_id {
my $self = shift;
my ($new_id) = @_;
$self->{id} = $new_id;
}
sub get_id {
my $self = shift;
return $self->{id};
}
sub get_start {
my $self = shift;
return $self->{start};
}
sub get_end {
my $self = shift;
return $self->{end};
}
sub get_length {
my $self = shift;
return $self->{end} - $self->{start} + 1;
}
sub get_phase {
my $self = shift;
return $self->{phase};
}
sub set_phase {
my $self = shift;
my ($phase) = @_;
$self->{phase} = $phase;
}
sub set_start {
my $self = shift;
my ($coord_start) = @_;
$self->{start} = $coord_start;
}
sub set_end {
my $self = shift;
my ($coord_end) = @_;
$self->{end} = $coord_end;
}
sub set_parent_exon {
my $self = shift;
my ($ref_exon) = @_;
$self->{exon} = $ref_exon;
}
sub get_parent_exon {
my $self = shift;
return $self->{exon};
}
sub is_attached {
my $self = shift;
return defined $self->{exon};
}
sub toGFF {
my $self = shift;
my ( $chrom, $parent, $strand ) = @_;
my $str =
$chrom . "\t.\t" . "CDS" . "\t"
. $self->{start} . "\t"
. $self->{end} . "\t.\t"
. $strand . "\t"
. $self->{phase} . "\t" . "ID="
. $self->{id} . ";"
. "Parent=$parent;\n";
return $str;
}
sub toGTF {
my $self = shift;
my ( $chrom, $parent, $strand ) = @_;
my $str;
#print STDERR "INSIDE CDS\n";
#getc();
# If the CDS is the first one in the transcript
if( $self->is_first_strand_based() ){
my $start_start_codon;
my $end_start_codon;
#print STDERR "FIRST CDS\n";
#getc();
if ( $strand eq '+' ) {
$start_start_codon = $self->{start};
$end_start_codon = $self->{start} + 2;
}
else {
$start_start_codon = $self->{end} - 2;
$end_start_codon = $self->{end};
}
$str .=
$chrom . "\t.\t" . "start_codon" . "\t"
. $start_start_codon . "\t"
. $end_start_codon . "\t.\t"
. $strand . "\t"
. $self->{phase} . "\n";
# If the CDS is the last one in the transcript the STOP codon
# should not be part of it
}
if ( $self->is_last_strand_based() ) {
#print STDERR "LAST CDS\n";
#getc();
my $start = $self->{start};
my $end = $self->{end};
if ( $strand eq '+' ) {
$end -= 3;
}
else {
$start += 3;
}
if( $start < $end ){
$str .=
$chrom . "\t.\t" . "CDS" . "\t"
. $start . "\t"
. $end . "\t.\t"
. $strand . "\t"
. $self->{phase} . "\n";
}
my $start_stop_codon;
my $end_stop_codon;
if ( $strand eq '+' ) {
$start_stop_codon = $self->{end} - 2;
$end_stop_codon = $self->{end};
}
else {
$start_stop_codon = $self->{start};
$end_stop_codon = $self->{start} + 2;
}
$str .=
$chrom . "\t.\t" . "stop_codon" . "\t"
. $start_stop_codon . "\t"
. $end_stop_codon . "\t.\t"
. $strand . "\t"
. $self->{phase} . "\n";
} else {
#print STDERR "REGULAR CDS\n";
#getc();
$str .=
$chrom . "\t.\t" . "CDS" . "\t"
. $self->{start} . "\t"
. $self->{end} . "\t.\t"
. $strand . "\t"
. $self->{phase} . "\n";
}
return $str;
}
sub set_attribute {
my $self = shift;
my ( $key, $value ) = @_;
return $self->{attrib}{$key} = $value;
}
sub get_attribute {
my $self = shift;
my ($key) = @_;
return $self->{attrib}{$key};
}
sub is_last_strand_based {
my $self = shift;
return $self eq $self->{parent}->get_last_CDS_strand_based();
}
sub is_first_strand_based {
my $self = shift;
return $self eq $self->{parent}->get_first_CDS_strand_based();
}
return 1;