#!/usr/bin/perl
package Homolog;

#####################################################################
# Author : Shuai Weng
# Date   : July 2003
# 
# 
#
#####################################################################

use strict;
use DBI;
use vars qw (@ISA %allowedConstructors);
use dictyBase_Table;
@ISA = qw (dictyBase_Table); # base class

# Class Globals

# put column names in the hash below, that are able to uniquely
# specify a row in the table

%allowedConstructors = (homolog_no=>undef,
			identifier=>undef);


####################################################################
sub GetIdentifiersForORF {
####################################################################
    my ($self, %args) = @_;

    my $dbh = $args{'dbh'} || $self->_err_report('dbh');
    my $orf = $args{'feature_name'} || $self->_err_report('feature_name');
    my $schema = $self->schema;

    my $sth = $dbh->prepare("
        SELECT unique H.identifier, H.taxon_id, T.tax_term
        FROM   $schema.homolog H, $schema.feature F, 
               $schema.homolog_alignment HA,
               $schema.taxonomy T
        WHERE  F.feature_name = ?
        AND    F.feature_no = HA.query_no
        AND    HA.target_no = H.homolog_no
        AND    H.taxon_id = T.taxon_id(+)
    ");

    $sth->execute(uc($orf));

    my $arrayRef = $sth->fetchall_arrayref();

    $sth->finish;

    return $arrayRef;

}

####################################################################
sub GetORFwithNRhomologArrayRef {
####################################################################
    my ($self, %args) = @_;

    my $dbh = $args{'dbh'} || $self->_err_report('dbh');
    my $schema = $self->schema;

    my $sth = $dbh->prepare("
        SELECT F.chromosome, F.feature_name, L.locus_name
        FROM   $schema.feature F, $schema.locus L 
        WHERE  F.locus_no = L.locus_no(+)
        AND    F.feature_no in 
               (select query_no from $schema.homolog_alignment)
        ORDER  by F.chromosome, F.feature_name 
    ");

    $sth->execute;

    my $arrayRef = $sth->fetchall_arrayref();

    $sth->finish;

    return $arrayRef;

}

######################################################################
sub GetHomologCountByOrganismForORF {
######################################################################
    my ($self, %args) = @_;

    my $dbh = $args{'dbh'} || $self->_err_report('dbh');
    my $orf = $args{'feature_name'} || $self->_err_report('feature_name');
    my $schema = $self->schema;

    my $sth = $dbh->prepare("
        SELECT count(*), T.tax_term
        FROM   $schema.homolog H, $schema.feature F,
               $schema.homolog_alignment HA, $schema.taxonomy T
        WHERE  F.feature_name = ?
        AND    F.feature_no = HA.query_no
        AND    HA.target_no = H.homolog_no
        AND    H.taxon_id = T.taxon_id
        GROUP BY T.tax_term
        ORDER BY T.tax_term
    ");

    $sth->execute(uc($orf));

    my $arrayRef = $sth->fetchall_arrayref();

    $sth->finish;

    return $arrayRef;

}

######################################################################
sub GetHomologCountForORF {
######################################################################
    my ($self, %args) = @_;

    my $dbh = $args{'dbh'} || $self->_err_report('dbh');
    my $orf = $args{'feature_name'} || $self->_err_report('feature_name');
    my $schema = $self->schema;

    my $sth = $dbh->prepare("
        SELECT count(H.identifier)
        FROM   $schema.homolog H, $schema.feature F,
               $schema.homolog_alignment HA
        WHERE  F.feature_name = ?
        AND    F.feature_no = HA.query_no
        AND    HA.target_no = H.homolog_no
    ");

    $sth->execute(uc($orf));

    my $count = $sth->fetchrow();

    $sth->finish;

    return $count;

}

######################################################################
sub _err_report {
######################################################################
    my ($self, $args) = @_;

    my ($file, $line, $method) = (caller(1))[1..3];

    print "The argument '$args' must be passed to '$method' method.",br;

    print "Please add this argument to line $line in $file.",br; 

    exit;

}

####################################################################
sub DESTROY{
####################################################################
# nothing needs to be done 

}


####################################################################
1; #################################################################
####################################################################

=pod

=head1 Name

Homolog.pm

=head1 Description

This perl object (Homolog.pm) acts as container for homolog info in oracle database. Once an object has been instantiated, several methods are available to retrieve the attributes of the object. 


=head1 Instantiating a New Homolog Object

To instantiate a new homolog object, you may use following syntax: 

my $obj = Homolog->new(dbh=>$dbh,
		       homolog_no=>$homologNo);


where $dbh is a valid database handle to either dictyBase or SDEV. All passed in values must be valid values for the columns that were provided, otherwise the script will die, with an appropriate error message.
 

=head1 Accessor Methods


All accessor methods take the form of : 


my $column_value = $obj->column_name, eg: 


my $length = $obj->length; 


my $taxonId = $obj->taxon_id;


etc. You can use an accessor for any valid column in the Homolog table. 

See valid columns in homolg table:

http:///usr/local/dicty/www_dictybase/db/lib/cgi-bin/dictyBase/tableSpecifications?table=HOMOLOG

=head1 getRow method

Usage:

my $row = $obj->getRow;

This method returns a tab-delimited row from homolog table.

=head1 Insert, update and delete Methods

You can also use 'Insert' class method and 'update' and 'delete' 
instance methods for inserting new row into database, updating and 
deleting info for a specified row. 

See dictyBase_Table documentation for usage details :

Update : http:///usr/local/dicty/www_dictybase/db/lib/staff/dictyBase/programmer/dictyBase_Table.html#Update_Method

Insert : http:///usr/local/dicty/www_dictybase/db/lib/staff/dictyBase/programmer/dictyBase_Table.html#Insert_Method

Delete : http:///usr/local/dicty/www_dictybase/db/lib/staff/dictyBase/programmer/dictyBase_Table.html#Delete_Method

=head1 Author

Shuai Weng

shuai@genome.stanford.edu

=cut











