-
Notifications
You must be signed in to change notification settings - Fork 1
/
BaseAncestralAlleles.pm
1425 lines (1132 loc) · 53.8 KB
/
BaseAncestralAlleles.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
package BaseAncestralAlleles;
use strict;
use warnings;
=pod
=head1 MODULE
BaseAncestralAlleles
=head1 DESCRIPTION
This module contain the internal methods used for polarising indels in a genome. It is meant to be used as a BASE class for
the RunAncestralAlleles* modules. These will handle the input and output layer.
=head1 Exported methods
This module exports by default the following methods:
=over
=item straighten_sorted_alignment
=item get_name_from_data
=item get_sub_sorted_alignment
=item splice_uninformative_sequences_from_alignment
=item get_reference_sequence_exception
=item run_ortheus
=item call_ancestral_allele_for_deletion
=item call_ancestral_allele_for_insertion
=back
=head1 INTERNAL DATA STRUCTURES
=head2 SORTED_ALIGNMENT
This is used for the original and sub-EMF alignments. It is a hash with 3 keys:
=over
=item * B<tree>: a string representation of the sequence tree, with no names for the
ancestral sequences
=item * B<positions>: defines the order of the sequences in the tree (required for running
Ortheus)
=item * B<sequences>: a sorted array (see positions) of hashes whose keys are:
=over
=item * B<species>: the species name
=item * B<chr>: the chromosome (or similar) name
=item * B<start>: the start position in that chromosome (e! coordinates)
=item * B<end>: the end position in that chromosomes (e! coordinates)
=item * B<strand>: the strand of the sequence (1 or -1)
=item * B<name>: a label representation of the above, as found in the tree lines of the
EMF files (i.e. Hsap_1_12345_22345[+])
=item * B<aligned_sequence>: self-explanatory. Might not exist if the sequence is to be
aligned.
=item * B<original_sequence>: self-explanatory. Might not exist if the sequence is already
aligned.
=back
=back
=head2 ORTHEUS_ALIGNMENT
This is used for capturing the Ortheus alignments. It is a hash, where each entry is one
of the sequences from Ortheus. The key is the name of the sequence "a la Ortheus"
(concatenation of the sequence numbers with an "_"), but with sorted leave names.
Additionally, the parser also calls "ref", "sis", "anc" and "old" as aliases for the
corresponding sequences. The values are a hash whose keys are:
=over
=item * B<leaves>: a hash with keys being the leaf names and values being simply 1.
Useful to match leaf content.
=item * B<num_leaves>: the number of leaves under that tree. 1 for leaves, more for
ancestral nodes.
=item * B<aligned_sequence>: self-explanatory
=item * B<name>: the same label as for the SORTED_ALIGNMENT for leaves. Concatenation
of labels these otherwise.
=back
B<Note>: after the calling of alleles, the ortheus_alignment contain a new key-value pair
with the lengths of the flank5, allele and flank3
=head2 TREES
The trees are represented as a nested set of tree nodes. The keys are:
=over
=item * B<children>: an arrayref to a set of other tree nodes
=item * B<distance_to_parent>: a number representing the distance to the parent node in
the tree
=item * B<label>: a string representing the name of this node (only for leaves)
=item * B<mark>: additional flag used when trimming the trees
=back
=head1 INTERNAL METHODS
The rest of the documentation refers to the internal methods implemented in this module.
=cut
use Exporter 'import';
our @EXPORT = qw(
straighten_sorted_alignment
get_name_from_data
call_ancestral_allele_from_ortheus_alignments
get_alleles_from_ortheus_alignment
print_ortheus_alignment
print_sorted_alignment
read_sorted_alignment_from_emf_fh
fix_alignment_tree
get_sub_sorted_alignment
splice_uninformative_sequences_from_alignment
get_reference_sequence_exception
run_ortheus
call_ancestral_allele_for_deletion
call_ancestral_allele_for_insertion
);
our %event_type = (
'deletion' => 1,
'complex_deletion' => 2,
'funny_deletion' => 3,
'insertion' => 4,
'complex_insertion' => 5,
'funny_insertion' => 6,
'unsure' => 7);
=head2 get_name_from_data
Arg[1]: string $species
Arg[2]: string $chromosome
Arg[3]: int $start
Arg[4]: int $end
Arg[5]: int $strand (1 or -1)
Returns: string $name
Description: Helper method to build a name string that describe the location of this
locus.
=cut
sub get_name_from_data {
my ($species, $chromosome, $start, $end, $strand) = @_;
my $name = $species;
$name =~ s/(.).+_([^_]{3})[^_]*/\u$1$2/;
$name = join("_", $name, $chromosome, $start, $end)."[".($strand==-1?"-":"+")."]";
return $name;
}
=head2 get_reference_sequence_exception
Arg[1]: string $flank5
Arg[2]: string $flank3
Arg[3]: int $alignment_length
Arg[4]: int $max_alignment_length
Arg[5]: string $work_dir
Arg[6]: bool $verbose
Returns: string $EXCEPTION or undef
Description: This method runs several check on the sequence and original alignment.
This is intended to be run before attempting to align the reference or
indel alleles. The checks this runs are:
- the aligned sequence is longer than $max_alignment_length
(exception: LONG_INSERTION)
- either $flank5 or $flank3 are all N's (exception: ALL_N)
- either $flank5 or $flank3 contains just one different nucleotide
(exception: LOW_COMPLEXITY (HR))
- the complexity of the reference sequence is too low (see check_
complexity_of_sequence(), exception: LOW_COMPLEXITY (muscle...))
=cut
sub get_reference_sequence_exception {
my ($flank5, $flank3, $alignment_length, $max_alignment_length, $muscle_exe, $work_dir, $verbose) = @_;
## EXCEPTION: the alignment is too long
if ($alignment_length > $max_alignment_length) {
return "LONG_INSERTION";
}
## EXCEPTION: Either flank is Ns only
if ($flank5 =~ /^[Nn\-]+$/ or $flank3 =~ /^[Nn\-]+$/) {
return "ALL_N";
}
# return undef;
## EXCEPTION: Low-complexity sequence. Skip if either flank contains
## only 1 different nucleotide (not counting Ns)
if ($flank5 =~ /[Aa]/ + $flank5 =~ /[Cc]/ + $flank5 =~ /[Gg]/ + $flank5 =~ /[Tt]/ < 2) {
return "LOW_COMPLEXITY (HR)";
}
if ($flank3 =~ /[Aa]/ + $flank3 =~ /[Cc]/ + $flank3 =~ /[Gg]/ + $flank3 =~ /[Tt]/ < 2) {
return "LOW_COMPLEXITY (HR)";
}
# my $exception = check_complexity_of_sequence($flank5.$flank3, $work_dir);
# return $exception if ($exception);
my $exception;
$exception = check_complexity_of_sequence($flank5, $muscle_exe, $work_dir);
return $exception if ($exception);
$exception = check_complexity_of_sequence($flank3, $muscle_exe, $work_dir);
return $exception if ($exception);
return undef;
}
=head2 check_complexity_of_sequence
Arg[1]: string $locus_sequence
Arg[2]: string $work_dir
Returns: string "LOW_COMPLEXITY..." or undef
Description: This method checks the complexity of this sequence by aligning it to
itself after offsetting the sequence by 2, 3 or 4 nucleotides. This is an
pragmatic way to look for STR that would affect the quality of the ortheus
alignment if present. If order to increase the specificity, the $locus_
sequence is truncated by 2 nucleotides each end before checking running
the alignments.
The alignments are run using muscle. When the resulting alignment produces
a number of matches that is larger than the length of the aligned sequences
minus 3, this sequence is considered to have a low complexity.
This method is called by get_reference_sequence_exception().
=cut
sub check_complexity_of_sequence {
my ($locus_sequence, $muscle_exe, $work_dir) = @_;
## TODO: muscle path is hard-coded here
$locus_sequence =~ s/\-//g;
$locus_sequence = substr($locus_sequence, 2, -2);
my $muscle_in = "$work_dir/locus.$$.fa";
for (my $offset = 2; $offset <= 6; $offset++) {
open(MUSCLE, ">$muscle_in") or die();
print MUSCLE ">locus1.$offset\n", substr($locus_sequence, $offset), "\n";
print MUSCLE ">locus2.$offset\n", substr($locus_sequence, 0, -$offset), "\n";
close(MUSCLE);
my $muscle_cmd = "$muscle_exe -maxiters 10 -diags -in $muscle_in -quiet";
my @muscle_lines = qx"$muscle_cmd";
if ($muscle_lines[1] ne ("-"x$offset).substr($locus_sequence, $offset)."\n" or
$muscle_lines[3] ne substr($locus_sequence, 0, -$offset).("-"x$offset)."\n") {
my $num_matches = 0;
for (my $pos = 0; $pos < length($muscle_lines[1])-1; $pos++) {
$num_matches++ if (substr($muscle_lines[1], $pos, 1) eq substr($muscle_lines[3], $pos, 1));
}
if ($num_matches > length($locus_sequence) - $offset - 3) {
# if ($num_matches > length($locus_sequence) - $offset - 5) {
# print "\n", @muscle_lines,"matches:$num_matches\n\n";
unlink($muscle_in);
return "LOW_COMPLEXITY (muscle $offset $num_matches ". (length($locus_sequence) - $offset - 3). " $locus_sequence)";
}
}
}
unlink($muscle_in);
return undef;
}
=head2 run_ortheus
Arg[1]: hash $sorted_alignment
Arg[2]: string $ortheus_exe
Arg[3]: string $muscle_exe (optional)
Arg[4]: srting $work_dir
Arg[5]: bool $verbose
Returns: hash $ortheus_alignment
Description: This method runs ortheus on the $sorted_alignment to produce a new $orteus_
alignment which includes predicted ancestral sequences. Please refer to
C<INTERNAL DATA STRUCTURES> elsewhere in this document.
You must provide the path to the Ortheus executable (or simply "ortheus"
available on the default PATH) and the $work_dir. The path for muscle is
optional. If provided, the method will build an initial alignment with
muscle to guide ortheus.
=cut
sub run_ortheus {
my ($sorted_alignment, $ortheus_exe, $muscle_exe, $work_dir, $verbose) = @_;
my $ortheus_alignment;
my $tree = $sorted_alignment->{tree};
my $fasta_filenames = [];
my $muscle_in = "$work_dir/muscle.in.$$.fa";
my $muscle_out = "$work_dir/muscle.out.$$.fa";
my $muscle_order;
if ($muscle_exe) {
open(MUSCLE, ">$muscle_in") or die;
}
for (my $i = 0; $i < @{$sorted_alignment->{positions}}; $i++) {
my $this_seq = $sorted_alignment->{sequences}->[$sorted_alignment->{positions}->[$i]];
my $file_name = "$work_dir/ortheus.$$.".$sorted_alignment->{positions}->[$i].".fa";
open(FASTA, ">$file_name") or die "Cannot open $file_name";
print FASTA ">", $this_seq->{name}, "\n", $this_seq->{original_sequence}, "\n";
if ($muscle_exe) {
print MUSCLE ">", $this_seq->{name}, "\n", $this_seq->{original_sequence}, "\n";
$muscle_order->{$this_seq->{name}} = $i;
}
close(FASTA);
push(@$fasta_filenames, $file_name);
}
if ($muscle_exe) {
close(MUSCLE);
my $muscle_cmd = "$muscle_exe -maxiters 3 -diags -in $muscle_in -out $muscle_out &> /dev/null";
system($muscle_cmd);
my @muscle_lines;
open(MUSCLE, $muscle_out);
my $order = -1;
while (<MUSCLE>) {
chomp;
if (/>(.+)/) {
$order = $muscle_order->{$1};
$muscle_lines[$order*2] = $_;
$muscle_lines[$order*2+1] = "";
} else {
$muscle_lines[$order*2+1] .= $_;
}
}
close(MUSCLE);
open(MUSCLE, ">".$muscle_out);
print MUSCLE join("\n", @muscle_lines), "\n";
close(MUSCLE);
}
my $output_alignment_file = "$work_dir/ortheus.$$.output.mfa";
my $output_score_file = "$work_dir/ortheus.$$.score.txt";
my $ortheus_cmd = "$ortheus_exe -b '$tree' -a ".join(" ", @$fasta_filenames)." -i 100 -j 0 -d $output_alignment_file -x $output_score_file";
my @ortheus_args = ("-b", $tree, "-a", @$fasta_filenames, "-d", $output_alignment_file, "-x", $output_score_file);
if ($muscle_exe) {
push(@ortheus_args, "-c", $muscle_out);
$ortheus_cmd .= " -c $muscle_out";
}
system($ortheus_exe, @ortheus_args) == 0 or die "system call to ortheus failed (".join(" ", @ortheus_args).": $?";
# qx"$ortheus_cmd";
open(MFA, $output_alignment_file) or die "ORTHEUS: $ortheus_cmd\nCannot open <$output_alignment_file>";
my $sort_code;
while(<MFA>) {
if (/^>(\S+)/) {
my $seq_name = $1;
my @leaves = split("_", $seq_name);
$sort_code = join("_", sort {$a <=> $b} @leaves);
foreach my $leaf (@leaves) {
$ortheus_alignment->{$sort_code}->{leaves}->{$leaf} = 1;
}
$ortheus_alignment->{$sort_code}->{num_leaves} = @leaves;
if (@leaves == 1) {
$ortheus_alignment->{$sort_code}->{name} = $sorted_alignment->{sequences}->[$sort_code]->{name};
}
} elsif (defined($sort_code)) {
chomp($_);
$ortheus_alignment->{$sort_code}->{aligned_sequence} .= $_;
}
}
close(MFA);
## Find the ref, anc, old and sis sequences in the ortheus tree
my ($ref_seq_name, $anc_seq_name, $old_seq_name, $sis_seq_name);
# Sort the nodes by number of leaves to get the reference, then the ancestral and finally the older sequence
foreach my $seq_name (sort {$ortheus_alignment->{$a}->{num_leaves} <=> $ortheus_alignment->{$b}->{num_leaves}} keys %$ortheus_alignment) {
# Skip if it doesn't include the reference sequence
next if (!$ortheus_alignment->{$seq_name}->{leaves}->{0});
if (!defined($ref_seq_name)) {
$ref_seq_name = $seq_name;
} elsif (!defined($anc_seq_name)) {
$anc_seq_name = $seq_name;
# The code name for the sister sequence must be the same as the ancestral minus the initial "0_"
$sis_seq_name = $seq_name;
$sis_seq_name =~ s/^0_//;
} elsif (!defined($old_seq_name)) {
$old_seq_name = $seq_name;
last; # No point in going any further
}
}
## It is OK not to have an "older" sequence. This can happen for shallow alignment or for cases where the reference is an out-sequence.
if (!defined($ref_seq_name) or !defined($sis_seq_name) or !defined($anc_seq_name)) {
print_ortheus_alignment($ortheus_alignment);
die "Problem parsing Ortheus output (".join(" ", @ortheus_args). ")";
}
$ortheus_alignment->{"ref"} = $ortheus_alignment->{$ref_seq_name};
$ortheus_alignment->{"sis"} = $ortheus_alignment->{$sis_seq_name};
$ortheus_alignment->{"anc"} = $ortheus_alignment->{$anc_seq_name};
$ortheus_alignment->{"old"} = $ortheus_alignment->{$old_seq_name} if ($old_seq_name);
# print_ortheus_alignment($ortheus_alignment);
return $ortheus_alignment;
}
=head2 call_ancestral_allele_for_deletion
Arg[1]: hashref $ref_alignment
Arg[2]: hashref $deletion_alignment
Arg[3]: string $deletion
Arg[4]: int $flank_length
Arg[5]: bool $verbose
Returns: array with
- $ref_allele from the $ref_alignment
- $ref_allele from the $insertion_alignment
- inferred final $ancestral_allele call,
- $indel_call
Description: The method gets the alles for both alignments using the get_alleles_for_
deletion() method and makes a call based on these results.
In verbose mode, prints both ref and deletion alignment together with the
final call.
=cut
sub call_ancestral_allele_for_deletion {
my ($self, $reference_ortheus_alignment, $deletion_ortheus_alignment, $this_deletion, $flank_length, $verbose) = @_;
my ($ref_reference_allele, $sis_reference_allele, $anc_reference_allele, $old_reference_allele) = get_alleles_for_deletion($reference_ortheus_alignment, $flank_length, $this_deletion);
my ($ref_deletion_allele, $sis_deletion_allele, $anc_deletion_allele, $old_deletion_allele) = get_alleles_for_deletion($deletion_ortheus_alignment, $flank_length, $this_deletion);
## TODO
## TODO: Add confidence information
## TODO
## TODO
## TODO
## TODO
## TODO
my $ancestral_allele_call = $anc_reference_allele;
my $indel_call;
if (uc($anc_reference_allele) eq uc($anc_deletion_allele)) {
if (uc($anc_reference_allele) eq uc($ref_reference_allele)) {
$indel_call = "deletion";
} elsif (uc($anc_reference_allele) eq uc($ref_deletion_allele)) {
$indel_call = "insertion";
} elsif (length($anc_reference_allele) == length($ref_reference_allele)) {
$indel_call = "complex_deletion";
} elsif (length($anc_reference_allele) == length($ref_deletion_allele)) {
$indel_call = "complex_insertion";
} else {
$indel_call = "cryptic_indel";
}
} elsif (length($anc_reference_allele) eq length($anc_deletion_allele)) {
if (uc($anc_reference_allele) eq uc($ref_reference_allele)) {
$indel_call = "deletion";
} elsif (uc($anc_deletion_allele) eq uc($ref_deletion_allele)) {
$ancestral_allele_call = $anc_deletion_allele;
$indel_call = "insertion";
} elsif (length($anc_reference_allele) == length($ref_reference_allele)) {
$indel_call = "complex_deletion";
} elsif (length($anc_deletion_allele) == length($ref_deletion_allele)) {
$ancestral_allele_call = $anc_deletion_allele;
$indel_call = "complex_insertion";
} else {
$indel_call = "cryptic_indel";
}
} else {
$ancestral_allele_call = "?";
$indel_call = "unsure";
}
$ref_reference_allele ||= "-";
$ref_deletion_allele ||= "-";
$ancestral_allele_call ||= "-";
if ($verbose) {
print "\nDELETION (-$this_deletion)\n";
print "Alignment for the reference allele:\n";
print_ortheus_alignment($reference_ortheus_alignment);
print "Alignment for the alternate allele (-$this_deletion):\n";
print_ortheus_alignment($deletion_ortheus_alignment);
print "ANCESTRAL ALLELE <$ancestral_allele_call>\n";
print "TYPE: $indel_call\n";
}
return (($ref_reference_allele or "-"), ($ref_deletion_allele or "-"), ($ancestral_allele_call or "-"), $indel_call);
}
=head2 call_ancestral_allele_for_insertion
Arg[1]: hashref $ref_alignment
Arg[2]: hashref $insertion_alignment
Arg[3]: string $insertion
Arg[4]: int $flank_length
Arg[5]: bool $verbose
Returns: array with
- $ref_allele from the $ref_alignment
- $ref_allele from the $insertion_alignment
- inferred final $ancestral_allele call,
- $indel_call
Description: The method gets the alles for both alignments using the get_alleles_for_
insertion() method and makes a call based on these results.
In verbose mode, prints both ref and insertion alignment together with the
final call.
=cut
sub call_ancestral_allele_for_insertion {
my ($self, $reference_ortheus_alignment, $insertion_ortheus_alignment, $this_insertion, $flank_length, $verbose) = @_;
my ($ref_reference_allele, $sis_reference_allele, $anc_reference_allele, $old_reference_allele) = get_alleles_for_insertion($reference_ortheus_alignment, $flank_length, $this_insertion);
my ($ref_insertion_allele, $sis_insertion_allele, $anc_insertion_allele, $old_insertion_allele) = get_alleles_for_insertion($insertion_ortheus_alignment, $flank_length, $this_insertion);
## TODO
## TODO: Add confidence information
## TODO
## TODO
## TODO
## TODO
## TODO
my $ancestral_allele_call = $anc_reference_allele;
my $indel_call;
if (uc($anc_reference_allele) eq uc($anc_insertion_allele)) {
if (uc($anc_reference_allele) eq uc($ref_reference_allele)) {
$indel_call = "insertion";
} elsif (uc($anc_reference_allele) eq uc($ref_insertion_allele)) {
$indel_call = "deletion";
} elsif (length($anc_reference_allele) == length($ref_reference_allele)) {
$indel_call = "complex_insertion";
} elsif (length($anc_reference_allele) == length($ref_insertion_allele)) {
$indel_call = "complex_deletion";
} else {
$indel_call = "cryptic_indel";
}
} elsif (length($anc_reference_allele) eq length($anc_insertion_allele)) {
if (uc($anc_reference_allele) eq uc($ref_reference_allele)) {
$indel_call = "insertion";
} elsif (uc($anc_insertion_allele) eq uc($ref_insertion_allele)) {
$ancestral_allele_call = $anc_insertion_allele;
$indel_call = "deletion";
} elsif (length($anc_reference_allele) == length($ref_reference_allele)) {
$indel_call = "complex_insertion";
} elsif (length($anc_reference_allele) == length($ref_insertion_allele)) {
$ancestral_allele_call = $anc_insertion_allele;
$indel_call = "complex_deletion";
} else {
$indel_call = "cryptic_indel";
}
} else {
$ancestral_allele_call = "?";
$indel_call = "unsure";
}
if ($verbose) {
print "\nINSERTION ($this_insertion)\n";
print "Alignment for the reference allele:\n";
print_ortheus_alignment($reference_ortheus_alignment);
print "Alignment for the alternate allele ($this_insertion):\n";
print_ortheus_alignment($insertion_ortheus_alignment);
print "ANCESTRAL ALLELE <$ancestral_allele_call>\n";
print "TYPE: $indel_call\n";
}
return (($ref_reference_allele or "-"), ($ref_insertion_allele or "-"), ($ancestral_allele_call or "-"), $indel_call);
}
sub call_ancestral_allele_from_ortheus_alignments {
my ($reference_ortheus_alignment, $alternate_ortheus_alignment, $flank5_length, $flank3_length, $verbose) = @_;
my ($ref_reference_allele, $sis_reference_allele, $anc_reference_allele, $old_reference_allele) =
get_alleles_from_ortheus_alignment($reference_ortheus_alignment, $flank5_length, $flank3_length);
my ($ref_alternate_allele, $sis_alternate_allele, $anc_alternate_allele, $old_alternate_allele) =
get_alleles_from_ortheus_alignment($alternate_ortheus_alignment, $flank5_length, $flank3_length);
if ($verbose) {
print VERBOSE join(" -- ", $ref_reference_allele, $sis_reference_allele, $anc_reference_allele, ($old_reference_allele or "")), "\n";
print VERBOSE join(" :: ", $ref_alternate_allele, $sis_alternate_allele, $anc_alternate_allele, ($old_alternate_allele or "")), "\n";
}
my $ancestral_allele_call = $anc_reference_allele;
my $indel_call = "";
if (uc($anc_reference_allele) eq uc($anc_alternate_allele)) {
if (uc($anc_reference_allele) eq uc($ref_reference_allele)) {
$indel_call = "DERIVED";
} elsif (uc($anc_reference_allele) eq uc($ref_alternate_allele)) {
$indel_call = "ANCESTRAL";
} elsif (length($anc_reference_allele) == length($ref_reference_allele)) {
$indel_call = "DERIVED (complex)";
} elsif (length($anc_reference_allele) == length($ref_alternate_allele)) {
$indel_call = "ANCESTRAL (complex)";
} else {
$indel_call = "CRYPTIC";
}
} else {
$ancestral_allele_call = "?";
$indel_call = "unsure";
}
return (($ref_reference_allele or "-"), ($ref_alternate_allele or "-"), ($ancestral_allele_call or "-"), $indel_call);
}
=head2 get_alleles_from_ortheus_alignment
=cut
sub get_alleles_from_ortheus_alignment {
my ($ortheus_alignment, $flank5_length, $flank3_length) = @_;
my $ref_aligned_seq = $ortheus_alignment->{"ref"}->{aligned_sequence};
my ($flank5, $allele, $flank3) = $ortheus_alignment->{"ref"}->{aligned_sequence} =~ /((?:\-*[a-zA-Z]){$flank5_length})(.*)((?:[a-zA-Z]\-*){$flank3_length})$/;
if (!$flank5 or !$flank3) {
print_ortheus_alignment($ortheus_alignment);
die "Cannot extract the flanks from sequence: ".$ortheus_alignment->{"ref"}->{aligned_sequence};
}
# print $ref_aligned_seq, "\n";
# print join("", $flank5, $allele, $flank3), "\n";
# print join(":", $flank5, $allele, $flank3), "\n";
# $flank5 =~ s/\-*($allele\-*)+$//g;
# my $length_flank5 = length($flank5);
# $flank3 =~ s/^(\-*$allele)+\-*//g;
# my $length_flank3 = length($flank3);
# my $length_allele = length($ortheus_alignment->{"ref"}->{aligned_sequence}) - $length_flank5 - $length_flank3;
my $length_flank5 = length($flank5);
my $length_allele = length($allele);
my $length_flank3 = length($flank3);
$ortheus_alignment->{"lengths"} = [$length_flank5, $length_allele, $length_flank3];
my $ref_allele = substr($ortheus_alignment->{"ref"}->{aligned_sequence}, $length_flank5, $length_allele);
my $sis_allele = substr($ortheus_alignment->{"sis"}->{aligned_sequence}, $length_flank5, $length_allele);
my $anc_allele = substr($ortheus_alignment->{"anc"}->{aligned_sequence}, $length_flank5, $length_allele);
my $old_allele;
if ($ortheus_alignment->{"old"}) {
$old_allele = substr($ortheus_alignment->{"old"}->{aligned_sequence}, $length_flank5, $length_allele);
}
for (my $pos = 0; $pos < length($anc_allele); $pos++) {
my $anc = substr($anc_allele, $pos, 1);
my $sis = substr($sis_allele, $pos, 1);
my $old = "";
if ($old_allele) {
$old = substr($old_allele, $pos, 1);
}
if ($anc eq $sis and $anc eq $old) {
$anc = uc($anc)
} elsif ($anc eq $sis or $anc eq $old) {
$anc = lc($anc);
} else {
$anc = "?";
}
substr($anc_allele, $pos, 1, $anc);
}
$ref_allele =~ s/\-//g;
$sis_allele =~ s/\-//g;
$anc_allele =~ s/\-//g;
$old_allele =~ s/\-//g if ($old_allele);
return($ref_allele, $sis_allele, $anc_allele, $old_allele);
}
=head2 get_alleles_for_deletion
Arg[1]: hashref $ortheus_alignment
Arg[2]: int $flank_length
Arg[3]: char $indel
Returns: array with string $ref_allele, string $sis_allele, string $anc_allele, string
$old_allele, int $length_flank5, int $length_allele, int $length_flank3.
Description: Knowing the $flank_length and the $indel, this method parses the $ortheus_alignment
to extract the ref, sis, anc and old alleles. It also returns the lengths of the
flanks and the allele. The allele might be longer than 1bp is this deletion affects
a homopolymer run.
The method assumes that the $ortheus_alignment already knows the "ref", "sis", "anc"
and "old" sequences (this is usually done by the run_ortheus() method).
Note that this method works for both the reference and the alternate allele ortheus
alignment. The expectation for most cases is to get:
REF.ALLELE.ALIGN => ("A", "A", "A", "A", 10, 1, 9);
ALT.ALLELE.ALIGN => ("", "A", "A", "A", 10, 1, 9);
=cut
sub get_alleles_for_deletion {
my ($ortheus_alignment, $flank_length, $indel) = @_;
my $flank3_length = $flank_length - 1;
my ($flank5, $flank3) = $ortheus_alignment->{"ref"}->{aligned_sequence} =~ /((?:\-*[a-zA-Z]){$flank_length}).*((?:[a-zA-Z]\-*){$flank3_length})$/;
if (!$flank5 or !$flank3) {
print_ortheus_alignment($ortheus_alignment);
die "Cannot extract the flanks from sequence (deletion): ".$ortheus_alignment->{"ref"}->{aligned_sequence};
}
$flank5 =~ s/\-*($indel\-*)+$//g;
my $length_flank5 = length($flank5);
$flank3 =~ s/^(\-*$indel)+\-*//g;
my $length_flank3 = length($flank3);
my $length_allele = length($ortheus_alignment->{"ref"}->{aligned_sequence}) - $length_flank5 - $length_flank3;
$ortheus_alignment->{"lengths"} = [$length_flank5, $length_allele, $length_flank3];
my $ref_allele = substr($ortheus_alignment->{"ref"}->{aligned_sequence}, $length_flank5, $length_allele);
my $sis_allele = substr($ortheus_alignment->{"sis"}->{aligned_sequence}, $length_flank5, $length_allele);
my $anc_allele = substr($ortheus_alignment->{"anc"}->{aligned_sequence}, $length_flank5, $length_allele);
my $old_allele;
if ($ortheus_alignment->{"old"}) {
$old_allele = substr($ortheus_alignment->{"old"}->{aligned_sequence}, $length_flank5, $length_allele);
}
# print_ortheus_alignment($ortheus_alignment);
for (my $pos = 0; $pos < length($anc_allele); $pos++) {
my $anc = substr($anc_allele, $pos, 1);
my $sis = substr($sis_allele, $pos, 1);
my $old = "";
if ($old_allele) {
$old = substr($old_allele, $pos, 1);
}
if ($anc eq $sis and $anc eq $old) {
$anc = uc($anc)
} elsif ($anc eq $sis or $anc eq $old) {
$anc = lc($anc);
} else {
$anc = "?";
}
substr($anc_allele, $pos, 1, $anc);
}
$ref_allele =~ s/\-//g;
$sis_allele =~ s/\-//g;
$anc_allele =~ s/\-//g;
$old_allele =~ s/\-//g if ($old_allele);
return($ref_allele, $sis_allele, $anc_allele, $old_allele, $length_flank5, $length_allele, $length_flank3);
}
=head2 get_alleles_for_insertion
Arg[1]: hashref $ortheus_alignment
Arg[2]: int $flank_length
Arg[3]: char $indel
Returns: array with string $ref_allele, string $sis_allele, string $anc_allele, string
$old_allele, int $length_flank5, int $length_allele, int $length_flank3.
Description: Knowing the $flank_length and the $indel, this method parses the $ortheus_alignment
to extract the ref, sis, anc and old alleles. It also returns the lengths of the
flanks and the allele. The allele might be longer than 1bp is this insertion affects
a homopolymer run.
The method assumes that the $ortheus_alignment already knows the "ref", "sis", "anc"
and "old" sequences (this is usually done by the run_ortheus() method).
Note that this method works for both the reference and the alternate allele ortheus
alignment. The expectation for most cases is to get:
REF.ALLELE.ALIGN => ("", "", "", "", 10, 0, 10);
ALT.ALLELE.ALIGN => ("A", "", "", "", 10, 1, 10);
=cut
sub get_alleles_for_insertion {
my ($ortheus_alignment, $flank_length, $indel) = @_;
my ($flank5, $flank3) = $ortheus_alignment->{"ref"}->{aligned_sequence} =~ /((?:\-*[a-zA-Z]){$flank_length}).*((?:[a-zA-Z]\-*){$flank_length})$/;
if (!$flank5 or !$flank3) {
print_ortheus_alignment($ortheus_alignment);
die "Cannot extract the flanks from sequence (insertion): ".$ortheus_alignment->{"ref"}->{aligned_sequence};
}
$flank5 =~ s/\-*($indel\-*)+$//g;
my $length_flank5 = length($flank5);
$flank3 =~ s/^(\-*$indel)+\-*//g;
my $length_flank3 = length($flank3);
my $length_allele = length($ortheus_alignment->{"ref"}->{aligned_sequence}) - $length_flank5 - $length_flank3;
$ortheus_alignment->{"lengths"} = [$length_flank5, $length_allele, $length_flank3];
my $ref_allele = substr($ortheus_alignment->{"ref"}->{aligned_sequence}, $length_flank5, $length_allele);
my $sis_allele = substr($ortheus_alignment->{"sis"}->{aligned_sequence}, $length_flank5, $length_allele);
my $anc_allele = substr($ortheus_alignment->{"anc"}->{aligned_sequence}, $length_flank5, $length_allele);
my $old_allele;
if ($ortheus_alignment->{"old"}) {
$old_allele = substr($ortheus_alignment->{"old"}->{aligned_sequence}, $length_flank5, $length_allele);
}
# print_ortheus_alignment($ortheus_alignment);
for (my $pos = 0; $pos < length($anc_allele); $pos++) {
my $anc = substr($anc_allele, $pos, 1);
my $sis = substr($sis_allele, $pos, 1);
my $old = "";
if ($old_allele) {
$old = substr($old_allele, $pos, 1);
}
if ($anc eq $sis and $anc eq $old) {
$anc = uc($anc)
} elsif ($anc eq $sis or $anc eq $old) {
$anc = lc($anc);
} else {
$anc = "?";
}
substr($anc_allele, $pos, 1, $anc);
}
$ref_allele =~ s/\-//g;
$sis_allele =~ s/\-//g;
$anc_allele =~ s/\-//g;
$old_allele =~ s/\-//g if ($old_allele);
return($ref_allele, $sis_allele, $anc_allele, $old_allele, $length_flank5, $length_allele, $length_flank3);
}
=head2 print_ortheus_alignment
Arg[1]: hashref $sorted_alignment
Returns: N/A
Description: Prints the sorted alignment to the STDOUT. If the alignment has "lengths", this
method uses those to visually split the alignment in 5' - allele - 3' regions.
The meothod also prints the "ref", "sis", "anc" and "old" sequences at the end
if these are defined (these are just references to the corresponding sequences).
These calls are made by the run_ortheus() method.
This method is used to print the alignments in verbose mode
=cut
sub print_ortheus_alignment {
my ($ortheus_alignment, $fh) = @_;
if (!$fh) {
$fh = *STDOUT;
}
foreach my $seq (sort {length($a) <=> length($b) || ($a =~ /^(\d+)/)[0] <=> ($b =~ /^(\d+)/)[0]} grep {!/^ref|sis|anc|old|lengths$/} keys %$ortheus_alignment) {
if ($ortheus_alignment->{"lengths"}) {
my $pattern = join("", map {"(.{".$_."})"} @{$ortheus_alignment->{"lengths"}});
print $fh "SEQ ", join(" | ", ($ortheus_alignment->{$seq}->{aligned_sequence} =~ /$pattern/)), " $seq ", ($ortheus_alignment->{$seq}->{name} or ""), "\n";
} else {
print $fh "SEQ ", $ortheus_alignment->{$seq}->{aligned_sequence}, " $seq ", ($ortheus_alignment->{$seq}->{name} or ""), "\n";
}
}
foreach my $seq ("ref", "sis", "anc", "old") {
next if (!$ortheus_alignment->{$seq});
if ($ortheus_alignment->{"lengths"}) {
my $pattern = join("", map {"(.{".$_."})"} @{$ortheus_alignment->{"lengths"}});
print $fh "$seq ", join(" | ", ($ortheus_alignment->{$seq}->{aligned_sequence} =~ /$pattern/)), "\n";
} else {
print $fh "$seq ", $ortheus_alignment->{$seq}->{aligned_sequence}, "\n";
}
}
}
##############################################################################################
##############################################################################################
##
## SORTED ALIGNMENT METHODS
##
##############################################################################################
##############################################################################################
=head1 Sorted alignment methods
=cut
sub print_sorted_alignment {
my ($sorted_alignment) = @_;
print "TREE: ", $sorted_alignment->{tree}, "\n";
print "POSI: ", join(", ", @{$sorted_alignment->{positions}}), "\n";
foreach my $seq (@{$sorted_alignment->{sequences}}) {
if ($seq->{aligned_sequence}) {
if (length($seq->{aligned_sequence}) < 50) {
print $seq->{aligned_sequence}, " ", $seq->{name}, "\n";
} else {
print substr($seq->{aligned_sequence}, 0, 50), " ", $seq->{name}, "\n";
}
} elsif ($seq->{original_sequence}) {
if (length($seq->{original_sequence}) < 30) {
printf("%-30s %s\n", $seq->{original_sequence}, $seq->{name});
} else {
printf("%30s %s\n", substr($seq->{original_sequence}, 0, 50), $seq->{name});
}
}
}
print "\n";
}
sub read_sorted_alignment_from_emf_fh {
my ($emf_fh) = @_;
my $sorted_alignment = undef;
my $pattern = "";
while (<$emf_fh>) {
if (/^SEQ (.+)/) {
my $info = $1;
my ($species, $chromosome, $start, $end, $strand) = $info =~ /(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)/;
if ($species eq "ancestral_sequences") {
# Match but don't store the sequence for the ancestral sequences
$pattern .= " ?\\S";
} else {
my $name = get_name_from_data($species, $chromosome, $start, $end, $strand);
push(@{$sorted_alignment->{sequences}}, {species=>$species, chr=>$chromosome, start=>$start, end=>$end, strand=>$strand, name=>$name});
# Match and store the sequence for the extant sequences
$pattern .= " ?(\\S)";
}
} elsif ($_ =~ /^SCORE/) {
# Match but don't store the score
$pattern .= " \-?[\\d\.]+";
} elsif (/^TREE (.+)/) {
my $tree = $1;
$tree =~ s/\:0([^\.])/:0.0001$1/g;
$tree =~ s/Aseq_Ancestor_[\d_]+\[.\]//g;
$sorted_alignment->{tree} = $tree;
} elsif (/^DATA/) {
while(<$emf_fh>) {
my @this_line = uc($_) =~ /$pattern/;
for (my $i=0; $i<@this_line; $i++) {
$sorted_alignment->{sequences}->[$i]->{aligned_sequence} .= $this_line[$i];
}
last if (/\/\//);
}
# Fix the tree.
fix_alignment_tree($sorted_alignment);
last;
}
}
return $sorted_alignment;
}
=head2 fix_alignment_tree
This method modifies the tree string extracted from the EMF file. It also capture the order in which the sequences are
listed on the tree. This is required by Ortheus as it relies on the order of the input files matching the order of the
leaves in the input tree.
The modified tree is stored back in the $sorted_alignment->{tree} variable and a new variable ($sorted_alignment->{positions})
contains an array with the positions of each sequence in the tree string.
=cut
sub fix_alignment_tree {
my ($sorted_alignment) = @_;
my $tree = $sorted_alignment->{tree};
for (my $i = 0; $i < @{$sorted_alignment->{sequences}}; $i++) {
my $name = $sorted_alignment->{sequences}->[$i]->{name};
$tree =~ s/\Q$name\E/$i/g;
}
my @positions = ($tree =~ /(\d+)\:/g);
$sorted_alignment->{tree} = $tree;
$sorted_alignment->{positions} = \@positions;
}
=head2 straighten_sorted_alignment
Arg[1]: hashref $sorted_alignment