#!/usr/local/bin/perl -w

#
# gi2fas  - write fasta files for a list of gis
#           
# Author: Leonardo Marino-Ramirez <marino@tofu.tamu.edu>
# 
# Please cite the author in any work or product based on this material.
#          

use strict;
use Getopt::Std;
use File::Basename;

use vars qw($opt_d $opt_i @gis $gi $fasta $db $in $opt_h
	    );

## Check command line
my $prog = basename($0);
getopt('hdi');

if ($opt_h) {
    usage($prog); exit();
} if ($opt_i) {
    $in = "$opt_i";
} if ($opt_d) { 
    $db = "$opt_d";
} else {
    usage($prog); exit();
}

@gis = `cat $in`; 
chomp @gis;

foreach $gi (@gis) {
    $fasta = `fastacmd -d $db -s $gi`;
    open (FILE, "> ./$gi.aa") or die $!;
    print FILE "$fasta";
    close (FILE);
}

## Normal end
exit(0);

## Usage display
sub usage {
  my $p = shift;
  print STDERR <<USAGE
usage: $p [options] <file>

options [default]:
    -h           Usage display.
    -i <file>    Input file with List of gi's.
    -d           Fasta database to extract gi's from.
USAGE
}