blib/conf/plugins/OligoFinder.pm untested

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 16 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 152 CC 5 SCAR 14.3
CC
5
Cov
0%
get_chroms
12.0
configure_form
6.0
auto_find
6.0
CRAP 4.2
1 package Bio::Graphics::Browser2::Plugin::OligoFinder;
2 # $Id: OligoFinder.pm,v 1.9 2004-08-23 15:56:31 lstein Exp $
3 # test plugin
4 use strict;
5 use Bio::Graphics::Browser2::Plugin;
6 use Bio::Graphics::Feature;
7 use Text::Shellwords;
8 use DBI;
9 use CGI qw(:standard *table);
10  
11 use vars '$VERSION','@ISA';
12 $VERSION = '0.15';
13  
14 @ISA = qw(Bio::Graphics::Browser2::Plugin);
15  
16 sub name { "Short Oligos" }
17  
18 sub description {
19 p("The oligo finder plugin finds oligos between 11 and 50 bp in length.",
20 "It does a slow search, making it suitable only for small (<150 MB) genomes.",
21 "[NOTE TO SYSADMINS: The browser must use the Bio::DB::GFF schema for this plugin to work.]").
22 p("This plugin was written by Lincoln Stein.");
23 }
24  
25 sub type { 'finder' }
26  
27 sub config_defaults {
28 my $self = shift;
29 return { };
30 }
31  
32 # we have no stable configuration
33 # sub reconfigure { }
34  
35 sub configure_form {
36 my $self = shift;
37 my $oligo = $self->config_param('searcholigo');
38 my $msg = $oligo && !$self->valid_oligo($oligo)
39 ? font({-color=>'red'},"Invalid oligo: either too short or not DNA")
40 : '';
41 return $msg .
42 table(TR({-class=>'searchtitle'},
43 th({-colspan=>2,-align=>'LEFT'},
44 'Enter an oligonucleotide between 12 and 50 bp in length.',
45 'The browser will identify all genomic regions that contain',
46 'this oligo. This is NOT a fast algorithm, so have patience.')),
47 TR({-class=>'searchbody'},
48 th('Enter oligo:'),
49 td(textfield(-name=>$self->config_name('searcholigo'),
50 -size=>50,-width=>50))
51 )
52 );
53 }
54  
55 # find() returns undef unless the OligoFinder.searcholigo parameter
56 # is specified and valid. Returning undef signals the browser to invoke the
57 # configure_form() method.
58 # If successful, it returns an array ref of Bio::SeqFeatureI objects.
59 sub find {
60 my $self = shift;
61 my $segments = shift; # current segments - can search inside them or ignore
62 # In this example we do a global search.
63  
64 my $oligo = lc $self->config_param('searcholigo');
65 $self->auto_find($oligo);
66 }
67  
68 # auto_find() does the actual work
69 # It is also called by the main page as a last resort when the user
70 # types something into the search box that isn't recognized.
71 sub auto_find {
72 my $self = shift;
73 my $oligo = lc shift;
74  
75 $self->valid_oligo($oligo) or return;
76  
77 (my $reversec = $oligo) =~ tr/gatcGATC/ctagCTAG/;
78 $reversec = reverse $reversec;
79 my $length = length $oligo;
80  
81 my $db = $self->database or die "I do not have a database";
82 my $dbi = $db->features_db;
83  
84 my @chroms = $self->get_chroms($db);
85 my $in = join ',',map {$dbi->quote($_)} @chroms;
86 my $sth =
87 $dbi->prepare("select fref,foffset,fdna from fdna where fref in ($in) order by fref,foffset",
88 { 'mysql_use_result' => 1}) or die "Couldn't prepare ",$db->errstr;
89 $sth->execute or die "Couldn't execute ",$db->errstr;
90  
91 my $bit_to_keep = length($oligo) - 1;
92 my ($current_ref,$offset,$dna,@results);
93
94 while (my ($ref,$off,$d) = $sth->fetchrow_array) {
95 if (!defined($current_ref) or $current_ref ne $ref) {
96 $offset = 0;
97 $dna = '';
98 }
99 $current_ref = $ref;
100  
101 # truncate all but the last length(oligo)-1 bases
102 substr($dna,0,length($dna)-$bit_to_keep) = '';
103 $offset = $off - length($dna);
104 $dna .= lc $d;
105  
106 my @forward = $self->exact_matches($dna,$oligo);
107 my @reverse = $self->exact_matches($dna,$reversec);
108  
109 for ([$oligo=>\@forward],[$reversec=>\@reverse]) {
110 my ($name,$pos) = @$_;
111 for my $p (@$pos) {
112 push @results, Bio::Graphics::Feature->new(-ref => $ref,
113 -type => 'oligo',
114 -name => $name,
115 -start => $offset+$p+1,
116 -score => '100%',
117 -end => $offset+$p+length($oligo),
118 -factory=> $db,
119 )
120 }
121 }
122 }
123 return \@results;
124 }
125  
126 sub exact_matches {
127 my $self = shift;
128 my ($dna,$oligo) = @_;
129 my @results;
130 my $offset = 0;
131 while ((my $i = index($dna,$oligo,$offset)) >= 0) {
132 push @results,$i;
133 $offset = $i+length($oligo);
134 }
135 @results;
136 }
137  
138 # This is a slowish query, so cache the results per-source.
139 # Bio::DB::GFF has no concept of the reference DNA, so we just look
140 # inside segments of DNA that are more than 20K in length and hence
141 # likely to represent genomic.
142 sub get_chroms {
143 my $self = shift;
144 my $db = shift;
145 my $dbi = $db->features_db;
146 my $source = $self->browser_config();
147 my $chroms;
148 my @chroms = shellwords($self->browser_config->plugin_setting('search_segments'));
149 if (@chroms) {
150 $chroms = \@chroms;
151 }
152 else {
153 return @{$self->{chroms}{$source}} if ref($self->{chroms}{$source});
154 $chroms = $dbi->selectcol_arrayref('select fref from fdna group by fref having count(fref)>10');
155 }
156 $self->{chroms}{$source} = $chroms;
157 return @$chroms;
158 }
159  
160 # return true if a valid oligo
161 sub valid_oligo {
162 my $self = shift;
163 my $oligo = shift or return;
164 return $oligo =~ /^[gatcn]{11,50}$/i
165 }
166  
167 1;