#!/usr/bin/perl -w

# $Id: acid_build_checker.pl,v 1.1 2004/11/15 14:23:01 jari Exp $

use strict;

use AcidConfig;
use AcidDB;
use ResultSet;

use LWP::Simple;
use URI::URL;



sub acid_unigene_build($) {
    my $speciesid = $_[0];
    my $build='none';
    my $acidDB = AcidDB->new();
    $acidDB->connect(connect  => AcidConfig->acid_connection(),
		     id       => AcidConfig->acid_userid(),
		     password => AcidConfig->acid_userpassword() );

    my $ResultSet = $acidDB->query("SELECT id, unigene_build FROM species");
    while(my $row = $ResultSet->Next()) {
	if($row->{'id'} eq $speciesid) {
	    $build=$row->{'unigene_build'};
	}	
    }
    return $build;
}



sub current_unigene_build($) {
    my $speciesid = $_[0];
    my $build='none';

    # get the page
    my $unigene_page="ftp://ftp.ncbi.nih.gov/repository/UniGene/$speciesid.info";
    my $url=
        url("$unigene_page");

    my $content;
    unless (defined ($content=get($url))) {
        print STDERR "Could not get URL: $url";
    }

    # parse the page
    my @lines=split(/\n/,$content);
    my @foo = split(/\s+/,$lines[0]);
    $build = $foo[2];
    @foo = split(/\#/,$build);
    $build=$foo[1];
    return $build;
}



sub acid_ucsc_assembly($) {
    my $speciesid = $_[0];
    my $build='none';
    my $acidDB = AcidDB->new();
    $acidDB->connect(connect  => AcidConfig->acid_connection(),
		     id       => AcidConfig->acid_userid(),
		     password => AcidConfig->acid_userpassword() );

    my $ResultSet = $acidDB->query("SELECT id, genomedb FROM species");
    while(my $row = $ResultSet->Next()) {
	if($row->{'id'} eq $speciesid) {
	    $build=$row->{'genomedb'};
	}	
    }
    return $build;
}



sub current_ucsc_assembly($) {
    my $speciesid = species2species($_[0]);
    
    my $ucsc_page= "http://genome.ucsc.edu/cgi-bin/hgGateway?org=$speciesid";
    my $url=
        url("$ucsc_page");
    
    my $content;
    unless (defined ($content=get($url))) {
        print STDERR "Could not get URL: $url";
    }

    # parse the page
    my @lines=split(/\n/,$content);
    my $version='none';
    foreach my $line (@lines) {
	if($line =~ /\(([a-zA-Z]+[0-9]+)\) assembly/) {
	    $version = $1;	    
	}
    }
    return $version;
}



sub current_local_assembly($) {
    my $buildlabel = species2genome($_[0]);
    
    my $ucsc_page= "http://bioinfo.thep.lu.se/genomebrowser.html";
    my $url=
        url("$ucsc_page"); 
    
    my $content;
    unless (defined ($content=get($url))) {
        print STDERR "Could not get URL: $url";
    }

    # parse the page
    my @lines=split(/\n/,$content);
    my $version='none';
    foreach my $line (@lines) {
	if($line =~ /($buildlabel[0-9]+)/) {
	    $version = $1;	    
	}
    }
    return $version;
}



sub species2species($) {
    my $speciesid='EMPTY';
    if($_[0] eq 'Hs') {
	$speciesid='Human';
    }
    elsif($_[0] eq 'Mm') {
	$speciesid='Mouse';
    }
    elsif($_[0] eq 'Rn') {
	$speciesid='Rat';
    }
    else {
	print STDERR "Error in acid_build_checker.pl: No support for a mapping to UCSC species name from species id $_[0]!\n";
    }
    return $speciesid;
}



sub species2genome($) {
    my $speciesid='EMPTY';
    if($_[0] eq 'Hs') {
	$speciesid='hg';
    }
    elsif($_[0] eq 'Mm') {
	$speciesid='mm';
    }
    elsif($_[0] eq 'Rn') {
	$speciesid='rn';
    }
    else {
	print STDERR "Error in acid_build_checker.pl: No support for a mapping to UCSC genome label from species id $_[0]!\n";
    }
    return $speciesid;
}



sub species_in_acid() {
    my @species;
    my $acidDB = AcidDB->new();
    $acidDB->connect(connect  => AcidConfig->acid_connection(),
		     id       => AcidConfig->acid_userid(),
		     password => AcidConfig->acid_userpassword() );

    my $ResultSet = $acidDB->query("SELECT id, unigene_build FROM species");
    while(my $row = $ResultSet->Next()) {
	push(@species,$row->{'id'});
    }
    return @species;
}



# The main program!!!!
my @species = species_in_acid();
my $updates = "Updates:\n";
print STDOUT "Results from running acid_build_checker.pl !\n\n";
print STDOUT "Versions:\n";
foreach my $id (@species) {
    my $acid_build = acid_unigene_build($id);
    my $unigene_build = current_unigene_build($id);
    my $ucsc_version=current_ucsc_assembly($id);
    my $acid_ucsc_version=acid_ucsc_assembly($id);
    my $local_ucsc_version=current_local_assembly($id);

    print STDOUT "$id build:\t $acid_build in ACID and $unigene_build in UniGene\n";
    print STDOUT "$id UCSC version: $local_ucsc_version locally, $acid_ucsc_version in ACID, and $ucsc_version at UCSC\n";
    if($acid_build ne $unigene_build) {
	$updates .= "Update $id UniGene!\n";
    }
    if($local_ucsc_version ne $ucsc_version || 
       $acid_ucsc_version ne $ucsc_version ) {
	$updates .= "Update $id UCSC genome assembly!\n";
    }

}

if($updates eq "Updates:\n") {
    $updates .= "Nothing to be done!\n";
}
print STDOUT "\n$updates";


