Seeder.pm

filter: Statementstmt 80.4% 103 / 128 Branchbran 31.8% 7 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.5% 14 / 16 Podpod 100% 8 / 8 totaltotal 75.8% 132 / 174 CC 20 SCAR 23.0
CC
20
Cov
73%
execution_time
42.0
encode
12.0
read_hd_index
3.0
CRAP 9.9
« Seeder/Background.pm ↑ Summary Seeder/Index.pm »
1 package Seeder;
2  
3 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Algorithm::Loops qw(NestedLoops);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 our @EXPORT;
11 4 our %EXPORT_TAGS = (
12 'all' => [
13 qw(
14 read_hd_index
15 generate_oligo
16 lookup_coord
17 bc_factor
18 generate_hd_index
19 execution_time
20 encode
21 decode
22 )
23 ]
24 );
25 4 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
26  
27 =head1 NAME
28  
29 Seeder - Motif discovery in DNA sequences
30  
31 =head1 VERSION
32  
33 Version 0.01
34  
35 =cut
36  
37 4 our $VERSION = '0.01';
38  
39 =head1 DESCRIPTION
40  
41 This module is a base class and is not meant to be instantiated itself.
42  
43 Seeder is a framework for DNA motif discovery. It is designed for efficient
44 and reliable prediction of regulatory motifs in eukaryotic promoters. In order
45 to generate DNA motifs, you need one positive set of DNA sequences in fasta
46 format (believe to contain a similar cis-regulatory element) and a background
47 set of DNA sequences in fasta format.
48  
49 To discover motifs in DNA sequences, follow this sequence:
50  
51 (1) Generation of the index (this structure improves the performance of HD
52 calculation). Restrict seed width to between 6 and 8.
53  
54 use Seeder::Index;
55 my $index = Seeder::Index->new(
56 seed_width => "6",
57 out_file => "6.index",
58 );
59 $index->get_index;
60
61 (2) Generation of the background distributions.
62
63 use Seeder::Background;
64 my $background = Seeder::Background->new(
65 seed_width => "6",
66 strand => "revcom",
67 hd_index_file => "6.index",
68 seq_file => "seqs.fasta",
69 out_file => "seqs.bkgd",
70 );
71 $background->get_background;
72
73 (3) Motif discovery.
74
75 use Seeder::Finder;
76 my $finder = Seeder::Finder->new(
77 seed_width => "6",
78 strand => "revcom",
79 motif_width => "12",
80 n_motif => "1",
81 hd_index_file => "6.index",
82 seq_file => "prom.fasta",
83 bkgd_file => "seqs.bkgd",
84 out_file => "prom.finder",
85 );
86 $finder->find_motifs;
87
88 =head1 EXPORT
89  
90 None by default
91  
92 =head1 FUNCTIONS
93  
94 =head2 read_hd_index
95  
96 Title : read_hd_index
97 Usage : $self->read_hd_index;
98 Function: read the index file
99 Returns : reference to a 2D array of positive integers
100 Args : none
101  
102 =cut
103  
104 1 ▶sub read_hd_index {
Subroutine read_hd_index: called CC=3 SCAR=11.0
Pod documented
105 1 my $self = shift;
106 1 my @hd_index;
107 1 ▶ open( IN, "$self->{hd_index_file}" )
Branch: 1 branch
BranchTrueFalseTotal
open IN, qq[$self->{"hd_index_file"}] or croak(qq[Cannot open $self->{"hd_index_file"}\n]) 1 0 1
108 or croak "Cannot open $self->{hd_index_file}\n";
109 1 ▶ while (<IN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline IN)) 4096 1 4097
110 4096 push @hd_index, [ split( m{\s}, $_ ) ];
111 }
112 1 $self->{hd_index_ref} = \@hd_index;
113 1 return \@hd_index;
114 }
115  
116 =head2 generate_oligo
117  
118 Title : generate_oligo
119 Usage : $self->generate_oligo;
120 Function: generate all combinations of nucleotides for a given word length,
121 represented by numbers (0=>A, 1=>C, 2=>G, 3=>T)
122 Returns : reference to a 2D array of oligos
123 Args : none
124  
125 =cut
126  
127 2 ▶sub generate_oligo {
Subroutine generate_oligo: called CC=2 SCAR=6.9
Pod documented
128 2 my $self = shift;
129 2 my @position;
130 2 for my $depth ( 0 .. $self->{seed_width} - 1 ) {
131 12 push @position, [qw(0 1 2 3)];
132 }
133 2 ▶ my @oligo = NestedLoops( \@position, sub { [@_] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
134 2 $self->{oligo_ref} = \@oligo;
135 2 return \@oligo;
136 }
137  
138 =head2 lookup_coord
139  
140 Title : lookup_coord
141 Usage : $self->lookup_coord;
142 Function: generate indices for lookup (HD calculation)
143 Returns : reference to 2D arrays of begin/end lookup indices
144 Args : none
145  
146 =cut
147  
148 2 ▶sub lookup_coord {
Subroutine lookup_coord: called CC=3 SCAR=11.0
Pod documented
149 2 my $self = shift;
150 2 my $bc_factor_ref = $self->bc_factor;
151 2 my ( @from, @to );
152 2 for my $depth ( 0 .. $#$bc_factor_ref ) {
153 12 $from[$depth][0] = 0;
154 12 $to[$depth][0] = 0;
155 12 for my $limit ( 1 .. $#{ $bc_factor_ref->[$depth] } ) {
156 42 $from[$depth][$limit] = $to[$depth][ $limit - 1 ] + 1;
157 42 $to[$depth][$limit] =
158 $from[$depth][$limit] + $bc_factor_ref->[$depth][$limit];
159 }
160 }
161 2 $self->{from_ref} = \@from;
162 2 $self->{to_ref} = \@to;
163 2 return (\@from, \@to);
164 }
165  
166 =head2 bc_factor
167  
168 Title : bc_factor
169 Usage : my $bc_factor_ref = $self->bc_factor;
170 Function: generate the number of neighbors in function of Hamming distance
171 and seed width
172 Returns : references to a 2D array of number of neighbors
173 Args : none
174  
175 =cut
176  
177 2 ▶sub bc_factor {
Subroutine bc_factor: called CC=3 SCAR=11.0
Pod documented
178 2 my $self = shift;
179 2 my @bc_factor;
180 2 for my $d ( 1 .. $self->{seed_width} ) {
181 12 my @n;
182 12 for my $k ( 0 .. $d ) {
183 54 push @n,
184 (
185 ( 3**$k ) * (
186 _factorial($d)
187 / ( _factorial($k) * _factorial( $d - $k ) )
188 ) - 1
189 );
190 }
191 12 push @bc_factor, [@n];
192 }
193 2 return \@bc_factor;
194 }
195  
196 =head2 _factorial
197  
198 Title : _factorial
199 Usage : my $r = _factorial($n);
200 Function: calculate the product of all positive integers less than or equal
201 to a given number
202 Returns : non-negative integer
203 Args : none
204  
205 =cut
206  
207 162 ▶sub _factorial {
Subroutine _factorial: called CC=2 SCAR=6.9
208 162 my $n = shift;
209 162 my ( $r, $i ) = ( 1, 2 );
210 162 ▶ for ( ; $i <= $n; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $n) 310 162 472
211 310 $r *= $i;
212 }
213 162 return $r;
214 }
215  
216 =head2 generate_hd_index
217  
218 Title : generate_hd_index
219 Usage : my $hd_index = $self->generate_hd_index( $oligo_ref );
220 Function: generate oligo indices for increasing Hamming distances
221 Returns : array of indices
222 Args : reference to oligo
223  
224 =cut
225  
226 4096 ▶sub generate_hd_index {
Subroutine generate_hd_index: called CC=3 SCAR=11.0
Pod documented
227 4096 my $self = shift;
228 4096 my $oligo_ref = shift;
229 4096 my @dna = ( 0 .. 3 );
230 4096 my @index;
231 4096 $index[0] = $oligo_ref->[$#$oligo_ref];
232 4096 @index[ 1 .. 3 ] = grep ( !m{$oligo_ref->[$#$oligo_ref]}, @dna );
233 4096 for my $depth ( 1 .. $#$oligo_ref ) {
234 20480 my @rest = grep ( !m{$oligo_ref->[ -( $depth + 1 ) ]}, @dna );
235 my @fst =
236 20480 map { $_ + ( ( 4**$depth ) * $oligo_ref->[ -( $depth + 1 ) ] ) }
237 @index;
238 20480 my @snd = map { $_ + ( ( 4**$depth ) * $rest[0] ) } @index;
239 20480 my @trd = map { $_ + ( ( 4**$depth ) * $rest[1] ) } @index;
240 20480 my @fth = map { $_ + ( ( 4**$depth ) * $rest[2] ) } @index;
241 20480 @index = $fst[0];
242 20480 for my $limit ( 1 .. $#{ $self->{from_ref}->[ $depth - 1 ] } ) {
243 push @index, @fst[ $self->{from_ref}->[ $depth - 1 ][$limit]
244 61440 .. $self->{to_ref}->[ $depth - 1 ][$limit] ];
245 push @index, @snd[ $self->{from_ref}->[ $depth - 1 ][ $limit - 1 ]
246 61440 .. $self->{to_ref}->[ $depth - 1 ][ $limit - 1 ] ];
247 push @index, @trd[ $self->{from_ref}->[ $depth - 1 ][ $limit - 1 ]
248 61440 .. $self->{to_ref}->[ $depth - 1 ][ $limit - 1 ] ];
249 push @index, @fth[ $self->{from_ref}->[ $depth - 1 ][ $limit - 1 ]
250 61440 .. $self->{to_ref}->[ $depth - 1 ][ $limit - 1 ] ];
251 }
252 push @index,
253 @snd[ $self->{from_ref}
254 20480 ->[ $depth - 1 ][ $#{ $self->{from_ref}->[ $depth - 1 ] } ]
255 .. $self->{to_ref}
256 20480 ->[ $depth - 1 ][ $#{ $self->{to_ref}->[ $depth - 1 ] } ] ];
257 push @index,
258 @trd[ $self->{from_ref}
259 20480 ->[ $depth - 1 ][ $#{ $self->{from_ref}->[ $depth - 1 ] } ]
260 .. $self->{to_ref}
261 20480 ->[ $depth - 1 ][ $#{ $self->{to_ref}->[ $depth - 1 ] } ] ];
262 push @index,
263 @fth[ $self->{from_ref}
264 20480 ->[ $depth - 1 ][ $#{ $self->{from_ref}->[ $depth - 1 ] } ]
265 .. $self->{to_ref}
266 20480 ->[ $depth - 1 ][ $#{ $self->{to_ref}->[ $depth - 1 ] } ] ];
267 }
268 4096 return \@index;
269 }
270  
271 =head2 execution_time
272  
273 Title : execution_time
274 Usage : my $time_string = $self->execution_time;
275 Function: transform a lapse of time in "seconds since epoch" into a
276 human-readable format
277 Returns : lapse of time (string)
278 Args : none
279  
280 =cut
281  
282 0 ▶sub execution_time {
Subroutine execution_time: not called CC=6 SCAR=37.4
Pod documented
283 0 my $self = shift;
284 0 my @time = ( $self->{launch_time}, $self->{land_time} );
285 0 my $t = $time[1] - $time[0];
286 0 my $days = sprintf( "%.0f", $t / 86400 );
287 0 $t = $t % 86400;
288 0 my $hours = sprintf( "%.0f", $t / 3600 );
289 0 $t = $t % 3600;
290 0 my $minutes = sprintf( "%.0f", $t / 60 );
291 0 $t = $t % 60;
292 0 my $seconds = sprintf( "%.0f", $t );
293 0 my $ex_time = "Time: ";
294 0 ▶ $ex_time .= $days > 0 ? "$days day(s), " : q{};
Branch: 1 branch
BranchTrueFalseTotal
$days > 0 ? : 0 0 0
295 0 ▶ $ex_time .= $hours > 0 ? "$hours hour(s), " : q{};
Branch: 1 branch
BranchTrueFalseTotal
$hours > 0 ? : 0 0 0
296 0 ▶ $ex_time .= $minutes > 0 ? "$minutes minute(s), " : q{};
Branch: 1 branch
BranchTrueFalseTotal
$minutes > 0 ? : 0 0 0
297 0 ▶ $ex_time .= $seconds > 0 ? "$seconds second(s)" : q{};
Branch: 1 branch
BranchTrueFalseTotal
$seconds > 0 ? : 0 0 0
298 0 ▶ $ex_time .= $ex_time eq "Time: " ? "less than 1 second" : q{};
Branch: 1 branch
BranchTrueFalseTotal
$ex_time eq 'Time: ' ? : 0 0 0
299 0 return $ex_time;
300 }
301  
302 =head2 encode
303  
304 Title : encode
305 Usage : my $representation = encode($value, $base, $depth);
306 Function: convert value (base 10) to representation in specified base
307 Returns : array or integers from 0 to 3
308 Args : base 10 number to be converted, base to which the number is
309 converted, width of the representation
310  
311 =cut
312  
313 0 ▶sub encode {
Subroutine encode: not called CC=3 SCAR=24.8
Pod documented
314 0 my ( $value, $base, $depth ) = @_;
315 0 my @representation;
316 0 ▶ while ( $value >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value >= 1) 0 0 0
317 0 push @representation, $value % $base;
318 0 $value = int( $value / $base );
319 }
320 0 ▶ while ( scalar @representation < $depth ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @representation < $depth) 0 0 0
321 0 push @representation, 0;
322 }
323 0 return reverse(@representation);
324 }
325  
326 =head2 decode
327  
328 Title : decode
329 Usage : my $value = decode($representation, $base);
330 Function: convert representation to value (base 10)
331 Returns : integer
332 Args : representation, base of the representation
333  
334 =cut
335  
336 9900 ▶sub decode {
Subroutine decode: called CC=3 SCAR=11.0
Pod documented
337 9900 my ( $representation, $base ) = @_;
338 9900 my @representation = split( q{}, $representation );
339 9900 my $value;
340 9900 for my $i ( 0 .. $#representation ) {
341 59400 ▶ $value
Branch: 1 branch
BranchTrueFalseTotal
$representation[$i] > 0 ? : 39622 19778 59400
342 += $representation[$i] > 0
343 ? ( $representation[$i] * ( $base**( $#representation - $i ) ) )
344 : 0;
345 }
346 9900 return $value;
347 }
348  
349 =head1 AUTHOR
350  
351 François Fauteux, C<< <ffauteux at cpan.org> >>
352  
353 =head1 BUGS
354  
355 Please report any bugs or feature requests to C<bug-Seeder at rt.cpan.org>, or
356 at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Seeder>. I will be
357 notified, and then you'll automatically be notified of progress on your bug as
358 I make changes.
359  
360 =head1 SUPPORT
361  
362 You can find documentation for this module with the perldoc command.
363  
364 perldoc Seeder
365  
366 You can also look for information at:
367  
368 =over 4
369  
370 =item * RT: CPAN's request tracker
371  
372 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Seeder>
373  
374 =item * AnnoCPAN: Annotated CPAN documentation
375  
376 L<http://annocpan.org/dist/Seeder>
377  
378 =item * CPAN Ratings
379  
380 L<http://cpanratings.perl.org/d/Seeder>
381  
382 =item * Search CPAN
383  
384 L<http://search.cpan.org/dist/Seeder>
385  
386 =back
387  
388 =head1 ACKNOWLEDGEMENTS
389  
390 This algorithm was developed by François Fauteux, Mathieu Blanchette and
391 Martina Strömvik. We thank the Perl Monks <http://www.perlmonks.org/> for
392 their support.
393  
394 =head1 COPYRIGHT & LICENSE
395  
396 Copyright 2008 François Fauteux, all rights reserved.
397  
398 This program is free software; you can redistribute it and/or modify it
399 under the same terms as Perl itself.
400  
401 =cut
402  
403 4 1; # End of Seeder
« Seeder/Background.pm ↑ Summary Seeder/Index.pm »