#!/usr/bin/perl
# -*- Mode: perl -*-
# file: marker

use lib '/usr/local/gramene/lib/perl/';
use strict;
use vars qw($DB);
use Ace 1.51;
use Ace::Browser::AceSubs qw(:DEFAULT Toggle);
use Ace::Browser::TreeSubs qw(AceImageHackURL);
use GrameneSubs;
use CGI 2.42 qw/:standard :html3 escape *table *TR *td *blockquote/;

use constant CLASS   => 'Marker';
#use constant EXAMPLE => 'A';

# open the database
$DB = OpenDatabase() || AceError("Couldn't open database.");

my ($obj_name, $obj);
$obj_name = param('name');
$obj = get_obj($obj_name) if $obj_name;

PrintTop($obj,CLASS,$obj ? CLASS . ": $obj" : CLASS . " Search");
PrintWarning($obj_name,CLASS) if $obj_name && !$obj;
if ($obj) {
  print start_table;
  print start_TR({-valign=>'TOP'}),start_td;
  print_report($obj);
  print end_td,start_td;
  print_image($obj);
  print end_td,end_TR,end_table;
}
PrintBottom();

sub get_obj {
  my $name = shift;
  my ($obj) = $DB->fetch(-class =>CLASS,
			 -name  => $name);
  return $obj;
}

sub print_report {
  my $obj = shift;

  print start_table({-border=>1});

  DataRow('Full Name',$obj->Full_name);
  DataRow('Aliases',$obj->Other_name);
  DataRow('Marker Type',$obj->Type);
  DataRow('Trait Name',$obj->Trait);
  #DataRow('PCR',$obj->PCR);	#only shows 'Primer' or nothing
  DataRow('Forward Primer',$obj->Forward_primer);
  DataRow('Reverse Primer',$obj->Reverse_primer);
  LocusRow('Locus',$obj->Locus);
  ContigviewRow('Genome view(s)',$obj->In_ensembl);
  DataRow('Chromosome', $obj->RGN_Chromosome);
  SequenceRow('Related sequence(s)',$obj->Related_Sequence);
  DataRow('Enzyme',$obj->Enzyme);
  DataRow('Polymorphism',$obj->Polymorphism);
  DataRow('Reference(s)',$obj->Reference);
  DataRow('Remarks',$obj->Remarks);

  print end_table;

  my $info = $obj->Help?$obj->Help->Info:undef;
  if ( $info ) {
    print br, br;
    if (Toggle('help','Help on marker')  ) {
      print pre($info->right);
    }
  }

}

sub print_image {
  my $obj = shift;
  my @images = get_images($obj);

  foreach my $img (@images) {
    my $picture = $img->Pick_me_to_call(2);
    print STDERR "marker: Image $img lacks Pick_me_to_call\n" and next unless $picture;
    print p(a({-href=>AceImageHackURL($picture),
	       -target=>'_new'},
	      img({-src=>AceImageHackURL($picture),
	       -border=>0,
		   -width=>300})));
  }

}

sub get_images {
    my $obj = shift;
    my @marker_images = $obj->Image;
    my @poly_images   = $DB->fetch(-query=>qq(find Marker "$obj"; follow Polymorphism; follow Image));
    my %images;
    %images=map { ($_,$_) } (@marker_images,@poly_images);
    return (values %images);
}

sub ContigviewRow {
  my $title = shift;
  my @r = @_;
  foreach (@r) {
      print TR(
	       th({-class=>'datatitle'},$title),
	       td({-class=>'databody'},a({href=>"/perl/contigview?clone=$_"},$_))
	      );
      $title='&nbsp;';
  } 
  unless (@r) {
      print TR(
	   th({-class=>'datatitle'},$title),
	   td({-class=>'databody'},'Not Available')
	   );
  }
}

sub SequenceRow {
  my $title = shift;
  my @Sequences=@_;
  foreach my $seq (@Sequences) {
      my %links=();
      foreach my $dbref ($seq->Database,$seq->External_DB_Key) {
	  #print STDERR "$dbref,", $dbref->right, "," , $dbref->right->right ,"\n";
          if($dbref =~ /Genbank/i && $dbref->right) {
	      $links{a({-href=>'http://www.ncbi.nlm.nih.gov/htbin-post/Entrez/query?db=n&form=1&field=Sequence+ID&term='.$dbref->right},"GenBank:".$dbref->right)}=1;
          }
      }
      print TR(
	       th({-class=>'datatitle'},$title),
	       td({-class=>'databody'},
	       a({-href=>"/gramene/seq/sequence?name=$seq&class=Sequence"},$seq)
	       .join ("",(map { ", $_" } sort keys %links))
	       ) );
      $title='&nbsp;';
  } 
  unless(@Sequences) {
      print TR(
	   th({-class=>'datatitle'},$title),
	   td({-class=>'databody'},'Not Available')
	   );
  }
}

sub LocusRow {
  my $title = shift;
  my @r = @_;
  if (@r) {
    my $first = shift @r;
    print TR(
	   th({-class=>'datatitle'},$title),
	   td({-class=>'databody'},($first->isObject ? ObjectLink($first) : $first)
           .'&nbsp;&nbsp;&nbsp;'.($first->Map_Study ? ( $first->Map_Study->Description ? $first->Map_Study->Description : $first->Map_study ) : '' ) )

	   );
    foreach (@r) {
      print TR(
	       th({-class=>'datatitle'},'&nbsp;'),
	       td({-class=>'databody'},($_->isObject ? ObjectLink($_) : $_)
               .'&nbsp;&nbsp;&nbsp;'.($_->Map_Study ? ( $_->Map_Study->Description ? $_->Map_Study->Description : $_->Map_study ) : '' ) )
	      );
    }
  } else {
      print TR(
	   th({-class=>'datatitle'},$title),
	   td({-class=>'databody'},'Not Available')
	   );
  }
}

