1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
package FastaIndex;
use strict;
use warnings;
use IO::File;
BEGIN {
for my $field (qw( fastaFileName startpos lines fh ))
{
my $slot = __PACKAGE__ . "::$field";
no strict "refs"; # So symbolic ref to typeglob works.
*$field = sub
{
my $self = shift;
$self->{$slot} = shift if @_;
return $self->{$slot};
};
}
}
sub new {
my ($this) = @_;
my $class = ref($this) || $this;
my $self = {};
bless $self, $class;
return $self;
}
sub make_index {
my ($this, $fastaFileName, $idregex, $dbmFileName) = @_;
if(!defined $idregex) { $idregex = "(\\S+)"; }
$this->fastaFileName($fastaFileName);
#open(IN, "$fastaFileName") or die "could not open $fastaFileName";
my $in = IO::File->new("$fastaFileName") or die "could not open $fastaFileName";
$this->fh($in);
if($dbmFileName)
{
my %openpos = ();
dbmopen( %openpos, $dbmFileName.".pos", 0666) || die ("Could not open DBM file $dbmFileName.pos");
$this->startpos(\%openpos);
my %openlines = ();
dbmopen( %openlines, $dbmFileName.".lines", 0666) || die ("Could not open DBM file $dbmFileName.lines");
$this->lines(\%openlines);
my $sizepos = scalar keys %openpos;
my $sizelines = scalar keys %openlines;
if($sizepos != $sizelines) { die "DBM files broken $dbmFileName\n"; }
if($sizepos > 0) {
print STDERR ("Index file $dbmFileName.pos already exists; assuming valid\n");
return;
};
}
else
{
# just store a hash in memory.
$this->startpos({});
$this->lines({});
}
my $lastpos = 0;
my $pos = 0;
#my $seq = "";
my $id = "";
my $numlines = 0;
while(<$in>)
{
my $line = $_;
if($line =~ /^>$idregex/)
{
if(defined $id && $id ne "") {
# write the previous record
#print STDERR "$id -> $lastpos\n";
$this->startpos()->{$id} = $lastpos;
$this->lines()->{$id} = $numlines;
}
# start a new record
$id = $1;
#$seq = $line;
$lastpos = $pos;
$numlines = 0;
}
# else { $seq .= $line; }
$pos += length($line);
$numlines++;
}
# write the final record
$this->startpos()->{$id} = $lastpos;
$this->lines()->{$id} = $numlines;
#delete($this->startpos()->{""}); # spurious entries from the start of the loop
#delete($this->lines()->{""}); # spurious entries from the start of the loop
}
sub count_records()
{
my ($this) = @_;
return scalar(keys %{$this->startpos()});
}
sub fetch {
my ($this, $id) = @_;
my $pos = $this->startpos()->{$id};
if(! defined $pos) { die "no sequence with id: $id in " . $this->fastaFileName(); }
my $numlines = $this->lines()->{$id};
my $result = "";
my $in = $this->fh();
#print $this->fastaFileName() . ": " . $in . "\n";
seek($in,$pos,0);
for (1..$numlines) { $result .= <$in> }
return $result;
}
sub close {
my ($this) = @_;
my $in = $this->fh();
close($in);
$this->startpos(undef);
$this->lines(undef);
}
1;
|