#!/usr/local/bin/perl

package InMemoryFastaAccess;

use warnings;
use strict;
use diagnostics;

#Stores filename info
my $fastaFile;

#Stores hash of Fasta file
my %fasta;

#constructor usage: new InMemoryFastaAccess($fastaFileName)
sub new {
	my $class = shift;
	$fastaFile = shift;

	makeHash();

	my $self = {};
	bless $self, $class;
	return $self;
}

sub makeHash {
	open( FILE, $fastaFile );
	my $line = <FILE>;
	my $fastaID = "";
	while ($line) {
		chomp($line);
		if ( substr( $line, 0, 1 ) eq '>' ) {
			my @tempStringArray = split( / /, $line );
			$fastaID =
			  substr( $tempStringArray[0], 1, length( $tempStringArray[0] )-1 );
		}
		my $fastaString = "";
		$line = <FILE>;
		chomp($line);
		while ( $line && !( substr( $line, 0, 1 ) eq '>' ) ) {

			chomp($line);
			$fastaString = $fastaString . $line;
			$line        = <FILE>;
		}
		$fasta{$fastaID} = $fastaString;
	}
}

#usage subseq($ID,$start,$endPos)
sub subseq {
	my $self  = shift;
	my $ID    = shift;
	my $start = shift;
	my $end   = shift;
	my $sequence = substr( $fasta{$ID}, $start - 1, $end - $start+1 );
	return $sequence;
}

1;

