#!/usr/bin/perl

###############################################################
#  programmer:		bradley e. slaven
#
#  description:		see help area below.
#
#
################################################################

use Getopt::Long;
use Carp qw(carp croak);
use strict;

local *TRIM_LOC;
local *OUT_FILE;

my %TRIM_HASH_BEG = ();
my %TRIM_HASH_LEN = ();
my %SEQ_HASH = ();
my @NAME_ORDER = ();

my $opt_h;
my $opt_f;
my $opt_t;

MAIN:
{  #beg Main

Getopt::Long::config('no_ignore_case');
use Carp qw(carp croak);
GetOptions('h!'		=> \$opt_h,
	   'help!'	=> \$opt_h,
	   'f=s'	=> \$opt_f,
	   't=s'	=> \$opt_t,
	   );

$opt_h and usage('help');
$opt_f or usage();
$opt_t or usage();

my $fFile = $opt_f;
my $tFile = $opt_t;
my $outFile = "$fFile" . "\.TRIM";

print "FASTA FILENAME = $fFile\n";
print "TRIM QUALITY FILENAME = $tFile\n";
print "NEW FASTA FILENAME = $outFile\n";

open (OUT_FILE, ">$outFile") or croak "Can Not open $outFile: $!\n";

createBegLenLocationHashs($tFile);
#printBegLenLocationHashs();
readFastaFilename($fFile);
trimReads();

close(TRIM_LOC);
close(OUT_FILE);

}  #end Main




sub printBegLenLocationHashs() {

	my $key = "";

	foreach $key (keys %TRIM_HASH_BEG) {
		print ("$key BEG=$TRIM_HASH_BEG{$key}\tEND=$TRIM_HASH_LEN{$key}\n");
	}

} #end printBegLocLocationHashs




sub createBegLenLocationHashs() {

	my $fname = $_[0];
	open (TRIM_LOC, "$fname") or croak "Can Not open $fname: $!\n";
	
	while (<TRIM_LOC>) {

		chomp;

		my $line = $_;
		my @lineArray = split(/\s+/, $line);

		my $name = $lineArray[0];
		my $status = $lineArray[1];
		my $beg = $lineArray[2];
		my $len = $lineArray[3];

		#print("loading $name with beg=$beg\tand\tlen=$len\n");

		if ($status =~ /ACCEPT/) {
			$TRIM_HASH_BEG{"$name"} = "$beg";
			$TRIM_HASH_LEN{"$name"} = "$len";
		} #end if

	} #end while

	close(TRIM_LOC);

} #end createBegLenLocationHashs




sub readFastaFilename() {

	my $fname = $_[0];
	open (IN_FILE, $fname) or croak "Can Not open $fname: $!\n";

	my $seq = "";
	my $line = "";
	my $name = "";
	my $numSeqs = 0;

	while (<IN_FILE>) {

		chomp;
		my $line = $_;

		if ($line =~ /^>/) {
			if ($numSeqs > 0) {
				$SEQ_HASH{"$name"}= $seq;
				my $index = $numSeqs - 1;
				$NAME_ORDER[$index] = "$name";
				$seq = "";
			} #end if numSeqs
			$name = $line;
			$name =~ s/^>//;
			$name =~ s/\s.*//;
			#print("name=$name\n");
			++$numSeqs;
		}	
		else {
			my $len = length($line);
                       	if ($len > 0) {
                               	$seq = "$seq" . "$line";
                       	} #end len GT 0
		} #end else - numSeq eq 0 

	} #end IN_FILE while

	close (IN_FILE);

} #end readFastaFilename


sub trimReads() {
	my $numReads = $#NAME_ORDER;

	for (my $i = 0; $i <= $numReads; $i++) {
		my $orderedName = $NAME_ORDER[$i];
		my $beg = "";
		my $len = "";

		$beg = $TRIM_HASH_BEG{"$orderedName"};
		$len = $TRIM_HASH_LEN{"$orderedName"};

		if ($beg != "") {
			my $seq = "";
			$seq = $SEQ_HASH{"$orderedName"};
			printTrimmedFastaSequence($orderedName, $beg, $len, $seq);
		}
		
	} #end for
} #end trimReads


sub printTrimmedFastaSequence() {

	my $name = $_[0];
	my $beg  = $_[1];
	my $len  = $_[2];
	my $seq  = $_[3];

	my $tseq = substr($seq, $beg, $len);
	
	print (">$name\n");
	print OUT_FILE (">$name\n");

	while (length($tseq) > 0) {
		my $line = "";
		$line = substr($tseq, 0, 50);
		print ("$line\n");
		print OUT_FILE ("$line\n");
		$tseq = substr($tseq, 50, 10000);
	} #end while

} #end printTrimmedFastaSequence




sub usage {
my $help = shift || 0;
print "Usage: $0 [-options]\n";
print "-h help\n";
print "-f fasta file\n";
print "-t trimed quality score location file\n";
help() if ($help);
exit;
}



sub help {
print qq~
===============================================================
$0: DOCUMENTATION
---------------------------------------------------------------
This program reads the trimed quality score location file
(created by qualityScoreBasedTrim.pl) and the fasta sequence
read file previously associated with the trimed quailty scores
file and extracts out the fasta seqs that match the trimed 
quality file created by the qualityScoreBasedTrim.pl program.

usage example:

	$0 -f fastaFilename -t fastaFilename.TRIM_LOC

~
}
