Skip to content

Commit

Permalink
remove commented code from SeqIO::fasta, but modify test grammar and …
Browse files Browse the repository at this point in the history
…get a 2x-3x increase in parsing
  • Loading branch information
cjfields committed Mar 26, 2017
1 parent 335c484 commit d1f1af2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 30 deletions.
8 changes: 4 additions & 4 deletions lib/Bio/Grammar/Fasta.pm6
Expand Up @@ -15,17 +15,17 @@ grammar Bio::Grammar::Fasta {
token record {
<description_line> <sequence>
}
token description_line {
^^\> <identifier> <description>? \n
rule description_line {
^^\><identifier><.ws><description>?\n
}
token identifier {
#assume we going to parse NCBI specific id for reference number and gi numbers
\S+
}
}
token description {
\N+
}
token sequence {
rule sequence {
<-[>]>+
}
}
33 changes: 7 additions & 26 deletions lib/Bio/SeqIO/fasta.pm6
Expand Up @@ -15,7 +15,7 @@ class Bio::Grammar::Fasta::Actions::PrimarySeq {
)
);
}

method sequence($/) {
$/.make(~$/.subst("\n", '', :g))
}
Expand All @@ -26,37 +26,18 @@ class Bio::SeqIO::fasta does Bio::Role::SeqStream does Bio::Role::IO {
has $!actions = Bio::Grammar::Fasta::Actions::PrimarySeq.new();
has $.width = 60;
has $.block = $!width; # NYI

multi method initialize-io(:$!width?, :$fh?, :$file?, *%args) {
# we reset the input record sep here
callwith(:nl-in("\n>"), :$fh, :$file, |%args);
}

# TODO: one key future optimization requires implementation in Rakudo:
# 1) Grammar parsing of a stream of data (e.g. Cat), which is now considered
# a post-6.0 update

# 2) Make next-Seq() a multi, which allows it to take arguments for various iterators
#method next-Seq() {
# return if $.fh.eof();
# while $.fh.get -> $chunk {
# #my ($header, $seq) = $chunk.split("\n", 2);
# #$header.=subst(/^^\>/, '');
# #my ($display_id, $desc) = $header.split(' ', 2);
# #$seq.=subst("\n", '', :g);
# #return Bio::PrimarySeq.new(
# # seq => $seq,
# # description => $desc || '',
# # display_id => $display_id
# #);
# if $chunk !~~ /^^\>/ {
# return Bio::Grammar::Fasta.parse( ">$chunk", actions => $!actions, rule => 'record').ast;
# } else {
# return Bio::Grammar::Fasta.parse( "$chunk", actions => $!actions, rule => 'record').ast;
# }
# }
#}


method next-Seq() {
return if $.fh.eof();
while $.fh.get -> $chunk {
Expand All @@ -76,9 +57,9 @@ class Bio::SeqIO::fasta does Bio::Role::SeqStream does Bio::Role::IO {
#}
}
}

method write-Seq(Bio::PrimarySeq $seq) {
self.fh.say(sprintf(">%s %s", $seq.display_id, $seq.description));
self.fh.say($seq.seq.subst( /(.** {$.width})/, { "$0\n" }, :g));
}
}
}

0 comments on commit d1f1af2

Please sign in to comment.