Skip to content

Commit

Permalink
Updated trunk to 0.4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
kgalinsky committed Oct 28, 2009
1 parent df6b6ec commit 15b5f30
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 40 deletions.
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Revision history for JCVI-Translator

0.4.3 Apr 1 2009
- Enforced that functions are called using func(@mandatory, %optional)

0.4.2 Apr 1 2009
- Added find method into utils

Expand Down
66 changes: 39 additions & 27 deletions lib/JCVI/Translator.pm
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use strict;
use warnings;

use version;
our $VERSION = qv('0.4.2');
our $VERSION = qv('0.4.3');

use base qw(Class::Accessor::Fast);
__PACKAGE__->mk_accessors(qw(id names _table _starts _reverse));
Expand Down Expand Up @@ -106,7 +106,7 @@ our $DEFAULT_SANITIZED = 0;
my $translator = new JCVI::Translator();
my $translator = new JCVI::Translator( $id );
my $translator = new JCVI::Translator( $id, $type );
my $translator = new JCVI::Translator( $id, \%params );
This method creates a translator by loading a translation table from the
internal list. Pass an ID and the type of ID. By default, it will load the
Expand Down Expand Up @@ -138,18 +138,18 @@ translation tables.
By default, the "Standard" translation table will be loaded. You may create a
translator with this table by calling any of the following:
my $t = new JCVI::Translator(); # default table
my $t = new JCVI::Translator(1); # explicitly set id
my $t = new JCVI::Translator( 1, 'id' ); # set id and type
my $t = new JCVI::Translator( 'Standard', 'name' ); # set name
my $t = new JCVI::Translator( 'SGC0', 'name' ); # alternate name
my $t = new JCVI::Translator( 'standard', 'name' ); # not case-sensitive
my $t = new JCVI::Translator( 'stan', 'name' ); # partial match ok
my $t = new JCVI::Translator(); # default table
my $t = new JCVI::Translator(1); # explicitly set id
my $t = new JCVI::Translator( 1, { type => 'id' } ); # set id and type
my $t = new JCVI::Translator( 'Standard', { type => 'name' } );
my $t = new JCVI::Translator( 'SGC0', { type => 'name' } );
my $t = new JCVI::Translator( 'standard', { type => 'name' } );
my $t = new JCVI::Translator( 'stan', { type => 'name' } );
For partial matches, JCVI::Translator will use the first matching translation
table.
my $t = new JCVI::Translator( 'mitochondrial', 'name' );
my $t = new JCVI::Translator( 'mitochondrial', { type => 'name' } );
This will use translation table with ID 2, "Vertebrate Mitochondrial," because
that is the first match (even though "Yeast Mitochondrial" would also match).
Expand All @@ -161,19 +161,25 @@ sub new {

my $class = shift;

my ( $id, $type ) = validate_pos(
my ( $id, @p );

( $id, $p[0] ) = validate_pos(
@_,
{ default => $DEFAULT_ID },
{ default => $DEFAULT_TYPE, regex => qr/id|name/ }
{ type => Params::Validate::HASHREF, default => {} }
);

TRACE( uc($type) . ': ' . $id );
my %p =
validate( @p,
{ type => { default => $DEFAULT_TYPE, regex => qr/id|name/ } } );

TRACE( uc( $p{type} ) . ': ' . $id );

# Get the beginning DATA so that we can seek back to it
my $start_pos = tell DATA;

# Set up regular expression for searching.
my $match = ( $type eq 'id' ) ? qr/id $id\b/ : qr/name ".*$id.*"/i;
my $match = ( $p{type} eq 'id' ) ? qr/id $id\b/ : qr/name ".*$id.*"/i;

# Go through every internal table until it matches on id or name.
my $found = 0;
Expand All @@ -190,17 +196,17 @@ sub new {
seek DATA, $start_pos, 0;

# Call custom with internal table. Complete is set to 1.
return $class->custom( \$_, 1 ) if ($found);
return $class->custom( \$_, { complete => 1 } ) if ($found);

# Internal table not matched.
ERROR("Table with $type of $id not found");
ERROR("Table with $p{type} of $id not found");
return undef;
}

=head2 custom()
my $translator = $translator->custom( $table_ref );
my $translator = $translator->custom( $table_ref, $complete );
my $translator = $translator->custom( $table_ref, \%params );
Create a translator table based off a passed table reference for custom
translation tables. Loads degenerate nucleotides if $complete isn't set (this
Expand Down Expand Up @@ -255,13 +261,19 @@ sub custom {

my $class = shift;

my ( $table_ref, $complete ) = validate_pos(
my ($table_ref, @p);

( $table_ref, $p[0] ) = validate_pos(
@_,
{ type => Params::Validate::SCALARREF },
{
{ type => Params::Validate::HASHREF, default => {} }
);

my %p = validate(@p, {
complete => {
default => $DEFAULT_COMPLETE,
regex => qr/^[01]$/
}
}}
);

# Match the table or return undef.
Expand Down Expand Up @@ -333,15 +345,15 @@ sub custom {
}

# Unroll the translation table unless it has been marked complete
$self->bootstrap() unless ($complete);
$self->bootstrap() unless ($p{complete});

return $self;
}

# Helper constructor. Instantiates the object with arrayrefs and hashrefs in
# the right places
sub _new {
my $self = shift->SUPER::new(
my $self = shift->SUPER::new(
{
names => [],
_table => [],
Expand Down Expand Up @@ -465,16 +477,16 @@ sub bootstrap {
=head2 table_string
my $table_string_ref = $translator->_table_string();
my $table_string_ref = $translator->_table_string( $bootstrap );
my $table_string_ref = $translator->_table_string( \%params );
Returns the table string. $bootstrap specifies whether or not this table should
try to bootstrap itself using the bootstrap function above. By default, it is
1.
Returns the table string. %params can specify whether or not this table should
try to bootstrap itself using the bootstrap function above. By default, it will
try to.
Examples:
my $table_string_ref = $translator->_table_string();
my $table_string_ref = $translator->_table_string(0); # To not bootstrap
my $table_string_ref = $translator->_table_string( { bootstrap => 0 } );
=cut

Expand Down
49 changes: 36 additions & 13 deletions lib/JCVI/Translator/Utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ sub _new {
=head2 codons
my $codon_array = $translator->codons( $residue);
my $codon_array = $translator->codons( $residue, $strand );
my $codon_array = $translator->codons( $residue, \%params );
Returns a list of codons for a particular residue or start codon. For start
codons, input "start" for the residue.
Expand All @@ -76,22 +76,34 @@ codons, input "start" for the residue.

sub codons {
my $self = shift;
my ( $residue, $strand ) = validate_pos(

my ( $residue, @p );

( $residue, $p[0] ) = validate_pos(
@_,
{ regex => qr/^(?:$aa_match|start|lower|upper)$/ },
{ type => Params::Validate::HASHREF, default => {} }
);

my %p = validate(
@p,
{
default => 1,
regex => qr/^[+-]?1$/
strand => {
default => 1,
regex => qr/^[+-]?1$/
}
}
);

if ( $residue eq 'lower' ) { $residue = $strand == 1 ? 'start' : '*' }
elsif ( $residue eq 'upper' ) { $residue = $strand == -1 ? 'start' : '*' }
if ( $residue eq 'lower' ) { $residue = $p{strand} == 1 ? 'start' : '*' }
elsif ( $residue eq 'upper' ) {
$residue = $p{strand} == -1 ? 'start' : '*';
}
elsif ( $residue eq 'start' ) { $residue = 'start' }
else { $residue = uc $residue }

return [
@{ $self->_reverse->[ $strand == 1 ? 0 : 1 ]->{$residue} ||= [] } ];
@{ $self->_reverse->[ $p{strand} == 1 ? 0 : 1 ]->{$residue} ||= [] } ];
}

=head2 regex
Expand All @@ -114,15 +126,26 @@ stop codon is stored as "*" by the translator.

sub regex {
my $self = shift;
my ( $residue, $strand ) = validate_pos(
@_, 1,

my ( $residue, @p );

( $residue, $p[0] ) = validate_pos(
@_,
{ regex => qr/^(?:$aa_match|start|lower|upper)$/ },
{ type => Params::Validate::HASHREF, default => {} }
);

my %p = validate(
@p,
{
default => 1,
regex => qr/^[+-]?1$/
strand => {
default => 1,
regex => qr/^[+-]?1$/
}
}
);

my $rc = $strand == 1 ? 0 : 1;
my $rc = $p{strand} == 1 ? 0 : 1;

my $regex = $self->_regexes->[$rc]->{residue};

Expand Down Expand Up @@ -174,7 +197,7 @@ sub find {
while ( $$seq_ref =~ m/(?=$regex)/ig ) {
push @positions, pos($$seq_ref);
}

return \@positions;
}

Expand Down

0 comments on commit 15b5f30

Please sign in to comment.