#!/usr/bin/perl
package ReferenceUserPage_base;

#######################################################################
##### Author :	Shuai Weng
##### Date   :  Feb. 2001
##### Description : This package contains all necessary methods for
#####               displaying dictyBase curated paper to user.
#####
#######################################################################
use strict;
use DBI;
use CGI qw/:all/;
use CGI::Carp qw(fatalsToBrowser);
use dicty::UI::Page;

use lib "/usr/local/dicty/www_dictybase/db/lib/dictyBase/Objects";
use Reference;
use GeneInfo;
use dictyBaseid;
use Community_annotation;
use TextSearch;
use ConfigURLdictyBase;

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

use lib "/usr/local/dicty/www_dictybase/db/lib/dictyBase";
use dictyBaseObject;
use FormatdictyBase qw(Divider75);
use dictyBaseCentralMod qw(:formatPage :getInfo :navigateInfo);

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

our $dbh;
our $dblink;
our $configUrl;

#######################################################################
sub new {    ############ constructor ###############################
#######################################################################

    my ( $type, %args ) = @_;

    my $self = {};
    bless $self, $type;

    $self->{'_database'} = $args{'database'};
    $self->{'_help'}
        = defined( $args{'help'} ) ? $args{'help'} : "Paper.html";
    $self->{'_refNo'}   = $args{'refNo'};
    $self->{'_pubmed'}  = $args{'pubmed'};
    $self->{'_ACEname'} = $args{'ACEname'};
    return $self;
}

sub help     { $_[0]->{_help} }
sub database { $_[0]->{_database} }
sub title    { $_[0]->{_title} }
sub refNo    { $_[0]->{_refNo} }
sub pubmed   { $_[0]->{_pubmed} }

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

######################################################################
sub start {
######################################################################

    my ($self) = @_;
    $configUrl = ConfigURLdictyBase->new;
    $dblink    = $configUrl->dblink( $self->database );
    if ( param('submit') ) {
        if ( param('parameter') =~ /Papers in dictyBase/i ) {
            $dbh = &ConnectToDatabase( $self->database );
            $self->{'_title'} = "dictyBase Curated Papers";
            $self->paperSearch;
            exit;
        }
        elsif ( param('parameter') =~ /Colleagues in dictyBase/i ) {
            my $author = param('author');
            $author =~ s/^([^ ]+) .*$/$1/;
            print "location: ", $configUrl->dictyBaseCGIRoot,
                "$dblink/colleague/colleagueSearch?lname=$author\n";
            print "Content-type: text/html\n\n";
            exit;
        }
        else {
            my $author = param('author');
            $author =~ s/ /\+/g;
            print "location: ", $configUrl->ncbiUrlRoot2,
                "query?form=4&db=m&term=$author&dispmax=20&relentrezdate=No+Limit\n";
            print "Content-type: text/html\n\n";
            exit;
        }
    }
    if ( param('author') ) {
        $dbh = &ConnectToDatabase( $self->database );
        $self->{'_title'} = "dictyBase Curated Papers";
        $self->paperSearch;
        exit;
    }
    $dbh = &ConnectToDatabase( $self->database );
    if ( $self->{'_ACEname'} ) {
        my $refNo = $self->getRefNo4AceNm;
        if ( !$refNo ) {
            my $aceNm = $self->{'_ACEname'};
            print "location: ", $configUrl->breadServerRoot,
                "cgi-bin/dbrun/SacchDB?find+AcePaper+$aceNm\n";
            print "Content-type: text/html\n\n";
            exit;
        }
        $self->{'_refNo'} = $refNo;
    }
    if ( !$self->refNo && !$self->pubmed ) {
        $self->{'_title'} = "Reference Search";
        $self->printEntryForm;
        exit;
    }
    $self->{'_title'} = "dictyBase Curated Paper";
    my $refObject;
    if ( $self->refNo ) {
        $refObject = Reference->new(
            dbh          => $dbh,
            reference_no => $self->refNo
        );
        if ( !$refObject ) {
            $self->err_report( "The reference_no you entered ("
                    . $self->refNo
                    . ") is not found in database. Please correct it and try again."
            );
            exit;
        }
    }
    else {
        $refObject = Reference->new(
            dbh    => $dbh,
            pubmed => $self->pubmed
        );
        if ( !$refObject->reference_no ) {
            $self->err_report( "The pubmed id you entered ("
                    . $self->pubmed
                    . ") is not found in database. Please correct it and try again."
            );
            exit;
        }
    }
    if ( !param('type') ) {
        $self->displayPaper($refObject);
    }
    else {
        $self->displayCommentErratum($refObject);
    }
}

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

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

    print center(
        table(
            {   -border      => 3,
                -cellpadding => 4,
                -cellspacing => 4
            },

            Tr( { -align => 'LEFT' },
                [   td( {   -bgcolor => "#b7d8e4",
                            -width   => 350
                        },
                        table(
                            { -border => 0 },
                            Tr( { align => 'LEFT' },
                                [   td( start_form, "Enter Reference_no : " )
                                        . td(
                                        textfield(
                                            -name => 'refNo',
                                            -size => '15'
                                        )
                                        )
                                ]
                                )
                                . Tr(
                                { align => 'CENTER' },
                                [   td( font( { -color => 'red' }, b("OR") )
                                    )
                                ]
                                )
                                . Tr(
                                { align => 'LEFT' },
                                [   td("Enter pubmed id : ")
                                        . td(
                                        textfield(
                                            -name => 'pubmed',
                                            -size => '15'
                                        )
                                        )
                                ]
                                )
                                . Tr(
                                { align => 'LEFT' },
                                [ td( submit, reset, end_form ) ]
                                )
                        )
                    )
                ]
            )
        )
    );

    &printEndPage;
}

########################################################################
sub displayPaper {
########################################################################
    my ( $self, $refObject ) = @_;
    my $title = $self->title;
    print dicty::UI::Page->Header();
    print qq{<div
                style="font-size:25px;font-weight:bold;margin:10px auto 20px
                            auto;width:20%;"> $title
                                        </div>};

    #&printStartPage($self->database, $self->title, $self->help);
    $self->pageContents($refObject);
    print &Divider75;
    $self->citation($refObject);
    $self->abstract($refObject);
    $self->geneList($refObject);

    $self->communityAnnotation($refObject);

    $self->authorSearch($refObject);
    print &Divider75;
    $self->pageContents($refObject);
    #&printEndPage;
    print dicty::UI::Page->Footer();
}

########################################################################
sub displayCommentErratum {
########################################################################
    my ( $self, $refObject ) = @_;

    &printStartPage( $self->database, $self->title, $self->help );
    $self->citation( $refObject, "comment" );
    my $list;
    foreach my $rowRef ( @{ $refObject->relatedRefArrayRef } ) {
        my ( $refNo, $pubtype ) = @$rowRef;
        my $thisRefObject = Reference->new(
            dbh          => $dbh,
            reference_no => $refNo
        );
        my $citation = $self->citation( $thisRefObject, "list" );
        $list .= Tr(
            td( {   -width  => '90%',
                    -valign => 'top'
                },
                $citation
                )
                . td(
                {   -width  => '10%',
                    -valign => 'top'
                },
                $pubtype
                )
        );
    }
    print table(
        {   -align       => 'center',
            -border      => '0',
            -cellpadding => '3',
            -cellspacing => '3',
            -width       => '100%'
        },
        Tr( { -bgcolor => '#a4abc2' },
            th(       { -align => 'left' }, "Reference" )
                . th( { -align => 'left' }, "Type" )
            )
            . $list
    );
    &printEndPage;
}

########################################################################
sub paperSearch {
########################################################################
    my ($self) = @_;
    my $arrayRef = $self->getRefInfoBYauthor( param('author') );
    if ( @$arrayRef == 1 ) {
        my $rowRef = @$arrayRef[0];
        my ( $refNo, $citation, $year ) = @$rowRef;
        my $refObject = Reference->new(
            dbh          => $dbh,
            reference_no => $refNo
        );
        $self->displayPaper($refObject);
        exit;
    }
    &printStartPage( $self->database, $self->title, $self->help );
    my @ref   = reverse(@$arrayRef);
    my $num   = @ref;
    my $tsObj = TextSearch->new(
        dbh   => $dbh,
        query => param('author')
    );
    my $authorNum = $tsObj->authorNum;
    print center(
        b(        "There are "
                . font( { -color => 'red' }, $authorNum )
                . " authors associated with "
                . font( { -color => 'red' }, $num )
                . " papers found for author like "
                . font( { -color => 'red' }, param('author') )
                . " in database"
        )
        ),
        p;
    my $rows;

    foreach my $rowRef (@ref) {
        my ( $refNo, $citation, $year ) = @$rowRef;
        my $thisRefObj = Reference->new(
            dbh          => $dbh,
            reference_no => $refNo
        );
        my $authorList = $thisRefObj->authorList;
        my $author     = param('author');
        $author =~ s/[\*\%]//g;

        $authorList
            =~ s/($author)/<B style="color:black;background-color:#ffff66">$1<\/b>/gi;
        $citation = $self->citation( $thisRefObj, "reflist" );

        $rows .= Tr( td($authorList) . td($citation) );
    }
    print table(
        {   -align  => 'center',
            -border => '3'
        },
        Tr( { -align => 'center' }, th("Author(s)") . th("Citation") ) . $rows
        ),
        p;
    &printEndPage;
}

########################################################################
sub citation {
########################################################################
    my ( $self, $refObject, $type ) = @_;
    my $citation;
    if ( $type =~ /(list|comment)/i ) {
        $citation = $refObject->formatedCitation4erratum;
    }
    elsif ($type) {
        $citation = $refObject->formatedCitation;
    }
    else {
        $citation = $refObject->formatedCitationWithFullAuthor;
    }
    ###
    if ( !$type ) {
        print table(
            {   -align       => 'center',
                -border      => '0',
                -cellpadding => '3',
                -cellspacing => '3',
                -width       => '600'
            },
            Tr( td( font( { -size => '+1' }, $citation ) ) )
        );
    }
    elsif ( $type =~ /comment/i ) {
        print table(
            {   -align       => 'center',
                -border      => '0',
                -cellpadding => '3',
                -cellspacing => '3',
                -width       => '100%'
            },
            Tr( th( {   -valign  => 'top',
                        -bgcolor => "#a4abc2"
                    },
                    "Comments" . br "& Errata" . br . "relevant to:"
                    )
                    . td( font( { -size => '+1' }, $citation ) )
            )
        );
    }
    else { return $citation; }
}

########################################################################
sub abstract {
########################################################################
    my ( $self, $refObject ) = @_;
    my $abstract        = $refObject->abstract;
    my $status          = $refObject->status;
    my $pubtypeArrayRef = $refObject->pubtypeArrayRef;
    my $source          = $refObject->ref_source;
    my $pubmed          = $refObject->pubmed;
    my $colspanNum      = 4;
    my $abstractContent
        = td( { -align => 'right' }, font( { -size => '-1' }, b("Status:") ) )
        . td( { -align => 'left' },  font( { -size => '-1' }, $status ) );

    #replace 'YPD' with 'Pubmed' as ref_source
    if ( $source eq 'YPD' ) {
        $source = 'PubMed';
    }

    if ( $$pubtypeArrayRef[0] ) {
        my $pubtype;
        foreach my $rowRef (@$pubtypeArrayRef) {
            my ($type) = @$rowRef;
            $pubtype .= $type . "|";
        }
        $pubtype =~ s/\|$//;
        $colspanNum += 2;
        $abstractContent
            .= td( { -align => 'right' },
            font( { -size => '-1' }, b("Type:") ) )
            . td( { -align => 'left' }, font( { -size => '-1' }, $pubtype ) );
    }
    $abstractContent
        .= td( { -align => 'right' },
        font( { -size => '-1' }, b("Source:") ) )
        . td( { -align => 'left' }, font( { -size => '-1' }, $source ) );
    if ($pubmed) {
        $colspanNum += 2;
        $abstractContent .= td( { -align => 'right' },
            font( { -size => '-1' }, b("PubMed ID:") ) )
            . td( { -align => 'left' }, font( { -size => '-1' }, $pubmed ) );
    }
    $abstractContent = Tr($abstractContent);
    if ($abstract) {
        print "<a name=abstract>&nbsp;</a>";
        $abstractContent
            = Tr(
            td( { -colspan => "$colspanNum" }, b("Abstract:") . $abstract ) )
            . $abstractContent;
    }
    print table(
        {   -align       => 'center',
            -border      => '0',
            -cellpadding => '3',
            -cellspacing => '3',
            -width       => '75%'
        },
        $abstractContent
    );

}

########################################################################
sub geneList {
########################################################################
    my ( $self, $refObject ) = @_;
    my @lociList = $self->getCuratedGeneList($refObject);
    my ( %loci, @loci );
    foreach my $row (@lociList) {
        my ( $locus, $locusNo ) = split( /:/, $row );
        $loci{$locus}++;
        push( @loci, $locus );
    }
    my @TMPnotYetCuratedLoci = $self->getNotYetCuratedGeneList($refObject);
    my @notYetCuratedLoci;
    foreach my $row (@TMPnotYetCuratedLoci) {
        my ( $locus, $locusNo ) = split( /:/, $row );
        if ( !$loci{$locus} ) {
            push( @notYetCuratedLoci, $locus );
        }
    }
    print &Divider75;
    if ( !@loci && !@notYetCuratedLoci ) { return; }
    print "<a name=summary>&nbsp;</a>";
    my %foundTopicGene;
    $self->topic4Gene( \%foundTopicGene, $refObject );
    if ( @loci > 20 ) {
        my $num = @loci;
        print center(
            b(        "There are "
                    . font( { -color => 'red', -size => '+1' }, $num )
                    . " different genes addressed in this paper."
            )
        );
        my @tableLabels;
        for ( my $i = 0; $i <= $#loci; $i += 10 ) {
            my $endIndex = $i + 9;
            if ( $endIndex > $#loci ) { $endIndex = $#loci; }
            my $beg   = $i + 1;
            my $end   = $endIndex + 1;
            my $label = "#$beg-$end($loci[$i]-$loci[$endIndex])";
            push( @tableLabels, "$label" );
        }
        my $popup = table(
            { -align => 'center' },
            Tr(       td( b("Jump toSummary Chart for: ") ) 
                    . td(start_form)
                    . td(
                    popup_menu(
                        -name     => 'jumpItem',
                        -"values" => \@tableLabels,
                        -onchange => 'jump(this)'
                    )
                    )
                    . td(end_form)
            )
        );
        print $popup, &Divider75;
        $self->note;
        my $j = 0;
        for ( my $i = 0; $i <= $#loci; $i += 10 ) {
            $j++;
            if ( $j == 4 || $j == 7 || $j == 10 ) {
                print $popup;
                $self->note;
            }
            my $endIndex = $i + 9;
            if ( $endIndex > $#loci ) { $endIndex = $#loci; }
            my @displayLoci = @loci[ $i .. $endIndex ];
            my $beg         = $i + 1;
            my $end         = $endIndex + 1;
            my $label       = "#$beg-$end($loci[$i]-$loci[$endIndex])";
            $self->geneTable( \@displayLoci, \%foundTopicGene, $label );
        }
    }
    elsif ( @loci > 10 ) {
        my $num      = @loci;
        my $jumpLink = Tr(
            td( {   -width => '50%',
                    -align => 'center'
                },
                a( { -href => "#1-10($loci[0]-$loci[9])" }, "#1-10" )
                    . " ($loci[0]-$loci[9])"
                )
                . td(
                {   -width => '50%',
                    -align => 'center'
                },
                a( { -href => "#11-$num($loci[10]-$loci[$#loci])" },
                    "#11-$num" )
                    . " ($loci[10]-$loci[$#loci])"
                )
        );

        print table(
            {   -width => '600',
                -align => 'center'
            },
            Tr( th( {   -colspan => '2',
                        -align   => 'center'
                    },
                    b(  "There are "
                            . font( { -color => 'red', -size => '+1' }, $num )
                            . " different genes addressed in this paper. Jump to Summary Chart for: "
                    )
                )
                )
                . $jumpLink
        ), &Divider75;

        my @firstLoci  = @loci[ 0 .. 9 ];
        my @secondLoci = @loci[ 10 .. $#loci ];
        my $label1     = "#1-10($loci[0]-$loci[9])";
        my $label2     = "#11-$num($loci[10]-$loci[$#loci])";
        $self->geneTable( \@firstLoci, \%foundTopicGene, $label1 );
        $self->note;
        $self->geneTable( \@secondLoci, \%foundTopicGene, $label2 );
        print p,
            table(
            {   -width => '600',
                -align => 'center'
            },
            Tr( th( {   -colspan => '2',
                        -align   => 'center'
                    },
                    b("Jump to Summary Chart for: ")
                )
                )
                . $jumpLink
            );
    }
    elsif ( @loci >= 1 ) {
        $self->geneTable( \@loci, \%foundTopicGene );
    }
    if (@notYetCuratedLoci) {
        my $genelist;
        foreach my $locus (@notYetCuratedLoci) {
            $genelist .= a(
                {   -href => $configUrl->dictyBaseCGIRoot
                        . "gene_page.pl?gene_name=$locus",
                    -target => 'infowin'
                },
                $locus
            ) . " | ";
        }
        if ( @notYetCuratedLoci == 1 ) {
            print p,
                center(
                b(  "There is one 'not yet curated' gene for this paper. "
                        . $genelist
                )
                );
        }
        else {
            print p,
                center(
                b(        "There are "
                        . @notYetCuratedLoci
                        . " 'not yet curated' genes for this paper. "
                        . $genelist
                )
                );
        }
    }

}

########################################################################
sub note {
########################################################################
    print table(
        Tr( td( {   -align => 'left',
                    -width => '100%'
                },
                ul( li( "To find other papers on a gene and topic, click in the appropriate box."
                        )
                        . li(
                        "To go to the Locus page for a gene, click on the gene name."
                        )
                )
            )
        )
    );
}

########################################################################
sub geneTable {
########################################################################
    my ( $self, $lociRef, $foundTopicGeneRef, $label ) = @_;
    my $subtitle = "Genes addressed in this paper ";
    if ($label) {
        print "<a name=$label>&nbsp;</a>";
        $label =~ s/^([^\(]+)\(.+$/$1/;
        if ( $label =~ /^#([0-9]+)-([0-9]+)$/ ) {
            if ( $1 == $2 ) { $label = "#$1"; }
        }
        $subtitle .= "($label)";
    }
    my $geneNum = @$lociRef;
    my $tableHeader;
    my %foundLocus;
    foreach my $locus (@$lociRef) {
        $tableHeader .= th(
            {   -rowspan => '2',
                -align   => 'center',
                -valign  => 'center',
                -bgcolor => '#b7d8e4'
            },
            a(  {   -href => $configUrl->dictyBaseCGIRoot
                        . "gene_page.pl?gene_name=$locus",
                    -target => 'infowin'
                },
                $locus
            )
        );
        $foundLocus{$locus}++;
    }
    my $geneTable;
    my %foundTopic;
    foreach my $key ( sort ( keys %$foundTopicGeneRef ) ) {
        my ( $topic, $locus ) = split( /:/, $key );
        my $linkTopic = $topic;
        $linkTopic =~ s/ /\+/g;
        if ( $foundLocus{$locus} && !$foundTopic{$topic} ) {
            $foundTopic{$topic}++;
            my $tableCell = td( { -width => "20%" }, $topic );
            foreach my $gene (@$lociRef) {
                if ( $$foundTopicGeneRef{"$topic:$gene"} ) {
                    $tableCell .= td(
                        {   -width => "10%",
                            -align => 'center'
                        },
                        a(  {   -href => $configUrl->dictyBaseCGIRoot
                                    . "$dblink/reference/geneinfo.pl?locus=$gene&topic=$linkTopic",
                                -target => 'infowin'
                            },
                            "X"
                        )
                    );
                }
                else {
                    $tableCell .= td( { -width => "10%" }, br );
                }
            }
            $geneTable .= Tr($tableCell);
        }
    }
    print p,
        table(
        {   -align       => 'center',
            -border      => '1',
            -cellpadding => '4',
            -cellspacing => '2',
            -width       => "100%"
        },
        Tr( td( { -rowspan => '2' } )
                . th(
                {   -align   => 'center',
                    -colspan => "$geneNum",
                    -align   => 'center',
                    -bgcolor => '#a4abc2'
                },
                $subtitle
                )
            )
            . Tr($tableHeader)
            . Tr(
            th( {   -align   => 'center',
                    -bgcolor => '#a4abc2'
                },
                "Topics in this paper"
            )
            )
            . $geneTable
        );

}

########################################################################
sub authorSearch {
########################################################################
    my ( $self, $refObject ) = @_;
    my $authorList = $refObject->authorList;
    my @author = split( /\, /, $authorList );
    if ( $author[$#author] =~ /et al/i ) {
        pop @author;
    }
    my $authorPopup = popup_menu(
        -name     => 'author',
        -"values" => \@author
    );
    my @parameter = (
        "Papers in dictyBase",
        "Colleagues in dictyBase",
        "PubMed"
    );
    my $paraPopup = popup_menu(
        -name     => 'parameter',
        -"values" => \@parameter
    );
    print "<a name=author>&nbsp;</a>";
    print start_form, table(
        {   -align       => 'center',
            -width       => '600',
            -border      => '0',
            -cellpadding => '3',
            -cellspacing => '3'
        },
        Tr( th( {   -align   => 'left',
                    -bgcolor => '#a4abc2'
                },
                "&nbsp;Author Searches"
            )
            )
            . Tr(
            td( { -bgcolor => '#d8d8d8' },
                table(
                    {   -border      => '0',
                        -cellpadding => '0',
                        -cellspacing => '0'
                    },
                    Tr( td( { -colspan => '3' },
                            "To find contact information or other publications by the authors of this paper, follow these three steps:"
                                . br
                                . br
                        )
                        )
                        . Tr(
                        td("(1) Choose an author,")
                            . td(
                            "(2) Choose a search parameter,&nbsp;&nbsp;&nbsp;&nbsp;"
                            )
                            . td("(3) Click to implement.")
                        )
                        .

                        Tr(
                              td($authorPopup) 
                            . td($paraPopup)
                            . td(
                            submit(
                                -name  => 'submit',
                                -value => 'Search!'
                            )
                            )
                        )
                )
            )
            )

        ),
        end_form;
}

########################################################################
sub topic4Gene {
########################################################################
    my ( $self, $foundTopicGeneRef, $refObject ) = @_;

    foreach my $rowRef ( @{ $refObject->locusTopicArrayRef } ) {
        my ( $topic, $locusNm ) = @$rowRef;
        uc($locusNm);
        $$foundTopicGeneRef{"$topic:$locusNm"}++;
    }
    foreach my $rowRef ( @{ $refObject->featureTopicArrayRef } ) {
        my ( $topic, $featNm ) = @$rowRef;
        uc($featNm);
        $$foundTopicGeneRef{"$topic:$featNm"}++;
    }
}

########################################################################
sub getRefNo4AceNm {
########################################################################
    my ($self) = @_;
    my $refNo = Reference->GetReferenceNoBYaceNm(
        dbh     => $dbh,
        ACEname => $self->{'_ACEname'}
    );
    return $refNo;
}

########################################################################
sub getCuratedGeneList {
########################################################################
    my ( $self, $refObj ) = @_;
    my @loci  = $refObj->getCuratedLoci;
    my @feats = $refObj->getCuratedFeatures;
    push( @loci, @feats );
    return @loci;
}

########################################################################
sub getNotYetCuratedGeneList {
########################################################################
    my ( $self, $refObj ) = @_;
    my @loci  = $refObj->getNotYetLoci;
    my @feats = $refObj->getNotYetFeatures;
    push( @loci, @feats );
    return @loci;
}

########################################################################
sub getRefInfoBYauthor {
########################################################################
    my ( $self, $author ) = @_;
    $author = "\U$author";
    $author =~ s/\*/\%/g;
    if ( $author !~ /\%/ ) {
        $author = $author . "\%";
    }
    my $arrayRef = Reference->GetRefInfoArrayRefBYauthor(
        dbh    => $dbh,
        author => $author
    );
    return $arrayRef;
}

########################################################################
sub pageContents {
########################################################################
    my ( $self, $refObject ) = @_;
    my $pageContent = a( { -href => '#author' }, "Author Search" ) . " | ";
    my $locusArrayRef = $refObject->locusArrayRef;
    my $featureArrayRef = $refObject->featureArrayRef;
    if ( $$locusArrayRef[0]->[0] || $$featureArrayRef[0]->[0] ) {
        $pageContent = a( { -href => '#summary' }, "Summary Chart" ) . " | "
            . $pageContent;
    }
    if ( $refObject->abstract ) {
        $pageContent = a( { -href => '#abstract' }, "Abstract" ) . " | "
            . $pageContent;
    }
    if ( $refObject->dictyBaseidForCommunityAnnotatedRef ) {
        $pageContent .= a( { -href => '#CA' }, "Community Annotation" )
            . a(
            { -href => '#CA' },
            img({   -src => $configUrl->dictyBaseImages . "star.gif",
                    -border => 0
                }
            )
            );
    }

    $pageContent = "Page Contents: " . $pageContent;
    print table( { -align => 'center' }, Tr( td($pageContent) ) );

}

########################################################################
sub communityAnnotation {
########################################################################
    my ( $self, $refObject ) = @_;

    if ( !$refObject->dictyBaseidForCommunityAnnotatedRef ) { return; }

    my @dictyBaseid
        = split( /\|/, $refObject->dictyBaseidForCommunityAnnotatedRef );

    my $communityUrl = $configUrl->dictyBaseCGIRoot
        . "$dblink/DisplayCommuAnno?dictyBaseid=";
    my $locusUrl
        = $configUrl->dictyBaseCGIRoot . "gene_page.pl?dictyBaseid=";

    my $rows;

    foreach my $dictyBaseid (@dictyBaseid) {

        my $dictyBaseidObj = dictyBaseid->new(
            dbh         => $dbh,
            dictyBaseid => $dictyBaseid
        );

        my $featNm = $dictyBaseidObj->feature_name;

        my $showNm = $dictyBaseidObj->locus_name;

        if ( $dictyBaseidObj->feature_name ) {

            if ($showNm) { $showNm .= "/"; }

            $showNm .= $dictyBaseidObj->feature_name;

        }

        my $arrayRef = Community_annotation->CommunityAnnotationArrayRef(
            dbh         => $dbh,
            dictyBaseid => $dictyBaseid
        );

        my $list;

        foreach my $rowRef (@$arrayRef) {

            my ( $community_annotation_no, $annotation_set_no, $topic,
                $other )
                = @$rowRef;

            my $anchor = $topic;

            $anchor =~ s/^([^ ]+) .+$/$1/;
            $anchor =~ s/[,;]$//;

            $list .= a(
                {   -href   => $communityUrl . $dictyBaseid . "#$anchor",
                    -target => 'infowin'
                },
                $topic
            ) . " | ";

        }
        $list =~ s/ \| $//;

        $rows .= Tr(
            td( { -align => 'center' },
                a(  {   -href   => $locusUrl . $dictyBaseid,
                        -target => 'infowin'
                    },
                    $showNm
                )
                )
                . td($list)
        );
    }

    print &Divider75;

    print table(
        {   -width       => '100%',
            -border      => 0,
            -cellspacing => 3,
            -cellpadding => 4
        },
        Tr( th( {   -align   => 'left',
                    -colspan => 2
                },
                a(  { -name => 'CA' },
                    img({   -src => $configUrl->dictyBaseImages
                                . "star.gif",
                            -border => 0
                        }
                    )
                    )
                    . font(
                    { -size => '+1' },
                    "This reference has been selected for Community Annotations on the genes and topics listed in the table below."
                    )
            )
            )
            . Tr(
            td( font( { -color => 'red' }, "NOTE: " )
                    . "Community annotations are submitted by members of the Dictyostelium community."
                    . a(
                    {   -href => $configUrl->dictyBaseCGIRoot
                            . "$dblink/communityAnnotation"
                    },
                    "Click here"
                    )
                    . " to submit a Community Annotation."
            )
            )
        ),
        br;

    print table(
        {   -width       => '100%',
            -border      => 1,
            -cellspacing => 3,
            -cellpadding => 4
        },
        Tr( th(       { -bgcolor => "#a4abc2" }, "Gene/ORF" )
                . th( { -bgcolor => "#a4abc2" }, "Topic" )
            )
            . $rows
        ),
        p;

    print &Divider75;

}

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

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

    print b($err);

    &printEndPage;

    if ( defined $dbh ) {
        $dbh->disconnect;
    }
    exit;
}

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

