-
Notifications
You must be signed in to change notification settings - Fork 2
/
Gene_obj_comparator.pm
executable file
·460 lines (298 loc) · 11.4 KB
/
Gene_obj_comparator.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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#!/usr/local/bin/perl
## This module is used to compare to Gene_objs. Everything here is 'static'. No intentional object members.
## the static fields can be reset by calling the reset method.
## The major assumption here is that the gene_obj's are on the same sequence. Only coordinates are compared, not seq info.
## used to compare Ceres cDNA alignment-based gene models to current Arabidopsis annotations.
package main;
our $SEE;
=head1 NAME
Gene_obj_comparator.pm
=cut
=head1 DESCRIPTION
This module is used to compare two gene objects (Gene_obj.pm) to see if they are identical, have the same CDS features, and/or same mRNA features. This package does not need to be instantiated. The methods are exported so they do not have to be qualified by the package name.
First, use the compare_genes() method to perform the comparison, then use the other public methods to capture the results of the comparison.
=cut
package Gene_obj_comparator;
require Exporter;
use strict;
our @ISA = qw(Exporter);
our @EXPORT = qw(compare_genes are_CDS_same are_mRNA_same reset are_identical analyze_cds_frames);
## file scoped lexicals. access via access methods. Reset to false via reset method.
my $CDS_same = 0; #initialize to false.
my $mRNA_same = 0; #initialize to false.
=item compare_gene_structures()
=over 4
B<Description:> This method performs the comparison between two Gene_obj objects. This method should be performed first, followed by the methods below to ascertain similiarities/differences.
B<Parameters:> $gene_obj1, $gene_obj2
$gene_obj1 and $gene_obj2 are of type Gene_obj specified in Gene_obj.pm
B<Returns:> [1|0]
This method returns the truth/false of an identity test. Identity tests are performed only based on coordinates.
** No alternative splicing support ** Only comparisons are done at the primary gene level. If alt isoforms exist, they should be compared separately.
=back
=cut
## module is now extended for functional annotation comparisons as well.
sub compare_gene_structures {
return (&compare_genes(@_));
}
## old method, left in for backwards compatibility. Called now by compare_gene_structures().
sub compare_genes {
my ($gene1, $gene2) = @_;
unless (ref ($gene1) && ref($gene2)) {
print STDERR "I require two gene objects to compare!\n";
return (undef());
}
&reset();
## Gather coord info
my (%CDS_coords1, %CDS_coords2, %mRNA_coords1, %mRNA_coords2);
&populate_gene_data ($gene1, \%mRNA_coords1, \%CDS_coords1);
&populate_gene_data($gene2, \%mRNA_coords2, \%CDS_coords2);
## perform coord comparison
$mRNA_same = &compare_coordsets(\%mRNA_coords1, \%mRNA_coords2);
$CDS_same = &compare_coordsets (\%CDS_coords1, \%CDS_coords2);
return ($mRNA_same && $CDS_same);
}
=item are_CDS_same()
=over 4
B<Description:> Determines whether two gene objects have identical CDS structures.
CDS refers to the protein coding segments of individual exons.
B<Parameters:> none
B<Returns:> [1|0]
1 (true) is returned if the CDS are the same.
0 (false) is returned if there are any differences in CDS structures.
=back
=cut
sub are_CDS_same {
return ($CDS_same);
}
=item are_mRNA_same()
=over 4
B<Description:> Determines if the mRNA exon structure is identical between two gene objects.
The mRNA exons are the intron-less segments of the full-length transcript, including the CDS features and untranslated regions (UTRs).
B<Parameters:> none.
B<Returns:> [1|0]
1=true, 0=false.
=back
=cut
sub are_mRNA_same {
return ($mRNA_same);
}
=item are_identical()
=over 4
B<Description:> Provides the result of an identity test between two objects. Identity tests are performed only at the coordinate level. An identical set of gene objects have identical mRNA exons and identical CDS structure coordinates.
B<Parameters:> none.
B<Returns:> [0|1]
1=true, 0=false.
=back
=cut
sub are_identical {
return ($CDS_same && $mRNA_same);
}
sub reset {
$CDS_same = 0;
$mRNA_same = 0;
}
####
sub compare_functional_annotations {
my ($A_gene, $B_gene) = @_;
unless (ref $A_gene && ref $B_gene) {
die "Must provide two Gene_obj references.\n";
}
my @updates;
## check for different com_name annotation.
my $a_com_name = $A_gene->get_product_names();
my $b_com_name = $B_gene->get_product_names();
if ($a_com_name ne $b_com_name) {
push (@updates, "product_name_change");
}
## check for different pseudogene annotation.
if ($A_gene->{is_pseudogene} != $B_gene->{is_pseudogene}) {
push (@updates, "pseudogene_change");
}
## check for gene symbol change:
if ($A_gene->get_gene_symbols() ne $B_gene->get_gene_symbols()) {
push (@updates, "gene_symbol_change");
}
## check for ec number change
if ($A_gene->get_ec_numbers() ne $B_gene->get_ec_numbers() ) {
push (@updates, "ec_number_change");
}
## check for gene name change
if ($A_gene->get_gene_names() ne $B_gene->get_gene_names()) {
push (@updates, "gene_name_change");
}
## Check for Gene Ontology updates:
my $same_GO = &same_GO_assignments ($A_gene, $B_gene);
unless ($same_GO) {
push (@updates, "GeneOntology_change");
}
return (@updates);
}
######################
# private methods
sub populate_gene_data {
my ($gene_obj, $mRNA_coords_ref, $CDS_coords_ref) = @_;
my @exons = $gene_obj->get_exons();
foreach my $exon (@exons) {
my ($mRNA_end5, $mRNA_end3) = $exon->get_mRNA_exon_end5_end3();
$mRNA_coords_ref->{$mRNA_end5} = $mRNA_end3;
my @CDS_coords = $exon->get_CDS_end5_end3();
if (@CDS_coords) {
my ($cds_end5, $cds_end3) = @CDS_coords;
$CDS_coords_ref->{$cds_end5} = $cds_end3;
}
}
}
sub compare_coordsets {
my ($cset1_ref, $cset2_ref) = @_;
my $same = 1;
foreach my $end5 (keys %$cset1_ref, keys %$cset2_ref) {
if ($cset1_ref->{$end5} != $cset2_ref->{$end5}) {
$same = 0;
last;
}
}
return ($same);
}
####
sub same_GO_assignments {
my ($A_gene, $B_gene) = @_;
my @A_gene_GO_assignments = $A_gene->get_gene_ontology_objs();
my $A_go_text = "";
my @A_go_texts;
foreach my $go_assignment (@A_gene_GO_assignments) {
push (@A_go_texts, $go_assignment->toString());
}
$A_go_text = join ("\n", @A_go_texts);
my @B_gene_GO_assignments = $B_gene->get_gene_ontology_objs();
my $B_go_text = "";
my @B_go_texts;
foreach my $go_assignment (@B_gene_GO_assignments) {
push (@B_go_texts, $go_assignment->toString());
}
$B_go_text = join ("\n", @B_go_texts);
if ($A_go_text eq $B_go_text) {
return (1);
} else {
return (0);
}
}
=item analyze_cds_frames()
=over 4
B<Description:> Given two gene objects (geneA, geneB), the coding region and introns of
geneA are compared to the coding region and introns of geneB
B<Parameters:> (geneA, geneB)
B<Returns:> compare_struct_href
compare_struct_href = {
coding_coding_same => 0|1, CDS regions overlap and are in the same frame
coding_coding_diff => 0|1, CDS regions overlap and are in different frames
coding_intron => 0|1, geneA coding region overlaps geneB intron
intron_coding => 0|1, geneA intron overlaps geneB coding
intron_intron => 0|1, geneA intron overlaps geneB intron
};
=back
=cut
####
sub analyze_cds_frames {
my ($gene_obj_A, $gene_obj_B) = @_;
my @coords = ($gene_obj_A->get_gene_span(), $gene_obj_B->get_gene_span());
@coords = sort {$a<=>$b} @coords;
my $min_lend = shift @coords;
my $max_rend = pop @coords;
my $length = $max_rend - $min_lend + 1;
## store frame for each bp.
# extragenic: (-)
# intron: (.)
# cds: ([123])
my @geneA_frames;
my @geneB_frames;
&_build_structure_vector (\@geneA_frames, $length, $min_lend, $gene_obj_A);
&_build_structure_vector (\@geneB_frames, $length, $min_lend, $gene_obj_B);
## just looking witin the model span of the template, ignoring utrs.
my $coding_coding_same = 0;
my $coding_coding_diff = 0;
my $coding_intron = 0;
my $intron_coding = 0;
my $intron_intron = 0;
for (my $i = 0; $i <= $length; $i++) {
my $geneA_class = $geneA_frames[$i];
my $geneB_class = $geneB_frames[$i];
if ($geneA_class ne '-' && $geneB_class ne '-') {
if ($geneA_class eq '.' || $geneB_class eq '.') {
## at least one is in the intron
if ($geneA_class eq '.' && $geneB_class eq '.') {
$intron_intron = 1;
}
elsif ($geneA_class ne '.') {
$coding_intron = 1;
}
elsif ($geneB_class ne '.') {
$intron_coding = 1;
}
}
else {
## neither in intron, so must be coding/coding comparison
if ($geneA_class eq $geneB_class) {
$coding_coding_same = 1;
}
else {
# must be in different frames
$coding_coding_diff = 1;
}
}
}
}
my $ret_struct = { coding_coding_same => $coding_coding_same,
coding_coding_diff => $coding_coding_diff,
coding_intron => $coding_intron,
intron_coding => $intron_coding,
intron_intron => $intron_intron,
};
return ($ret_struct);
}
sub _build_structure_vector {
my ($structure_aref, $vector_length, $lend_adjust, $gene_obj) = @_;
# extragenic: (-)
# intron: (.)
# cds: ([123])
# init all as extragenic
for (my $i = 0; $i <= $vector_length; $i++) {
$structure_aref->[$i] = "-";
}
## build coding vector portion
foreach my $exon ($gene_obj->get_exons()) {
my $cds = $exon->get_CDS_obj();
if (ref $cds) {
my ($end5, $end3) = $cds->get_coords();
$end5 -= $lend_adjust;
$end3 -= $lend_adjust;
my $phase = $cds->{phase};
unless (defined $phase) {
die "Error, phase is not set for cds." . $gene_obj->toString();
}
my $count = 1 + $phase;
if ($gene_obj->get_orientation() eq '+') {
for (my $i = $end5; $i <= $end3; $i++) {
$structure_aref->[$i] = ($count - 1) % 3;
$count++;
}
}
else {
# minus strand
for (my $i = $end5; $i >= $end3; $i--) {
$structure_aref->[$i] = ($count - 1) % 3;
$count++;
}
}
}
}
## build intron vector portion
foreach my $intron_coordset ($gene_obj->get_intron_coordinates()) {
my ($intron_lend, $intron_rend) = sort {$a<=>$b} @$intron_coordset;
$intron_lend -= $lend_adjust;
$intron_rend -= $lend_adjust;
for (my $i = $intron_lend; $i <= $intron_rend; $i++) {
$structure_aref->[$i] = '.';
}
}
}
1; #end of module