Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
get variable width output working
  • Loading branch information
cjfields committed Nov 2, 2014
1 parent a889dcc commit dfa155e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
10 changes: 5 additions & 5 deletions lib/Bio/Role/IO.pm6
Expand Up @@ -5,13 +5,13 @@ 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>;

# initializer; I'm not sure if we can have a BUILD method in the role, but might be worth testing
method initialize_io(*%args) {
# generic IO initializer; more specific ones (e.g. has unique parameter
# settings) should create a new multimethod with a distinct signature.
# See Bio::SeqIO::fasta for an example
multi 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).
$!fh = %args<file>.IO.open(|%args) orelse die "Can't open file: $!";
}
}
$!fh //= %args{'fh'};
}

Expand Down
25 changes: 16 additions & 9 deletions lib/Bio/SeqIO/fasta.pm6
Expand Up @@ -15,8 +15,16 @@ class Bio::Grammar::Fasta::Actions::PrimarySeq {
}

role Bio::SeqIO::fasta does Bio::Role::SeqStream {
has $!buffer;
has $.buffer is rw;
has $!actions = Bio::Grammar::Fasta::Actions::PrimarySeq.new();
has $.width = 60;
has $.block = $!width; # NYI

# Note multimethod signature
multi method initialize_io(:$width?, *%args) {
$!width = $width;
nextsame();
}

# TODO: this is a temporary iterator to return one sequence record at a
# time; two future optimizations require implementation in Rakudo:
Expand All @@ -26,14 +34,14 @@ role Bio::SeqIO::fasta does Bio::Role::SeqStream {
method !chunkify {
return if $.eof();
my $current_record;
while $.get() -> $line {
if $!buffer {
$current_record = $!buffer;
$!buffer = Nil;
while $.get -> $line {
if $.buffer {
$current_record = $.buffer;
$.buffer = Nil;
}
if $line ~~ /^^\>/ {
if $current_record.defined {
$!buffer = "$line\n";
$.buffer = "$line\n";
last;
} else {
$current_record = "$line\n";
Expand All @@ -45,7 +53,6 @@ role Bio::SeqIO::fasta does Bio::Role::SeqStream {
return $current_record;
};


method next-Seq {
my $chunk = self!chunkify;
return if !?$chunk.defined;
Expand All @@ -54,7 +61,7 @@ role Bio::SeqIO::fasta does Bio::Role::SeqStream {
}

method write-Seq(Bio::PrimarySeq $seq) {
#self.fh.print(">");
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 dfa155e

Please sign in to comment.