Skip to content

Commit

Permalink
promote Types to their own namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Fields committed Jun 12, 2010
1 parent 955ca05 commit bfc97a8
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
106 changes: 106 additions & 0 deletions lib/Biome/Type/Segment.pm
@@ -0,0 +1,106 @@
# This is a simple package with proposed subtypes; these may end up
# being split into various packages based on use, namespace, etc.

# TODO:
# I have tried MooseX::Types and I find it a bit slower that vanilla
# Moose::Util::TypeParameters, so may revert to the latter if speed becomes an
# issue

package Biome::Type::Segment;

use MooseX::Types -declare => [qw(
Segment_Pos_Symbol
Segment_Pos_Type
Segment_Symbol
Segment_Type
Split_Segment_Type
)];

use MooseX::Types::Moose qw(Int Str Object CodeRef Any);

my %VALID_SEGMENT_SYMBOL = (
'.' => 'WITHIN',
'..' => 'EXACT',
'^' => 'IN-BETWEEN',
);

my %VALID_SEGMENT_POS_SYMBOL = (
'..' => 'EXACT',
'<' => 'BEFORE',
'>' => 'AFTER',
'.' => 'WITHIN',
'?' => 'UNCERTAIN'
);

my %SYMBOL_TYPE = (
'EXACT' => '..',
'BEFORE' => '<',
'AFTER' => '>',
'WITHIN' => '.',
'IN-BETWEEN' => '^',
'UNCERTAIN' => '?'
);

my %TYPE_SYMBOL = map {$SYMBOL_TYPE{$_} => $_} keys %SYMBOL_TYPE;

# WITHIN here is very rare but does occur, ex (122.144)
my %VALID_SEGMENT_TYPE = map {$_ => 1}
qw(EXACT IN-BETWEEN WITHIN);

my %VALID_SEGMENT_POS_TYPE = map {$_ => 1}
qw(EXACT BEFORE AFTER WITHIN UNCERTAIN);

# TODO: some of these could probably be redef. as enums, but it makes coercion
# easier, needs checking

subtype Segment_Symbol,
as Str,
where {exists $VALID_SEGMENT_SYMBOL{$_}},
message {"Unknown Segment symbol $_"};

subtype Segment_Type,
as Str,
where {exists $VALID_SEGMENT_TYPE{$_}},
message {"Unknown Segment type $_"};

subtype Segment_Pos_Symbol,
as Str,
where {exists $VALID_SEGMENT_POS_SYMBOL{$_}},
message {"Unknown Segment positional symbol $_"};

subtype Segment_Pos_Type,
as Str,
where {exists $VALID_SEGMENT_POS_TYPE{$_}},
message {"Unknown Segment positional type $_"};

coerce Segment_Pos_Type,
from Segment_Pos_Symbol,
via {$TYPE_SYMBOL{$_}};

coerce Segment_Pos_Symbol,
from Segment_Pos_Type,
via {$SYMBOL_TYPE{$_}};

coerce Segment_Symbol,
from Segment_Type,
via {$SYMBOL_TYPE{$_}};

coerce Segment_Type,
from Segment_Symbol,
via {$TYPE_SYMBOL{$_}};

my %VALID_SPLIT_TYPE = map {$_ => 1}
qw(JOIN ORDER BOND);

subtype Split_Segment_Type,
as Str,
where {exists $VALID_SPLIT_TYPE{uc $_}},
message {"Unknown Split Location type $_"};

no MooseX::Types;
no MooseX::Types::Moose;

1;

__END__
52 changes: 52 additions & 0 deletions lib/Biome/Type/Sequence.pm
@@ -0,0 +1,52 @@
# This is a simple package with proposed subtypes; these may end up
# being split into various packages based on use, namespace, etc.

# TODO:
# I have tried MooseX::Types and I find it a bit slower that vanilla
# Moose::Util::TypeParameters, so may revert to the latter if speed becomes an
# issue

package Biome::Type::Sequence;

use MooseX::Types -declare => [qw(
Sequence_Strand
Sequence_Strand_Int
Sequence_Strand_Symbol
Sequence_Alphabet
)];

use MooseX::Types::Moose qw(Int Str Object CodeRef Any);

subtype Sequence_Strand,
as Int,
where {$_ >= -1 && $_ <= 1},
message { "Strand can be -1, 0, or 1, not $_"};

subtype Sequence_Strand_Symbol,
as Str,
where { /^(?:[\+\-\.])$/},
message { "Strand symbol can be one of [-.+], not $_"};


my %STRAND_SYMBOL = (
'+' => 1,
'.' => 0,
'-' => -1
);

coerce Sequence_Strand,
from Sequence_Strand_Symbol,
via {$STRAND_SYMBOL{$_}};

subtype Sequence_Alphabet,
as Str,
where { /^(?:dna|rna|protein)$/xism }, # do we want more?
message { "Strand must be 'dna', 'rna', or 'protein'"};

no MooseX::Types;
no MooseX::Types::Moose;

1;

__END__

0 comments on commit bfc97a8

Please sign in to comment.