####################################################################
#!/usr/local/bin/perl -w
# Mode: perl
# Programmer: Kuan Chang
# Date: 04/22/2001
#####################################################################

use strict;
use CGI qw(:standard *table *TR *th *td *ul *ol);
use Gramene::Mutant::MutantSearchDB;
use Gramene::Page;

#####################################################################
# constants

use constant GO_PREFIX => 'GO:';
use constant TO_PREFIX => 'TO:';
use constant GRO_PREFIX => 'GRO:';  # growth stage
use constant PO_PREFIX => 'PO:';
# GR_PREFIX is for mutant
use constant GR_PREFIX => 'GR:';
# my $GR_PREFIX = "GR:";
use constant STUDY_PREFIX => 'GR:ST:';
use constant ALLELE_PREFIX => 'GR:AL:';
use constant GERMPLASM_PREFIX => 'GR:GP:';

use constant DEFAULT_IMAGE_COMMENT => 'No Caption';

use constant EVIDENCE_CODE_HOME => '/plant_ontology/';
use constant EVIDENCE_CODE_EXE_NAME => 'evidence_codes.html';
use constant EXE_NAME => 'search_mutant';
use constant ONTOLOGY_HOME => '/db/ontology/';
use constant CMAP_HOME => '/db/cmap/feature';
use constant ONTOLOGY_EXE_NAME => 'search_term';
use constant STUDY_EXE_NAME => 'search_study';
use constant ALLELE_EXE_NAME => 'search_allele';
use constant GERMPLASM_EXE_NAME => 'search_germplasm';
use constant SEARCH_PROTEIN_EXE_NAME => 'protein_search';
use constant MUTANT_IMAGE_HOME => '/images/mutant/';
use constant MUTANT_THUMBNAIL_IMAGE_HOME => '/images/mutant/tmb/';
use constant MAX_LENGTH_OF_ACCESSION => 7;
use constant CORNELL_RFLP_2001 => 3;
use constant JRGP_2000 => 6;
#this is for raclette 
use constant RICE_MORPHOLOGICAL_MAP_2000 => 8;

# my $CORNELL_RFLP_2001 = 1;
# my $JRGP_2000 = 2;
# my $RICE_MORPHOLOGICAL_MAP_2000 = 4;

#####################################################################
# Connect to the database
my $db = Gramene::Mutant::MutantSearchDB->new();

$db->connect_db();   


my $page = Gramene::Page->new(Apache->request);

my $input  = param('id');
my $sort_by  = param('sort_by');
my $symbol_from_url  = param('gene_symbol');

if( ( ! $sort_by ) || ( $sort_by ne "dev_stage" ) ) {
    $sort_by = "to";
}


my ($id, $is_GR);
undef $id;
if( $input ) {
    ($id, $is_GR) = extract_ID($input);
} 
if( $symbol_from_url ) {
    $id = $db->get_id_by_symbol( $symbol_from_url );
    if($id){
       $is_GR = 1;
       $input = "GR:00".$id;
    }else{
       $is_GR =0;
       $input =$symbol_from_url;
    }
}

# create CGI object
my $cgi = new CGI;
print $cgi->header;

if(defined $id){

  my $gis_on_embl_genome = $db->get_gis_on_ensembl_genome();
  
  my $name =  $db->get_mutant_name($id);
  my $symbol = $db->get_mutant_symbol($id);
  my $synonym = $db->get_mutant_synonym($id);
  my $phenotypic_description =  $db->get_phenotypic_description($id);
#  my @image_names = $db->get_mutant_images($id);
  my @mutant_images = $db->get_mutant_images_and_image_comments($id);
  my $phenotypesRef = $db->get_object_phenotypes( 'mutant', $id, );
  my %phenotypes = %$phenotypesRef;
  my @mutant_studies = $db->get_mutant_studies($id);
  my @mutant_alleles = $db->get_alleles('mutant', $id);
  my @mutant_germplasms = $db->get_germplasms('mutant', $id);
#  my @mutant_map_positions = $db->get_map_positions('mutant', $id);
  my @gene_identities = $db->get_gene_identities('mutant', $id);
  my @gene_products = $db->get_gene_products('mutant', $id);

   my $mutant_reference_ids_ref =  $db->get_reference_ids('gramene.gene', $id);

  my @mutant_evidence_codes = $db->get_evidence_codes('mutant', $id);
  my $oryza_linkout = $db->get_oryza_linkout($id);
 
  my $cmap_feature_arrRef = $db->get_cmap_feature($id);
 
  #$db->get_general_mutant_info('object_to_linkout', 'url_link', 'mutant', $id);

  #check if parameters are empty, set them undefined
  undef $name unless $name;
  undef $symbol unless $symbol;
  undef $synonym unless $synonym;
  undef $phenotypic_description unless $phenotypic_description;
#  undef @image_names unless @image_names;
  undef @mutant_images unless @mutant_images;
  undef %phenotypes unless (%phenotypes && scalar( keys %phenotypes)>0 );
  undef @mutant_studies unless @mutant_studies;
  undef @mutant_alleles unless @mutant_alleles;
  undef @mutant_germplasms unless @mutant_germplasms;
#  undef @mutant_map_positions unless @mutant_map_positions;
  undef @gene_identities unless @gene_identities;
  undef @gene_products unless @gene_products;
 
  #undef @mutant_reference_ids unless @mutant_reference_ids;
 

   undef @mutant_evidence_codes unless @mutant_evidence_codes;
  undef $oryza_linkout unless $oryza_linkout;

 
#######################################################################
  if($is_GR) {
    print $page->start_html(-title=>"Summary for Rice Gene: $name ($input)"); 
  }
  else{
    print $page->start_html(-title=>"Cannot find any information about $input"); 
  }

  print $page->start_body;
  # show Gramene search engine
  print_mutant_search_area();


  # show mutant page
  if($is_GR && defined $name) {
	 #mutant under curation
    if(!(@$mutant_reference_ids_ref||@mutant_evidence_codes||@gene_identities||@mutant_images || %phenotypes ||  @mutant_studies ||@mutant_alleles||@mutant_germplasms )){


        print h1("Gene: <i>$name</i> ($input)  is under curation"); 
        print start_table({-width=>'98%', -align=>'center', -cellpadding=>4, -cellspacing=>1}); 
        print TR(th({-align=>'left', -class=>'datatitle', -width=>'25%'}, "<a name=\"general\"></a
>Gene Name"), td({-colspan=>4, -class=>'databody'}, $name)); 
       
        print TR(th({-align=>'left', -class=>'datatitle', -width=>'25%'}, 'Gene Symbol'), td({-colspan=>4, -class=>'databody'}, "<i>$symbol</i>")); 
  print TR(th({-align=>'left', -class=>'datatitle', -width=>'25%'}, 'Gene Synonym'), td({-colspan=>4, -class=>'databody'}, $synonym)) if (defined $synonym); 

  print TR(th({-align=>'left', -class=>'datatitle', -width=>'25%'}, 'Phenotypic Description'
), td({-colspan=>4, -class=>'databody'}, $phenotypic_description)); 
 
    if( @gene_products){ 
       print start_TR;
       print td( {-colspan=>5, -align=>'right'}, "<br>" );
	print end_TR;
       
       my $row_number = scalar( @gene_products );

	print start_TR;
	print th( {-align=>'left', -class=>'datatitle', -width=>'25%', -rowspan=>$row_number}, 'Gene Product');
	print td({-colspan=>4, -class=>'databody', -align=>'left'}, a({-target=>'gene_product',-href=>"/db/protein/" . SEARCH_PROTEIN_EXE_NAME . "?acc=$gene_products[0]"}, $gene_products[0]));
	print end_TR;
	for( my $i = 1; $i <= ( scalar( @gene_products ) - 1 ); $i++ ) {
	    print start_TR;
	    print td({-colspan=>4, -class=>'databody', -align=>'left'}, a({-target=>'gene_product',-href=>"/db/protein/" . SEARCH_PROTEIN_EXE_NAME . "?acc=$gene_products[$i]"}, $gene_products[$i]));
	    print end_TR;
	}

    }

  print end_table;

  
  print_map_position($cmap_feature_arrRef,\@gene_identities);

  print_linkout_and_references($oryza_linkout,$mutant_reference_ids_ref,$db);

 }else{ #the curated mutant   
   
   my $map_info_link='';
   if(($cmap_feature_arrRef && scalar(@$cmap_feature_arrRef)>0) || @gene_identities ){
    $map_info_link=' |&nbsp;<a href="#map" class="gopage"><b>Map and Sequence Info</b></a>&nbsp;'
  }
  my $allele_link='';
  if(@mutant_alleles || @mutant_germplasms){
     $allele_link=' |&nbsp;<a href="#allele_germ" class="gopage"><b>Allele and Germplasm</b></a>&nbsp;'
   }    
 
   print <<SMALLBAR;
<table>
  <tr>
    <td>&nbsp;<img src="/images/icons/grain_icon.jpg" alt="grain_icon" height=16 width=16 align="top">
    </td>
    <td>
      &nbsp;<a href="#general" class="gopage"><b>General Info. for Gene</b></a>&nbsp;
      $allele_link
      $map_info_link 
      |&nbsp;<a href="#feature" class="gopage"><b>Associated Features</b></a>&nbsp
      |&nbsp;<a href="#ref" class="gopage"><b>References</b></a>&nbsp;
    </td>
  </tr>
</table>
SMALLBAR



        print h1("<a name=\"top\"></a>Summary for Rice Gene: <i>$name</i> ($input)");
	print start_table({-width=>'98%', -align=>'center', -cellpadding=>4, -cellspacing=>1});
	print TR(th({-align=>'left', -class=>'datatitle', -width=>'25%'}, "<a name=\"general\"></a>Gene Name"), td({-colspan=>4, -class=>'databody'}, $name));
	print TR(th({-align=>'left', -class=>'datatitle', -width=>'25%'}, 'Gene Symbol'), td({-colspan=>4, -class=>'databody'}, "<i>$symbol</i>"));
	print TR(th({-align=>'left', -class=>'datatitle', -width=>'25%'}, 'Gene Synonym'), td({-colspan=>4, -class=>'databody'}, $synonym)) if (defined $synonym);
	print TR(th({-align=>'left', -class=>'datatitle', -width=>'25%'}, 'Phenotypic Description'), td({-colspan=>4, -class=>'databody'}, $phenotypic_description));


	if( @mutant_images){
	    my $image_count = 1;
	    foreach my $mutant_image(@mutant_images) { 
	        my ($image_name, $image_comment) = split("\t", $mutant_image);
	
		if($image_count % 2 == 1) {
		    print start_TR;
		    print th({-align=>'left', -class=>'datatitle', -width=>'25%'}, 'Phenotypic Image');     
		}

		unless($image_comment){
		    $image_comment = DEFAULT_IMAGE_COMMENT;
		}
	

		print td({-align=>'right'}, $image_count, '.', a({-target=>'_blank', -href=>MUTANT_IMAGE_HOME."$image_name".".jpg"}, img({-src=>MUTANT_THUMBNAIL_IMAGE_HOME."$image_name"."_tn.jpg", -alt=>"$name"})));

		print td({-class=>'databody', -align=>'left'}, $image_count, '.', $image_comment);  

		$image_count += 1;
		# where to end the row (when there are enough images but not run out of all images)

		if($image_count % 2 == 1 && ($#mutant_images+2 != $image_count)) {	
		    print end_TR;
		}
	    }
	    print end_TR;
	}

 ###################
       print start_TR;
       print td( {-colspan=>5, -align=>'right'}, "<br><b>[</b>&nbsp;<a href=\"#top\" class=\"gopage\"><b>Top</b></a>&nbsp;<b>]</b></br>" );
	print end_TR;
  ########################
   if( @mutant_alleles ){
     
	print start_TR;
	print th( {-align=>'left', -class=>'datatitle', -width=>'25%'}, "<a
	name=\"allele_germ\"></a>Allele");
	my %linked_alleles;
	for( my $i = 0; $i<= $#mutant_alleles; $i++ ) {
	    my @mutant_allele_columns = split( "\t", $mutant_alleles[$i] );
	    my $allele_accession = get_canonical_accession( ALLELE_PREFIX, $mutant_allele_columns[0] );
	    $mutant_allele_columns[1] =~ s/^(\D+)/<i>$1\<\/i>/;
	    my $allele_link = '<a target="allele" href="'.  ALLELE_EXE_NAME ."?id=$allele_accession".'">'.$mutant_allele_columns[1].'</a>';
	    my $link = $linked_alleles{$mutant_allele_columns[1]};
	    if($link){
		$linked_alleles{$mutant_allele_columns[1]} = $link.", $allele_link"; 
	    }else{
		$linked_alleles{$mutant_allele_columns[1]} = $allele_link;
	    }
	
	}


	my $sorted_linked_alleles = ''; # sort  for displaying
	foreach my $allele_name ( sort keys %linked_alleles){
	    my $links = $linked_alleles{$allele_name};
	    $sorted_linked_alleles = 
		$sorted_linked_alleles ?
		"$sorted_linked_alleles, $links" 
		: "$links";
	}
	print td({-colspan=>4, -class=>'databody', -align=>'_blank'}, $sorted_linked_alleles);
	print end_TR;
	
    }

   if(@mutant_germplasms){
     my $count_germplasm = @mutant_germplasms;
     print start_TR;
     my $germ_anchor='';
     $germ_anchor= "<a name=\"allele_germ\"></a>$germ_anchor" unless @mutant_alleles;
     print th({-align=>'left', -class=>'datatitle', -width=>'25%' },
     $germ_anchor."Germplasm");
     my %linked_germplasms;
     for( my $i = 0; $i <$count_germplasm; $i++ ) {
         my @mutant_germplasm_columns =  split("\t", $mutant_germplasms[$i]);
	 my $germplasm_accession = get_canonical_accession(GERMPLASM_PREFIX, $mutant_germplasm_columns[0]);
	 my $germplasm_link = '<a target="germplasm" href="'.GERMPLASM_EXE_NAME ."?id=$germplasm_accession".'">'.$mutant_germplasm_columns[1].'</a>';
	 my $link = $linked_germplasms{$mutant_germplasm_columns[1]};
	 if($link){
	    $linked_germplasms{$mutant_germplasm_columns[1]} = $link.", $germplasm_link"; 
	}else{
	    $linked_germplasms{$mutant_germplasm_columns[1]} = $germplasm_link;
	}
     }


    my $sorted_linked_germplasms = ''; # sort  for displaying
    foreach my $germ_name ( sort keys %linked_germplasms){
	    my $links = $linked_germplasms{$germ_name};
	    $sorted_linked_germplasms = 
		$sorted_linked_germplasms ?
		"$sorted_linked_germplasms, $links" 
		: "$links";
    }

     print td({-colspan=>4,-class=>'databody',-align=>'left'},$sorted_linked_germplasms); 
     print end_TR;
     
 }
       
    ##################
     if( @mutant_alleles||@mutant_germplasms){
  	print start_TR;
	print td( {-colspan=>5, -align=>'right'}, "<br><b>[</b>&nbsp;<a href=\"#top\" class=\"gopage\"><b>Top</b></a>&nbsp;<b>]</b></br>" );
	print end_TR;
       
    }
    ######################
   

    

    if(@gene_identities){ 
        my $row_number = scalar( @gene_identities );   

	print start_TR;
	print th({-align=>'left', -class=>'datatitle', -width=>'25%'}, "<a name=\"map\"></a>GenBank accession");
	my @linked_gis;
	for( my $i = 0; $i <= $#gene_identities; $i++ ) {
	    my $gi = $gene_identities[$i];
	    my $gi_link = $gi.'( <a target="gene_identity"
	    href="http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?cmd=search&db=nucleotide&term='.$gi.'[accn]">[NCBI]</a>';

	    # link to gramene ensembl browser
	    if($gis_on_embl_genome->{$gi}){
		$gi_link .= ' <a target="gene_identity" href="/Oryza_sativa/unisearch?type=All&q='.$gi.'"> [Gramene Genomes]</a>';
	    
	    };
	    $gi_link .= ' )';
	    #my $gi_link = '<a target="gene_identity" href="http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?cmd=search&db=nucleotide&term='.$gene_identities[$i].'[accn]">'.$gene_identities[$i].'</a>';
	    push @linked_gis, $gi_link;
	}
      
	print td({-colspan=>4, -class=>'databody', -align=>'left'},join(", ", @linked_gis));
	print end_TR;

    }

    if( @gene_products){ 
        my $row_number = scalar( @gene_products );

	print start_TR;
	print th( {-align=>'left', -class=>'datatitle', -width=>'25%', -rowspan=>$row_number}, 'Gene Product');
	my @linked_gps;
	for( my $i = 0; $i <$row_number; $i++ ) {
	    my $gp_link = '<a target="gene_product" href="/db/protein/'. SEARCH_PROTEIN_EXE_NAME ."?acc=$gene_products[$i]".'">'.$gene_products[$i].'</a>';
	    push @linked_gps, $gp_link;
	}

	print td({-colspan=>4, -class=>'databody', -align=>'left'},join(", ", @linked_gps));
	print end_TR;
    }

        
  print end_table;  # Just moved here.all general info and gene sequence,product info.
   
 
  print_map_position($cmap_feature_arrRef,\@gene_identities);
   
#############################
if( $cmap_feature_arrRef && scalar(@$cmap_feature_arrRef)>0){
print start_table({-width=>'98%', -align=>'center', -cellpadding=>4, -cellspacing=>1});
print start_TR;
print td( {-align=>'right'}, "<br><b>[</b>&nbsp;<a href=\"#top\" class=\"gopage\"><b>Top</b></a>&nbsp;<b>]</b></br>" );
print end_TR;
print end_table;


}
##############################

   

  if( %phenotypes ) {

    print "<br>";

    print start_table({-border=>1, -width=>'98%', -align=>'center', -cellpadding=>4, -cellspacing=>1});
    print TR(th({-align=>'left', -class=>'datatitle', -colspan=>'5'},
    q[ <a name="feature"></a>Associated Feature
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	( Evidence Code: 
	<a href="/plant_ontology/evidence_codes.html#TAS" target="evn">TAS</a>
	<a href="/plant_ontology/evidence_codes.html#IAGP" target="evn">IAGP</a>
	)
    ]));
    my @sorted_term_types = ('Trait',
			     'Growth Stage',
			     'Plant Structure',
			     'Cellular Component',
			     'Biological Process',
			     'Molecular Function',
			     'Enviroment'
			     );

    foreach my $term_type (@sorted_term_types){
	if(exists $phenotypes{$term_type}){

	    my $term_accs = $phenotypes{$term_type};
	    my %linked_terms;
	    foreach my $term_acc (@$term_accs){
		my $term_name = $db->get_term_name($term_acc);
		$term_name ||= $term_acc;
		my $term_link =  '<a target="ontology"
		href="'.ONTOLOGY_HOME.''.ONTOLOGY_EXE_NAME."?id=$term_acc".'">'."$term_name ($term_acc)".'</a>';
		my $links = $linked_terms{$term_name};
		if($links){
		    $linked_terms{$term_name} = $links.", $term_link";
		}else{
		    $linked_terms{$term_name} = $term_link;
		}
	    }

	    my $sorted_linked_terms = ''; # sort terms for displaying
	    foreach my $term_name ( sort keys %linked_terms){
		my $links = $linked_terms{$term_name};
		$sorted_linked_terms = 
		$sorted_linked_terms ?
		"$sorted_linked_terms, $links" 
		: "$links";
	    }


	    print start_TR;
	    print th({-align=>'left', -class=>'datatitle', -width=>'25%'}, "$term_type");
	    print td({-colspan=>4, -class=>'databody', -align=>'left'}, $sorted_linked_terms);
	    print end_TR;
	}
    }

    

    print end_table;
  

 print start_table({-width=>'98%', -align=>'center', -cellpadding=>4, -cellspacing=>1});
 print start_TR;
 print td( {-align=>'right'}, "<br><b>[</b>&nbsp;<a href=\"#top\" class=\"gopage\"><b>Top</b></a>&nbsp;<b>]</b></br>" );
 print end_TR;
 print end_table;

  
}

##############################
print_linkout_and_references($oryza_linkout,$mutant_reference_ids_ref,$db);
 
### print end_table;
    }#curated mutant
  }
  else{
    #print_top("Cannot find any information about $input");
    #print_mutant_search_area();
    print h1("Sorry! Cannot find any information about <i>$input</i>");
  }

  print $page->end_body;
}
else{
  #default for no data available
  print $page->start_html(-title=>"Cannot find any information about $input" 
		         );
  #show Gramene::Page header
#  print header();
  print $page->start_body;
  print_mutant_search_area();  
  print h1("Sorry! Cannot find any information about <i>$input</i>"); 
  print $page->end_body;
}

#####################################################################
# utility functions
#####################################################################
sub extract_ID{
  my $id = shift;
  my $is_GR = 0;
  my $tmp = GR_PREFIX;
  if($id =~ /$tmp/) {
    $id =~ s/$tmp//;
    $id =~s/^0+//;
    $is_GR = 1;
  } 

  undef $id unless(is_digit($id));

  return ($id, $is_GR);
}

sub get_canonical_accession{
  my ($prefix, $id) = @_;
  my $canonical_accession = $prefix;
  my $length = MAX_LENGTH_OF_ACCESSION - length($id);
  for my $x (1 .. $length){
    $canonical_accession .= "0";
  }
  $canonical_accession .= $id;
  return $canonical_accession;
}

sub is_digit{
  my $query = shift;
  $query =~ s/^\s+//;
  $query =~ s/\s+$//;
  if ($query =~ /^\d+$/ ){
    return 1;
  }else{ 
    return 0;
  }
}

sub remove_spaces_on_both_sides{
  my $query = shift;
  $query =~ s/^\s+//;
  $query =~ s/\s+$//;
  return $query;
}


#################
sub print_mutant_search_area {
    my $self = url(-relative=>1);
    print start_form(-method=>'GET', -action=>"search_core");


  print <<SEARCH_AREA;
&nbsp;<img src="/images/icons/grain_icon.jpg" alt="grain_icon" height="16" width="16">
<a href="/rice_mutant/index.html" class="gopage">Gene Home</a> |
<a href="/rice_mutant/gene_search.html" class="gopage">Search</a> |
<a href="/rice_mutant/gene_submit.html" class="gopage">Submit</a> |
<a href="/rice_mutant/gene_help.html" class="gopage">Help Documents</a> |
<a href="/tutorials/mutant.html" class="gopage">Tutorial</a> |
<a href="/documentation/faq/mut_faq.html" class="gopage">FAQ</a>
<p></p>
<table cellspacing="0" cellpadding="3" border="0" width="98%" align=center>
  <tr><th align=center class=searchtitle>Gene Search</th></tr>
  <tr align=center class=searchbody>
    <td align=center>&nbsp;&nbsp;&nbsp;&nbsp;<input type=text value="" framewidth=4 name=query size=40 maxlength=256>&nbsp;&nbsp;&nbsp;&nbsp;<input name=btn type=submit value="Search">&nbsp;&nbsp;<input name=Clear type=reset value="Clear">
      <br><span class="alert">Search by Gene Symbol, Gene Name, Gramene Accesssion&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>[e.g. <a href="/db/mutant/search_mutant?id=GR:0060198">dwarf-15</a> or <a href="/db/mutant/search_mutant?id=GR:0060198">GR:0060198</a>]&nbsp;&nbsp;<a href="/rice_mutant/Mutant_Help1.html">HELP</a> 
    </td>
  </tr>
</table>
SEARCH_AREA

    print end_form();
    print "<hr>";
}


sub print_map_position{
   my ($cmap_feature_arrRef,$gene_identities_ref) = @_;
   

 if( $cmap_feature_arrRef && scalar(@$cmap_feature_arrRef)>0){
      print "<br><br>";
      print start_table({-border=>1, -width=>'98%', -align=>'center', -cellpadding=>4, -cellspacing=>1});
       my $map_anchor="";
       $map_anchor ="<a name=\"map\"></a>" unless scalar(@$gene_identities_ref)>0; 
       print TR(th({-align=>'left', -class=>'datatitle', -colspan=>'6'},$map_anchor."Map Position"));
  
      print TR(th({-align=>'center', -class=>'datatitle'}, ['#','Map Set Name', 'Linkage group', 'Start Position', 'Stop Position']));     
      my $map_count = 1;
      foreach my $map_feature(@$cmap_feature_arrRef){
	my( $feature_acc,$map_set,$map,$start_pos,$stop_pos,$unit)=@$map_feature;
	if($map_count % 2 == 0) {
	  print start_TR({-class=>'databody', -align=>'center'});
	} else {
	  print start_TR({-align=>'center'}); 
	}


	print td({-align=>'center'}, $map_count);
        print td({-align=>'center'},a({-target=>'map',-href=>CMAP_HOME."?feature_aid=$feature_acc"}, "Rice-$map_set"));
	print td({-align=>'center'}, $map);
	print td({-align=>'center'},$start_pos.' '.$unit);
        if($stop_pos){
           print td({-align=>'center'}, $stop_pos.' '.$unit);
        }else{
	  $stop_pos="&nbsp;&nbsp;"; #for priting issue
          print td({-align=>'center'}, $stop_pos);
        }
	
	$map_count += 1;
      }
      print end_table;
   }
   

}





sub print_linkout_and_references{
    my($oryza_linkout,$mutant_reference_ids_ref,$db)=@_;

    if( (defined $oryza_linkout) || ( $mutant_reference_ids_ref && scalar(@$mutant_reference_ids_ref)>0 ) ){
        print "<br>";
        print start_table({-border=>1, -width=>'98%', -align=>'center', -cellpadding=>4, -cellspacing=>1});

      	print TR(th({-align=>'left', -class=>'datatitle', -colspan=>'2'}, "<a name=\"ref\"></a>Reference"));

      	if(defined $oryza_linkout){
      
      	    print TR(th({-align=>'left', -class=>'datatitle', -width=>'25%'}, 'DBxref'), td({-class=>'databody'}, a({-target=>'_blank',-href=>"$oryza_linkout"},"Oryzabase") ));

      
    	}

    
      
      my $reference_count=0;
      foreach my $reference_id_ref (@$mutant_reference_ids_ref){
        my ($reference_id) = @$reference_id_ref;
	if($reference_count % 2 == 0) {
	  print start_TR({-class=>'databody', -align=>'center'});
	} else {
	  print start_TR({-align=>'center'}); 
	}
      
	my $ref_title =  $db->get_reference_info('title', $reference_id);
	my $ref_source = $db->get_reference_source($reference_id);
	my $ref_author = $db->get_reference_author($reference_id);
	my $ref_source_trivia = $db->get_reference_source_trivia($reference_id);
	my $index = $reference_count + 1;

	print <<ITEM;
          <td align="LEFT" colspan="2">
            <dl>
              <dt>
                <table cellspacing="0" cellpadding="0">
                  <tr>
                    <td align="CENTER" valign="CENTER">
                      <img src="/images/icons/grain_icon.jpg" alt="grain_icon" height=16 width=16 align="center"></td><td align="CENTER" valign="CENTER"><span class="reftitle"><b>$index.<b></span>
                    </td>
                    <td align="LEFT" valign="CENTER">
                      <a class="gopage" href="/db/literature/pub_search?ref_id=$reference_id" target="reference">$ref_author</a>
                    </td>
                  </tr>
                </table>
              </dt>
              <dd>
                <table cellspacing="0" cellpadding="0">
                  <tr><td><span class="reftitle">$ref_title.</span></td></tr>
                  <tr><span class="smallrefsize"><td>$ref_source. $ref_source_trivia</td></span></tr>
                </table>
              </dd>
            </dl>
          </td>
ITEM
      print  end_TR;

      $reference_count += 1;
      }
      # tmp additioni

      print end_table;
     
    } #oryzabase and reference

}#end sub
