#!/usr/bin/perl -w
use strict;

if (!@ARGV or scalar @ARGV != 2) {
   print "USAGE: getSubSeq from,to seqFile.fa\n";
   exit 1;
}

my $pos = shift @ARGV;
my $seqFile = shift @ARGV;
my $s;
my $e;

if ($pos =~ /^(\d+),(\d+)$/) {
   $s = $1;
   $e = $2;
}else {
   die "ERROR bad position $pos, should be from,to\n";
}
if ($s > $e) { die "ERROR bad position $pos, from should be greater than to.\n"; }

open(FH, $seqFile) or die "Couldn't open $seqFile, $!\n";
my $i = 0;
my $seq;
while (<FH>) {
   chomp;
   if (/^>/) { next; } #fasta header
   #allow ambiguous codes
   if (!/^[ACTGNRWKSYactgnrwksy]+$/) { die "ERROR bad line in sequence file, $_\n"; }
   my $d = uc($_);
   $i += length $d;
   $seq .= $d;
   if ($e <= $i) { last; } #have enough
}
close(FH) or die "Couldn't close $seqFile, $!\n";

if ($e > $i) { die "ERROR range is outside of sequence $e > $i\n"; }
my @seq = split(/ */, $seq);
print ">getSubSeq $pos $seqFile\n";
for ($i=$s-1; $i<$e; $i++) { print $seq[$i]; }
print "\n";

exit 0;
