#!/usr/bin/perl
package Locus_base;
#####################################################################
# Author : Shuai Weng  
# Date   : July 2001
#
# Usage: 
#     
#   use Locus;
#
#   ### You may use one of the following syntaxes to 
#   ### instantiate a new Locus object.
#
#   our $locusObj = Locus->new(dbh=>$dbh,
#                             locus_name=>$locusName);
#       
#   our $locusObj = Locus->new(dbh=>$dbh,
#                             locus_no=>$locusNo);
#               
#
#   ### You can use an accessor for any valid column 
#   ### in the Locus table. For example: 
#   our $locus_name = $locusObj->locus_name;
#   our $chromosome = $locusObj->chromosome;
#   our $genetic_position = $locusObj->genetic_position;
#   our $desc = $locusObj->description;
#   .....
#
#   ### The object also provides other accessor methods to 
#   ### access no-locus columns. For example:
#   our $featureNameList = $locusObj->featureNameList;
#   ....
#   See documentation for detail.
#   
#   http:///usr/local/dicty/www_dictybase/db/lib/html/dictyBase/programmer/Locus.html
#
#####################################################################
use strict;
use DBI;
use Carp;
use vars qw (@ISA %allowedConstructors);
use dictyBase_Table;
use Feature;

@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 = (locus_name=>undef,
	                locus_no=>undef);


####################################################################
sub delete{
####################################################################

    my ($self) = @_;
 
    if ($self->featureNameList) {
	my @feature = split(/\|/, $self->featureNameList);
	foreach my $featureName (@feature) {
	    my $featureObj = Feature->new(dbh=>$self->dbh,
					  feature_name=>$featureName);
	    $featureObj->updateLocus_no("");
	    $featureObj->enterUpdates;
	}
    }
    $self->SUPER::delete();
}

####################################################################
sub featureNameList {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT feature_name 
        FROM   $schema.feature
        WHERE  locus_no = ?
    ");
    $sth->execute($self->locus_no);
    my $featList;
    while (my($feat) = $sth->fetchrow()) {
	if ($featList) { $featList .= "|"; }
	$featList .= $feat;
    }
    $sth->finish;
    return $featList;
}

####################################################################
sub aliasNameList {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT A.alias_name 
        FROM   $schema.alias A, $schema.locus_alias LA
        WHERE  LA.locus_no = ?
        AND    LA.alias_no = A.alias_no        
    ");
    $sth->execute($self->locus_no);
    my $aliasList;
    while (my($alias) = $sth->fetchrow()) {
	if ($aliasList) { $aliasList .= "|"; }
	$aliasList .= $alias;
    }
    $sth->finish;
    return $aliasList;
}


####################################################################
sub uniformAliasNameList {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT A.alias_name 
        FROM   $schema.alias A, $schema.locus_alias LA
        WHERE  LA.locus_no = ?
        AND    LA.alias_no = A.alias_no 
        AND    A.alias_type = 'Uniform'
    ");
    $sth->execute($self->locus_no);
    my $aliasList;
    while (my($alias) = $sth->fetchrow()) {
	if ($aliasList) { $aliasList .= "|"; }
	$aliasList .= $alias;
    }
    $sth->finish;
    return $aliasList;
}


####################################################################
sub nonUniformAliasNameList {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT A.alias_name 
        FROM   $schema.alias A, $schema.locus_alias LA
        WHERE  LA.locus_no = ?
        AND    LA.alias_no = A.alias_no 
        AND    A.alias_type = 'Non-uniform'
    ");
    $sth->execute($self->locus_no);
    my $aliasList;
    while (my($alias) = $sth->fetchrow()) {
	if ($aliasList) { $aliasList .= "|"; }
	$aliasList .= $alias;
    }
    $sth->finish;
    return $aliasList;
}


####################################################################
sub proteinNameAliasNameList {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT A.alias_name 
        FROM   $schema.alias A, $schema.locus_alias LA
        WHERE  LA.locus_no = ?
        AND    LA.alias_no = A.alias_no 
        AND    A.alias_type = 'Protein name'
    ");
    $sth->execute($self->locus_no);
    my $aliasList;
    while (my($alias) = $sth->fetchrow()) {
	if ($aliasList) { $aliasList .= "|"; }
	$aliasList .= $alias;
    }
    $sth->finish;
    return $aliasList;
}

####################################################################
sub dictyBaseid {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT dictyBaseid
        FROM   $schema.dictyBaseid
        WHERE  primary_key = ?
        AND    tab_name = 'LOCUS'
        AND    dictyBaseid_type = 'Primary'
    ");
    $sth->execute($self->locus_no);
    my $dictyBaseid = $sth->fetchrow;
    $sth->finish;
    return $dictyBaseid;
}

####################################################################
sub dictyBaseidList {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    if (!$self->featureNameList) {
	return $self->dictyBaseid;
    }
    my $sth = $self->dbh->prepare("
        SELECT S.dictyBaseid
        FROM   $schema.dictyBaseid S, $schema.feature F  
        WHERE  F.locus_no = ?
        AND    F.feature_no = S.primary_key
        AND    S.tab_name = 'FEATURE'
        AND    S.dictyBaseid_type = 'Primary'
    ");
    $sth->execute($self->locus_no);
    my $dictyBaseidList;
    while (my($dictyBaseid) = $sth->fetchrow()) {
	if ($dictyBaseidList) { $dictyBaseidList .= "|"; }
	$dictyBaseidList .= $dictyBaseid;
    }
    $sth->finish;
    return $dictyBaseidList;
}

####################################################################
sub gene_product {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT GP.gene_product
        FROM   $schema.gene_product GP, $schema.locus_gp LGP
        WHERE  LGP.locus_no = ?
        AND    LGP.gene_product_no = GP.gene_product_no
    ");
    $sth->execute($self->locus_no);
    my $gpList;
    while(my ($gp) = $sth->fetchrow()) {
	if ($gpList) { $gpList .= "\t"; }
        $gpList .= $gp;
    }
    $sth->finish;
    return $gpList;
}

####################################################################
sub freeTextPhenotype {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT P.phenotype
        FROM   $schema.phenotype P, $schema.locus_pheno LP
        WHERE  LP.locus_no = ?
        AND    LP.phenotype_type = 'Free text'
        AND    LP.phenotype_no = P.phenotype_no
    ");
    $sth->execute($self->locus_no);

    my $freeTextPheno = $sth->fetchrow();

    $sth->finish;

    return $freeTextPheno;
}

####################################################################
# sub sacchdb_phenotype {
####################################################################
#    my ($self) = @_;
#    my $schema = $self->schema;
#    my $sth = $self->dbh->prepare("
#        SELECT sacchdb_phenotype
#        FROM   $schema.sacchdb_phenotype
#        WHERE  locus_no = ?
#    ");
#    $sth->execute($self->locus_no);
#    my $sachdbPheno;
#    while(my ($pheno) = $sth->fetchrow()) {
#	if ($sachdbPheno) { $sachdbPheno .= "; "; }
#	$sachdbPheno .= $pheno;
#    }
#    $sth->finish;
#    return $sachdbPheno;
# }

#######################################################################
sub geneReservationInfo {
#######################################################################
    my($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT reservation_no, reservation_date, expiration_date, 
               date_standardized, is_standardized
        FROM   $schema.gene_reservation
        WHERE  locus_no = ?
    ");
    $sth->execute($self->locus_no);
    my($Rnum, $Rdate, $Edate, $Sdate, $isStd) = $sth->fetchrow();
    $sth->finish;
    if ($Rnum) {
	return $Rnum.":".$Rdate.":".$Edate.":".$Sdate.":".$isStd;
    }
    return ;
}

#######################################################################
sub colleagueNmNo {
#######################################################################
    my($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
         SELECT C.colleague_no, C.last_name, C.first_name
         FROM   $schema.colleague C, $schema.coll_locus CL
         WHERE  CL.locus_no = ? and
                CL.colleague_no = C.colleague_no
    ");
    $sth->execute($self->locus_no);
    my $collInfo;
    while(my($collNo, $lname, $fname) = $sth->fetchrow()) {
	$collInfo .= "\t"."$fname $lname"."::".$collNo;
    }
    $collInfo =~ s/^\t//;
    $sth->finish;
    return $collInfo;
}

#######################################################################
sub phenotypeInfoArrayRef {
#######################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->{'_dbh'}->prepare("
        SELECT P.phenotype_no, P.phenotype, LP.phenotype_type, 
               LP.sentence
        FROM   $schema.phenotype P, $schema.locus_pheno LP
        WHERE  P.phenotype_no = LP.phenotype_no
        AND    LP.locus_no = ?
    ");  
    $sth->execute($self->locus_no);
    my $locusPhenoArrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $locusPhenoArrayRef;
}

####################################################################
sub goidArrayRef {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT unique goid
        FROM   $schema.go_locus_goev
        WHERE  locus_no = ?
    ");
    $sth->execute($self->locus_no);
    my $arrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $arrayRef;
}


####################################################################
sub goInfoForTreeArrayRef {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT unique G.goid, go_term, go_aspect, go_definition
        FROM   $schema.go G, $schema.go_locus_goev GLG
        WHERE  GLG.locus_no = ?
        AND    GLG.goid = G.goid
        AND    GLG.is_not = 'N'
    ");
    $sth->execute($self->locus_no);
    my $arrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $arrayRef;
}

####################################################################
sub goInfoArrayRef {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT unique G.goid, go_term, go_aspect, go_definition
        FROM   $schema.go G, $schema.go_locus_goev GLG
        WHERE  GLG.locus_no = ?
        AND    GLG.goid = G.goid
    ");
    $sth->execute($self->locus_no);
    my $arrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $arrayRef;
}


####################################################################
sub locusGeneInfoArrayRef {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT reference_no, literature_topic
        FROM   $schema.locus_gene_info
        WHERE  locus_no = ?
    ");
    $sth->execute($self->locus_no);
    my $arrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $arrayRef;
}

####################################################################
sub goLocusGoevArrayRef {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT goid, go_evidence_no, is_not
        FROM   $schema.go_locus_goev
        WHERE  locus_no = ?
    ");
    $sth->execute($self->locus_no);
    my $arrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $arrayRef;
}

####################################################################
sub curatorNoteInfoArrayRef {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT CN.curator_note_no, CN.note, CN.is_public
        FROM   $schema.curator_note CN, $schema.locus_cn LCN
        WHERE  CN.curator_note_no = LCN.curator_note_no
        AND    LCN.locus_no = ?
    ");
    $sth->execute($self->locus_no);
    my $arrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $arrayRef;
}

####################################################################
sub locusAliasArrayRef {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT A.alias_no, A.alias_name
        FROM   $schema.alias A, $schema.locus_alias LA
        WHERE  A.alias_no = LA.alias_no
        AND    LA.locus_no = ?
    ");
    $sth->execute($self->locus_no);
    my $arrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $arrayRef;
}

####################################################################
sub aliasNameAliasTypeList {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT A.alias_name, A.alias_type 
        FROM   $schema.alias A, $schema.locus_alias LA
        WHERE  LA.locus_no = ?
        AND    LA.alias_no = A.alias_no        
    ");
    $sth->execute($self->locus_no);
    my $arrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $arrayRef;
}

####################################################################
sub locusGpArrayRef {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT GP.gene_product_no, GP.gene_product
        FROM   $schema.gene_product GP, $schema.locus_gp LGP
        WHERE  GP.gene_product_no = LGP.gene_product_no
        AND    LGP.locus_no = ?
    ");
    $sth->execute($self->locus_no);
    my $arrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $arrayRef;
}

####################################################################
# sub sacchdbPhenoArrayRef {
####################################################################
#    my ($self) = @_;
#    my $schema = $self->schema;
#    my $sth = $self->dbh->prepare("
#        SELECT sacchdb_phenotype_no, sacchdb_phenotype
#        FROM   $schema.sacchdb_phenotype
#        WHERE  locus_no = ?
#    ");
#    $sth->execute($self->locus_no);
#    my $arrayRef = $sth->fetchall_arrayref();
#    $sth->finish;
#    return $arrayRef;
# }

####################################################################
sub unPublishedPaperInfoArrayRef {
####################################################################
    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.primary_key = ?
	AND    RL.tab_name = 'LOCUS'
	AND    R.status != 'Published'
        AND    RL.col_name is null
	AND    RL.reference_no = R.reference_no
        ORDER BY R.year
    ");
    $sth->execute($self->locus_no);
    my $arrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $arrayRef;
}

####################################################################
sub twoPointInfoArrayRef {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT  T.two_point_no, T.two_point_name, T.distance
        FROM    $schema.two_point T, $schema.locus_twopt LT
        WHERE   LT.locus_no = ?
        AND     LT.two_point_no = T.two_point_no
    ");
    $sth->execute($self->locus_no);
    my $arrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $arrayRef;
}

####################################################################
sub curatedRefNum {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
         SELECT count(unique reference_no)
         FROM   $schema.locus_gene_info
         WHERE  locus_no = ?
         AND    upper(literature_topic) != 'NOT YET CURATED' 
    ");
    $sth->execute($self->locus_no);
    my $curatedNum = $sth->fetchrow();
    $sth->finish;
    return $curatedNum;
}

####################################################################
sub notYetCuratedRefNum {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
         SELECT count(unique reference_no)
         FROM   $schema.locus_gene_info
         WHERE  locus_no = ?
         AND    upper(literature_topic) = 'NOT YET CURATED' 
    ");
    $sth->execute($self->locus_no);
    my $notYetCuratedNum = $sth->fetchrow();
    $sth->finish;
    return $notYetCuratedNum;
}

####################################################################
sub updateLogInfoArrayRef {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        SELECT date_created, old_value, new_value, col_name, 
               description, is_public 
	FROM   $schema.update_log 
	WHERE  tab_name = 'LOCUS'
	AND    primary_key = ?
    ");
    $sth->execute($self->locus_no);
    my $arrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $arrayRef;
}

####################################################################
sub makeStdName {  
####################################################################
### make this the standard name
    my ($self) = @_;
    my $schema = $self->schema;
#    my $sth = $self->dbh->prepare("
#         DELETE from $schema.gene_reservation
#         WHERE  locus_no = ?
#    ");
    my $sth = $self->dbh->prepare("
        UPDATE  $schema.gene_reservation
        SET     is_standardized = 'Y',
                date_standardized = sysdate
        WHERE   locus_no = ?
    ");
    $sth->execute($self->locus_no);
    $sth->finish;
}

####################################################################
sub deleteFreeTextPhenotype {
####################################################################
    my ($self) = @_;

    my $schema = $self->schema;

    my $sth = $self->dbh->prepare("
        SELECT phenotype_no 
        FROM   $schema.locus_pheno
        WHERE  locus_no = ?
        AND    phenotype_type = 'Free text'
    ");
    
    $sth->execute($self->locus_no);

    my $phenotypeNo = $sth->fetchrow();

    $sth->finish;

    ### delete free text pheno and locus association

    my $sth = $self->dbh->prepare("
        DELETE from $schema.locus_pheno
        WHERE  locus_no = ?
        AND    phenotype_type = 'Free text'
    ");

    $sth->execute($self->locus_no);

    $sth->finish;

    ### delete associated reflink entry 

    my $prikey = $self->locus_no."::".$phenotypeNo."::Free text";
    my $prikeyCol = "LOCUS_NO::PHENOTYPE_NO::PHENOTYPE_TYPE";
    my $tabNm = "LOCUS_PHENO";
    
    my $sth = $self->dbh->prepare("
        DELETE from $schema.reflink
        WHERE  tab_name = ?
        AND    primary_key_col = ?
        AND    primary_key = ?
    ");
  
    $sth->execute($tabNm, $prikeyCol, $prikey);

    $sth->finish;
  
    ### check if there is other locus associated with this free text entry

    my $sth = $self->dbh->prepare("
        SELECT locus_no  
        FROM   $schema.locus_pheno
        WHERE  phenotype_no = ?
         union
        SELECT feature_no
        FROM   $schema.feat_pheno
        WHERE  phenotype_no = ?
    ");
    
    $sth->execute($phenotypeNo, $phenotypeNo);

    my $arrayRef = $sth->fetchall_arrayref();

    $sth->finish;

    if (@$arrayRef) { return; }  ### this free text is still used 
                               ### by other loci or features, keep it

    ### otherwise, delete this free text entry from phenotype table

    my $sth = $self->dbh->prepare("
        DELETE from $schema.phenotype
        WHERE  phenotype_no = ?
    ");
    $sth->execute($phenotypeNo);

    $sth->finish;

}


####################################################################
# sub deleteSacchdbPhenotype {
####################################################################
#    my ($self) = @_;
#    my $schema = $self->schema;
#    my $sth = $self->dbh->prepare("
#        DELETE from $schema.sacchdb_phenotype
#        WHERE  locus_no = ?
#    ");
#    $sth->execute($self->locus_no);
#    $sth->finish;
# }

####################################################################
sub deleteLocusGp {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        DELETE from $schema.locus_gp
        WHERE  locus_no = ?
    ");
    $sth->execute($self->locus_no);
    $sth->finish;
}

####################################################################
sub deleteReference {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
        DELETE from $schema.reference
        WHERE  reference_no in
               (select reference_no
                from   $schema.locus_gene_info
                where  locus_no = ?
                union
                select reference_no
                from   $schema.reflink
                where  primary_key = ?
                and    tab_name = 'LOCUS'
                and    primary_key_col = 'LOCUS_NO')
    ");
    $sth->execute($self->locus_no, $self->locus_no);
    $sth->finish;
}

####################################################################
sub deleteOldRefAssociation {
####################################################################
    my ($self) = @_;
    my $schema = $self->schema;

    ####### get all reference_no associated with this locus
    ####### and entries listed in reflink table with tab_name = 'LOCUS' 
    ####### --- for unpublished references 
    my $sth = $self->dbh->prepare("
        SELECT reference_no
        FROM   $schema.reflink
        WHERE  primary_key = ?
        AND    tab_name = 'LOCUS'
        AND    primary_key_col = 'LOCUS_NO'
    ");
    $sth->execute($self->locus_no);
    
    my @refNo;

    while (my ($refNo) = $sth->fetchrow()) {
	
	push(@refNo, $refNo);
	
    }

    $sth->finish;

    ####### delete all reflink entries associated with this locus 
    ####### with tab_name = 'LOCUS' --- for unpublished references 
    my $sth = $self->dbh->prepare("
        DELETE from $schema.reflink
        WHERE  primary_key = ?
        AND    tab_name = 'LOCUS'
        AND    primary_key_col = 'LOCUS_NO'
    ");
    $sth->execute($self->locus_no);

    $sth->finish;
       
    foreach my $refNo (@refNo) {
 
	my $sth = $self->dbh->prepare("
            DELETE from $schema.reflink
            WHERE  reference_no = ?
            AND    tab_name = 'LOCUS_GP' and
                   primary_key like ? and
                   primary_key_col = 'LOCUS_NO::GENE_PRODUCT_NO'
        ");

        $sth->execute($refNo, $self->locus_no."::%");

        $sth->finish;
       
	###### delete this reference only if there is no other association
        ###### found in database
	my $sth = $self->dbh->prepare("
            DELETE from $schema.reference
            WHERE  reference_no = ?
            AND    reference_no not in
                   (select reference_no
                    from   $schema.locus_gene_info)
            AND    reference_no not in
                   (select reference_no
                    from   $schema.feat_gene_info)
            AND    reference_no not in
                   (select reference_no
                    from   $schema.reflink)
        ");

	$sth->execute($refNo);

	$sth->finish;

    }

}

####################################################################
sub GetLocusArrayRefBYname {
####################################################################
    my ($self, %args) = @_;
    my $dbh = $args{'dbh'} || "A database handle must be passed into 'GetLocusArrayRefBYname' method.";
    my $schema = $self->schema;
    my $sth = $dbh->prepare("
        SELECT locus_name
        FROM   $schema.locus 
        WHERE  upper(locus_name) like ?
        ORDER BY locus_name
    ");
    $sth->execute(uc($args{'locus_name'}));
    my $arrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $arrayRef;
}

####################################################################
sub AddColleagueContact {
####################################################################
    my ($self, %args) = @_;
    my $dbh = $args{'dbh'} || die "A database handle must be passed to 'AddColleagueContact' method.";
    my $schema = $self->schema;
    my $collNo = $args{'colleague_no'};
    my $locusNo = $args{'locus_no'};
    my $sth = $dbh->prepare("
        INSERT INTO $schema.coll_locus(colleague_no, locus_no)
        VALUES(?,?)
    ");
    $sth->execute($collNo, $locusNo);
    $sth->finish;
}

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

}


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

=pod

=head1 Name

Locus.pm


=head1 Description

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


=head1 Instantiating a New Locus Object

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

my $locusObj = Locus->new(dbh=>$dbh, $colNm=>$value); 

where $colNm is either locus_no or locus_name 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 $locusObj = Locus->new(dbh=>$dbh, locus_no=>$locusNo); 

my $locusObj = Locus->new(dbh=>$dbh, locus_name=>$locusName); 


=head1 Accessor Methods


All accessor methods take the form of : 


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


my $locusName = $locusObj->locus_name; 


my $chromsome = $locusObj->chromosome; 

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


See valid columns in locus table:

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


In addition, the following accessor methods are provided to access no Locus columns in database.


=head2 featureNameList

Usage:

my $featureNameList = $locusObj->featureNameList;

This method returns pipe ('|') delimited feature names associated with
a given locus.


=head2 aliasNameList

Usage:

my $aliasNameList = $locusObj->aliasNameList;


This method returns pipe ('|') delimited alias names associated with
a given locus.

=head2 uniformAliasNameList

Usage:

my $uniformAliasNameList = $locusObj->uniformAliasNameList;


This method returns pipe ('|') delimited uniform alias names associated with
a given locus.

=head2 nonUniformAliasNameList

Usage:

my $nonUniformAliasNameList = $locusObj->nonUniformAliasNameList;


This method returns pipe ('|') delimited Non-uniform alias names associated with
a given locus.  

=head2 proteinNameAliasNameList

Usage:

my $proteinNameAliasNameList = $locusObj->proteinNameAliasNameList;


This method returns pipe ('|') delimited protein name alias names associated with
a given locus.  

=head2 gene_product

Usage:

my $gp = $locusObj->gene_product;

This method returns tab-delimited gene_products for a given locus.

=head2 geneReservationInfo

Usage :


my ($reservation_no, $reservation_date, $expiration_date, 
    $date_standardized, $is_standardized) 

    = split(/:/, $locusObj->geneReservationInfo);

This method returns gene_reservation info for the given locus.

=head2 colleagueNmNo

Usage :

my $collInfo = $locusObj->colleagueNmNo;

my @collNmNo = split(/\t/, $collInfo);

foreach my $NmNo (@collNmNo) {

    my ($colleague_fullname, $colleague_no) = split(/::/, $NmNo);

    my ($fisrt_name, $last_name) = split(/ /, $colleague_fullname);


    #######

}


=head2 phenotypeInfoArrayRef

Usage : 

foreach my $rowRef (@{$locusObj->phenotypeInfoArrayRef}) {
    
    my ($phenotype_no, $phenotype, $phenotype_type, $sentence) 

	= @$rowRef;

    .....
	
}

This method returns an arrayref for the phenotype information 
associated with a given locus.

=head2 goidArrayRef

Usage : 

foreach $rowRef (@{$locusObj->goidArrayRef}) {

    my ($goid) = @$rowRef;

    ....

}

This method returns an arrayref for the goid associated with
a given locus.

=head2 goInfoArrayRef

Usage : 

foreach $rowRef (@{$locusObj->goInfoArrayRef}) {

    my ($goid, $goTerm, $goAspect, $goDefinition) = @$rowRef;

    ....

}

This method returns an arrayref to the go information associated with
a given locus.

=head2 locusGeneInfoArrayRef

Usage : 

foreach $rowRef (@{$locusObj->locusGeneInfoArrayRef}) {

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

    ....

}

This method returns an arrayref to the locus gene info associated with
a given locus.


=head2 goLocusGoevArrayRef

Usage : 

foreach $rowRef (@{$locusObj->goLocusGoevArrayRef}) {

    my ($goid, $goEvidenceNo, $isNot) = @$rowRef;

    ....

}

This method returns an arrayref to the go_locus_goev info associated with
a given locus


=head2 curatorNoteInfoArrayRef

Usage :

foreach my $rowRef (@{$locusObj->curatorNoteInfoArrayRef}) {

    my ($noteNo, $note, $isPublic) = @$rowRef;

    #####
}


This method returns curator_note info for the given locus.


=head2 locusAliasArrayRef

Usage :

foreach my $rowRef (@{$locusObj->locusAliasArrayRef}) {

    my ($aliasNo, $aliasNm) = @$rowRef;

    #####
}


This method returns alias info for the given locus.

=head2 aliasNameAliasTypeList

Usage :

foreach my $rowRef (@{$locusObj->aliasNameAliasTypeList}) {

    my ($aliasNm, $aliasType) = @$rowRef;

    #####
}


This method returns alias name and alias type of all the aliases for the given locus.


=head2 locusGpArrayRef

Usage :

foreach my $rowRef (@{$locusObj->locusGpArrayRef}) {

    my ($gpNo, $gp) = @$rowRef;

    #####
}


This method returns gene_product info for the given locus.


=head2 unPublishedPaperInfoArrayRef


Usage :

foreach my $rowRef (@{$locusObj->unPublishedPaperInfoArrayRef}) {

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

    #####
}

This method returns unpublished paper info for the given locus.


=head2 twoPointInfoArrayRef

Usage :

foreach my $rowRef (@{$locusObj->twoPointInfoArrayRef}) {

    my ($two_point_no, $two_point_name, $distance) = @$rowRef;

    #####

}

This method returns two_point data for the given locus.

=head2 curatedRefNum

Usage : 

my $curatedRefNum = $locusObj->curatedRefNum;

This method returns the number of curated references associated with
the given locus.

=head2 notYetCuratedRefNum

Usage : 

my $notYetCuratedRefNum = $locusObj->notYetCuratedRefNum;

This method returns the number of not yet curated references 
associated with the given locus.


=head2 getRow

Usage :

my $row = $locusObj->getRow;

This method returns tab_delimited row for the given locus entry.


=head2 updateLogInfoArrayRef

Usage :

foreach my $rowRef (@{$locusObj->updateLogInfoArrayRef}) 

    my ($dateCreated, $oldVal, $newVal, $colNm, $desc, $isPublic) 

    = @$rowRef;


    ####

}


This method returns update_log info for the given locus entry.



=head2 GetLocusArrayRefBYname

Usage :

my $arrayRef = Locus->GetLocusArrayRefBYname(dbh=>$dbh,
					     locus_name=>$locus);

foreach my $rowRef (@$arrayRef) {

    my ($locusNm) = @$rowRef;

    #####

}


This method return all loci that match $locus.


=head1 makeStdName Method

Usage :

eval { $locusObj->makeStdName; };

This method makes the given locus to standard gene name bt deleting its entry from gene_reservation table.

=head1 AddColleagueContact Method

Usage :

eval {

    Locus->AddColleagueContact(dbh=>$dbh,
			       locus_no=>$locusNo,
			       colleague_no=>$collNo);

};

This method creates an entry in coll_locus linking table for the given locus_no and colleague_no.

=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









