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

# gi2swissprot
# Prints the spid if available from 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($fasta
	    @tmp1
	    $spidl
	    @tmp4
	    $spid
	    $opt_i
	    $opt_h
	    $gi_list
	    );

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

getopt('hi');

if ($opt_h) {
    usage($prog); exit();
} elsif ($opt_i) {
    $gi_list = $opt_i;
} else {
    usage($prog); exit();
}

my @gis = `/bin/cat $gi_list`; chomp @gis;

foreach my $gi (@gis) {	
    $fasta = `fastacmd -d nr -s $gi`;
    @tmp1 = split /sp\|/, $fasta;
    $spidl  = $tmp1[1];
    @tmp4 = split (/\|/, $spidl);
    $spid = $tmp4[0];
    
    print "$gi\t$spid\n";
}

## 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.
USAGE
}