Debian Codegraph @

blib/lib/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::ICMscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/;
128
129 use varsscip-perl cpan TFBS v0.5.0 vars/ '@ISA';
130 use PDLscip-perl cpan TFBS v0.5.0 PDL/;
131 use strictscip-perl cpan TFBS v0.5.0 strict/;
132 use Bio::Root::Rootscip-perl cpan TFBS v0.5.0 Bio/Root/Root/;
133 use Bio::SeqIOscip-perl cpan TFBS v0.5.0 Bio/SeqIO/;
134 use TFBS::Matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/;
135 #use GD;
136 use File::Tempscip-perl cpan TFBS v0.5.0 File/Temp/ qw/:POSIX/;
137 @ISAscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/`@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 newscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/new(). {
169 my ($class, %args) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/`@_`.;
170 my $matrix = TFBS::Matrix->newscip-perl cpan TFBS v0.5.0 TFBS/Matrix/new().(%args, -matrixtype=>"ICM");
171 my $self = blessscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/bless(). $matrix, refscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/ref().($class) || $class;
172 $self->_check_ic_validityscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/_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_PWMscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/to_PWM(). {
192 my ($self) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/`@_`.;
193 $self->throwscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/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_logoscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/draw_logo(). {
226
227 no strict;
228 my $self = shiftscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/shift().;
229 my %args = (-xsize => 600,
230 -full_scale => 2.25,
231 -graph_title=> "",
232 -x_title => "",
233 -y_title => "",
234 @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/`@_`.);
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_logoscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/_draw_ps_logo().($self, @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/`@_`.);
243 }
244
245 requirescip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/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 intscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/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->newscip-perl cpan TFBS v0.5.0 GD/Image/new().($xsize, $ysize);
261 my $white = $image->colorAllocatescip-perl cpan TFBS v0.5.0 GD/Image/colorAllocate().(255,255,255);
262 my $black = $image->colorAllocatescip-perl cpan TFBS v0.5.0 GD/Image/colorAllocate().(0,0,0);
263 my $motif_size = $self->pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/pdl_matrix().->getdimscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/getdim().(0);
264 my $font = ((&GD::gdTinyFontscip-perl cpan TFBS v0.5.0 GD/gdTinyFont().(), &GD::gdSmallFontscip-perl cpan TFBS v0.5.0 GD/gdSmallFont().(), &GD::gdMediumBoldFontscip-perl cpan TFBS v0.5.0 GD/gdMediumBoldFont().(),
265 &GD::gdLargeFontscip-perl cpan TFBS v0.5.0 GD/gdLargeFont().(), &GD::gdGiantFontscip-perl cpan TFBS v0.5.0 GD/gdGiantFont().())[intscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/int().(($ysize-50)/100)]
266 or &GD::gdGiantFontscip-perl cpan TFBS v0.5.0 GD/gdGiantFont().());
267 my $title_font = ((&GD::gdSmallFontscip-perl cpan TFBS v0.5.0 GD/gdSmallFont().(), &GD::gdMediumBoldFontscip-perl cpan TFBS v0.5.0 GD/gdMediumBoldFont().(),
268 &GD::gdLargeFontscip-perl cpan TFBS v0.5.0 GD/gdLargeFont().(), &GD::gdGiantFontscip-perl cpan TFBS v0.5.0 GD/gdGiantFont().())[intscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/int().(($ysize-50)/100)]
269 or &GD::gdGiantFontscip-perl cpan TFBS v0.5.0 GD/gdGiantFont().());
270
271
272 # WRITE LABELS AND TITLE
273
274 # graph title #&GD::Font::MediumBold
275 $image->stringscip-perl cpan TFBS v0.5.0 GD/Image/string().($title_font,
276 $xsize/2-lengthscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/length().($args{-graph_title})* $title_font->widthscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/width().() /2,
277 $margin/2 - $title_font->heightscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/height().()/2,
278 $args{-graph_title}, $black);
279
280 # x_title
281 $image->stringscip-perl cpan TFBS v0.5.0 GD/Image/string().($font,
282 $xsize/2-lengthscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/length().($args{-x_title})*$font->widthscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/width().()/2,
283 $ysize-( $margin - $font->heightscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/height().()*0 - 5*$line_width)/2
284 - $font->heightscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/height().()/2*0,
285 $args{-x_title},
286 $black);
287 # y_title
288 $image->stringUpscip-perl cpan TFBS v0.5.0 GD/Image/stringUp().($font,
289 ($margin -$font->widthscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/width().()- 5*$line_width)/2
290 - $font->heightscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/height().()/2 ,
291 $ysize/2+lengthscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/length().($args{'-y_title'})*$font->widthscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/width().()/2,
292 $args{'-y_title'}, $black);
293
294
295 # DRAW AXES
296
297 # vertical: (top left to bottom right)
298 $image->filledRectanglescip-perl cpan TFBS v0.5.0 GD/Image/filledRectangle().($margin-$line_width, $margin-$line_width,
299 $margin-1, $ysize-$margin+$line_width,
300 $black);
301 # horizontal: (ditto)
302 $image->filledRectanglescip-perl cpan TFBS v0.5.0 GD/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->filledRectanglescip-perl cpan TFBS v0.5.0 GD/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->stringscip-perl cpan TFBS v0.5.0 GD/Image/string().($font,
317 $margin-5*$line_width - $font->widthscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/width().,
318 $ysize - $margin - $i*$ic_1 - $font->heightscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/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_Ascip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/draw_A().,
327 C => \&draw_Cscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/draw_C().,
328 G => \&draw_Gscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/draw_G().,
329 T => \&draw_Tscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/draw_T(). );
330
331 my $horiz_step = ($xsize -2*$margin) / $motif_size;
332 foreach my $i (0..$motif_size) {
333
334 $image->filledRectanglescip-perl cpan TFBS v0.5.0 GD/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}) = listscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/list(). $self->pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/pdl_matrix().->slicescip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/slice().($i);
344
345 # sort nucleotides by increasing information content
346 my @draw_order = sortscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/sort(). {$ic{$ascip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/$a.}<=>$ic{$bscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/$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 = intscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/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 refscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/ref().($args{'-error_bars'}) eq "ARRAY") {
364 my $sd_pix = intscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/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->linescip-perl cpan TFBS v0.5.0 GD/Image/line().($xpos - $xlettersize/8, $yb,
375 $xpos + $xlettersize/8, $yb,
376 $black);
377 }
378
379 $image->linescip-perl cpan TFBS v0.5.0 GD/Image/line().($xpos, $yt, $xpos, $yb, $black);
380 $image->linescip-perl cpan TFBS v0.5.0 GD/Image/line().($xpos - 1 , $ybottom, $xpos+1, $ybottom, $black);
381 $image->linescip-perl cpan TFBS v0.5.0 GD/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->stringscip-perl cpan TFBS v0.5.0 GD/Image/string().($font,
390 $margin + ($i+0.5)*$horiz_step - $font->widthscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/width().()/2,
391 $ysize - $margin +5*$line_width,
392 $i+1,
393 $black);
394 }
395
396 # print $args{-file};
397 if ($args{-file}) {
398 openscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/open(). (PNGFILE, ">".$args{-file})
399 or $self->throwscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/throw().("Could not write to ".$args{-file});
400 print PNGFILE $image->pngscip-perl cpan TFBS v0.5.0 GD/Image/png().;
401 close PNGFILE;
402 }
403 return $image;
404 }
405
406
407
408
409
410
411 sub total_icscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/total_ic(). {
412 return $_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/`@_`.[0]->pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/pdl_matrix().->sumscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/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_logoscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/_draw_ps_logo().{
441 my $self = shiftscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/shift().;
442 my %args = (-xsize => 600,
443 -full_scale => 2.25,
444 -graph_title=> "",
445 -x_title => "",
446 -y_title => "",
447 @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/`@_`.);
448
449 my $xsize= $args{'-xsize'};
450 my $max_ysize= $args{'-ysize'} ||intscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/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= intscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/int(). $ysize*0.68;
477 my $fontwidth=1.5*($xsize/$self->lengthscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/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->lengthscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/length().()) -0.006*$xsize;
529 my $x_now;
530
531 for(my $i=1; $i<=$self->lengthscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/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->lengthscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/length().()-1 ) { # get the $i-th column of matrix
540
541 my %ic;
542 ($ic{A}, $ic{C}, $ic{G}, $ic{T}) = listscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/list(). $self->pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/pdl_matrix().->slicescip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/slice().($i);
543 my @draw_order = sortscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/sort(). {$ic{$ascip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/$a.}<=>$ic{$bscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/$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 openscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/open(). (PSFILE, ">".$args{-file})
563 or $self->throwscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/throw().("Could not write to ".$args{-file});
564 print PSFILE $out;
565 close PSFILE;
566 }
567 if ($args{-pdf}){
568
569 systemscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/system(). "ps2pdf $args{-file} ".$args{-file}.".pdf ";
570 systemscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/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_validityscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/_check_ic_validity(). {
609 my ($self) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/`@_`.;
610 # to do
611 }
612
613 sub DESTROYscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/DESTROY(). {
614 # nothing
615 }
616
617
618 #################################################################
619 # UTILITY FUNCTIONS
620 #################################################################
621
622
623 # letter drawing routines
624
625 sub draw_Ascip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/draw_A(). {
626
627 my ($im, $x, $y, $xsize, $ysize, $white) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/`@_`.;
628 my $green = $im->colorAllocatescip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/colorAllocate().(0,255,0);
629 my $outPoly = GD::Polygon->newscip-perl cpan TFBS v0.5.0 GD/Polygon/new().();
630 $outPoly->addPtscip-perl cpan TFBS v0.5.0 GD/Polygon/addPt().($x, $y+$ysize);
631 $outPoly->addPtscip-perl cpan TFBS v0.5.0 GD/Polygon/addPt().($x+$xsize*.42, $y);
632 $outPoly->addPtscip-perl cpan TFBS v0.5.0 GD/Polygon/addPt().($x+$xsize*.58, $y);
633 $outPoly->addPtscip-perl cpan TFBS v0.5.0 GD/Polygon/addPt().($x+$xsize, $y+$ysize);
634 $outPoly->addPtscip-perl cpan TFBS v0.5.0 GD/Polygon/addPt().($x+0.85*$xsize, $y+$ysize);
635 $outPoly->addPtscip-perl cpan TFBS v0.5.0 GD/Polygon/addPt().($x+0.725*$xsize, $y+0.75*$ysize);
636 $outPoly->addPtscip-perl cpan TFBS v0.5.0 GD/Polygon/addPt().($x+0.275*$xsize, $y+0.75*$ysize);
637 $outPoly->addPtscip-perl cpan TFBS v0.5.0 GD/Polygon/addPt().($x+0.15*$xsize, $y+$ysize);
638 $im->filledPolygonscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/filledPolygon().($outPoly, $green);
639 if ($ysize>8) {
640 my $inPoly = GD::Polygon->newscip-perl cpan TFBS v0.5.0 GD/Polygon/new().();
641 $inPoly->addPtscip-perl cpan TFBS v0.5.0 GD/Polygon/addPt().($x+$xsize*.5, $y+0.2*$ysize);
642 $inPoly->addPtscip-perl cpan TFBS v0.5.0 GD/Polygon/addPt().($x+$xsize*.34, $y+0.6*$ysize-1);
643 $inPoly->addPtscip-perl cpan TFBS v0.5.0 GD/Polygon/addPt().($x+$xsize*.64, $y+0.6*$ysize-1);
644 $im->filledPolygonscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/filledPolygon().($inPoly, $white);
645 }
646 return 1;
647 }
648
649 sub draw_Cscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/draw_C(). {
650 my ($im, $x, $y, $xsize, $ysize, $white) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/`@_`.;
651 my $blue = $im->colorAllocatescip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/colorAllocate().(0,0,255);
652 $im->arcscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/arc().($x+$xsize*0.54, $y+$ysize/2,1.08*$xsize,$ysize,0,360,$blue);
653 $im->fillscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/fill().($x+$xsize/2, $y+$ysize/2, $blue);
654 if ($ysize>12) {
655 $im->arcscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/arc().($x+$xsize*0.53, $y+$ysize/2,
656 0.75*$xsize, (0.725-0.725/$ysize)*$ysize,
657 0,360,$white);
658 $im->fillscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/fill().($x+$xsize/2, $y+$ysize/2, $white);
659 $im->filledRectanglescip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/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->arcscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/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->fillscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/fill().($x+$xsize/2, $y+$ysize/2, $white);
668 $im->filledRectanglescip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/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_Gscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/draw_G(). {
677 my ($im, $x, $y, $xsize, $ysize, $white) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/`@_`.;
678 my $yellow = $im->colorAllocatescip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/colorAllocate().(200,200,0);
679 $im->arcscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/arc().($x+$xsize*0.54, $y+$ysize/2,1.08*$xsize,$ysize,0,360,$yellow);
680 $im->fillscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/fill().($x+$xsize/2, $y+$ysize/2, $yellow);
681 if ($ysize>20) {
682 $im->arcscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/arc().($x+$xsize*0.53, $y+$ysize/2,
683 0.75*$xsize, (0.725-0.725/$ysize)*$ysize,
684 0,360,$white);
685 $im->fillscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/fill().($x+$xsize/2, $y+$ysize/2, $white);
686 $im->filledRectanglescip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/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->arcscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/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->fillscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/fill().($x+$xsize/2, $y+$ysize/2, $white);
695 $im->filledRectanglescip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/filledRectangle().($x+$xsize*0.25, $y+$ysize/2,
696 $x+$xsize*1.1, $y+$ysize/2,
697 $white);
698
699 }
700 $im->filledRectanglescip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/filledRectangle().($x+0.85*$xsize, $y+$ysize/2,
701 $x+$xsize,$y+(3*$ysize/4)-1,
702 $yellow);
703 $im->filledRectanglescip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/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_Tscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/draw_T(). {
710
711 my ($im, $x, $y, $xsize, $ysize, $white) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/`@_`.;
712 my $red = $im->colorAllocatescip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/colorAllocate().(255,0,0);
713 $im->filledRectanglescip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/filledRectangle().($x, $y, $x+$xsize, $y+0.16*$ysize, $red);
714 $im->filledRectanglescip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/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