Debian Codegraph @

TFBS/Matrix/PFM.pm

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 # TFBS module for TFBS::Matrix::PFM
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::PFM - class for raw position frequency matrix patterns
13
14
15 =head1 SYNOPSIS
16
17 =over 4
18
19 =item * creating a TFBS::Matrix::PFM object manually:
20
21
22 my $matrixref = [ [ 12, 3, 0, 0, 4, 0 ],
23 [ 0, 0, 0, 11, 7, 0 ],
24 [ 0, 9, 12, 0, 0, 0 ],
25 [ 0, 0, 0, 1, 1, 12 ]
26 ];
27 my $pfm = TFBS::Matrix::PFM->new(-matrix => $matrixref,
28 -name => "MyProfile",
29 -ID => "M0001"
30 );
31 # or
32
33 my $matrixstring =
34 "12 3 0 0 4 0\n0 0 0 11 7 0\n0 9 12 0 0 0\n0 0 0 1 1 12";
35
36 my $pfm = TFBS::Matrix::PFM->new(-matrixstring => $matrixstring,
37 -name => "MyProfile",
38 -ID => "M0001"
39 );
40
41
42 =item * retrieving a TFBS::Matix::PFM object from a database:
43
44 (See documentation of individual TFBS::DB::* modules to learn
45 how to connect to different types of pattern databases and
46 retrieve TFBS::Matrix::* objects from them.)
47
48 my $db_obj = TFBS::DB::JASPAR2->new
49 (-connect => ["dbi:mysql:JASPAR2:myhost",
50 "myusername", "mypassword"]);
51 my $pfm = $db_obj->get_Matrix_by_ID("M0001", "PFM");
52 # or
53 my $pfm = $db_obj->get_Matrix_by_name("MyProfile", "PFM");
54
55
56 =item * retrieving list of individual TFBS::Matrix::PFM objects
57 from a TFBS::MatrixSet object
58
59 (See the L<TFBS::MatrixSet> to learn how to create
60 objects for storage and manipulation of multiple matrices.)
61
62 my @pfm_list = $matrixset->all_patterns(-sort_by=>"name");
63
64
65 =item * convert a raw frequency matrix to other matrix types:
66
67 my $pwm = $pfm->to_PWM(); # convert to position weight matrix
68 my $icm = $icm->to_ICM(); # convert to information con
69
70 =back
71
72 =head1 DESCRIPTION
73
74 TFBS::Matrix::PFM is a class whose instances are objects representing
75 raw position frequency matrices (PFMs). A PFM is derived from N
76 nucleotide patterns of fixed size, e.g. the set of sequences
77
78 AGGCCT
79 AAGCCT
80 AGGCAT
81 AAGCCT
82 AAGCCT
83 AGGCAT
84 AGGCCT
85 AGGCAT
86 AGGTTT
87 AGGCAT
88 AGGCCT
89 AGGCCT
90
91
92 will give the matrix:
93
94 A:[ 12 3 0 0 4 0 ]
95 C:[ 0 0 0 11 7 0 ]
96 G:[ 0 9 12 0 0 0 ]
97 T:[ 0 0 0 1 1 12 ]
98
99 which contains the count of each nucleotide at each position in the
100 sequence. (If you have a set of sequences as above and want to
101 create a TFBS::Matrix::PFM object out of them, have a look at
102 TFBS::PatternGen::SimplePFM module.)
103
104 PFMs are easily converted to other types of matrices, namely
105 information content matrices and position weight matrices. A
106 TFBS::Matrix::PFM object has the methods to_ICM and to_PWM which
107 do just that, returning a TFBS::Matrix::ICM and TFBS::Matrix::PWM
108 objects, respectively.
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
126 # The code begins HERE:
127
128 package TFBS::Matrix::PFMscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/;
129
130 use varsscip-perl cpan TFBS v0.5.0 vars/ '@ISA';
131 use PDLscip-perl cpan TFBS v0.5.0 PDL/;
132 use strictscip-perl cpan TFBS v0.5.0 strict/;
133 use Bio::Root::Rootscip-perl cpan TFBS v0.5.0 Bio/Root/Root/;
134 use Bio::SeqIOscip-perl cpan TFBS v0.5.0 Bio/SeqIO/;
135 use TFBS::Matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/;
136 use TFBS::Matrix::ICMscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/;
137 use TFBS::Matrix::PWMscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PWM/;
138 use File::Tempscip-perl cpan TFBS v0.5.0 File/Temp/ qw/:POSIX/;
139 @ISAscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/`@ISA`. = qw(TFBS::Matrix Bio::Root::Root);
140
141 use constantscip-perl cpan TFBS v0.5.0 constant/ EXACT_SCHNEIDER_MAXscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/EXACT_SCHNEIDER_MAX(). => 30;
142
143
144 #######################################################
145 # PUBLIC METHODS
146 #######################################################
147
148 =head2 new
149
150 Title : new
151 Usage : my $pfm = TFBS::Matrix::PFM->new(%args)
152 Function: constructor for the TFBS::Matrix::PFM object
153 Returns : a new TFBS::Matrix::PFM object
154 Args : # you must specify either one of the following three:
155
156 -matrix, # reference to an array of arrays of integers
157 #or
158 -matrixstring,# a string containing four lines
159 # of tab- or space-delimited integers
160 #or
161 -matrixfile, # the name of a file containing four lines
162 # of tab- or space-delimited integers
163 #######
164
165 -name, # string, OPTIONAL
166 -ID, # string, OPTIONAL
167 -class, # string, OPTIONAL
168 -tags # an array reference, OPTIONAL
169 Warnings : Warns if the matrix provided has columns with different
170 sums. Columns with different sums contradict the usual
171 origin of matrix data and, unless you are absolutely sure
172 that column sums _should_ be different, it would be wise to
173 check your matrices.
174
175 =cut
176
177 sub newscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/new(). {
178 my ($class, %args) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/`@_`.;
179 my $matrix = TFBS::Matrix->newscip-perl cpan TFBS v0.5.0 TFBS/Matrix/new().(%args, -matrixtype=>"PFM");
180 my $self = blessscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/bless(). $matrix, refscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/ref().($class) || $class;
181 $self->_check_column_sumsscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/_check_column_sums().();
182 return $self;
183 }
184
185 =head2 column_sum
186
187 Title : column_sum
188 Usage : my $nr_sequences = $pfm->column_sum()
189 Function: calculates the sum of elements of one column
190 (the first one by default) which normally equals the
191 number of sequences used to derive the PFM.
192 Returns : the sum of elements of one column (an integer)
193 Args : columnn number (starting from 1), OPTIONAL - you DO NOT
194 need to specify it unless you are dealing with a matrix
195
196 =cut
197
198 sub column_sumscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/column_sum(). {
199 my ($self, $column) = (@_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/`@_`.,1);
200 return $self->pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/pdl_matrix().->slicescip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/slice().($column-1)->sumscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/sum().;
201
202 }
203
204 =head2 to_PWM
205
206 Title : to_PWM
207 Usage : my $pwm = $pfm->to_PWM()
208 Function: converts a raw frequency matrix (a TFBS::Matrix::PFM object)
209 to position weight matrix. At present it assumes uniform
210 background distribution of nucleotide frequencies.
211 Returns : a new TFBS::Matrix::PWM object
212 Args : none; in the future releases, it should be able to accept
213 a user defined background probability of the four
214 nucleotides
215
216 =cut
217
218 sub to_PWMscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/to_PWM(). {
219 my ($self, %args) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/`@_`.;
220 my $bg = ($args{'-bg_probabilities' } || $self->{'bg_probabilities'});
221 my $bg_pdl =
222 transposescip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/transpose(). pdlscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/pdl(). ($bg->{'A'}, $bg->{'C'}, $bg->{'G'}, $bg->{'T'});
223 my $nseqs = $self->pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/pdl_matrix().->sumscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/sum(). / $self->lengthscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/length().;
224 my $q_pdl = ($self->pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/pdl_matrix(). +$bg_pdl*sqrtscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/sqrt().($nseqs))
225 /
226 ($nseqs + sqrtscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/sqrt().($nseqs));
227 my $pwm_pdl = log2scip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/log2().(4*$q_pdl);
228
229 my $PWM = TFBS::Matrix::PWM->newscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PWM/new().
230 ( (mapscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/map(). {("-$_", $self->{$_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$_.}) } keysscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/keys(). %$self),
231 # do not want tags to point to the same arrayref as in $self:
232 -tags => \%{ $self->{'tags'}},
233 -bg_probabilities => \%{ $self->{'bg_probabilities'}},
234 -matrix => $pwm_pdl
235 );
236 return $PWM;
237
238 }
239
240
241 =head2 to_ICM
242
243 Title : to_ICM
244 Usage : my $icm = $pfm->to_ICM()
245 Function: converts a raw frequency matrix (a TFBS::Matrix::PFM object)
246 to information content matrix. At present it assumes uniform
247 background distribution of nucleotide frequencies.
248 Returns : a new TFBS::Matrix::ICM object
249 Args : -small_sample_correction # undef (default), 'schneider' or 'pseudocounts'
250
251 How a PFM is converted to ICM:
252
253 For a PFM element PFM[i,k], the probability without
254 pseudocounts is estimated to be simply
255
256 p[i,k] = PFM[i,k] / Z
257
258 where
259 - Z equals the column sum of the matrix i.e. the number of motifs used
260 to construct the PFM.
261 - i is the column index (position in the motif)
262 - k is the row index (a letter in the alphacer, here k is one of
263 (A,C,G,T)
264
265 Here is how one normally calculates the pseudocount-corrected positional
266 probability p'[i,j]:
267
268 p'[i,k] = (PFM[i,k] + 0.25*sqrt(Z)) / (Z + sqrt(Z))
269
270 0.25 is for the flat distribution of nucleotides, and sqrt(Z) is the
271 recommended pseudocount weight. In the general case,
272
273 p'[i,k] = (PFM[i,k] + q[k]*B) / (Z + B)
274
275 where q[k] is the background distribution of the letter (nucleotide) k,
276 and B an arbitrary pseudocount value or expression (for no pseudocounts
277 B=0).
278
279 For a given position i, the deviation from random distribution in bits
280 is calculated as (Baldi and Brunak eq. 1.9 (2ed) or 1.8 (1ed)):
281
282 - for an arbitrary alphabet of A letters:
283
284 D[i] = log2(A) + sum_for_all_k(p[i,k]*log2(p[i,k]))
285
286 - special case for nucleotides (A=4)
287
288 D[i] = 2 + sum_for_all_k(p[i,k]*log2(p[i,k]))
289
290 D[i] equals the information content of the position i in the motif. To
291 calculate the entire ICM, you have to calculate the contrubution of each
292 nucleotide at a position i to D[i], i.e.
293
294 ICM[i,k] = p'[i,k] * D[i]
295
296
297 =cut
298
299 sub to_ICMscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/to_ICM(). {
300 my ($self, %args) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/`@_`.;
301 my $bg = ($args{'-bg_probabilities' } || $self->{'bg_probabilities'});
302
303
304
305 # compute ICM
306
307 my $bg_pdl =
308 transposescip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/transpose(). pdlscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/pdl(). ($bg->{'A'}, $bg->{'C'}, $bg->{'G'}, $bg->{'T'});
309 my $Z_pdl = $self->pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/pdl_matrix().->xchgscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/xchg().(0,1)->sumoverscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/sumover().;
310
311 # pseudocount calculation
312
313 my $B = 0;
314 if (lc($args{'-small_sample_correction'} or "") eq "pseudocounts") {
315
316 $B = sqrtscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/sqrt().($Z_pdl);
317 }
318 else {
319 $B = 0; # do not add pseudocounts
320 }
321
322
323 my $p_pdl = ($self->pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/pdl_matrix(). +$bg_pdl*$B)/ ($Z_pdl + $B);
324 my $plog_pdl = $p_pdl*log2scip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/log2().($p_pdl);
325 $plog_pdl = $plog_pdl->badmaskscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/badmask().(0);
326 my $D_pdl = 2 + $plog_pdl->xchgscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/xchg().(0,1)->sumoverscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/sumover().;
327 my $ic_pdl = $p_pdl * $D_pdl;
328
329
330 # apply Schneider correction if requested
331
332 if (lc($args{'-small_sample_correction'} or "") eq "schneider") {
333 my $columnsum_pdl = $ic_pdl->transposescip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/transpose().->sumoverscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/sumover().;
334 my $corrected_columnsum_pdl =
335 $columnsum_pdl
336 + _schneider_correctionscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/_schneider_correction(). ($self->pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/pdl_matrix()., $bg_pdl);
337 $ic_pdl *= $corrected_columnsum_pdl/$columnsum_pdl;
338 }
339
340 # construct and return an ICM object
341
342 my $ICM = TFBS::Matrix::ICM->newscip-perl cpan TFBS v0.5.0 TFBS/Matrix/ICM/new().
343 ( (mapscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/map(). {("-$_" => $self->{$_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$_.})} keysscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/keys(). %$self),
344 -tags => \%{ $self->{'tags'}},
345 -bg_probabilities => \%{ $self->{'bg_probabilities'}},
346 -matrix => $ic_pdl
347 );
348 return $ICM;
349
350 }
351
352
353 =head2 draw_logo
354
355 Title : draw_logo
356 Usage : my $gd_image = $pfm->draw_logo()
357 Function: draws a sequence logo; similar to the
358 method in TFBS::Matrix::ICM, but can automatically calculate
359 error bars for drawing
360 Returns : a GD image object (see documentation of GD module)
361 Args : many; PFM-specific options are:
362 -small_sample_correction # One of
363 # "Schneider" (uses correction
364 # described by Schneider et al.
365 # (Schneider t et al. (1986) J.Biol.Chem.
366 # "pseudocounts" - standard pseudocount
367 # correction, more suitable for
368 # PFMs with large r column sums
•369 # If the parameter is omitted, small
370 # sample correction is not applied
371
372 -draw_error_bars # if true, adds error bars to each position
373 # in the logo. To calculate the error bars,
374 # it uses the -small_sample_connection
375 # argument if explicitly set,
376 # or "Schneider" by default
377 For other args, see draw_logo entry in TFBS::Matrix::ICM documentation
378
379 =cut
380
381 sub draw_logoscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/draw_logo(). {
382 my ($self, %args) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/`@_`.;
383 if ($args{'-draw_error_bars'}) {
384 $args{'-small_sample_correction'} ||= "Schneider"; # default Schneider
385
386 my $pdl_no_correction =
387 $self->to_ICMscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/to_ICM().()
388 ->pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/pdl_matrix().->transposescip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/transpose().->sumoverscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/sumover().;
389 my $pdl_with_correction =
390 $self->to_ICMscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/to_ICM().(-small_sample_correction
391 => $args{'-small_sample_correction'})
392 ->pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/pdl_matrix().->transposescip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/transpose().->sumoverscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/sumover().;
393
394 $args{'-error_bars'} =
395 [listscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/list(). ($pdl_no_correction - $pdl_with_correction)];
396
397 }
398 $self->to_ICMscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/to_ICM().(%args)->draw_logoscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/draw_logo().(%args);
399 }
400
401
402 =head2 add_PFM
403
404 Title : add_PFM
405 Usage : $pfm->add_PFM($another_pfm)
406 Function: adds the values of $pnother_pfm matrix to $pfm
407 Returns : reference to the updated $pfm object
408 Args : a TFBS::Matrix::PFM object
409
410 =cut
411
412
413 sub add_PFMscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/add_PFM(). {
414 my ($self, $pfm) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/`@_`.;
415 $pfm->isascip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/isa().("TFBS::Matrix::PFM")
416 or $self->throwscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/throw().("Wrong or no argument passed to add_PFM");
417 my $sum = $self->pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/pdl_matrix(). + $pfm->pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/pdl_matrix().;
418 $self->set_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/set_matrix().($sum);
419 return $self;
420 }
421
422
423
424 =head2 name
425
426 =head2 ID
427
428 =head2 class
429
430 =head2 matrix
431
432 =head2 length
433
434 =head2 revcom
435
436 =head2 rawprint
437
438 =head2 prettyprint
439
440 The above methods are common to all matrix objects. Please consult
441 L<TFBS::Matrix> to find out how to use them.
442
443 =cut
444
445 ###############################################
446 # PRIVATE METHODS
447 ###############################################
448
449 sub _check_column_sumsscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/_check_column_sums(). {
450 my ($self) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/`@_`.;
451 my $pdl = $self->pdl_matrixscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/pdl_matrix().->severscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/sever().();
452 my $rowsums = $pdl->xchgscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/xchg().(0,1)->sumoverscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/sumover().();
453 if ($rowsums->wherescip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/where().($rowsums != $rowsums->slicescip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/slice().(0))->getdimscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/getdim().(0) > 0) {
454 $self->warnscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/warn().("PFM for ".$self->{ID}." has unequal column sums");
455 }
456 }
457
458 sub DESTROYscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/DESTROY(). {
459 # does nothing
460 }
461
462 ###############################################
463 # UTILITY FUNCTIONS
464 ###############################################
465
466 sub log2scip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/log2(). { logscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/log().($_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/`@_`.[0]) / log(2); }
467
468
469 sub _schneider_correction {
470 my ($pdl, $bg_pdl) = @_;
471 my $Hg = -sum ($bg_pdl*log2($bg_pdl));
472 my (@Hnbs, %saved_Hnb);
473 my $is_flat = _is_bg_flat(list $bg_pdl);
474
475 my @factorials = (1);
476 if (min($pdl->transpose->sumover) <= EXACT_SCHNEIDER_MAX) {
477 foreach my $i (1..max($pdl->transpose->sumover)) {
478 $factorials[$i] =$factorials[$i-1] * $i;
479 }
480 }
481 my @column_sums = list $pdl->transpose->sumover;
482 foreach my $colsum (@column_sums) {
483 if (defined($saved_Hnb{$colsum})) {
484 push @Hnbs, $saved_Hnb{$colsum};
485 }
486 else {
487 my $Hnb;
488 if ($colsum <= EXACT_SCHNEIDER_MAX) {
489 if ($is_flat) {
490 $Hnb = _schneider_Hnb_precomputed($colsum);
491 }
492 else {
493 $Hnb = _schneider_Hnb_exact($colsum, $bg_pdl,
494 \@factorials);
495 }
496 }
497 else {
498 $Hnb = _schneider_Hnb_approx($colsum, $Hg);
499
500 }
501 $saved_Hnb{$colsum} = $Hnb;
502 push @Hnbs, $Hnb;
503 }
504 }
505 return -$Hg + pdl(@Hnbs);
506
507 }
508
509
510 sub _schneider_Hnb_exact {
511 my ($n, $bg_pdl, $rFactorial) = @_;
512
513 my $is_flat = _is_bg_flat(list $bg_pdl);
514 return 0 if $n==1;
515 # my @fctrl = (1);
516 # foreach my $i (1..max($pdl->transpose->sumover)) {
517 # $rFactorial->[$i] =$rFactorial->[$i-1] * $i;
518 # }
519 # my @colsum = list $pdl->transpose->sumover;
520 my ($na, $nc, $ng, $nt) = ($n, 0,0,0);
521 # my $n = $colsum[0];
522 my $E_Hnb=0;
523 while (1) {
524 my $ns_pdl = pdl [$na, $nc, $ng, $nt];
525 my $Pnb = ($rFactorial->[$n]
526 /
527 ($rFactorialscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$rFactorial.->[$nascip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$na.]
528 *$rFactorialscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$rFactorial.->[$ncscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$nc.]
529 *$rFactorialscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$rFactorial.->[$ngscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$ng.]
530 *$rFactorialscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$rFactorial.->[$ntscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$nt.])
531 )*prod($bg_pdlscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$bg_pdl.->transposescip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/transpose().**pdlscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/pdl().($nascip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$na., $ncscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$nc., $ngscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$ng., $ntscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$nt.));
532 my $Hnb = -1 * sumscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/sum().(($ns_pdlscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$ns_pdl./$nscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$n.)*log2scip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/log2().($ns_pdlscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$ns_pdl./$nscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$n.)->badmaskscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/badmask().(0));
533 $E_Hnbscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$E_Hnb. += $Pnbscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$Pnb.*$Hnb;
534
535
536 if ($ntscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$nt.) {
537 if ($ngscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$ng.) { $ngscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$ng.--; $ntscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$nt.++, }
538 elsif ($ncscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$nc.) { $ncscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$nc.--; $ngscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$ng. = $ntscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$nt.+1; $ntscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$nt. = 0; }
539 elsif ($nascip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$na.) { $nascip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$na.--; $ncscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$nc. = $ntscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$nt.+1; $ntscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$nt. = 0; }
540 else { last; }
541 }
542 else {
543 if ($ngscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$ng.) { $ngscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$ng.--; $ntscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$nt.++, }
544 elsif ($ncscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$nc.) { $ncscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$nc.--; $ngscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$ng.++; }
545 else { $nascip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$na.--; $ncscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$nc.++; $ntscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$nt. = 0; }
546 }
547 }
548 return $E_Hnbscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/$E_Hnb.;
549 }
550
551
552
553 sub _schneider_Hnb_approxscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/_schneider_Hnb_approx(). {
554 my ($colsum, $Hg) = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/`@_`.;
555 return $Hg -3/(2*logscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/log().(2)*$colsum);
556
557 }
558
559
560
561 sub _schneider_Hnb_precomputedscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/_schneider_Hnb_precomputed(). {
562 my $i = shiftscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/shift().;
563 if ($i<1 or $i>30) {
564 diescip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/die(). "Precomputed params only available for colsums 1 to 30)";
565 }
566 my @precomputed =
567 (
568 0, # 1
569 0.75, # 2
570 1.11090234442608, # 3
571 1.32398964833609, # 4
572 1.46290503577084, # 5
573 1.55922640783176, # 6
574 1.62900374746751, # 7
575 1.68128673969433, # 8
576 1.7215504663901, # 9
577 1.75328193031842, # 10
578 1.77879136615189, # 11
579 1.79965855531179, # 12
580 1.81699248819687, # 13
581 1.8315892710679, # 14
582 1.84403166371213, # 15
583 1.85475371994775, # 16
584 1.86408383599326, # 17
585 1.87227404728809, # 18
586 1.87952034817826, # 19
587 1.88597702438913, # 20
588 1.89176691659196, # 21
589 1.89698887214968, # 22
590 1.90172322434865, # 23
591 1.90603586889234, # 24
592 1.90998133028897, # 25
593 1.91360509239859, # 26
594 1.91694538711761, # 27
595 1.92003457997914, # 28
596 1.92290025302018, # 29
597 1.92556605820924, # 30
598 );
599 return $precomputed[$i-1];
600 }
601
602
603 sub _is_bg_flatscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/_is_bg_flat(). {
604 my @bg = @_scip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/`@_`.;
605 my $ref = shiftscip-perl cpan TFBS v0.5.0 TFBS/Matrix/PFM/shift().;
606 foreach my $other (@bg) {
607 return 0 unless $ref==$other;
608 }
609 return 1;
610 }
611
612
613 1;