#!/usr/bin/perl

use strict;
use MaXMLParser;

if (scalar(@ARGV) < 1) {
    print STDERR "Usage: $0 [-t idtype] ID1 ID2 ...\n";
    print STDERR " Get riken_def from the FANTOM WWW site\n";
    print STDERR "  -t idtype: type of IDs in arguments (default cloneid)\n";
    print STDERR "             cloneid, seqid, accession, ...\n";
    exit 1;
}

my @ids = ();
my $idtype = undef;
while (@ARGV) {
    my $id = shift(@ARGV);
    if ($id eq '-t') {
	$idtype = shift(@ARGV);
    } else {
	push(@ids, $id);
    }
}

foreach my $id (@ids) {
    my $results = MaXMLParser->get_entries([$id], ['gene_name'], $idtype);

    if (defined $results) {
	foreach my $result (@$results) {
	    my $cloneid = $result->{cloneid};
	    foreach my $anndata (@{$result->{anndata}}) {
		my $qualifier = $anndata->{qualifier};
		next if ($qualifier ne 'gene_name');
		my $anntext   = $anndata->{anntext};
		my $datasrc   = $anndata->{datasrc};
		my $srckey    = $anndata->{srckey};
		my $evidence  = $anndata->{evidence};
		print "$id\t$cloneid\t$anntext";
		print "($datasrc/$srckey)" if (($datasrc ne '')||($srckey ne ''));
		print " ($evidence)", "\n";
	    }
	}
    } else {
	print "$id not found\n";
    }
}
