#!/usr/bin/perl
package sdevGeneRegistry;

#######################################################################
##### Author :	Shuai Weng
##### Date   :  April 2001
##### Description : This package contains all necessary methods for dictyBase
#####               curators to insert gene registry info  
#####               into oracle database. 
#####              
################## notes for updating the reference info ##############
#
# Reference Update:
# ================
# 
# Situation 1:    With pubmed id
#
#        * Check if the pubmed id is in database
#                ** If yes, retrieve reference_no
#                ** otherwise (the new paper is not in database) 
#                    *** If it is a gene UPDATE entry
#                        ****  check if there are any reflink entries 
#                                  (with 'old reference')
#                                  associated with this gene with 
#                                  tab_name = 'LOCUS' or 
#                                  tab_name = 'LOCUS_PHENO' or
#                                  tab_name = 'LOCUS_GP'
#                                        ***** if yes, delete these entries
#                                              from reflink table 
#                                              and check if there is any 
#                                              other reflink or
#                                              locus_gene_info 
#                                              or feat_gene_info entry 
#                                              associated with this  
#                                              'old reference'
#                                                ****** if not, delete this 
#                                                       'old reference' from
#                                                       reference table
#           
#                    *** insert the new reference info into reference, 
#                        journal table, etc., and get reference_no
# 
#                       
# Situation 2:    No pubmed id, with author name(s) and published year provided
#
#        *       Create citation based on the info user entered
#        *       Check if the citation is in database
#                        ** if yes, retrieve reference_no
#                        ** otherwise (the paper is not in database) 
#                            *** if this is a gene UPDATE entry 
#                                **** check if there are any reflink entries 
#                                     ('old reference')
#                                     associated with this gene with 
#                                     tab_name = 'LOCUS' or 
#                                     tab_name = 'LOCUS_PHENO' or
#                                     tab_name = 'LOCUS_GP'
#                                        ***** if yes, delete these entries
#                                              from reflink table 
#                                              and check if there is any 
#                                              other reflink or
#                                              locus_gene_info 
#                                              or feat_gene_info entry 
#                                              associated with this  
#                                              'old reference'
#                                                ****** if not, delete this 
#                                                       'old reference' from
#                                                       reference table
#                         
#                            *** insert new reference info into reference 
#                                and its related tables, and get
#                                reference_no.
#                        
#        
#
# Associate Reference to Locus:
# ============================
#
# Situation 1: Published paper
#
#        * Check if this new reference is associated with the given gene in 
#          locus_gene_info table
#                ** If not, insert info into locus_gene_info table
#
#        * Check if this reference is associated with the given gene in
#          reflink table with tab_name = 'LOCUS'
#                ** if yes, delete this entry from reflink table
#                
# Situation 2: Unpublished paper
#
#        * Check if this new reference is associated with the given gene in
#          reflink table with tab_name = 'LOCUS'
#                ** If not, insert entry into reflink table with 
#                   tab_name = 'LOCUS'
#
#
# Associate New Reference to Gene_product and Locus_pheno:
# =============================================================
# 
# If there is any gene_product or locus_pheno associated with 
# the given gene, then insert new entries into reflink table with
# tab_name = 'LOCUS_GP' or tab_name = 'LOCUS_PHENO' and 
# reference_no = new refNo.   
#
# ======================================================================
#
#######################################################################
use strict;
use DBI;
use CGI qw/:all :html3/;

use lib "/usr/local/dicty/www_dictybase/db/lib/common";
use Login qw (ConnectToDatabase);
use TextUtil qw(DeleteUnwantedChar);
use MedlineParse qw(CreateCitation);

use lib "/usr/local/dicty/www_dictybase/db/lib/dictyBase";
use dictyBaseCentralMod qw(:formatPage :getInfo);
use Pubmed;

use lib "/usr/local/dicty/www_dictybase/db/lib/dictyBase/curation";
use ColleagueCurationMod qw (:formSub);
use printReferenceMod qw (:formatSub);
use printFeatureMod qw (:printInfo);

use lib "/usr/local/dicty/www_dictybase/db/lib/dictyBase/Objects";
use Gene_reservation;
use Locus_gene_info;
use ConfigPathdictyBase;
use ConfigURLdictyBase;
use Gene_product;
use Curator_note;
use Locus_alias;
use Coll_locus;
use Colleague;
use Reference;
use Reflink;
use Journal;
use Author;
use Author_editor;
use Abstract;
use Publication_type;
use Phenotype;
use Locus_pheno;
use Locus_gp;
use Feature;
use Locus;
use Alias;
use dictyBaseid;

#######################################################################
#################### global variables #################################
#######################################################################

my $dbh;
my $dblink; 
my $configUrl;
my $configPath;
my $dbname;
my $dir;

#######################################################################
sub new {      ############ constructor ###############################
#######################################################################
       
	my ($self, %args) = @_;

	$self = {};
	bless $self;

      	$self->{'_database'} = $args{'database'};
	$self->{'_user'}     = $args{'user'};
	$self->{'_help'}     = $args{'help'};
	$self->{'_id'}       = param('id');
      	return $self;
}

sub database { $_[0]->{_database} }
sub user { $_[0]->{_user} }
sub title { $_[0]->{_title} }
sub help { $_[0]->{_help} }
sub id { $_[0]->{_id} }

######################################################################
sub DESTROY {   ############ destructor ##############################
######################################################################
    	if (defined $dbh) {
		$dbh->disconnect;
    	}
}

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

	$configUrl = ConfigURLdictyBase->new;	
	$dblink = $configUrl->dblink($self->database);
        if (!$self->user) {
	    print "location: ", $configUrl->dictyBaseCGIRoot, "$dblink/curatorLogin\n";
	    print "Content-type: text/html\n\n";
	    exit;
	}
	my ($dbuser, $dbpasswd) = &getUsernamePassword(uc($self->user), 
						       $self->database);
	if (!$dbuser || !$dbpasswd) {
	    print "location: ", $configUrl->dictyBaseCGIRoot, "$dblink/curatorLogin\n";
	    print "Content-type: text/html\n\n";
	    exit;
	}
	$dbname = $self->database;
	$dbname = "\U$dbname";
	$self->{'_title'} = "$dbname Curator Page for Gene Registry Information";
	$configPath = ConfigPathdictyBase->new;
	$dir = $configPath->dataDir4web."submission/colleague";

	if (param('gene')) {
	    if (param('delayCB') =~ /on/i || param('deleteCB') =~ /on/i) {
		$self->processFile;
	    }
	    else {
		$dbh = &ConnectToDatabase($self->database, $dbuser, 
					  $dbpasswd);
		$self->commitInfo;
		$dbh->disconnect;
	    }
	}
	else {    
	    $dbh = &ConnectToDatabase($self->database);
	    $self->displayInfo;
	}
}

#######################################################################
sub displayInfo {
#######################################################################
    my ($self) = shift;

    &printStartPage($self->database, $self->title, $self->help);

    my (@author);
    $self->populateInfo(\@author);

    print center(h2($self->{'_type'})),p;
    
    
    if ($self->{'_orf'}) {

	my $featObj = Feature->new(dbh=>$dbh,
				   feature_name=>$self->{'_orf'});
	
	if ($featObj->featureTypeList =~ /Dubious/i) {

	    print font({-color=>'red'}, 
		       "The ORF name user entered (".$self->{'_orf'}.") is a ".
		       a({-href=>$configUrl->dictyBaseHelp."glossary.html#dubious", 
			  -target=>'infowin'}, "Dubious ORF")."."),p;
			  
	}

    }

    my $id = $self->id;
    my $fullname = $self->{'_lname'}.", ".$self->{'_fname'};
    if (-e "$dir/$id.MajorUpdate") {
	print font({-color=>'red'}, b("Please update colleague ".a({-href=>$configUrl->dictyBaseCGIRoot."$dblink/curation/colleagueMajorUpdate?id=$id&user=".$self->user, -target=>"infowin"}, $fullname)." info in database before commit current gene registry info.")), p;
    }
    elsif (-e "$dir/$id.newEntry") {
	print font({-color=>'red'}, b("Please commit new colleague ".a({-href=>$configUrl->dictyBaseCGIRoot."$dblink/curation/colleagueNewEntry?id=$id&user=".$self->user, -target=>"infowin"}, $fullname)." info into database before commit current gene registry info.")), p; 
    }
    
    if ($self->{'_warning'}) {
	print p, b(font({-color=>'red'}, "Warning: ".$self->{'_warning'})), p;
    }

    print b("Please check the information below and make any changes necessary."), p;
   
    print startform,
          hidden(-name=>'id',
		 -value=>$self->id),
          hidden(-name=>'user',
		 -value=>$self->user),
          hidden(-name=>'type',
		 -value=>$self->{'_type'}),
          hidden(-name=>'lname',
		 -value=>$self->{'_lname'}),
          hidden(-name=>'fname',
		 -value=>$self->{'_fname'}),
          hidden(-name=>'email',
		 -value=>$self->{'_email'});
    print &subtitle("Information about the Gene");

    my $locusObj = Locus->new(dbh=>$dbh,
			      locus_name=>$self->{'_gene'});

    my $updateHeader;
    if ($self->{'_type'} =~ /update/i) {
	$updateHeader = $self->updateHeader;
    }
    print table({-border=>'0',
		 -cellpadding=>'4',
		 -cellspacing=>'3',
		 -width=>'100%'},
		$updateHeader.
		$self->geneName($locusObj).
		$self->geneDesc($locusObj).
		$self->geneFullDesc($locusObj).
		$self->orfName($locusObj).
		$self->uniformAliasName($locusObj).
		$self->nonUniformAliasName($locusObj).
		$self->proteinNameAlias($locusObj).
		$self->geneProduct($locusObj).
		$self->phenotype($locusObj).
		$self->briefId($locusObj)),p;
    print table(Tr(td({-align=>'left'}, 
		      checkbox(-name=>'stdNmCB',
			       -label=>'').
		      b(font({-color=>'red'},
			     " Make this the standard gene name")))));
    if ($self->{'_type'} =~ /^Gene Registry/i && !$self->{'_orf'}) {
	&printAssigndictyBaseidBox;
    }

    print p, a({-href=>$configUrl->dictyBaseCGIRoot."$dblink/curation/dbSearch?type=gene_product", -target=>"infowin"}, "Search existing gene products"),p;	

    print &subtitle("Reference Section");
    &author(@author);
    &refTitle($self->{'_reftitle'});
    print table(&journal($self->{'_journal'}).
		&volume($self->{'_volume'}).
		&page($self->{'_page'}).
		&year($self->{'_year'}));
    &publishedStatus($self->{'_reftype'});
    &refSource($self->{'_refSource'}, $self->{'_pubmed'});
    &pubType;

    print &subtitle("Curator Section");
    print b("Curator Note:"), br;
    print &newCuratorNote;
    print &delayDelete;   
    print &submitReset;
    print end_form;
    &printEndPage;
}

#######################################################################
sub commitInfo {
#######################################################################
    my ($self) = shift;

    &printStartPage($self->database, $self->title, $self->help);
    my @author;
    $self->processData(\@author);
    
    if (!$self->{'_gene'}) {
	print "The gene name is required for completing this gene registry. Please go back, enter the gene name and try again.";
        exit;
    }
    $self->{'_email'} =~ s/^(.+)[;, ]/$1/;
    
    my $colleague_no = Colleague->GetColleagueNoBYnameAndEmail(dbh=>$dbh,
					  last_name=>$self->{'_lname'},
					  first_name=>$self->{'_fname'},
					  email=>$self->{'_email'});

    if (!$colleague_no) {
	print p, b("Colleague ".font({-color=>'red'}, $self->{'_lname'}.", ".$self->{'_fname'})." with email address ".font({-color=>'red'}, $self->{'_email'})." can't be found in the database. Please go back and make sure you have commited this colleague info into database."),p;
	exit;
    }

    #### get feature object
    my $featObj = $self->getFeatureObject;

    #### update locus related tables
    my $chr;
    if ($featObj) { $chr = $featObj->chromosome; }
	
    my $locusObj = $self->updateLocusRelatedTables($colleague_no, $chr);
    
    #### update feature table
    if ($featObj) {
	$self->updateFeatureTable($featObj, $locusObj->locus_no, 
				  $self->{'_briefId'});
    }

    #### update alias table for uniform alias names
    $self->updateAliasTable($locusObj, $self->{'_uniformAliasName'}, 'Uniform');
    
    #### update alias table for non-uniform alias names
    $self->updateAliasTable($locusObj, $self->{'_nonUniformAliasName'}, 'Non-uniform');
    
    #### update alias table for protein name aliases
    $self->updateAliasTable($locusObj, $self->{'_proteinNameAlias'}, 'Protein name');
    
    #### update reference related tables
    my $refNo = $self->updateReferenceRelatedTables($locusObj, \@author);

    #### update gene_product
    $self->updateGeneProduct($locusObj, $self->{'_geneproduct'},
			     $refNo);
    
    #### update free text phenotype
    $self->updateFreeTextPhenotype($locusObj, $self->{'_phenotype'},
				   $refNo);
    
    ####### update curator_note
    $self->updateCuratorNoteTable($locusObj, param('note'), 
				  param('idList'), param('isPublic'));


    #################
    $dbh->disconnect;
    #################
    my $id = param('id');

    system("/usr/bin/mv $dir/$id.GeneRegistry4ace $dir/archive/");
    
    &returnToCuratorCentral($self->user, $configUrl->dictyBaseCGIRoot."$dblink/curatorLogin");
    &printEndPage;
}

#######################################################################
sub getFeatureObject {
#######################################################################
    my ($self) = @_;
    
    if ($self->{'_orf'}) {
	my $featObj = Feature->new(dbh=>$dbh,
				   feature_name=>$self->{'_orf'});
	if ($featObj) { return $featObj; } 
    }
    return ;
}

########################################################################
sub updateCuratorNoteTable {
########################################################################
    my ($self, $locusObj, $note, $idList, $isPublic) = @_;
    if (!$note) { return; }
    if ($isPublic =~ /^Y/) { $isPublic = "Y"; }
    else { $isPublic = "N"; }
    &DeleteUnwantedChar(\$note);
    &DeleteUnwantedChar(\$idList);
    $idList =~ s/ +/ /g;
    $idList =~ s/, */\,/g;
    my $locusNo = $locusObj->locus_no;
    if ($idList !~ /Locus,${locusNo}/i) {
        $idList .= " Locus,$locusNo";
    }
    $idList =~ s/^ //;
    my $noteObj = Curator_note->new(dbh=>$dbh,
				    note=>$note,
				    is_public=>$isPublic);
    if (!$noteObj) {
	eval {
	    Curator_note->Insert(dbh=>$dbh,
				 literals=>{curator_note_no=>'CGM_DDB.cnno_seq.nextval'},
				 binds=>{note=>$note,
					 is_public=>$isPublic});
	};
	if ($@) {
	    print "An error occurred when inserting new curator_note '$note' into database:$@", br;
	    $dbh->rollback;
	    return;
	}
	else {
	    print "The new curator_note '$note' has been inserted into database.", br;
	    $dbh->commit;
	    $noteObj = Curator_note->new(dbh=>$dbh,
					 note=>$note,
					 is_public=>$isPublic);
	}
    }
    eval {
        Curator_note->InsertCuratorNoteLinkInfo(dbh=>$dbh,
			  idList=>$idList,
			  curator_note_no=>$noteObj->curator_note_no);
    };
    if ($@) {
	print "An error occurred when inserting info into curator_note link table:$@", br;
	$dbh->rollback;
    }
    else {
	print "The curator note info has been inserted into linking table(s).", br;
	$dbh->commit;
    }
}


########################################################################
sub updateFreeTextPhenotype {
########################################################################
    my ($self, $locusObj, $freeTextPheno, $refNo) = @_;

    if ($locusObj->freeTextPhenotype && 
	$locusObj->freeTextPhenotype ne $freeTextPheno) {

	eval { $locusObj->deleteFreeTextPhenotype; };

	if ($@) {
	    print "An error occurred when deleting old free text phenotype entry for locus_no = ".$locusObj->locus_no." from database.:$@", br;
	    return ;
	}

	$dbh->commit;

    }

    

    my $phenotypeObj;
    
    if ($freeTextPheno) {

	$phenotypeObj = Phenotype->new(dbh=>$dbh,
				       phenotype=>$freeTextPheno);
				       
    
	if (!$phenotypeObj) { 

	    eval {

		Phenotype->Insert(dbh=>$dbh,
				  literals=>{phenotype_no=>'CGM_DDB.phenono_seq.nextval'},
				  binds=>{phenotype=>$freeTextPheno});
				     

	    };

	    if ($@) {

		print "An error occurred when inserting new phenotype '$freeTextPheno' into database:$@", br;

		$dbh->rollback;
		
	    }
	    else {

		print "The new phenotype '$freeTextPheno' has been inserted into database.", br;
		
		$phenotypeObj = Phenotype->new(dbh=>$dbh,
					       phenotype=>$freeTextPheno);

	    }

	}

	if ($phenotypeObj) {

	    eval {

		Locus_pheno->Insert(dbh=>$dbh,
				    binds=>{phenotype_no=>$phenotypeObj->phenotype_no,
					    locus_no=>$locusObj->locus_no,
					    phenotype_type=>'Free text'});

	    

	    };

	    if ($@) {

		print "Error occurred when inserting locus_no=", $locusObj->locus_no, ", phenotype_type=", $phenotypeObj->phenotype_no, ", and phenotype_type = 'Free text' into database:$@",p;

		$dbh->rollback;
	
	    }
	    else {
		
		print "The new locus_pheno entry for locus_no=", $locusObj->locus_no, ", phenotype_type=", $phenotypeObj->phenotype_no, ", and phenotype_type = 'Free text' has been inserted inro database.",p;

		$dbh->commit;

	    }
	}

    }
    if (!$refNo) { return; }

    if (!$phenotypeObj && $locusObj->freeTextPhenotype) {

	$phenotypeObj = Phenotype->new(dbh=>$dbh,
				       phenotype=>$locusObj->freeTextPhenotype);

	if (!$phenotypeObj) { return; }
	
    }
    
    my $tabNm = "LOCUS_PHENO";
    my $prikey = $locusObj->locus_no."::".$phenotypeObj->phenotype_no."::Free text";
    my $prikeyCol = "LOCUS_NO::PHENOTYPE_NO::PHENOTYPE_TYPE";
    $self->insertRefLink($refNo, $tabNm, $prikey, $prikeyCol);

}

########################################################################
sub updateGeneProduct {
########################################################################
    my ($self, $locusObj, $gp, $refNo) = @_;
    my $gpDB = $locusObj->gene_product;
    $gpDB =~ s/\t/\; /g;
    if ($gpDB && $gpDB ne $gp) {
	eval { $locusObj->deleteLocusGp; };
	if ($@) {
	    print "An error occurred when deleting old locus_gp entry for locus_no = ".$locusObj->locus_no." from database.:$@", br;
	    return;
	}
	$dbh->commit;
    }

    # if (!$gp || $gpDB eq $gp) { return; }

    my ($gpObj, $gpNo);

    if ($gp =~ /^[0-9]+$/) {
	$gpObj = Gene_product->new(dbh=>$dbh,
				      gene_product_no=>$gp);
	if (!$gpObj) {
	    print "The gene_product_no = $gpNo is not found in database.", br;
        }
	else {
	    $gpNo = $gp;
	}	    
    }
    elsif ($gp && $gp ne $gpDB) {
	$gpObj = Gene_product->new(dbh=>$dbh,
				   gene_product=>$gp);
	if ($gpObj) {
	    $gpNo = $gpObj->gene_product_no;
	}
	else {
	    $gpNo = $self->insertGeneProductTable($gp);
	}
	if ($gpNo) {
	    $self->insertIntoLocusGpTable($locusObj->locus_no, $gpNo);
	}
    }
    elsif ($gpDB && !$gpNo) {
	
	$gpObj = Gene_product->new(dbh=>$dbh,
				   gene_product=>$gpDB);

	if ($gpObj) { $gpNo = $gpObj->gene_product_no; }

    }

    if (!$refNo || !$gpNo) { return;}

    my $tabNm = "LOCUS_GP";

    my $prikey = $locusObj->locus_no."::".$gpNo;

    my $prikeyCol = "LOCUS_NO::GENE_PRODUCT_NO";

    $self->insertRefLink($refNo, $tabNm, $prikey, $prikeyCol);

}

########################################################################
sub insertGeneProductTable {
########################################################################
    my ($self, $gp) = @_;
    eval {
	 Gene_product->Insert(dbh=>$dbh,
			      literals=>{gene_product_no=>'CGM_DDB.gpno_seq.nextval'},
			      binds=>{gene_product=>$gp});
    };
    if ($@) {
	 print "An error occurred when inserting gene_product '$gp' intp database:$@", br;
	 $dbh->rollback;
	 return;
    }
    else {
	 print "The new gene_product '$gp' has been inserted into database.", br;
	 $dbh->commit;
	 my $gpObj = Gene_product->new(dbh=>$dbh,
				       gene_product=>$gp);
	 return $gpObj->gene_product_no;
    }
}

########################################################################
sub insertIntoLocusGpTable {
########################################################################
    my ($self, $locusNo, $gpNo) = @_;
    eval {
	Locus_gp->Insert(dbh=>$dbh,
			 binds=>{locus_no=>$locusNo,
				 gene_product_no=>$gpNo});
    };
    if ($@) {
	print "An error occurred when inserting new locus_gp entry into database:$@", br;
	$dbh->rollback;
    }
    else {
	print "The new locus_gp entry has been inserted into database.", br;
	$dbh->commit;
    }
}

########################################################################
sub updateReferenceRelatedTables {
########################################################################
    my ($self, $locusObj, $authorRef) = @_;
    
    my $refNo;
    if ($self->{'_pubmed'}) {
	
	my $refObject = Pubmed->new(dbh=>$dbh,
				    pubmed=>$self->{'_pubmed'});
	$refNo = $refObject->referenceNo;

	if ($refNo) {

	    if ($refObject->error =~ /exist/i) {

		print "The pubmed reference (pubmed id = ".$self->{'_pubmed'}.") is already in database.",br;
		
	    }
	    else {

		print "Reference info has been inserted into reference related tables in database.", br;
		
		if (param('type') =~ /update/i) { ### delete old one
		    print "Delete old, bad reference if there is one...", br;

       		    eval { $locusObj->deleteOldRefAssociation; };

		    if ($@) {
			print "An error occurred when deleting old reference info for locus_no = ".$locusObj->locus_no.".:$@", br;	
		    }
	        }

	    }
	}
	else {
	    print $refObject->error,br;
	}
    }
    elsif ($$authorRef[0]) {
	if ($self->{'_year'}) {
	    my ($journalNo, $journal);
	    if ($self->{'_journal'}) {
		($journalNo, $journal) = $self->insertJournal($self->{'_journal'});
		if (!$journalNo || !$journal) { return; }
	    }
	    my $citation = $self->createCitation($self->{'_year'}, 
						 $self->{'_reftitle'},
						 $journal, 
						 $self->{'_volume'}, '',
						 $self->{'_page'},
						 $authorRef);
	    $refNo = Reference->GetRefNoBYcitation(dbh=>$dbh,
					    citation=>$citation);
	    if ($refNo) {
		print "The reference with citation = '$citation' is already in database. The reference_no = $refNo.", br;
	    }
	    else  {

		if (param('type') =~ /update/i) { ### delete old one

		    print "Delete old, bad reference if there is one...", br;

       		    eval { $locusObj->deleteOldRefAssociation; };

		    if ($@) {
			print "An error occurred when deleting old reference info for locus_no = ".$locusObj->locus_no.".:$@", br;	
		    }

		}
		### insert new old
		eval {
		    Reference->Insert(dbh=>$dbh,
				  literals=>{reference_no=>'CGM_DDB.refno_seq.nextval'},
				  binds=>{ref_source=>param('refsource'),
				      status=>param('reftype'),
				      year=>$self->{'_year'},
				      page=>$self->{'_page'},
				      volume=>$self->{'_volume'},
				      title=>$self->{'_reftitle'},
				      citation=>$citation,
				      journal_no=>$journalNo});
		};
		if ($@) {
		    print "An error occurred when inserting reference info into database:$@", br;
		    $dbh->rollback;
		}
		else {
		    print "The reference info has been inserted into database.", br;
		    $dbh->commit;
		    my $sth = $dbh->prepare("
                        SELECT CGM_DDB.refno_seq.currval
                        FROM   dual
                    ");
		    $sth->execute;
		    $refNo = $sth->fetchrow;
		    # foreach my $pubtype (param('pubtype')) {
		    #	$self->insertPubType($refNo, $pubtype)
		    # }
		    my $order;
		    foreach my $author (@$authorRef) {
			$order++;
			my $authorNo = $self->insertAuthor($author);
			if ($authorNo) {
			    $self->insertAuthorEditor($authorNo, $refNo, 
						  "Author", $order);
			}
		    }
		}
	    }
	}
	else {
	    print "The reference info can't be inserted into database due to the missing year info.",p;
	}
    }
    if (!$refNo) { return; }

    #### insert gene_info or reflink table
    my $tabNm = "LOCUS";
    my $prikey = $locusObj->locus_no;
    my $prikeyCol = "LOCUS_NO";

    if (param('reftype') =~ /^Published/i || $self->{'_pubmed'}) {
#	my $refNo4gi = Locus_gene_info->GetRefNoBYlocusNo(dbh=>$dbh,
#				   locus_no=>$locusObj->locus_no);
	my $topicArrayRef = 
	    Locus_gene_info->GetTopicArrayRefBYrefNoLocusNo(dbh=>$dbh,
					  locus_no=>$locusObj->locus_no,
                                          reference_no=>$refNo);
        if (@$topicArrayRef) {
	    print "The locus_gene_info entry for locus_no = ".$locusObj->locus_no." and reference_no = $refNo is already in database.",br;
	}
	else {
	    $self->insertLocusGeneInfo($locusObj->locus_no, $refNo);
	}

	my $refNo4reflink = Reflink->GetRefNoBYlocusNo(dbh=>$dbh,
				   locus_no=>$locusObj->locus_no);
	if ($refNo4reflink) {
	    my $reflinkObj = Reflink->new(dbh=>$dbh,
					  reference_no=>$refNo,
					  tab_name=>$tabNm,
					  primary_key=>$prikey,
					  primary_key_col=>$prikeyCol);
	    if ($reflinkObj) {
		eval { $reflinkObj->delete; };
		if ($@) {
		    print "An error occurred when deleting info from reflink table:$@", br;
		}
		else {
		    $dbh->commit;
		    print "The reflink entry for reference_no = $refNo, tab_name = '$tabNm', primary_key = '$prikey' and primary_key_col = '$prikeyCol' has been deleted from database.:$@", br;
		}
	    }
	}

    }
    else {
	my $refNo4reflink = Reflink->GetRefNoBYlocusNo(dbh=>$dbh,
				   locus_no=>$locusObj->locus_no);
	if (!$refNo4reflink) {
	    $self->insertRefLink($refNo, $tabNm, $prikey, $prikeyCol);
	}
    }
    return $refNo;

}

########################################################################
sub createCitation {
########################################################################
    my ($self, $year, $reftitle, $journal, $volume, $issue, $page, 
	$authorRef) = @_;
    my $citation = &CreateCitation(\$year, \$reftitle, \$journal, 
				   \$volume, \$issue, \$page, 
				   $authorRef);
    return $citation;	
}

########################################################################
sub insertLocusGeneInfo {
########################################################################
    my ($self, $locusNo, $refNo) = @_;
   
    eval {
	Locus_gene_info->Insert(dbh=>$dbh,
				binds=>{locus_no=>$locusNo,
				    reference_no=>$refNo,
				    literature_topic=>'Not yet curated'});
    };
    if ($@) {
	print "An error occurred when inserting new locus_gene_info into database:$@", br;
	$dbh->rollback;
    }
    else {
	print "The new locus_gene_info entry has been inserted into database.", br;
	$dbh->commit;
    }
}

########################################################################
sub insertRefLink {
########################################################################
    my ($self, $refNo, $tabNm, $prikey, $prikeyCol) = @_;

    my $refObj = Reflink->new(dbh=>$dbh,
			      reference_no=>$refNo,
			      tab_name=>$tabNm,
			      primary_key=>$prikey,
			      primary_key_col=>$prikeyCol);

    if ($refObj) { return; }

    eval {
	Reflink->Insert(dbh=>$dbh,
			literals=>{reflink_no=>'CGM_DDB.reflinkno_seq.nextval'},
			binds=>{reference_no=>$refNo,
				tab_name=>$tabNm,
				primary_key=>$prikey,
				primary_key_col=>$prikeyCol});
    };
    if ($@) {
	print "An error occurred when inserting new entry into reflink table:$@", br;
	$dbh->rollback;
    }
    else {
	print "The new reflink entry has been inserted into database.", br;
	$dbh->commit;
    }
}

########################################################################
sub insertAuthor{
########################################################################
    my ($self, $author) = @_;
    my $authorObj = Author->new(dbh=>$dbh,
				author_name=>$author);
    if ($authorObj) { return $authorObj->author_no; }
    eval {
	Author->Insert(dbh=>$dbh,
		       literals=>{author_no=>'CGM_DDB.authorno_seq.nextval'},
		       binds=>{author_name=>$author});
    };
    if ($@) {
	print "An error occurred when inserting author '$author' into database:$@", br;
	$dbh->rollback;
    }
    else {
	print "The author '$author' has been inserted into database.", br;
	$dbh->commit;
	my $authorObj = Author->new(dbh=>$dbh,
				    author_name=>$author);
	return $authorObj->author_no;
    }
}

########################################################################
sub insertAuthorEditor {
########################################################################
    my ($self, $authorNo, $refNo, $authorType, $order) = @_;
    eval {
	Author_editor->Insert(dbh=>$dbh,
			      binds=>{author_no=>$authorNo,
				  reference_no=>$refNo,
				  author_type=>$authorType,
				  author_order=>$order});
    };
    if ($@) {
	print "An error occurred when inserting author_editor info into database:$@", br;
	$dbh->rollback;
    }
    else {
	print "The author_editor info has been inserted into database.", br;
	$dbh->commit;
    }
}

########################################################################
sub insertPubType {
########################################################################
    my ($self, $refNo, $pubtype) = @_;
    eval {
	Publication_type->Insert(dbh=>$dbh,
				 binds=>{reference_no=>$refNo,
					 pub_type=>$pubtype});
    };
    if ($@) {
	print "An error occurred when inserting pub_type info into publication_type table:$@", br;
	$dbh->rollback;
    }
    else {
	print "The pub_type info has been inserted into database.", br;
	$dbh->commit;
    }
}


########################################################################
sub insertJournal {
########################################################################
    my ($self, $journal) = @_;
    if ($journal =~ /^[0-9]+$/) {
	my $journalObj = Journal->new(dbh=>$dbh,
				      journal_no=>$journal);
	if ($journalObj) {
	    return ($journalObj->journal_no, $journalObj->abbreviation);
	}
	else {
	    print "The journal_no = $journal is not found in database.",br;
	    return;
	}
    }
    my $journalObj = Journal->new(dbh=>$dbh,
  				  abbreviation=>$journal);
    if ($journalObj) {
 	return ($journalObj->journal_no, $journalObj->abbreviation);
    }
    my ($abbrev, $issn, $Jtitle) = &findFullJournalInfo($journal);
    eval {
 	Journal->Insert(dbh=>$dbh,
 			literals=>{journal_no=>'CGM_DDB.journalno_seq.nextval'},
 			binds=>{abbreviation=>$journal,
			        issn=>$issn,
			        full_name=>$Jtitle});
    };
    if ($@) {
 	print "An error occurred when inserting journal '$journal' into database:$@", br;
 	$dbh->rollback;
 	return ;
    }
    else {
 	print "The journal '$journal' has been inserted into journal table.", br;
 	$dbh->commit;
 	my $journalObj = Journal->new(dbh=>$dbh,
 				     abbreviation=>$journal);
 	return ($journalObj->journal_no, $journalObj->abbreviation);
    }
}

########################################################################
sub updateAliasTable {
########################################################################
    my ($self, $locusObj, $aliasList, $type) = @_;

    $aliasList =~ s/[;,:]/\|/g;
    if ($type eq 'Uniform') { 
	$aliasList =~ s/ //g;
    }

    my $aliasListDB;
    if ($type eq 'Uniform'){
	$aliasListDB = $locusObj->uniformAliasNameList;
     }
    elsif ($type eq 'Non-uniform'){
	$aliasListDB = $locusObj->nonUniformAliasNameList;
     }
    else {
	$aliasListDB = $locusObj->proteinNameAliasNameList;
    }
    
    if ("\U$aliasList" eq "\U$aliasListDB") { return; }

    my @aliasNm = split(/\|/, $aliasList);
    my @aliasNmDB = split(/\|/, $aliasListDB);

    my (%foundAlias, %foundAliasDB);
    foreach my $aliasNm (@aliasNm) {
	$foundAlias{"\U$aliasNm"}++;
    }	

    foreach my $aliasNm (@aliasNmDB) {
	$foundAliasDB{"\U$aliasNm"}++;
	if ($foundAlias{"\U$aliasNm"}) { next; }
	#### it is not found in the new alias list
	#### delete it from locus_alias table
	if ($type eq 'Uniform') {$aliasNm = uc($aliasNm);} 
	my $aliasObj = Alias->new(dbh=>$dbh,
				  alias_name=>$aliasNm,
				  alias_type=>$type);
	$self->deleteFromLocusAliasTable($locusObj->locus_no, 
					 $aliasObj->alias_no);
    }
    foreach my $aliasNm (@aliasNm) {
	if ($foundAliasDB{"\U$aliasNm"}) { next; }
	#### it id not found in the alias list for this locus 
        #### from database
	#### add this alias into alias table if it not exist yet
        #### add it to locus_alias table
	if ($type eq 'Uniform') {$aliasNm = uc($aliasNm);} 
	my $aliasObj = Alias->new(dbh=>$dbh,
				  alias_name=>$aliasNm,
				  alias_type=>$type);
	if (!$aliasObj) {
	    $aliasObj = $self->insertIntoAliasTable($aliasNm, $type);
	    if (!$aliasObj) { next;}
	}
	$self->insertIntoLocusAliasTable($locusObj->locus_no, 
					 $aliasObj->alias_no);
    }
}

########################################################################
sub insertIntoAliasTable {
########################################################################
    my ($self, $aliasNm, $alias_type) = @_;

    eval {
	Alias->Insert(dbh=>$dbh,
		      literals=>{alias_no=>'CGM_DDB.aliasno_seq.nextval'},
		      binds=>{alias_name=>$aliasNm,
			      alias_type=>$alias_type});
    };
    if ($@) {
	print "An error occurred when inserting new alias '$aliasNm' into database:$@", br;
	$dbh->rollback;
	return;
    }
    else {
	print "The new alias '$aliasNm' has been inserted into alias table.", br;
	$dbh->commit;
	my $aliasObj = Alias->new(dbh=>$dbh,
				  alias_name=>$aliasNm,
				  alias_type=>$alias_type);
	return $aliasObj;
    }
}

########################################################################
sub insertIntoLocusAliasTable {
########################################################################
    my ($self, $locusNo, $aliasNo) = @_;
    eval { 
	Locus_alias->Insert(dbh=>$dbh,
			    binds=>{locus_no=>$locusNo,
				    alias_no=>$aliasNo});
    };
    if ($@) {
	print "An error occurred when inserting new locus_alias entry for locus_no = $locusNo and alias_no = $aliasNo into database:$@", br;
	$dbh->rollback;
    }
    else {
	print "The new locus_alias entry for locus_no = $locusNo and alias_no = $aliasNo has been inserted into database.", br;
	$dbh->commit;
    }
}

########################################################################
sub deleteFromLocusAliasTable {
########################################################################
    my ($self, $locusNo, $aliasNo) = @_;
    my $locusAliasObj = Locus_alias->new(dbh=>$dbh,
					 locus_no=>$locusNo,
					 alias_no=>$aliasNo);
    eval { $locusAliasObj->delete; };
    if ($@) {
	print "An error occurred when deleting locus_alias entry for locus_no = $locusNo and alias_no = $aliasNo:$@", br;
	$dbh->rollback;
    }
    else {
	print "The locus_alias entry for locus_no = $locusNo and alias_no = $aliasNo has been deleted from database.", br;
	$dbh->commit;
    }
}

#######################################################################
sub updateFeatureTable {
#######################################################################
    my ($self, $featObj, $locusNo, $briefId) = @_;
    
    $featObj->updateLocus_no($locusNo);
    $featObj->updateBrief_id($briefId);
    eval { $featObj->enterUpdates; };
    if ($@) {
	print "An error occurred when updating feature table:$@", br;
	$dbh->rollback;
    }
    else {
	print "The feature table has been updated.", br;
	$dbh->commit;
    }
}

#######################################################################
sub updateLocusRelatedTables {
#######################################################################
    my ($self, $colleague_no, $chr) = @_;

    ###### update or insert locus, coll_locus table
    my $locusObj = Locus->new(dbh=>$dbh,
			      locus_name=>$self->{'_gene'});
    my $locusNo;
    if ($locusObj) {
	$locusNo = $locusObj->locus_no;
    }
    if ($locusNo) { ### gene update
	if ($self->{'_genedesc'}) {
	    $locusObj->updateName_description($self->{'_genedesc'});
	    eval { $locusObj->enterUpdates;};
	    if ($@) {
		print "An error occurred when updating name_description column for locus_no = $locusNo:$@", br;
		$dbh->rollback;
	    }
	    else {
		print "The name_description for locus ".font({-color=>'red'}, $self->{'_gene'})." in ".font({-color=>'red'}, "locus")." table has been updated.", p;
		$dbh->commit;
	    }
	}

	    if ($self->{'_description'}) {
	    $locusObj->updateDescription($self->{'_description'});
	    eval { $locusObj->enterUpdates;};
	    if ($@) {
		print "An error occurred when updating description column for locus_no = $locusNo:$@", br;
		$dbh->rollback;
	    }
	    else {
		print "The description for locus ".font({-color=>'red'}, $self->{'_gene'})." in ".font({-color=>'red'}, "locus")." table has been updated.", p;
		$dbh->commit;
	    }

	}
	###### insert into coll_locus table if it does not exist
	my $collLocusObj = Coll_locus->new(dbh=>$dbh,
					   locus_no=>$locusNo,
					   colleague_no=>$colleague_no);
	if (!$collLocusObj) {
	    $self->insertIntoCollLocusTable($locusNo, $colleague_no);
	}
	#########
	if (!$self->{'_stdNmCB'}) {
	    my $sth = $dbh->prepare( "
                 SELECT  add_months(sysdate, 12) 
                 FROM    dual
            ");
	    $sth->execute;
	    my $expire_date = $sth->fetchrow;
	    my $geneReservInfo = $locusObj->geneReservationInfo;
	    my ($reservNo, $others) = split(/\:/, $geneReservInfo);
	    if ($reservNo) {
		my $reservObj = Gene_reservation->new(dbh=>$dbh,
					reservation_no=>$reservNo);
		$reservObj->updateExpiration_date($expire_date); 
		eval { $reservObj->enterUpdates; };
		if ($@) {
		    print "An error occurred when updating gene_reservation table:$@", br;
		}
		else {
		    print "The ".font({-color=>'red'}, "gene_reservation")." tables have been updated.", p;
		    $dbh->commit;
		}

	    }
	}
	else {  ### make it standard gene name
	    eval { $locusObj->makeStdName; };
	    if ($@) {
		print "An error occurred when updating gene reservation entry in ".font({-color=>'red'}, "gene_reservation")." table for locus ".$self->{'_gene'}.":$@", br;
		$dbh->rollback;
	    }
	    else {
		print "The gene reservation entry in ".font({-color=>'red'}, "gene_reservation")." table for locus ".$self->{'_gene'}." has been updated.",br; 
		$dbh->commit;
	    }
	}
    }
    else { #### gene registry
	eval {
	    Locus->Insert(dbh=>$dbh,
			  literals=>{locus_no=>'CGM_DDB.locusno_seq.nextval'},
			  binds=>{locus_name=>$self->{'_gene'}, 
                                  chromosome=>$chr,
				  name_description=>$self->{'_genedesc'},
			          description=>$self->{'_description'}});
	};
	if ($@) {
	    print "An error occurred when inserting new locus entry for locus_name = ".$self->{'_gene'}." into database:$@", br;
	    $dbh->rollback;
	}
	else {
	    print "The new locus entry for locus_name=".font({-color=>'red'}, $self->{'_gene'})." has been inserted into database.", p;
	    $dbh->commit;
	    $locusObj = Locus->new(dbh=>$dbh,
				   locus_name=>$self->{'_gene'});
	    $locusNo = $locusObj->locus_no;
	    $self->insertIntoCollLocusTable($locusNo, $colleague_no);
	    $self->assigndictyBaseid4newLocus($locusNo);

	}
	##########
	my $sth = $dbh->prepare( "
            SELECT  add_months(sysdate, 12) 
            FROM    dual
        ");
	$sth->execute;
	my $expire_date = $sth->fetchrow;
	if (!$self->{'_stdNmCB'}) {
	    eval {
		Gene_reservation->Insert(dbh=>$dbh,
		     literals=>{reservation_no=>'CGM_DDB.resvno_seq.nextval',
				reservation_date=>'SYSDATE',
			        locus_no=>'CGM_DDB.locusno_seq.currval'},
		     binds=>{expiration_date=>$expire_date,
			     is_standardized=>'N'});
	    };
	    if ($@) {
		print "An error occurred when inserting new entry into gene_reservation table:$@", br;
	    }
	    else {
		print "The ".font({-color=>'red'}, "gene_reservation")." table have been updated.", p;
		$dbh->commit;
	    }
	}
	else { ### make it standard name
	    eval {
		Gene_reservation->Insert(dbh=>$dbh,
		     literals=>{reservation_no=>'CGM_DDB.resvno_seq.nextval',
				reservation_date=>'SYSDATE',
				date_standardized=>'SYSDATE',
			        locus_no=>'CGM_DDB.locusno_seq.currval'},
		     binds=>{expiration_date=>$expire_date,
			     is_standardized=>'Y'});
	    };
	    if ($@) {
		print "An error occurred when inserting new entry into gene_reservation table:$@", br;
	    }
	    else {
		print "The ".font({-color=>'red'}, "gene_reservation")." table have been updated.", p;
		$dbh->commit;
	    }
	}
    }
    return $locusObj;
}

#######################################################################
sub assigndictyBaseid4newLocus {
########################################################################
    my ($self, $locusNo) = @_;
    if (param('assigndictyBaseidCB') !~ /on/i) {  return; }

    print "okay, I am here",p;

    my $sth = $dbh->prepare("
         SELECT CGM_DDB.dictyBaseidno_seq.nextval
         FROM   dual
    ");

    $sth->execute;

    my $dictyBaseidNo = $sth->fetchrow;

    my $dictyBaseid = $dictyBaseidNo;

    for (my $i = 0; $i < 7-length($dictyBaseidNo); $i++) {
	$dictyBaseid = "0".$dictyBaseid;
    }

    $dictyBaseid = "S".$dictyBaseid;

    eval {
	dictyBaseid->Insert(dbh=>$dbh,
		      literals=>{dictyBaseid_no=>'CGM_DDB.dictyBaseidno_seq.currval'},
		      binds=>{dictyBaseid=>$dictyBaseid,
			      dictyBaseid_type=>'Primary',
			      tab_name=>'LOCUS',
			      primary_key=>$locusNo});
    };
    if ($@) {
	print "An error occurred when inserting new dictyBaseid entry for locus_no = $locusNo into database:$@", p;
	$dbh->rollback;
    }
    else {
	print "The new dictyBaseid entry for locus_no = $locusNo has been inserted into database.", p;
	$dbh->commit;
    }
}

#######################################################################
sub insertIntoCollLocusTable {
#######################################################################
    my ($self, $locusNo, $colleague_no) = @_; 
    eval {
	Coll_locus->Insert(dbh=>$dbh,
			   literals=>{locus_no=>$locusNo},
			   binds=>{colleague_no=>$colleague_no});
    };
    if ($@) {
	print "An error occurred when inserting new coll_locus entry for locus_no = $locusNo and colleague_no = $colleague_no:$@", br;
	$dbh->rollback;
    }
    else {
	print "The ".font({-color=>'red'}, "coll_locus")." table has been updated.", p;
	$dbh->commit;
    }
}

#######################################################################
sub processFile {
#######################################################################
    my ($self) = shift;

    &printStartPage($self->database, $self->title, $self->help);

    if (!$self->id) { $self->{'_id'} = param('id'); }

    if (param('delayCB') =~ /on/i) { 
	&delayFile($self->id, $dir, param('delayComment'), $self->user);
    }
    elsif (param('deleteCB') =~ /on/i) {
	&deleteFile($dir, $self->id.".GeneRegistry4ace");
    }
    &returnToCuratorCentral($self->user, $configUrl->dictyBaseCGIRoot."$dblink/curatorLogin");
    &printEndPage;
}

#######################################################################
sub populateInfo {
#######################################################################
    my ($self, $authorRef) = @_;
    my $id = param('id');
    if (-e "$dir/$id.GeneRegistry4ace") {
	open(IN, "$dir/$id.GeneRegistry4ace") || die "commitGeneRegistry: Can't open '$id.GeneRegistry4ace' for reading:$!\n";
    }
    else {
	print "There is no file under $dir with the id ".font({-color=>'red'}, $id), p;
	exit;
    }
    while(<IN>) {
	    chomp;
	    my ($name, $value);
	    if (/^([^ ]+) \"(.+)\"$/) {
		$name = $1;
		$value = $2;
	    }
	    if ($name =~ /^Type/i) {
		$self->{'_type'} = $value;
	    }
	    elsif ($name =~ /LName/i) {
		$self->{'_lname'} = $value;
	    }
	    elsif ($name =~ /FName/i) {
		$self->{'_fname'} = $value;
	    }
	    elsif ($name =~ /Internet/i) {
		$self->{'_email'} = $value;
		$self->{'_email'} =~ s/[,;]/ /g;
	    }
	    elsif ($name =~ /Locus/i) {
		$self->{'_gene'} = $value;
	    }
	    elsif ($name =~ /Description/i) {
		$self->{'_genedesc'} = $value;
	    }
	    elsif ($name =~ /Full_Desc/i) {
		$self->{'_description'} = $value;
	    }
	    elsif ($name =~ /ORF_name/i) {
		$self->{'_orf'} = $value;
	    }
	    elsif ($name =~ /Alias/i) {
		$self->{'_uniformAliasName'} = $value;
	    }
	    elsif ($name =~ /Gene_product/i) {
		$self->{'_geneproduct'} = $value;
	    }
	    elsif ($name =~ /Null_Phenotype/i) {
		$self->{'_phenotype'} = $value;
		if ($value !~ /^Null:/i) {
		    $self->{'_phenotype'} = "Null: ".$value;
		}    
	    }
	    elsif ($name =~ /Other_Phenotype/i) {
		if ($self->{'_phenotype'}) {
		    $self->{'_phenotype'} .= ".  ";
		}
		$self->{'_phenotype'} .= "Other phenotypes: ".$value;
	    }
	    elsif ($name =~ /Author/i) {
		push(@$authorRef, $value);
	    }
	    elsif ($name =~ /Title/i) {
		$self->{'_reftitle'} = $value;
	    }
	    elsif ($name =~ /Journal/i) {
		$self->{'_journal'} = $value;
	    }
	    elsif ($name =~ /Volume/i) {
		$self->{'_volume'} = $value;
	    }
	    elsif ($name =~ /Page/i) {
		$self->{'_page'} = $value;
	    }
	    elsif ($name =~ /Year/i) {
		$self->{'_year'} = $value;
	    }
	    elsif ($name =~ /PubMed/i) {
		$self->{'_pubmed'} = $value;
	    }
	    elsif ($name =~ /ref_type/i) {
		$self->{'_reftype'} = $value;
	    }
	    elsif ($name =~ /Comment/i) {
		$self->{'_comment'} = $value;
	    }
    }
    if ($self->{'_orf'}) {
	$dbh = &ConnectToDatabase($self->database);
	my $featObj = Feature->new(dbh=>$dbh,
				   feature_name=>$self->{'_orf'});
	if (!$featObj) { 
	    $self->{'_warning'} = "No ORF found in database with name ".$self->{'_orf'};
	}
	else {
	    $self->{'_briefId'} = $featObj->brief_id;
	}
	if ($featObj && $featObj->locus_name && 
	       uc($featObj->locus_name) ne uc($self->{'_gene'})) {
	    $self->{'_warning'} = "The ORF name (".$self->{'_orf'}.") is already named in our database. Its gene name is (".$featObj->locus_name.").";
	}
    }
    close(IN);
}

########################################################################
sub processData {
########################################################################
    my ($self, $authorRef) = @_;
    
    $self->{'_lname'} = param('lname');
    $self->{'_fname'} = param('fname');
    $self->{'_email'} = param('email');
    $self->{'_type'} = param('type');
    $self->{'_stdNmCB'} = param('stdNmCB');

    my $value = param('gene');
    &DeleteUnwantedChar(\$value);
    $self->{'_gene'} = $value;
 
    $value = param('genedesc');
    &DeleteUnwantedChar(\$value);
    $self->{'_genedesc'} = $value;

    $value = param('description');
    &DeleteUnwantedChar(\$value);
    $self->{'_description'} = $value;
 
    $value = param('orf');
    &DeleteUnwantedChar(\$value);
    $self->{'_orf'} = $value;

    $value = param('uniformAliasName');
    &DeleteUnwantedChar(\$value);
    $self->{'_uniformAliasName'} = $value;

    $value = param('nonUniformAliasName');
    &DeleteUnwantedChar(\$value);
    $self->{'_nonUniformAliasName'} = $value;

    $value = param('proteinNameAlias');
    &DeleteUnwantedChar(\$value);
    $self->{'_proteinNameAlias'} = $value;

    $value = param('geneproduct');
    &DeleteUnwantedChar(\$value);
    $self->{'_geneproduct'} = $value;

    $value = param('phenotype');
    &DeleteUnwantedChar(\$value);
    $self->{'_phenotype'} = $value;

    $value = param('briefId');
    &DeleteUnwantedChar(\$value);
    $self->{'_briefId'} = $value;

    for (my $i = 1; $i <= 12; $i++) {
	$value = param("author$i");
	&DeleteUnwantedChar(\$value);
	if ($value) {
	    push(@$authorRef, $value);
	}
    }
    $value = param('reftitle');
    &DeleteUnwantedChar(\$value);
    $self->{'_reftitle'} = $value;

    $value = param('journal');
    &DeleteUnwantedChar(\$value);
    $self->{'_journal'} = $value;
 
    $value = param('volume');
    &DeleteUnwantedChar(\$value);
    $self->{'_volume'} = $value;

    $value = param('page');
    &DeleteUnwantedChar(\$value);
    $self->{'_page'} = $value;

    $value = param('year');
    &DeleteUnwantedChar(\$value);
    $self->{'_year'} = $value;

    $value = param('pubmed');
    &DeleteUnwantedChar(\$value);
    $self->{'_pubmed'} = $value;
}

########################################################################
sub updateHeader {
########################################################################
    return Tr({-bgcolor=>'#CCCCCC'},
	      td({-width=>'20%'}).
	      th({-width=>'40%',
		  -bgcolor=>'#CCCCCC'},
		 "prior entry").
	      th({-width=>'40%',
		  -bgcolor=>'#a4abc2'},
		 "new entry"));
}

########################################################################
sub arrow {
########################################################################
    return td({-align=>'left'},
	      img({-src=>$configUrl->dictyBaseImages."delta.gif",
		   -width=>"27",
		   -height=>"18"}));

}

##########################################################################
sub geneName {
##########################################################################
    my($self, $locusObj) = @_;
    if ($self->{'_type'} !~ /update/i) {
	return Tr(td({-width=>'120',
		      -bgcolor=>'#ACBDCC'},
		     "Proposed Gene Name").
		  td(textfield(-name=>'gene',
			       -value=>$self->{'_gene'},
			       -size=>'40')));
    }   
    my $old;
    if ($locusObj) {
	$old = $locusObj->locus_name;
    }
    my $arrow;
    if (uc($self->{'_gene'}) ne uc($old)) { $arrow = "yes"; }
    if (!$old) { $old = br; } 
    my $entry = td({-width=>'120',
		    -bgcolor=>'#ACBDCC'},
		   "Proposed Gene Name").
		td({-width=>'250',
		    -bgcolor=>"#d8d8d8"},
		   $old).
		td(textfield(-name=>'gene',
			     -value=>$self->{'_gene'},
			     -size=>'40'));
    if ($arrow) { $entry .= &arrow; }
    else { $entry .= td(br); }
    return Tr($entry);
}

##########################################################################
sub geneFullDesc {
##########################################################################
    my ($self, $locusObj) = @_;
    if ($self->{'_type'} !~ /update/i) {
	return Tr(td({-width=>'120',
		      -bgcolor=>'#ACBDCC'},
		     "Description").
		  td(textarea(-name=>'description', 
			      -value=>$self->{'_description'},
			      -rows=>'3',
			      -columns=>'40')));
    }
    my $old;
    if ($locusObj) {
	$old = $locusObj->description;
    }
    my $arrow;
    if ($self->{'_description'} ne $old) { $arrow = "yes"; }
    if (!$old) { $old = br; } 
    my $entry = td({-width=>'120',
		      -bgcolor=>'#ACBDCC'},
		     "Description").
		td({-width=>'250',
		    -bgcolor=>"#d8d8d8"},
		   $old).
		td(textarea(-name=>'description', 
			    -value=>$self->{'_description'},
			    -rows=>'3',
			    -columns=>'40'));
    if ($arrow) { $entry .= &arrow; }
    else { $entry .= td(br); }
    return Tr($entry);
}

##########################################################################
sub geneDesc {
##########################################################################
    my ($self, $locusObj) = @_;
    if ($self->{'_type'} !~ /update/i) {
	return Tr(td({-width=>'120',
		      -bgcolor=>'#ACBDCC'},
		     "Description of Gene Name Acronym ").
		  td(textarea(-name=>'genedesc', 
			      -value=>$self->{'_genedesc'},
			      -rows=>'3',
			      -columns=>'40')));
    }
    my $old;
    if ($locusObj) {
	$old = $locusObj->name_description;
    }
    my $arrow;
    if ($self->{'_genedesc'} ne $old) { $arrow = "yes"; }
    if (!$old) { $old = br; } 
    my $entry = td({-width=>'120',
		      -bgcolor=>'#ACBDCC'},
		     "Description of Gene Name Acronym ").
		td({-width=>'250',
		    -bgcolor=>"#d8d8d8"},
		   $old).
		td(textarea(-name=>'genedesc', 
			    -value=>$self->{'_genedesc'},
			    -rows=>'3',
			    -columns=>'40'));
    if ($arrow) { $entry .= &arrow; }
    else { $entry .= td(br); }
    return Tr($entry);
}

##########################################################################
sub orfName {
##########################################################################
    my($self, $locusObj) = @_;
    
    my $typeBox;

    if ($self->{'_orf'}) {

	my $featObj = Feature->new(dbh=>$dbh,
				   feature_name=>$self->{'_orf'});

	my $types = $featObj->featureTypeList;

	my $type = $types;

	$type =~ s/\|?ORF\|?//;

	$type =~ tr/A-Z/a-z/;

	$types =~ s/\|/, /g;

	$typeBox = Tr(td({-width=>'120',
			   -bgcolor=>'#ACBDCC'},
			  "ORF Type(s)").
		       td(a({-href=>$configUrl->dictyBaseHelp."glossary.html#$type",
			     -target=>'infowin'}, $types)));

    }

    if ($self->{'_type'} !~ /update/i) {

	return Tr(td({-width=>'120',
		      -bgcolor=>'#ACBDCC'},
		     "ORF Name").
		  td(textfield(-name=>'orf',
			       -value=>$self->{'_orf'},
			       -size=>'40'))).$typeBox;
	
    }
    my $old;
    if ($locusObj) {
	$old = $locusObj->featureNameList;
    }
    my $arrow;
    if (uc($self->{'_orf'}) ne uc($old)) { $arrow = "yes"; }
    if (!$old) { $old = br; } 
    my $entry = td({-width=>'120',
		    -bgcolor=>'#ACBDCC'},
		   "ORF Name").
		td({-width=>'250',
		    -bgcolor=>"#d8d8d8"},
		   $old).
		td(textarea(-name=>'orf', 
			    -value=>$self->{'_orf'},
			    -rows=>'3',
			    -columns=>'40'));
    if ($arrow) { $entry .= &arrow; }
    else { $entry .= td(br); }
    return Tr($entry).$typeBox;

}

##########################################################################
sub uniformAliasName {
##########################################################################
    my ($self, $locusObj) = @_;
    my $guess_alias_type = $self->{'_uniformAliasName'};
    $guess_alias_type =~ s/[;,:]/\|/g;
    my @guess_aliases = split(/\|/, $guess_alias_type);
    my ($uniformList, $nonUniformList);
    foreach my $alias (@guess_aliases) {
	if ($alias =~ /^(\w){3}(\d)+$/i) {
	    if ($uniformList) {
		$uniformList .= '|' . $alias;
	    }
	    else {
		$uniformList = $alias;
	    }
	}
	else {
	    if ($nonUniformList) {
		$nonUniformList .= '|' . $alias;
	    }
	    else {
		$nonUniformList = $alias;
	    }
	}
    }     
    $self->{'_uniformAliasName'} = $uniformList;
    $self->{'_nonUniformAliasName'} = $nonUniformList;
    if ($self->{'_type'} !~ /update/i) {
	return Tr(td({-width=>'120',
		      -bgcolor=>'#ACBDCC'},
		     "Uniform Alias Name(s)".br."Eg: PHM5|EPP1").
		  td(textfield(-name=>'uniformAliasName',
			       -value=>$self->{'_uniformAliasName'},
			       -size=>'40')));
    }
    my $old;
    if ($locusObj) {
	$old = $locusObj->uniformAliasNameList;
    }
    my $arrow;
    if (uc($self->{'_uniformAliasName'}) ne uc($old)) { $arrow = "yes"; }
    if (!$old) { $old = br; } 
    my $entry = td({-width=>'120',
		      -bgcolor=>'#ACBDCC'},
		     "Uniform Alias Name(s)".br."Eg: PHM5|EPP1").
		td({-width=>'250',
		    -bgcolor=>"#d8d8d8"},
		   $old).
		td(textarea(-name=>'uniformAliasName', 
			    -value=>$self->{'_uniformAliasName'},
			    -rows=>'3',
			    -columns=>'40'));
    if ($arrow) { $entry .= &arrow; }
    else { $entry .= td(br); }
    return Tr($entry);
}

##########################################################################
sub nonUniformAliasName {
##########################################################################
    my ($self, $locusObj) = @_;
    if ($self->{'_type'} !~ /update/i) {
	return Tr(td({-width=>'120',
		      -bgcolor=>'#ACBDCC'},
		     "Non-uniform Alias Name(s)".br."Eg: Endo-1|EnodoPP1").
		  td(textfield(-name=>'nonUniformAliasName',
			       -value=>$self->{'_nonUniformAliasName'},
			       -size=>'40')));
    }
    my $old;
    if ($locusObj) {
	$old = $locusObj->nonUniformAliasNameList;
    }
    my $arrow;
    if (uc($self->{'_nonUniformAliasName'}) ne uc($old)) { $arrow = "yes"; }
    if (!$old) { $old = br; } 
    my $entry = td({-width=>'120',
		      -bgcolor=>'#ACBDCC'},
		     "Non-uniform Alias Name(s)".br."Eg: Endo-1|EnodoPP1").
		td({-width=>'250',
		    -bgcolor=>"#d8d8d8"},
		   $old).
		td(textarea(-name=>'nonUniformAliasName', 
			    -value=>$self->{'_nonUniformAliasName'},
			    -rows=>'3',
			    -columns=>'40'));
    if ($arrow) { $entry .= &arrow; }
    else { $entry .= td(br); }
    return Tr($entry);
}

##########################################################################
sub proteinNameAlias {
##########################################################################
    my ($self, $locusObj) = @_;
    if ($self->{'_type'} !~ /update/i) {
	return Tr(td({-width=>'120',
		      -bgcolor=>'#ACBDCC'},
		     "Protein name Alias Name(s)".br."Eg: EndoPolyPase").
		  td(textfield(-name=>'proteinNameAlias',
			       -value=>$self->{'_proteinNameAlias'},
			       -size=>'40')));
    }
    my $old;
    if ($locusObj) {
	$old = $locusObj->proteinNameAliasNameList;
    }
    my $arrow;
    if (uc($self->{'_proteinNameAlias'}) ne uc($old)) { $arrow = "yes"; }
    if (!$old) { $old = br; } 
    my $entry = td({-width=>'120',
		      -bgcolor=>'#ACBDCC'},
		     "Protein name Alias Name(s)".br."Eg: EndoPolyPase").
		td({-width=>'250',
		    -bgcolor=>"#d8d8d8"},
		   $old).
		td(textarea(-name=>'proteinNameAlias', 
			    -value=>$self->{'_proteinNameAlias'},
			    -rows=>'3',
			    -columns=>'40'));
    if ($arrow) { $entry .= &arrow; }
    else { $entry .= td(br); }
    return Tr($entry);
}

##########################################################################
sub geneProduct {
##########################################################################
    my ($self, $locusObj) = @_;
    if ($self->{'_type'} !~ /update/i) {
	return Tr(td({-width=>'120',
		      -bgcolor=>'#ACBDCC'},
		     "Gene Product or".br."gene_product_no").
		  td(textfield(-name=>'geneproduct',
			       -value=>$self->{'_geneproduct'},
			       -size=>'40')));
    }
    my $old;
    if ($locusObj) {
	$old = $locusObj->gene_product;
	$old =~ s/\t/; /g;
    }
    my $arrow;
    if (uc($self->{'_geneproduct'}) ne uc($old)) { $arrow = "yes"; }
    if (!$old) { $old = br; } 
    my $entry = td({-width=>'120',
		      -bgcolor=>'#ACBDCC'},
		     "Gene Product or".br."gene_product_no").
		td({-width=>'250',
		    -bgcolor=>"#d8d8d8"},
		   $old).
		td(textarea(-name=>'geneproduct', 
			    -value=>$self->{'_geneproduct'},
			    -rows=>'3',
			    -columns=>'40'));
    if ($arrow) { $entry .= &arrow; }
    else { $entry .= td(br); }
    return Tr($entry);
}

##########################################################################
sub phenotype {
##########################################################################
    my ($self, $locusObj) = @_;
    if ($self->{'_type'} !~ /update/i) {
	return Tr(td({-width=>'120',
		      -bgcolor=>'#ACBDCC'},
		     "Phenotypes").
		  td(textfield(-name=>'phenotype',
			       -value=>$self->{'_phenotype'},
			       -size=>'40')));
    }
    my $old;
    if ($locusObj) {
	$old = $locusObj->freeTextPhenotype;
    }
    my $arrow;
    if (uc($self->{'_phenotype'}) ne uc($old)) { $arrow = "yes"; }
    if (!$old) { $old = br; } 
    my $entry = td({-width=>'120',
		      -bgcolor=>'#ACBDCC'},
		     "Phenotypes").
		td({-width=>'250',
		    -bgcolor=>"#d8d8d8"},
		   $old).
		td(textarea(-name=>'phenotype', 
			    -value=>$self->{'_phenotype'},
			    -rows=>'3',
			    -columns=>'40'));
    if ($arrow) { $entry .= &arrow; }
    else { $entry .= td(br); }
    return Tr($entry);
}

##########################################################################
sub briefId {
##########################################################################
    my ($self, $locusObj) = @_;
    if ($self->{'_type'} !~ /update/i) {
	return Tr(td({-width=>'120',
		      -bgcolor=>'#ACBDCC'},
		     "Brief_id for ".br."feature table").
		  td(textfield(-name=>'briefId',
			       -value=>$self->{'_briefId'},
			       -size=>'40')));
    }
    return Tr(td({-width=>'120',
		  -bgcolor=>'#ACBDCC'},
		  "Brief_id for ".br."feature table").
	      td({-width=>'250',
		  -bgcolor=>"#d8d8d8"},
		 $self->{'_briefId'}).
	      td(textfield(-name=>'briefId',
			   -value=>$self->{'_briefId'},
			   -size=>'40')).
	      td(br));
}

########################################################################
sub err_report {
########################################################################
    my ($self, $err) = @_;

    &printStartPage($self->database, $self->title, $self->help);
   
    print b($err);
    
    &printEndPage;

    exit;
}

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









