File: DrawTransmembrane.pm

package info (click to toggle)
libbio-graphics-perl 2.40-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 3,276 kB
  • sloc: perl: 21,801; makefile: 14
file content (1046 lines) | stat: -rwxr-xr-x 48,145 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
package Bio::Graphics::DrawTransmembrane;

use strict;
use warnings;
use GD;
use base 'Bio::Root::Root';

my %DRAWOPTIONS = (
    ## general parameters
    'topology'                => { 'private' => 'topology_array',
                                   'default' => []},
    'topology_string'         => { 'private' => 'topology_string',
                                   'default' => 0},
    'n_terminal'              => { 'private' => 'n_term',
                                   'default' => 'out', },
    'title'                   => { 'private' => 'title',
                                   'default' => ''},
    'inside_label'            => { 'private' => 'in',
                                   'default' => "Cytoplasmic"},
    'outside_label'           => { 'private' => 'out',
                                   'default' => "Extracellular"},
    'membrane_label'          => { 'private' => 'membrane',
                                   'default' => "Plasma Membrane"},
    ## dimensions
    'helix_height'            => { 'private' => 'helix_height',
                                   'default' => 130},
    'helix_width'             => { 'private' => 'helix_width',
                                   'default' => 50},
    'loop_width'              => { 'private' => 'loop_width',
                                   'default' => 20},
    'vertical_padding'        => { 'private' => 'vertical_padding',
                                   'default' => 140},
    'horizontal_padding'      => { 'private' => 'horizontal_padding',
                                   'default' => 150},
    'membrane_offset'         => { 'private' => 'offset',
                                   'default' => 6},
    ## loop lengths and limits
    'short_loop_height'       => { 'private' => 'short_loop',
                                   'default' => 90},
    'medium_loop_height'      => { 'private' => 'medium_loop',
                                   'default' => 120},
    'long_loop_height'        => { 'private' => 'long_loop',
                                   'default' => 150},
    'short_loop_limit'        => { 'private' => 'short_loop_limit',
                                   'default' => 15},
    'long_loop_limit'         => { 'private' => 'long_loop_limit',
                                   'default' => 30},
    'n_terminal_height'       => { 'private' => 'n_terminal_height',
                                   'default' => 150},
    'c_terminal_height'       => { 'private' => 'c_terminal_height',
                                   'default' => 80},
    'loop_heights'            => { 'private' => 'loop_heights',
                                   'default' => {}},
    'n_terminal_offset'       => { 'private' => 'n_term_offset',
                                   'default' => 0},
    'c_terminal_offset'       => { 'private' => 'c_term_offset',
                                   'default' => 0},
    ## colour scheme & display options
    'show_labels'             => { 'private' => 'labels',
                                   'default' => 'on'},
    'bold_helices'            => { 'private' => 'bold_helices',
                                   'default' => 1},
    'bold_labels'             => { 'private' => 'bold_labels',
                                   'default' => 0},
    'colour_scheme'           => { 'private' => 'scheme',
                                   'default' => 'yellow'},
    'draw_cytosol'            => { 'private' => 'draw_cytosol',
                                   'default' => 0},
    'draw_bilayer'            => { 'private' => 'draw_bilayer',
                                   'default' => 1},
    'draw_loops'              => { 'private' => 'draw_loops',
                                   'default' => 1},
    'draw_terminai'           => { 'private' => 'draw_terminai',
                                   'default' => 1},
    'draw_helices'            => { 'private' => 'draw_helices',
                                   'default' => 1},
    ## labeling options
    'labels'                  => { 'private' => 'loop_labels',
                                   'default' => {}},
    'text_offset'             => { 'private' => 'text_offset',
                                   'default' => 0},
    'helix_label'             => { 'private' => 'helix_label',
                                   'default' => 'S'},
	'n_term_label'            => { 'private' => 'n_term_label',
								   'default' => 'N-Terminal'},
	'c_term_label'            => { 'private' => 'c_term_label',
								   'default' => 'C-Terminal'},
	'dontsort'                => { 'private' => 'dontsort',
								   'default' => 0},
	'ttf_font'                => { 'private' => 'ttf_font',
								   'default' => 0},
	'ttf_font_size'           => { 'private' => 'ttf_font_size',
								   'default' => 8},
);

sub new {
	my ($class, @args) = @_;	
	my $self = $class->SUPER::new(@args);
	my %opt = @args;
	%opt = map {my $k = $_;
		$k =~ s{^-}{};
		$k => $opt{$_}} keys %opt;
	
    # need to shore up private variables, check for req'd parameters
	for my $param (sort keys %DRAWOPTIONS) {
		my ($priv, $def) = ($DRAWOPTIONS{$param}->{'private'},$DRAWOPTIONS{$param}->{'default'});
		$self->{$priv} = (exists $opt{$param}) ? $opt{$param} : $def;
	}

	$self->{'loop_count'} = 1;

	return $self;
}

sub png {

 	my $self = shift;

	my @numeric = ('helix_height','helix_width','loop_width','vertical_padding','horizontal_padding','short_length','medium_loop_length','long_loop_length','short_loop_limit','long_loop_limit','n_terminal_height','membrane_offset','text_offset','n_term_offset','c_term_offset');

	foreach (@numeric){
		die "\nParameter $_ must be numeric.\n\n" if exists $self->{$_} && $self->{$_} =~ /-\{?}\D+/;
	}

	foreach (keys %{$self->{'loop_labels'}}){
		die "\nLabel position $_ must be numeric.\n\n" if $_ =~ /\D+/;
	}

	foreach (keys %{$self->{'loop_heights'}}){
		die "\nLoop number $_ must be numeric.\n\n" if $_ =~ /\D+/;
	}

	foreach (values %{$self->{'loop_heights'}}){
		die "\nLoop height $_ must be numeric.\n\n" if $_ =~ /\D+/;
	}

	## n-terminal defaults to outside in it's not in,inside,out,outside
	$self->{'n_term'} = 'out' if (($self->{'n_term'} ne 'in')&&($self->{'n_term'} ne 'inside')&&($self->{'n_term'} ne 'out')||$self->{'n_term'} eq 'outside');
	$self->{'n_term'} = 'in' if $self->{'n_term'} eq 'inside';

	if ($self->{'topology_string'}){
		$self->{'topology_string'} =~ s/\D\.//g;
		$self->{'topology_string'} =~ s/;/,/g;
		@{$self->{'topology_array'}} = split(/,/,$self->{'topology_string'});
	}

	## check to make sure we have pairs of helix boundaries and that data is numeric otherwise quit
	if (scalar @{$self->{'topology_array'}} % 2){
		die "\nUneven number of helix boundaries.\n\n";
	}

	foreach (@{$self->{'topology_array'}}){
		if ($_ =~ /\D/){
			die "\nTopology data is not numeric. $_\n\n";
			
		}
	}

	## check to make sure the TTF font exists, otherwise use gdSmallFont
	if ($self->{'ttf_font'}){
		unless (-e $self->{'ttf_font'}){
			print "\nCan't find font ".$self->{'ttf_font'}.".\n";
			$self->{'ttf_font'} = 0;
		}
	}

	my @sorted_topology = sort {$a <=> $b} @{$self->{'topology_array'}};
	
	## Don't automatically sort the topology array
	@sorted_topology = @{$self->{'topology_array'}} if $self->{'dontsort'};
	
	$self->{'helix_count'} = scalar @{$self->{'topology_array'}} / 2;
	
	unless ($self->{'helix_count'}){
		die "\nNo topology data found.\n\n";
	}

	## put helix start/stop points in $self->{'helix_span'} and loop lengths in $self->{'loop_length'}
	foreach (0..($self->{'helix_count'} - 1)){
		my $count = $_ * 2;
		$self->{'helix_span'}{$_ + 1}{'start'} = $sorted_topology[$count];
		$self->{'helix_span'}{$_ + 1}{'stop'} = $sorted_topology[$count + 1];
		$self->{'loop_length'}{$_ + 1} = scalar ($sorted_topology[$count + 2] - $sorted_topology[$count + 1]) unless ($_ + 1 == $self->{'helix_count'});
	}
	
	$self->{'width'} = ($self->{'horizontal_padding'} * 2) + ($self->{'helix_width'} * $self->{'helix_count'}) + ($self->{'loop_width'} * ($self->{'helix_count'} - 1));
	$self->{'height'} = $self->{'helix_height'} + ($self->{'vertical_padding'} * 2);
	
	## create a new image
	$self->{'im'} = new GD::Image($self->{'width'},$self->{'height'});
	
	$self->{'black'} = $self->{'im'}->colorAllocate(0,0,0);
	$self->{'white'} = $self->{'im'}->colorAllocate(255,255,255);
	$self->{'im'}->fill(0,0,$self->{'white'});
	
	## write title
	if ($self->{'ttf_font'}){
		$self->{'im'}->stringFT($self->{'black'},$self->{'ttf_font'},$self->{'ttf_font_size'},0,4,12,$self->{'title'},{linespacing=>0.6,charmap  => 'Unicode',}) if $self->{'title'};
	}else{
		$self->{'im'}->string(gdSmallFont,4,3,$self->{'title'},$self->{'black'}) if $self->{'title'};
	}

	$self->draw_cytosol if $self->{'draw_cytosol'};
	$self->draw_bilayer if $self->{'draw_bilayer'};
	$self->draw_loops if $self->{'draw_loops'};
	$self->draw_terminai if $self->{'draw_terminai'};
	$self->draw_helices if $self->{'draw_helices'};

	## use GD to convert to png
	return $self->{'im'}->GD::Image::png;
	
}

sub add_tmhmm_feat {

	my $self = shift;
	my $feat = shift;
	
	#print Dumper $feat;
	
	## add a helix from a tmhmm feature	
	if ($feat->{'_primary_tag'} eq 'transmembrane'){
		push @{$self->{'topology_array'}},$feat->{'_location'}{'_start'};
		push @{$self->{'topology_array'}},$feat->{'_location'}{'_end'};
	}

	## i've made a few changes to TmHmm.pm to include the inside/outside loops.
	## this bit looks for the topology of the 1st residue so we can now position the n-terminal 
	if ($feat->{'_location'}{'_start'} == 1){
		if ($feat->{'_primary_tag'} =~ /(\w+)_loop/){
			$self->{'n_term'} = $1; 
		}
	}
	
	return $self;

}

sub draw_cytosol {

  	my $self = shift;
	
	my $cytosol_offset = 5;
	my $light_grey = $self->{'im'}->colorAllocate(164,164,164);

	## draw cytosol
	$self->{'im'}->filledRectangle(($self->{'horizontal_padding'} / 3) ,($self->{'vertical_padding'} + $self->{'helix_height'} - $cytosol_offset),($self->{'width'} - ($self->{'horizontal_padding'} / 3)),($self->{'vertical_padding'} + ($self->{'helix_height'} * 2 ) + $cytosol_offset),$light_grey);

	return $self;
}

sub draw_bilayer {

  	my $self = shift;
	
	my $dark_grey = $self->{'im'}->colorAllocate(40,40,40);
	my $dark_grey1 = $self->{'im'}->colorAllocate(50,50,50);
	my $dark_grey2 = $self->{'im'}->colorAllocate(60,60,60);
	my $dark_grey3 = $self->{'im'}->colorAllocate(70,70,70);

	## label either side of membrane
	if ($self->{'ttf_font'}){

		$self->{'im'}->stringFT($self->{'black'},$self->{'ttf_font'},$self->{'ttf_font_size'},0,($self->{'horizontal_padding'} / 3) + 2,($self->{'vertical_padding'} + $self->{'offset'} - 3),$self->{'out'},{linespacing=>0.6,charmap  => 'Unicode',}) if $self->{'labels'};
		$self->{'im'}->stringFT($self->{'black'},$self->{'ttf_font'},$self->{'ttf_font_size'},0,($self->{'horizontal_padding'} / 3) + 2,($self->{'vertical_padding'} + $self->{'helix_height'} - $self->{'offset'} + 12),$self->{'in'},{linespacing=>0.6,charmap  => 'Unicode',}) if $self->{'labels'};
	}else{
		$self->{'im'}->string(gdSmallFont,($self->{'horizontal_padding'} / 3) + 2,($self->{'vertical_padding'} + $self->{'offset'} - 14),$self->{'out'},$self->{'black'}) if $self->{'labels'};
		$self->{'im'}->string(gdSmallFont,($self->{'horizontal_padding'} / 3) + 2,($self->{'vertical_padding'} + $self->{'helix_height'} - $self->{'offset'} + 1),$self->{'in'},$self->{'black'}) if $self->{'labels'};
	}



	## draw membrane with graded fill
	$self->{'im'}->filledRectangle(($self->{'horizontal_padding'} / 3),($self->{'vertical_padding'} + $self->{'offset'}),($self->{'width'} - $self->{'horizontal_padding'} / 3),($self->{'vertical_padding'} + $self->{'helix_height'} - $self->{'offset'}),$dark_grey);
	$self->{'im'}->filledRectangle(($self->{'horizontal_padding'} / 3) + 1,($self->{'vertical_padding'} + $self->{'offset'}) + 1,($self->{'width'} - $self->{'horizontal_padding'} / 3) - 1,($self->{'vertical_padding'} + $self->{'helix_height'} - $self->{'offset'}) - 1,$dark_grey1);
	$self->{'im'}->filledRectangle(($self->{'horizontal_padding'} / 3) + 2,($self->{'vertical_padding'} + $self->{'offset'}) + 2,($self->{'width'} - $self->{'horizontal_padding'} / 3) - 2,($self->{'vertical_padding'} + $self->{'helix_height'} - $self->{'offset'}) - 2,$dark_grey2);
	$self->{'im'}->filledRectangle(($self->{'horizontal_padding'} / 3) + 3,($self->{'vertical_padding'} + $self->{'offset'}) + 3,($self->{'width'} - $self->{'horizontal_padding'} / 3) - 3,($self->{'vertical_padding'} + $self->{'helix_height'} - $self->{'offset'}) - 3,$dark_grey3);
			
	## label membrane
	if ($self->{'ttf_font'}){
		$self->{'im'}->stringFT($self->{'white'},$self->{'ttf_font'},$self->{'ttf_font_size'},0,($self->{'horizontal_padding'} + ($self->{'helix_count'} * $self->{'helix_width'}) + (($self->{'helix_count'} - 1) * $self->{'loop_width'}) + 4) + 1,($self->{'vertical_padding'} + $self->{'helix_height'} - $self->{'offset'} - 3),$self->{'membrane'},{linespacing=>0.6,charmap  => 'Unicode',}) if $self->{'labels'};
	}else{
		$self->{'im'}->string(gdSmallFont,($self->{'horizontal_padding'} + ($self->{'helix_count'} * $self->{'helix_width'}) + (($self->{'helix_count'} - 1) * $self->{'loop_width'}) + 4) + 1,($self->{'vertical_padding'} + $self->{'helix_height'} - $self->{'offset'} - 14),$self->{'membrane'},$self->{'white'}) if $self->{'labels'};
	}

	
	return $self;

}

sub draw_helices {

	my $self = shift;

	my $x = $self->{'horizontal_padding'};
	my $y = $self->{'vertical_padding'};

	my ($colour,$colour1,$colour2,$colour3,$colour4,$colour5,$colour6);
		
	if($self->{'scheme'} eq 'blue'){

		$colour = $self->{'im'}->colorAllocate(90,160,255);
		$colour1 = $self->{'im'}->colorAllocate(80,150,255);
		$colour2 = $self->{'im'}->colorAllocate(70,140,255);
		$colour3 = $self->{'im'}->colorAllocate(60,130,255);
		$colour4 = $self->{'im'}->colorAllocate(50,120,255);
		$colour5 = $self->{'im'}->colorAllocate(40,110,255);
		$colour6 = $self->{'im'}->colorAllocate(30,100,255);	
	
	}elsif($self->{'scheme'} eq 'pink'){

		$colour = $self->{'im'}->colorAllocate(255,1,255);
		$colour1 = $self->{'im'}->colorAllocate(240,1,255);
		$colour2 = $self->{'im'}->colorAllocate(230,1,255);
		$colour3 = $self->{'im'}->colorAllocate(220,1,255);
		$colour4 = $self->{'im'}->colorAllocate(200,1,255);
		$colour5 = $self->{'im'}->colorAllocate(180,1,255);
		$colour6 = $self->{'im'}->colorAllocate(160,1,255);

	}elsif($self->{'scheme'} eq 'green'){

		$colour = $self->{'im'}->colorAllocate(5,240,0);
		$colour1 = $self->{'im'}->colorAllocate(5,230,0);
		$colour2 = $self->{'im'}->colorAllocate(5,220,0);
		$colour3 = $self->{'im'}->colorAllocate(5,205,0);
		$colour4 = $self->{'im'}->colorAllocate(5,195,0);
		$colour5 = $self->{'im'}->colorAllocate(5,185,0);
		$colour6 = $self->{'im'}->colorAllocate(5,155,0);	
		
	}elsif($self->{'scheme'} eq 'red'){

		$colour = $self->{'im'}->colorAllocate(240,0,0);
		$colour1 = $self->{'im'}->colorAllocate(230,0,0);
		$colour2 = $self->{'im'}->colorAllocate(220,0,0);
		$colour3 = $self->{'im'}->colorAllocate(205,0,0);
		$colour4 = $self->{'im'}->colorAllocate(190,0,0);
		$colour5 = $self->{'im'}->colorAllocate(170,0,0);
		$colour6 = $self->{'im'}->colorAllocate(150,0,0);
			
	}elsif($self->{'scheme'} eq 'white'){

		$colour = $self->{'white'};
		$colour1 = $self->{'white'};
		$colour2 = $self->{'white'};
		$colour3 = $self->{'white'};
		$colour4 = $self->{'white'};
		$colour5 = $self->{'black'};
		$colour6 = $self->{'black'};
			
	}else{

		## default is yellow

		$colour = $self->{'im'}->colorAllocate(255,235,55);
		$colour1 = $self->{'im'}->colorAllocate(255,230,50);
		$colour2 = $self->{'im'}->colorAllocate(255,220,40);
		$colour3 = $self->{'im'}->colorAllocate(255,210,30);
		$colour4 = $self->{'im'}->colorAllocate(255,200,20);
		$colour5 = $self->{'im'}->colorAllocate(255,190,10);
		$colour6 = $self->{'im'}->colorAllocate(255,180,0);
	}


	for (1..$self->{'helix_count'}){

		## draw helix, with graduated fill
		$self->{'im'}->filledRectangle($x,$y,($x + $self->{'helix_width'})-1,($y + $self->{'helix_height'})-1,$colour6);	
		$self->{'im'}->filledRectangle($x+1,$y+1,($x + $self->{'helix_width'})-1,($y + $self->{'helix_height'})-1,$colour5);
		$self->{'im'}->filledRectangle($x+2,$y+2,($x + $self->{'helix_width'})-2,($y + $self->{'helix_height'})-2,$colour4);
		$self->{'im'}->filledRectangle($x+3,$y+3,($x + $self->{'helix_width'})-3,($y + $self->{'helix_height'})-3,$colour3);
		$self->{'im'}->filledRectangle($x+4,$y+4,($x + $self->{'helix_width'})-4,($y + $self->{'helix_height'})-4,$colour2);
		$self->{'im'}->filledRectangle($x+5,$y+5,($x + $self->{'helix_width'})-5,($y + $self->{'helix_height'})-5,$colour1);
		$self->{'im'}->filledRectangle($x+6,$y+6,($x + $self->{'helix_width'})-6,($y + $self->{'helix_height'})-6,$colour);
	
		## draw a white box around it
		if ($self->{'bold_helices'}){
			$self->{'im'}->rectangle($x,$y,($x + $self->{'helix_width'}),($y + $self->{'helix_height'}),$self->{'black'});
			$self->{'im'}->rectangle($x - 1,$y - 1,($x + $self->{'helix_width'} + 1),($y + $self->{'helix_height'} + 1),$self->{'white'});
		}else{
			$self->{'im'}->rectangle($x,$y,($x + $self->{'helix_width'}),($y + $self->{'helix_height'}),$self->{'white'});
		}
		
		## this is the text on each helix
		my $text = substr($self->{'helix_label'},0,1).$_;

		## draw a white box in the centre and label the helix
		my $x_offset = 5;
		$x_offset = 8 if $_ >= 10;
	
		my $white_box = 12;
		$white_box = 17 if $_ >= 10;

		$self->{'im'}->filledRectangle(($x + ($self->{'helix_width'} / 2) - $x_offset) - 5,($y + ($self->{'helix_height'} / 2) - 7) - 3,$white_box + ($x + ($self->{'helix_width'} / 2) - $x_offset) + 3,12 + ($y + ($self->{'helix_height'} / 2) - 4),$colour1) if $self->{'labels'};
		$self->{'im'}->filledRectangle(($x + ($self->{'helix_width'} / 2) - $x_offset) - 4,($y + ($self->{'helix_height'} / 2) - 7) - 2,$white_box + ($x + ($self->{'helix_width'} / 2) - $x_offset) + 2,12 + ($y + ($self->{'helix_height'} / 2) - 5),$colour2) if $self->{'labels'};
		$self->{'im'}->filledRectangle(($x + ($self->{'helix_width'} / 2) - $x_offset) - 3,($y + ($self->{'helix_height'} / 2) - 7) - 1,$white_box + ($x + ($self->{'helix_width'} / 2) - $x_offset) + 1,12 + ($y + ($self->{'helix_height'} / 2) - 6),$colour3) if $self->{'labels'};
		
		
		$self->{'im'}->filledRectangle(($x + ($self->{'helix_width'} / 2) - $x_offset) - 2,($y + ($self->{'helix_height'} / 2) - 7),$white_box + ($x + ($self->{'helix_width'} / 2) - $x_offset),12 + ($y + ($self->{'helix_height'} / 2) - 7),$self->{'white'}) if $self->{'labels'};

		if ($self->{'ttf_font'}){
			$self->{'im'}->stringFT($self->{'black'},$self->{'ttf_font'},$self->{'ttf_font_size'},0,($x + ($self->{'helix_width'} / 2) - $x_offset - 1),($y + ($self->{'helix_height'} / 2) + 4),$text,{linespacing=>0.6,charmap  => 'Unicode',}) if $self->{'labels'};
		}else{
			$self->{'im'}->string(gdSmallFont,($x + ($self->{'helix_width'} / 2) - $x_offset),($y + ($self->{'helix_height'} / 2) - 7),$text,$self->{'black'}) if $self->{'labels'};
		}

		## label start and end positions of helices
		
		$self->{'x'} = $x;
		$self->{'y'} = $y;
		
		if ($self->{'labels'}){
		
			if (($self->{'n_term'} eq 'out')&&($_ % 2)){
				
				$self->label_helix_o_i();
				
			}elsif($self->{'n_term'} eq 'out'){
				
				$self->label_helix_i_o();
				
			}elsif(($self->{'n_term'} eq 'in')&&($_ % 2)){
				
				$self->label_helix_i_o();
				
			}else{
				
				$self->label_helix_o_i();
				
			}
		}
		
		$x = $self->{'horizontal_padding'} + ($_ * ($self->{'helix_width'} + $self->{'loop_width'}));
	
	}

	if ($self->{'labels'}){

		$x = $self->{'horizontal_padding'};
		$y = $self->{'vertical_padding'};

		for (1..$self->{'helix_count'}){
	
			my $y_mod = 0;
			
			foreach my $l (sort {$b <=> $a} keys %{$self->{'loop_labels'}}){

				if (($l >= $self->{'helix_span'}{$_}{'start'})&&($l <= $self->{'helix_span'}{$_}{'stop'})){

					my $label_length = 0;
					if ($self->{'ttf_font'}){
						## Might need to fiddle with this
						my $size_dif = $self->{'ttf_font_size'} - 8;
						$label_length = 6 + (6 * (length $self->{'loop_labels'}{$l}) + (8 * $size_dif));
					}else{					
						$label_length = 9 + (6 * length $self->{'loop_labels'}{$l});
					}
				
					if ($_ % 2){

						if ($self->{'bold_labels'}){
							$self->{'im'}->filledRectangle(($x + ($self->{'helix_width'} / 2)) + 5,($y + ($self->{'helix_height'} / 2) - 30) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)) + 2,12 + ($y + ($self->{'helix_height'} / 2) - 24) + $y_mod,$self->{'black'});
							my $b = new GD::Polygon;
        						$b->addPt(($x + ($self->{'helix_width'} / 2)) + 4,($y + ($self->{'helix_height'} / 2) - 30) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)));
        						$b->addPt(($x + ($self->{'helix_width'} / 2)) - 5,($y + ($self->{'helix_height'} / 2) - 21) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)));
       							$b->addPt(($x + ($self->{'helix_width'} / 2)) + 4,($y + ($self->{'helix_height'} / 2) - 12) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)));
        						$self->{'im'}->filledPolygon($b,$self->{'black'});
						}

						## add darker box
						$self->{'im'}->filledRectangle(($x + ($self->{'helix_width'} / 2)) + 6,($y + ($self->{'helix_height'} / 2) - 29) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)) + 1,12 + ($y + ($self->{'helix_height'} / 2) - 25) + $y_mod,$colour6);
						## add white box
						$self->{'im'}->filledRectangle(($x + ($self->{'helix_width'} / 2)) + 7,($y + ($self->{'helix_height'} / 2) - 28) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)),12 + ($y + ($self->{'helix_height'} / 2) - 26) + $y_mod,$self->{'white'});


						## draw darker arrowhead
				        	my $poly = new GD::Polygon;
        					$poly->addPt(($x + ($self->{'helix_width'} / 2)) + 5,($y + ($self->{'helix_height'} / 2) - 29) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)));
        					$poly->addPt(($x + ($self->{'helix_width'} / 2)) - 3,($y + ($self->{'helix_height'} / 2) - 21) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)));
       						$poly->addPt(($x + ($self->{'helix_width'} / 2)) + 5,($y + ($self->{'helix_height'} / 2) - 13) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)));
        					$self->{'im'}->filledPolygon($poly,$colour6);

						## draw white arrowhead
				        	my $poly2 = new GD::Polygon;
        					$poly2->addPt(($x + ($self->{'helix_width'} / 2)) + 6,($y + ($self->{'helix_height'} / 2) - 28) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)));
        					$poly2->addPt(($x + ($self->{'helix_width'} / 2)) - 1,($y + ($self->{'helix_height'} / 2) - 21) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)));
       						$poly2->addPt(($x + ($self->{'helix_width'} / 2)) + 6,($y + ($self->{'helix_height'} / 2) - 14) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)));
        					$self->{'im'}->filledPolygon($poly2,$self->{'white'});

						## add label
						if ($self->{'ttf_font'}){
							$self->{'im'}->stringFT($self->{'black'},$self->{'ttf_font'},$self->{'ttf_font_size'},0,($x + ($self->{'helix_width'} / 2)) + 10,($y + ($self->{'helix_height'} / 2) - 16) + $y_mod,$self->{'loop_labels'}{$l},{linespacing=>0.6,charmap  => 'Unicode',});
						}else{
							$self->{'im'}->string(gdSmallFont,($x + ($self->{'helix_width'} / 2)) + 9,($y + ($self->{'helix_height'} / 2) - 27) + $y_mod,$self->{'loop_labels'}{$l},$self->{'black'});
						}
			
						$y_mod = $y_mod - 19;	
							
					}else{

						if ($self->{'bold_labels'}){
							$self->{'im'}->filledRectangle(($x + ($self->{'helix_width'} / 2)) + 5,($y + ($self->{'helix_height'} / 2) + 9) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)) + 2,13 + ($y + ($self->{'helix_height'} / 2) + 15) + $y_mod,$self->{'black'});
				        		my $b = new GD::Polygon;
        						$b->addPt(($x + ($self->{'helix_width'} / 2)) + 4,($y + ($self->{'helix_height'} / 2) + 10) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)));
        						$b->addPt(($x + ($self->{'helix_width'} / 2)) - 5,($y + ($self->{'helix_height'} / 2) + 19) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)));
       							$b->addPt(($x + ($self->{'helix_width'} / 2)) + 4,($y + ($self->{'helix_height'} / 2) + 28) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)));
        						$self->{'im'}->filledPolygon($b,$self->{'black'});
						}

						## add darker box
						$self->{'im'}->filledRectangle(($x + ($self->{'helix_width'} / 2)) + 6,($y + ($self->{'helix_height'} / 2) + 11) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)) + 1,12 + ($y + ($self->{'helix_height'} / 2) + 15) + $y_mod,$colour6);

						## add white box
						$self->{'im'}->filledRectangle(($x + ($self->{'helix_width'} / 2)) + 7,($y + ($self->{'helix_height'} / 2) + 12) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)),12 + ($y + ($self->{'helix_height'} / 2) + 14) + $y_mod,$self->{'white'});

						## draw darker arrowhead
				        	my $poly = new GD::Polygon;
        					$poly->addPt(($x + ($self->{'helix_width'} / 2)) + 5,($y + ($self->{'helix_height'} / 2) + 11) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)));
        					$poly->addPt(($x + ($self->{'helix_width'} / 2)) - 3,($y + ($self->{'helix_height'} / 2) + 19) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)));
       						$poly->addPt(($x + ($self->{'helix_width'} / 2)) + 5,($y + ($self->{'helix_height'} / 2) + 27) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)));
        					$self->{'im'}->filledPolygon($poly,$colour6);

						## draw white arrowhead
				        	my $poly2 = new GD::Polygon;
        					$poly2->addPt(($x + ($self->{'helix_width'} / 2)) + 6,($y + ($self->{'helix_height'} / 2) + 12) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)));
        					$poly2->addPt(($x + ($self->{'helix_width'} / 2)) - 1,($y + ($self->{'helix_height'} / 2) + 19) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)));
       						$poly2->addPt(($x + ($self->{'helix_width'} / 2)) + 6,($y + ($self->{'helix_height'} / 2) + 26) + $y_mod,$label_length + ($x + ($self->{'helix_width'} / 2)));
        					$self->{'im'}->filledPolygon($poly2,$self->{'white'});

						## add label
						if ($self->{'ttf_font'}){
							$self->{'im'}->stringFT($self->{'black'},$self->{'ttf_font'},$self->{'ttf_font_size'},0,($x + ($self->{'helix_width'} / 2)) + 10,($y + ($self->{'helix_height'} / 2) + 24) + $y_mod,$self->{'loop_labels'}{$l},{linespacing=>0.6,charmap  => 'Unicode',});
						}else{
							$self->{'im'}->string(gdSmallFont,($x + ($self->{'helix_width'} / 2)) + 9,($y + ($self->{'helix_height'} / 2) + 12) + $y_mod,$self->{'loop_labels'}{$l},$self->{'black'});
						}

						$y_mod = $y_mod + 19;	
					}
				}
			}
		
			$x = $self->{'horizontal_padding'} + ($_ * ($self->{'helix_width'} + $self->{'loop_width'}));
		}
	}

	return $self;
}

sub draw_terminai {

	my $self = shift;
	
	my $loop_number = ($self->{'helix_count'} - 1);

	## width of terminal
	$self->{'w'} = $self->{'helix_width'} + $self->{'loop_width'};
	$self->{'cx'} = $self->{'horizontal_padding'} - ($self->{'loop_width'} / 2);
	$self->{'cy'} = $self->{'vertical_padding'};

	## draw N-terminal
	if ($self->{'n_term'} eq 'out'){

		$self->{'im'}->arc(($self->{'cx'} - $self->{'n_term_offset'}),$self->{'cy'},($self->{'w'} + (2 * $self->{'n_term_offset'})),$self->{'n_terminal_height'},270,360,$self->{'black'});	
		if ($self->{'ttf_font'}){
			$self->{'im'}->stringFT($self->{'black'},$self->{'ttf_font'},$self->{'ttf_font_size'},0,($self->{'horizontal_padding'} - ($self->{'w'} / 2) - 33 - $self->{'n_term_offset'}),($self->{'cy'} - ($self->{'n_terminal_height'} / 2) + 5),$self->{'n_term_label'},{linespacing=>0.6,charmap  => 'Unicode',}) if $self->{'labels'};
		}else{
			$self->{'im'}->string(gdSmallFont,($self->{'horizontal_padding'} - ($self->{'w'} / 2) - 40 - $self->{'n_term_offset'}),($self->{'cy'} - ($self->{'n_terminal_height'} / 2) - 6),$self->{'n_term_label'},$self->{'black'}) if $self->{'labels'};
		}
		
		
		
		## label n-terminal
		my $y_mod = 0;
		foreach (sort {$b <=> $a} keys %{$self->{'loop_labels'}}){
			if ($_ <= $self->{'helix_span'}{1}{'start'}){
					
				if ($self->{'ttf_font'}){
					$self->{'im'}->stringFT($self->{'black'},$self->{'ttf_font'},$self->{'ttf_font_size'},0,($self->{'horizontal_padding'} - ($self->{'w'} / 2) - 33 - $self->{'n_term_offset'}),($self->{'cy'} - ($self->{'n_terminal_height'} / 2) - 6) - 4 + $y_mod,$self->{'loop_labels'}{$_},{linespacing=>0.6,charmap  => 'Unicode',}) if $self->{'labels'};
				}else{
					$self->{'im'}->string(gdSmallFont,($self->{'horizontal_padding'} - ($self->{'w'} / 2) - 40 - $self->{'n_term_offset'}),($self->{'cy'} - ($self->{'n_terminal_height'} / 2) - 6) - 15 + $y_mod,$self->{'loop_labels'}{$_},$self->{'black'}) if $self->{'labels'};
				}

				$y_mod = $y_mod - 15;
			}
		}

	}else{
		$self->{'cy'} = $self->{'cy'} + $self->{'helix_height'};
		$self->{'im'}->arc(($self->{'cx'} - $self->{'n_term_offset'}),$self->{'cy'},($self->{'w'} + (2 * $self->{'n_term_offset'})),$self->{'n_terminal_height'},0,90,$self->{'black'});
		
		if ($self->{'ttf_font'}){
			$self->{'im'}->stringFT($self->{'black'},$self->{'ttf_font'},$self->{'ttf_font_size'},0,($self->{'horizontal_padding'} - ($self->{'w'} / 2) - 33 - $self->{'n_term_offset'}),($self->{'cy'} + ($self->{'n_terminal_height'} / 2) + 5),$self->{'n_term_label'},{linespacing=>0.6,charmap  => 'Unicode',}) if $self->{'labels'};
		}else{
			$self->{'im'}->string(gdSmallFont,($self->{'horizontal_padding'} - ($self->{'w'} / 2) - 40 - $self->{'n_term_offset'}),($self->{'cy'} + ($self->{'n_terminal_height'} / 2) - 6),$self->{'n_term_label'},$self->{'black'}) if $self->{'labels'};
		}

		## label n-terminal
		my $y_mod = 0;
		foreach (sort {$a <=> $b} keys %{$self->{'loop_labels'}}){
			if ($_ <= $self->{'helix_span'}{1}{'start'}){
				
			if ($self->{'ttf_font'}){
				$self->{'im'}->stringFT($self->{'black'},$self->{'ttf_font'},$self->{'ttf_font_size'},0,($self->{'horizontal_padding'} - ($self->{'w'} / 2) - 33 - $self->{'n_term_offset'}),($self->{'cy'} + ($self->{'n_terminal_height'} / 2) - 6) + 26 + $y_mod,$self->{'loop_labels'}{$_},{linespacing=>0.6,charmap  => 'Unicode',}) if $self->{'labels'};
			}else{
				$self->{'im'}->string(gdSmallFont,($self->{'horizontal_padding'} - ($self->{'w'} / 2) - 40 - $self->{'n_term_offset'}),($self->{'cy'} + ($self->{'n_terminal_height'} / 2) - 6) + 15 + $y_mod,$self->{'loop_labels'}{$_},$self->{'black'}) if $self->{'labels'};
			}	

				$y_mod = $y_mod + 15;
			}
		}

	}

	$self->{'cx'} = ($self->{'helix_count'} * $self->{'helix_width'}) + (($self->{'helix_count'} - 1) * $self->{'loop_width'}) + $self->{'horizontal_padding'} + ($self->{'loop_width'} / 2);

	## draw C-terminal
	if (($self->{'n_term'} eq 'out')&&($loop_number % 2)){
		
		$self->draw_ext_c_term;

	}elsif($self->{'n_term'} eq 'out'){
		
		$self->draw_int_c_term;

	}elsif(($self->{'n_term'} eq 'in')&&($loop_number % 2)){
		
		$self->draw_int_c_term;

	}elsif($self->{'n_term'} eq 'in'){

		$self->draw_ext_c_term;

	}

	return $self;
}

sub draw_loops {

	my $self = shift;
	
	$self->{'x'} = $self->{'horizontal_padding'} + ($self->{'helix_width'} / 2);
	$self->{'h'} = $self->{'medium_loop'};
	$self->{'w'} = $self->{'helix_width'} + $self->{'loop_width'};

	for (1..($self->{'helix_count'} - 1)){

		## Alter loop height according to its actual length
		if ($self->{'loop_length'}{$_} < $self->{'short_loop_limit'}){
			$self->{'h'} = $self->{'short_loop'};
		}elsif($self->{'loop_length'}{$_} > $self->{'long_loop_limit'}){
			$self->{'h'} = $self->{'long_loop'};
		}

		$self->{'l_start'} = $self->{'helix_span'}{$_}{'stop'};
		$self->{'l_stop'} = $self->{'helix_span'}{$_ + 1}{'start'};

		if (($self->{'n_term'} eq 'out')&&($_ % 2)){

			$self->draw_int_loop;

		}elsif($self->{'n_term'} eq 'out'){
			$self->draw_ext_loop;

		}elsif(($self->{'n_term'} eq 'in')&&($_ % 2)){
			$self->draw_ext_loop;

		}elsif($self->{'n_term'} eq 'in'){
			
			$self->draw_int_loop;

		}
		
		$self->{'x'} = $self->{'x'} + $self->{'helix_width'} + $self->{'loop_width'};
		$self->{'x'} = $self->{'horizontal_padding'} if $_ == ($self->{'helix_count'} - 1);
		
	}
	
	return $self;
}

sub label_helix_o_i {

	my $self = shift;

	my $offset = 6;
	$offset = 3 if $self->{'helix_span'}{$_}{'start'} < 100;
	if ($self->{'ttf_font'}){
		$self->{'im'}->stringFT($self->{'black'},$self->{'ttf_font'},$self->{'ttf_font_size'},0,($self->{'x'} + ($self->{'helix_width'} / 2) - $offset) - 1,$self->{'y'} + 13,$self->{'helix_span'}{$_}{'start'},{linespacing=>0.6,charmap  => 'Unicode',});
	}else{
		$self->{'im'}->string(gdSmallFont,($self->{'x'} + ($self->{'helix_width'} / 2) - $offset) - 1,$self->{'y'} + 1,$self->{'helix_span'}{$_}{'start'},$self->{'black'});
	}	

	$offset = 3 if $self->{'helix_span'}{$_}{'stop'} < 100;
	$offset = 6 if $self->{'helix_span'}{$_}{'stop'} >= 100;

	if ($self->{'ttf_font'}){
		$self->{'im'}->stringFT($self->{'black'},$self->{'ttf_font'},$self->{'ttf_font_size'},0,($self->{'x'} + ($self->{'helix_width'} / 2) - $offset) - 1,($self->{'y'} + $self->{'helix_height'} - 3),$self->{'helix_span'}{$_}{'stop'},{linespacing=>0.6,charmap  => 'Unicode',});
	}else{
		$self->{'im'}->string(gdSmallFont,($self->{'x'} + ($self->{'helix_width'} / 2) - $offset) - 1,($self->{'y'} + $self->{'helix_height'} - 14),$self->{'helix_span'}{$_}{'stop'},$self->{'black'});
	}	
	
	return $self;
}

sub label_helix_i_o {

	my $self = shift;

	my $offset = 6;
	$offset = 3 if $self->{'helix_span'}{$_}{'stop'} < 100;
	if ($self->{'ttf_font'}){
		$self->{'im'}->stringFT($self->{'black'},$self->{'ttf_font'},$self->{'ttf_font_size'},0,($self->{'x'} + ($self->{'helix_width'} / 2) - $offset) - 1,$self->{'y'} + 13,$self->{'helix_span'}{$_}{'stop'},{linespacing=>0.6,charmap  => 'Unicode',});
	}else{
		$self->{'im'}->string(gdSmallFont,($self->{'x'} + ($self->{'helix_width'} / 2) - $offset) - 1,$self->{'y'} + 1,$self->{'helix_span'}{$_}{'stop'},$self->{'black'});
	}	

	$offset = 3 if $self->{'helix_span'}{$_}{'start'} < 100;

	if ($self->{'ttf_font'}){
		$self->{'im'}->stringFT($self->{'black'},$self->{'ttf_font'},$self->{'ttf_font_size'},0,($self->{'x'} + ($self->{'helix_width'} / 2) - $offset) - 1,($self->{'y'} + $self->{'helix_height'} - 3),$self->{'helix_span'}{$_}{'start'},{linespacing=>0.6,charmap  => 'Unicode',});
	}else{
		$self->{'im'}->string(gdSmallFont,($self->{'x'} + ($self->{'helix_width'} / 2) - $offset) - 1,($self->{'y'} + $self->{'helix_height'} - 14),$self->{'helix_span'}{$_}{'start'},$self->{'black'});
	}	
	
	return $self;
}

sub draw_int_c_term {

	my $self = shift;
	
	## draw internal c-terminal
	$self->{'im'}->arc(($self->{'cx'} + $self->{'c_term_offset'}),($self->{'vertical_padding'} + $self->{'helix_height'}),($self->{'w'} + (2 * $self->{'c_term_offset'})),$self->{'c_terminal_height'},90,180,$self->{'black'});	

	if ($self->{'ttf_font'}){
		$self->{'im'}->stringFT($self->{'black'},$self->{'ttf_font'},$self->{'ttf_font_size'},0,($self->{'cx'} + 4 + $self->{'c_term_offset'}),(($self->{'vertical_padding'} + $self->{'helix_height'}) + ($self->{'c_terminal_height'} / 2) + 5),$self->{'c_term_label'},{linespacing=>0.6,charmap  => 'Unicode',}) if $self->{'labels'};
	}else{
		$self->{'im'}->string(gdSmallFont,($self->{'cx'} + 3 + $self->{'c_term_offset'}),(($self->{'vertical_padding'} + $self->{'helix_height'}) + ($self->{'c_terminal_height'} / 2) - 6),$self->{'c_term_label'},$self->{'black'}) if $self->{'labels'};
	}	

	## label terminal
	if ($self->{'labels'}){
		my $y_mod = 0;
		foreach (sort {$a <=> $b} keys %{$self->{'loop_labels'}}){
			if ($_ >= $self->{'helix_span'}{$self->{'helix_count'}}{'stop'}){
							
				if ($self->{'ttf_font'}){
					$self->{'im'}->stringFT($self->{'black'},$self->{'ttf_font'},$self->{'ttf_font_size'},0,($self->{'cx'} + 4 + $self->{'c_term_offset'}),(($self->{'vertical_padding'} + $self->{'helix_height'}) + ($self->{'c_terminal_height'} / 2) - 6) + 26 + $y_mod,$self->{'loop_labels'}{$_},{linespacing=>0.6,charmap  => 'Unicode',}) if $self->{'labels'};
				}else{
					$self->{'im'}->string(gdSmallFont,($self->{'cx'} + 3 + $self->{'c_term_offset'}),(($self->{'vertical_padding'} + $self->{'helix_height'}) + ($self->{'c_terminal_height'} / 2) - 6) + 15 + $y_mod,$self->{'loop_labels'}{$_},$self->{'black'});
				}
				$y_mod = $y_mod + 15;
			}
		}
	}
	return $self;
}

sub draw_ext_c_term {

	my $self = shift;
	
	## draw external c-terminal
	$self->{'im'}->arc(($self->{'cx'} + $self->{'c_term_offset'}),$self->{'vertical_padding'},($self->{'w'} + (2 * $self->{'c_term_offset'})),$self->{'c_terminal_height'},180,270,$self->{'black'});
	
	if ($self->{'ttf_font'}){
		$self->{'im'}->stringFT($self->{'black'},$self->{'ttf_font'},$self->{'ttf_font_size'},0,,($self->{'cx'} + 3 + $self->{'c_term_offset'}),($self->{'vertical_padding'} - ($self->{'c_terminal_height'} / 2) + 5),$self->{'c_term_label'},{linespacing=>0.6,charmap  => 'Unicode',}) if $self->{'labels'};
	}else{
		$self->{'im'}->string(gdSmallFont,($self->{'cx'} + 3 + $self->{'c_term_offset'}),($self->{'vertical_padding'} - ($self->{'c_terminal_height'} / 2) - 6),$self->{'c_term_label'},$self->{'black'}) if $self->{'labels'};
	}


	## label terminal
	if ($self->{'labels'}){
		my $y_mod = 0;
		foreach (sort {$b <=> $a} keys %{$self->{'loop_labels'}}){
			if ($_ >= $self->{'helix_span'}{$self->{'helix_count'}}{'stop'}){

				if ($self->{'ttf_font'}){
					$self->{'im'}->stringFT($self->{'black'},$self->{'ttf_font'},$self->{'ttf_font_size'},0,($self->{'cx'} + 3 + $self->{'c_term_offset'}),($self->{'vertical_padding'} - ($self->{'c_terminal_height'} / 2) - 6) - 4 - $y_mod,$self->{'loop_labels'}{$_},{linespacing=>0.6,charmap  => 'Unicode',}) if $self->{'labels'};
				}else{
					$self->{'im'}->string(gdSmallFont,($self->{'cx'} + 3 + $self->{'c_term_offset'}),($self->{'vertical_padding'} - ($self->{'c_terminal_height'} / 2) - 6) - 15 - $y_mod,$self->{'loop_labels'}{$_},$self->{'black'});
				}
				$y_mod = $y_mod + 15;
			}
		}
	}
	return $self;
}

sub draw_int_loop {

	my $self = shift;
	
	## draw internal loop
	$self->{'cx'} = $self->{'x'} + ($self->{'helix_width'} / 2) + ($self->{'loop_width'} / 2);

	## this sets the height to the value given by the loop_heights hash
	foreach (sort {$a <=> $b} keys %{$self->{'loop_heights'}}){
		$self->{'h'} = $self->{'loop_heights'}{$_} if $_ == $self->{'loop_count'};
	}

	$self->{'im'}->arc($self->{'cx'},($self->{'vertical_padding'} + $self->{'helix_height'}),$self->{'w'},$self->{'h'},0,180,$self->{'black'});

	## label loop
	if ($self->{'labels'}){
		my $y_mod = 0;
		foreach (sort {$a <=> $b} keys %{$self->{'loop_labels'}}){
			if (($_ >= $self->{'l_start'})&&($_ <= $self->{'l_stop'})){
				$self->{'im'}->line($self->{'cx'},($self->{'vertical_padding'} + $self->{'helix_height'} + ($self->{'h'} / 2)),$self->{'cx'},($self->{'vertical_padding'} + $self->{'helix_height'} + ($self->{'h'} / 2) + 5),$self->{'black'}) unless $y_mod;

				if ($self->{'ttf_font'}){
					$self->{'im'}->stringFT($self->{'black'},$self->{'ttf_font'},$self->{'ttf_font_size'},0,($self->{'cx'} + $self->{'text_offset'} - 3),($self->{'vertical_padding'} + $self->{'helix_height'} + ($self->{'h'} / 2) + 17) + $y_mod,$self->{'loop_labels'}{$_},{linespacing=>0.6,charmap  => 'Unicode',}) if $self->{'labels'};
				}else{
					$self->{'im'}->string(gdSmallFont,($self->{'cx'} + $self->{'text_offset'}),($self->{'vertical_padding'} + $self->{'helix_height'} + ($self->{'h'} / 2) + 6) + $y_mod,$self->{'loop_labels'}{$_},$self->{'black'}) ;
				}
				$y_mod = $y_mod + 15;
			}
		}
	}
	$self->{'loop_count'}++;
	return $self;
}

sub draw_ext_loop {

	my $self = shift;
	
	## draw external loop
	$self->{'cx'} = $self->{'x'} + ($self->{'helix_width'} / 2) + ($self->{'loop_width'} / 2);
	
	## this sets the height to the value given by the loop_heights hash
	foreach (sort {$a <=> $b} keys %{$self->{'loop_heights'}}){
		$self->{'h'} = $self->{'loop_heights'}{$_} if $_ == $self->{'loop_count'};
	}
	
	$self->{'im'}->arc($self->{'cx'},$self->{'vertical_padding'},$self->{'w'},$self->{'h'},180,360,$self->{'black'});

	## label loop
	if ($self->{'labels'}){
		my $y_mod = 0;
		foreach (sort {$b <=> $a} keys %{$self->{'loop_labels'}}){
			if (($_ >= $self->{'l_start'})&&($_ <= $self->{'l_stop'})){
				$self->{'im'}->line($self->{'cx'},($self->{'vertical_padding'} - ($self->{'h'} / 2)),$self->{'cx'},($self->{'vertical_padding'} - ($self->{'h'} / 2) - 5),$self->{'black'}) unless $y_mod;

				if ($self->{'ttf_font'}){
					$self->{'im'}->stringFT($self->{'black'},$self->{'ttf_font'},$self->{'ttf_font_size'},0,($self->{'cx'} + $self->{'text_offset'} - 3),($self->{'vertical_padding'} - ($self->{'h'} / 2) - 8) + $y_mod,$self->{'loop_labels'}{$_},{linespacing=>0.6,charmap  => 'Unicode',}) if $self->{'labels'};
				}else{
					$self->{'im'}->string(gdSmallFont,($self->{'cx'} + $self->{'text_offset'}),($self->{'vertical_padding'} - ($self->{'h'} / 2) - 19) + $y_mod,$self->{'loop_labels'}{$_},$self->{'black'});
				}

				$y_mod = $y_mod - 15;
			}	
		}
	}
	$self->{'loop_count'}++;
	return $self;
}

1;

=head1 NAME

Bio::Graphics::DrawTransmembrane - draw a cartoon of an Alpha-helical transmembrane protein.

=head1 SYNOPSIS

  use Bio::Graphics::DrawTransmembrane;
  my @topology = (20,45,59,70,86,109,145,168,194,220);

  ## Simple use - -topology is the only option that is required

  my $im = Bio::Graphics::DrawTransmembrane->new(
      -title => 'This is a cartoon displaying transmembrane helices.',
      -topology => \@topology);

  ## More advanced use
  my %labels = (5 => '5 - Sulphation Site',
                21 => '1st Helix',
                47 => '40 - Mutation',
                60 => 'Voltage Sensor',
                72 => '72 - Mutation 2',
                73 => '73 - Mutation 3',
                138 => '138 - Glycosylation Site',
                170 => '170 - Phosphorylation Site',
                200 => 'Last Helix');

  my $im = Bio::Graphics::DrawTransmembrane->new(-n_terminal=> 'out',
                                  -topology => \@topology,
                                  -bold_helices=> 1,
                                  -labels=> \%labels,
                                  -text_offset=> -15,
                                  -outside_label=>'Lumen',
                                  -inside_label=>'Cytoplasm',
                                  -membrane_label=>'Membrane',
                                  -vertical_padding=> 155);

  ## Parse Tmhmm data
  use Bio::Tools::Tmhmm;
  my $im = Bio::Graphics::DrawTransmembrane->new(
      -title=>'Let\'s parse some Tmhmm output...',
      -bold_helices=> 1);
  open(FILE, 'tmhmm.out');
  my $parser = new Bio::Tools::Tmhmm(-fh => \*FILE );
  while(my $tmhmm_feat = $parser->next_result ) {
	## Load features into DrawTransmembrane object
	$im->add_tmhmm_feat($tmhmm_feat);
  }
  close FILE;

  ## Now write the image to a .png file
  open(OUTPUT, ">output.png");
  binmode OUTPUT;
  print OUTPUT $im->png;
  close OUTPUT;

=head1 DESCRIPTION

A module to draw a cartoon of an alpha-helical transmembrane
protein. It uses GD and allows the image to be written to a .png file.

The options are a set of tag/value pairs as follows:

  Option              Value                                         Default
  ------              -----                                         -------

  -topology           Array containing transmembrane helix          none
	              boundaries. This is the only option that 
		      is required

  -topology_string    Alternative to -topology, provide a string    none
                      containing the topology data in the form
		      A.11,31;B.41,59;C.86,107;D.145,166

  -n_terminal         Location of the N-terminal of the sequence,   out
  	              either 'in' or 'out'

  -title              Title to add to the image                     none

  -inside_label       Label for the inside of the membrane          Cytoplasmic

  -outside_label      Label for the outside of the membrane         Extracellular

  -membrane_label     Label for the membrane                        Plasma Membrane

  -colour_scheme      Colour scheme to use. Current choices are     blue
                      blue, yellow, red, green, pink or white. 

  -labels             Label loops and helices using data from a     none
                      hash, e.g.

		      %labels = (138 => 'Glycosylation Site',
		                 190 => 'Binding Site');

		      The hash key must be numeric, ranges are 
		      not allowed.

  -bold_helices       Draws black boxes round helices               1

  -bold_labels        Draws black boxes round labels                0

  -text_offset        Shift the text labeling the loops. Use a      0 
                      negative value to shift it left, a positive
		      value to shift it right

  -helix_height       Transmembrane helix height                    130

  -helix_width        Transmembrane helix width                     50

  -loop_width         Loop width                                    20

  -vertical_padding   Vertical padding                              140

  -horizontal_padding Horizontal Padding                            150

  -membrane_offset    Offest between helix end and membrane         6

  -short_loop_height  Height of short loops                         90

  -medium_loop_height Height of medium loops                        120

  -long_loop_height   Height of long loops                          150

  -short_loop_limit   Length in residues below which a loop is      15
                      classed as short

  -long_loop_limit    Length in residues above which a loop is      30
                      classed as long

  -loop_heights       Explicitly set heights of each loop, e.g.

                      %loop_heights = (1 => 45,
                                       2 => 220,
                                       3 => 50,
                                       4 => 220,
                                       9 => 70);

                      The key corresponds to the loop number. Both
                      key and value must be numeric. If you use
                      -loop_height and there is a defined height
                      for the current loop then other height values
                      will be overridden

  -n_terminal_height  Height of N-terminal                          150

  -c_terminal_height  Height of C-terminal                          80

  -n_terminal_offset  Shift the N-terminal left by this amount      0

  -c_terminal_offset  Shift the C-terminal right by this amount     0

  -helix_label        Change the 'S' label on each helix. Only 1    S
                      character is allowed

  -show_labels        Display text labels                           on

  -draw_cytosol       Show the cytosol                              false

  -draw_bilayer       Show the membrane                             true

  -draw_loops         Show the loops                                true

  -draw_terminai      Show the terminai                             true

  -draw_helices       Show the helices                              true

  -dontsort           Don't automatically sort the topology array   0

  -ttf_font           Path to TTF font, e.g.                        none 
                      /usr/share/fonts/msttcorefonts/arial.ttf

  -ttf_font_size      Default size for TTF font. Use 7-9 with       8
                      Arial for best results  


Height, width, padding and other numerical values can gernerally be
left alone. They are useful if your labels consists of a lot of text
as this may lead to them overlapping. In this case try increasing the
loop_width or helix_width options. -text_offset is also very useful
for avoiding overlapping.

=head1 AUTHOR

Tim Nugent E<lt>timnugent@gmail.comE<gt>

=cut