#!/usr/bin/perl
package Expression_connection;

#####################################################################
# Author : Shuai Weng
# Date   : July 2001
# 
# See documentation for the usage details. 
#    
# http:///usr/local/dicty/www_dictybase/db/lib/html/dictyBase/programmer/Expression_connection.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 = (ec_no=>undef,
			name=>undef);

########################################################################
sub getTimePoints {
########################################################################
    my ($self) = @_;
    my $table = $self->schema.".ec_point";
    my $sth = $self->dbh->prepare("
         SELECT xaxis_point
         FROM   $table
         WHERE  ec_no = ?
         ORDER BY point_order
    ");
    $sth->execute($self->ec_no);
    my $timePoints;
    while(my($point) = $sth->fetchrow()) {
	$timePoints .= ", ".$point;
    }
    $timePoints =~ s/^, //;
    $sth->finish;
    return $timePoints;
}

####################################################################
sub deleteTimePoints {
####################################################################
    my ($self) = @_;
    my $table = $self->schema.".ec_point";
    my $sth = $self->dbh->prepare("
        DELETE from $table
        WHERE  ec_no = ?
    ");
    $sth->execute($self->ec_no);
    $sth->finish;
    $self->dbh->commit;
}

#####################################################################
sub GetECDetail {
#####################################################################
    my ($self, %args) = @_;
    my $dbh = $args{'dbh'} || die "A database handle must be passed into GetECDetail method";
    my $table = $self->schema.".expression_connection";
    my $sth = $dbh->prepare("
         SELECT ec_no, name, source, title, x_blocksize, y_blocksize, 
                xaxis_title, website_url, query_url, date_created, 
                created_by 
	 FROM   $table
         ORDER BY source, name
    ");
    $sth->execute;
    my $arrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $arrayRef;
}

####################################################################
sub GetReferences {
####################################################################
    my ($self, %args) = @_;
    my $dbh = $args{'dbh'} || die "A database handle must be passed into GetReferences method";
    my $table = $self->schema.".reflink";
    my $sth = $dbh->prepare("
         SELECT primary_key, reference_no
	 FROM   $table
         WHERE  tab_name = 'EXPRESSION_CONNECTION'
    ");
    $sth->execute;
    my $arrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $arrayRef;
}

####################################################################
sub InsertECPoints {
####################################################################
    my ($self, %args) = @_;
    my $dbh = $args{'dbh'} || die "A database handle must be passed into InsertECPoint method";
    my $table = $self->schema.".ec_point";
    my @timePoint = split(/,/, $args{'timePoints'});
    my $order;
    foreach my $point (@timePoint) {
	if ($point eq '') { next;}
	$order++;
	my $sth;
	if ($args{'ec_no'}) {
	    $sth = $dbh->prepare("
               INSERT INTO $table (ec_no, xaxis_point, point_order)
               VALUES(?,?,?)
            ");
	    $sth->execute($args{'ec_no'}, $point, $order);
	}
	else {
	    my $oracleSeqNm = $self->schema.".ecno_seq.currval";
	    $sth = $dbh->prepare("
               INSERT INTO $table (ec_no, xaxis_point, point_order)
               VALUES($oracleSeqNm,?,?)
            ");
	    $sth->execute($point, $order);
	}
	$sth->finish;
    }
}

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

}


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


=pod

=head1 Name

Expression_connection.pm

=head1 Description

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


=head1 Instantiating a New Expression_connection Object

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

my $ecObj = Expression_connection->new(dbh=>$dbh, $colNm=>$value); 

where $colNm is ec_no and $dbh is a valid database handle to either dictyBase or SDEV. $value must be a valid value for the column that was provided, otherwise the script will die, with an appropriate error message.
 

Syntax example :

my $ecObj = Expression_connection->new(dbh=>$dbh, ec_no=>$ecNo); 


=head1 Accessor Methods


All accessor methods take the form of : 


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


my $name = $ecObj->name; 


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


See valid columns in abstact table:

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


In addition, the following methods are provided to access other related info or update or delete ec related info in database. 


=head1 getTimePoints

Usage :

my $timePoints = $ecObj->getTimePoints;

This method returns ec_points (separated by ', ') for the specified ec_no.


=head1 deleteTimePoints

Usage :

$ecObj->deleteTimePoints;

This method deleted ec_points associated with the specified ec_no.

=head1 GetECDetail

Usage :

my $ecArrayRef = Expression_connection->GetECDetail(dbh=>$dbh);

foreach my $rowRef (@$ecArrayRef) {

    my ($ec_no, $name, $source, $title, $blocksize, $xaxis_title, 
        $website_url, $query_url, $date_created, $created_by) 
	= @$rowRef; 

    ....

}

This method returns an array ref for the data in the expression_connection table.

=head1 GetReferences

Usage :

my $refArrayRef = Expression_connection->GetReferences(dbh=>$dbh);

foreach my $rowRef (@$refArrayRef) {

    my ($refNo, $ec_no)= @$rowRef; 

    ....

}

This method returns an array ref for the reference info in reflink table.


=head1 InsertECPoints

Usage :

Expression_connection->InsertECPoints(dbh=>$dbh,
				      ec_no=>$ecNo,
				      timePoints=>$timePoints);


if no ec_no is passed to this method, it will insert CGM_DDB.ecno_seq.currval 
into ec_point table. This method is used to insert ec_point data into ec_point table.



=head1 getRow method

Usage:

my $row = $obj->getRow;

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











