Debian Codegraph @

TFBS/Matrix/ICM.pm

raw symbols SCIP
1 # TFBS module for TFBS::Matrix::ICM
2 #
3 # Copyright Boris Lenhard
4 #
5 # You may distribute this module under the same terms as perl itself
6 #
7
8 # POD
9
10 =head1 NAME
11
12 TFBS::Matrix::ICM - class for information content matrices of nucleotide
13 patterns
14
15 =head1 SYNOPSIS
16
17 =over 4
18
19 =item * creating a TFBS::Matrix::ICM object manually:
20
21 my $matrixref = [ [ 0.00, 0.30, 0.00, 0.00, 0.24, 0.00 ],
22 [ 0.00, 0.00, 0.00, 1.45, 0.42, 0.00 ],
23 [ 0.00, 0.89, 2.00, 0.00, 0.00, 0.00 ],
24 [ 0.00, 0.00, 0.00, 0.13, 0.06, 2.00 ]
25 ];
26 my $icm = TFBS::Matrix::ICM->new(-matrix => $matrixref,
27 -name => "MyProfile",
28 -ID => "M0001"
29 );
30
31 # or
32
33 my $matrixstring = <<ENDMATRIX
34 2.00 0.30 0.00 0.00 0.24 0.00
35 0.00 0.00 0.00 1.45 0.42 0.00
36 0.00 0.89 2.00 0.00 0.00 0.00
37 0.00 0.00 0.00 0.13 0.06 2.00
38 ENDMATRIX
39 ;
40 my $icm = TFBS::Matrix::ICM->new(-matrixstring => $matrixstring,
41 -name => "MyProfile",
42 -ID => "M0001"
43 );
44
45
46 =item * retrieving a TFBS::Matix::ICM object from a database:
47
48 (See documentation of individual TFBS::DB::* modules to learn
49 how to connect to different types of pattern databases and retrieve
50 TFBS::Matrix::* objects from them.)
51
52 my $db_obj = TFBS::DB::JASPAR2->new
53 (-connect => ["dbi:mysql:JASPAR2:myhost",
54 "myusername", "mypassword"]);
55 my $pfm = $db_obj->get_Matrix_by_ID("M0001", "ICM");
56 # or
57 my $pfm = $db_obj->get_Matrix_by_name("MyProfile", "ICM");
58
59
60 =item * retrieving list of individual TFBS::Matrix::ICM objects
61 from a TFBS::MatrixSet object
62
63 (see decumentation of TFBS::MatrixSet to learn how to create
64 objects for storage and manipulation of multiple matrices)
65
66 my @icm_list = $matrixset->all_patterns(-sort_by=>"name");
67
68 * drawing a sequence logo
69
70 $icm->draw_logo(-file=>"logo.png",
71 -full_scale =>2.25,
72 -xsize=>500,
73 -ysize =>250,
74 -graph_title=>"C/EBPalpha binding site logo",
75 -x_title=>"position",
76 -y_title=>"bits");
77
78 =back
79
80 =head1 DESCRIPTION
81
82 TFBS::Matrix::ICM is a class whose instances are objects representing
83 position weight matrices (PFMs). An ICM is normally calculated from a
84 raw position frequency matrix (see L<TFBS::Matrix::PFM>
85 for the explanation of position frequency matrices). For example, given
86 the following position frequency matrix,
87
88 A:[ 12 3 0 0 4 0 ]
89 C:[ 0 0 0 11 7 0 ]
90 G:[ 0 9 12 0 0 0 ]
91 T:[ 0 0 0 1 1 12 ]
92
93 the standard computational procedure is applied to convert it into the
94 following information content matrix:
95
96 A:[2.00 0.30 0.00 0.00 0.24 0.00]
97 C:[0.00 0.00 0.00 1.45 0.42 0.00]
98 G:[0.00 0.89 2.00 0.00 0.00 0.00]
99 T:[0.00 0.00 0.00 0.13 0.06 2.00]
100
101 which contains the "weights" associated with the occurrence of each
102 nucleotide at the given position in a pattern.
103
104 A TFBS::Matrix::PWM object is equipped with methods to search nucleotide
105 sequences and pairwise alignments of nucleotide sequences with the
106 pattern they represent, and return a set of sites in nucleotide
107 sequence (a TFBS::SiteSet object for single sequence search, and a
108 TFBS::SitePairSet for the alignment search).
109
110 =head1 FEEDBACK
111
112 Please send bug reports and other comments to the author.
113
114 =head1 AUTHOR - Boris Lenhard
115
116 Boris Lenhard E<lt>[email protected]<gt>
117
118 =head1 APPENDIX
119
120 The rest of the documentation details each of the object
121 methods. Internal methods are preceded with an underscore.
122
123 =cut
124
125 # The code starts HERE:
126
127 package TFBS::Matrix::ICM;
128
129 use vars '@ISA';
130 use PDL;
131 use strict;
132 use Bio::Root::Root;
133 use Bio::SeqIO;
134 use TFBS::Matrix;
135 BEGIN {
136 # this will not fail if the modules are nit available
137 # but only if the user tries to actually draw a logo
138 eval "use SVG";
139 eval "use GD";
140
141 };
142 use File::Temp qw/:POSIX/;
143 @ISA = qw(TFBS::Matrix Bio::Root::Root);
144
145 #################################################################
146 # PUBLIC METHODS
147 #################################################################
148
149 =head2 new
150
151 Title : new
152 Usage : my $icm = TFBS::Matrix::ICM->new(%args)
153 Function: constructor for the TFBS::Matrix::ICM object
154 Returns : a new TFBS::Matrix::ICM object
155 Args : # you must specify either one of the following three:
156
157 -matrix, # reference to an array of arrays of integers
158 #or
159 -matrixstring,# a string containing four lines
160 # of tab- or space-delimited integers
161 #or
162 -matrixfile, # the name of a file containing four lines
163 # of tab- or space-delimited integers
164 #######
165
166 -name, # string, OPTIONAL
167 -ID, # string, OPTIONAL
168 -class, # string, OPTIONAL
169 -tags # an array reference, OPTIONAL
170
171 =cut
172
173
174 sub new {
175 my ($class, %args) = @_;
176 my $matrix = TFBS::Matrix->new(%args, -matrixtype=>"ICM");
177 my $self = bless $matrix, ref($class) || $class;
178 $self->_check_ic_validity();
179 return $self;
180 }
181
182 =head2 to_PWM
183
184 Title : to_PWM
185 Usage : my $pwm = $icm->to_PWM()
186 Function: converts an information content matrix (a TFBS::Matrix::ICM object)
187 to position weight matrix. At present it assumes uniform
188 background distribution of nucleotide frequencies.
189 Returns : a new TFBS::Matrix::PWM object
190 Args : none; in the future releases, it should be able to accept
191 a user defined background probability of the four
192 nucleotides
193
194 =cut
195
196
197 sub to_PWM {
198 my ($self) = @_;
199 $self->throw ("Method to_PWM not yet implemented.");
200 }
201
202 =head2 draw_logo
203
204 Title : draw_logo
205 Usage : my $gdImageObj = $icm->draw_logo(%args)
206 Function: Draws a "sequence logo", a graphical representation
207 of a possibly degenerate fixed-width nucleotide
208 sequence pattern, from the information content matrix
209 Returns : a GD::Image object;
210 if you only need the image file you can ignore it
211 Args : -file, # the name of the output PNG image file
212 # OPTIONAL: default none
213 -xsize # width of the image in pixels
214 # OPTIONAL: default 600
215 -ysize # height of the image in pixels
216 # OPTIONAL: default 5/8 of -x_size
217 -startpos # start position in the logo for x axis
218 # OPTIONAL: default is 1
219 -margin # size of image margins in pixels
220 # OPTIONAL: default 15% of -y_size
221 -full_scale # the maximum value on the y-axis, in bits
222 # OPTIONAL: default 2.25
223 -graph_title,# the graph title
224 # OPTIONAL: default none
225 -x_title, # x-axis title; OPTIONAL: default none
226 -y_title # y-axis title; OPTIONAL: default none
227 -error_bars # reference to an array of S.D. values for each column; OPTIONAL
228 -ps # if true, produces a postscript string instead of a GD::Image object
229 -pdf # if true AND the -file argumant is used, produces an output pdf file
230
231 =cut
232
233 sub draw_logo {
234
235 no strict;
236 my $self = shift;
237 my %args = (-xsize => 600,
238 -full_scale => 2.25,
239 -graph_title=> "",
240 -x_title => "",
241 -y_title => "",
242 -startpos => 1,
243 @_);
244 # Other parameters that can be specified:
245 # -ysize -line_width -margin
246 # do not have a fixed default value
247 # - they are calculated from xsize if not specified
248
249 # draw postscript logo if asked for
250 if ($args{'-ps'} || $args{'-pdf'}){
251 return _draw_ps_logo($self, %args);
252 }
253 if ($args{'-svg'} || $args{'-SVG'}){
254 return _draw_svg_logo($self, %args);
255 }
256
257 my ($xsize,$FULL_SCALE, $x_title, $y_title)
258 = @args{qw(-xsize -full_scale -x_title y_title)} ;
259
260 my $PER_PIXEL_LINE = 300;
261
262 # calculate other parameters if not specified
263
264 my $line_width = ($args{-line_width} or int ($xsize/$PER_PIXEL_LINE) or 1);
265 my $ysize = ($args{-ysize} or $xsize/1.6);
266 # remark (the line above): 1.6 is a standard screen x:y ratio
267 my $margin = ($args{-margin} or $ysize*0.15);
268
269 my $image = GD::Image->new($xsize, $ysize);
270 my $white = $image->colorAllocate(255,255,255);
271 my $black = $image->colorAllocate(0,0,0);
272 my $motif_size = $self->pdl_matrix->getdim(0);
273 my $font = ((&GD::gdTinyFont(), &GD::gdSmallFont(), &GD::gdMediumBoldFont(),
274 &GD::gdLargeFont(), &GD::gdGiantFont())[int(($ysize-50)/100)]
275 or &GD::gdGiantFont());
276 my $title_font = ((&GD::gdSmallFont(), &GD::gdMediumBoldFont(),
277 &GD::gdLargeFont(), &GD::gdGiantFont())[int(($ysize-50)/100)]
278 or &GD::gdGiantFont());
279
280
281 # WRITE LABELS AND TITLE
282
283 # graph title #&GD::Font::MediumBold
284 $image->string($title_font,
285 $xsize/2-length($args{-graph_title})* $title_font->width() /2,
286 $margin/2 - $title_font->height()/2,
287 $args{-graph_title}, $black);
288
289 # x_title
290 $image->string($font,
291 $xsize/2-length($args{-x_title})*$font->width()/2,
292 $ysize-( $margin - $font->height()*0 - 5*$line_width)/2
293 - $font->height()/2*0,
294 $args{-x_title},
295 $black);
296 # y_title
297 $image->stringUp($font,
298 ($margin -$font->width()- 5*$line_width)/2
299 - $font->height()/2 ,
300 $ysize/2+length($args{'-y_title'})*$font->width()/2,
301 $args{'-y_title'}, $black);
302
303
304 # DRAW AXES
305
306 # vertical: (top left to bottom right)
307 $image->filledRectangle($margin-$line_width, $margin-$line_width,
308 $margin-1, $ysize-$margin+$line_width,
309 $black);
310 # horizontal: (ditto)
311 $image->filledRectangle($margin-$line_width, $ysize-$margin+1,
312 $xsize-$margin+$line_width,$ysize-$margin+$line_width,
313 $black);
314
315 # DRAW VERTICAL TICKS AND LABELS
316
317 # vertical axis (IC 1 and 2)
318 my $ic_1 = ($ysize - 2* $margin) / $FULL_SCALE;
319 foreach my $i (1..$FULL_SCALE) {
320 $image->filledRectangle($margin-3*$line_width,
321 $ysize-$margin - $i*$ic_1,
322 $margin-1,
323 $ysize-$margin+$line_width - $i*$ic_1,
324 $black);
325 $image->string($font,
326 $margin-5*$line_width - $font->width,
327 $ysize - $margin - $i*$ic_1 - $font->height()/2,
328 $i,
329 $black);
330 }
331
332 # DRAW HORIZONTAL TICKS AND LABELS, AND THE LOGO ITSELF
333
334 # define function refs as hash elements
335 my %draw_letter = ( A => \&_png_draw_A,
336 C => \&_png_draw_C,
337 G => \&_png_draw_G,
338 T => \&_png_draw_T );
339
340 my $horiz_step = ($xsize -2*$margin) / $motif_size;
341
342 #this is to avoid clutter on X axis:
343 my $longest_label_length = length("$motif_size");
344 if (length ($args{-startpos}) > $longest_label_length) {
345 $longest_label_length = length ($args{-startpos});
346 }
347 if (length ($args{-startpos}+$motif_size) > $longest_label_length) {
348 $longest_label_length = length ($args{-startpos}+$motif_size);
349 }
350 my $draw_every_nth_label = int($longest_label_length*$font->width+2) / $horiz_step + 1;
351 foreach my $i (0..$motif_size) {
352
353 $image->filledRectangle($margin + $i*$horiz_step,
354 $ysize-$margin+1,
355 $margin + $i*$horiz_step+ $line_width,
356 $ysize-$margin+3*$line_width,
357 $black);
358 last if $i==$motif_size;
359
360 # get the $i-th column of matrix
361 my %ic;
362 ($ic{A}, $ic{C}, $ic{G}, $ic{T}) = list $self->pdl_matrix->slice($i);
363
364 # sort nucleotides by increasing information content
365 my @draw_order = sort {$ic{$a}<=>$ic{$b}} qw(A C G T);
366
367 # draw logo column
368 my $xlettersize = $horiz_step /1.1;
369 my $ybottom = $ysize - $margin;
370 foreach my $base (@draw_order) {
371 my $ylettersize = int($ic{$base}*$ic_1 +0.5);
372 next if $ylettersize ==0;
373
374 # draw letter
375 $draw_letter{$base}->($image,
376 $margin + $i*$horiz_step,
377 $ybottom - $ylettersize,
378 $xlettersize, $ylettersize, $white);
379 $ybottom = $ybottom - $ylettersize-1;
380 }
381
382 if ($args{'-error_bars'} and ref($args{'-error_bars'}) eq "ARRAY") {
383 my $sd_pix = int($args{'-error_bars'}->[$i]*$ic_1);
384 my $yt = $ybottom - $sd_pix+1;
385 my $yb = $ybottom + $sd_pix-1;
386 my $xpos = $margin + ($i+0.45)*$horiz_step;
387 my $half_width;
388
389 if ($yb > $ysize-$margin+$line_width) {
390 $yb = $ysize-$margin+$line_width
391 }
392 else {
393 $image->line($xpos - $xlettersize/8, $yb,
394 $xpos + $xlettersize/8, $yb,
395 $black);
396 }
397
398 $image->line($xpos, $yt, $xpos, $yb, $black);
399 $image->line($xpos - 1 , $ybottom, $xpos+1, $ybottom, $black);
400 $image->line($xpos - $xlettersize/8, $yt,
401 $xpos + $xlettersize/8, $yt,
402 $black);
403
404
405 }
406
407 # print position number on x axis (The if condition is for avoiding clutter)
408 my $xlabel = $i+ $args{-startpos};
409 if ($args{-startpos}<0 and $xlabel>=0) {
410 $xlabel ++;
411 }
412 if ($xlabel % $draw_every_nth_label == 0) {
413 $image->string($font,
414 $margin + ($i+0.5)*$horiz_step - $font->width()/2,
415 $ysize - $margin +5*$line_width,
416 $xlabel,
417 $black);
418 }
419 }
420
421 # print $args{-file};
422 if ($args{-file}) {
423 open (PNGFILE, ">".$args{-file})
424 or $self->throw("Could not write to ".$args{-file});
425 print PNGFILE $image->png;
426 close PNGFILE;
427 }
428 return $image;
429 }
430
431
432
433
434
435
436 sub total_ic {
437 return $_[0]->pdl_matrix->sum();
438 }
439 =head2 _draw_ps_logo
440
441 Title : _draw_ps_logo
442 Usage : my $postscript_string = $icm->_draw_ps_logo(%args)
443 Internal method, should be accessed using draw_logo()
444 Function: Draws a "sequence logo", a graphical representation
445 of a possibly degenerate fixed-width nucleotide
446 sequence pattern, from the information content matrix
447 Returns : a postscript string;
448 if you only need the image file you can ignore it
449 Args : -file, # the name of the output PNG image file
450 # OPTIONAL: default none
451 -xsize # width of the image in pixels
452 # OPTIONAL: default 600
453 -ysize # height of the image in pixels
454 # OPTIONAL: default 5/8 of -x_size
455 -full_scale # the maximum value on the y-axis, in bits
456 # OPTIONAL: default 2.25
457 -graph_title,# the graph title
458 # OPTIONAL: default none
459 -x_title, # x-axis title; OPTIONAL: default none
460 -y_title # y-axis title; OPTIONAL: default none
461
462
463
464 =cut
465 sub _draw_ps_logo{
466 my $self = shift;
467 my %args = (-xsize => 600,
468 -full_scale => 2.25,
469 -graph_title=> "",
470 -x_title => "",
471 -y_title => "",
472 @_);
473
474 my $xsize= $args{'-xsize'};
475 my $max_ysize= $args{'-ysize'} ||int 5* $args{'-xsize'}/8;
476 my $ysize= $max_ysize*($args{'-full_scale'}-($args{'-full_scale'}-2))/$args{'-full_scale'};
477
478 my $x=100; # nternal, for placement on 'paper'
479 my $y=100;
480
481 my $out= "%!PS-Adobe-2.0
482 %%Orientation: Portrait
483 %%Pages: 1
484 %%BoundingBox: 0 0 ".($args{'-xsize'}*1.2)." ".( $max_ysize*1.5)."
485 %%BeginSetup
486 %%EndSetup
487 %%Magnification: 1.0000
488 %%EndProlog
489 %%end
490 %%save
491 gsave\n";
492
493 #colors and correction definitions
494 my %color;
495 $color{'black'}="0.000 0.000 0.000 setrgbcolor";
496 $color{'A'}="0.000 1.000 0.000 setrgbcolor";
497 $color{'C'}="0.000 0.000 1.000 setrgbcolor";
498 $color{'G'}="1.000 0.860 0.000 setrgbcolor";
499 $color{'T'}="1.000 0.000 0.000 setrgbcolor";
500
501 my $fontsize= int $ysize*0.68;
502 my $fontwidth=1.5*($xsize/$self->length());
503 my %w_correct; # correction of font widths
504 $w_correct{'A'}=0.95;
505 $w_correct{'T'}=1.05;
506 $w_correct{'C'}=0.90;
507 $w_correct{'G'}=0.90;
508
509 my %y_next;#correction of font heights
510 $y_next{'A'}=1;
511 $y_next{'T'}=1;
512 $y_next{'C'}=0.94;
513 $y_next{'G'}=0.94;
514
515 my %y_correct; #correction of font bounding boxes
516 $y_correct{'A'}=0;
517 $y_correct{'C'}=0.035*$fontsize;
518 $y_correct{'G'}=0.035*$fontsize;
519 $y_correct{'T'}=0;
520
521
522 #define y axis,tickmarks and scaling
523
524 my $font= $fontwidth/5;
525 $out.="newpath\n ". ($x-10)." ". ($y+2*$ysize/4 )." moveto\n". "$x ". ($y+2*$ysize/4 ) ." lineto\n stroke\n";
526 $out.= "gsave\n/Times-Bold findfont $color{black} [$font 0 0 $font 0 0] makefont setfont\n".($x-20). " ".( $y+$ysize/2)." moveto\n";
527 $out.=" (1) show\n grestore\n" ;
528 $out.="newpath\n ". ($x-10)." ". ($y+$ysize )." moveto\n". "$x ". ($y+$ysize) ." lineto\n stroke\n";
529 $out.="newpath\n ". ($x-10)." ". ($y+$max_ysize )." moveto\n". "$x ". ($y+$max_ysize) ." lineto\n stroke\n";
530 $out.= "gsave\n/Times-Bold findfont $color{black} [$font 0 0 $font 0 0] makefont setfont\n".($x-20). " ".( $y+$ysize)." moveto\n";
531 $out.=" (2) show\n grestore\n" ;
532 $out.="newpath\n $x $y moveto\n". ($x). " ".($y+$max_ysize) ." lineto\n stroke\n";
533 $out.="newpath\n $x $y moveto\n". ($x+$xsize). " ".($y) ." lineto\n stroke\n";
534
535
536 # draw titles if requested
537 if ($args{'-y_title'}){
538 $out.= "gsave\n/Times-Italic findfont $color{black} [$font 0 0 $font 0 0] makefont setfont\n".($x-40). " ".( $y+$ysize/2)." moveto\n";
539 $out.=" 90 rotate ($args{'-y_title'}) show\n grestore\n" ;
540 }
541 if ($args{'-x_title'}){
542 $out.= "gsave\n/Times-Italic findfont $color{black} [$font 0 0 $font 0 0] makefont setfont\n".($x+$xsize/2.5). " ".( $y*(0.60))." moveto\n";
543 $out.=" ($args{'-x_title'}) show\n grestore\n" ;
544 }
545 if ($args{'-title'}){
546 $out.= "gsave\n/Times-Roman findfont $color{black} [".($font*2)." 0 0 $font 0 0] makefont setfont\n".($x+$xsize/3). " ".( $y+$max_ysize*1.1)." moveto\n";
547
548 $out.=" ($args{'-title'}) show\n grestore\n" ;
549 }
550
551
552 # define x axis and x tickmarks
553 my $col_width=($xsize/$self->length()) -0.006*$xsize;
554 my $x_now;
555
556 for(my $i=1; $i<=$self->length(); $i++){
557 $x_now=$x+$col_width*$i;
558 $out.="newpath\n ". ($x_now)." ". ($y)." moveto\n". ($x_now)." ". ($y-$ysize/20 ) ." lineto\n stroke\n";
559 $out.= "gsave\n/Times-Bold findfont $color{black} [$font 0 0 $font 0 0] makefont setfont\n".($x_now-$col_width/2). " ".( $y-20)." moveto\n";
560 $out.=" ($i) show\n grestore\n" ;
561 }
562
563 # draw the logo
564 foreach my $i (0..$self->length()-1 ) { # get the $i-th column of matrix
565
566 my %ic;
567 ($ic{A}, $ic{C}, $ic{G}, $ic{T}) = list $self->pdl_matrix->slice($i);
568 my @draw_order = sort {$ic{$a}<=>$ic{$b}} qw(A C G T);
569
570 #draw this position
571
572 foreach my $letter (@draw_order){
573 $ic{$letter}=0.0000001 if ( $ic{$letter}==0); # some interpretors do not uderstand size 0
574 $out.= "gsave\n/Helvetica-Bold findfont $color{$letter} [".$fontwidth*$w_correct{$letter}." 0 0 ";
575 $out.= $ic{$letter}*$fontsize*$y_next{$letter} ;
576 $y+=$y_correct{$letter}*$ic{$letter}; #movement that isletter specific, due to bounding boxes
577 $out.= " 0 0] makefont setfont\n$x $y moveto\n";
578 $out.= " ($letter) show\n grestore\n" ;
579 $y+=$fontsize*$ic{$letter}*0.75; #ic content move
580 }
581 $x+=$fontwidth/1.6;
582 $y=100;
583
584 }
585 # save as file if requested
586 if ($args{-file}) {
587 open (PSFILE, ">".$args{-file})
588 or $self->throw("Could not write to ".$args{-file});
589 print PSFILE $out;
590 close PSFILE;
591 }
592 if ($args{-pdf}){
593
594 system "ps2pdf $args{-file} ".$args{-file}.".pdf ";
595 system " mv $args{-file}.pdf $args{-file}";
596 }
597
598
599
600
601 return $out;
602 }
603
604 =head2 _draw_svg_logo
605
606
607 =cut
608
609 sub _draw_svg_logo {
610 my $self = shift;
611 my %args = (-xsize => 800,
612 -full_scale => 2.25,
613 -graph_title=> "",
614 -x_title => "",
615 -y_title => "",
616 @_);
617
618 my $max_ysize= $args{'-ysize'} ||int 5* $args{'-xsize'}/8;
619 my ($xsize,$FULL_SCALE, $x_title, $y_title)
620 = @args{qw(-xsize -full_scale -x_title y_title)} ;
621
622 my $PER_PIXEL_LINE = 200;
623
624 # calculate other parameters if not specified
625
626 my $ysize = ($args{-ysize} or $xsize/1.6);
627 my $line_width = ($args{-line_width} or $ysize/$PER_PIXEL_LINE);
628 # remark (the line above): 1.6 is a standard screen x:y ratio
629 my $margin = ($args{-margin} or $ysize*0.15);
630
631
632
633 my $image = SVG->new(width=>$xsize, height=>$ysize);
634 my $white = 'rgb(255,255,255)';
635 my $black = 'rgb(0,0,0)';
636 my $motif_size = $self->pdl_matrix->getdim(0);
637 my $fontsize = int ($ysize/25);
638 my $title_font = {width=>$fontsize*1.5, height=>$fontsize*1.5};
639 my $font = {width=>$fontsize, height=>$fontsize};
640
641 # WRITE LABELS AND TITLE
642
643 # graph title
644 $image->text(id=>"Title",
645 'font-size'=>$title_font->{width},
646 x => $xsize/2,
647 y => 0.6*$margin,
648 'text-anchor'=>'middle'
649 )->cdata($args{-graph_title});
650 # x title
651
652 $image->text(id=>"X_title",
653 'font-size'=>$font->{width},
654 x => $xsize/2,
655 y => $ysize -0.3*$margin,
656 'text-anchor'=>'middle'
657 )->cdata($args{-x_title});
658
659 # y title
660
661 my $g = $image->group;
662 $g->text(id=>"Y_title",
663 'font-size'=>$font->{width},
664 x => 0 ,
665 'text-anchor'=>'middle',
666 y => 0,
667 transform => 'rotate(-90) translate(-'.($ysize/2).','.($margin/2).')')->cdata($args{-y_title});
668
669
670 # DRAW AXES
671
672 # vertical: (top left to bottom right)
673 $image->rectangle(id => "y_axis",
674 style => {
675 #stroke => $black,
676 fill => $black
677 },
678 x => $margin-$line_width,
679 y => $margin,
680 width => $line_width,
681 height => $ysize -2*$margin
682 );
683 #$image->filledRectangle($margin-$line_width, $margin-$line_width,
684 # $margin-1, $ysize-$margin+$line_width, #
685 # $black);
686 # horizontal: (ditto)
687 $image->rectangle(id => "x_axis",
688 style => {
689 #stroke => $black,
690 fill => $black
691 },
692 x => $margin-$line_width,
693 y => $ysize-$margin,
694 width => $xsize-2*$margin+$line_width,
695 height => $line_width
696 );
697 #$image->filledRectangle($margin-$line_width, $ysize-$margin+1,
698 # $xsize-$margin+$line_width,$ysize-$margin+$line_width,
699 # $black);
700
701 # DRAW VERTICAL TICKS AND LABELS
702
703 # vertical axis (IC 1 and 2)
704 my $ic_1 = ($ysize - 2* $margin) / $FULL_SCALE;
705 foreach my $i (1..$FULL_SCALE) {
706 $image->rectangle(x => $margin-3*$line_width,
707 y => $ysize-$margin - $i*$ic_1,
708 width => 3*$line_width,
709 height => $line_width
710 );
711 $image->text(x => $margin-5*$line_width - $font->{width},
712 y => $ysize - $margin - $i*$ic_1 +$font->{height}/2,
713 'font-size'=>$font->{width},
714 'text-anchor'=>"right"
715 )->cdata($i);
716
717 }
718
719 # DRAW HORIZONTAL TICKS AND LABELS, AND THE LOGO ITSELF
720
721 # define function refs as hash elements
722
723 my %draw_letter = ( A => \&_svg_draw_A,
724 C => \&_svg_draw_C,
725 G => \&_svg_draw_G,
726 T => \&_svg_draw_T );
727
728 my $horiz_step = ($xsize -2*$margin) / $motif_size;
729
730 #this is to avoid clutter on X axis:
731
732 my $longest_label_length = length("$motif_size");
733 if (length ($args{-startpos}) > $longest_label_length) {
734 $longest_label_length = length ($args{-startpos});
735 }
736 if (length ($args{-startpos}+$motif_size) > $longest_label_length) {
737 $longest_label_length = length ($args{-startpos}+$motif_size);
738 }
739 my $draw_every_nth_label = int(($longest_label_length+0.25)*$font->{width}) / $horiz_step + 1;
740
741 foreach my $i (0..$motif_size) {
742 my $height = 3*$line_width;
743 if ($i and $i==$args{-startpos}*-1){
744 $height = 5*$line_width;
745 }
746 $image->rectangle(x => $margin + $i*$horiz_step -$line_width/2,
747 y => $ysize-$margin,
748 width => $line_width,
749 height => $height
750 );
751 last if $i==$motif_size;
752
753 # get the $i-th column of matrix
754 my %ic;
755 ($ic{A}, $ic{C}, $ic{G}, $ic{T}) = list $self->pdl_matrix->slice($i);
756
757 # sort nucleotides by increasing information content
758 my @draw_order = sort {$ic{$a}<=>$ic{$b}} qw(A C G T);
759
760 # draw logo column
761 my $xlettersize = $horiz_step*0.95;
762 my $ybottom = $ysize - $margin;
763 foreach my $base (@draw_order) {
764 my $ylettersize = $ic{$base}*$ic_1;
765 next if $ylettersize ==0;
766
767 # draw letter
768 $draw_letter{$base}->($image,
769 $margin + $i*$horiz_step + 0.025* $horiz_step,
770 $ybottom - $ylettersize,
771 $xlettersize, $ylettersize, $white);
772 $ybottom = $ybottom - $ylettersize;
773 }
774
775 if ($args{'-error_bars'} and ref($args{'-error_bars'}) eq "ARRAY") {
776 my $sd_pix = int($args{'-error_bars'}->[$i]*$ic_1);
777 my $yt = $ybottom - $sd_pix+1;
778 my $yb = $ybottom + $sd_pix-1;
779 my $xpos = $margin + ($i+0.5)*$horiz_step;
780 my $half_width;
781
782 if ($yb > $ysize-$margin+$line_width) {
783 $yb = $ysize-$margin+$line_width
784 }
785 else {
786 $image->line(x1=>$xpos - $xlettersize/8, y1=> $yb,
787 x2=> $xpos + $xlettersize/8, y2=>$yb, stroke=>$black,
788 'stroke-width'=>$line_width);
789 }
790
791 $image->line(x1=>$xpos, y1=>$yt, x2=>$xpos, y2=>$yb, stroke=>$black,
792 'stroke-width'=>$line_width);
793 $image->line(x1=>$xpos - $line_width , y1=>$ybottom, x2=>$xpos+$line_width, y2=>$ybottom, stroke=>$black,
794 'stroke-width'=>$line_width);
795 $image->line(x1=>$xpos - $xlettersize/8, y1=>$yt,
796 x2=>$xpos + $xlettersize/8, y2=>$yt, stroke=>$black,
797 'stroke-width'=>$line_width);
798
799
800 }
801
802 # print position number on x axis
803 my $xlabel = $i+ $args{-startpos};
804 if ($args{-startpos}<0 and $xlabel>=0) {
805 $xlabel ++;
806 }
807 if ($xlabel % $draw_every_nth_label == 0) {
808 $image->text(x => $margin + ($i+0.5)*$horiz_step - $font->{width}/2,
809 y => $ysize - $margin +5*$line_width + $font->{width}/2,
810 'font-size'=>$font->{width},
811 'text-anchor'=>"bottom"
812
813 )->cdata($xlabel);
814 }
815 }
816
817 # print to $args{-file};
818 if ($args{-file}) {
819 open (SVGFILE, ">".$args{-file})
820 or $self->throw("Could not write to ".$args{-file});
821 my $xml = $image->xmlify;
822 $xml =~ s/\s+<\/text/<\/text/gs;
823 print SVGFILE $xml;
824 close SVGFILE;
825 }
826 return $image;
827
828 }
829
830
831 =head2 name
832
833 =head2 ID
834
835 =head2 class
836
837 =head2 matrix
838
839 =head2 length
840
841 =head2 revcom
842
843 =head2 rawprint
844
845 =head2 prettyprint
846
847 The above methods are common to all matrix objects. Please consult
848 L<TFBS::Matrix> to find out how to use them.
849
850 =cut
851
852
853 #################################################################
854 # INTERNAL METHODS
855 #################################################################
856
857
858 sub _check_ic_validity {
859 my ($self) = @_;
860 # to do
861 }
862
863 sub DESTROY {
864 # nothing
865 }
866
867
868 #################################################################
869 # UTILITY FUNCTIONS
870 #################################################################
871
872
873 # letter drawing routines
874
875 sub _png_draw_A {
876
877 my ($im, $x, $y, $xsize, $ysize, $white) = @_;
878 my $green = $im->colorAllocate(0,255,0);
879 my $outPoly = GD::Polygon->new();
880 $outPoly->addPt($x, $y+$ysize);
881 $outPoly->addPt($x+$xsize*.42, $y);
882 $outPoly->addPt($x+$xsize*.58, $y);
883 $outPoly->addPt($x+$xsize, $y+$ysize);
884 $outPoly->addPt($x+0.85*$xsize, $y+$ysize);
885 $outPoly->addPt($x+0.725*$xsize, $y+0.75*$ysize);
886 $outPoly->addPt($x+0.275*$xsize, $y+0.75*$ysize);
887 $outPoly->addPt($x+0.15*$xsize, $y+$ysize);
888 $im->filledPolygon($outPoly, $green);
889 if ($ysize>8) {
890 my $inPoly = GD::Polygon->new();
891 $inPoly->addPt($x+$xsize*.5, $y+0.2*$ysize);
892 $inPoly->addPt($x+$xsize*.34, $y+0.6*$ysize-1);
893 $inPoly->addPt($x+$xsize*.64, $y+0.6*$ysize-1);
894 $im->filledPolygon($inPoly, $white);
895 }
896 return 1;
897 }
898
899 sub _png_draw_C {
900 my ($im, $x, $y, $xsize, $ysize, $white) = @_;
901 my $blue = $im->colorAllocate(0,0,255);
902 $im->arc($x+$xsize*0.54, $y+$ysize/2,1.08*$xsize,$ysize,0,360,$blue);
903 $im->fill($x+$xsize/2, $y+$ysize/2, $blue);
904 if ($ysize>12) {
905 $im->arc($x+$xsize*0.53, $y+$ysize/2,
906 0.75*$xsize, (0.725-0.725/$ysize)*$ysize,
907 0,360,$white);
908 $im->fill($x+$xsize/2, $y+$ysize/2, $white);
909 $im->filledRectangle($x+$xsize/2, $y+$ysize/4+1,
910 $x+$xsize*1.1, $y+(3*$ysize/4)-1,
911 $white);
912 }
913 elsif ($ysize>3) {
914 $im->arc($x+$xsize*0.53, $y+$ysize/2,
915 (0.75-0.75/$ysize)*$xsize, (0.725-0.725/$ysize)*$ysize,
916 0,360,$white);
917 $im->fill($x+$xsize/2, $y+$ysize/2, $white);
918 $im->filledRectangle($x+$xsize*0.25, $y+$ysize/2,
919 $x+$xsize*1.1, $y+$ysize/2,
920 $white);
921
922 }
923 return 1;
924 }
925
926 sub _png_draw_G {
927 my ($im, $x, $y, $xsize, $ysize, $white) = @_;
928 my $yellow = $im->colorAllocate(200,200,0);
929 $im->arc($x+$xsize*0.54, $y+$ysize/2,1.08*$xsize,$ysize,0,360,$yellow);
930 $im->fill($x+$xsize/2, $y+$ysize/2, $yellow);
931 if ($ysize>20) {
932 $im->arc($x+$xsize*0.53, $y+$ysize/2,
933 0.75*$xsize, (0.725-0.725/$ysize)*$ysize,
934 0,360,$white);
935 $im->fill($x+$xsize/2, $y+$ysize/2, $white);
936 $im->filledRectangle($x+$xsize/2, $y+$ysize/4+1,
937 $x+$xsize*1.1, $y+$ysize/2-1,
938 $white);
939 }
940 elsif($ysize>3) {
941 $im->arc($x+$xsize*0.53, $y+$ysize/2,
942 (0.75-0.75/$ysize)*$xsize, (0.725-0.725/$ysize)*$ysize,
943 0,360,$white);
944 $im->fill($x+$xsize/2, $y+$ysize/2, $white);
945 $im->filledRectangle($x+$xsize*0.25, $y+$ysize/2,
946 $x+$xsize*1.1, $y+$ysize/2,
947 $white);
948
949 }
950 $im->filledRectangle($x+0.85*$xsize, $y+$ysize/2,
951 $x+$xsize,$y+(3*$ysize/4)-1,
952 $yellow);
953 $im->filledRectangle($x+0.6*$xsize, $y+$ysize/2,
954 $x+$xsize,$y+(5*$ysize/8)-1,
955 $yellow);
956 return 1;
957 }
958
959 sub _png_draw_T {
960
961 my ($im, $x, $y, $xsize, $ysize, $white) = @_;
962 my $red = $im->colorAllocate(255,0,0);
963 $im->filledRectangle($x, $y, $x+$xsize, $y+0.16*$ysize, $red);
964 $im->filledRectangle($x+0.42*$xsize, $y, $x+0.58*$xsize, $y+$ysize, $red);
965 return 1;
966 }
967
968
969
970 sub _svg_draw_A {
971
972 my ($im, $x, $y, $xsize, $ysize) = @_;
973 $im->polygon( points => [$x, $y+$ysize, $x+$xsize*.42, $y, $x+$xsize*.58, $y, $x+$xsize, $y+$ysize,
974 $x+0.85*$xsize, $y+$ysize, $x+0.725*$xsize, $y+0.75*$ysize, $x+0.275*$xsize, $y+0.75*$ysize,
975 $x+0.15*$xsize, $y+$ysize, $x, $y+$ysize],
976 fill => 'rgb(0,255,0)'
977 );
978 $im->polygon( points => [$x+$xsize*.5, $y+0.2*$ysize, $x+$xsize*.34, $y+0.6*$ysize, $x+$xsize*.64, $y+0.6*$ysize ],
979 fill => 'rgb(255,255,255)');
980 return 1;
981 }
982
983
984 sub _svg_draw_C {
985 my ($im, $x, $y, $xsize, $ysize) = @_;
986 $im->ellipse(cx=>$x+$xsize*0.54, cy=>$y+$ysize/2, rx=>$xsize*0.54, ry=>$ysize/2,
987 fill => 'rgb(0,0,255)');
988 $im->ellipse( cx=>$x+$xsize*0.53, cy=>$y+$ysize/2, rx=>$xsize*0.375, ry=>$ysize*0.375,
989 fill => 'rgb(255,255,255)');
990 $im->rectangle(x=>$x+$xsize/2, y=>$y+$ysize/4,
991 width =>$xsize*0.6, height =>$ysize/2,
992 fill=> 'rgb(255,255,255)');
993 return 1;
994 }
995
996 sub _svg_draw_G {
997 my ($im, $x, $y, $xsize, $ysize, $white) = @_;
998 $im->ellipse(cx => $x+$xsize*0.54, cy => $y+$ysize/2,
999 rx => 0.54*$xsize, ry => $ysize/2,
1000 fill => 'rgb(200,200,0)');
1001 $im->ellipse(cx => $x+$xsize*0.53, cy => $y+$ysize/2,
1002 rx => 0.375*$xsize, ry => 0.375*$ysize,
1003 fill => 'rgb(255,255,255)');
1004
1005 $im->rectangle(x=>$x+$xsize/2, y=>$y+$ysize/4,
1006 width =>$xsize*0.6, height =>$ysize/2,
1007 fill=> 'rgb(255,255,255)');
1008 $im->rectangle(x=>$x+0.80*$xsize, y=>$y+$ysize/2,
1009 width =>$xsize*0.208, height =>$ysize/4,
1010 fill=> 'rgb(200,200,0)');
1011 $im->rectangle(x=>$x+0.6*$xsize, y=>$y+$ysize/2,
1012 width =>$xsize*0.408, height =>$ysize/8,
1013 fill=> 'rgb(200,200,0)');
1014
1015 return 1;
1016 }
1017
1018 sub _svg_draw_T {
1019
1020 my ($im, $x, $y, $xsize, $ysize, $white) = @_;
1021 $im->polygon (points =>[$x, $y, $x+$xsize, $y, $x+$xsize, $y+0.16*$ysize,
1022 $x+0.58*$xsize, $y+0.16*$ysize, $x+0.58*$xsize, $y+$ysize,
1023 $x+0.42*$xsize, $y+$ysize, $x+0.42*$xsize, $y+0.16*$ysize,
1024 $x, $y+0.16*$ysize],
1025 fill => 'rgb(255,0,0)');
1026 return 1;
1027 }
1028
1029
1030
1031 1;
1032
1033
1034
1035
1036
1037
1038
1039
1040