| 1 | # $Id: epcr.pm,v 1.16 2005/08/23 13:51:14 smckay Exp $ | ||
| 2 | |||
| 3 | package Bio::PrimerDesigner::epcr; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Bio::PrimerDesigner::epcr - A class for accessing the epcr binary | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use Bio::PrimerDesigner::epcr; | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | A low-level interface to the e-PCR binary. Uses supplied PCR primers, | ||
| 16 | DNA sequence and stringency parameters to predict both expected | ||
| 17 | and unexpected PCR products. | ||
| 18 | |||
| 19 | =head1 METHODS | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | use strict; | ||
| 24 | use File::Spec::Functions 'catfile'; | ||
| 25 | use File::Temp 'tempfile'; | ||
| 26 | use Bio::PrimerDesigner::Remote; | ||
| 27 | use Bio::PrimerDesigner::Result; | ||
| 28 | use base 'Class::Base'; | ||
| 29 | |||
| 30 | use vars '$VERSION'; | ||
| 31 | $VERSION = sprintf "%d.%02d", q$Revision: 1.16 $ =~ /(\d+)\.(\d+)/; | ||
| 32 | |||
| 33 | # ------------------------------------------------------------------- | ||
| 34 | sub run { | ||
| 35 | |||
| 36 | =head2 run | ||
| 37 | |||
| 38 | Sets up the e-PCR request for a single primer combination and returns | ||
| 39 | an Bio::PrimerDesigner::Result object | ||
| 40 | |||
| 41 | If the permute flag is true, all three possible primer combinations | ||
| 42 | will be tested (ie: forward + reverse, forward + forward, reverse + reverse) | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | my $self = shift; | ||
| 47 | my @result = (); | ||
| 48 | my @params = @_ or return $self->error("No arguments for run method"); | ||
| 49 | my $args = $_[2]; | ||
| 50 | my $permute = $args->{'permute'} ? 1 : 0; | ||
| 51 | my $left = $args->{'left'} or $self->error("No left primer"); | ||
| 52 | my $right = $args->{'right'} or $self->error("No right primer"); | ||
| 53 | |||
| 54 | $args->{'permute'} = 0; | ||
| 55 | |||
| 56 | if ( $permute ) { | ||
| 57 | for my $combo ( 1 .. 3 ) { | ||
| 58 | my %seen = (); | ||
| 59 | local $args->{'right'} = $left if $combo == 2; | ||
| 60 | local $args->{'left'} = $right if $combo == 3; | ||
| 61 | $params[2] = $args; | ||
| 62 | my @pre_result = $self->request(@params); | ||
| 63 | |||
| 64 | # | ||
| 65 | # e-pcr quirk, same-primer comparisons give two identical | ||
| 66 | # results, we will ignore duplicates | ||
| 67 | # | ||
| 68 | for my $line (@pre_result) { | ||
| 69 | push @result, $line unless $seen{$line}; | ||
| 70 | $seen{$line} = 1 if !$seen{$line}; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | } | ||
| 74 | else { | ||
| 75 | @result = $self->request( @params ); | ||
| 76 | } | ||
| 77 | |||
| 78 | my $out = Bio::PrimerDesigner::Result->new; | ||
| 79 | |||
| 80 | $out->{1}->{'products'} = @result; | ||
| 81 | $out->{1}->{'raw_output'} = join '', grep {defined} @result; | ||
| 82 | my $count = 0; | ||
| 83 | |||
| 84 | for (@result) { | ||
| 85 | $count++; | ||
| 86 | next unless $_ && /\.\./; | ||
| 87 | my ($start, $stop) = /(\d+)\.\.(\d+)/; | ||
| 88 | my $size = abs($stop - $start); | ||
| 89 | $out->{$count}->{'start'} = $start - 1; | ||
| 90 | $out->{$count}->{'stop'} = $out->{$count}->{'end'} = $stop - 1; | ||
| 91 | $out->{$count}->{'size'} = $size; | ||
| 92 | } | ||
| 93 | |||
| 94 | return $out; | ||
| 95 | } | ||
| 96 | |||
| 97 | # ------------------------------------------------------------------- | ||
| 98 | sub request { | ||
| 99 | |||
| 100 | =head2 request | ||
| 101 | |||
| 102 | Assembles the e-PCR config file and command-line arguments and send | ||
| 103 | the e-PCR request to the local e-PCR binary or remote server. | ||
| 104 | |||
| 105 | =cut | ||
| 106 | |||
| 107 | my $self = shift; | ||
| 108 | my ($method, $loc, $args) = @_; | ||
| 109 | my @data = (); | ||
| 110 | $method ||= 'remote'; | ||
| 111 | |||
| 112 | if ( $method eq 'remote' ) { | ||
| 113 | if ( ! defined $args->{'seq'} ) { | ||
| 114 | $self->error( | ||
| 115 | "A sequence must be supplied (not a file name) for remote epcr" | ||
| 116 | ); | ||
| 117 | return ''; | ||
| 118 | } | ||
| 119 | |||
| 120 | my $cgi = Bio::PrimerDesigner::Remote->new; | ||
| 121 | $cgi->{'program'} = 'e-PCR'; | ||
| 122 | $args->{'program'} = 'e-PCR'; | ||
| 123 | |||
| 124 | @data = $cgi->CGI_request( $loc, $args ); | ||
| 125 | } | ||
| 126 | elsif ( $method eq 'local') { # run ePCR locally | ||
| 127 | # | ||
| 128 | # required parameters | ||
| 129 | # | ||
| 130 | my $left = uc $args->{'left'} || $self->error("no left primer"); | ||
| 131 | my $right = uc $args->{'right'} || $self->error("no right primer"); | ||
| 132 | my $seq = $args->{'seq'} || ''; | ||
| 133 | my $file = $args->{'seqfile'} || ''; | ||
| 134 | $self->error("No sequence supplied") unless $seq || $file; | ||
| 135 | |||
| 136 | # | ||
| 137 | # optional parameters | ||
| 138 | # | ||
| 139 | my $prod_size = $args->{'prod_size'} || 2000; | ||
| 140 | my $margin = $args->{'margin'} || 2000; | ||
| 141 | my $word_size = $args->{'word_size'} || 7; | ||
| 142 | my $num_mismatch = $args->{'mismatch'} || 2; | ||
| 143 | |||
| 144 | # | ||
| 145 | # e-PCR config file | ||
| 146 | # | ||
| 147 | my ( $temp_loc_fh, $temp_loc ) = tempfile; | ||
| 148 | print $temp_loc_fh "ePCR_test\t$left\t$right\t$prod_size\t\n"; | ||
| 149 | close $temp_loc_fh; | ||
| 150 | |||
| 151 | # | ||
| 152 | # e-PCR sequence file (fasta format) | ||
| 153 | # | ||
| 154 | my ($seq_loc_fh, $seq_loc, $seq_file, $seq_temp); | ||
| 155 | if ($seq) { | ||
| 156 | ( $seq_loc_fh, $seq_loc ) = tempfile; | ||
| 157 | $seq_temp = $seq_loc; | ||
| 158 | print $seq_loc_fh ">Test sequence\n$seq\n"; | ||
| 159 | close $seq_loc_fh; | ||
| 160 | } | ||
| 161 | else { | ||
| 162 | $seq_loc = $file or $self->error('No sequence file'); | ||
| 163 | } | ||
| 164 | |||
| 165 | # | ||
| 166 | # e-PCR command-line arguments | ||
| 167 | # | ||
| 168 | my $params = "$temp_loc $seq_loc "; | ||
| 169 | $params .= "M=$margin W=$word_size N=$num_mismatch"; | ||
| 170 | $loc = catfile( $loc, $self->binary_name ); | ||
| 171 | |||
| 172 | # | ||
| 173 | # run e-PCR | ||
| 174 | # | ||
| 175 | open EPCR, "$loc $params |"; | ||
| 176 | @data = <EPCR>; | ||
| 177 | close EPCR; | ||
| 178 | |||
| 179 | unlink $temp_loc; | ||
| 180 | unlink $seq_temp if $seq_temp && -e $seq_temp; | ||
| 181 | } | ||
| 182 | |||
| 183 | return @data; | ||
| 184 | } | ||
| 185 | |||
| 186 | # ------------------------------------------------------------------- | ||
| 187 | sub verify { | ||
| 188 | |||
| 189 | =head2 verify | ||
| 190 | |||
| 191 | Check to make that the e-PCR binary is installed and functioning | ||
| 192 | properly. Since e-PCR returns nothing if no PCR product is found | ||
| 193 | in the sequence, we have to be able to distinguish between a valid, | ||
| 194 | undefined output from a functioning e-PCR and an undefined output | ||
| 195 | for some other reason. verify uses sham e-PCR data that is known | ||
| 196 | to produce a PCR product. | ||
| 197 | |||
| 198 | =cut | ||
| 199 | |||
| 200 | my $self = shift; | ||
| 201 | my ($method, $loc) = @_ or $self->error('No verify parameters'); | ||
| 202 | my %param = (); | ||
| 203 | $param{'left'} = 'TTGCGCATTTACGATTACGA'; | ||
| 204 | $param{'right'} = 'ATGCTGTAATCGGCTGTCCT'; | ||
| 205 | $param{'seq'} = 'GCAGCGAGTTGCGCATTTACGATTACGACATACGACACGA' . | ||
| 206 | 'TTACAGACAGGACAGCCGATTACAGCATATCGACAGCAT'; | ||
| 207 | $param{'prod_size'} = 70; | ||
| 208 | $param{'margin'} = 20; | ||
| 209 | |||
| 210 | my $result = $self->run( $method, $loc, \%param ); | ||
| 211 | my $output = $result->raw_output || ''; | ||
| 212 | |||
| 213 | unless ( $output =~ /\d+\.\.\d+/ ) { | ||
| 214 | return $self->error("e-PCR did not verify!"); | ||
| 215 | } | ||
| 216 | else { | ||
| 217 | return $result; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | # ------------------------------------------------------------------- | ||
| 222 | sub binary_name { | ||
| 223 | |||
| 224 | =pod | ||
| 225 | |||
| 226 | =head2 binary_name | ||
| 227 | |||
| 228 | Defines the binary's name on the system. | ||
| 229 | |||
| 230 | =cut | ||
| 231 | |||
| 232 | my $self = shift; | ||
| 233 | return 'e-PCR'; | ||
| 234 | } | ||
| 235 | |||
| 236 | # ------------------------------------------------------------------- | ||
| 237 | sub list_aliases { | ||
| 238 | |||
| 239 | =pod | ||
| 240 | |||
| 241 | =head2 list_aliases | ||
| 242 | |||
| 243 | There are no aliases to list for epcr. | ||
| 244 | |||
| 245 | =cut | ||
| 246 | |||
| 247 | my $self = shift; | ||
| 248 | return; | ||
| 249 | } | ||
| 250 | |||
| 251 | # ------------------------------------------------------------------- | ||
| 252 | sub list_params { | ||
| 253 | |||
| 254 | =pod | ||
| 255 | |||
| 256 | =head2 list_params | ||
| 257 | |||
| 258 | Returns a list of e-PCR configuration options. Required e-PCR input is | ||
| 259 | a sequence string or file and the left and right primers. Default values | ||
| 260 | will be used for the remaining options if none are supplied. | ||
| 261 | |||
| 262 | |||
| 263 | =cut | ||
| 264 | |||
| 265 | my $self = shift; | ||
| 266 | |||
| 267 | return ( | ||
| 268 | 'REQUIRED:', | ||
| 269 | 'seq (string) Raw DNA sequence to search for PCR products', | ||
| 270 | 'OR', | ||
| 271 | 'seqfile (string) Fasta file to search for PCR products', | ||
| 272 | '', | ||
| 273 | 'left (string) Left primer sequence', | ||
| 274 | 'right (string) Right primer sequence', | ||
| 275 | '', 'OPTIONAL:', | ||
| 276 | 'word_size (int; default 7) The size of the perfect match at 3\' end', | ||
| 277 | 'mismatch (int; default 2) Allowed number of mismatches', | ||
| 278 | 'prod_size (int; default 2000) Expected PCR product size', | ||
| 279 | 'margin (int; default 2000) Allowed size variation', | ||
| 280 | 'permute (true) Try all primer combinations (l/r, l/l, r/r)' | ||
| 281 | ); | ||
| 282 | } | ||
| 283 | |||
| 284 | 1; | ||
| 285 | |||
| 286 | # ------------------------------------------------------------------- | ||
| 287 | |||
| 288 | =head1 AUTHOR | ||
| 289 | |||
| 290 | Copyright (C) 2003-2008 Sheldon McKay E<lt>mckays@cshl.eduE<gt>, | ||
| 291 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>. | ||
| 292 | |||
| 293 | =head1 LICENSE | ||
| 294 | |||
| 295 | This program is free software; you can redistribute it and/or modify | ||
| 296 | it under the terms of the GNU General Public License as published by | ||
| 297 | the Free Software Foundation; version 2. | ||
| 298 | |||
| 299 | This program is distributed in the hope that it will be useful, but | ||
| 300 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 301 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 302 | General Public License for more details. | ||
| 303 | |||
| 304 | You should have received a copy of the GNU General Public License | ||
| 305 | along with this program; if not, write to the Free Software | ||
| 306 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | ||
| 307 | USA. | ||
| 308 | |||
| 309 | =head1 SEE ALSO | ||
| 310 | |||
| 311 | Bio::PrimerDesigner::primer3. | ||
| 312 | |||
| 313 | =cut |