PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/crest/perlmodules/perl5/modules/BioPerl-1.6.923/share/perl/5.14.2/Bio/Tools/Primer3.pm

https://gitlab.com/pooja043/Globus_Docker_2
Perl | 437 lines | 300 code | 103 blank | 34 comment | 29 complexity | 30fa6d8fe4b5baccecb9948973b68f31 MD5 | raw file
  1. #
  2. # BioPerl module for Bio::Tools::Primer3
  3. #
  4. # Copyright (c) 2003 bioperl, Rob Edwards. All Rights Reserved.
  5. # This module is free software; you can redistribute it and/or
  6. # modify it under the same terms as Perl itself.
  7. #
  8. # Copyright Rob Edwards
  9. #
  10. # You may distribute this module under the same terms as perl itself
  11. # POD documentation - main docs before the code
  12. =head1 NAME
  13. Bio::Tools::Primer3 - Create input for and work with the output from
  14. the program primer3
  15. =head1 SYNOPSIS
  16. # parse primer3 output to get some data
  17. # this is also called from Bio::Tools::Run::Primer3
  18. use Bio::Tools::Primer3;
  19. # read a primer3 output file
  20. my $p3 = Bio::Tools::Primer3->new(-file=>"data/primer3_output.txt");
  21. # how many results were there?
  22. my $num = $p3->number_of_results;
  23. print "There were $num results\n";
  24. # get all the results
  25. my $all_results = $p3->all_results;
  26. print "ALL the results\n";
  27. foreach my $key (keys %{$all_results}) {
  28. print "$key\t${$all_results}{$key}\n";
  29. }
  30. # get specific results
  31. my $result1 = $p3->primer_results(1);
  32. print "The first primer is\n";
  33. foreach my $key (keys %{$result1}) {
  34. print "$key\t${$result1}{$key}\n";
  35. }
  36. # get the results as a Bio::Seq::PrimedSeq stream
  37. my $primer = $p3->next_primer;
  38. print "The left primer in the stream is ",
  39. $primer->get_primer('-left_primer')->seq->seq, "\n";
  40. =head1 DESCRIPTION
  41. Bio::Tools::Primer3 creates the input files needed to design primers using
  42. primer3 and provides mechanisms to access data in the primer3 output files.
  43. This module provides a bioperl interface to the program primer3. See
  44. http://www-genome.wi.mit.edu/genome_software/other/primer3.html
  45. for details and to download the software.
  46. This module is based on one written by Chad Matsalla
  47. (bioinformatics1@dieselwurks.com)
  48. I have ripped some of his code, and added a lot of my own. I hope he
  49. is not mad at me!
  50. This is probably best run in one of the two following ways:
  51. i. To parse the output from Bio::Tools::Run::Primer3.
  52. You will most likely just use next_primer to get the results from
  53. Bio::Tools::Run::Primer3.
  54. ii. To parse the output of primer3 handed to it as a file name.
  55. =head1 FEEDBACK
  56. =head2 Mailing Lists
  57. User feedback is an integral part of the evolution of this and other
  58. Bioperl modules. Send your comments and suggestions preferably to one
  59. of the Bioperl mailing lists. Your participation is much appreciated.
  60. bioperl-l@bioperl.org - General discussion
  61. http://bioperl.org/wiki/Mailing_lists - About the mailing lists
  62. =head2 Support
  63. Please direct usage questions or support issues to the mailing list:
  64. I<bioperl-l@bioperl.org>
  65. rather than to the module maintainer directly. Many experienced and
  66. reponsive experts will be able look at the problem and quickly
  67. address it. Please include a thorough description of the problem
  68. with code and data examples if at all possible.
  69. =head2 Reporting Bugs
  70. Report bugs to the Bioperl bug tracking system to help us keep track
  71. the bugs and their resolution. Bug reports can be submitted via the web:
  72. https://redmine.open-bio.org/projects/bioperl/
  73. =head1 AUTHOR -
  74. Rob Edwards
  75. redwards@utmem.edu
  76. Based heavily on work of
  77. Chad Matsalla
  78. bioinformatics1@dieselwurks.com
  79. =head1 CONTRIBUTORS
  80. Brian Osborne bosborne at alum.mit.edu
  81. =head1 APPENDIX
  82. The rest of the documentation details each of the object methods.
  83. Internal methods are usually preceded with a _
  84. =cut
  85. # Let the code begin...
  86. package Bio::Tools::Primer3;
  87. use strict;
  88. use Bio::Seq;
  89. use Bio::Seq::PrimedSeq;
  90. use Bio::SeqFeature::Primer;
  91. use vars qw($AUTOLOAD @PRIMER3_PARAMS %OK_FIELD $ID);
  92. BEGIN {
  93. @PRIMER3_PARAMS = qw(results seqobject);
  94. foreach my $attr (@PRIMER3_PARAMS) {$OK_FIELD{$attr}++}
  95. }
  96. use base qw(Bio::Root::Root Bio::Root::IO);
  97. sub AUTOLOAD {
  98. my $self = shift;
  99. my $attr = $AUTOLOAD;
  100. $attr =~ s/.*:://;
  101. $self->throw("Unallowed parameter: $attr !") unless $OK_FIELD{$attr};
  102. $self->{$attr} = shift if @_;
  103. return $self->{$attr};
  104. }
  105. =head2 new
  106. Title : new()
  107. Usage : my $primer3 = Bio::Tools::Primer3->new(-file=>$file);
  108. Function: Parse primer3 output
  109. Returns : Does not return anything. If called with a filename will
  110. allow you to retrieve the results
  111. Args : -file (optional) file of primer3 results to parse -verbose
  112. (optional) set verbose output
  113. Notes :
  114. =cut
  115. sub new {
  116. my($class,%args) = @_;
  117. my $self = $class->SUPER::new(%args);
  118. if ($args{'-file'}) {
  119. $self->_readfile($args{'-file'});
  120. }
  121. if ($args{'-verbose'}) {
  122. $self->{'verbose'} = 1;
  123. }
  124. return $self;
  125. }
  126. =head2 number_of_results
  127. Title : number_of_results()
  128. Usage : my $count = $primer3->number_of_results();
  129. Function: Retrieve the number of primers returned from Primer3.
  130. Returns : A scalar
  131. Args : None
  132. Notes : This returns the count of the primers returned by Primer3
  133. (aka how many of them there are).
  134. This is one more than the maximum offset into the zero
  135. based list of primers that is accessed by primer_results().
  136. =cut
  137. sub number_of_results {
  138. my $self = shift;
  139. return $self->{'maximum_primers_returned'} + 1;
  140. }
  141. =head2 all_results
  142. Title : all_results()
  143. Usage : my $results = $primer3->all_results();
  144. or
  145. my $results = $primer3->all_results('primer3 result name', 'other results');
  146. Function: Retrieve the results returned from Primer3.
  147. Returns : A reference to a hash
  148. Args : Optional array of specific results to retrieve
  149. =cut
  150. sub all_results {
  151. my ($self, @results) = @_;
  152. my %hash;
  153. if (@results) {
  154. # we only want a few things
  155. foreach my $result (@results) {
  156. $hash{$result} = $self->{'results'}->$result;
  157. }
  158. } else {
  159. foreach my $result (keys %{$self->{'results'}}) {
  160. $hash{$result}=$self->{'results'}->{$result};
  161. }
  162. }
  163. return \%hash;
  164. }
  165. =head2 primer_results
  166. Title : primer_results()
  167. Usage : my $results = $primer3->primer_results(2); # results for third primer
  168. Function: Retrieve the results returned from Primer3 for specific primer pairs.
  169. Returns : A reference to a hash
  170. Args : A number between 0 and the maximum number of primers to retrieve
  171. =cut
  172. sub primer_results {
  173. my ($self, $toget) = @_;
  174. if ($toget > $self->{'maximum_primers_returned'}) {
  175. $self->warn("Didn't get any results for $toget");
  176. return 0;
  177. } else {
  178. return \%{$self->{'results_by_number'}->{$toget}};
  179. }
  180. }
  181. =head2 _readfile
  182. Title : _readfile()
  183. Usage : $self->_readfile();
  184. Function: An internal function that reads a file and sets up the results
  185. Returns : Nothing.
  186. Args : None
  187. Notes :
  188. =cut
  189. sub _readfile {
  190. my ($self, $file) = @_;
  191. $self->_initialize_io(-file=>$file);
  192. my $line;
  193. my $id='primer 3 parsed results'; # hopefully we'll get this, but we can set a temp id in case not.
  194. while (defined($line = $self->_readline()) ) {
  195. chomp $line;
  196. next unless ($line);
  197. my ($return, $value) = split /=/, $line;
  198. if (uc($return) eq "SEQUENCE") {
  199. $self->{seqobject} = Bio::Seq->new(-seq=>$value, $id=>$id);
  200. next;
  201. }
  202. if (uc($return) eq "PRIMER_SEQUENCE_ID") {
  203. if ($self->{seqobject}) {$self->{seqobject}->id($value)} else {$id=$value}
  204. }
  205. $self->{'results'}->{$return} = $value;
  206. }
  207. # convert the results to individual results
  208. $self->_separate();
  209. }
  210. =head2 next_primer
  211. Title : next_primer()
  212. Usage : while (my $primed_seq = $primer3->next_primer()) {
  213. Function: Retrieve the primed sequence and a primer pair, one at a time
  214. Returns : Returns a Bio::Seq::PrimedSeq object, one at a time
  215. Args : None
  216. Notes : Use $primed_seq->annotated_seq to get an annotated sequence
  217. object you can write out.
  218. =cut
  219. sub next_primer {
  220. my $self = shift;
  221. # here we are going to convert the primers to Bio::SeqFeature::Primer objects
  222. # and the primer/sequence to Bio::Seq::PrimedSeq objects
  223. # the problem at the moment is that PrimedSeq can only take one sequence/primer pair, and
  224. # yet for each sequence we can have lots of primer pairs. We need a way to overcome this.
  225. # at the moment we can do this as a stream, I guess.
  226. $self->warn("No primers were found for: ".$self->{'seqobject'}->{'primary_id'})
  227. if (! $self->number_of_results);
  228. $self->{'next_to_return'} = 0 unless ($self->{'next_to_return'});
  229. return if ($self->{'next_to_return'} >= $self->number_of_results);
  230. my $results = $self->primer_results($self->{'next_to_return'});
  231. $self->throw("No left primer sequence") unless (${$results}{'PRIMER_LEFT_SEQUENCE'});
  232. $self->throw("No right primer sequence") unless (${$results}{'PRIMER_RIGHT_SEQUENCE'});
  233. $self->throw("No target sequence") unless ($self->{'seqobject'});
  234. my $left_seq = Bio::SeqFeature::Primer->new(
  235. -id => 'left_primer',
  236. -seq => ${$results}{'PRIMER_LEFT_SEQUENCE'},
  237. -display_id => ($self->{'next_to_return'} + 1),
  238. );
  239. my $right_seq = Bio::SeqFeature::Primer->new(
  240. -id => "right_primer",
  241. -seq => ${$results}{'PRIMER_RIGHT_SEQUENCE'},
  242. -display_id => ($self->{'next_to_return'} + 1) );
  243. # add data to the Primer objects
  244. for my $key (%$results) {
  245. # skip the primer sequence data, already added above
  246. next if ($key =~ /PRIMER_(LEFT|RIGHT)_SEQUENCE/i );
  247. if ($key =~ /PRIMER_LEFT/i) {
  248. $left_seq->add_tag_value($key, $$results{$key});
  249. } elsif ($key =~ /PRIMER_RIGHT/i) {
  250. $right_seq->add_tag_value($key, $$results{$key});
  251. }
  252. }
  253. my $primed_seq = Bio::Seq::PrimedSeq->new(
  254. -target_sequence => $self->{'seqobject'}->clone,
  255. -left_primer => $left_seq,
  256. -right_primer => $right_seq,
  257. );
  258. # add data to the the PrimedSeq object that's not specific to the Primers
  259. for my $key (%$results) {
  260. next if ($key =~ /PRIMER_(LEFT|RIGHT)/i );
  261. $primed_seq->add_tag_value($key, $$results{$key});
  262. }
  263. $self->{'next_to_return'}++;
  264. return $primed_seq;
  265. }
  266. =head2 primer_stream
  267. Title : primer_stream()
  268. Usage : while (my $primed_seq = $primer3->primer_stream()) {
  269. Function: Retrieve the primer/sequences one at a time
  270. Returns : Returns a Bio::Seq::PrimedSeq object, one at a time
  271. Args : None
  272. Notes : Deprecated, just a link to next_primer
  273. =cut
  274. sub primer_stream {
  275. my $self = shift;
  276. my $primedseq = $self->next_primer;
  277. return $primedseq;
  278. }
  279. =head2 _separate
  280. Title : _separate()
  281. Usage : $self->_separate();
  282. Function: An internal function that groups the results by number
  283. (e.g. primer pair 1, etc)
  284. Returns : Nothing.
  285. Args : None
  286. Notes :
  287. =cut
  288. sub _separate {
  289. my $self = shift;
  290. my %results; # the results that we find
  291. my $maxlocation = -1; # the maximum number of primers returned
  292. foreach my $key (keys %{$self->{'results'}}) {
  293. next if (${$self->{'input_options'}}{$key}); # don't process it if it is an input key
  294. my $location; # the number of the primer pair
  295. # names will have values like
  296. # PRIMER_RIGHT_SEQUENCE, PRIMER_RIGHT_2_SEQUENCE, PRIMER_PRODUCT_SIZE, and
  297. # PRIMER_PRODUCT_SIZE_3 hence we need to find and remove the number
  298. my $tempkey = $key;
  299. if ($tempkey =~ s/_(\d+)//) {
  300. $location = $1;
  301. if ($location > $maxlocation) {$maxlocation = $location}
  302. } elsif ( $tempkey =~ /PRIMER_(RIGHT|LEFT)_SEQUENCE/ ) {
  303. # first primers reported without a number, therefore set $location to 0
  304. $location = 0;
  305. if ($location > $maxlocation) {$maxlocation = $location}
  306. } else {
  307. $location = 0;
  308. }
  309. # we will hash the results by number, and then by name
  310. ${$results{$location}}{$tempkey}=${$self->{'results'}}{$key};
  311. }
  312. $self->{'results_by_number'} = \%results;
  313. $self->{'maximum_primers_returned'} = $maxlocation;
  314. }
  315. =head2 _set_variable
  316. Title : _set_variable()
  317. Usage : $self->_set_variable('variable name', 'value');
  318. Function: An internal function that sets a variable
  319. Returns : Nothing.
  320. Args : None
  321. Notes : Used to set $self->{results} and $self->seqobject
  322. =cut
  323. sub _set_variable {
  324. my ($self, $name, $value) = @_;
  325. next unless ($name);
  326. $self->{$name} = $value;
  327. }
  328. 1;
  329. __END__