Last active
August 29, 2015 14:00
-
-
Save MattOates/0a0dac1fd96377da2c35 to your computer and use it in GitHub Desktop.
Playing with FASTA parsing
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
#!/usr/bin/env perl6 | |
use v6; | |
class Bio::Sequence { | |
has Str $.id = ""; | |
has Str $.comment = ""; | |
has Str $.sequence = ""; | |
} | |
class Bio::Sequence::Amino is Bio::Sequence { | |
} | |
class Bio::Actions::FASTA { | |
method TOP($/) { | |
make $<record>>>.ast; | |
} | |
method record($/) { | |
given ~$<sequence><sym> { | |
when "aa" { | |
make Bio::Sequence::Amino.new( | |
id => ~$<id>, | |
comment => ~$<comment>, | |
sequence => ~$<sequence>.subst("\n","", :g) | |
); | |
} | |
default { | |
make Bio::Sequence.new( | |
id => ~$<id>, | |
comment => ~$<comment>, | |
sequence => ~$<sequence>.subst("\n","", :g) | |
); | |
} | |
} | |
} | |
} | |
grammar Bio::Grammar::FASTA { | |
token TOP { | |
<record>+ | |
} | |
token record { | |
">"<id>" "<comment>"\n"<sequence> | |
} | |
token id { | |
<-[\ ]>+ | |
} | |
token comment { | |
<-[\n]>+ | |
} | |
proto token sequence {*} | |
token sequence:sym<aa> { | |
<[A..Z\*\-\n]>+ | |
} | |
token sequence:sym<dna> { | |
<[ACGTRYKMSWBDHVNX\-\n]>+ | |
} | |
token sequence:sym<rna> { | |
<[ACGURYKMSWBDHVNX\-\n]>+ | |
} | |
} | |
my $sequences = slurp "test.fa"; | |
my $results = Bio::Grammar::FASTA.parse( $sequences, actions => Bio::Actions::FASTA.new() ).ast; | |
say $results.perl; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You may want something more like:
Hope that's helpful ^_^