#!/usr/bin/perl
package Ypd_protein_info;

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

use strict;
use DBI;
use Carp;
use vars qw (@ISA %allowedConstructors);
use dictyBase_Table;
use dictyBaseid;
@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 = (ypd_protein_info_no=>undef,
			ypdid=>undef,
			gene_name=>undef,
			dictyBaseid=>undef);


####################################################################
sub ypdPaperNum {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT count(unique RL.reference_no)
        FROM   $schema.reflink RL, $schema.ypd_protein_info Y
        WHERE  Y.dictyBaseid = ?
        AND    RL.tab_name = 'YPD_PROTEIN_INFO'
        AND    RL.primary_key = Y.ypd_protein_info_no
    ");
    $sth->execute($self->dictyBaseid);
    my $count = $sth->fetchrow;
#    return $count;

    my $dictyBaseidObj = dictyBaseid->new(dbh=>$self->dbh,
			      dictyBaseid=>$self->dictyBaseid);
    my ($featNo, $locusNo);
    if ($dictyBaseidObj) {
	$featNo = $dictyBaseidObj->feature_no;
	$locusNo = $dictyBaseidObj->locus_no;
    }
    if (!$featNo && !$locusNo) { 
	print "The dictyBaseid = ".$self->dictyBaseid." is not associated with a feature/locus.";
	return; 
    }
    my @whereValues;
    push(@whereValues, $self->dictyBaseid);

    ### YPD_PROTEIN_INFO
    my $whereClause4refunlink = 
	"where (tab_name = 'YPD_PROTEIN_INFO' and primary_key = ?)";
    push(@whereValues, $self->ypd_protein_info_no);
    ### LOCUS
    if ($locusNo) {
	$whereClause4refunlink .= 
	    " or (tab_name = 'LOCUS' and primary_key = ?)";
	push(@whereValues, $locusNo);
    }
    ### FEATURE
    if ($featNo) {
	$whereClause4refunlink .= 
	    "or (tab_name = 'FEATURE' and primary_key = ?)";
	push(@whereValues, $featNo);
    }
    
    my $union;
    if ($locusNo) {
	$union = "select unique reference_no from $schema.locus_gene_info where locus_no = ?";
	push(@whereValues, $locusNo);
    }
    if ($featNo) {
	if ($union) { $union .= " UNION "; }
	$union .= "select unique reference_no from $schema.feat_gene_info where feature_no = ?";
	push(@whereValues, $featNo);
    }
    my $andClause = "R.pubmed in  
                     (select unique pubmed
                      from   $schema.ref_unlink 
                      $whereClause4refunlink
                     ) ";
    if ($union) { 
	$andClause = "AND  (  $andClause 
                              or 
                              R.reference_no in ($union)
                            )"; 
    }
    else { $andClause = "AND ".$andClause; }

    my $sth = $self->dbh->prepare("
        SELECT count(unique RL.reference_no)
        FROM   $schema.reflink RL, $schema.ypd_protein_info Y, 
               $schema.reference R
        WHERE  Y.dictyBaseid = ?
        AND    RL.tab_name = 'YPD_PROTEIN_INFO'
        AND    RL.primary_key = Y.ypd_protein_info_no
        AND    RL.reference_no = R.reference_no
        $andClause
    "); 
    $sth->execute(@whereValues);
   
    $count -= $sth->fetchrow;
    $sth->finish;
    return $count;
}

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

}


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

=pod

=head1 Name

Ypd_protein_info.pm

=head1 Description

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

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

my $obj = Ypd_protein_info->new(dbh=>$dbh,
				ypd_protein_info_no=>$ypdNo);


OR


my $obj = Ypd_protein_info->new(dbh=>$dbh,
				ypdid=>$ypdid);


OR


my $obj = Ypd_protein_info->new(dbh=>$dbh,
				gene_name=>$geneNm);


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 $chr = $obj->chromosome; 

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

See valid columns in Ypd_protein_info table:

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

=head1 getRow method

Usage:

my $row = $obj->getRow;

This method returns a tab-delimited row from Ypd_protein_info 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











