| 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 | #use GD; |
| 136 | use File::Temp qw/:POSIX/; |
| 137 | @ISA = qw(TFBS::Matrix Bio::Root::Root); |
| 138 | |
| 139 | ################################################################# |
| 140 | # PUBLIC METHODS |
| 141 | ################################################################# |
| 142 | |
| 143 | =head2 new |
| 144 | |
| 145 | Title : new |
| 146 | Usage : my $icm = TFBS::Matrix::ICM->new(%args) |
| 147 | Function: constructor for the TFBS::Matrix::ICM object |
| 148 | Returns : a new TFBS::Matrix::ICM object |
| 149 | Args : # you must specify either one of the following three: |
| 150 | |
| 151 | -matrix, # reference to an array of arrays of integers |
| 152 | #or |
| 153 | -matrixstring,# a string containing four lines |
| 154 | # of tab- or space-delimited integers |
| 155 | #or |
| 156 | -matrixfile, # the name of a file containing four lines |
| 157 | # of tab- or space-delimited integers |
| 158 | ####### |
| 159 | |
| 160 | -name, # string, OPTIONAL |
| 161 | -ID, # string, OPTIONAL |
| 162 | -class, # string, OPTIONAL |
| 163 | -tags # an array reference, OPTIONAL |
| 164 | |
| 165 | =cut |
| 166 | |
| 167 | |
| 168 | sub new { |
| 169 | my ($class, %args) = @_; |
| 170 | my $matrix = TFBS::Matrix->new(%args, -matrixtype=>"ICM"); |
| 171 | my $self = bless $matrix, ref($class) || $class; |
| 172 | $self->_check_ic_validity(); |
| 173 | return $self; |
| 174 | } |
| 175 | |
| 176 | =head2 to_PWM |
| 177 | |
| 178 | Title : to_PWM |
| 179 | Usage : my $pwm = $icm->to_PWM() |
| 180 | Function: converts an information content matrix (a TFBS::Matrix::ICM object) |
| 181 | to position weight matrix. At present it assumes uniform |
| 182 | background distribution of nucleotide frequencies. |
| 183 | Returns : a new TFBS::Matrix::PWM object |
| 184 | Args : none; in the future releases, it should be able to accept |
| 185 | a user defined background probability of the four |
| 186 | nucleotides |
| 187 | |
| 188 | =cut |
| 189 | |
| 190 | |
| 191 | sub to_PWM { |
| 192 | my ($self) = @_; |
| 193 | $self->throw ("Method to_PWM not yet implemented."); |
| 194 | } |
| 195 | |
| 196 | =head2 draw_logo |
| 197 | |
| 198 | Title : draw_logo |
| 199 | Usage : my $gdImageObj = $icm->draw_logo(%args) |
| 200 | Function: Draws a "sequence logo", a graphical representation |
| 201 | of a possibly degenerate fixed-width nucleotide |
| 202 | sequence pattern, from the information content matrix |
| 203 | Returns : a GD::Image object; |
| 204 | if you only need the image file you can ignore it |
| 205 | Args : -file, # the name of the output PNG image file |
| 206 | # OPTIONAL: default none |
| 207 | -xsize # width of the image in pixels |
| 208 | # OPTIONAL: default 600 |
| 209 | -ysize # height of the image in pixels |
| 210 | # OPTIONAL: default 5/8 of -x_size |
| 211 | -margin # size of image margins in pixels |
| 212 | # OPTIONAL: default 15% of -y_size |
| 213 | -full_scale # the maximum value on the y-axis, in bits |
| 214 | # OPTIONAL: default 2.25 |
| 215 | -graph_title,# the graph title |
| 216 | # OPTIONAL: default none |
| 217 | -x_title, # x-axis title; OPTIONAL: default none |
| 218 | -y_title # y-axis title; OPTIONAL: default none |
| 219 | -error_bars # reference to an array of S.D. values for each column; OPTIONAL |
| 220 | -ps # if true, produces a postscript string instead of a GD::Image object |
| 221 | -pdf # if true AND the -file argumant is used, produces an output pdf file |
| 222 | |
| 223 | =cut |
| 224 | |
| 225 | sub draw_logo { |
| 226 | |
| 227 | no strict; |
| 228 | my $self = shift; |
| 229 | my %args = (-xsize => 600, |
| 230 | -full_scale => 2.25, |
| 231 | -graph_title=> "", |
| 232 | -x_title => "", |
| 233 | -y_title => "", |
| 234 | @_); |
| 235 | # Other parameters that can be specified: |
| 236 | # -ysize -line_width -margin |
| 237 | # do not have a fixed default value |
| 238 | # - they are calculated from xsize if not specified |
| 239 | |
| 240 | # draw postscript logo if asked for |
| 241 | if ($args{'-ps'} || $args{'-pdf'}){ |
| 242 | return _draw_ps_logo($self, @_); |
| 243 | } |
| 244 | |
| 245 | require GD; |
| 246 | |
| 247 | |
| 248 | my ($xsize,$FULL_SCALE, $x_title, $y_title) |
| 249 | = @args{qw(-xsize -full_scale -x_title y_title)} ; |
| 250 | |
| 251 | my $PER_PIXEL_LINE = 300; |
| 252 | |
| 253 | # calculate other parameters if not specified |
| 254 | |
| 255 | my $line_width = ($args{-line_width} or int ($xsize/$PER_PIXEL_LINE) or 1); |
| 256 | my $ysize = ($args{-ysize} or $xsize/1.6); |
| 257 | # remark (the line above): 1.6 is a standard screen x:y ratio |
| 258 | my $margin = ($args{-margin} or $ysize*0.15); |
| 259 | |
| 260 | my $image = GD::Image->new($xsize, $ysize); |
| 261 | my $white = $image->colorAllocate(255,255,255); |
| 262 | my $black = $image->colorAllocate(0,0,0); |
| 263 | my $motif_size = $self->pdl_matrix->getdim(0); |
| 264 | my $font = ((&GD::gdTinyFont(), &GD::gdSmallFont(), &GD::gdMediumBoldFont(), |
| 265 | &GD::gdLargeFont(), &GD::gdGiantFont())[int(($ysize-50)/100)] |
| 266 | or &GD::gdGiantFont()); |
| 267 | my $title_font = ((&GD::gdSmallFont(), &GD::gdMediumBoldFont(), |
| 268 | &GD::gdLargeFont(), &GD::gdGiantFont())[int(($ysize-50)/100)] |
| 269 | or &GD::gdGiantFont()); |
| 270 | |
| 271 | |
| 272 | # WRITE LABELS AND TITLE |
| 273 | |
| 274 | # graph title #&GD::Font::MediumBold |
| 275 | $image->string($title_font, |
| 276 | $xsize/2-length($args{-graph_title})* $title_font->width() /2, |
| 277 | $margin/2 - $title_font->height()/2, |
| 278 | $args{-graph_title}, $black); |
| 279 | |
| 280 | # x_title |
| 281 | $image->string($font, |
| 282 | $xsize/2-length($args{-x_title})*$font->width()/2, |
| 283 | $ysize-( $margin - $font->height()*0 - 5*$line_width)/2 |
| 284 | - $font->height()/2*0, |
| 285 | $args{-x_title}, |
| 286 | $black); |
| 287 | # y_title |
| 288 | $image->stringUp($font, |
| 289 | ($margin -$font->width()- 5*$line_width)/2 |
| 290 | - $font->height()/2 , |
| 291 | $ysize/2+length($args{'-y_title'})*$font->width()/2, |
| 292 | $args{'-y_title'}, $black); |
| 293 | |
| 294 | |
| 295 | # DRAW AXES |
| 296 | |
| 297 | # vertical: (top left to bottom right) |
| 298 | $image->filledRectangle($margin-$line_width, $margin-$line_width, |
| 299 | $margin-1, $ysize-$margin+$line_width, |
| 300 | $black); |
| 301 | # horizontal: (ditto) |
| 302 | $image->filledRectangle($margin-$line_width, $ysize-$margin+1, |
| 303 | $xsize-$margin+$line_width,$ysize-$margin+$line_width, |
| 304 | $black); |
| 305 | |
| 306 | # DRAW VERTICAL TICKS AND LABELS |
| 307 | |
| 308 | # vertical axis (IC 1 and 2) |
| 309 | my $ic_1 = ($ysize - 2* $margin) / $FULL_SCALE; |
| 310 | foreach my $i (1..$FULL_SCALE) { |
| 311 | $image->filledRectangle($margin-3*$line_width, |
| 312 | $ysize-$margin - $i*$ic_1, |
| 313 | $margin-1, |
| 314 | $ysize-$margin+$line_width - $i*$ic_1, |
| 315 | $black); |
| 316 | $image->string($font, |
| 317 | $margin-5*$line_width - $font->width, |
| 318 | $ysize - $margin - $i*$ic_1 - $font->height()/2, |
| 319 | $i, |
| 320 | $black); |
| 321 | } |
| 322 | |
| 323 | # DRAW HORIZONTAL TICKS AND LABELS, AND THE LOGO ITSELF |
| 324 | |
| 325 | # define function refs as hash elements |
| 326 | my %draw_letter = ( A => \&draw_A, |
| 327 | C => \&draw_C, |
| 328 | G => \&draw_G, |
| 329 | T => \&draw_T ); |
| 330 | |
| 331 | my $horiz_step = ($xsize -2*$margin) / $motif_size; |
| 332 | foreach my $i (0..$motif_size) { |
| 333 | |
| 334 | $image->filledRectangle($margin + $i*$horiz_step, |
| 335 | $ysize-$margin+1, |
| 336 | $margin + $i*$horiz_step+ $line_width, |
| 337 | $ysize-$margin+3*$line_width, |
| 338 | $black); |
| 339 | last if $i==$motif_size; |
| 340 | |
| 341 | # get the $i-th column of matrix |
| 342 | my %ic; |
| 343 | ($ic{A}, $ic{C}, $ic{G}, $ic{T}) = list $self->pdl_matrix->slice($i); |
| 344 | |
| 345 | # sort nucleotides by increasing information content |
| 346 | my @draw_order = sort {$ic{$a}<=>$ic{$b}} qw(A C G T); |
| 347 | |
| 348 | # draw logo column |
| 349 | my $xlettersize = $horiz_step /1.1; |
| 350 | my $ybottom = $ysize - $margin; |
| 351 | foreach my $base (@draw_order) { |
| 352 | my $ylettersize = int($ic{$base}*$ic_1 +0.5); |
| 353 | next if $ylettersize ==0; |
| 354 | |
| 355 | # draw letter |
| 356 | $draw_letter{$base}->($image, |
| 357 | $margin + $i*$horiz_step, |
| 358 | $ybottom - $ylettersize, |
| 359 | $xlettersize, $ylettersize, $white); |
| 360 | $ybottom = $ybottom - $ylettersize-1; |
| 361 | } |
| 362 | |
| 363 | if ($args{'-error_bars'} and ref($args{'-error_bars'}) eq "ARRAY") { |
| 364 | my $sd_pix = int($args{'-error_bars'}->[$i]*$ic_1); |
| 365 | my $yt = $ybottom - $sd_pix+1; |
| 366 | my $yb = $ybottom + $sd_pix-1; |
| 367 | my $xpos = $margin + ($i+0.45)*$horiz_step; |
| 368 | my $half_width; |
| 369 | |
| 370 | if ($yb > $ysize-$margin+$line_width) { |
| 371 | $yb = $ysize-$margin+$line_width |
| 372 | } |
| 373 | else { |
| 374 | $image->line($xpos - $xlettersize/8, $yb, |
| 375 | $xpos + $xlettersize/8, $yb, |
| 376 | $black); |
| 377 | } |
| 378 | |
| 379 | $image->line($xpos, $yt, $xpos, $yb, $black); |
| 380 | $image->line($xpos - 1 , $ybottom, $xpos+1, $ybottom, $black); |
| 381 | $image->line($xpos - $xlettersize/8, $yt, |
| 382 | $xpos + $xlettersize/8, $yt, |
| 383 | $black); |
| 384 | |
| 385 | |
| 386 | } |
| 387 | |
| 388 | # print position number on x axis |
| 389 | $image->string($font, |
| 390 | $margin + ($i+0.5)*$horiz_step - $font->width()/2, |
| 391 | $ysize - $margin +5*$line_width, |
| 392 | $i+1, |
| 393 | $black); |
| 394 | } |
| 395 | |
| 396 | # print $args{-file}; |
| 397 | if ($args{-file}) { |
| 398 | open (PNGFILE, ">".$args{-file}) |
| 399 | or $self->throw("Could not write to ".$args{-file}); |
| 400 | print PNGFILE $image->png; |
| 401 | close PNGFILE; |
| 402 | } |
| 403 | return $image; |
| 404 | } |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | sub total_ic { |
| 412 | return $_[0]->pdl_matrix->sum(); |
| 413 | } |
| 414 | =head2 _draw_ps_logo |
| 415 | |
| 416 | Title : _draw_ps_logo |
| 417 | Usage : my $postscript_string = $icm->_draw_ps_logo(%args) |
| 418 | Internal method, should be accessed using draw_logo() |
| 419 | Function: Draws a "sequence logo", a graphical representation |
| 420 | of a possibly degenerate fixed-width nucleotide |
| 421 | sequence pattern, from the information content matrix |
| 422 | Returns : a postscript string; |
| 423 | if you only need the image file you can ignore it |
| 424 | Args : -file, # the name of the output PNG image file |
| 425 | # OPTIONAL: default none |
| 426 | -xsize # width of the image in pixels |
| 427 | # OPTIONAL: default 600 |
| 428 | -ysize # height of the image in pixels |
| 429 | # OPTIONAL: default 5/8 of -x_size |
| 430 | -full_scale # the maximum value on the y-axis, in bits |
| 431 | # OPTIONAL: default 2.25 |
| 432 | -graph_title,# the graph title |
| 433 | # OPTIONAL: default none |
| 434 | -x_title, # x-axis title; OPTIONAL: default none |
| 435 | -y_title # y-axis title; OPTIONAL: default none |
| 436 | |
| 437 | |
| 438 | |
| 439 | =cut |
| 440 | sub _draw_ps_logo{ |
| 441 | my $self = shift; |
| 442 | my %args = (-xsize => 600, |
| 443 | -full_scale => 2.25, |
| 444 | -graph_title=> "", |
| 445 | -x_title => "", |
| 446 | -y_title => "", |
| 447 | @_); |
| 448 | |
| 449 | my $xsize= $args{'-xsize'}; |
| 450 | my $max_ysize= $args{'-ysize'} ||int 5* $args{'-xsize'}/8; |
| 451 | my $ysize= $max_ysize*($args{'-full_scale'}-($args{'-full_scale'}-2))/$args{'-full_scale'}; |
| 452 | |
| 453 | my $x=100; # nternal, for placement on 'paper' |
| 454 | my $y=100; |
| 455 | |
| 456 | my $out= "%!PS-Adobe-2.0 |
| 457 | %%Orientation: Portrait |
| 458 | %%Pages: 1 |
| 459 | %%BoundingBox: 0 0 ".($args{'-xsize'}*1.2)." ".( $max_ysize*1.5)." |
| 460 | %%BeginSetup |
| 461 | %%EndSetup |
| 462 | %%Magnification: 1.0000 |
| 463 | %%EndProlog |
| 464 | %%end |
| 465 | %%save |
| 466 | gsave\n"; |
| 467 | |
| 468 | #colors and correction definitions |
| 469 | my %color; |
| 470 | $color{'black'}="0.000 0.000 0.000 setrgbcolor"; |
| 471 | $color{'A'}="0.000 1.000 0.000 setrgbcolor"; |
| 472 | $color{'C'}="0.000 0.000 1.000 setrgbcolor"; |
| 473 | $color{'G'}="1.000 0.860 0.000 setrgbcolor"; |
| 474 | $color{'T'}="1.000 0.000 0.000 setrgbcolor"; |
| 475 | |
| 476 | my $fontsize= int $ysize*0.68; |
| 477 | my $fontwidth=1.5*($xsize/$self->length()); |
| 478 | my %w_correct; # correction of font widths |
| 479 | $w_correct{'A'}=0.95; |
| 480 | $w_correct{'T'}=1.05; |
| 481 | $w_correct{'C'}=0.90; |
| 482 | $w_correct{'G'}=0.90; |
| 483 | |
| 484 | my %y_next;#correction of font heights |
| 485 | $y_next{'A'}=1; |
| 486 | $y_next{'T'}=1; |
| 487 | $y_next{'C'}=0.94; |
| 488 | $y_next{'G'}=0.94; |
| 489 | |
| 490 | my %y_correct; #correction of font bounding boxes |
| 491 | $y_correct{'A'}=0; |
| 492 | $y_correct{'C'}=0.035*$fontsize; |
| 493 | $y_correct{'G'}=0.035*$fontsize; |
| 494 | $y_correct{'T'}=0; |
| 495 | |
| 496 | |
| 497 | #define y axis,tickmarks and scaling |
| 498 | |
| 499 | my $font= $fontwidth/5; |
| 500 | $out.="newpath\n ". ($x-10)." ". ($y+2*$ysize/4 )." moveto\n". "$x ". ($y+2*$ysize/4 ) ." lineto\n stroke\n"; |
| 501 | $out.= "gsave\n/Times-Bold findfont $color{black} [$font 0 0 $font 0 0] makefont setfont\n".($x-20). " ".( $y+$ysize/2)." moveto\n"; |
| 502 | $out.=" (1) show\n grestore\n" ; |
| 503 | $out.="newpath\n ". ($x-10)." ". ($y+$ysize )." moveto\n". "$x ". ($y+$ysize) ." lineto\n stroke\n"; |
| 504 | $out.="newpath\n ". ($x-10)." ". ($y+$max_ysize )." moveto\n". "$x ". ($y+$max_ysize) ." lineto\n stroke\n"; |
| 505 | $out.= "gsave\n/Times-Bold findfont $color{black} [$font 0 0 $font 0 0] makefont setfont\n".($x-20). " ".( $y+$ysize)." moveto\n"; |
| 506 | $out.=" (2) show\n grestore\n" ; |
| 507 | $out.="newpath\n $x $y moveto\n". ($x). " ".($y+$max_ysize) ." lineto\n stroke\n"; |
| 508 | $out.="newpath\n $x $y moveto\n". ($x+$xsize). " ".($y) ." lineto\n stroke\n"; |
| 509 | |
| 510 | |
| 511 | # draw titles if requested |
| 512 | if ($args{'-y_title'}){ |
| 513 | $out.= "gsave\n/Times-Italic findfont $color{black} [$font 0 0 $font 0 0] makefont setfont\n".($x-40). " ".( $y+$ysize/2)." moveto\n"; |
| 514 | $out.=" 90 rotate ($args{'-y_title'}) show\n grestore\n" ; |
| 515 | } |
| 516 | if ($args{'-x_title'}){ |
| 517 | $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"; |
| 518 | $out.=" ($args{'-x_title'}) show\n grestore\n" ; |
| 519 | } |
| 520 | if ($args{'-title'}){ |
| 521 | $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"; |
| 522 | |
| 523 | $out.=" ($args{'-title'}) show\n grestore\n" ; |
| 524 | } |
| 525 | |
| 526 | |
| 527 | # define x axis and x tickmarks |
| 528 | my $col_width=($xsize/$self->length()) -0.006*$xsize; |
| 529 | my $x_now; |
| 530 | |
| 531 | for(my $i=1; $i<=$self->length(); $i++){ |
| 532 | $x_now=$x+$col_width*$i; |
| 533 | $out.="newpath\n ". ($x_now)." ". ($y)." moveto\n". ($x_now)." ". ($y-$ysize/20 ) ." lineto\n stroke\n"; |
| 534 | $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"; |
| 535 | $out.=" ($i) show\n grestore\n" ; |
| 536 | } |
| 537 | |
| 538 | # draw the logo |
| 539 | foreach my $i (0..$self->length()-1 ) { # get the $i-th column of matrix |
| 540 | |
| 541 | my %ic; |
| 542 | ($ic{A}, $ic{C}, $ic{G}, $ic{T}) = list $self->pdl_matrix->slice($i); |
| 543 | my @draw_order = sort {$ic{$a}<=>$ic{$b}} qw(A C G T); |
| 544 | |
| 545 | #draw this position |
| 546 | |
| 547 | foreach my $letter (@draw_order){ |
| 548 | $ic{$letter}=0.0000001 if ( $ic{$letter}==0); # some interpretors do not uderstand size 0 |
| 549 | $out.= "gsave\n/Helvetica-Bold findfont $color{$letter} [".$fontwidth*$w_correct{$letter}." 0 0 "; |
| 550 | $out.= $ic{$letter}*$fontsize*$y_next{$letter} ; |
| 551 | $y+=$y_correct{$letter}*$ic{$letter}; #movement that isletter specific, due to bounding boxes |
| 552 | $out.= " 0 0] makefont setfont\n$x $y moveto\n"; |
| 553 | $out.= " ($letter) show\n grestore\n" ; |
| 554 | $y+=$fontsize*$ic{$letter}*0.75; #ic content move |
| 555 | } |
| 556 | $x+=$fontwidth/1.6; |
| 557 | $y=100; |
| 558 | |
| 559 | } |
| 560 | # save as file if requested |
| 561 | if ($args{-file}) { |
| 562 | open (PSFILE, ">".$args{-file}) |
| 563 | or $self->throw("Could not write to ".$args{-file}); |
| 564 | print PSFILE $out; |
| 565 | close PSFILE; |
| 566 | } |
| 567 | if ($args{-pdf}){ |
| 568 | |
| 569 | system "ps2pdf $args{-file} ".$args{-file}.".pdf "; |
| 570 | system " mv $args{-file}.pdf $args{-file}"; |
| 571 | } |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | return $out; |
| 577 | } |
| 578 | |
| 579 | |
| 580 | |
| 581 | =head2 name |
| 582 | |
| 583 | =head2 ID |
| 584 | |
| 585 | =head2 class |
| 586 | |
| 587 | =head2 matrix |
| 588 | |
| 589 | =head2 length |
| 590 | |
| 591 | =head2 revcom |
| 592 | |
| 593 | =head2 rawprint |
| 594 | |
| 595 | =head2 prettyprint |
| 596 | |
| 597 | The above methods are common to all matrix objects. Please consult |
| 598 | L<TFBS::Matrix> to find out how to use them. |
| 599 | |
| 600 | =cut |
| 601 | |
| 602 | |
| 603 | ################################################################# |
| 604 | # INTERNAL METHODS |
| 605 | ################################################################# |
| 606 | |
| 607 | |
| 608 | sub _check_ic_validity { |
| 609 | my ($self) = @_; |
| 610 | # to do |
| 611 | } |
| 612 | |
| 613 | sub DESTROY { |
| 614 | # nothing |
| 615 | } |
| 616 | |
| 617 | |
| 618 | ################################################################# |
| 619 | # UTILITY FUNCTIONS |
| 620 | ################################################################# |
| 621 | |
| 622 | |
| 623 | # letter drawing routines |
| 624 | |
| 625 | sub draw_A { |
| 626 | |
| 627 | my ($im, $x, $y, $xsize, $ysize, $white) = @_; |
| 628 | my $green = $im->colorAllocate(0,255,0); |
| 629 | my $outPoly = GD::Polygon->new(); |
| 630 | $outPoly->addPt($x, $y+$ysize); |
| 631 | $outPoly->addPt($x+$xsize*.42, $y); |
| 632 | $outPoly->addPt($x+$xsize*.58, $y); |
| 633 | $outPoly->addPt($x+$xsize, $y+$ysize); |
| 634 | $outPoly->addPt($x+0.85*$xsize, $y+$ysize); |
| 635 | $outPoly->addPt($x+0.725*$xsize, $y+0.75*$ysize); |
| 636 | $outPoly->addPt($x+0.275*$xsize, $y+0.75*$ysize); |
| 637 | $outPoly->addPt($x+0.15*$xsize, $y+$ysize); |
| 638 | $im->filledPolygon($outPoly, $green); |
| 639 | if ($ysize>8) { |
| 640 | my $inPoly = GD::Polygon->new(); |
| 641 | $inPoly->addPt($x+$xsize*.5, $y+0.2*$ysize); |
| 642 | $inPoly->addPt($x+$xsize*.34, $y+0.6*$ysize-1); |
| 643 | $inPoly->addPt($x+$xsize*.64, $y+0.6*$ysize-1); |
| 644 | $im->filledPolygon($inPoly, $white); |
| 645 | } |
| 646 | return 1; |
| 647 | } |
| 648 | |
| 649 | sub draw_C { |
| 650 | my ($im, $x, $y, $xsize, $ysize, $white) = @_; |
| 651 | my $blue = $im->colorAllocate(0,0,255); |
| 652 | $im->arc($x+$xsize*0.54, $y+$ysize/2,1.08*$xsize,$ysize,0,360,$blue); |
| 653 | $im->fill($x+$xsize/2, $y+$ysize/2, $blue); |
| 654 | if ($ysize>12) { |
| 655 | $im->arc($x+$xsize*0.53, $y+$ysize/2, |
| 656 | 0.75*$xsize, (0.725-0.725/$ysize)*$ysize, |
| 657 | 0,360,$white); |
| 658 | $im->fill($x+$xsize/2, $y+$ysize/2, $white); |
| 659 | $im->filledRectangle($x+$xsize/2, $y+$ysize/4+1, |
| 660 | $x+$xsize*1.1, $y+(3*$ysize/4)-1, |
| 661 | $white); |
| 662 | } |
| 663 | elsif ($ysize>3) { |
| 664 | $im->arc($x+$xsize*0.53, $y+$ysize/2, |
| 665 | (0.75-0.75/$ysize)*$xsize, (0.725-0.725/$ysize)*$ysize, |
| 666 | 0,360,$white); |
| 667 | $im->fill($x+$xsize/2, $y+$ysize/2, $white); |
| 668 | $im->filledRectangle($x+$xsize*0.25, $y+$ysize/2, |
| 669 | $x+$xsize*1.1, $y+$ysize/2, |
| 670 | $white); |
| 671 | |
| 672 | } |
| 673 | return 1; |
| 674 | } |
| 675 | |
| 676 | sub draw_G { |
| 677 | my ($im, $x, $y, $xsize, $ysize, $white) = @_; |
| 678 | my $yellow = $im->colorAllocate(200,200,0); |
| 679 | $im->arc($x+$xsize*0.54, $y+$ysize/2,1.08*$xsize,$ysize,0,360,$yellow); |
| 680 | $im->fill($x+$xsize/2, $y+$ysize/2, $yellow); |
| 681 | if ($ysize>20) { |
| 682 | $im->arc($x+$xsize*0.53, $y+$ysize/2, |
| 683 | 0.75*$xsize, (0.725-0.725/$ysize)*$ysize, |
| 684 | 0,360,$white); |
| 685 | $im->fill($x+$xsize/2, $y+$ysize/2, $white); |
| 686 | $im->filledRectangle($x+$xsize/2, $y+$ysize/4+1, |
| 687 | $x+$xsize*1.1, $y+$ysize/2-1, |
| 688 | $white); |
| 689 | } |
| 690 | elsif($ysize>3) { |
| 691 | $im->arc($x+$xsize*0.53, $y+$ysize/2, |
| 692 | (0.75-0.75/$ysize)*$xsize, (0.725-0.725/$ysize)*$ysize, |
| 693 | 0,360,$white); |
| 694 | $im->fill($x+$xsize/2, $y+$ysize/2, $white); |
| 695 | $im->filledRectangle($x+$xsize*0.25, $y+$ysize/2, |
| 696 | $x+$xsize*1.1, $y+$ysize/2, |
| 697 | $white); |
| 698 | |
| 699 | } |
| 700 | $im->filledRectangle($x+0.85*$xsize, $y+$ysize/2, |
| 701 | $x+$xsize,$y+(3*$ysize/4)-1, |
| 702 | $yellow); |
| 703 | $im->filledRectangle($x+0.6*$xsize, $y+$ysize/2, |
| 704 | $x+$xsize,$y+(5*$ysize/8)-1, |
| 705 | $yellow); |
| 706 | return 1; |
| 707 | } |
| 708 | |
| 709 | sub draw_T { |
| 710 | |
| 711 | my ($im, $x, $y, $xsize, $ysize, $white) = @_; |
| 712 | my $red = $im->colorAllocate(255,0,0); |
| 713 | $im->filledRectangle($x, $y, $x+$xsize, $y+0.16*$ysize, $red); |
| 714 | $im->filledRectangle($x+0.42*$xsize, $y, $x+0.58*$xsize, $y+$ysize, $red); |
| 715 | return 1; |
| 716 | } |
| 717 | |
| 718 | |
| 719 | 1; |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 |