forked from PASApipeline/PASApipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSplice_graph_assembler.pm
executable file
·2425 lines (1938 loc) · 87.2 KB
/
Splice_graph_assembler.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/local/bin/perl
package main;
our $SEE;
package CDNA::Splice_graph_assembler;
use strict;
use warnings;
use Carp;
use Overlap_piler;
use CDNA::CDNA_alignment;
use CDNA::Alignment_segment;
use Data::Dumper;
no warnings "recursion";
our $FUZZ_DIST = 20; # bp's not to trust at non-splice termini; untrustworthy alignment extensions.
## allowable connections between gene structure components:
my %ACCEPTABLE_CONNECTIONS = ( "terminal_left_exon" => {"intron" => 1},
"internal_exon" => {"intron" => 1},
"intron" => {"internal_exon" => 1,
"terminal_right_exon" => 1,
},
);
####
sub new {
my $packagename = shift;
my $self = {
_graph_nodes => [], # each and every graph node (main repository)
_incoming_alignments => [], # alignments to assemble
_assemblies => [], # resulting alignment assemblies
_unincorporated_alignments => [],
_valid_splice_paths => [], # Splice_graph_path objects
_assembled_splice_paths => [], # compatible splice paths chained into splice path assemblies.
## other helpers
_graph_node_hashkey_lookup => {}, # key is lend,rend,type,orient
_graph_node_via_nodeID => {}, # node access by nodeID
## various node types: (all subsets of _graph_nodes and accessible via helpers above.
_internal_exons => [],
_introns => [],
_terminal_left_exons => [],
_terminal_right_exons => [],
_singleton_exons => [],
## misc
_terminal_exon_nodeID_to_nonsplice_position_list => {}, # track coords of non-splice site ends in terminal exon supports
};
bless ($self, $packagename);
return ($self);
}
####
sub assemble_alignments {
my $self = shift;
my @alignments = @_;
## build the splicing graph.
$self->build_splicing_graph(@alignments);
## Chain together splice paths that are compatible:
print "-Chaining compatible splice paths\n" if $SEE;
$self->_chain_compatible_splice_paths();
## Extend splice paths into maximally scoring complete paths with terminal exons
print "-Extending splice paths into maximal structures.\n" if $SEE;
$self->_extend_splice_paths_to_termini(); ## products are included in the assemblies list.
## Add the singleton assemblies
$self->_append_singletons_to_assembly_list(); ## convert the singletons to cdna alignments and add to assembly list
## assign the incoming alignments to the assemblies that contain them.
print "-Assigning transcript alignments to assemblies\n" if $SEE;
my $have_unincorporated_alignments_flag = $self->_correlate_assemblies_with_incoming_alignments();
if ($have_unincorporated_alignments_flag) {
## find paths from unincorporated terminal segments.
print "-Not all alignments were included. Exploring assemblies from uninorporated alignments\n" if $SEE;
$self->_explore_assemblies_from_unincorporated_alignments();
print "-Assigning transcript alignments to assemblies, again...\n" if $SEE;
$have_unincorporated_alignments_flag = $self->_correlate_assemblies_with_incoming_alignments();
if ($have_unincorporated_alignments_flag) {
## something horribly has gone wrong.
confess "Error, not all incoming alignments are accounted for!\n" . $self->toString();
}
}
print $self->toString() if $SEE;
}
####
sub build_splicing_graph {
my $self = shift;
my @alignments = @_;
$self->set_incoming_alignments(@alignments);
## going to process the alignments in several phases:
# -decompose alignments into structural components
# -identify valid paths to connect components
# -extract assemblies using valid paths
# -associate transcripts with assemblies
## first, examine the alignments with the most segments first:
@alignments = reverse sort {$a->get_num_segments() <=> $b->get_num_segments()} @alignments;
## build components of the splice graph
## add internal exons and all introns to splice graph: (unambiguous structures)
## terminal structures are not as well defined. Apply these later.
print "-Decomposing alignments into gene structure components.\n" if $SEE;
foreach my $alignment (@alignments) {
if ($alignment->get_num_segments() > 1) {
$self->_decompose_unambiguous_alignment_structures_add_nodes($alignment);
}
}
## Add terminal exons:
## Only add terminal exons where it's clear that it's not just part of an existing internal exon
print "-Adding terminal exon segments\n" if $SEE;
my @single_segments; # capture those alignments w/o introns
foreach my $alignment (@alignments) {
my $segment_orient = $alignment->get_spliced_orientation();
if ($alignment->get_num_segments() > 1) {
foreach my $segment ($alignment->get_alignment_segments()) {
if ($segment->is_first() || $segment->is_last()) {
my $exon_type = ($segment->is_first()) ? "terminal_left_exon" : "terminal_right_exon";
$self->_try_terminal_exon_addition($exon_type, $segment, $segment_orient);
## at this point, introns and internal exons are scored only by perfect support (exact boundaries).
## terminal exons, on the other hand, have evidence scored based on boundary match.
## Now, need to augment scores of internal exons that encompass terminal exons
my ($segment_lend, $segment_rend) = $segment->get_coords();
$self->_augment_internal_exon_scores_using_terminal_alignment_segment($exon_type, $segment_lend,
$segment_rend, $segment_orient);
}
}
}
else {
push (@single_segments, $alignment);
}
}
## reconstruct some internal exons based on overlapping right/left terminal exons
print "-Merging overlapping right-to-left terminal exons\n" if $SEE;
$self->_merge_overlapping_right_to_left_terminal_exons();
## apply intron-less segments as evidence to internal and terminal segments
## and instantiate single exons where they're not simply supporting existing structures.
print "-Analyzing intronless alignments\n" if $SEE;
$self->_analyze_intronless_alignments(@single_segments);
print "-Building the splice graph\n" if $SEE;
$self->_build_splice_graph(); # could/should have done this earlier during alignment parse for efficiency.
return;
}
####
sub _find_internal_exons_encompassing_coords_and_share_boundary {
my $self = shift;
my ($exon_type, $segment_lend, $segment_rend, $segment_orient) = @_;
unless ($exon_type =~ /left|right/) {
confess "invalide terminal exon type: $exon_type\n";
}
my @internal_exons_found;
foreach my $internal_exon (@{$self->{_internal_exons}}) {
my $internal_exon_orient = $internal_exon->get_orient();
my ($internal_exon_lend, $internal_exon_rend) = $internal_exon->get_coords();
## if internal exon encompasses the terminal exon, add it to its evidence collection
if ( ($segment_orient eq $internal_exon_orient)
&&
($internal_exon_lend <= ($segment_lend + $FUZZ_DIST) && ($segment_rend - $FUZZ_DIST) <= $internal_exon_rend) # internal encompasses it
&&
( ($exon_type eq "terminal_right_exon" && $internal_exon_lend == $segment_lend)
||
($exon_type eq "terminal_left_exon" && $internal_exon_rend == $segment_rend) ) ## a splice boundary in common
)
{
push (@internal_exons_found, $internal_exon);
}
}
return (@internal_exons_found);
}
sub _find_terminal_exons_encompassing_segment {
my $self = shift;
my ($lend, $rend, $orient) = @_;
my @nodes_encompassing_segment;
foreach my $node_obj (@{$self->{_terminal_left_exons}}, @{$self->{_terminal_right_exons}}) {
my ($node_lend, $node_rend) = $node_obj->get_coords();
my $node_orient = $node_obj->get_orient();
if ( ($node_orient eq $orient || $orient eq '?') &&
($lend + $FUZZ_DIST >= $node_lend) &&
($rend - $FUZZ_DIST <= $node_rend) ) {
push (@nodes_encompassing_segment, $node_obj);
}
}
return (@nodes_encompassing_segment);
}
####
sub get_assemblies {
my $self = shift;
return (@{$self->{_assemblies}});
}
####
sub get_incoming_alignments {
my $self = shift;
return (@{$self->{_incoming_alignments}});
}
####
sub get_unincorporated_alignments {
my $self = shift;
return (@{$self->{_unincorporated_alignments}});
}
####
sub _add_alignment_assembly {
my $self = shift;
my (@cdna_alignments) = @_;
push (@{$self->{_assemblies}}, @cdna_alignments);
return;
}
####
sub set_incoming_alignments {
my $self = shift;
my @alignments = @_;
@{$self->{_incoming_alignments}} = @alignments;
return;
}
####
sub _decompose_unambiguous_alignment_structures_add_nodes {
my $self = shift;
my ($alignment) = @_;
my $spliced_orient = $alignment->get_spliced_orientation();
my @path_nodes;
## Add internal exons
my @segments = $alignment->get_alignment_segments();
foreach my $segment (@segments) {
#print "seg: " . $segment->toString() . "\n";
if ($segment->is_internal()) {
my ($exon_lend, $exon_rend) = $segment->get_coords();
my $node = $self->_add_internal_exon($exon_lend, $exon_rend, $spliced_orient);
push (@path_nodes, $node);
}
}
## Add introns
my @intron_coords = $alignment->get_intron_coords();
foreach my $intron_coordset (@intron_coords) {
my ($intron_lend, $intron_rend) = @$intron_coordset; #already sorted
my $node = $self->_add_intron($intron_lend, $intron_rend, $spliced_orient);
push (@path_nodes, $node);
}
## add a path
$self->_extract_and_add_path_from_node_list(@path_nodes);
return;
}
####
sub _extract_and_add_path_from_node_list {
my $self = shift;
my @path_nodes = @_;
## sort by genome order:
@path_nodes = sort {$a->{lend} <=> $b->{lend}} @path_nodes;
my @ordered_nodeID_list;
my @coords;
foreach my $path_node (@path_nodes) {
my $nodeID = $path_node->get_nodeID();
push (@ordered_nodeID_list, $nodeID);
push (@coords, $path_node->get_coords());
}
my $orient = $path_nodes[0]->get_orient();
@coords = sort {$a<=>$b} @coords;
my $lend = shift @coords;
my $rend = pop @coords;
my $splice_graph_path = Splice_graph_path->new($lend, $rend, $orient, \@ordered_nodeID_list);
$self->_add_splice_graph_path($splice_graph_path);
return;
}
####
sub _try_terminal_exon_addition {
my $self = shift;
my ($exon_type, $segment, $orient) = @_;
my ($segment_lend, $segment_rend) = $segment->get_coords();
print "Examining terminal exon: $exon_type, $segment_lend, $segment_rend\n" if $SEE;
## only consider this a genuine terminal exon if it doesn't appear to be part of an
## existing internal exon from a more complete alignment
if (my @internal_segments = $self->_find_internal_exons_encompassing_coords_and_share_boundary($exon_type, $segment_lend, $segment_rend, $orient)) {
if ($SEE) {
print "$exon_type, $segment_lend-$segment_rend, $orient, found already represented by internal exons:\n";
foreach my $internal_segment (@internal_segments) {
print "\t" . $internal_segment->toString() . "\n";
}
}
return;
}
my $exon = $self->_find_existing_terminal_exon ($exon_type, $segment_lend, $segment_rend, $orient);
if ($exon) {
print "-found existing terminal exon with shared boundary: " . $exon->toString() . "\n" if $SEE;
my $nodeID = $exon->get_nodeID();
## check for boundary adjustment:
my ($exon_lend, $exon_rend) = $exon->get_coords();
my $nonsplice_coord = undef;
if ($exon_type eq "terminal_left_exon") {
$nonsplice_coord = $segment_lend;
if ($segment_lend < $exon_lend) {
print "-extending left boundary to $segment_lend\n" if $SEE;
$exon->set_coords($segment_lend, $exon_rend); # extend left boundary
}
}
elsif ($exon_type eq "terminal_right_exon") {
$nonsplice_coord = $segment_rend;
if ($segment_rend > $exon_rend) {
print "-extending right boundary to $segment_rend\n" if $SEE;
$exon->set_coords($exon_lend, $segment_rend);
}
}
else {
confess "Error, exon type $exon_type not accounted for. "; # should never get here anyway
}
$exon->increment_evidence_support(); ## account for extra evidence
$self->_add_to_terminal_exon_nonsplice_position_list($nodeID, $nonsplice_coord);
}
else {
## add new terminal exon:
$self->_add_graph_node($exon_type, $segment_lend, $segment_rend, $orient);
}
return;
}
####
sub _add_to_terminal_exon_nonsplice_position_list {
my $self = shift;
my ($nodeID, $nonsplice_coord) = @_;
my $nodeID_to_pos_list_href = $self->{_terminal_exon_nodeID_to_nonsplice_position_list};
my $list_aref = $nodeID_to_pos_list_href->{$nodeID};
unless (ref $list_aref) {
$list_aref = $nodeID_to_pos_list_href->{$nodeID} = [];
}
push (@$list_aref, $nonsplice_coord);
return;
}
####
sub _find_existing_terminal_exon {
my $self = shift;
my ($exon_type, $segment_lend, $segment_rend, $orient) = @_;
my $exon_list_aref = undef;
if ($exon_type eq "terminal_left_exon") {
$exon_list_aref = $self->{_terminal_left_exons};
}
elsif ($exon_type eq "terminal_right_exon") {
$exon_list_aref = $self->{_terminal_right_exons};
}
else {
confess "exon_type $exon_type not accepted for terminal exons";
}
foreach my $exon (@$exon_list_aref) {
my ($lend, $rend) = $exon->get_coords();
if (
($exon->get_orient() eq $orient) &&
(
($exon_type eq "terminal_left_exon" && $rend == $segment_rend)
||
($exon_type eq "terminal_right_exon" && $lend == $segment_lend)
)
)
{
return ($exon); # found it!
}
}
return (undef); # didn't find one.
}
####
sub _augment_internal_exon_scores_using_terminal_alignment_segment {
my $self = shift;
my ($exon_type, $segment_lend, $segment_rend, $segment_orient) = @_;
my @relevant_internal_exons = $self->_find_internal_exons_encompassing_coords_and_share_boundary($exon_type, $segment_lend, $segment_rend, $segment_orient);
foreach my $internal_exon (@relevant_internal_exons) {
$internal_exon->increment_evidence_support();
}
return;
}
####
sub _merge_overlapping_right_to_left_terminal_exons {
my $self = shift;
## looking for this situation:
# <----------- right terminal exon
# ---------> left terminal exon
# that can be merged into:
# <--------------> an internal exon
#
my %nodeIDs_targeted_for_removal; # if they fully overlap, construct a nice internal exon w/o extensions beyond splice boundary
foreach my $right_terminal_exon (@{$self->{_terminal_right_exons}}) {
my $right_orient = $right_terminal_exon->get_orient();
my ($right_lend, $right_rend) = $right_terminal_exon->get_coords();
my $right_nodeID = $right_terminal_exon->get_nodeID();
foreach my $left_terminal_exon (@{$self->{_terminal_left_exons}}) {
my $left_orient = $left_terminal_exon->get_orient();
my ($left_lend, $left_rend) = $left_terminal_exon->get_coords();
my $left_nodeID = $left_terminal_exon->get_nodeID();
unless ($right_orient eq $left_orient) { next; } # must be transcribed on same strand!
## check for overlap:
unless ($left_lend <= $right_rend && $left_rend >= $right_lend) { next;}
## make sure right's left splice is before left's right splice (doh! should have better names).
unless ($right_lend < $left_rend) { next; }
## check for extensions:
my $left_overhang = $right_lend - $left_lend;
my $right_overhang = $right_rend - $left_rend;
my $merge_flag = 0;
if ($left_overhang <= $FUZZ_DIST && $right_overhang <= $FUZZ_DIST) {
## nice merge, as in illustration
$merge_flag = 1;
## also, target these for deletion now.
$nodeIDs_targeted_for_removal{$right_nodeID} = 1;
$nodeIDs_targeted_for_removal{$left_nodeID} = 1;
}
else {
## must check position lists to see if transcripts are contained that have
## nicely overlapping boundaries
if ($self->_right_left_terminal_exons_overlap_via_position_lists($right_nodeID, $left_nodeID, $right_lend, $left_rend)) {
$merge_flag = 1;
if ($left_overhang <= $FUZZ_DIST) { ## check for insufficient overhang
$nodeIDs_targeted_for_removal{$left_nodeID} = 1;
}
if ($right_overhang <= $FUZZ_DIST) {
$nodeIDs_targeted_for_removal{$right_nodeID} = 1;
}
}
}
if ($merge_flag) {
$self->_merge_terminal_exons($right_terminal_exon, $left_terminal_exon);
}
}
}
## process deletions:
if (%nodeIDs_targeted_for_removal) {
$self->_purge_nodes(%nodeIDs_targeted_for_removal);
}
return;
}
####
sub _merge_terminal_exons {
my $self = shift;
my ($right_terminal_exon, $left_terminal_exon) = @_;
my ($right_lend, $right_rend) = $right_terminal_exon->get_coords();
my ($left_lend, $left_rend) = $left_terminal_exon->get_coords();
my ($exon_lend, $exon_rend) = ($right_lend, $left_rend); ## splice junctions for new internal exon
my $orient = $right_terminal_exon->get_orient();
if ($orient ne $left_terminal_exon->get_orient()) {
confess "Error, trying to merge two terminal exons with opposite transcriptional orientations!";
}
if ($self->_graph_node_exists("internal_exon", $exon_lend, $exon_rend, $orient)) {
confess "Error, trying to merge two terminal exons into an internal exon that already exists!";
}
my $node = $self->_add_graph_node("internal_exon", $exon_lend, $exon_rend, $orient);
## add to it the evidence from the other nodes being merged:
my $evidence_support_to_add = $left_terminal_exon->get_num_evidence_support() + $right_terminal_exon->get_num_evidence_support();
$evidence_support_to_add -= 1; # node already has value of one.
if ($evidence_support_to_add) {
$node->increment_evidence_support($evidence_support_to_add);
}
return;
}
####
sub _add_splice_graph_path {
my $self = shift;
my ($splice_graph_path) = @_;
## add it as long as:
# -it's not a subpath of an already existing path
# -if an existing path is a subpath of this, remove it and replace it with this path
my @current_valid_splice_paths = $self->_get_valid_splice_paths();
## check to see if splice_graph_path is already represented in the current path list:
foreach my $current_valid_splice_path (@current_valid_splice_paths) {
if ($splice_graph_path->is_subpath_of($current_valid_splice_path)) {
return; # nothing to do; path is already included as a subset of the current path set
}
}
# if got this far, our new path is not already fully represented.
# add it, and any other existing splice paths that are not a subpath of it.
my @new_valid_splice_paths = ($splice_graph_path);
foreach my $current_valid_splice_path (@current_valid_splice_paths) {
if (! $current_valid_splice_path->is_subpath_of($splice_graph_path)) {
push (@new_valid_splice_paths, $current_valid_splice_path);
}
}
$self->_set_valid_splice_paths(@new_valid_splice_paths);
return;
}
####
sub _get_valid_splice_paths {
my $self = shift;
return (@{$self->{_valid_splice_paths}});
}
####
sub _set_valid_splice_paths {
my $self = shift;
my @valid_splice_paths = @_;
## completely stomps the existing contents!!!
@{$self->{_valid_splice_paths}} = @valid_splice_paths;
return;
}
sub _graph_node_exists {
my $self = shift;
my ($type, $lend, $rend, $orient) = @_;
my $hashkey = $self->_get_hash_key($type, $lend, $rend, $orient);
return (exists $self->{_graph_node_hashkey_lookup}->{$hashkey});
}
sub _get_graph_node_via_coords_n_type {
my $self = shift;
my ($type, $lend, $rend, $orient) = @_;
my $hashkey = $self->_get_hash_key($type, $lend, $rend, $orient);
my $node = $self->{_graph_node_hashkey_lookup}->{$hashkey};
unless ($node) {
confess "Error, no graph node retrieved based on data ($type, $lend, $rend, $orient)";
}
return ($node);
}
####
sub _add_graph_node {
my $self = shift;
my ($type, $lend, $rend, $orient) = @_;
my $hash_key = $self->_get_hash_key($type, $lend, $rend, $orient);
## only add the node if it doesn't already exist!
my $graph_node_hashkey_lookup_href = $self->{_graph_node_hashkey_lookup};
if (my $existing_node = $graph_node_hashkey_lookup_href->{$hash_key}) {
## increment evidence for existing node:
$existing_node->increment_evidence_support();
return ($existing_node);
}
else {
# add it
my $node = Splice_graph_node->new($type, $lend, $rend, $orient);
push (@{$self->{_graph_nodes}}, $node); # add to complete node list
## helpers for node access:
$graph_node_hashkey_lookup_href->{$hash_key} = $node; # store in lookup table
my $nodeID = $node->get_nodeID();
$self->{_graph_node_via_nodeID}->{$nodeID} = $node;
## splay based on type:
my $type = $node->get_type();
if ($type eq "internal_exon") {
push (@{$self->{_internal_exons}}, $node);
}
elsif ($type eq "intron") {
push (@{$self->{_introns}}, $node);
}
elsif ($type eq "terminal_left_exon") {
push (@{$self->{_terminal_left_exons}}, $node);
}
elsif ($type eq "terminal_right_exon") {
push (@{$self->{_terminal_right_exons}}, $node);
}
elsif ($type eq "singleton_exon") {
push (@{$self->{_singleton_exons}}, $node);
}
else {
confess "Error, do not recognize type: $type for node";
}
return ($node);
}
}
####
sub _add_intron {
my $self = shift;
my ($intron_lend, $intron_rend, $spliced_orient) = @_;
my $node = $self->_add_graph_node("intron", $intron_lend, $intron_rend, $spliced_orient);
return ($node);
}
####
sub _add_internal_exon {
my $self = shift;
my ($exon_lend, $exon_rend, $spliced_orient) = @_;
my $node = $self->_add_graph_node("internal_exon", $exon_lend, $exon_rend, $spliced_orient);
return ($node);
}
####
sub get_graph_nodes {
my $self = shift;
return (sort {$a->{lend}<=>$b->{lend}} @{$self->{_graph_nodes}});
}
####
sub get_graph_node_via_nodeID {
my $self = shift;
my $nodeID = shift;
my $node_obj = $self->{_graph_node_via_nodeID}->{$nodeID} or confess "Error, no node found based on nodeID: $nodeID\n" . $self->toString();
return ($node_obj);
}
####
sub toString {
my $self = shift;
my @graph_nodes = $self->get_graph_nodes();
my $num_graph_nodes = scalar (@graph_nodes);
my $text = "Splice_graph_assembler instance with $num_graph_nodes graph nodes:\n";
foreach my $graph_node (@graph_nodes) {
$text .= $graph_node->toString() . "\n";
}
$text .= "\tvalid paths thru nodes:\n";
foreach my $splice_path ($self->_get_valid_splice_paths()) {
$text .= "\t" . $splice_path->toString() . "\n";
}
$text .= "\tassembled splice paths:\n";
foreach my $splice_path (@{$self->{_assembled_splice_paths}}) {
$text .= "\t" . $splice_path->toString() . "\n";
}
$text .= "\tFinal assemblies with termini\n";
foreach my $assembly ($self->get_assemblies()) {
my @node_list = @{$assembly->{__Splice_graph_assembler_nodeID_list}};
$text .= "\t" . join (",", @node_list) . "\n";
}
$text .= $self->toAlignIllustration(60);
return ($text);
}
=item toAlignIllustration()
=over 4
B<Description:> illustrates the individual cDNAs to be assembled along with the final products.
B<Parameters:> $max_line_chars(optional)
$max_line_chars is an integer representing the maximum number of characters in a single line of output to the terminal. The default is 100.
B<Returns:> $alignment_illustration_text
$alignment_illustration_text is a string containing a paragraph of text which illustrates the alignments and assemblies. An example is below:
---> <--> <-----> <---> <---------------- (+)gi|1199466
---> <--> <-----> <---> <------------ (+)gi|1209702
----> <--> <---- (+)AV827070
----> <--> <--- (+)AV828861
----> <--> <--- (+)AV830936
---> <--> <- (+)H36350
ASSEMBLIES: (1)
----> <--> <-----> <---> <---------------- (+) gi|1199466, gi|1209702, AV827070, AV828861, AV830936, H36350
=back
=cut
;
sub toAlignIllustration () {
my $self = shift;
my $max_line_chars = shift;
$max_line_chars = ($max_line_chars) ? $max_line_chars : 100; #if not specified, 100 chars / line is default.
## Get minimum coord for relative positioning.
my @coords;
my @alignments = @{$self->{_incoming_alignments}};
foreach my $alignment (@alignments) {
my @c = $alignment->get_coords();
push (@coords, @c);
}
@coords = sort {$a<=>$b} @coords;
print "coords: @coords\n" if $::SEE;
my $min_coord = shift @coords;
my $max_coord = pop @coords;
my $rel_max = $max_coord - $min_coord;
my $alignment_text = "";
## print each alignment followed by assemblies:
my $num_alignments = $#alignments + 1;
$alignment_text .= "Individual Alignments: ($num_alignments)\n";
my $i = 0;
foreach my $alignment (@alignments) {
$alignment_text .= (sprintf ("%3d ", $i)) . $alignment->toAlignIllustration($min_coord, $rel_max, $max_line_chars) . "\n";
$i++;
}
my @assemblies = @{$self->{_assemblies}};
my $num_assemblies = $#assemblies + 1;
$alignment_text .= "\n\nASSEMBLIES: ($num_assemblies)\n";
foreach my $assembly (@assemblies) {
$alignment_text .= " " . $assembly->toAlignIllustration($min_coord, $rel_max, $max_line_chars) . "\n";
}
if (my @unincorporated_alignments = $self->get_unincorporated_alignments()) {
my $num_unincorporated = scalar @unincorporated_alignments;
$alignment_text .= "\n\nUNINCORPORATED_ALIGNMENTS($num_unincorporated)\n";
foreach my $alignment (@unincorporated_alignments) {
$alignment_text .= " " . $alignment->toAlignIllustration($min_coord, $rel_max, $max_line_chars) . "\n";
}
}
return ($alignment_text);
}
####
sub _get_hash_key {
my $self = shift;
my ($type, $lend, $rend, $orient) = @_;
return ("$type,$lend,$rend,$orient");
}
####
sub _purge_nodes {
my $self = shift;
my (%nodeIDs) = @_;
## perform deletions based on hashkeys
foreach my $nodeID (keys %nodeIDs) {
print "PURGING node: $nodeID\n" if $SEE;
my $node = $self->{_graph_node_via_nodeID}->{$nodeID};
my $type = $node->get_type();
my ($lend, $rend) = $node->get_coords();
my $orient = $node->get_orient();
my $hashkey = $self->_get_hash_key($type, $lend, $rend, $orient);
delete $self->{_graph_node_hashkey_lookup}->{$hashkey};
delete $self->{_graph_node_via_nodeID}->{$nodeID};
}
## now, do array replacements:
foreach my $node_list_aref ( $self->{_graph_nodes},
## only deleting the terminal exons, even though this could be more generic
$self->{_terminal_left_exons},
$self->{_terminal_right_exons},
) {
my @replacments;
my $need_replacement_flag = 0;
foreach my $node (@$node_list_aref) {
my $nodeID = $node->get_nodeID();
if ($nodeIDs{$nodeID}) {
## must delete!
$need_replacement_flag = 1;
}
else {
push (@replacments, $node);
}
}
if ($need_replacement_flag) {
@$node_list_aref = @replacments; ## Doing Replacment
}
}
return;
}
####
sub _right_left_terminal_exons_overlap_via_position_lists {
my $self = shift;
my ($right_nodeID, $left_nodeID, $left_splice_coord, $right_splice_coord) = @_;
## looking for the following:
#
# ## termini of transcripts in right terminal
# <---------------------X---------------------- ## right terminal exon
# -------------------X---------------------------> ## left terminal exon
# ## termini of transcripts in left terminal
#
#
# The X termini show the endpoints of other transcripts incorporated that would define a proper merging situation.
# look for boundary pair such that both are included within the splice junctions
# and right boundary >= left boundary
my $right_pos_list_aref = $self->{_terminal_exon_nodeID_to_nonsplice_position_list}->{$right_nodeID};
my $left_pos_list_aref = $self->{_terminal_exon_nodeID_to_nonsplice_position_list}->{$left_nodeID};
foreach my $right_pos (@$right_pos_list_aref) {
unless ($right_pos > $left_splice_coord && $right_pos < $right_splice_coord) { next; }
foreach my $left_pos (@$left_pos_list_aref) {
unless ($left_pos > $left_splice_coord && $left_pos < $right_splice_coord) { next; }
if ($right_pos >= $left_pos) {
return (1); # found suitable case
}
}
}
return (0); # no such example found.
}
####
sub _analyze_intronless_alignments {
my $self = shift;
my @intronless_segments = @_; ## actually alignment objects.
print "method: _analyze_intronless_alignments()\n" if $SEE;
my %applied_segment_indices;
{
## hack in a hidden attribute that uniquely identifies each of these segments.
## perl allows this, but I'm not so happy with doing it. for now, it'll be fine.
## This __intronless_segment_index will be used to identify those segments that are
## applied to existing structures (internal/terminal exons), and those left over
## and still need to be accounted for.
my $id = 0;
foreach my $intronless_segment (@intronless_segments) {
$id++;
$intronless_segment->{__intronless_segment_ID} = $id;
}
}
## increment evidence for internal exons containing intronless segment
## and extend terminal exons overlapping intronless segments
## first, examine the internal_segments:
foreach my $internal_exon (@{$self->{_internal_exons}}) {
my $internal_exon_orient = $internal_exon->get_orient();
my ($internal_exon_lend, $internal_exon_rend) = $internal_exon->get_coords();
foreach my $intronless_segment (@intronless_segments) {
my $intronless_segment_orient = $intronless_segment->get_spliced_orientation();
my ($intronless_lend, $intronless_rend) = $intronless_segment->get_coords();
if ($intronless_segment_orient eq '?' || $intronless_segment_orient eq $internal_exon_orient) {
## look for encapsulation
if ( ($intronless_lend + $FUZZ_DIST) >= $internal_exon_lend &&
($intronless_rend - $FUZZ_DIST) <= $internal_exon_rend) {
$internal_exon->increment_evidence_support();
$applied_segment_indices{ $intronless_segment->{__intronless_segment_ID} } = 1; # incorporated already
print "-intronless segment $intronless_lend, $intronless_rend, $intronless_segment_orient found incorporated in internal exon: " . $internal_exon->toString() . "\n" if $SEE;
}
}
}
}
## examine left terminal exons
## first sort so we examine the right boundaries in order from right to left to faciliate extensions
@intronless_segments = reverse sort {$a->{rend}<=>$b->{rend}} @intronless_segments;
foreach my $left_terminal_exon (@{$self->{_terminal_left_exons}}) {
my $left_terminal_exon_orient = $left_terminal_exon->get_orient();
# -----------------> # left terminal exon
foreach my $intronless_segment (@intronless_segments) {
my ($left_terminal_lend, $left_terminal_rend) = $left_terminal_exon->get_coords(); # do it here because it might change below
my $intronless_segment_orient = $intronless_segment->get_spliced_orientation();
my ($intronless_lend, $intronless_rend) = $intronless_segment->get_coords();
unless ($intronless_segment_orient eq '?' || $intronless_segment_orient eq $left_terminal_exon_orient) { next; }
## must overlap
unless ($left_terminal_lend <= $intronless_rend && $left_terminal_rend >= $intronless_lend) { next; }
unless ($intronless_rend - $FUZZ_DIST <= $left_terminal_rend) { next; }
## must be :
# -----------------------> # left terminal exon
# ---------------- # intronless segment, overlaps but not passed the splice boundary
# extend lend if intronless segment passes it
if ($intronless_lend < $left_terminal_lend) {
# update coords:
$left_terminal_exon->set_coords($intronless_lend, $left_terminal_rend);
}
## if got this far, evidence is incorporated.
$left_terminal_exon->increment_evidence_support();
$applied_segment_indices{ $intronless_segment->{__intronless_segment_ID} } = 1; # incorporated
print "-intronless segment $intronless_lend, $intronless_rend, $intronless_segment_orient incorporated in left terminal exon: " . $left_terminal_exon->toString() . "\n" if $SEE;
}
}
## examine right terminal exons
## first sort so we examine the left boundaries in order from left to right to faciliate extensions
@intronless_segments = sort {$a->{lend}<=>$b->{lend}} @intronless_segments;