Skip to content

Instantly share code, notes, and snippets.

@MattOates
Last active August 29, 2015 14:00
Show Gist options
  • Save MattOates/0a0dac1fd96377da2c35 to your computer and use it in GitHub Desktop.
Save MattOates/0a0dac1fd96377da2c35 to your computer and use it in GitHub Desktop.
Playing with FASTA parsing
#!/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;
@Mouq
Copy link

Mouq commented May 3, 2014

You may want something more like:

method record($/) {
    make $<sequence>.ast.new(
        id => ~$<id>,
        comment => ~$<comment>,
        sequence => $<sequence>.subst("\n","", :g) # [aside: ~$<s…>.subst(…) means ~($<s…>.subst(…))]
    );
}
# "sequence:sym<aa>" works just as well as "sequence:aa" as long
# as they're named the same in the grammar and actions :)
method sequence:aa  ($/) { make Bio::Sequence::Amino }
method sequence:dna ($/) { make Bio::Sequence }
method sequence:rna ($/) { make Bio::Sequence }

Hope that's helpful ^_^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment