forked from Ensembl/VEP_plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VEPbrowse.pm
1873 lines (1427 loc) · 53.4 KB
/
VEPbrowse.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
=head1 LICENSE
Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=head1 CONTACT
Will McLaren <[email protected]>
=cut
=head1 NAME
Draw
=head1 SYNOPSIS
mv VEPbrowse.pm ~/.vep/Plugins
perl variant_effect_predictor.pl -i variations.vcf --plugin VEPbrowse --individual JohnDoe
=head1 DESCRIPTION
A VEP plugin that generates HTML with an overview of an individual's mutations
across the genome, as well as a transcript-specific sequence view. This plugin
is under development, please expect bugs!!!
=cut
package VEPbrowse;
use strict;
use warnings;
use Bio::EnsEMBL::Variation::Utils::BaseVepPlugin;
use Bio::EnsEMBL::Variation::Utils::VariationEffect qw(MAX_DISTANCE_FROM_TRANSCRIPT overlap);
use Bio::EnsEMBL::Variation::Utils::Sequence qw(align_seqs);
use Bio::EnsEMBL::Utils::Sequence qw(reverse_comp);
use Bio::EnsEMBL::Utils::Exception qw(throw warning);
use Bio::SeqUtils;
use base qw(Bio::EnsEMBL::Variation::Utils::BaseVepPlugin);
my $ktbs;
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
# configure
my @params = @{$self->params};
# initialize cache
$self->{cache} = {};
$self->{has_cache} = 1;
# force various options
$self->{config}->{prefetch} = 1;
$self->{config}->{hgnc} = 1;
return $self;
}
sub version {
return '2.5';
}
sub feature_types {
return ['Transcript'];
}
sub variant_feature_types {
return ['VariationFeature'];
}
sub get_header_info {
return {};
}
# the destroy method will be run as the script closes
# so we can use this to build the summary HTML page
sub DESTROY {
my $self = shift;
open OUT, ">index.html";
if(defined($self->{karyotype_bands})) {
my ($canvas_width, $canvas_height) = (1000, 500);
my $html =<<END;
<!DOCTYPE html>
<html>
<head>
</head>
<body onload="init();">
<div id="canvas_holder" style="border:1px solid #c3c3c3; width:${canvas_width}px;">
<canvas id="karyotype" width="$canvas_width" height="$canvas_height">
Your browser does not support the HTML5 canvas element.
</canvas>
</div>
<div id="info" style="width:${canvas_width}px;height:200px;overflow:auto;border:1px solid #c3c3c3; padding: 5px; font-family: arial, sans-serif;">
</div>
<script type="text/javascript">
var c, ctx, tool, genes, info;
var select_from, select_to;
var canvas_width, canvas_height;
var x_scale, x_off;
function init() {
c = document.getElementById("karyotype");
ctx = c.getContext("2d");
info = document.getElementById("info");
canvas_width = $canvas_width;
canvas_height = $canvas_height;
tool = new tool_pencil();
drawKaryotype();
c.addEventListener('mousedown', ev_canvas, false);
c.addEventListener('mousemove', ev_canvas, false);
c.addEventListener('mouseup', ev_canvas, false);
}
END
$html .= $self->karyotype_jscript();
$html .= $self->render_karyotype_html($canvas_width, $canvas_height);
$html .= "\n</script>\n</body>\n</html>";
print OUT $html;
}
else {
print OUT "<ul>";
foreach my $gene(keys %{$self->{genes}}) {
print OUT "<li>$gene<ul>\n";
foreach my $tr(@{$self->{genes}->{$gene}->{transcripts}}) {
print OUT "<li><a href='".$tr->{transcript}.".html'>$tr->{transcript}</a>";
print OUT " Mismatches: ".$tr->{mismatches}." Length ratio: ".$tr->{length_ratio};
print OUT "</li>";
}
print OUT "</ul></li>";
}
print OUT "</ul>";
}
close OUT;
}
sub run {
my ($self, $tva) = @_;
my $tr = $tva->transcript;
my $vf = $tva->base_variation_feature;
my $tr_stable_id = $tr->stable_id;
my $individual = $vf->{individual};
$self->get_karyotype_bands($self->config->{reg}->get_adaptor($self->config->{species}, 'core', 'slice')->db);
return {} unless defined($vf->{last_in_transcript}->{$tr_stable_id});
return {} unless defined $tr->{vfs} && scalar @{$tr->{vfs}};
my $exons = $tr->get_all_Exons();
return {} unless defined $exons && scalar @$exons;
$_->{slice} ||= $tr->{slice} for @$exons;
my $mapper = $tr->{_variation_effect_feature_cache}->{mapper};
return {} unless defined $mapper;
my $codon_table = $tr->{_variation_effect_feature_cache}->{codon_table};
return {} unless defined $tr->translation;
my $translateable_seq = $tr->{_variation_effect_feature_cache}->{translateable_seq};
# check if this transcript has any variants that change its sequence
my $ok = 0;
my $worst_con;
foreach my $vf(@{$tr->{vfs}}) {
foreach my $tv(@{$vf->get_all_TranscriptVariations([$tr])}) {
foreach my $tva(@{$tv->get_all_TranscriptVariationAlleles}) {
foreach my $oc(@{$tva->get_all_OverlapConsequences}) {
my $so_term = $oc->SO_term;
$worst_con = $oc if !defined($worst_con) || $oc->rank < $worst_con->rank;
if(
$so_term eq 'missense_variant' ||
$so_term eq 'stop_gained' ||
$so_term eq 'stop_lost' ||
$so_term eq 'transcript_ablation' ||
$so_term eq 'frameshift_variant' ||
$so_term eq 'initiator_codon_variant' ||
$so_term =~ /inframe_codon/
) {
$ok = 1;
last;
}
}
}
}
}
return {} unless $ok;
#$DBsingle = 1;
#print STDERR "\nDoing $tr_stable_id $individual\n";
#return {} unless $tr_stable_id eq 'ENST00000343703';
#print STDERR "Getting ref features\n";
# get ref features
my @ref_features = @{$self->get_ref_transcript_features($tr)};
# check ref features
die("ERROR: Ref features borked\n") unless grep {$_->{t} eq 'coding' && $_->{seq}} @ref_features;
#print STDERR "Getting ind features\n";
# get aligned individual features
# this also re-aligns the reference
my $features_arrayref = $self->get_individual_features($tr, \@ref_features);
my $var_features = pop @$features_arrayref;
#print STDERR "Getting translations\n";
# get peptides
my @peptides = map {$self->translate_features($_, $codon_table)} (\@ref_features, @$features_arrayref);
#print STDERR "Aligning ref peptide\n";
# align peptides to features
$self->align_pep_to_features($peptides[0], \@ref_features);
#print STDERR "Aligning ind peptides\n";
my $i = 1;
$self->align_pep_to_features($peptides[$i++], $_) for @$features_arrayref;
#print STDERR "Marking up codons\n";
# mark up codons
$self->markup_codons($_) for (\@ref_features, @$features_arrayref);
#print STDERR "Compiling sequences\n";
# construct seq from features
my $ref_tr_seq = "";
$ref_tr_seq .= $_->{marked_seq} for @ref_features;
# compile sequences, ref first, tr then pep
my @seqs = ($ref_tr_seq);
my $seq = "";
$seq .= $_->{pep} for @ref_features;
push @seqs, $seq;
for my $phase(@$features_arrayref) {
my $seq = "";
$seq .= $_->{marked_seq} for @{$phase};
push @seqs, $seq;
$seq = "";
$seq .= $_->{pep} for @{$phase};
push @seqs, $seq;
}
$seq = "";
$seq .= $_->{seq} for @$var_features;
push @seqs, $seq;
my $seqs = join ",", map {"'$_'"} @seqs;
# compare peptides
my $ref_pep = $seqs[1];
my ($worst_length_ratio, $worst_mismatches) = (100, 0);
for(my $i=3; $i<=$#seqs; $i+=2) {
my $pep = $seqs[$i];
# seq_differences
my ($tp1, $tp2) = ($ref_pep, $pep);
$tp1 =~ s/\-|\_//g;
$tp2 =~ s/\-|\_//g;
my $length_ratio = $tp1 ? 100 * (length($tp2) / length($tp1)) : 0;
$worst_length_ratio = $length_ratio if abs($length_ratio - 100) > abs($worst_length_ratio - 100);
my $mismatches = 0;
for(my $j=0; $j<length($tp1) && $j<length($tp2); $j+=3) {
$mismatches++ unless substr($tp1, $j, 3) eq substr($tp2, $j, 3);
}
$worst_mismatches = $mismatches if $mismatches > $worst_mismatches;
}
#my @seqs = ([map {$_->{marked_seq}} @ref_features],[map {$_->{pep_seq}} @ref_features]);
#
#foreach my $phase(@$features_arrayref) {
# push @seqs, [map {$_->{marked_seq}} @{$phase}];
# push @seqs, [map {$_->{pep_seq}} @{$phase}];
#}
#
#my $seqs = '[';
#
#foreach my $seq(@seqs) {
# $seqs .= '['.(join ",", map {"'$_'"} @$seq).'],';
#}
#
#$seqs =~ s/\,$//g;
#$seqs .= ']';
# log gaps in align
my @gaps = ('SHIFTMEOFF');
for my $i(0..(length($ref_tr_seq) - 1)) {
push @gaps, $i + 1 if substr($ref_tr_seq, $i, 1) eq '-';
}
my $gaps = join ",", map {"'$_'"} @gaps;
# parse VFs
my $vfs = $self->format_vfs($tr);
my $canvas_width = 1000;
my $canvas_height = 100;
my $x_scale = ($canvas_width - 20) / ($tr->end - $tr->start + 1);
my $y_scale = ($canvas_height - 20) / 100;
my $x_off = 10;
my $css = get_css();
my $html =<<END;
<!DOCTYPE html>
<html>
<head>
$css
</head>
<body onload="init();">
<div id="header" style="width:${canvas_width}px" class="header">
<div style="display: inline; float: right;"><img src="http://www.ensembl.org/img/vep_logo.png"></div>
<h1 style="display: inline; vertical-align: top;">$tr_stable_id</h1><br/>
<h2 style="display: inline; vertical-align: top;">$individual</h2>
<div style="font-size:small"><a href="index.html">Back to index</a></div>
</div>
<div id="canvas_holder" style="border:1px solid #c3c3c3; width:${canvas_width}px;">
<canvas id="transcript" width="$canvas_width" height="$canvas_height">
Your browser does not support the HTML5 canvas element.
</canvas>
</div>
<div id="control" style="width:${canvas_width}px" class="control">
<b>Selection: </b>
<a href="#" onClick="select_all();">Select all</a>
|
<a href="#" onClick="clear_selection();">Clear selection</a>
|
<b>Zoom: </b><a href="#" onClick="zoom(0.75);">+</a> / <a href="#" onClick="zoom(1.33);">-</a>
|
<b>Move: </b><a href="#" onClick="move(-0.5);"><<</a> / <a href="#" onClick="move(-0.1);"><</a> / <a href="#" onClick="move(0.1);">></a> / <a href="#" onClick="move(0.5);">>></a>
<hr class="control_spacer">
<b>Options: </b>
<input type="checkbox" name="coding_only_checkbox" id="coding_only_checkbox" onclick="coding_only=toggle_value(coding_only);render_seq();" checked=1/>
<span onClick="var c = document.getElementById('coding_only_checkbox'); c.checked =(! c.checked); coding_only=toggle_value(coding_only);render_seq();">Show coding sequence only</span>
<hr class="control_spacer">
<b>Sequences: </b>
Reference
<select name="ref_seqs_select" id="ref_seqs_select" onchange="ref_seqs=this.value;render_seq();">
<option value="tr_pep">Both</option>
<option value="tr">Transcript</option>
<option value="pep">Peptide</option>
<option value="none">None</option>
</select>
|
Phase 1
<select name="p1_seqs_select" id="p1_seqs_select" onchange="p1_seqs=this.value;render_seq();">
<option value="tr_pep">Both</option>
<option value="tr">Transcript</option>
<option value="pep">Peptide</option>
<option value="none">None</option>
</select>
|
Phase 2
<select name="p2_seqs_select" id="p2_seqs_select" onchange="p2_seqs=this.value;render_seq();">
<option value="tr_pep">Both</option>
<option value="tr">Transcript</option>
<option value="pep">Peptide</option>
<option value="none">None</option>
</select>
</div>
<div id="sequence" style="width:${canvas_width}px;height:400px;overflow:auto;border:1px solid #c3c3c3">
Click and drag to select a region of the transcript and show sequence.
</div>
<div id="debug" style="width:${canvas_width}px;height:150px;overflow:auto;border:1px solid #c3c3c3; font-size: small;"><h3>Debug</h3></div>
<script type="text/javascript">
var c, ctx, tool, s, debug_div, canvas_scale;
var select_from, select_to;
var seqs, vfs;
var coding_only, ref_seqs, p1_seqs, p2_seqs;
var seq_div_html;
var canvas_width, canvas_height;
var x_scale, x_off;
var gaps;
function init() {
c = document.getElementById("transcript");
ctx = c.getContext("2d");
s = document.getElementById("sequence");
debug_div = document.getElementById("debug");
canvas_width = $canvas_width;
canvas_height = $canvas_height;
x_scale = $x_scale;
x_off = $x_off;
gaps = new Array($gaps);
seqs = new Array($seqs);
vfs = $vfs;
// shift off first element
// bugfix for strange javascript behaviour when you try to create an array
// with one numeric element
gaps.shift();
drawTranscript();
canvas_scale = 1;
coding_only = 1;
ref_seqs = 'tr_pep';
p1_seqs = 'tr_pep';
p2_seqs = 'tr_pep';
seq_div_html = s.innerHTML;
tool = new tool_pencil();
c.addEventListener('mousedown', ev_canvas, false);
c.addEventListener('mousemove', ev_canvas, false);
c.addEventListener('mouseup', ev_canvas, false);
}
END
# add main jscript stuff
$html .= $self->jscript_html();
# add transcript render jscript
$html .= $self->render_transcript_html($tr, $canvas_width, $canvas_height);
# close javascript
$html .= "\n</script>\n";
$html .= "\n</body>\n</html>";
open OUT, ">".$tr->stable_id.".html";
print OUT $html;
close OUT;
# write transcript data to cache
my $gene = $tr->{_gene};
$self->{genes}->{$gene->stable_id} ||= {
coords => {
chr => $tr->slice->seq_region_name,
start => $gene->start,
end => $gene->end,
},
hgnc => $tr->{_gene_hgnc},
transcripts => [],
};
push @{$self->{genes}->{$gene->stable_id}->{transcripts}}, {
transcript => $tr->stable_id,
mismatches => $worst_mismatches,
length_ratio => $worst_length_ratio,
worst_con => $worst_con->label,
};
return {};
}
sub max {
return (sort {$a <=> $b} @_)[-1];
}
sub min {
return (sort {$a <=> $b} @_)[0];
}
sub print_aligned {
my $self = shift;
my ($s1, $s2) = @_;
my $width = 60;
while(1) {
my $c1 = substr($s1, 0, $width);
my $c2 = substr($s2, 0, $width);
#for my $i(0..(CORE::length($c1) - 1)) {
# substr($c2, $i, 1) = "." if substr($c2, $i, 1) eq substr($c1, $i, 1);
#}
#$c1 =~ s/([A-Z]{3})/$1 /g;
#$c2 =~ s/([A-Z]{3})/$1 /g;
print "$c1\n$c2\n\n";
last if CORE::length($s1) <= $width;
$s1 = substr($s1, $width);
$s2 = substr($s2, $width);
last unless $s1 && $s2;
}
}
sub get_ref_transcript_features {
my $self = shift;
my $tr = shift;
my $exons = $tr->get_all_Exons();
return {} unless defined $exons && scalar @$exons;
my $mapper = $tr->{_variation_effect_feature_cache}->{mapper};
return {} unless defined $mapper;
my $translateable_seq = $tr->{_variation_effect_feature_cache}->{translateable_seq};
#$DBsingle = 1;
my @features = ();
my $tr_seq = "";
my $prev_exon;
foreach my $exon(@$exons) {
# intron
if(defined($prev_exon)) {
my $seq = "_" x ($exon->start > $prev_exon->start ? $exon->start - $prev_exon->end : $prev_exon->start - $exon->end);
my $feat = {
t => 'intron',
s => length($tr_seq),
seq => $seq
};
$tr_seq .= $seq;
$feat->{e} = length($tr_seq);
push @features, $feat;
}
$prev_exon = $exon;
# map exon to CDS coords
my @coords = $mapper->genomic2cds($exon->start, $exon->end, $exon->strand);
foreach my $coord(@coords) {
# UTR
if($coord->isa('Bio::EnsEMBL::Mapper::Gap')) {
my $seq = '#' x $coord->length;
my $feat = {
t => 'utr',
s => length($tr_seq),
seq => $seq
};
$tr_seq .= $seq;
$feat->{e} = length($tr_seq);
push @features, $feat;
}
else {
my $seq = substr($translateable_seq, $coord->start - 1, $coord->length);
my $feat = {
t => 'coding',
s => length($tr_seq),
seq => $seq,
};
$tr_seq .= $seq;
$feat->{e} = length($tr_seq);
push @features, $feat;
}
}
}
return \@features;
}
sub get_individual_features {
my $self = shift;
my $tr = shift;
my $ref_features = shift;
# construct sequence from ref features
my $ref_tr_seq = "";
$ref_tr_seq .= $_->{seq} for @$ref_features;
# back up features
my @backup_features;
foreach(@$ref_features) {
push @backup_features, {
seq => $_->{seq},
t => $_->{t}
};
}
my @ind_features_array;
# copy features
foreach my $phase(0..1) {
my ($new_tr_seq, $new_features);
$new_tr_seq = $ref_tr_seq;
# clone features
foreach(@backup_features) {
push @$new_features, {
seq => $_->{seq},
t => $_->{t}
};
}
push @ind_features_array, $new_features;
}
# copy for var track
my @var_features;
foreach(@backup_features) {
push @var_features, {
'seq' => $_->{t} eq 'coding' ? (' ' x length($_->{seq})) : $_->{seq},
't' => $_->{t}
}
}
foreach my $vf(sort {$b->{start} <=> $a->{start}} @{$tr->{vfs}}) {
# get coords
my ($tr_vf_start, $tr_vf_end);
if($tr->strand == 1) {
$tr_vf_start = ($vf->start - $tr->start) + 1;
$tr_vf_end = ($vf->end - $tr->start) + 1;
}
else {
$tr_vf_start = ($tr->end - $vf->end) + 1;
$tr_vf_end = ($tr->end - $vf->start) + 1;
}
my $vf_length = ($vf->{end} - $vf->{start}) + 1;
# get alleles
next unless defined $vf->{genotype};
my @alleles = @{$vf->{genotype}};
next unless scalar @alleles;
# find longest
my $longest = (sort {length($a) <=> length($b)} @alleles)[-1];
my $longest_length = length($longest);
$longest_length = $vf_length if $vf_length > $longest_length;
# pad alleles
$_ .= "-" x ($longest_length - length($_)) for @alleles;
# replace sequence
for my $i(0..(scalar @$ref_features - 1)) {
my $ref_feat = $ref_features->[$i];
my $var_feat = $var_features[$i];
if(overlap($ref_feat->{s}, $ref_feat->{e}, $tr_vf_start, $tr_vf_end)) {
my ($s, $e) = ($tr_vf_start - $ref_feat->{s}, $tr_vf_end - $ref_feat->{s});
# pad ref sequence if necessary
if($longest_length > $vf_length) {
my $char = $ref_feat->{t} eq 'coding' ? '-' : ($ref_feat->{t} eq 'intron' ? '_' : '#');
substr($ref_feat->{seq}, $s, 0) = $char x ($longest_length - $vf_length);
substr($var_feat->{seq}, $s, 0) = ($ref_feat->{t} eq 'coding' ? 'i' : $char) x ($longest_length - $vf_length);
}
else {
substr($var_feat->{seq}, $s, ($e - $s) + 1) = ($vf->allele_string =~ /\-/ ? 'd' : 'v') x (($e - $s) + 1)
}
for my $phase(0..1) {
my $feat = $ind_features_array[$phase]->[$i];
my $tmp_allele = $alleles[$phase];
my ($s, $e) = ($tr_vf_start - $ref_feat->{s}, $tr_vf_end - $ref_feat->{s});
if($s < 0 && $tmp_allele ne '') {
$tmp_allele = substr($tmp_allele, 0 - $s);
$s = 0;
}
if($e > ($ref_feat->{e} - $ref_feat->{s})) {
$tmp_allele = substr($tmp_allele, 0, $e - ($ref_feat->{e} - $ref_feat->{s}));
$e = ($ref_feat->{e} - $ref_feat->{s});
}
$tmp_allele =~ s/./\_/g if $ref_feat->{t} eq 'intron';
$tmp_allele =~ s/./\#/g if $ref_feat->{t} eq 'utr';
substr($feat->{seq}, $s, ($e - $s) + 1) = $tmp_allele;
}
}
}
}
push @ind_features_array, \@var_features;
return \@ind_features_array;
}
sub markup_codons {
my $self = shift;
my $features = shift;
my $phase_adjust = 0;
my $tmp_phase = 0;
my $flipper = 1;
for my $i(0..(scalar @$features - 1)) {
my $feat = $features->[$i];
$feat->{marked_seq} = $feat->{seq};
if($feat->{t} eq 'coding') {
my $j = 0;
while($j < length($feat->{marked_seq})) {
my $l = 3 - $phase_adjust;
my $tl = $l;
die("Something's gone wrong in sequence markup\n") if $l < 0;
my $test = substr($feat->{marked_seq}, $j, $tl);
my $prev_length = length($test);
$test =~ s/\-//g;
my $t_length = length($test);
while($t_length < $l) {
$tl++;
$test = substr($feat->{marked_seq}, $j, $tl);
last if $prev_length == length($test);
$prev_length = length($test);
$test =~ s/\-//g;
$t_length = length($test);
}
if($flipper) {
$test = substr($feat->{marked_seq}, $j, $tl);
$test =~ tr/ACGT-/VWXYZ/;
substr($feat->{marked_seq}, $j, $tl) = $test;
}
$flipper = 1 - $flipper;
$j += $tl;
$phase_adjust = 0;
$tmp_phase = length($test) == 3 ? 0 : length($test);
}
$phase_adjust = $tmp_phase;
$flipper = 1 - $flipper if $phase_adjust;
}
}
}
sub translate_features {
my $self = shift;
my $features = shift;
my $codon_table = shift;
my $tr_seq = "";
$tr_seq .= $_->{seq} for @$features;
$tr_seq =~ s/\#|\_|n|i|\-//g;
my $pep_seq = Bio::Seq->new(-seq => $tr_seq)->translate(undef, undef, undef, $codon_table);
# get sequence as 3-letter AA codes
my $seq3 = Bio::SeqUtils->seq3($pep_seq);
# shorten the sequence if a new terminator has been introduced
while($seq3 =~ m/Ter/g) {
my $pos = pos($seq3);
substr($seq3, $pos) = "_" x (length($seq3) - $pos) if $pos < length($seq3);
last;
}
return $seq3;
}
sub align_pep_to_features {
my $self = shift;
my $pep = shift;
my $features = shift;
my $tmp_pep = $pep;
foreach my $f(@$features) {
if($f->{t} eq 'coding') {
my $tmp = $f->{seq};
$tmp =~ s/\-//g;
my $tmp2 = substr($tmp_pep, 0, length($tmp));
# re-insert gaps
for(my $i=0; $i < length($f->{seq}); $i++) {
substr($tmp2, $i, 0) = '-' if substr($f->{seq}, $i, 1) eq '-';
}
$f->{pep} = $tmp2;
# shorten tmp_pep
$tmp_pep = substr($tmp_pep, length($tmp)) unless length($tmp_pep) < length($tmp);
}
else {
$f->{pep} = "_" x length($f->{seq});
}
}
}
sub format_vfs {
my $self = shift;
my $tr = shift;
my @vfs;
foreach my $vf(@{$tr->{vfs}}) {
foreach my $tv(@{$vf->get_all_TranscriptVariations([$tr])}) {
foreach my $tva(@{$tv->get_all_TranscriptVariationAlleles}) {
push @vfs, [
$vf->variation_name,
$vf->{chr}, $vf->{start}, $vf->{end},
(join ",", map {$_->label} @{$tva->get_all_OverlapConsequences})
];
}
}
}
my $return = '[';
$return .= join ",", map {"[".(join ",", map {$_ =~ /^\d+$/ ? $_ : "'$_'"} @$_)."]"} @vfs;
$return .= ']';
#print $return."\n";
return $return;
}
sub get_karyotype_bands {
my $self = shift;
my $db = shift;
return if defined($self->{karyotype_bands});
my $config = $self->config;
my $ktba = $db->get_KaryotypeBandAdaptor;
my $sa = $db->get_SliceAdaptor;
if(!defined($ktba) || !defined($sa)) {
warn("WARNING: VEPbrowse plugin could not get karyotype bands");
$self->{karyotype_bands} = {};
return;
}
foreach my $slice(grep {$_->is_reference} @{$sa->fetch_all('chromosome')}) {
foreach my $ktb(@{$ktba->fetch_all_by_Slice($slice)}) {
push @{$self->{karyotype_bands}->{$slice->seq_region_name}}, {
start => $ktb->start,
end => $ktb->end,
stain => $ktb->stain,
};
}
}
}
sub jscript_html {
my $self = shift;
my $html =<<END;
function tool_pencil () {
var tool = this;
this.started = false;
var start_x;
// This is called when you start holding down the mouse button.
// This starts the pencil drawing.
this.mousedown = function (ev) {
start_x = ev._x;
tool.started = true;
};
// This function is called every time you move the mouse. Obviously, it only
// draws if the tool.started state is set to true (when you are holding down
// the mouse button).
this.mousemove = function (ev) {
if (tool.started) {
var x = start_x < ev._x ? start_x : ev._x;
var w = ev._x - start_x;
if(w < 0) {
w = 0 - w;
}
select_from = start_x < ev._x ? start_x : ev._x;
select_to = start_x > ev._x ? start_x : ev._x;
// clear canvas
ctx.clearRect(0, 0, canvas_width, canvas_height);
// redraw transcript
drawTranscript();
draw_box(x, w);
}
};
// This is called when you release the mouse button.
this.mouseup = function (ev) {
if (tool.started) {
tool.mousemove(ev);
tool.started = false;
render_seq();
}
}
}
function ev_canvas (ev) {
var element = c, offsetX = 0, offsetY = 0, div = document.getElementById('canvas_holder');
// Compute the total offset
if (element.offsetParent !== undefined) {
do {
offsetX += element.offsetLeft;
offsetY += element.offsetTop;
} while ((element = element.offsetParent));
}
// Add padding and border style widths to offset
// Also add the <html> offsets in case there's a position:fixed bar
//offsetX += div.stylePaddingLeft + div.styleBorderLeft + div.htmlLeft;
//offsetY += div.stylePaddingTop + div.styleBorderTop + div.htmlTop;
ev._x = ev.pageX - offsetX;
ev._y = ev.pageY - offsetY;
// old way
//if (ev.layerX || ev.layerX == 0) { // Firefox
// ev._x = ev.layerX;
// ev._y = ev.layerY;
//} else if (ev.offsetX || ev.offsetX == 0) { // Opera
// ev._x = ev.offsetX;
// ev._y = ev.offsetY;
//}