| 1 | # $Id: ispcr.pm,v 1.1 2005/08/23 13:51:14 smckay Exp $ | ||
| 2 | |||
| 3 | package Bio::PrimerDesigner::ispcr; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Bio::PrimerDesigner::ispcr - A class for accessing the isPcr (in-silico PCR) binary | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use Bio::PrimerDesigner::ispcr; | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | A low-level interface to the isPcr program. 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.1 $ =~ /(\d+)\.(\d+)/; | ||
| 32 | |||
| 33 | # ------------------------------------------------------------------- | ||
| 34 | |||
| 35 | =head2 run | ||
| 36 | |||
| 37 | Sets up the isPcr request for a single primer combination and returns | ||
| 38 | an Bio::PrimerDesigner::Result object | ||
| 39 | |||
| 40 | If the permute flag is true, all three possible primer combinations | ||
| 41 | will be tested (ie: forward + reverse, forward + forward, reverse + reverse) | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | sub run { | ||
| 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 | # 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} > 1; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | else { | ||
| 74 | @result = $self->request( @params ); | ||
| 75 | } | ||
| 76 | |||
| 77 | # first element will be empty | ||
| 78 | shift @result; | ||
| 79 | |||
| 80 | my $out = Bio::PrimerDesigner::Result->new; | ||
| 81 | |||
| 82 | $out->{1}->{'products'} = @result; | ||
| 83 | $out->{1}->{'raw_output'} = join('','>',@result); | ||
| 84 | |||
| 85 | my $count = 0; | ||
| 86 | |||
| 87 | for (@result) { | ||
| 88 | $count++; | ||
| 89 | s/>//; | ||
| 90 | my @lines = split "\n", $_; | ||
| 91 | chomp @lines; | ||
| 92 | |||
| 93 | my $idline = shift @lines; | ||
| 94 | my ($location) = split /\s+/, $idline; | ||
| 95 | my ($start,$strand,$stop) = $location =~ /(\d+)([-+])(\d+)$/; | ||
| 96 | my $size = abs($stop - $start); | ||
| 97 | |||
| 98 | $out->{$count}->{'start'} = $start; | ||
| 99 | $out->{$count}->{'stop'} = $out->{$count}->{'end'} = $stop; | ||
| 100 | $out->{$count}->{'size'} = $size; | ||
| 101 | $out->{$count}->{'amplicon'} = join '', @lines; | ||
| 102 | } | ||
| 103 | |||
| 104 | return $out; | ||
| 105 | } | ||
| 106 | |||
| 107 | # ------------------------------------------------------------------- | ||
| 108 | |||
| 109 | =head2 request | ||
| 110 | |||
| 111 | Assembles the config file and command-line arguments and sends | ||
| 112 | the request to the local binary or remote server. | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | sub request { | ||
| 117 | my $self = shift; | ||
| 118 | my ($method, $loc, $args) = @_; | ||
| 119 | my @data = (); | ||
| 120 | $method ||= 'remote'; | ||
| 121 | |||
| 122 | if ( $method eq 'remote' ) { | ||
| 123 | if ( ! defined $args->{'seq'} ) { | ||
| 124 | $self->error( | ||
| 125 | "A sequence must be supplied (not a file name) for remote ispcr" | ||
| 126 | ); | ||
| 127 | return ''; | ||
| 128 | } | ||
| 129 | |||
| 130 | my $cgi = Bio::PrimerDesigner::Remote->new; | ||
| 131 | $cgi->{'program'} = 'ispcr'; | ||
| 132 | $args->{'program'} = 'ispcr'; | ||
| 133 | |||
| 134 | @data = $cgi->CGI_request( $loc, $args ); | ||
| 135 | } | ||
| 136 | elsif ( $method eq 'local') { # run ePCR locally | ||
| 137 | # | ||
| 138 | # required parameters | ||
| 139 | # | ||
| 140 | my $left = uc $args->{'left'} || $self->error("no left primer"); | ||
| 141 | my $right = uc $args->{'right'} || $self->error("no right primer"); | ||
| 142 | my $seq = $args->{'seq'} || ''; | ||
| 143 | my $file = $args->{'seqfile'} || ''; | ||
| 144 | $self->error("No sequence supplied") unless $seq || $file; | ||
| 145 | |||
| 146 | # | ||
| 147 | # config file | ||
| 148 | # | ||
| 149 | my ( $temp_loc_fh, $temp_loc ) = tempfile; | ||
| 150 | print $temp_loc_fh "isPCR_test\t$left\t$right\n"; | ||
| 151 | close $temp_loc_fh; | ||
| 152 | |||
| 153 | # | ||
| 154 | # sequence file (fasta format) | ||
| 155 | # | ||
| 156 | my ($seq_loc_fh, $seq_loc, $seq_file, $seq_temp); | ||
| 157 | if ($seq) { | ||
| 158 | ( $seq_loc_fh, $seq_loc ) = tempfile; | ||
| 159 | $seq_temp = $seq_loc; | ||
| 160 | print $seq_loc_fh ">test\n$seq\n"; | ||
| 161 | close $seq_loc_fh; | ||
| 162 | } | ||
| 163 | else { | ||
| 164 | $seq_loc = $file or $self->error('No sequence file'); | ||
| 165 | } | ||
| 166 | |||
| 167 | # | ||
| 168 | # command-line arguments | ||
| 169 | # | ||
| 170 | my $params = ''; | ||
| 171 | for my $p (qw/tileSize stepSize maxSize minSize minPerfect minGood mask/) { | ||
| 172 | $params .= "-$p=$args->{$p}" if defined $args->{$p}; | ||
| 173 | } | ||
| 174 | |||
| 175 | $loc = catfile( $loc, $self->binary_name ); | ||
| 176 | |||
| 177 | local $/ = '>'; | ||
| 178 | open ISPCR, "$loc $seq_loc $temp_loc $params stdout |"; | ||
| 179 | @data = (<ISPCR>); | ||
| 180 | close ISPCR; | ||
| 181 | |||
| 182 | |||
| 183 | unlink $temp_loc; | ||
| 184 | unlink $seq_temp if $seq_temp && -e $seq_temp; | ||
| 185 | } | ||
| 186 | |||
| 187 | return @data; | ||
| 188 | } | ||
| 189 | |||
| 190 | # ------------------------------------------------------------------- | ||
| 191 | |||
| 192 | |||
| 193 | =head2 verify | ||
| 194 | |||
| 195 | Check to make that the isPCR binary is installed and functioning | ||
| 196 | properly. Since ispcr returns nothing if no PCR product is found | ||
| 197 | in the sequence, we have to be able to distinguish between a valid, | ||
| 198 | undefined output from a functioning ispcr and an undefined output | ||
| 199 | for some other reason. verify uses sham ispcr data that is known | ||
| 200 | to produce a PCR product. | ||
| 201 | |||
| 202 | =cut | ||
| 203 | |||
| 204 | |||
| 205 | sub verify { | ||
| 206 | my $self = shift; | ||
| 207 | my ($method, $loc) = @_ or $self->error('No verify parameters'); | ||
| 208 | my %param = (); | ||
| 209 | $param{'left'} = 'TTGCGCATTTACGATTACGA'; | ||
| 210 | $param{'right'} = 'ATGCTGTAATCGGCTGTCCT'; | ||
| 211 | $param{'seq'} = 'GCAGCGAGTTGCGCATTTACGATTACGACATACGACACGA' . | ||
| 212 | 'TTACAGACAGGACAGCCGATTACAGCATATCGACAGCAT'; | ||
| 213 | |||
| 214 | my $result = $self->run( $method, $loc, \%param ); | ||
| 215 | my $output = $result->raw_output || ''; | ||
| 216 | |||
| 217 | unless ( $output =~ />\S+\s+\S+\s+\S+\s+\S+/ ) { | ||
| 218 | return $self->error("ispcr did not verify!"); | ||
| 219 | } | ||
| 220 | else { | ||
| 221 | return $result; | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | # ------------------------------------------------------------------- | ||
| 226 | sub binary_name { | ||
| 227 | |||
| 228 | =pod | ||
| 229 | |||
| 230 | =head2 binary_name | ||
| 231 | |||
| 232 | Defines the binary's name on the system. | ||
| 233 | |||
| 234 | =cut | ||
| 235 | |||
| 236 | my $self = shift; | ||
| 237 | return 'isPcr'; | ||
| 238 | } | ||
| 239 | |||
| 240 | # ------------------------------------------------------------------- | ||
| 241 | sub list_aliases { | ||
| 242 | |||
| 243 | =pod | ||
| 244 | |||
| 245 | =head2 list_aliases | ||
| 246 | |||
| 247 | There are no aliases to list for ispcr. | ||
| 248 | |||
| 249 | =cut | ||
| 250 | |||
| 251 | my $self = shift; | ||
| 252 | return; | ||
| 253 | } | ||
| 254 | |||
| 255 | # ------------------------------------------------------------------- | ||
| 256 | sub list_params { | ||
| 257 | |||
| 258 | =pod | ||
| 259 | |||
| 260 | =head2 list_params | ||
| 261 | |||
| 262 | Returns a list of ispcr configuration options. Required ispcr input is | ||
| 263 | a sequence string or file and the left and right primers. Default values | ||
| 264 | will be used for the remaining options if none are supplied. | ||
| 265 | |||
| 266 | |||
| 267 | =cut | ||
| 268 | |||
| 269 | my $self = shift; | ||
| 270 | |||
| 271 | return ( | ||
| 272 | 'REQUIRED:', | ||
| 273 | 'seq (string) Raw DNA sequence to search for PCR products', | ||
| 274 | 'OR', | ||
| 275 | 'seqfile (string) Fasta file to search for PCR products', | ||
| 276 | '', | ||
| 277 | 'left (string) Left primer sequence', | ||
| 278 | 'right (string) Right primer sequence', | ||
| 279 | '', 'OPTIONAL:', | ||
| 280 | 'tileSize (int) size of match that triggers alignment (default 11)', | ||
| 281 | 'stepSize (int) spacing between tiles (default 5)', | ||
| 282 | 'maxSize (int) max size of PCR product (default 4000)', | ||
| 283 | 'minSize (int) min size of PCR product (default 0)', | ||
| 284 | 'minPerfect (int) min size of perfect match at 3 prime end of primer (default 15)', | ||
| 285 | 'minGood (int) min size where there must be 2 matches for each mismatch (default 15)', | ||
| 286 | 'permute (true) Try all primer combinations (l/r, l/l, r/r)', | ||
| 287 | 'mask (upper|lower) Mask out lower or upper-cased sequences' | ||
| 288 | ); | ||
| 289 | } | ||
| 290 | |||
| 291 | 1; | ||
| 292 | |||
| 293 | # ------------------------------------------------------------------- | ||
| 294 | |||
| 295 | =head1 AUTHOR | ||
| 296 | |||
| 297 | Copyright (C) 2003-2008 Sheldon McKay E<lt>mckays@cshl.edu<gt>, | ||
| 298 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>. | ||
| 299 | |||
| 300 | =head1 LICENSE | ||
| 301 | |||
| 302 | This program is free software; you can redistribute it and/or modify | ||
| 303 | it under the terms of the GNU General Public License as published by | ||
| 304 | the Free Software Foundation; version 2. | ||
| 305 | |||
| 306 | This program is distributed in the hope that it will be useful, but | ||
| 307 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 308 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 309 | General Public License for more details. | ||
| 310 | |||
| 311 | You should have received a copy of the GNU General Public License | ||
| 312 | along with this program; if not, write to the Free Software | ||
| 313 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | ||
| 314 | USA. | ||
| 315 | |||
| 316 | =head1 SEE ALSO | ||
| 317 | |||
| 318 | Bio::PrimerDesigner::primer3. | ||
| 319 | |||
| 320 | =cut |