#!/usr/bin/perl
package Gene_product_base;

#####################################################################
# Author : Shuai Weng
# Date   : July 2001
# 
# See documentation for the usage details. 
#    
# http:///usr/local/dicty/www_dictybase/db/lib/html/dictyBase/programmer/Gene_product.html
#
#####################################################################

use strict;
use DBI;
use Carp;
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 = (gene_product_no=>undef,
			gene_product=>undef);
       

#######################################################################
sub getLocusArrayRef {
#######################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT L.locus_no, L.locus_name
        FROM   $schema.locus L, $schema.locus_gp LGP
        WHERE  LGP.gene_product_no = ?
        AND    L.locus_no = LGP.locus_no
        ORDER BY upper(L.locus_name)
    ");
    $sth->execute($self->gene_product_no);
    my $arrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $arrayRef;
}

####################################################################
sub getLocusNoList {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT locus_no
        FROM   $schema.locus_gp
        WHERE  gene_product_no = ?
    ");
    $sth->execute($self->gene_product_no);
    my $locusNoList;
    while( my($locusNo) = $sth->fetchrow()) {
	if ($locusNoList) { $locusNoList .= ":"; }
	$locusNoList .= $locusNo;
    }
    return $locusNoList;
}

####################################################################
sub GetORFGeneArrayRefBYgeneProduct {
####################################################################
    my ($self, %args) = @_;
    my $dbh = $args{'dbh'} || die "A database handle must be passed to 'GetORFGeneArrayRefBYgeneProduct' method.";
    my $schema = $self->schema;
    my $sth = $dbh->prepare("
        SELECT F.feature_name, L.locus_name, GP.gene_product
        FROM   $schema.feature F, $schema.locus L, 
               $schema.gene_product GP, $schema.locus_gp LGP
        WHERE  upper(GP.gene_product) like ?
        AND    GP.gene_product_no = LGP.gene_product_no
        AND    LGP.locus_no = L.locus_no
        AND    L.locus_no = F.locus_no 
    ");
    $sth->execute(uc($args{'gene_product'}));
    my $arrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $arrayRef;
}

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

}


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

=pod

=head1 Name

Gene_product.pm

=head1 Description

This perl object (Gene_product.pm) acts as container for Gene_product 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 Gene_product Object

To instantiate a new Gene_product object, you may use one of the following syntaxes: 

my $Obj = Gene_product->new(dbh=>$dbh,
			    gene_product_no=>$gpNo);


OR


my $Obj = Gene_product->new(dbh=>$dbh, 
			    gene_product=>$gp);


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 $gp = $Obj->gene_product; 


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


See valid columns in gene_product table:

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

You can use following methods to access other related info.

=head2 getLocusArrayRef

Usage :

foreach my $rowRef (@{$Obj->getLocusArrayRef}) {

    my ($locusNo, $locusNm) = @$rowRef;

    ######

}


This method returns loci (locus_name and locus_no) associated with the specified gene_product. 

=head2 getLocusNoList

Usage :

my $locusNoList = $Obj->getLocusNoList;

This method returns colon (':') delimited locus_no list for the specified gene_product. 


=head2 getRow method

Usage:

my $row = $obj->getRow;

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

=head1 GetORFGeneArrayRefBYgeneProduct

Usage :

my $arrayRef = Gene_product->GetORFGeneArrayRefBYgeneProduct(dbh=>$dbh,
						     gene_product=>$gp);


foreach my $rowRef (@$arrayRef) {
    
    my ($featNm, $locusNm, $gp) = @$rowRef;

    ....

}

The method returns feature/gene names with their gene_products which match
$gp.


=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











