Skip to content

Instantly share code, notes, and snippets.

@swuecho
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swuecho/09a0b815a2f08a74f140 to your computer and use it in GitHub Desktop.
Save swuecho/09a0b815a2f08a74f140 to your computer and use it in GitHub Desktop.
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