Bioperl Training Exercise 8
From BITS wiki
- Convert the solution of Bioperl Training Exercise 7 into an Object Oriented way. Remember the 3 little rules:
- To create a class, build a package
- To create a method, write a subroutine
- To create an object, bless a referent
- In other words: create the primer class BITS::Training::Primer with the following methods:
- new (constructor accepting 1 argument: the primer sequence): returns a reference to the blessed referent. Remark: most say that the constructor returns an object; this is actually a kind of 'shortcut' expression;
- getSequence: returns the primer nucleotide stretch
- getLength: return the length of the primer
- isDegenerate: returns true if the primer contains ambiguous residus.
- numberOf (takes a single nucleotide as argument; e.g. you should be able to call $primer->numberOf('A').
- as already mentioned above, use the arrow operator to call a method instead of the 'normal' subroutine call. Remember the Blah->make_noise($noise) call in Bioperl Training Exercise 5 ? The thingy left of the error becomes the first argument ! This thingy might be a package name (aka class) or an object (reference).
- Make the primer.pl script 'consume' your class. Use the Data::Dumper module to dump the primer container (@primer) to STDERR
primer.pl |
---|
|
BITS::Training::Primer |
---|
In the folder where primer.pl is located create the appropriate folders to store the module of package BITS::Training::Primer: (you can do it via the Places menu as well.)
Add the following contents to Primer.pm: package BITS::Training::Primer; use strict; sub new { my ($class, $sequence) = @_; my $self = { _sequence => $sequence, _length => length($sequence), _degenerate => ($sequence =~ /[^ACGT]/i ? 1 : 0), _composition => { A => $sequence =~ tr/A/A/, # count number of A's C => $sequence =~ tr/C/C/, # count number of C's G => $sequence =~ tr/G/G/, # count number of G's T => $sequence =~ tr/T/T/ # count number of T's } }; bless $self, $class; } sub getSequence { my $self = shift; return $self->{_sequence}; } sub getLength { my $self = shift; return $self->{_length}; } sub isDegenerate { my $self = shift; return $self->{_degenerate}; } sub numberOf { my $self = shift; my $nt = shift; $nt = uc $nt; return exists $self->{_composition}->{$nt} ? $self->{_composition}->{$nt} : 0; } 'Just Another True Value'; |