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
|
#!/usr/local/bin/perl
package main;
our $SEE;
package CDNA::Genome_based_cDNA_graph_assembler;
=head1 NAME
CDNA::Genome_based_cdna_assembler
=cut
=head1 DESCRIPTION
This module is used to assemble compatible cDNA alignments. The algorithm is as follows:
must describe this here.
=cut
use strict;
use CDNA::CDNA_alignment;
use Data::Dumper;
use base ("CDNA::Genome_based_cDNA_assembler");
=item new()
=over 4
B<Description:> instantiates a new cDNA assembler obj.
B<Parameters:> $sequence_sref
$sequence_sref is a reference to a scalar containing the genomic sequence string.
B<Returns:> $obj_href
$obj_href is the object reference newly instantiated by this new method.
=back
=cut
sub new () {
my $package_name = shift;
my $self = {};
bless ($self, $package_name);
$self->_init(@_);
return ($self);
}
sub _init {
my $self = shift;
$self->CDNA::Genome_based_cDNA_assembler::_init(@_);
$self->{show_matrix} = 0;
$self->{compatibilities} = []; #retains results of compatibility comparisons:
$self->{incapsulations} = [];
$self->{LobjsForient} = []; #non-fli single-segment alignments forced in forward orientation.
$self->{LobjsRorient} = []; #non-fli single segment alignments forced in reverse orientation.
$self->{indices_included} = []; #remember what indices are built into assemblies.
}
=item assemble_alignments()
=over 4
B<DESCRIPTION:> assembles a series of cDNA aligmnments into one or more cDNA assemblies using a directed acyclic graph.
B<Parameters:> @alignments
@alignments is an array of CDNA::CDNA_alignment objects
B<Returns:> none.
=back
=cut
sub assemble_alignments {
my $self = shift;
my @alignments = @_;
@alignments = sort {$a->{lend}<=>$b->{lend}} @alignments; #keep in order of lend across genomic sequence to provide a layout.
$self->{incoming_alignments} = [@alignments];
my $num_alignments = $#alignments + 1;
## Initialize globals.
$self->{incapsulations} = []; #initialize.
$self->{compatibilities} = {}; #initialize.
$self->{indices_included} = []; #initialize
$self->{assemblies} = []; #initialize
#precompute compatible alignment pairs and full encapsulations.
$self->determine_compatibilities_and_encapsulations();
######################################
# Do Forward Scan Dynamic Programming.
my $LobjsForient = $self->{LobjsForient} = [];
my $LobjsRorient = $self->{LobjsRorient} = [];
$self->force_flexorient('+');
$self->do_full_Fscan($LobjsForient);
$self->force_flexorient('-');
$self->do_full_Fscan($LobjsRorient);
&Describe_containment($LobjsForient, $LobjsRorient) if $SEE;
## Get the highest scoring Lobj
my $struct = $self->get_top_alignment_indices();
## Create the highest scoring assembly.
my $assembly = $self->create_assembly($struct);
my $num_cdnas_included = $struct->{num_cdnas};
if ( $num_cdnas_included == $num_alignments) { return();}
#######################################
## Do Reverse Scan Dynamic Programming (if cDNAs exist and not included in assembly above).
$self->force_flexorient('+');
$self->do_full_Rscan($LobjsForient);
$self->force_flexorient('-');
$self->do_full_Rscan($LobjsRorient);
## Create assemblies for each cDNA not included yet.
my @asmbl_sets;
my %seen;
for (my $i = 0; $i < $num_alignments; $i++) {
unless ($self->{indices_included}->[$i]) {
print "Not Included: $i, Doing Back trace starting at $i.\n" if $SEE;
my $struct = $self->get_top_alignment_indices_starting_index($i);
push (@asmbl_sets, $struct);
}
}
@asmbl_sets = reverse sort {$a->{num_cdnas}<=>$b->{num_cdnas}} @asmbl_sets;
my $all_included_flag = 0;
while ((!$all_included_flag) && @asmbl_sets) {
my $asmbl_set = shift @asmbl_sets;
$all_included_flag = $self->test_and_add_asmbl($asmbl_set);
}
unless ($all_included_flag) {
die "FATAL: All assemblies not included!!\n";
}
}
# private.
sub do_full_Fscan {
my $self = shift;
my $Lobjs = shift;
print "\n# Doing full Fscan\n" if $SEE;
my $alignments_aref = $self->{incoming_alignments};
my $num_alignments = $#{$alignments_aref} + 1;
for (my $i = 0; $i < $num_alignments; $i++) {
my $Lobj = Lobject->new($i, $self);
if ($i != 0) { #first cDNA, base case.
## Must compare to previous alignments:
my $top_score = 0;
my $top_scoring_index = -1;
my $i_alignment = $alignments_aref->[$i];
for (my $j = $i - 1; $j >= 0; $j--) {
my $j_alignment = $alignments_aref->[$j];
my $prevLobj = $Lobjs->[$j];
print "Comparing $i to $j\n" if $SEE;
my $compatibility = $self->{compatibilities}->{$i}->{$j};
my $j_contains_i = $prevLobj->{contained_cdna_indices}->{$i};
my $i_contains_j = $Lobj->{contained_cdna_indices}->{$j};
print "\tcompat: $compatibility\tj_contains_i: $j_contains_i\n" if $SEE;
if ($compatibility && (!($j_contains_i||$i_contains_j))) {
unless (&runtime_compatible($i_alignment, $j_alignment)) {next;}
my $curr_Lscore = $Lobjs->[$j]->{LscoreF};
my $Cscore = &get_Cscore($Lobj, $prevLobj);
my $curr_total_score = $curr_Lscore + $Cscore;
print "\tFSCAN score: ($i,$j) = $curr_total_score\n" if $SEE;
if ($curr_total_score > $top_score) {
$top_scoring_index = $j;
$top_score = $curr_total_score;
print "\t*making topscore.\n" if $SEE;
}
}
}
if ($top_scoring_index > -1) {
my $topLobj = $Lobjs->[$top_scoring_index];
$Lobj->{fromLptr} = $topLobj;
$Lobj->{LscoreF} = $top_score;
}
}
$Lobjs->[$i] = $Lobj;
}
}
# private.
sub do_full_Rscan {
my $self = shift;
my $Lobjs = shift;
print "\n# Doing full Rscan\n" if $SEE;
my $alignments_aref = $self->{incoming_alignments};
my $num_alignments = $#{$alignments_aref} + 1;
for (my $i = $num_alignments - 2; $i >= 0; $i--) {
my $Lobj = $Lobjs->[$i];
my $i_alignment = $alignments_aref->[$i];
## Must compare to previous alignments:
my $top_score = 0;
my $top_scoring_index = -1;
for (my $j = $i + 1; $j < $num_alignments; $j++) {
my $j_alignment = $alignments_aref->[$j];
my $nextLobj = $Lobjs->[$j];
print "Comparing $i to $j\n" if $SEE;
my $compatibility = $self->{compatibilities}->{$i}->{$j};
my $j_contains_i = $nextLobj->{contained_cdna_indices}->{$i};
my $i_contains_j = $Lobj->{contained_cdna_indices}->{$j};
print "\tcompat: $compatibility\tj_contains_i: $j_contains_i\n" if $SEE;
if ($compatibility && (!($j_contains_i||$i_contains_j))) {
unless (&runtime_compatible($i_alignment, $j_alignment)) { next;}
my $curr_Lscore = $Lobjs->[$j]->{LscoreR};
my $Cscore = &get_Cscore($Lobj, $nextLobj);
my $curr_total_score = $curr_Lscore + $Cscore;
print "\tRSCAN score: ($i,$j) = $curr_total_score\n" if $SEE;
if ($curr_total_score > $top_score) {
$top_scoring_index = $j;
$top_score = $curr_total_score;
print "\t*making topscore.\n" if $SEE;
}
}
}
if ($top_scoring_index > -1) {
my $topLobj = $Lobjs->[$top_scoring_index];
$Lobj->{toLptr} = $topLobj;
$Lobj->{LscoreR} = $top_score;
}
}
}
# private.
sub get_Cscore {
## Cscore is the number of cdnas contained in a but not in b including a itself
my ($a, $b) = @_;
my $score = 0;
my $a_container_ref = $a->{contained_cdna_indices};
my $b_container_ref = $b->{contained_cdna_indices};
foreach my $a_index (keys %$a_container_ref) {
unless ($b_container_ref->{$a_index}) {
$score++;
}
}
return ($score);
}
=over 4
=item encapsulates()
B<Description:> Returns true (1) if alignment A encapsulates the span of alignment B
B<Parameters:> ($alignment_A, $alignment_B)
Alignments A and B are of type CDNA::CDNA_alignment
B<Returns:> [1|0]
=back
=cut
sub encapsulates {
my $self = shift;
my ($alignmentA, $alignmentB) = @_;
my ($alend, $arend) = $alignmentA->get_coords();
my ($blend, $brend) = $alignmentB->get_coords();
if ($blend >= $alend && $brend <= $arend) {
return (1);
} else {
return (0);
}
}
sub determine_compatibilities_and_encapsulations {
my $self = shift;
my $alignments_aref = $self->{incoming_alignments};
my $num_alignments = $#{$alignments_aref} + 1;
#initialize incapsulations list
for (my $i = 0; $i < $num_alignments; $i++) {
$self->{incapsulations}->[$i] = [];
}
#compare each downstream alignment to the upstream alignment for compatiblity and encapsulation:
for (my $i = 0; $i < $num_alignments; $i++) {
for (my $j = $i + 1; $j < $num_alignments; $j++) {
my $alignment_i = $alignments_aref->[$i];
my $alignment_j = $alignments_aref->[$j];
my $can_merge = 0;
if ($self->can_merge($alignment_i, $alignment_j)) {
$can_merge = 1;
# set compatibility flags.
$self->{compatibilities}->{$j}->{$i} = 1;
$self->{compatibilities}->{$i}->{$j} = 1;
}
my $align_i_single_seg = ($alignment_i->get_num_segments() == 1) ? 1:0;
my $align_j_single_seg = ($alignment_j->get_num_segments() == 1) ? 1:0;
## Analyze compatible alignments for containment. Single segment alignments may conflict if in opposite orientations and fli-status, so should check these incompatible alignments for containment as well to avoid multiple identical assemblies from being generated.
if ($can_merge || ($align_i_single_seg && $align_j_single_seg)) {
## Check for encapsulation:
if ($self->encapsulates($alignment_j, $alignment_i)) {
push (@{$self->{incapsulations}->[$j]}, $i);
print "$j contains $i\n" if $SEE;
}
if ($self->encapsulates($alignment_i, $alignment_j)) {
push (@{$self->{incapsulations}->[$i]}, $j);
print "$i contains $j\n" if $SEE;
}
}
}
}
}
sub force_flexorient {
my $self = shift;
my $orient = shift;
my $alignments_aref = $self->{incoming_alignments};
my $num_alignments = $#{$alignments_aref} + 1;
## Fix orientations of fli and multi-segment alignments:
for (my $i = 0; $i < $num_alignments; $i++) {
my $alignment = $alignments_aref->[$i];
my $num_segments = $alignment->get_num_segments();
my $spliced_orientation = $alignment->get_spliced_orientation();
if ($spliced_orientation =~ /^[+-]$/) { # set specifically
$alignment->{fixed_orient} = $spliced_orientation; ## adding tag, using only in this module.
} else {
print "Setting $i to flex orient: $orient.\n" if $SEE;
$alignment->{fixed_orient} = $orient;
}
}
}
sub back_trace {
my $self = shift;
my $top_scoring_index = shift;
my $Lobjs = shift;
print "Back_trace: " if $SEE;
my $Lobj = $Lobjs->[$top_scoring_index];
## Traverse the fromLptrs
my %unique_indices;
my @trace_indices;
while ($Lobj != 0) {
my $index = $Lobj->{myIndex};
print "$index " if $SEE;
push (@trace_indices, $index);
# get composed accs:
foreach my $contained_index ($Lobj->get_contained_indices()) {
$unique_indices{$contained_index} = 1;
}
$Lobj = $Lobj->{fromLptr};
}
print "\n" if $SEE;
my @unique = keys %unique_indices;
my $num_cdnas_included = $#unique + 1;
my $struct = {trace_indices=>\@trace_indices,
num_cdnas=>$num_cdnas_included,
all_indices=>\@unique};
return ($struct);
}
sub forward_trace {
my $self = shift;
my $index = shift;
my $Lobjs = shift;
print "Forward_trace: " if $SEE;
my $Lobj = $Lobjs->[$index];
## Traverse the fromLptrs
my @trace_indices;
my %unique_indices;
while ($Lobj != 0) {
my $index = $Lobj->{myIndex};
print " $index" if $SEE;
push (@trace_indices, $index);
# get composed accs:
foreach my $contained_index ($Lobj->get_contained_indices()) {
$unique_indices{$contained_index} = 1;
}
$Lobj = $Lobj->{toLptr};
}
print "\n" if $SEE;
my @unique = keys %unique_indices;
my $num_cdnas_included = $#unique + 1;
my $struct = {trace_indices=>\@trace_indices,
num_cdnas=>$num_cdnas_included,
all_indices=>\@unique};
return ($struct);
}
sub create_assembly {
my $self = shift;
my $struct = shift;
my $indices_aref = $struct->{trace_indices};
my $alignments_aref = $self->{incoming_alignments};
my @indices = sort {$a<=>$b} @$indices_aref;
my $all_indices_ref = $struct->{all_indices};
my @all_indices = sort {$a<=>$b} @$all_indices_ref;
print "Creating assembly: @indices\n" if $SEE;
my $first_index = shift @indices;
my $alignment = $alignments_aref->[$first_index];
$self->{indices_included}->[$first_index] = 1;
print "Index: $first_index\t" . $alignment->toToken() . "\n" if $SEE;
my $assembly = $alignment->clone();
my @alignments_to_assemble;
# first index already included in assembly; ...now, include the others.
foreach my $index (@indices) {
my $alignment = $alignments_aref->[$index];
print "Index: $index\t" . $alignment->toToken() . "\n" if $SEE;
$assembly = $self->merge_alignments($assembly, $alignment);
}
print "assembly contains: @all_indices\n" if $SEE;
my @all_accs;
foreach my $index (@all_indices) {
$self->{indices_included}->[$index] = 1;
my $acc = $alignments_aref->[$index]->get_acc();
push (@all_accs, $acc);
}
my $acc = $self->merge_accs(@all_accs);
$assembly->set_acc($acc);
push (@{$self->{assemblies}}, $assembly);;
return ($assembly);
}
sub runtime_compatible {
my ($a, $b) = @_;
my $a_fixed_orient = $a->{fixed_orient};
my $b_fixed_orient = $b->{fixed_orient};
print "a($a_fixed_orient) vs. b($b_fixed_orient)\n" if $SEE;
if ($a_fixed_orient eq $b_fixed_orient) {
print "runtime compatible.\n" if $SEE;
return (1);
} else {
print "NOT runtime compatible\n" if $SEE;
return (0);
}
}
sub get_top_alignment_indices {
my $self = shift;
my $LobjsForient = $self->{LobjsForient};
my $LobjsRorient = $self->{LobjsRorient};
## Process Forient (singletons forced Forward orient)
my $top_Forient_index = $self->top_scoring_index_from_Lobjs($LobjsForient, 'LscoreF');
my $ForientStruct = $self->back_trace($top_Forient_index, $LobjsForient);
## Process Rorient (singletons forced Reverse orient)
my $top_Rorient_index = $self->top_scoring_index_from_Lobjs($LobjsRorient, 'LscoreF');
my $RorientStruct = $self->back_trace($top_Rorient_index, $LobjsRorient);
if ($ForientStruct->{num_cdnas} >= $RorientStruct->{num_cdnas}) {
return ($ForientStruct);
} else {
return ($RorientStruct);
}
}
sub get_top_alignment_indices_starting_index {
my $self = shift;
my $index = shift;
my $LobjsForient = $self->{LobjsForient};
my $LobjsRorient = $self->{LobjsRorient};
my $LobjsForient_num_contained = $LobjsForient->[$index]->get_contained_indices();
my $LobjsRorient_num_contained = $LobjsRorient->[$index]->get_contained_indices();
## Process Forient (singletons forced forward orient)
my $ForientBtraceStruct = $self->back_trace($index, $LobjsForient);
my $ForientFtraceStruct = $self->forward_trace($index, $LobjsForient);
my $ForientNumCdnas = $ForientBtraceStruct->{num_cdnas} + $ForientFtraceStruct->{num_cdnas} -$LobjsForient_num_contained;
## Process Rorient (singletons forced reverse orient)
my $RorientBtraceStruct = $self->back_trace($index, $LobjsRorient);
my $RorientFtraceStruct = $self->forward_trace($index, $LobjsRorient);
my $RorientNumCdnas = $RorientBtraceStruct->{num_cdnas} + $RorientFtraceStruct->{num_cdnas} - $LobjsRorient_num_contained;
my ($Ftrace, $Btrace,$total_cdnas);
if ($ForientNumCdnas > $RorientNumCdnas) {
($Ftrace, $Btrace) = ($ForientFtraceStruct, $ForientBtraceStruct);
$total_cdnas = $ForientNumCdnas;
} else {
($Ftrace, $Btrace) = ($RorientFtraceStruct, $RorientBtraceStruct);
$total_cdnas = $RorientNumCdnas;
}
## Create new struct
my @unique_trace = &unique_entries(@{$Ftrace->{trace_indices}}, @{$Btrace->{trace_indices}});
my @all_indices = &unique_entries (@{$Ftrace->{all_indices}}, @{$Btrace->{all_indices}});
if ($#all_indices +1 != $total_cdnas) {
die "Error: total number of alignments not calculated correctly.\n";
}
print "FnB trace from index[$index] yields assembly containing indices [@all_indices]\n" if $SEE;
my $struct = {trace_indices=>\@unique_trace,
num_cdnas=>$total_cdnas,
all_indices=>\@all_indices};
return ($struct);
}
sub unique_entries {
my @x = @_;
my %z;
foreach my $y (@x) {
$z{$y}=1;
}
return (keys %z);
}
sub top_scoring_index_from_Lobjs {
my $self = shift;
my $Lobjs = shift;
my $score_type = shift;
my $alignments_aref = $self->{incoming_alignments};
my $num_alignments = $#{$alignments_aref} + 1;
my $top_scoring_index = -1;
my $top_score = -1;
for (my $i = 0; $i < $num_alignments; $i++) {
my $Lscore = $Lobjs->[$i]->{$score_type};
if ($Lscore > $top_score) {
$top_scoring_index = $i;
$top_score = $Lscore;
}
}
return ($top_scoring_index);
}
sub test_and_add_asmbl {
my $self = shift;
my $asmbl_set = shift;
my $all_indices_ref = $asmbl_set->{all_indices};
my $indices_included_ref = $self->{indices_included};
## If the asmbl_set contains a cDNA missing from an assembly inclusion, then add it.
my $num_alignments = $#{$self->{incoming_alignments}} + 1;
my $contains_unincorporated = 0;
foreach my $index (@$all_indices_ref) {
if (! $indices_included_ref->[$index]) {
$contains_unincorporated = 1;
last;
}
}
if ($contains_unincorporated) {
$self->create_assembly($asmbl_set);
}
## Test to see if all cDNAs are accounted for in assemblies.
my $all_included_flag = 1;
for (my $i =0; $i < $num_alignments; $i++) {
if (! $indices_included_ref->[$i]) {
print "$i not included!\n" if $SEE;
$all_included_flag = 0;
}
}
return ($all_included_flag);
}
sub Describe_containment {
my ($LobjsForient, $LobjsRorient) = @_;
for (my $i = 0; $i <= $#$LobjsForient; $i++) {
print "Fixed Forient: index $i contains: " . join (" ", $LobjsForient->[$i]->get_contained_indices()) . "\n";
print "Fixed Rorient: index $i contains: " . join (" ", $LobjsRorient->[$i]->get_contained_indices()) . "\n";
}
print "Lscores:\n";
for (my $i = 0; $i <= $#$LobjsForient; $i++) {
print "$i: F-forced singleton orient Lscores: F: " . $LobjsForient->[$i]->{LscoreF} . " R: " . $LobjsForient->[$i]->{LscoreR} . "\n";
print "$i: R-forced singleton orient Lscores: F: " . $LobjsRorient->[$i]->{LscoreF} . " R: " . $LobjsRorient->[$i]->{LscoreR} . "\n";
}
}
#######################
package Lobject;
sub new {
my $packagename = shift;
my $index = shift;
my $assembler_ref = shift;
my $self = {
contained_cdna_indices =>{ $index => 1 #include as containing itself.
},
myIndex => $index, #remember Lobj position.
LscoreF => 1, #Forward scan Lscore.
LscoreR => 1, #Reverse scan Lscore.
toLPtr => 0, # used in Rscan for forward tracking.
fromLPtr => 0 # used in Fscan for backtracking
};
## populate list of contained entries.
my @contained_indices = @{$assembler_ref->{incapsulations}->[$index]};
foreach my $contained_index (@contained_indices) {
$self->{contained_cdna_indices}->{$contained_index} = 1;
$self->{LscoreF}++;
$self->{LscoreR}++;
}
bless ($self, $packagename);
return ($self);
}
sub get_contained_indices {
my $self = shift;
return (keys %{$self->{contained_cdna_indices}});
}
1; #EOM
|