File Coverage

blib/lib/Bio/SSRTool.pm
Criterion Covered Total %
statement 61 62 98.3
branch 13 18 72.2
condition 9 20 45.0
subroutine 8 8 100.0
pod 1 1 100.0
total 92 109 84.4


line stmt bran cond sub pod time code
1             package Bio::SSRTool;
2              
3 2     2   57620 use 5.006;
  2         7  
  2         148  
4 2     2   12 use strict;
  2         4  
  2         62  
5 2     2   10 use warnings;
  2         7  
  2         73  
6 2     2   9 use vars qw( @ISA @EXPORT @EXPORT_OK );
  2         3  
  2         145  
7 2     2   11 use Carp qw( croak );
  2         2  
  2         132  
8 2     2   2640 use IO::Scalar;
  2         61640  
  2         5483  
9              
10             @ISA = qw( Exporter );
11             @EXPORT = qw( find_ssr );
12             @EXPORT_OK = qw( find_ssr );
13              
14             our $VERSION = '0.04';
15              
16             =head1 NAME
17              
18             Bio::SSRTool - The great new Bio::SSRTool!
19              
20             =head1 SYNOPSIS
21              
22             Examines FASTA-formatted sequence data for simple sequence repeats (SSRs).
23              
24             use Bio::SSRTool;
25              
26             =head1 EXPORT
27              
28             =head1 SUBROUTINES/METHODS
29              
30             =cut
31              
32             # ------------------------------------------------------------
33             sub find_ssr {
34              
35             =head2 find_ssr
36              
37             my $ssr_tool = Bio::SSRTool->new();
38             my @ssrs = $ssr_tool->find_ssr( $seq, { min_repeats => 10 } );
39              
40             Or:
41              
42             use Bio::SSRTool 'find_ssr';
43              
44             my @ssrs = find_ssr( $fh, { motif_length => 'trimer' } );
45              
46             The "find_ssr" routine expects a sequence string in FASTA format
47             (or a filehandle to read a FASTA file) and an optional hash-ref of
48             arguments including:
49              
50             =over 4
51              
52             =item min_repeats
53              
54             A positive integer
55              
56             =item motif_length
57              
58             A positive integer between 1 and 10. Default is 4.
59              
60             =back
61              
62             =cut
63              
64 3 50   3 1 5824 my $seq = shift or return;
65 3   50     12 my $args = shift || {};
66 3   50     15 my $motif_len = $args->{'motif_length'} || 4;
67 3   50     10 my $min_repeats = $args->{'min_repeats'} || 5;
68              
69 3 50 33     37 unless (
      33        
70             $motif_len =~ /^\d{1,2}$/ && $motif_len > 0 && $motif_len < 11
71             ) {
72 0         0 croak "Invalid motif length '$motif_len'";
73             }
74              
75             #
76             # Make sure it acts like a filehandle
77             #
78 3 100       11 if ( ref $seq ne 'GLOB' ) {
79 2 100 66     36 if ( $seq !~ /\n$/ && -e $seq ) {
80 1         3 my $tmp = $seq;
81 1 50       42 open my $fh, '<', $tmp or die "Can't read '$tmp': $!\n";
82 1         3 $seq = $fh;
83             }
84             else {
85 1         3 my $tmp = $seq;
86 1         9 $seq = IO::Scalar->new( \$tmp );
87             }
88             }
89              
90 3         169 my @ssrs;
91 3         11 $/ = '>';
92 3         53 while ( my $rec = <$seq> ) {
93 9         111 chomp $rec;
94 9 100       29 next unless $rec;
95 6         21 my ( $titleline, $sequence ) = split /\n/, $rec, 2;
96 6 50 33     30 next unless ( $sequence && $titleline );
97              
98             # the ID is the first whitespace-delimited item on titleline
99 6         22 my ( $id ) = $titleline =~ /^(\S+)/;
100 6   50     24 $id ||= 'INPUT';
101 6         42 $sequence =~ s/\s+//g; # concatenate multi-line sequence
102 6         10 my $seqlength = length $sequence;
103 6         10 my $ssr_number = 1; # track multiple ssrs within a single sequence
104 6         7 my %locations; # track location of SSRs as detected
105              
106             # test each spec against sequence
107 6         11 for my $len ( 1 .. $motif_len ) {
108 24         421 my $re = qr/(([gatc]{$len})\2{$min_repeats,})/;
109              
110 24         806 while ( $sequence =~ m/$re/ig ) {
111 18         38 my $ssr = $1;
112 18         35 my $motif = lc $2;
113              
114             # reject "aaaaaaaaa", "ggggggggggg", etc.
115 18 100       34 next if _homopolymer( $motif, $len );
116 12         20 my $ssrlength = length( $ssr ); # SSR length
117 12         19 my $repeats = $ssrlength / $len; # of rep units
118 12         14 my $end = pos( $sequence ); # where SSR ends
119 12         52 pos($sequence) = $end - $len; # see docs
120 12         22 my $start = $end - $ssrlength + 1; # SSR starts
121 12         34 my $id_and_num = $id . "-" . $ssr_number++;
122              
123             # count SSR only once
124 12 50       40 unless ( $locations{ $start }++ ) {
125 12         430 push @ssrs, {
126             sequence => $id_and_num,
127             motif => $motif,
128             num_repeats => $repeats,
129             start => $start,
130             end => $end,
131             seq_length => $seqlength
132             };
133             }
134             }
135             }
136             }
137              
138 3         41 return @ssrs;
139             }
140              
141             # ------------------------------------------------------------
142             sub _homopolymer {
143             # returns 'true' if motif is repeat of single nucleotide
144 18     18   29 my ( $motif, $motiflength ) = @_;
145 18         23 my ( $reps ) = $motiflength - 1;
146 18         423 return $motif =~ /([gatc])\1{$reps}/;
147             }
148              
149             # ------------------------------------------------------------
150             =head1 AUTHOR
151              
152             Ken Youens-Clark, C<< >>
153              
154             =head1 BUGS
155              
156             Please report any bugs or feature requests to C
157             rt.cpan.org>, or through the web interface at
158             L. I will be
159             notified, and then you'll automatically be notified of progress on your bug
160             as I make changes.
161              
162             =head1 SUPPORT
163              
164             You can find documentation for this module with the perldoc command.
165              
166             perldoc Bio::SSRTool
167              
168             =over 4
169              
170             =item * RT: CPAN's request tracker (report bugs here)
171              
172             L
173              
174             =item * AnnoCPAN: Annotated CPAN documentation
175              
176             L
177              
178             =item * CPAN Ratings
179              
180             L
181              
182             =item * Search CPAN
183              
184             L
185              
186             =back
187              
188             =head1 ACKNOWLEDGEMENTS
189              
190             This was originally written in 1999 by Sam Cartinhour. Thanks to Jim
191             Thomason for code review.
192              
193             =head1 LICENSE AND COPYRIGHT
194              
195             Copyright 2012 Ken Youens-Clark.
196              
197             This program is released under the following license: GPL
198              
199             =cut
200              
201             1;