#!/usr/bin/perl
package Two_point;

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

use strict;
use DBI;
use Carp;
use CGI qw/:all/;
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 = (two_point_no=>undef,
			two_point_name=>undef);


####################################################################
sub getLoci {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT  L.locus_name
        FROM    $schema.locus_twopt LT, $schema.locus L
        WHERE   LT.two_point_no = ? 
        AND     LT.locus_no = L.locus_no
    ");
    $sth->execute($self->two_point_no);
    my @loci;
    while (my($locus) = $sth->fetchrow()) {
       push(@loci, $locus);
    }
    return \@loci;
}


####################################################################
sub getReferenceInfo4twoPoint {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT R.reference_no, R.citation, R.year
        FROM   $schema.reference R, $schema.reflink RL
        WHERE  RL.tab_name = 'TWO_POINT'
        AND    RL.primary_key = ?
        AND    RL.reference_no = R.reference_no
        ORDER BY R.year
    ");
    $sth->execute($self->two_point_no);
    my $arrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $arrayRef;
}

####################################################################
sub GetTwoPointInfoArrayRefBYname {
####################################################################
    my ($self, %args) = @_;
    my $dbh = $args{'dbh'} || die "A database handle must be passed into 'GetTwoPointInfoArrayRefBYname' method.";
    my $schema = $self->schema;
    my $sth = $dbh->prepare("
        SELECT two_point_no, two_point_name, parental_ditype, 
               nonparental_ditype, tetratype, first_division, 
               second_division, distance, std_err, 
               interference, interference_std_err, remark
        FROM   $schema.two_point
        WHERE  two_point_name like ?
    ");
    $sth->execute($args{'twoPointNm'});
    my $arrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $arrayRef;
}


####################################################################
sub GetNotesBYloci {
####################################################################
    my ($self, %args) = @_;
    my $dbh = $args{'dbh'} || die "A database handle must be passed into 'GetNotesBYloci' method.";
    my $schema = $self->schema;
    my $sth = $dbh->prepare("
         SELECT  L.locus_name, CN.note 
         FROM    $schema.locus L, $schema.locus_CN LCN, 
                 $schema.curator_note CN
         WHERE   (upper(L.locus_name) = ? or 
                  upper(L.locus_name) = ? ) 
         AND     L.locus_no = LCN.locus_no 
         AND     LCN.curator_note_no = CN.curator_note_no 
         AND     CN.is_public = 'Y'  
    ");
    $sth->execute(uc($args{'locus1'}), uc($args{'locus2'}));
    my $notes;
    while(my ($locus, $note) = $sth->fetchrow()) {
	$locus = "\U$locus";
	$notes .= p.b($locus . " Notes:")." $note";
    }
    return $notes;
}


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

}


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

=pod

=head1 Name

Two_point.pm

=head1 Description

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

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

my $Obj = Two_point->new(dbh=>$dbh,
			 two_point_no=>$twoPtNo);


OR


my $Obj = Two_point->new(dbh=>$dbh, 
			 two_point_name=>$twoPtNm);


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 $dateCreated = $Obj->date_created; 


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

See valid columns in two_point table:

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

=head2 getLoci

Usage :

my $lociArrayRef = $Obj->getLoci;

foreach my $locusNm (@$lociArrayRef) {

    ####

}

This method returns loci info associated with the specified two_point data.

=head2 getReferenceInfo4twoPoint

Usage :

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

    my ($refNo, $citation, $year) = @$rowRef;

    #####

}

This method returns reference info for the specified two_point data.


=head2 GetTwoPointInfoArrayRefBYname

Usage :

my $arrayRef = Two_point->GetTwoPointInfoArrayRefBYname(dbh=>$dbh,
					   twoPointNm=$twoPointNm);



foreach my $rowRef (@$arrayRef) {

    my ($two_point_no, $two_point_name, $parental_ditype, 
        $nonparental_ditype, $tetratype, $first_division, 
        $second_division, $distance, $std_err, $interference, 
	$interference_std_err, $remark) = @$rowRef;

    #####

}


This method returns two point data info in which the two_point_name matches $twoPointNm.

=head2 GetNotesBYloci

Usage :

my $arrayRef = Two_point->GetNotesBYloci(dbh=>$dbh,
					 locus1=>$locus1,
					 locus2=>$locus2);


foreach my $rowRef (@$arrayRef) {

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

    #####

}

This method returns notes for the two_point data specified for $locus1 and $locus2.


=head2 getRow

Usage :

my $row = $Obj->getRow;

This method returns tab-delimited row entry for the specified two_point data.



=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











