squash waffling test
[bioperl-live.git] / Bio / SeqEvolution / Factory.pm
blobe190a1790257945477a1aa6067d07eb1f1277793
2 # BioPerl module for Bio::SeqEvolution::Factory
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Heikki Lehvaslaiho <heikki at bioperl dot org>
8 # Copyright Heikki Lehvaslaiho
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
14 =head1 NAME
16 Bio::SeqEvolution::Factory - Factory object to instantiate sequence evolving classes
18 =head1 SYNOPSIS
20 # not an instantiable class
22 =head1 DESCRIPTION
24 This is the factory class that can be used to call for a specific
25 model to mutate a sequence.
27 Bio::SeqEvolution::DNAPoint is the default for nucleotide sequences
28 and the only implementation at this point.
30 =head1 FEEDBACK
32 =head2 Mailing Lists
34 User feedback is an integral part of the evolution of this and other
35 Bioperl modules. Send your comments and suggestions preferably to
36 the Bioperl mailing list. Your participation is much appreciated.
38 bioperl-l@bioperl.org - General discussion
39 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
41 =head2 Support
43 Please direct usage questions or support issues to the mailing list:
45 I<bioperl-l@bioperl.org>
47 rather than to the module maintainer directly. Many experienced and
48 reponsive experts will be able look at the problem and quickly
49 address it. Please include a thorough description of the problem
50 with code and data examples if at all possible.
52 =head2 Reporting Bugs
54 Report bugs to the Bioperl bug tracking system to help us keep track
55 of the bugs and their resolution. Bug reports can be submitted via the
56 web:
58 https://github.com/bioperl/bioperl-live/issues
60 =head1 AUTHOR
62 Heikki Lehvaslaiho E<lt>heikki at bioperl dot orgE<gt>
64 =head1 CONTRIBUTORS
66 Additional contributor's names and emails here
68 =head1 APPENDIX
70 The rest of the documentation details each of the object methods.
71 Internal methods are usually preceded with a _
73 =cut
76 # Let the code begin...
79 package Bio::SeqEvolution::Factory;
80 use strict;
81 use Bio::Root::Root;
82 use Bio::SeqEvolution::EvolutionI;
83 use base qw(Bio::Root::Root Bio::SeqEvolution::EvolutionI);
85 =head2 new
87 Title : new
88 Usage : my $obj = Bio::SeqEvolution::Factory->new();
89 Function: Builds a new Bio:SeqEvolution::EvolutionI object
90 Returns : Bio:SeqEvolution::EvolutionI object
91 Args : -type => class name
93 See L<Bio:SeqEvolution::EvolutionI>
95 =cut
97 sub new {
98 my($caller,@args) = @_;
99 my $class = ref($caller) || $caller;
101 my %param = @args;
102 @param{ map { lc $_ } keys %param } = values %param; # lowercase keys
104 if ( $class eq 'Bio::SeqEvolution::Factory') {
105 #my %param = @args;
106 #@param{ map { lc $_ } keys %param } = values %param; # lowercase keys
108 if (exists $param{'-type'}) {
109 # $self->type($param{'-type'});
110 } else {
111 $param{'-type'} = 'Bio::SeqEvolution::DNAPoint';
112 #$self->type('Bio::SeqEvolution::DNAPoint'} unless $seq->alphabet == 'protein'
114 my $type = $param{'-type'};
115 return unless( $class->_load_format_module($param{'-type'}) );
116 return $type->new(%param);
117 } else {
118 my ($self) = $class->SUPER::new(%param);
119 $self->_initialize(%param);
120 return $self;
124 sub _initialize {
125 my($self, @args) = @_;
127 $self->SUPER::_initialize(@args);
128 my %param = @args;
129 @param{ map { lc $_ } keys %param } = values %param; # lowercase keys
131 exists $param{'-seq'} && $self->seq($param{'-seq'});
132 exists $param{'-set_mutated_seq'} && $self->set_mutated_seq($param{'-set_mutated_seq'});
133 exists $param{'-identity'} && $self->identity($param{'-identity'});
134 exists $param{'-pam'} && $self->pam($param{'-pam'});
135 exists $param{'-mutation_count'} && $self->mutation_count($param{'-mutation_count'});
140 =head2 _load_format_module
142 Title : _load_format_module
143 Usage : *INTERNAL SeqIO stuff*
144 Function: Loads up (like use) a module at run time on demand
145 Example :
146 Returns :
147 Args :
149 =cut
151 sub _load_format_module {
152 my ($self, $format) = @_;
153 my $module = $format;
154 my $ok;
156 eval {
157 $ok = $self->_load_module($module);
159 if ( $@ ) {
160 print STDERR <<END;
161 $self: $format cannot be found
162 Exception $@
166 return $ok;
170 =head2 type
172 Title : type
173 Usage : $obj->type($newval)
174 Function: Set used evolution model. It is set by giving a
175 valid Bio::SeqEvolution::* class name
176 Returns : value of type
177 Args : newvalue (optional)
179 Defaults to Bio::SeqEvolution::DNAPoint.
181 =cut
183 sub type{
184 my $self = shift;
185 if (@_) {
186 $self->{'_type'} = shift @_;
187 $self->_load_module($self->{'_type'});
189 return $self->{'_type'} || 'Bio::SeqEvolution::DNAPoint';
192 =head1 mutation counters
194 The next three methods set a value to limit the number of mutations
195 introduced the the input sequence.
197 =cut
199 =head2 identity
201 Title : identity
202 Usage : $obj->identity($newval)
203 Function: Set the desired identity between original and mutated sequence
204 Returns : value of identity
205 Args : newvalue (optional)
207 =cut
209 sub identity{
210 my $self = shift;
211 $self->{'_identity'} = shift @_ if @_;
212 return $self->{'_identity'};
216 =head2 pam
218 Title : pam
219 Usage : $obj->pam($newval)
220 Function: Set the wanted Percentage of Accepted Mutations, PAM
221 Returns : value of PAM
222 Args : newvalue (optional)
224 When you are measuring sequence divergence, PAM needs to be
225 estimated. When you are generating sequences, PAM is simply the count
226 of mutations introduced to the reference sequence normalised to the
227 original sequence length.
229 =cut
231 sub pam{
232 my $self = shift;
233 $self->{'_pam'} = shift @_ if @_;
234 return $self->{'_pam'};
237 =head2 mutation_count
239 Title : mutation_count
240 Usage : $obj->mutation_count($newval)
241 Function: Set the number of wanted mutations to the sequence
242 Returns : value of mutation_count
243 Args : newvalue (optional)
245 =cut
247 sub mutation_count{
248 my $self = shift;
249 $self->{'_mutation_count'} = shift @_ if @_;
250 return $self->{'_mutation_count'};
255 =head2 seq
257 Title : seq
258 Usage : $obj->seq($newval)
259 Function: Set the sequence object for the original sequence
260 Returns : The sequence object
261 Args : newvalue (optional)
263 Setting this will reset mutation and generated mutation counters.
265 =cut
267 sub seq {
268 my $self = shift;
269 if (@_) {
270 $self->{'_seq'} = shift @_ ;
271 return $self->{'_seq'};
272 $self->reset_mutation_counter;
273 $self->reset_sequence_counter;
275 return $self->{'_seq'};
278 =head2 seq_type
280 Title : seq_type
281 Usage : $obj->seq_type($newval)
282 Function: Set the returned seq_type to one needed
283 Returns : value of seq_type
284 Args : newvalue (optional)
286 Defaults to Bio::PrimarySeq.
288 =cut
290 sub seq_type{
291 my $self = shift;
292 if (@_) {
293 $self->{'_seq_type'} = shift @_;
294 $self->_load_module($self->{'_seq_type'});
296 return $self->{'_seq_type'} || 'Bio::PrimarySeq';
300 =head2 get_mutation_counter
302 Title : get_mutation_counter
303 Usage : $obj->get_mutation_counter()
304 Function: Get the count of sequences created
305 Returns : value of counter
306 Args : -
308 =cut
310 sub get_mutation_counter{
311 return shift->{'_mutation_counter'};
315 =head2 reset_mutation_counter
317 Title : reset_mutation_counter
318 Usage : $obj->reset_mutation_counter()
319 Function: Resert the counter of mutations
320 Returns : value of counter
321 Args : -
323 =cut
325 sub reset_mutation_counter{
326 shift->{'_mutation_counter'} = 0;
327 return 1;
331 =head2 get_sequence_counter
333 Title : get_sequence_counter
334 Usage : $obj->get_sequence_counter()
335 Function: Get the count of sequences created
336 Returns : value of counter
337 Args : -
339 =cut
341 sub get_sequence_counter{
342 return shift->{'_sequence_counter'};
345 =head2 reset_sequence_counter
347 Title : reset_sequence_counter
348 Usage : $obj->reset_sequence_counter()
349 Function: Resert the counter of sequences created
350 Returns : value of counter
351 Args : -
353 This is called when ever mutated sequences are reassigned new values
354 using methods seq() and mutated_seq(). As a side affect, this method
355 also recreates the intermal alignment that is used to calculate the
356 sequence identity.
358 =cut
360 sub reset_sequence_counter{
361 my $self = shift;
362 $self->{'_sequence_counter'} = 0;
363 $self->_init_alignment;
364 return 1;
369 =head2 each_seq
371 Title : each_seq
372 Usage : $obj->each_seq($int)
373 Function:
374 Returns : an array of sequences mutated from the reference sequence
375 according to evolutionary parameters given
376 Args : -
378 =cut
380 sub each_seq{
381 my $self = shift;
382 my $number = shift;
384 $self->throw("[$number] ". ' should be a positive integer')
385 unless $number =~ /^[+\d]+$/;
387 my @array;
388 for (my $count=1; $count<$number; $count++) {
389 push @array, $self->next_seq();
392 return @array;
397 =head2 each_mutation
399 Title : each_mutation
400 Usage : $obj->each_mutation
401 Function: return the mutations leading to the last generated
402 sequence in objects
403 Returns : an array of Bio::Variation::DNAMutation objects
404 Args : optional argument to return an array of stringified names
406 =cut
408 sub each_mutation {
409 my $self = shift;
410 my $string = shift;
412 return @{$self->{'_mutations'}} if $string;
414 return map {
415 /(\d+)(\w*)>(\w*)/;
416 # print;
417 my $dnamut = Bio::Variation::DNAMutation->new
418 ('-start' => $1,
419 '-end' => $1,
420 '-length' => 1,
421 '-isMutation' => 1
423 $dnamut->allele_ori( Bio::Variation::Allele->new(-seq => $2,
424 -alphabet => 'dna') );
425 $dnamut->add_Allele( Bio::Variation::Allele->new(-seq => $3,
426 -alphabet => 'dna') );
427 $dnamut;
428 } @{$self->{'_mutations'}}
432 sub get_alignment_identity {
433 my $self = shift;
434 return $self->{'_align'}->overall_percentage_identity;
438 sub get_alignmet {
439 my $self = shift;
440 return $self->{'_align'}->remove_gaps('-', 'all-gaps');
444 =head1 Internal methods
446 =cut
449 =head2 _increase_mutation_counter
451 Title : _increase_mutation_counter
452 Usage : $obj->_increase_mutation_counter()
453 Function: Internal method to increase the counter of mutations performed
454 Returns : value of counter
455 Args : -
457 =cut
459 sub _increase_mutation_counter{
460 return shift->{'_mutation_counter'}++;
465 =head2 _increase_sequence_counter
467 Title : _increase_sequence_counter
468 Usage : $obj->_increase_sequence_counter()
469 Function: Internal method to increase the counter of sequences created
470 Returns : value of counter
471 Args : -
473 =cut
475 sub _increase_sequence_counter{
476 return shift->{'_sequence_counter'}++;