Last active
August 29, 2015 14:11
-
-
Save swuecho/09a0b815a2f08a74f140 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v5.20; | |
use DDP; | |
package Seq { | |
use Moo; | |
has [ 'id', 'comment', 'sequence' ] => ( is => 'rw' ); | |
1; | |
} | |
package FASTA { | |
use Moo; | |
my $parser = do { | |
use Regexp::Grammars; | |
qr/ | |
<TOP> | |
<nocontext:> | |
<token: TOP> <[record]>+ | |
<token: record> <.start=(\>)><id><comment>?\n<sequence> | |
<token: id> [^\-\s\n]+ | |
<token: comment> \s[^\n]+ | |
<token: sequence> <dna>|<rna>|<aa> | |
<token: dna> [ACGTRYKMSWBDHVNX\-\n]+ | |
<token: rna> [ACGURYKMSWBDHVNX\-\n]+ | |
<token: aa> [A-Z\*\-\n]+ | |
/; | |
}; | |
has 'parser' => ( is => 'ro', default => sub { $parser } ); | |
sub record { | |
my ( $self, $result ) = @_; | |
return Seq->new(%$result); | |
} | |
} | |
my $content = <<'END'; | |
>hello | |
GCTATATAAGC | |
>world prot | |
TATAKEKEKELKL | |
END | |
my $fasta = FASTA->new(); | |
if ( $content =~ $fasta->parser->with_actions($fasta) ) { | |
p %/; | |
} | |
__END__ | |
{ | |
TOP { | |
record [ | |
[0] Seq { | |
Parents Moo::Object | |
public methods (4) : comment, id, new, sequence | |
private methods (0) | |
internals: { | |
id "hello", | |
sequence { | |
dna "GCTATATAAGC | |
" | |
} | |
} | |
}, | |
[1] Seq { | |
Parents Moo::Object | |
public methods (4) : comment, id, new, sequence | |
private methods (0) | |
internals: { | |
comment " prot", | |
id "world", | |
sequence { | |
dna "TATAK" | |
} | |
} | |
} | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment