/
SeqCanvas.pm
3021 lines (2522 loc) · 125 KB
/
SeqCanvas.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
# $Id$
=head1 NAME
Bio::Tk::SeqCanvas.pm - (v3.0) Graphical display of SeqI objects
=head1 SYNOPSIS
# To create a BioSeq map and return a handle to the map object:
use Tk;
use Bio::SeqIO;
use Bio::Tk::SeqCanvas;
Begin();
MainLoop;
sub Begin {
# set up the Tk Windows
my $MW = MainWindow->new (-title => "Map Of BioSeq Object");
my $Frame = $MW->Frame()->pack(-side => 'top');
my $lblSysMess = $MW->Label()->pack(-side => 'bottom', -fill => 'both');
# create a BioSeq object
# (note: the test file used below is in the /t/ folder)
my $SIO = Bio::SeqIO->new(-file=> 'testseq.gb', -format => 'genbank');
my $SeqObj = $SIO->next_seq();
# Draw the Map
my $axis_length = 800; # how large (long axis) I want the final map to be
my $MapObj = Bio::Tk::SeqCanvas->new(
$axis_length,
$Frame,
$lblSysMess,
$SeqObj,
-orientation => 'horizontal',
label => 'primary_tag',
width => 200,
);
}
=head1 DESCRIPTION
Creates an interactive scalable/zoomable map of all features and
subfeatures of Bio::SeqI compliant and GeneStructureI compliant objects.
Selecting single and multiple map objects is handled in the object
itself: left-mouse click to select, SHIFT-left-mouse to select
multiple. All other Tk Events are passed back up to the MainWindow
object and can be trapped/bound by the user as they see fit. Colors
and axis-offsets of mapped objects are assigned based on the "source"
tag of SeqFeature::Generic objects, and by the ordinal transcript number
of Gene:;TranscriptI compliant objects. These are assigned "on the fly" based on
whatever is contained in the BioSeq object provided. The maps re-size
on the fly to accomodate newly added features.
=head1 AUTHORS
Mark Wilkinson (mwilkinson@gene.pbi.nrc.ca),
David Block (dblock@gnf.org)
=head1 ACKNOWLEDGEMENTS
This module requires an updated version of Gregg Helt's original
BioTkPerl modules (version 0.82) which are available from BioPerl. The
original BioTkPerl (version 0.80) is Copyright (c) Gregg Helt, 1995;
Version 0.82 was generated by Mark Wilkinson, PBI-NRC, May, 2001.
Zooming routines/events in this module are conceptually based on the
Zoom routines from Genotator (Copyright (c) 1996, The Regents of the
University of California. Harris, N.L. (1997), Genome Research
7 (7):754-762)
=head1 DISCLAIMER
Anyone who intends to use and uses this software and code acknowledges and
agrees to the following: The National Research Council of Canada (herein "NRC")
disclaims any warranties, expressed, implied, or statutory, of any kind or
nature with respect to the software, including without limitation any warranty
or merchantability or fitness for a particular purpose. NRC shall not be liable
in any event for any damages, whether direct or indirect,
consequential or incidental, arising from the use of the software.
=head1 NEW FEATURES IN THIS VERSION
=head2 Re-casting of SeqFeature types
During editing you may map numerous features of the type SeqFeature::Generic
or other non-GeneStructureI compliant feature type. Since the Finished canvas
accepts only GeneStructureI objects, it is now possible to "re-cast"
features on the draft canvas into any of the GeneStructureI features types (Exon,
Intron, UTR, PolyA, Promoter, NC_Feature). This is accomplished by selecting the
features you wish to be re-cast, right-mouse-clicking on the canvas, and selecting
the new cast from the drop-down menu. Internally a new object of the selected type is
created from the GFF2 string of the original feature. This is added to the Seq object,
the original object is deleted from the Seq object, the old widget unmapped and the new
widget mapped. (Note: each widget has a unique WidgetID, but may point to the same
underlying feature. Thus in the case of multiple transcripts, the same SeqFeature object
may be represented by multiple Widgets with unique WidgetID's)
=head2 Live Sequence Object Editing and Retrieval
All events happening on the canvas are fed back through to the SequenceI object
that was used to initialize the SeqCanvas. Thus at any given moment the Seq object
can be retrieved using:
$SeqObj = $SeqCanvas->MapSeq;
and the structure of the object you obtain is precisely (I believe) what is currently visible
on the canvas. Thus, if you should ever wish to do so, SeqCanvas could be used as
a 'sanity checkpoint' in a pipeline, where an annotator can view the data, make
any obvious modifications, and then send it farther along the pipe. N.B. I haven't
tested this thoroughly, so don't take it as gospel until you have done this a few
times.
=head2 Drag-n-Drop editing
This version of SeqCanvas supports drag-n-drop for the creation of:
=over
=item *
New genes
=item *
New gene transcripts
=item *
addition of features to existing transcripts
=back
The interface is straightforward - simply select one or more features on the Draft canvas
and drag them onto the Finished canvas. If you drag them to an empty spot, you will create
a new gene. If you drag them onto an existing gene, you will create a new transcript for that
gene. If you drag them onto an existing transcript you will add them to that transcript.
=head1 WIDGET TAGS
Each map-widget has several "reliable" tags attached to it. These tags
are FIDxxxx, Source and Strand, Type, and Canvas, where:
=over
=item *
FIDxxxx is the unique identifier for that particular map-widget over all maps (even in multiple windows)
=item *
Source is derived from the "source" tag of the SeqFeature object this widget represents
=item *
Strand is derived from the "strand" tag of the SeqFeature object, converted into the GFF standard of +/-/. to represent the three possible strand values.
=item *
Type isthe feature type, derived from the primary_tag of the SeqFeature object
=item *
Map is either 'draft' or 'finished' to represent an object on the white or blue maps respectively
=back
So for example, a map widget might have the tags :
FID22354 (no space)
Source GeneMarkHMM (single space separated)
Strand + (single space separated)
Type exon ( " )
Canvas draft ( " )
If your BioSeq Features are being derived from an external database,
it is possible to also include the unique index number of that
database entry as a fourth tag on the associated map-widget. To do so,
create your SeqFeature objects with an additional tag "id", where the
unique databse index number is the value of this tag. This index
number is then attached to the widget as a fourth tag with the form:
DB_ID xxxxx (x's represent the unique index value)
The values of these three/four tags can be retrieved for any selected
object using the getSelectedTags function (see below) in order to
relate mapped objects back to their original database entries. Using
the selectWithTag or recolorWithTag routines (see below) requires that
you pass the **full tag** as the desired selection (eg. pass "Source
GeneMarkHMM" not just "GeneMarkHMM")
=cut
=head1 CODE EXAMPLES: Adding/Binding Features
SeqCanvas is a dynamic map, allowing features to be added or removed
after the object has been created. In addition, events occurring on
this canvas can be externally bound and assigned to subroutines to
allow the canvas to feed information out to an external program for
further processing.
Some code examples are below; these can be added into the Begin()
subroutine in the synopsis to see how they work:
# MAPPING SIMPLE GENERIC FEATURES
#________________________________
# make six arbitrary features and map them
# if this is added into the Synopsis routine
# you will see that these features are labelled
# using their "author" tag values
my $x = 1;
while ($x < 6){
my $feat = new Bio::SeqFeature::Generic (
-start => 5000*$x,
-end => 6000*$x,
-strand => -1,
-primary => 'Unusual_feature',
-source => "example$x",
-score => 1000,
-tag => {new => 1,
author => 'someone_$x',
sillytag => 'this is silly!'}
);
my ($FID) = $MapObj->mapFeatures(undef, [$feat]);
++$x;
}
# MAPPING GeneStructureI COMPLIANT FEATURES
#__________________________________________
# first create the feature
use Bio::SeqFeature::Gene::Exon;
use Bio::SeqFeature::Gene::Transcript;
use Bio::SeqFeature::Gene::GeneStructure;
use Bio::SeqFeature::Generic;
my $exon1 = new Bio::SeqFeature::Gene::Exon (-start => 3300, -end => 4000, -primary => "exon", -source => "genscan", -strand => -1);
my $exon2 = new Bio::SeqFeature::Gene::Exon (-start => 4400, -end => 6000, -primary => "exon", -source => "genscan", -strand => -1);
my $exon3 = new Bio::SeqFeature::Gene::Exon (-start => 3000, -end => 4000, -primary => "exon", -source => "genemark", -strand => -1);
my $exon4 = new Bio::SeqFeature::Gene::Exon (-start => 5000, -end => 6000, -primary => "exon", -source => "genemark", -strand => -1);
my $polyA = Bio::SeqFeature::Generic->new(-start => 2500, -end => 2800, -primary=> "polyA", -source => "polyA-scan", -strand => -1);
my $prom = Bio::SeqFeature::Generic->new(-start => 6200, -end => 6500, -primary=> "promoter", -source => "prom-find", -strand => -1);
my $transcript = Bio::SeqFeature::Gene::Transcript->new(-start => 2500, -end => 6500, -primary => "transcript", -source => "transcript", -strand => -1);
my $transcript2 = Bio::SeqFeature::Gene::Transcript->new(-start => 2500, -end => 6500, -primary => "transcript", -source => "transcript", -strand => -1);
$transcript->add_promoter($prom);
$transcript->add_exon($exon1, 'initial');
$transcript->add_exon($exon2, 'terminal');
$transcript->poly_A_site($polyA);
$transcript->source_tag("cDNA_evidence");
$transcript2->add_promoter($prom);
$transcript2->add_exon($exon3, 'initial');
$transcript2->add_exon($exon4, 'terminal');
$transcript2->poly_A_site($polyA);
$transcript2->source_tag("EST_evidence");
my $Gene = Bio::SeqFeature::Gene::GeneStructure->new(-start => 2500, -end => 6500, -strand => -1, -primary => "gene", -source => "mark");
$Gene->add_transcript($transcript);
$Gene->add_transcript($transcript2);
#$Gene->add_transcript($transcript8);
$SeqObj->add_SeqFeature($Gene);
my ($FID) = $MapObj->mapFeatures(undef, [$Gene]);
# BINDING EVENTS
#_______________
# usually you will want to bind events to the
# MainWindow ($MW) object.
#
$MW->bind("<Button-1>" => sub {
my ($FID, $strand, $source, $type, $canvas, $DB_ID) = $MapObj->getSelectedTags;
print "Feature ID = $FID\n";
print "Source = $source\n";
print "Primary_tag = $type\n";
print "Strand = $strand\n\n";
});
$MW->bind("<Motion>" => sub {
my @FIDs = @{$MapObj->getIDsWithTag(["Mouse_over"])};
# do whatever here, for example:
# foreach my $ID(@FIDs){print "id=$ID\n"}
});
# one useful binding would be to retrieve the current Seq object
# after an editing session. The code below binds the "n" key
# to retrieve the Seq object from the current SeqCanvas window and create a
# new SeqCanvas window with it.
$MW->bind("<Key-n>" => sub {
my $MW = MainWindow->new (-title => "Map Of BioSeq Object");
my $Frame = $MW->Frame()->pack(-side => 'top');
my $lblSysMess = $MW->Label()->pack(-side => 'bottom', -fill => 'both');
my $axis_length = 800; # how large I want the final map to be
my $MapObj2 = Bio::Tk::SeqCanvas->new(
$axis_length,
$Frame,
$lblSysMess,
$MapObj->MapSeq, # Here we are getting the up-to-date Seq object
-orientation => 'horizontal',
label => 'author',
width => 300,
);
});
=cut
package Bio::Tk::SeqCanvas;
use Bio::Tk::SeqCanvasFeature;
use strict;
use Tk;
use Tk::DragDrop;
use Tk::DropSite;
use Carp;
use Bio::Tk::AnnotMap;
use Tk::widgets qw(ColorEditor Dialog);
use Bio::SeqI;
use Bio::SeqIO;
#use Bio::SeqFeature::Gene::Exon; deprecated
use Bio::SeqFeature::Gene::Exon;
use Bio::SeqFeature::Gene::Transcript;
use Bio::SeqFeature::Gene::GeneStructure;
use vars qw(@ISA $AUTOLOAD);
require Bio::Tk::BioTkPerl8_2; # requires version 0.82 of Gregg Helt's Bio::TkPerl (can be obtained from BioPerl website)
# note that this is NOT the version available directly from Berkeley!!
Tk::Widget->Construct('SeqCanvas');
$Bio::Tk::SeqCanvas::VERSION='3.0';
{
#Encapsulated class data
#Encapsulated class data for a SeqCanvas
my %colordef = (
magenta => '#ee00ee',
fuschia => '#ff00cc',
red => '#ff1100',
pink => '#ffdddd',
orange => '#ffaa00',
yellow => '#eed007',
purple => '#bb00ff',
darkblue => '#3300ee',
lightblue => '#99bbee',
turquoise => '#00ddcc',
green => '#11dd11',
chartreuse => '#aacc00',
yellowgreen => '#669900',
black => '#000000',
brown => '#994444',
dkgreen => '#00aa00',
ltgreen => '#aaeeaa',
);
my @colorlist = qw(darkblue yellowgreen fuschia orange purple dkgreen chartreuse lightblue magenta turquoise green yellow brown ltgreen);
my $dxa;
my $dya;
my $dxb;
my $dyb;
sub dxa {
my ($self, $newval) = @_;
if ($newval){$dxa = $newval}
$dxa=0 unless $dxa;
return $dxa;
}
sub dya {
my ($self, $newval) = @_;
if ($newval){$dya = $newval}
$dya=0 unless $dya;
return $dya;
}
sub dxb {
my ($self, $newval) = @_;
if ($newval){$dxb = $newval}
$dxb=0 unless $dxb;
return $dxb;
}
sub dyb {
my ($self, $newval) = @_;
if ($newval){$dyb = $newval}
$dyb=0 unless $dyb;
return $dyb;
}
#___________________________________________________________
#ATTRIBUTES
my %_attr_data = # DEFAULT ACCESSIBILITY
(
#dxa => [0, 'read/write'], # x/y coords of the draft (d) and finished (f) canvases
#dya => [0, 'read/write'],
#dxb => [0, 'read/write'],
#dyb => [0, 'read/write'],
fxa => [0, 'read/write'],
fya => [0, 'read/write'],
fxb => [0, 'read/write'],
fyb => [0, 'read/write'],
-axis_loc => [0, 'read/write'],
-labelfont => ['TimesNewRoman 9 normal', 'read/write'],
-range => [undef, 'read/write'],
label => [undef, 'read/write'], # if this is defined then this is the Feature tag used to write labels on mapped objects
ScrollBar => [undef, 'read/write'],
ZoomBar => [undef, 'read/write'],
FinishedMap => [undef, 'read/write'],
DraftMap => [undef, 'read/write'],
MapSeq => [undef, 'read/write'],
MapFrame => [undef, 'read/write'],
SeqFrame => [undef, 'read/write'], # the frame to hold the sequence display
#SeqText => [undef, 'read/write'], # the sequence display text box
ZoomFrame => [undef, 'read/write'],
ScrollFrame => [undef, 'read/write'],
DraftCanvas => [undef, 'read/write'],
FinishedCanvas => [undef, 'read/write'],
AnnotTextFrame => [undef, 'read/write'], # as below
AnnotTextCanvas => [undef, 'read/write'], # this is not used directly in SeqCanvas, but can be used by external routines to generate a third frame containing textual information beside the annotatinos (a la AceDB)
AnnotTextMap => [undef, 'read/write'], # as above
DraftLabelCanvas => [undef, 'read/write'],
FinishedLabelCanvas => [undef, 'read/write'],
InitialFinishedLabels=> [['gene'], 'read/write'],
InitialSources => [['hand_annotation'], 'read/write'],
BioPerlFeatureTypes => [{
"Gene" => "Bio::SeqFeature::Gene::GeneStructure",
"Transcript" => "Bio::SeqFeature::Gene::Transcript",
"Exon" => "Bio::SeqFeature::Gene::Exon",
"Intron" => "Bio::SeqFeature::Gene::Intron",
"Promoter" => "Bio::SeqFeature::Gene::Promoter",
"Poly_A_site" => "Bio::SeqFeature::Gene::Poly_A_site",
"UTR" => "Bio::SeqFeature::Gene::UTR",
"Non-Coding" => "Bio::SeqFeature::Gene::NC_Feature",
}, 'read/write'],
Menu => [undef, 'read/write'], # the menu that contains "Re-Cast Selected As", to allow additions from outside of the module
ReCastMenu => [undef, 'read/write'], # the menu that is cascaded by selecting Re-CastSelected As" to allow re-configuration of callbacks from outside if you are not using generic BioPerl feature objects
Colors => [{}, 'read/write'], # the colors associated with each source $Colors{$source} = "color"; Class property
colordefs => [\%colordef, 'read/write'],
colorlist => [\@colorlist, 'read/write'],
current_offsets => [{}, 'read/write'],
zoom_triggers => [{}, 'read/write'],
min_zoom => [1, 'read/write'],
max_zoom => [2, 'read/write'],
zoom_ratio => [1, 'read/write'],
zoom_level => [0, 'read/write'],
current_loc => [1, 'read/write'],
finished_total_offset=> [undef, 'read/write'], # the largest offset for the finished map
draft_total_offset => [undef, 'read/write'], # the largest offset for the draft map
width => [200, 'read/write'], # the "width" (perpendicular to the axis) of the maps at the outset
-orientation => [undef, 'read/write'],
whitespace => [10, 'read'], # whitespace is the distance between the axis and the first widget; the default never changes
SysMess => [undef, 'read/write'], # this is an (optional) handle back out to a label on the top level window to send system messages
dragx1 => [undef, 'read/write'],
dragy1 => [undef, 'read/write'],
dragx2 => [undef, 'read/write'],
dragy2 => [undef, 'read/write'],
def_offset => [10, 'read/write'],
_activeDelete => ["off", 'read/write'], # does pressing the delete key delete the selected features?
DropHighlighted => [undef, 'read/write'], # this gets set during a drag-n-drop motion event, it holds the FID of a currently mouse-drag-over feature
);
my $_nextDoffset=0;
my $_nextFoffset=0;
# note that these are encapsulated CLASS properties
my %colors; # and thus are constant from one instantiation to the next
my $_color_pos = 0;
my @FinishedSourceLabels;
my @Sources;
my %AllFeatures; # this is the list of features mapped onto the canvas, key is the stringified feature hash address, value is the feature object
#_____________________________________________________________
#METHODS, to operate on encapsulated class data
# Is a specified object attribute accessible in a given mode
sub _accessible {
my ($self, $attr, $mode) = @_;
return 0 unless ($_attr_data{$attr}[1] && $mode);
$_attr_data{$attr}[1] =~ /$mode/
}
# Classwide default value for a specified object attribute
sub _default_for {
my ($self, $attr) = @_;
$_attr_data{$attr}[0];
}
# List of names of all specified object attributes
sub _standard_keys {
keys %_attr_data;
}
sub FinishedSources { # for backwards compatibility
return \@FinishedSourceLabels;
}
sub DraftSources { # for backwards compatibility
return \@Sources;
}
sub AllFeatures {
# this is the hash of the unique FeatureID and associated SeqCanvasFeature value.
# can be called three ways:
#->AllFeatures($FID, $SCF) sets the value
#->AllFeatures($FID) returns the single value
#->AllFeatures() returns all values
my ($self, $FID, $SeqCanvasFeature) = @_;
if ($SeqCanvasFeature){
$AllFeatures{$FID} = $SeqCanvasFeature;
return 1;
} # the stringified address of the hash is the key, the feature is the value
if ($FID){
return $AllFeatures{$FID}
}
return \%AllFeatures
}
sub FinishedSourceLabels { # this is a get/set
my ($self, @labels) = @_;
if (scalar @labels){@FinishedSourceLabels = @labels}
return @FinishedSourceLabels;
}
sub addFinishedSourceLabel {
my ($self, @labels) = @_;
my %sourcehash;
foreach my $source(@FinishedSourceLabels){$sourcehash{$source} = 1}
foreach my $new(@labels){$sourcehash{$new} = 1}
@FinishedSourceLabels = (keys %sourcehash);
return @FinishedSourceLabels;
}
sub Sources {
my ($self, @sources) = @_;
if (scalar @sources){@Sources = @sources}
return @Sources;
}
sub addSource {
my ($self, @new_sources) = @_;
my %sourcehash;
foreach my $source(@Sources){$sourcehash{$source} = 1}
foreach my $new(@new_sources){$sourcehash{$new} = 1}
@Sources = (keys %sourcehash);
return @Sources;
}
sub colorlist {
return @colorlist
}
sub colordef {
return %colordef
}
sub MapArgs {
my ($self) = @_;
my %_map_args;
foreach my $key ($self->_standard_keys) {
if ($key =~ /^-/) {
$_map_args{$key} = $self->{$key};
}
}
return %_map_args;
}
#sub next_id {
# return $_nextid++;
#}
my $ori;
sub ori { # the orientation of the canvas has been set... horizontal or vertical.
my ($self, $val) = @_;
$ori = $val if $val;
return $ori;
}
sub next_draft_offset {
if (!$_nextDoffset){$_nextDoffset=2}
return $_nextDoffset++; # in this case increment it
}
sub next_finished_offset {
if (!$_nextFoffset){$_nextFoffset=2}
return $_nextFoffset++; # in this case increment it
}
sub draft_offset_pointer {
if (!$_nextDoffset){$_nextDoffset = 2}
return $_nextDoffset; # in this case just send it as it is
}
sub finished_offset_pointer {
if (!$_nextFoffset){$_nextFoffset = 2}
return $_nextFoffset; # in this case just send it as it is
}
#sub current_offsets {
# return \%offsets;
#}
sub current_colors {
return \%colors;
}
sub next_colorpos {
my $pos = $_color_pos;
++$_color_pos;
if (!$colorlist[$_color_pos]){$_color_pos = 0} # if we are beyond the end of the colorlist then return the pointer to zero
return $pos;
}
}
sub Tk::Error {
Tk->break;
}
sub subseq {
my ($self,@args)=@_;
return $self->MapSeq->subseq(@args);
}
sub start {return $_[0]->MapSeq->start; }
sub end {return $_[0]->MapSeq->end; }
sub DESTROY { }
sub localDestroy {
my ($self) = @_;
if ($self->Menu){eval {$self->Menu->destroy}}
if ($self->ReCastMenu){eval {$self->ReCastMenu->destroy}}
eval {$self->FinishedCanvas->destroy};
eval {$self->DraftCanvas->destroy};
eval {$self->destroy};
}
sub AUTOLOAD {
no strict "refs";
my ($self, $newval) = @_;
$AUTOLOAD =~ /.*::(\w+)/;
my $attr=$1;
if ($self->_accessible($attr,'write')) {
*{$AUTOLOAD} = sub {
if ($_[1]) { $_[0]->{$attr} = $_[1] }
return $_[0]->{$attr};
}; ### end of created subroutine
### this is called first time only
if ($newval) {
$self->{$attr} = $newval
}
return $self->{$attr};
} elsif ($self->_accessible($attr,'read')) {
*{$AUTOLOAD} = sub {
return $_[0]->{$attr} }; ### end of created subroutine
return $self->{$attr} }
# Must have been a mistake then...
croak "No such method: $AUTOLOAD";
}
#__________________________________________________________________________________
# Object Methods
#__________________________________________________________________________________
=head1 Methods
=cut
=head2 new
Title : new
Usage : $MapObj= SeqCanvas->new(
$axis_length,
$Frame,
[$lblSysMess | undef],
$SeqObj,
-orientation => ['horizontal'|'vertical']
[, label => $tag])
Function : create a map from the Feature object provided
Returns : Handle to the Map object
Args :
axis_length in pixels,
a Tk::Frame object,
a Tk::Label or undef,
a BioSeqI compliant object,
the orientation for the map,
optionally the SeqFeature tag you wish to
use as the label
=cut
sub new {
# returns object reference for success
# returns -1 for failed initiation - no $SeqObj supplied
# returns -2 for wrong object type - must be (ISA) SeqI Sequence object
# returns -3 sequence has length 0
# returns -4 if orientation is uninterpretable
# returns -5 if supplied frame object is not a TK::frame
# the reference to TOP in the next line is the top-level TK window.
# if this is passed as 'defined' then a Tk::Label object with the name
# lblSysMess ***MUST*** exist in this window to receive output
# messages from this module. If $TOP is undefined this feature is disabled.
my ($caller, $window_length, $frame, $TOP, $SeqObj, %args) = @_;
my $caller_is_obj = ref($caller);
my $class = $caller_is_obj || $caller;
# check incoming data for validity
if (!$SeqObj){return -1}
if (!$SeqObj->isa("Bio::SeqI")) {return -2}
if ($SeqObj->length == 0) {return -3}
if (!$frame->isa("Tk::Frame")){return -5}
#Create Object
my $self = bless {}, $class;
if ($args{-orientation} =~ /h/i) {$self->{-orientation} = "horizontal"; $self->ori("horizontal")}
elsif ($args{-orientation} =~ /v/i) {$self->{-orientation} = "vertical"; $self->ori("vertical")}
else {return -4}
delete $args{-orientation};
foreach my $attrname ( $self->_standard_keys ) {
next if $attrname eq "-orientation";
if (exists $args{$attrname}) {
$self->{$attrname} = $args{$attrname} }
elsif ($caller_is_obj) {
$self->{$attrname} = $caller->{$attrname} }
else {
$self->{$attrname} = $self->_default_for($attrname) }
}
$self->SysMess($TOP); # a handle out to the top-level window system for passing messages
# the sub-frame to hold the zoom-bar
$self->ZoomFrame($frame->Frame->pack(-side => 'bottom', -fill => 'x'));
if ($self->{-orientation} eq "horizontal"){
#$self->SeqFrame($frame->Frame->pack(-side => 'top', expand => 1, -fill => 'x')); # the frame to hold the sequence text
$self->ScrollFrame($frame->Frame->pack(-side => 'bottom', -fill => 'x'));
} else {
#$self->SeqFrame($frame->Frame->pack(-side => 'left', expand => 1, -fill => 'y')); # the frame to hold the sequence text
$self->ScrollFrame($frame->Frame->pack(-side => 'right', -fill => 'y'));
}
$self->MapFrame($frame->Frame->pack(-side => 'top', -expand => 1, -fill => "both")); # the sub-frame to hold the two maps
$self->MapSeq($SeqObj); # SeqCanvas contains the sequence object
# *******************************
# - This line ensures that the sequences fills the allocated space.
$self->{-range} = [0, ($SeqObj->length)];
# within these routines the features are counted and assigned
# colors and offsets from the map axis. the width of each is
# thus double (plus strand and minus strand) the largest axis
# offset of a feature the largest offset value is stored in
# $self->total_offset
# ************************************************************
# ***********************************************************
# Create the MapCanvases with correct dimensions
my $map_width;
# remember, $self->dxa/dya are *class* variables, not instance varaibles!
$map_width = (2* ($self->whitespace + ($self->draft_offset_pointer*$self->def_offset)));
if ($map_width < 200){$map_width = 200}
if ($self->{-orientation} eq "horizontal") {
# the SeqText widget breaks MS-Windows, and is of questionable value anyway...
# I have removed it, but if you are running *nix and create only horizontal
# maps you are free to uncomment these lines, as well as the reference in the
# initializing hash at the top of the code and any other lines that
# make reference to ->SeqText to make it appear again.
#$self->SeqText($self->SeqFrame->Scrolled("Text", -scrollbars => "s", -height => 3, -background => 'black', -foreground => "white", -wrap => 'none')->pack(-expand => 1, -fill => 'both')); # text box for teh sequence
#$self->SeqText->insert('end', "\n");
#$self->SeqText->insert('end', $SeqObj->seq);
$self->dya(-$map_width/2); # each map is equally distributed
$self->fya(-$map_width/2); # each map is equally distributed
$self->dyb($map_width/2); # around the zero axis
$self->fyb($map_width/2); # around the zero axis
$self->{-axis_loc} = $map_width/2; # axis goes half-way (this is a strange bug in AnnotMap... even if you specify that the map is -100 to +100, you can't set the axis at 0, you have to set it at +100 to put it in the middle of this 200 range....
$self->dxa(1);
$self->fxa(1);
$self->dxb($window_length);# height is unchanged
$self->fxb($window_length);# height is unchanged
my $DLF = $self->MapFrame->Frame->pack(-side => 'top', -fill => 'both', -expand => 1); # frame for Draft map and labels
my $FLF = $self->MapFrame->Frame->pack(-side => 'top', -fill => 'both', -expand => 1); # frame for Finished map and labels
$self->DraftLabelCanvas($DLF->Canvas(-width => 100, -height => $map_width, -background => "#ffffff")->pack(-side => 'left', -fill => 'both', -expand => 1));
$self->DraftCanvas($DLF->Canvas(-width => $window_length, -height => $map_width, -background => "#ffffff")->pack(-side => 'left', -fill => 'both', -expand => 1));
my $Dyscrollbar = $DLF->Scrollbar('-orient' => 'vertical','-command' => sub {$self->DraftLabelCanvas->yview(@_);$self->DraftCanvas->yview(@_)})->pack(-side => 'left', '-fill'=>'y', '-expand' => 1);
$self->FinishedLabelCanvas($FLF->Canvas(-width => 100, -height => $map_width, -background => "#eeeeff")->pack(-side => 'left', -fill => 'both', -expand => 1));
$self->FinishedCanvas($FLF->Canvas(-width => $window_length, -height => $map_width, -background => "#eeeeff")->pack(-side => 'left', -fill => 'both', -expand => 1));
my $Fyscrollbar = $FLF->Scrollbar('-orient' => 'vertical','-command' => sub {$self->FinishedLabelCanvas->yview(@_);$self->FinishedCanvas->yview(@_)})->pack(-side => 'left', '-fill'=>'y', '-expand' => 1);
$self->DraftLabelCanvas->configure('-yscrollcommand' => ['set' => $Dyscrollbar] );
$self->FinishedLabelCanvas->configure('-yscrollcommand' => ['set' => $Fyscrollbar] );
$self->DraftCanvas->configure('-yscrollcommand' => ['set' => $Dyscrollbar] );
$self->FinishedCanvas->configure('-yscrollcommand' => ['set' => $Fyscrollbar] );
$self->DraftCanvas->configure(-scrollregion => [1, $self->dya, $self->dxb, $self->dyb]);
$self->FinishedCanvas->configure(-scrollregion => [1, $self->fya, $self->fxb, $self->fyb]);
$self->DraftLabelCanvas->configure(-scrollregion => [1, $self->dya, 100, $self->dyb]);
$self->FinishedLabelCanvas->configure(-scrollregion => [1, $self->fya, 100, $self->fyb]);
my $s = $self->ScrollFrame->Scrollbar('-orient' => 'horizontal', '-command' => sub {$self->FinishedCanvas->xview(@_); $self->DraftCanvas->xview(@_)});
$self->DraftCanvas->configure('-xscrollcommand' => ['set' => $s] ); # since they are identical only one canvas needs to feed-back to the scroll bar to show it's extents
$s->pack('-side'=>'bottom', '-fill'=>'x', '-expand' => 'x');
$self->ScrollBar($s);
} else { # vertical
$self->dxa(-$map_width/2); # each map is equally distributed
$self->fxa(-$map_width/2); # each map is equally distributed
$self->dxb($map_width/2); # around the zero axis
$self->fxb($map_width/2); # around the zero axis
$self->{-axis_loc} = $map_width/2; # axis goes half-way (this is a strange bug in AnnotMap... even if you specify that the map is -100 to +100, you can't set the axis at 0, you have to set it at +100 to put it in the middle of this 200 range....
$self->dya(1);
$self->fya(1);
$self->dyb($window_length);# height is unchanged
$self->fyb($window_length);# height is unchanged
my $DLF = $self->MapFrame->Frame->pack(-side => 'left', -fill => 'both'); # frame for Draft map and labels
my $FLF = $self->MapFrame->Frame->pack(-side => 'left', -fill => 'both'); # frame for Finished map and labels
$self->DraftLabelCanvas($DLF->Canvas(-width => $map_width, -height => 100, -background => "#ffffff")->pack(-side => 'top', -fill => 'both'));
$self->DraftCanvas($DLF->Canvas(-width => $map_width, -height => $window_length, -background => "#ffffff")->pack(-side => 'top', -fill => 'both'));
my $Dyscrollbar = $DLF->Scrollbar('-orient' => 'horizontal','-command' => sub {$self->DraftLabelCanvas->xview(@_);$self->DraftCanvas->xview(@_)})->pack(-side => 'top', '-fill'=>'x', '-expand' => 'y');
$self->FinishedLabelCanvas($FLF->Canvas(-width => $map_width, -height => 100, -background => "#eeeeff")->pack(-side => 'top', -fill => 'both'));
$self->FinishedCanvas($FLF->Canvas(-width => $map_width, -height => $window_length, -background => "#eeeeff")->pack(-side => 'top', -fill => 'both'));
my $Fyscrollbar = $FLF->Scrollbar('-orient' => 'horizontal','-command' => sub {$self->FinishedLabelCanvas->xview(@_);$self->FinishedCanvas->xview(@_)})->pack(-side => 'top', '-fill'=>'x', '-expand' => 'y');
$self->DraftLabelCanvas->configure('-xscrollcommand' => ['set' => $Dyscrollbar] );
$self->FinishedLabelCanvas->configure('-xscrollcommand' => ['set' => $Fyscrollbar] );
$self->DraftCanvas->configure('-xscrollcommand' => ['set' => $Dyscrollbar] );
$self->FinishedCanvas->configure('-xscrollcommand' => ['set' => $Fyscrollbar] );
$self->DraftCanvas->configure(-scrollregion => [$self->dxa, 1, $self->dxb, $self->dyb]);
$self->FinishedCanvas->configure(-scrollregion => [$self->fxa, 1, $self->fxb, $self->fyb]);
$self->DraftLabelCanvas->configure(-scrollregion => [$self->dxa, 1, $self->dxb, $self->dyb]);
$self->FinishedLabelCanvas->configure(-scrollregion => [$self->dxa, 1, $self->dxb, 100]);
my $s = $self->ScrollFrame->Scrollbar('-orient' => 'vertical', '-command' => sub {$self->FinishedCanvas->yview(@_); $self->DraftCanvas->yview(@_)});
$self->DraftCanvas->configure('-yscrollcommand' => ['set' => $s] );
$s->pack('-side'=>'right', '-fill'=>'y', '-expand' => 'y');
$self->ScrollBar($s);
}
$self->DraftCanvas->update;
$self->DraftCanvas->Tk::bind('<Enter>', sub { $self->DraftCanvas->Tk::focus; } ); # set focus on the appropriate map when mouse enters
$self->FinishedCanvas->Tk::bind('<Enter>', sub { $self->FinishedCanvas->Tk::focus; } ); # the space
if ($self->_activeDelete eq "on"){$self->DraftCanvas->Tk::bind("<KeyPress-Delete>", sub {$self->deleteFeatures([(keys %{$self->getSelectedFeatures})])} )}
if ($self->_activeDelete eq "on"){$self->FinishedCanvas->Tk::bind("<KeyPress-Delete>", sub {$self->deleteFeatures([(keys %{$self->getSelectedFeatures})])} )};
# and now create the maps
$self->FinishedMap($self->FinishedCanvas->AnnotMap($self->fxa, $self->fya, $self->fxb, $self->fyb, $self->MapArgs));
$self->DraftMap($self->DraftCanvas->AnnotMap($self->dxa, $self->dya, $self->dxb, $self->dyb, $self->MapArgs));
_setupAxes($self); # draw the axis on the two maps
# now we have to deal with the elements of the ZOOM
my $zoomlabel = $self->ZoomFrame->Label(-text => "Zoom")->pack(-side => 'left');
$self->min_zoom($self->DraftMap->{scale_factor}); # the original maps display the entire sequence, therefore this is the minimum level of zoom
$self->max_zoom(2); # this is somewhat arbitrary...
my $min_scroll = 0;
my $max_scroll = 100;
$self->zoom_ratio( ($self->max_zoom - $self->min_zoom)/($max_scroll - $min_scroll)); #/
my $zoom_scale_length = ($self->{-orientation} eq 'horizontal')?$window_length:$map_width; # the zoom-scale spans the horizontal bottom of the window,
# the length of which depends on the orientation of the map
my $zoomscale = $self->ZoomFrame->Scale(-orient => 'horizontal',
-sliderlength => 25,
-length => $zoom_scale_length - 80, # the 80 pixels compensates for the "Zoom" label beside the widget
-width => 10,
-from => 1,
-to => 100,
-showvalue => 0,
-variable => \$self->{zoom_level},
-command => sub {$self->DoZoom()} );
$zoomscale->pack(-side => 'left', -expand => 'yes', -fill => 'x', -anchor => 'e');
$self->ZoomBar($zoomscale);
$self->{zoom_level} = 5;
$self->DoZoom;
# initialize the canvas with the default rows & colors
foreach my $source(@{$self->InitialSources}){
$self->_check_and_expand_draft_canvas($source);
$self->addSource($source);
}
foreach my $label(@{$self->InitialFinishedLabels}){
$self->_check_and_expand_finished_canvas($label);
$self->addFinishedSourceLabel($label);
}
# draw the labels for default rows and colors
foreach my $source($self->Sources){
$self->_drawDraftLabels([$source]);
}
foreach my $label($self->FinishedSourceLabels){
$self->_drawFinishedLabels([$label]);
}
# now that everything is set up, go ahead and draw the features
my @features = $self->MapSeq->top_SeqFeatures;
my @IDs = @{$self->mapFeatures("both", \@features)}; # only the features from a top_SeqFeatures call -> screened for GeneStructure objects in this routine
$self->_bindMultiSelection(); # this sets up teh mouse-bindings for the "rubber-band box" that snaps around multiple features
$self->_setupDrag_n_Drop(); # this sets up the basics of the drag n drop interface
my $toplevel = $self->DraftCanvas->toplevel;
my $a = $toplevel->geometry;
$a =~ /(\d+)x(\d+)\+-?(\d+)\+-?(\d+)/; #get current screen position of top-level window eg. 500x300+20+-45
$toplevel->geometry("$1"."x"."$2+10+10"); # set it so that the control bar is entirely visible at the top of the screen
$self->_addMenus;
$self->MapFrame->OnDestroy(sub {&localDestroy($self)});
$self->{zoom_level} = 1;
$self->DoZoom;
# return the object handle
return $self;
}
=head2 activeDelete
Title : activeDelete
Usage : $MapObj->activeDelete("on" | "off")
Function : enable/disable 'delete' key to delete mapped features (default off)
Returns :
Args : "on" | "off" (case sensitive)
=cut
sub activeDelete {
my ($self, $onoff) = @_;
$self->_activeDelete($onoff);
if ($self->_activeDelete eq "on"){
$self->DraftCanvas->Tk::bind("<KeyPress-Delete>", sub {$self->deleteFeatures([(keys %{$self->getSelectedFeatures})])} );
}else {
$self->DraftCanvas->Tk::bind("<KeyPress-Delete>", sub {} );
}
if ($self->_activeDelete eq "on"){
$self->FinishedCanvas->Tk::bind("<KeyPress-Delete>", sub {$self->deleteFeatures([(keys %{$self->getSelectedFeatures})])} );
}else {
$self->FinishedCanvas->Tk::bind("<KeyPress-Delete>", sub {} );
}
}
sub _addMenus {
my ($self) = @_;
#if ($self->Menu){eval {$self->Menu->destroy}}
#if ($self->ReCastMenu){eval {$self->ReCastMenu->destroy}}
my $canvas = $self->DraftCanvas;
my $menu = $canvas->Menu(-type => 'normal', -tearoff => 0);
my $cm = $menu->Menu(-type => 'normal', -tearoff => 0);
foreach my $type (keys %{$self->BioPerlFeatureTypes()}) {
$cm->add(
'command',
-label => "$type",
-command => sub {$self->reCastAs(${$self->BioPerlFeatureTypes}{$type});},
);
}
my $f = $menu->add(
'cascade',
-label => 'Re-Cast Selected As',