-
Notifications
You must be signed in to change notification settings - Fork 0
/
gbrowse_syn
executable file
·1651 lines (1403 loc) · 50.5 KB
/
gbrowse_syn
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/bin/perl -w
###################################################################
# Non-modperl users should change this variable if needed to point
# to the directory in which the configuration files are stored.
#
$CONF_DIR = '/FRONT-END/www/conf/gbrowse.conf';
#
###################################################################
$VERSION = 1.70;
$BIOGRAPHICS_VERSION = 1.8;
#$Id: gbrowse_syn.PLS,v 1.9.2.18 2009/05/21 15:14:20 sheldon_mckay Exp $
use strict;
use CGI qw/:standard Map Area delete_all/;
use CGI::Carp 'fatalsToBrowser';
use CGI::Toggle;
use Bio::Graphics;
use Bio::Graphics::Browser;
use Bio::Graphics::Browser::Util;
use Bio::Graphics::Browser::Synteny;
use Bio::Graphics::Browser::PageSettings;
use Bio::DB::GFF::SyntenyIO;
use Bio::DB::GFF::SyntenyBlock;
use Text::Shellwords 'shellwords';
use List::Util qw/min max sum/;
use Digest::MD5 'md5_hex';
use Data::Dumper;
use constant OVERVIEW_RATIO => 0.9;
use constant OVERVIEW_BGCOLOR => 'gainsboro';
use constant IMAGE_WIDTH => 800;
use constant INTERIMAGE_PAD => 5;
use constant VERTICAL_PAD => 40;
use constant ALIGN_HEIGHT => 6;
use constant MAX_SPAN => 0.3; # largest gap allowed for merging inset panels
use constant TOO_SMALL => 0.02; # minimum span for displayed alignments
use constant MAX_GAP => 50_001; # maximum gap for chaining
use constant MAX_SEGMENT => 400_001;
use constant EXTENSION => 'syn'; # extension for species conf files
use constant DEBUG => 0;
use constant HELP => 'http://gmod.org/wiki/GBrowse_syn_Help';
# Display options -- using a hash here so that
# $page_settings can be updated without using hard-coded
# keys (not counting this constant!).
use constant SETTINGS =>
(
aggregate => 0 , # chain alignments
pgrid => 1, # gridlines
shading => 1, # shaded polygons
tiny => 0, # ignore small alignments
pflip => 1, # flip (-) strand panels
edge => 1, # outline polygons
imagewidth => 800, # width of the image
display => 'expanded', # ref + 2 or all in one
ref => undef,
start => undef,
stop => undef,
end => undef,
name => undef,
search_src => undef,
species => undef
);
use vars qw($CONF_DIR $SCONF $CONF $VERSION $MAP $BIOGRAPHICS_VERSION $SYNTENY_IO @hits);
# initialization
$CONF_DIR = conf_dir($CONF_DIR);
# search soure (general) configuration
$CONF = Bio::Graphics::Browser::Synteny->new();
$CONF->read_configuration($CONF_DIR,'synconf');
# species-specific configuration
my $extension = $CONF->setting('config_extension') || EXTENSION;
$SCONF = open_config($CONF_DIR,$extension);
my ($page_settings,$session) = page_settings();
my $source = $page_settings->{source};
$CONF->page_settings($page_settings);
$CONF->search_src($page_settings->{search_src});
$CONF->source($page_settings->{source});
$MAP = db_map();
$SYNTENY_IO = Bio::DB::GFF::SyntenyIO->new($CONF->setting('join'));
my $segment = landmark2segment($page_settings);
if ($segment) {
my $name = format_segment($segment);
if (ref $segment ne 'Bio::DB::GFF::RelSegment') {
redirect(url()."?name=$name");
}
$CONF->current_segment($segment);
param(name => $name);
$page_settings->{name} = $name;
}
my @reset = (0,0);
if (param('reset')) {
@reset = (1, 'All settings have been reset to their default values');
}
$CONF->print_page_top($CONF->setting('description'),@reset,$session);
# warning if trying to search with no species
if (!$segment && param('name') && !$CONF->page_settings("search_src")) {
print warning("Error: select a species to search");
}
# Which aligned species have been asked for
my @requested_species = _unique(@{$page_settings->{species}});
if (!@requested_species) {
my $search_src = $page_settings->{search_src} || 'nosrc';
@requested_species = grep {$_ ne $search_src} keys %$MAP;
}
if ($segment) {
@hits = map {$SYNTENY_IO->get_synteny_by_range(-src => $page_settings->{search_src},
-ref => $segment->abs_ref,
-start => $segment->abs_start,
-end => $segment->abs_end,
-tgt => $_) } @requested_species;
}
my $header = $CONF->setting('header');
$header = &$header if ref $header;
print $header || h1($CONF->setting('description'));
search_form($segment);
print overview_panel($CONF->whole_segment($segment),$segment) if $segment;
if ($segment) {
# make sure no hits go off-screen
remap_coordinates($_) for @hits;
my %species = map {$_->src2 => 1} @hits;
my @species = sort keys %species;
# either display ref species + 2 repeating or 'all in one'
if ($page_settings->{display} eq 'expanded') {
while (my @pair = splice @species, 0, 2) {
draw_image($page_settings,\@hits,@pair);
}
}
else {
draw_image($page_settings,\@hits,@species);
}
}
options_table();
print end_form();
print $CONF->footer || end_html();
# remember former source for the checkbox
$page_settings->{old_src} = $CONF->search_src;
exit 0;
sub species_chooser {
# pointless if < 3 species
return '' if keys %$MAP < 3;
my $src = $CONF->search_src();
my @species = grep {$_ ne $src} keys %$MAP;
my $default = $CONF->page_settings->{species} || \@species;
push @$default, $CONF->page_settings('old_src');
if (!param('species')) {
$default = [keys %$MAP];
}
b(wiki_help('Aligned_Species',,$CONF->tr('Aligned Species'))) . ':' . br .
checkbox_group(
-id => 'speciesChooser',
-name => 'species',
-values => \@species,
-labels => { map {$_ => $MAP->{$_}->{desc} } @species },
-default => $default,
-multiple => 1,
-size => 8,
-override => 1,
);
}
sub expand_notice {
# pointless if < 4 species
return '' if keys %$MAP < 4;
my $state = param('display') || $CONF->page_settings->{display};
my $ref = $CONF->search_src;
my $title = b(wiki_help("Display_Mode",$CONF->tr("Display Mode")), ':');
if ($state eq 'expanded') {
return br, $title, br, "Three species/panel ",
a( {-href => url.'?display=compact'}, 'Click to show all species in one panel');
}
else {
return br, $title, br, 'All species in one panel ',
a( {-href => url.'?display=expanded'}, 'Click to show reference plus two species/panel');
}
}
sub landmark_search {
my $segment = shift;
my $default = format_segment($segment) if $segment;
return $CONF->setting('no search')
? '' : b(wiki_help('Landmark',$CONF->tr('Landmark'))).':'.br.
textfield(-name=>'name', -size=>25, -value=>$default);
}
sub species_search {
my $default = $CONF->page_settings("search_src");
my %labels = map {$_=>$MAP->{$_}{desc}} keys %$MAP;
my $values = [sort {$MAP->{$a}{desc} cmp $MAP->{$b}{desc}} keys %labels];
unshift @$values, '';
my $onchange = "document.mainform.submit()";
return b(wiki_help('Reference_Species',$CONF->tr('Reference Species'))) . ':' . br .
popup_menu(
-onchange => $onchange,
-name =>'search_src',
-values => $values,
-labels => \%labels,
-default => $default,
-override => 1
);
}
sub search_form {
my $segment = shift;
print start_form(-name=>'mainform',-method => 'post');
navigation_table($segment);
}
sub db_map {
my %map;
my @map = shellwords($CONF->setting('source_map'));
while (my($symbol,$db,$desc) = splice(@map,0,3)) {
$map{$symbol}{db} = $db;
$map{$symbol}{desc} = $desc;
}
\%map;
}
sub _type_from_box {
my $box = shift;
my @type = split ':', $box->[0];
my ($feature,$fname) = @type[0,-1];
return ($feature,$fname);
}
sub draw_image {
my ($page_settings,$hits,@species) = @_;
my ($toggle_section,@hits);
for my $species (@species) {
push @hits, grep {$_->src2 eq $species} @$hits;
}
my $src = $CONF->page_settings("search_src");
my $segment = $CONF->current_segment or return;
my $max_segment = $CONF->setting('max_segment') || MAX_SEGMENT;
if ( $segment->length > $max_segment) {
my $units = $CONF->unit_label($max_segment);
print h2("Sorry: the size of region $segment exceeds the maximum ($units)");
exit;
}
my $max_gap = $segment->length * ($CONF->setting('max_span') || MAX_SPAN);
# dynamically create synteny blocks
@hits = aggregate(\@hits) if $CONF->page_settings("aggregate");
# save the hits by name so we can access them from the name alone
for (@hits) {
$CONF->name2hit( $_->name => $_ );
}
# relate hits into "span" sorted by target contig/chromosomes
# %span = ( target_database,target_contig => { target, contig, start, end } } )
my (%span,$hit2span,%instance);
for my $h (sort {$a->tstart <=> $b->tstart} @hits) {
my $src = $h->src2;
my $contig = $h->target;
$instance{$src,$contig} ||= 0;
my $key = join $;,$src,$contig,$instance{$src,$contig};
# start a new span if the gap is too large
if ($span{$key} && ($h->tstart >= $span{$key}{end}+$max_gap)) {
$key = join $;,$src,$contig,++$instance{$src,$contig};
}
# Tally the plus and minus strand total. We will flip the panel
# if more aligned sequence is on the minus strand than the plus strand.
# only count actual alignmnts (not aggregates).
my $panel_flip = $CONF->panel_flip($key);
if ($h->parts) {
for my $p (@{$h->parts}) {
$panel_flip->{$key}{yes} += ($p->tend - $p->tstart) if $p->tstrand eq '-';
$panel_flip->{$key}{no} += ($p->tend - $p->tstart) if $p->tstrand eq '+';
}
}
else {
$panel_flip->{$key}{yes} += ($h->tend - $h->tstart) if $h->tstrand eq '-';
$panel_flip->{$key}{no} += ($h->tend - $h->tstart) if $h->tstrand eq '+';
}
$span{$key}{start} = $h->tstart
if !defined $span{$key}{start} or $span{$key}{start} > $h->tstart;
$span{$key}{end} = $h->tend
if !defined $span{$key}{end} or $span{$key}{end} < $h->tend;
$contig =~ s/Superc/C/;
$span{$key}{src} ||= $src;
$span{$key}{contig} ||= $contig;
$span{$key}{tstart} ||= $h->start;
$span{$key}{tend} ||= $h->end;
($span{$key}{tstart}) = sort {$a<=>$b} ($h->start,$span{$key}{tstart});
($span{$key}{tend}) = sort {$b<=>$a} ($h->end,$span{$key}{tend});
$hit2span->{$h} = $key;
$hit2span->{$h->name} = $key;
}
# get rid of tiny spans
my $src_fraction = ($CONF->setting('min_alignment_size') || TOO_SMALL || 0) * $segment->length;
my @unused_hits;
unless ($CONF->page_settings("tiny")) {
for my $key (keys %span) {
my $too_short = ($span{$key}{end} - $span{$key}{start}) < $src_fraction
&& ($span{$key}{tend} - $span{$key}{tstart}) < $src_fraction;
if ($too_short) {
delete $span{$key};
push @unused_hits, grep { $hit2span->{$_} eq $key } @hits;
@hits = grep { $hit2span->{$_} ne $key } @hits;
}
}
}
# sort hits into upper and lower hits, based on this pattern:
#
# 1 3 5 7 upper spans
#
# - - - - - - - - - upper hits
#
# - - - - - - - - - lower hits
#
# 0 2 4 6 lower spans
#
my (%hit_positions);
my $pidx = 0;
my %species = map {$_->src2 => 1} @hits;
my %position = map {$_ => ++$pidx} sort keys %species;
for my $h (sort {($b->end-$b->start)<=>($a->end-$a->start)} @hits) {
my $src = $h->src2;
my $span_key = $hit2span->{$h};
$span{$span_key}{position} = $position{$src};
$hit_positions{$h} ||= $span{$span_key}{position};
}
# Create the middle (reference) panel
my ($upper_ff,$lower_ff,%is_upper);
# restrict segment to aligned sequence
my $only_aligned = 0;
if ($only_aligned && @hits) {
my @coords = map {$_->start, $_->end} @hits;
my $new_name = $segment->ref .':'.(min @coords).'..'.(max @coords);
$segment = landmark2segment($page_settings,$new_name,$CONF->page_settings('search_src'));
}
for my $h (@hits) {
my $src = $h->src2;
# keep track of the hits for mapping purposes
$is_upper{$src} ||= $hit_positions{$h} % 2;
my $ff = $is_upper{$src} ? \$upper_ff : \$lower_ff;
$$ff ||= Bio::Graphics::FeatureFile->new;
add_hit_to_ff($segment,$$ff,$h,$hit2span);
}
my $orphan_hit = 0;
for my $h (@unused_hits) {
warn "I am an orphan! ".$h->start."..".$h->end."\n" if DEBUG;
my $is_upper = ++$orphan_hit % 2;
my $ff = $is_upper ? \$upper_ff : \$lower_ff;
$$ff ||= Bio::Graphics::FeatureFile->new;
add_hit_to_ff($segment,$$ff,$h,$hit2span);
}
# base image width
my $width = $CONF->page_settings('imagewidth') || $CONF->setting("imagewidth") || IMAGE_WIDTH;
# but also allow for padding
my $ip = $SCONF->setting('image_padding') || 0;
my $pl = $SCONF->setting('pad_left') || 0;
my $pr = $SCONF->setting('pad_right') || 0;
my $padding = ($pl || $ip) + ($pr || $ip);
my ($ref_img,$ref_boxes) = segment2image($segment,
$src,
{
width => $width - $padding,
features_top => $upper_ff,
features_bottom => $lower_ff,
background => 'white',
}
) or die("no image");
$width = $ref_img->width;
my $ref_title = $MAP->{$src}{desc} . ' ' . format_segment($segment);
# pad all of the span coordinates for a bit of regional context
for my $key (keys %span) {
my $width = $span{$key}{end} - $span{$key}{start};
$span{$key}{start} -= int $width/20;
$span{$key}{end } += int $width/20;
}
# we now create panels and corresponding hit feature files for each of the small
# panels
my $refwidth = $width;
my $panel_count = keys %span;
my $panels_above = my @panels_above = grep { $span{$_}{position} % 2 } keys %span;
my $panels_below = my @panels_below = grep {!($span{$_}{position} % 2) } keys %span;
my $bases_above = sum( map {$span{$_}{end} - $span{$_}{start}} @panels_above );
my $bases_below = sum( map {$span{$_}{end} - $span{$_}{start}} @panels_below );
my ($pad_top,$pad_bottom) = (0,0);
my ($img,$boxes);
my $im_pad = $CONF->setting('interimage_pad') || INTERIMAGE_PAD;
for my $key (keys %span) {
my $panel_position = $span{$key}{position};
my $is_above = $panel_position % 2;
my $total_bases = $is_above ? $bases_above : $bases_below;
my $panels = $is_above ? $panels_above : $panels_below;
my $total_width = $refwidth - $panels*3*$im_pad + $im_pad;
my $src = $span{$key}{src};
my $contig = $span{$key}{contig};
my $end = $span{$key}{end};
my $start = $span{$key}{start};
my $bases = $end - $start;
my $name = "$contig:$start..$end";
my $segment = landmark2segment($page_settings,$name,$src);
my @relevant_hits = grep {$hit2span->{$_} eq $key} @hits;
my $rsegment = landmark2segment($page_settings,"$contig:$start..$end",$src);
# width of inset panels scaled by size of target sequence
$bases or next;
my $scale = $bases/$total_bases;
my $width = $total_width*$scale;
my $ff = Bio::Graphics::FeatureFile->new;
add_hit_to_ff($rsegment,$ff,$_,$hit2span,'invert') foreach @relevant_hits;
my $segment_args = {width => $width};
if ($is_above) {
$segment_args->{features_bottom} = $ff;
$segment_args->{noscale} = 1;
}
else {
$segment_args->{features_top} = $ff;
}
$segment_args->{flip}++ if panel_is_flipped($key,1);
($img,$boxes) = segment2image($segment, $src, $segment_args);
$img or next;
$span{$key}{image} = $img;
$span{$key}{boxes} = $boxes;
$span{$key}{title} = $MAP->{$src}{desc};
$span{$key}{title} .= ' (reverse)' if panel_is_flipped($key);
$pad_top = $img->height if $is_above && $pad_top < $img->height;
$pad_bottom = $img->height if !$is_above && $pad_bottom < $img->height;
}
# total height is height of reference + pad_top + pad_bottom + VERTICAL_PAD pixels of spacing
my $total_height = $ref_img->height + $pad_top + $pad_bottom;
my $vertical_pad = $CONF->setting('vertical_pad') || VERTICAL_PAD;
$total_height += $vertical_pad if $panels_above;
$total_height += $vertical_pad if $panels_below;
$total_height += $im_pad;
# create a master image for all panels
my $gd = GD::Image->new($ref_img->width+2*$im_pad,$total_height+1,1) or return;
$gd->saveAlpha(0);
$gd->alphaBlending(1);
my $white = $gd->colorAllocate(255,255,255);
my $black = $gd->colorAllocate(0,0,0);
my $cyan = $gd->colorAllocate(0,255,255);
$gd->filledRectangle(0,0,$gd->width,$gd->height,$white);
my $translucent = $gd->colorAllocateAlpha(0,0,255,90);
for my $key (keys %span) {
my $color = $CONF->setting($MAP->{$span{$key}{src}}{db}=>'color');
$span{$key}{tcolor} = $gd->colorAllocateAlpha(Bio::Graphics::Panel->color_name_to_rgb($color),110);
$span{$key}{bgcolor} = $gd->colorAllocateAlpha(Bio::Graphics::Panel->color_name_to_rgb($color),115);
$span{$key}{border} = $gd->colorResolve(Bio::Graphics::Panel->color_name_to_rgb($color));
}
my $ref_top = $panels_above ? $pad_top + $vertical_pad : $im_pad;
my $ref_bottom = $ref_top + $ref_img->height;
# paste the individual panels into the picture
my @map_items;
my %x = ( above => $im_pad, below => $im_pad);
# order the panels by hit order
my (@sorted_spans,%seen_span);
for my $h (sort {$a->start <=> $b->start} @hits) {
my $span = $hit2span->{$h};
push @sorted_spans, $span if ++$seen_span{$span} == 1;
}
my $max_height_above = max( map {eval{$span{$_}{image}->height}} grep {$span{$_}{position} % 2} @sorted_spans);
my $max_height_below = max( map {eval{$span{$_}{image}->height}} grep {! ($span{$_}{position} % 2)} @sorted_spans);
for my $key (@sorted_spans) {
my $is_above = $span{$key}{position} % 2;
my $img = $span{$key}{image} or next;
my $xi = $is_above ? 'above' : 'below';
my $max_height = $is_above ? $max_height_above : $max_height_below;
my $img_y = $is_above ? $ref_top-$vertical_pad-$img->height : $ref_bottom+$vertical_pad ;
my $msk_y = $is_above ? $ref_top-$vertical_pad-$max_height : $img_y;
$span{$key}{offsets} = [$x{$xi},$img_y];
my @rect = ($x{$xi},$msk_y,$x{$xi}+$img->width,$msk_y+$max_height);
$gd->copy($img,$x{$xi},$img_y,0,0,$img->width,$img->height);
$gd->filledRectangle(@rect,$span{$key}{bgcolor});
$gd->rectangle(@rect,$span{$key}{border});
$gd->string(GD::gdSmallFont,$x{$xi}+5,$msk_y,$span{$key}{title},$black)
if $img->width > 75;
$x{$xi} += $img->width + $im_pad;
my $name = "$span{$key}{contig}:$span{$key}{start}..$span{$key}{end}";
push @map_items,Area({shape=>'rect',
coords=>join(',',@rect),
href=>"?search_src=$span{$key}{src};name=$name",
title=>$span{$key}{title}});
}
# middle row (reference)
my @rect = ($im_pad,$ref_top,$im_pad+$ref_img->width,$ref_top+$ref_img->height);
$gd->copy($ref_img,$im_pad,$ref_top,0,0,$ref_img->width,$ref_img->height);
my $color = $CONF->setting($MAP->{$src}{db}=>'color');
$color ||= 'blue';
my $bgcolor = $gd->colorAllocateAlpha(Bio::Graphics::Panel->color_name_to_rgb($color),110);
my $border = $gd->colorResolve(Bio::Graphics::Panel->color_name_to_rgb($color));
$gd->filledRectangle(@rect,$bgcolor);
$gd->rectangle(@rect,$border);
my $title = $MAP->{$src}{desc} . ' (reference)';
$gd->string(GD::gdSmallFont,$rect[0]+5,$rect[1],$title,$black);
push @map_items,Area({shape=>'rect',
coords=>join(',',@rect),
title=>$ref_title});
# sort out the coordinates of all the hits so that we can join them
# first the hits in the reference panel
my (%ref_boxes,%panel_boxes);
for my $box (@$ref_boxes) {
ref $box or next;
my ($feature,$fname) = _type_from_box($box);
my @rect = ($im_pad+$box->[1],$ref_top+$box->[2],$im_pad+$box->[3],$ref_top+$box->[4]);
if ($feature =~ /^match(_part)?$/) {
$ref_boxes{$fname} = \@rect;
}
else {
my %atts = %{$box->[5]};
my $url = url();
$url =~ s/gbrowse_syn.*$//;
$atts{href} =~ s/\.\.\/\.\.\//$url/ if $atts{href};
push @map_items,Area({shape=>'rect',coords=>join(',',@rect),%atts});
}
$gd->rectangle(@rect,$black) if DEBUG;
}
# now for the hits in each individual panel
my %tcolors;
for my $key (keys %span) {
defined $span{$key}{offsets} or next;
my ($left,$top) = @{$span{$key}{offsets}};
my $boxes = $span{$key}{boxes};
for my $box (@$boxes) {
ref $box or next;
my ($feature,$fname) = _type_from_box($box);
#next unless $feature =~ /^match(_part)?$/;
my @rect = ($left+$box->[1],$top+$box->[2],$left+$box->[3],$top+$box->[4]);
if ($feature =~ /^match(_part)?$/) {
$panel_boxes{$fname} = \@rect;
}
else {
push @map_items, Area( {
shape => 'rect',
coords => join(',',@rect),
%{$box->[5]}});
}
$tcolors{$fname} = $span{$key}{tcolor};
$gd->rectangle(@rect,$black) if DEBUG;
}
}
my %grid_line;
my %gc;
$gc{1} = $CONF->page_settings("pgrid") ? $gd->colorResolveAlpha(10,10,10,100) : $gd->colorResolveAlpha(10,10,10,70);
$gc{3} = $gd->colorResolveAlpha(10,10,255,100);
my $thickness = 3;
my $grid_upper;
for my $feature (keys %ref_boxes) {
next unless defined $ref_boxes{$feature} && defined $panel_boxes{$feature};
my $hit = $CONF->name2hit($feature);
next unless defined $hit;
my $span = $hit2span->{$feature};
my $flip = !panel_is_flipped($span) && $CONF->flip($feature)
|| panel_is_flipped($span) && !$CONF->flip($feature);
my ($rx1,$ry1,$rx2,$ry2) = @{$ref_boxes{$feature}};
my ($px1,$py1,$px2,$py2) = @{$panel_boxes{$feature}};
my $upper = $py2 < $ry1;
if ($CONF->page_settings("shading")) {
my $poly = GD::Polygon->new();
$upper = $py2 < $ry1;
($rx1,$rx2) = ($rx2,$rx1) if $flip;
if ($upper) {
$grid_upper ||= $ry2;
$poly->addPt($px1,$py2);
$poly->addPt($px2,$py2);
$poly->addPt($rx2,$ry1);
$poly->addPt($rx1,$ry1);
}
else {
$poly->addPt($px1,$py1);
$poly->addPt($px2,$py1);
$poly->addPt($rx2,$ry2);
$poly->addPt($rx1,$ry2);
}
$gd->filledPolygon($poly,$tcolors{$feature});
}
}
my %within_a_pixel;
# reloop to avoid mysterious alpha-channel interaction
# between shading and grid-lines
for my $feature (keys %ref_boxes) {
next unless defined $ref_boxes{$feature} && defined $panel_boxes{$feature};
next if $feature =~ /aggregate/;
my $exact = $CONF->setting('grid coordinates');
$exact = $exact && $exact eq 'exact';
next unless my $hit = $CONF->name2hit($feature);
my $span = $hit2span->{$feature};
my $flip = !panel_is_flipped($span) && $CONF->flip($feature)
|| panel_is_flipped($span) && !$CONF->flip($feature);
my ($rx1,$ry1,$rx2,$ry2) = @{$ref_boxes{$feature}};
my ($px1,$py1,$px2,$py2) = @{$panel_boxes{$feature}};
my $upper = $py2 < $ry1;
my @grid_coords = $CONF->page_settings("pgrid")
? grid_coords( $hit,
$ref_boxes{$feature},
$panel_boxes{$feature},
panel_is_flipped($span),
$segment) : ();
# add edges
if ($CONF->page_settings("edge")) {
unshift @grid_coords, $flip ? [$rx1,$px2] : [$rx1,$px1];
push @grid_coords, $flip ? [$rx2,$px1] : [$rx2,$px2];
}
my $tidx = 0;
for my $pair (@grid_coords) {
my ($x1,$x2) = @$pair;
$grid_line{$x1}{thickness} = 1 if $exact;
unless ($grid_line{$x1}{thickness}) {
$thickness = ++$tidx == 5 ? 3 : 1;
$tidx = 0 if $tidx == 5;
}
else {
$thickness = $grid_line{$x1}{thickness};
}
$gd->setThickness($thickness);
if ($upper) {
$gd->line($x1,$ry1,$x2,$py2,$gc{$thickness});
$gd->line($x2,$py2,$x2,0,$gc{$thickness});
$grid_line{$x1}{top} ||= $ry2;
}
else {
my $pad = $im_pad;
$gd->line($x1,$ry2,$x2,$py1,$gc{$thickness});
$gd->line($x2,$py1,$x2,$gd->height-$pad-1,$gc{$thickness});
$grid_line{$x1}{bottom} ||= $ry1;
}
$grid_line{$x1}{thickness} ||= $thickness;
}
}
for my $g (keys %grid_line) {
# skip the line if there is one already drawn
# a pixel to the left or right;
next if $within_a_pixel{$g-1} || $within_a_pixel{$g+1} || $within_a_pixel{$g};
$within_a_pixel{$g} = 1;
my $thickness = $grid_line{$g}{thickness};
$gd->setThickness($thickness);
$grid_line{$g}{top} ||= $grid_upper;#$ref_top;
$grid_line{$g}{bottom} ||= $ref_bottom;
$gd->line($g,$grid_line{$g}{top},$g,$grid_line{$g}{bottom},$gc{$thickness});
}
my $url = $SCONF->generate_image($gd);
my $label = $CONF->page_settings->{display} eq 'compact' ? # all
'Details' : # or a sub-set
join(', ',@species);
my $map_name = md5_hex($label);
print toggle( $label,
table( {-width=>'100%'},
Tr( td( {-align=>'center', -class => 'databody'},
img({-src=>$url,-border=>0,-usemap=>'#'.$map_name} )))
),
);
my $map = Map({-name=>$map_name},reverse @map_items);
$map =~ s/\</\n\</g;
print $map;
}
sub segment2image {
my ($segment,$src,$options) = @_;
$segment or return;
my $width = $options->{width};
my $hits_top = $options->{features_top};
my $hits_bottom = $options->{features_bottom};
my $background = $options->{background} || 'white';
my $flip = $options->{flip};
my $dsn = $MAP->{$src}{db} or return;
$SCONF->source($dsn);
# make sure balloon tooltips are turned on
$SCONF->setting('GENERAL','balloon tips',1);
my @tracks = shellwords($CONF->setting($dsn => 'tracks'));
my $ff_scale = Bio::Graphics::FeatureFile->new;
$ff_scale->add_type( SCALE => { fgcolor => 'black',
glyph => 'arrow',
tick => 2,
double => 0,
description => 0} );
$ff_scale->add_feature(segment2feature($segment,$ff_scale));
my @labels;
if ($hits_top && $hits_bottom) {
@labels = ('ff_top','ff_scale',@tracks,'ff_bottom');
}
elsif ($hits_top) {
@labels = ('ff_top','zz_scale',@tracks);
}
elsif ($hits_bottom) {
@labels = (@tracks,'ff_scale','ff_bottom');
}
else {
@labels = ('ff_scale',@tracks);
}
my %ff_hash;
$ff_hash{ff_top} = $hits_top if $hits_top;
$ff_hash{ff_bottom} = $hits_bottom if $hits_bottom;
# coerce correct track order in bottom panel
my $scale_label = $hits_bottom && !$hits_bottom ? 'zz_scale' : 'ff_scale';
$ff_hash{$scale_label} = $ff_scale;
$SCONF->width($width);
my $im_pad = $CONF->setting('interimage_pad') || INTERIMAGE_PAD;
# padding must be temporarily overridden for inset panels
my ($pl,$pr);
unless ($src eq $CONF->search_src()) {
$pl = $SCONF->setting('pad_left');
$pr = $SCONF->setting('pad_right');
$SCONF->setting('general','pad_left' => $im_pad);
$SCONF->setting('general','pad_right' => $im_pad);
}
$SCONF->setting('general','detail bgcolor'=> $background);
my $title = $MAP->{$src}{desc};
# cache no panels if some params change
my $no_cache = [md5_hex(url(-query_string=>1))];
landmark2segment() if _isref($segment);
my ($img,$boxes) = $SCONF->render_panels(
{
drag_n_drop => 0,
image_and_map => 1,
keystyle => 'none',
segment => $segment,
labels => \@labels,
title => $title,
-grid => 0,
do_map => 1,
noscale => 1,
feature_files=>\%ff_hash,
-flip => $flip,
cache_extra => $no_cache,
}
);
# restore original padding
unless ($src eq $CONF->search_src()) {
$SCONF->setting('general','pad_left' => $pl);
$SCONF->setting('general','pad_right' => $pr);
}
return ($img,$boxes,$segment);
}
sub _isref {
my $segment = shift;
return $segment->ref eq $CONF->page_settings('ref') &&
$segment->start == $CONF->page_settings('start') &&
$segment->stop == $CONF->page_settings('stop');
}
sub format_segment {
my $seg = shift;
return $seg->ref.':'.$seg->start.'..'.$seg->end;
}
sub landmark2segment {
my $settings = shift || $CONF->page_settings;
my ($name,$source) = @_;
if ($name && $source) {
}
elsif ($CONF->page_settings("name")) {
$name = $CONF->page_settings("name");
}
elsif ($CONF->page_settings("ref")) {
$name = $CONF->page_settings("ref") . ':';
$name .= $CONF->page_settings("start") . ':';
$name .= $CONF->page_settings("stop");
}
$source ||= $CONF->page_settings("search_src");
my $segment = _do_search($name,$source) if $name && $source;
# Did not find our segment? Try the other species
if (!$segment) {
for my $src (grep {defined $_} @{$settings->{species}}) {
next if $src eq $source;
$segment = _do_search($name,$src) if $name;
if ($segment) {
$settings->{search_src} = $src;
$CONF->search_src($src);
last;
}
}
}
# remember our search!
if ($segment && $source && $source eq $settings->{search_src}) {
$settings->{"ref"} = $segment->refseq;
$settings->{"start"} = $segment->start;
$settings->{"stop"} = $segment->end;
$settings->{"name"} = format_segment($segment);
}
return $segment;
}
sub _do_search {
my ($landmark,$src) = @_;
my $dsn = $MAP->{$src}{db} or return undef;
$SCONF->source($dsn);
my $db = open_database();
my ($segment) = grep {$_} $SCONF->name2segments($landmark,$db);
return $segment;
}
sub warning {
h2({-style=>'color:red'},@_);
}
sub add_hit_to_ff {
my ($segment,$ff,$hit,$hit2span,$invert) = @_;
my $flip_hit = $hit->tstrand eq '-';
$CONF->flip($hit->name => 1) if $flip_hit;
my @attributes = $flip_hit ? (-attributes => {flip=>1}) : ();
my $src = $hit->src2;
my $setcolor = $CONF->setting($MAP->{$src}->{db} => 'color');
$ff->add_type(
"match:$src" => {
bgcolor => $setcolor,
fgcolor => $setcolor,
height => $CONF->setting('align_height') || ALIGN_HEIGHT,
glyph => 'segments',
label => 0,
box_subparts => 1,
connector => 'dashed',
},
);
my $seqid = $invert ? $hit->target : $hit->seqid;
my $strand = $invert ? $hit->tstrand : $hit->strand;
my $start = $invert ? $hit->tstart : $hit->start;
my $end = $invert ? $hit->tend : $hit->end;
my $feature = Bio::Graphics::Feature->new(
-source => $src,
-type => 'match',
-name => $hit->name,
-seq_id => $seqid,
-strand => $strand eq '-' ? -1 : 1,
-configurator => $ff
);
my $parts = $CONF->{parts}->{$hit->name};
if ($parts) {
for my $part (@$parts) {
$CONF->flip($part->name => 1) if $flip_hit;
$hit2span->{$part->name} = $hit2span->{$hit};
$CONF->name2hit($part->name => $part);
my $start = $invert ? $part->tstart : $part->start;
my $end = $invert ? $part->tend : $part->end;
my $subfeat = Bio::Graphics::Feature->new(
-type => 'match_part',
-name => $part->name,
-start => $start,
-end => $end,
-strand => $strand
);
$feature->add_segment($subfeat,'EXPAND');
}
}
else {
$feature->start($start);
$feature->end($end);
}
$ff->add_feature($feature);
}
sub adjust_container {
my $h = shift;
if ($h->parts) {
my @coords = map {$_->tstart,$_->tend} @{$h->parts};
my ($max,$min) = (max(@coords),min(@coords));
$h->tstart($min) if $h->tstart != $min;
$h->tend($max) if $h->tend != $max;
@coords = map {$_->start,$_->end} @{$h->parts};
($max,$min) = (max(@coords),min(@coords));
$h->start($min) if $h->start != $min;
$h->end($max) if $h->end != $max;
}
return $h;
}
sub navigation_table {
my $segment = shift;
my @species = @_;
my ($table,$whole_segment);
my $slidertable = '';
if ($segment) {
my $whole_segment = $CONF->whole_segment();
my ($label) = $CONF->tr('Scroll');
$slidertable = $CONF->slidertable;
}
elsif (my $name = $CONF->page_settings('name')) {