Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
remedial FASTA parser with grammar working
  • Loading branch information
cjfields committed Oct 31, 2014
1 parent e040384 commit 51d4c53
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 44 deletions.
7 changes: 1 addition & 6 deletions lib/Bio/Role/IO.pm6
Expand Up @@ -5,13 +5,8 @@ use Bio::Role::Temp;
role Bio::Role::IO does Bio::Role::Temp {
has IO::Handle $.fh handles <close encoding eof fileno flush get getc ins p print read say seek t tell write>;

# Methods using
submethod BUILD(*%args) {
self!initialize_io(|%args);
}

# initializer; I'm not sure if we can have a BUILD method in the role, but might be worth testing
method !initialize_io(*%args) {
method initialize_io(*%args) {
if %args{'file'}:exists {
# really, what we need to do is make it so a subset of the named
# args passed in are then passed along (e.g. :r, :w, etc).
Expand Down
4 changes: 3 additions & 1 deletion lib/Bio/Role/SeqStream.pm6
@@ -1,6 +1,8 @@
use v6;

role Bio::Role::SeqStream;
use Bio::Role::IO;

role Bio::Role::SeqStream does Bio::Role::IO;

method next-Seq { ... }

Expand Down
19 changes: 10 additions & 9 deletions lib/Bio/SeqIO.pm6
@@ -1,22 +1,22 @@
use v6;

use Bio::Role::Pluggable;
use Bio::Role::SeqStream;
use Bio::Role::RecordFormat;
use Bio::Role::IO;

class Bio::SeqIO does Bio::Role::Pluggable['SeqIO']
does Bio::Role::RecordFormat
does Bio::Role::SeqStream
does Bio::Role::IO {
{

submethod BUILD(:$!format!, :$!format-variant, :$!format-version) {
submethod BUILD(*%args) {
die "Must provide format" unless %args<format>:exists;

if $!format ~~ / <[-]> / {
($!format, $!format-variant) = $!format.split: '-', 2;
if %args<format> ~~ / <[-]> / {
($!format, $!format-variant) = %args<format>.split: '-', 2;
} else {
$!format = %args<format>.lc;
}
my $plugin = "Bio::SeqIO::" ~ $!format.lc;

my $plugin = "Bio::SeqIO::" ~ $!format;

try {
require $plugin;
Expand All @@ -28,6 +28,7 @@ class Bio::SeqIO does Bio::Role::Pluggable['SeqIO']
# mix in the plugin module
self does ::($plugin);
}
self.initialize_io(|%args);
}

method next-Seq { ... }
Expand Down
72 changes: 44 additions & 28 deletions lib/Bio/SeqIO/fasta.pm6
@@ -1,39 +1,55 @@
use v6;

use Bio::Role::SeqStream;
use Bio::Grammar::Fasta;

role Bio::SeqIO::fasta does Bio::Role::SeqStream;
class Bio::Grammar::Fasta::Actions {

has $!buffer;
method record($/) {
make $<sequence>
}
}

method chunkify {
# null condition to stop iterator
return if self.fh.eof;
my $current_record;
while self.fh.get -> $line {
if $!buffer {
$current_record = $!buffer;
$!buffer = Nil;
}
if $line ~~ /^^\>/ {
if $current_record.defined {
$!buffer = "$line\n";
last;
role Bio::SeqIO::fasta does Bio::Role::SeqStream {
has $!buffer;

has $!actions = Bio::Grammar::Fasta::Actions.new();

# TODO: this is a temporary iterator to return one sequence record at a
# time; two future optimizations require implementation in Rakudo:
# 1) Chunking in IO::Handle using nl => "\n>"
# 2) Grammar parsing of a stream of data (e.g. Cat), which is now considered
# a close post-6.0 update
method !chunkify {
return if $.eof();
my $current_record;
while $.get() -> $line {
if $!buffer {
$current_record = $!buffer;
$!buffer = Nil;
}
if $line ~~ /^^\>/ {
if $current_record.defined {
$!buffer = "$line\n";
last;
} else {
$current_record = "$line\n";
}
} else {
$current_record = "$line\n";
$current_record ~= $line;
}
} else {
$current_record ~= $line;
}
return $current_record;
};


method next-Seq {
my $chunk = self!chunkify;
return if !?$chunk.defined;
my $t = Bio::Grammar::Fasta.subparse($chunk, actions => $!actions, rule => 'record');
return $t;
}
return $current_record;
};


method write-Seq { ... }

method next-Seq {
self.chunkify();
}

method write-Seq {

}
}

0 comments on commit 51d4c53

Please sign in to comment.