/usr/share/perl5/Bio/Tools/AmpliconSearch.pm is in libbio-perl-perl 1.6.924-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 | # BioPerl module for Bio::Tools::AmpliconSearch
#
# Copyright Florent Angly
#
# You may distribute this module under the same terms as perl itself
package Bio::Tools::AmpliconSearch;
use strict;
use warnings;
use Bio::Tools::IUPAC;
use Bio::SeqFeature::Amplicon;
use Bio::Tools::SeqPattern;
# we require Bio::SeqIO
# and Bio::SeqFeature::Primer
use base qw(Bio::Root::Root);
my $template_str;
=head1 NAME
Bio::Tools::AmpliconSearch - Find amplicons in a template using degenerate PCR primers
=head1 SYNOPSIS
use Bio::PrimarySeq;
use Bio::Tools::AmpliconSearch;
my $template = Bio::PrimarySeq->new(
-seq => 'aaaaaCCCCaaaaaaaaaaTTTTTTaaaaaCCACaaaaaTTTTTTaaaaaaaaaa',
);
my $fwd_primer = Bio::PrimarySeq->new(
-seq => 'CCNC',
);
my $rev_primer = Bio::PrimarySeq->new(
-seq => 'AAAAA',
);
my $search = Bio::Tools::AmpliconSearch->new(
-template => $template,
-fwd_primer => $fwd_primer,
-rev_primer => $rev_primer,
);
while (my $amplicon = $search->next_amplicon) {
print "Found amplicon at position ".$amplicon->start.'..'.$amplicon->end.":\n";
print $amplicon->seq->seq."\n\n";
}
# Now change the template (but you could change the primers instead) and look
# for amplicons again
$template = Bio::PrimarySeq->new(
-seq => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
);
$search->template($template);
while (my $amplicon = $search->next_amplicon) {
print "Found amplicon at position ".$amplicon->start.'..'.$amplicon->end.":\n";
print $amplicon->seq->seq."\n\n";
}
=head1 DESCRIPTION
Perform an in silico PCR reaction, i.e. search for amplicons in a given template
sequence using the specified degenerate primer.
The template sequence is a sequence object, e.g. L<Bio::Seq>, and the primers
can be a sequence or a L<Bio::SeqFeature::Primer> object and contain ambiguous
residues as defined in the IUPAC conventions. The primer sequences are converted
into regular expressions using L<Bio::Tools::IUPAC> and the matching regions of
the template sequence, i.e. the amplicons, are returned as L<Bio::Seq::PrimedSeq>
objects.
AmpliconSearch will look for amplicons on both strands (forward and reverse-
complement) of the specified template sequence. If the reverse primer is not
provided, an amplicon will be returned and span a match of the forward primer to
the end of the template. Similarly, when no forward primer is given, match from
the beginning of the template sequence. When several amplicons overlap, only the
shortest one to more accurately represent the biases of PCR. Future improvements
may include modelling the effects of the number of PCR cycles or temperature on
the PCR products.
=head1 TODO
Future improvements may include:
=over
=item *
Allowing a small number of primer mismatches
=item *
Reporting all amplicons, including overlapping ones
=item *
Putting a limit on the length of amplicons, in accordance with the processivity
of the polymerase used
=back
=head1 FEEDBACK
=head2 Mailing Lists
User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to one
of the Bioperl mailing lists. Your participation is much appreciated.
bioperl-l@bioperl.org - General discussion
http://bioperl.org/wiki/Mailing_lists - About the mailing lists
=head2 Support
Please direct usage questions or support issues to the mailing list:
I<bioperl-l@bioperl.org>
rather than to the module maintainer directly. Many experienced and
reponsive experts will be able look at the problem and quickly
address it. Please include a thorough description of the problem
with code and data examples if at all possible.
=head2 Reporting Bugs
Report bugs to the Bioperl bug tracking system to help us keep track
the bugs and their resolution. Bug reports can be submitted via the
web:
https://github.com/bioperl/bioperl-live/issues
=head1 AUTHOR
Florent Angly <florent.angly@gmail.com>
=head1 APPENDIX
The rest of the documentation details each of the object
methods. Internal methods are usually preceded with a _
=head2 new
Title : new
Usage : my $search = Bio::Tools::AmpliconSearch->new( );
Function : Initialize an amplicon search
Args : -template Sequence object for the template sequence. This object
will be converted to Bio::Seq if needed in since features
(amplicons and primers) will be added to this object.
-fwd_primer A sequence object representing the forward primer
-rev_primer A sequence object representing the reverse primer
-primer_file Read primers from a sequence file. It replaces
-fwd_primer and -rev_primer (optional)
-attach_primers Whether or not to attach primers to Amplicon objects. Default: 0 (off)
Returns : A Bio::Tools::AmpliconSearch object
=cut
sub new {
my ($class, @args) = @_;
my $self = $class->SUPER::new(@args);
my ($template, $primer_file, $fwd_primer, $rev_primer, $attach_primers) =
$self->_rearrange([qw(TEMPLATE PRIMER_FILE FWD_PRIMER REV_PRIMER ATTACH_PRIMERS)],
@args);
# Get primers
if (defined $primer_file) {
$self->primer_file($primer_file);
} else {
$self->fwd_primer($fwd_primer || '');
$self->rev_primer($rev_primer || '');
}
# Get template sequence
$self->template($template) if defined $template;
$self->attach_primers($attach_primers) if defined $attach_primers;
return $self;
}
=head2 template
Title : template
Usage : my $template = $search->template;
Function : Get/set the template sequence. Setting a new template resets any
search in progress.
Args : Optional Bio::Seq object
Returns : A Bio::Seq object
=cut
sub template {
my ($self, $template) = @_;
if (defined $template) {
if ( not(ref $template) || not $template->isa('Bio::PrimarySeqI') ) {
# Not a Bio::Seq or Bio::PrimarySeq
$self->throw("Expected a sequence object as input but got a '".ref($template)."'\n");
}
if (not $template->isa('Bio::SeqI')) {
# Convert sequence object to Bio::Seq Seq so that features can be added
my $primary_seq = $template;
$template = Bio::Seq->new();
$template->primary_seq($primary_seq);
}
$self->{template} = $template;
# Reset search in progress
$template_str = undef;
}
return $self->{template};
}
=head2 fwd_primer
Title : fwd_primer
Usage : my $primer = $search->fwd_primer;
Function : Get/set the forward primer. Setting a new forward primer resets any
search in progress.
Args : Optional sequence object or primer object or '' to match beginning
of sequence.
Returns : A sequence object or primer object or undef
=cut
sub fwd_primer {
my ($self, $primer) = @_;
if (defined $primer) {
$self->_set_primer('fwd', $primer);
}
return $self->{fwd_primer};
}
=head2 rev_primer
Title : rev_primer
Usage : my $primer = $search->rev_primer;
Function : Get/set the reverse primer. Setting a new reverse primer resets any
search in progress.
Args : Optional sequence object or primer object or '' to match end of
sequence.
Returns : A sequence object or primer object or undef
=cut
sub rev_primer {
my ($self, $primer) = @_;
if (defined $primer) {
$self->_set_primer('rev', $primer);
}
return $self->{rev_primer};
}
sub _set_primer {
# Save a primer (sequence object) and convert it to regexp. Type is 'fwd' for
# the forward primer or 'rev' for the reverse primer.
my ($self, $type, $primer) = @_;
my $re;
my $match_rna = 1;
if ($primer eq '') {
$re = $type eq 'fwd' ? '^' : '$';
} else {
if ( not(ref $primer) || (
not($primer->isa('Bio::PrimarySeqI')) &&
not($primer->isa('Bio::SeqFeature::Primer')) ) ) {
$self->throw('Expected a sequence or primer object as input but got a '.ref($primer)."\n");
}
$self->{$type.'_primer'} = $primer;
my $seq = $primer->isa('Bio::SeqFeature::Primer') ? $primer->seq : $primer;
$re = Bio::Tools::IUPAC->new(
-seq => $type eq 'fwd' ? $seq : $seq->revcom,
)->regexp($match_rna);
}
$self->{$type.'_regexp'} = $re;
# Reset search in progress
$template_str = undef;
$self->{regexp} = undef;
return $self->{$type.'_primer'};
}
=head2 primer_file
Title : primer_file
Usage : my ($fwd, $rev) = $search->primer_file;
Function : Get/set a sequence file to read the primer from. The first sequence
must be the forward primer, and the second is the optional reverse
primer. After reading the file, the primers are set using fwd_primer()
and rev_primer() and returned.
Args : Sequence file
Returns : Array containing forward and reverse primers as sequence objects.
=cut
sub primer_file {
my ($self, $primer_file) = @_;
# Read primer file and convert primers into regular expressions to catch
# amplicons present in the database
if (not defined $primer_file) {
$self->throw("Need to provide an input file\n");
}
# Mandatory first primer
require Bio::SeqIO;
my $in = Bio::SeqIO->new( -file => $primer_file );
my $fwd_primer = $in->next_seq;
if (not defined $fwd_primer) {
$self->throw("The file '$primer_file' contains no primers\n");
}
$fwd_primer->alphabet('dna'); # Force the alphabet since degenerate primers can look like protein sequences
# Optional reverse primers
my $rev_primer = $in->next_seq;
if (defined $rev_primer) {
$rev_primer->alphabet('dna');
} else {
$rev_primer = '';
}
$in->close;
$self->fwd_primer($fwd_primer);
$self->rev_primer($rev_primer);
return ($fwd_primer, $rev_primer);
}
=head2 attach_primers
Title : attach_primers
Usage : my $attached = $search->attach_primers;
Function : Get/set whether or not to attach primer objects to the amplicon
objects.
Args : Optional integer (1 for yes, 0 for no)
Returns : Integer (1 for yes, 0 for no)
=cut
sub attach_primers {
my ($self, $attach) = @_;
if (defined $attach) {
$self->{attach_primers} = $attach;
require Bio::SeqFeature::Primer;
}
return $self->{attach_primers} || 0;
}
=head2 next_amplicon
Title : next_amplicon
Usage : my $amplicon = $search->next_amplicon;
Function : Get the next amplicon
Args : None
Returns : A Bio::SeqFeature::Amplicon object
=cut
sub next_amplicon {
my ($self) = @_;
# Initialize search
if (not defined $template_str) {
$self->_init;
}
my $re = $self->_regexp;
my $amplicon;
if ($template_str =~ m/$re/g) {
my ($match, $rev_match) = ($1, $2);
my $strand = $rev_match ? -1 : 1;
$match = $match || $rev_match;
my $end = pos($template_str);
my $start = $end - length($match) + 1;
$amplicon = $self->_attach_amplicon($start, $end, $strand);
}
# If no more matches. Make sure calls to next_amplicon() will return undef.
if (not $amplicon) {
$template_str = '';
}
return $amplicon;
}
sub _init {
my ($self) = @_;
# Sanity checks
if ( not $self->template ) {
$self->throw('Need to provide a template sequence');
}
if ( not($self->fwd_primer) && not($self->rev_primer) ) {
$self->throw('Need to provide at least a primer');
}
# Set the template sequence string
$template_str = $self->template->seq;
# Set the regular expression to match amplicons
$self->_regexp;
return 1;
}
sub _regexp {
# Get the regexp to match amplicon. If the regexp is not set, initialize it.
my ($self, $regexp) = @_;
if ( not defined $self->{regexp} ) {
# Build regexp that matches amplicons on both strands and reports shortest
# amplicon when there are several overlapping amplicons
my $fwd_regexp = $self->_fwd_regexp;
my $rev_regexp = $self->_rev_regexp;
my ($fwd_regexp_rc, $basic_fwd_match, $rev_regexp_rc, $basic_rev_match);
if ($fwd_regexp eq '^') {
$fwd_regexp_rc = '';
$basic_fwd_match = "(?:.*?$rev_regexp)";
} else {
$fwd_regexp_rc = Bio::Tools::SeqPattern->new(
-seq => $fwd_regexp,
-type => 'dna',
)->revcom->str;
$basic_fwd_match = "(?:$fwd_regexp.*?$rev_regexp)";
}
if ($rev_regexp eq '$') {
$rev_regexp_rc = '';
$basic_rev_match = "(?:.*?$fwd_regexp_rc)";
} else {
$rev_regexp_rc = Bio::Tools::SeqPattern->new(
-seq => $rev_regexp,
-type => 'dna',
)->revcom->str;
$basic_rev_match = "(?:$rev_regexp_rc.*?$fwd_regexp_rc)";
}
my $fwd_exclude = "(?!$basic_rev_match".
($fwd_regexp eq '^' ? '' : "|$fwd_regexp").
")";
my $rev_exclude = "(?!$basic_fwd_match".
($rev_regexp eq '$' ? '' : "|$rev_regexp_rc").
')';
$self->{regexp} = qr/
( $fwd_regexp (?:$fwd_exclude.)*? $rev_regexp ) |
( $rev_regexp_rc (?:$rev_exclude.)*? $fwd_regexp_rc )
/xi;
}
return $self->{regexp};
}
=head2 annotate_template
Title : annotate_template
Usage : my $template = $search->annotate_template;
Function : Search for all amplicons and attach them to the template.
This is equivalent to running:
while (my $amplicon = $self->next_amplicon) {
# do something
}
my $annotated = $self->template;
Args : None
Returns : A Bio::Seq object with attached Bio::SeqFeature::Amplicons (and
Bio::SeqFeature::Primers if you set -attach_primers to 1).
=cut
sub annotate_template {
my ($self) = @_;
# Search all amplicons and attach them to template
1 while $self->next_amplicon;
# Return annotated template
return $self->template;
}
sub _fwd_regexp {
my ($self) = @_;
return $self->{fwd_regexp};
}
sub _rev_regexp {
my ($self) = @_;
return $self->{rev_regexp};
}
sub _attach_amplicon {
# Create an amplicon object and attach it to template
my ($self, $start, $end, $strand) = @_;
# Create Bio::SeqFeature::Amplicon feature and attach it to the template
my $amplicon = Bio::SeqFeature::Amplicon->new(
-start => $start,
-end => $end,
-strand => $strand,
-template => $self->template,
);
# Create Bio::SeqFeature::Primer feature and attach them to the amplicon
if ($self->attach_primers) {
for my $type ('fwd', 'rev') {
my ($pstart, $pend, $pstrand, $primer_seq);
# Coordinates relative to amplicon
if ($type eq 'fwd') {
# Forward primer
$primer_seq = $self->fwd_primer;
next if not defined $primer_seq;
$pstart = 1;
$pend = $primer_seq->length;
$pstrand = $amplicon->strand;
} else {
# Optional reverse primer
$primer_seq = $self->rev_primer;
next if not defined $primer_seq;
$pstart = $end - $primer_seq->length + 1;
$pend = $end;
$pstrand = -1 * $amplicon->strand;
}
# Absolute coordinates needed
$pstart += $start - 1;
$pend += $start - 1;
my $primer = Bio::SeqFeature::Primer->new(
-start => $pstart,
-end => $pend,
-strand => $pstrand,
-template => $amplicon,
);
# Attach primer to amplicon
if ($type eq 'fwd') {
$amplicon->fwd_primer($primer);
} else {
$amplicon->rev_primer($primer);
}
}
}
return $amplicon;
}
1;
|