#!/usr/bin/perl -w
use strict;
use Getopt::Std;

###############################################################################
# Print usage message
sub usage {
    $0 =~ s/.*?([^\/]+)$/$1/;
    printf "usage: %s FILE\n", $0;
    printf "From RegulonDB, get pages of E-Coli genes listed in FILE\n";
    printf "\n";
    printf "  -h,? show this message\n";
    exit 1;
}

###############################################################################
# Main

my (@line, %opt);
my ($file, $type, $p, $r, $sum_p, $sum_r, $avg_p, $avg_r, %sum);
my ($n_files, $n_lines);

getopts "h?", \%opt;
usage if ($opt{"h"} || $opt{"?"} || @ARGV == 0);

my (@gene, $i);

$i = 0;
while (<>) {
    chomp $_;
    $gene[$i++] = $_;
}

my ($gene, $url, $procedure, $query, $pattern, $result);

$url="http://kinich.cifn.unam.mx:8850/";
$procedure="regulondb_queries.query_simple";
$query="?v_organism=ECK12&tabla=3&nombre=";
$pattern="v_gene_id=.+";

foreach $gene (@gene) {
    printf "$gene\t";
#    $result=`wwwgrab.pl \"$url$procedure$query$gene\" | grep -E \"$pattern>$gene</A>\" | uniq`;
    $result=`wwwgrab.pl \"$url$procedure$query$gene\" | grep -E \"$pattern\" | uniq`;
    $result =~ s/.*(ECK12[0-9]+).*/$1/;
    if ($result) {
	printf "$result";
    } else {
	printf "\n";
    }
}
