Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Day 15 of the Perl6 2014 Advent Calendar
grammar FASTA::Grammar {
token TOP { <record>+ }
token record { ">"<id><comment>?"\n"<sequence> }
token id { <-[\ \n]>+ }
token comment { " "<-[\n]>+ }
proto rule sequence {*}
rule sequence:sym<dna> { <[ACGTRYKMSWBDHVNX\-\n]>+ }
rule sequence:sym<rna> { <[ACGURYKMSWBDHVNX\-\n]>+ }
rule sequence:sym<aa> { <[A..Z\*\-\n]>+ }
}
class Seq {
has Str $.id = "";
has Str $.comment = "";
has Str $.sequence = "";
multi method Numeric (Seq:D: --> Numeric) {
$.sequence.codes;
}
multi method Bag (Seq:D: --> Bag) {
bag $.sequence.comb;
}
multi method Str (Seq:D: --> Str) {
$.sequence;
}
multi method gist (Seq:D: --> Str) {
">$.id $.comment\n$.sequence";
}
}
class FASTA::Actions {
method TOP($/) {
make $<record>>>.ast;
}
method record($/) {
make Seq.new(
id => ~$<id>,
comment => ($<comment>) ?? (~$<comment>).trim !! '',
sequence => $<sequence>.subst("\n","", :g)
);
}
}
TOP
>
| record
>
| | id
>
| | * MATCH "hello"
>
| | comment
>
| | * FAIL
>
| | sequence
>
| | | sequence:sym<dna>
>
| | | * MATCH "GCTATATAAGC\n"
>
| | * MATCH "GCTATATAAGC\n"
>
| * MATCH ">hello\nGCTATATAAGC\n"
>
| record
>
| | id
>
| | * MATCH "world"
>
| | comment
>
| | * MATCH " prot"
>
| | sequence
>
| | | sequence:sym<dna>
>
| | | * FAIL
>
| | | sequence:sym<rna>
>
| | | * FAIL
>
| | | sequence:sym<aa>
>
| | | * MATCH "TATAKEKEKELKL\n"
>
| | * MATCH "TATAKEKEKELKL\n"
>
| * MATCH ">world prot\nTATAKEKEKELKL\n"
>
| record
>
| * FAIL
>
* MATCH ">hello\nGCTATATAAGC\n>world prot\nTATAKEKEKELKL\n"
>hello
GCTATATAAGC
>world prot
TATAKEKEKELKL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment