#!/usr/bin/perl
package Subfeature_base;

#####################################################################
# Author : Mira Kaloper 
# Date   : April 22nd 2002
#
# Usage: 
#     
#   use Subfeature;
#
#   ### You may use one of the following syntaxes to 
#   ### instantiate a new Subfeature object.
#
#   our $subfeatureObj = Subfeature->new(dbh=>$dbh,
#                                       subfeature_no=>$subfeatureNo);
#       
#
#   ### You can use an accessor for any valid column 
#   ### in the Subfeature table. For example: 
#   our $subfeaturei_type = $subfeatureObj->subfeature_type;
#
#   See documentation for detail.
#   
#   http:///usr/local/dicty/www_dictybase/db/lib/html/dictyBase/programmer/Subfeature.html
#
#####################################################################
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 = (subfeature_no=>undef);


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

}



####################################################################
sub GetStartStopTypeHash {
####################################################################
    my ($self, %args) = @_;
    my $dbh = $args{'dbh'} || "A database handle must be passed into 'GetStartStopType' method.";

    my $feature_no = $args{'feature_no'} || "feature_no must be passed into 'GetStartStopType' method.";

    my $schema = $self->schema;

    my $sth = $dbh->prepare("SELECT s.subfeature_no,
                                      start_coord, stop_coord, 
                                      subfeature_type 
                                    FROM $schema.subfeature s, 
                                         $schema.subfeature_type st
                                    WHERE feature_no = ?
                                    AND s.subfeature_no = st.subfeature_no
                                    AND s.subfeature_no NOT IN
                                        (SELECT subfeature_no FROM 
                                           $schema.subfeature_type  
                                         WHERE subfeature_type LIKE 'Deleted%') 
                                    ");

    $sth->execute($feature_no);

    my %hash;

    while (my ($subfeature_no, $start, $stop, $feature_type) = 
               $sth->fetchrow_array()){
      $hash{$start} = "$subfeature_no|$stop|$feature_type";

    }

    $sth->finish;
    return %hash;
 
}


####################################################################
sub GetSubFeatureNoHash {
####################################################################
    my ($self, %args) = @_;
    my $dbh = $args{'dbh'} || "A database handle must be passed into 'GetSubFeatureNoHash' method.";

    my $feature_no = $args{'feature_no'} || "feature_no must be passed into 'GetSubFeatureNoHash' method.";

    my $schema = $self->schema;

    my $sth = $dbh->prepare("SELECT s.subfeature_no,
                                      subfeature_type
                                    FROM $schema.subfeature s,
                                         $schema.subfeature_type st
                                    WHERE feature_no = ?
                                    AND s.subfeature_no = st.subfeature_no
                                    AND s.subfeature_no NOT IN 
                                        (SELECT subfeature_no FROM 
                                           $schema.subfeature_type  
                                         WHERE subfeature_type LIKE 'Deleted%')
                                    ");

    $sth->execute($feature_no);

    my %hash;

    while (my ($subfeature_no, $subfeature_type) = $sth->fetchrow_array()){
      $hash{$subfeature_no} = $subfeature_type;

    }

    $sth->finish;
    return %hash;

}


####################################################################
sub GetStartStopArray {
####################################################################
    my ($self, %args) = @_;
    my $dbh = $args{'dbh'} || "A database handle must be passed into 'GetStartStop' method.";

    my $feature_no = $args{'feature_no'} || "feature_no must be passed into 'GetStartStopType' method.";

    my $schema = $self->schema;

    my $sth = $dbh->prepare("SELECT s.subfeature_no,
                                      start_coord, stop_coord,
                                      subfeature_type 
                                    FROM $schema.subfeature s,
                                         $schema.subfeature_type st
                                    WHERE feature_no = ?
                                    AND s.subfeature_no = st.subfeature_no
                                    AND s.subfeature_no NOT IN 
                                        (SELECT subfeature_no FROM 
                                           $schema.subfeature_type  
                                         WHERE subfeature_type LIKE 'Deleted%')
                                    ORDER BY start_coord");

    $sth->execute($feature_no);

    my @array;

    while (my ($subfeature_no, $start, $stop, $feature_type) =
               $sth->fetchrow_array()){
      push(@array,"$start|$stop|$feature_type|$subfeature_no");

    }

    $sth->finish;
    return @array;

}



####################################################################
sub GetMaxStartStopExon {
####################################################################
    my ($self, %args) = @_;
    my $dbh = $args{'dbh'} || "A database handle must be passed into 'GetMaxStartStopExon' method.";

    my $feature_no = $args{'feature_no'} || "feature_no must be passed into 'Get
StartStopType' method.";

    my $schema = $self->schema;

    my $sth = $dbh->prepare("SELECT s.subfeature_no,
                                      start_coord, stop_coord
                                    FROM $schema.subfeature s,
                                         $schema.subfeature_type st
                                    WHERE feature_no = ?
                                    AND s.subfeature_no = st.subfeature_no
                                    AND s.subfeature_no NOT IN 
                                        (SELECT subfeature_no FROM 
                                           $schema.subfeature_type  
                                         WHERE subfeature_type LIKE 'Deleted%')
                                    ORDER BY start_coord DESC
                                    ");

    $sth->execute($feature_no);

    my ($subfeature_no, $start, $stop) = $sth->fetchrow_array();

    $sth->finish;

    return ($subfeature_no, $start, $stop);

}

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


=pod

=head1 Name

Subfeature.pm


=head1 Description

This perl object (Subfeature.pm) acts as container for Subfeature info associated with feature_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 Subfeature Object

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

my $subfeatureObj = Subfeature->new(dbh=>$dbh, $colNm=>$value); 

where $colNm is subfeature_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 $subfeatureObj = Subfeature->new(dbh=>$dbh, subfeature_no=>$subfeatureNo); 


=head1 Accessor Methods


All accessor methods take the form of : 

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

my $subfeature_type = $subfeatureObj->subfeature_type; 


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

See valid columns in alias table:

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


=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

Mira Kaloper

shuai@genome.stanford.edu

=cut











