package SGN::View::Mason::Commands;
use strict;
use vars qw($m $c);
HTML::Mason::Component::FileBased->new(
'code' => sub {
use utf8; local $SGN::View::Mason::Commands::m = $HTML::Mason::Commands::m;
HTML::Mason::Exception::Params->throw
    ( error =>
      "Odd number of parameters passed to component expecting name/value pairs"
    ) if @_ % 2;
my ( $person_id, $locus );
{
    my %pos;
    for ( my $x = 0; $x < @_; $x += 2 )
    {
        $pos{ $_[$x] } = $x + 1;
    }

    foreach my $arg ( qw( person_id locus ) )
    {
        HTML::Mason::Exception::Params->throw
            ( error => "no value sent for required parameter '$arg'" )
                unless exists $pos{$arg};
    }
#line 32 "/home/production/cxgn/sgn/mason/locus/map_location.mas"
    $person_id = $_[ $pos{'person_id'} ];
#line 33 "/home/production/cxgn/sgn/mason/locus/map_location.mas"
    $locus = $_[ $pos{'locus'} ];
}
$m->debug_hook( $m->current_comp->path ) if ( HTML::Mason::Compiler::IN_PERL_DB() );

#line 27 "/home/production/cxgn/sgn/mason/locus/map_location.mas"
$m->print( '


' );
#line 36 "/home/production/cxgn/sgn/mason/locus/map_location.mas"
$m->print( '

' );
#line 38 "/home/production/cxgn/sgn/mason/locus/map_location.mas"



use CXGN::Phenome::Locus;
use CXGN::Marker;
use CXGN::Cview::MapFactory;
use CXGN::Cview::ChrMarkerImage;



my $maps = 1;
my $lg_name = $locus->get_linkage_group();
my $arm     = $locus->get_lg_arm();
my $location_html = qq|<table><tr>|;

my @locus_marker_objs = $locus->get_locus_markers();    #array of locus_marker objects
foreach my $lmo (@locus_marker_objs) {
  my $marker_id = $lmo->get_marker_id();    #{marker_id};
  my $marker = CXGN::Marker->new( $locus->get_dbh(), $marker_id ); #a new marker object
  my $marker_name = $marker->name_that_marker();
  my $experiments = $marker->current_mapping_experiments();
  if (    $experiments
	  and @{$experiments}
	  and grep { $_->{location} } @{$experiments} )
    {
      my $count = 1;
      for my $experiment ( @{$experiments} ) {
	if ( my $loc = $experiment->{location} ) {
	  my $map_version_id = $loc->map_version_id();
	  my $lg_name        = $loc->lg_name();
	  if ($map_version_id) {
	    my $map_factory =
	      CXGN::Cview::MapFactory->new( $locus->get_dbh() );
	    my $map = $map_factory->create(
					   { map_version_id => $map_version_id } );
	    my $map_version_id = $map->get_id();
	    my $map_name       = $map->get_short_name();
	    my $chromosome =
	      CXGN::Cview::ChrMarkerImage->new(
                 "", 250, 150, $locus->get_dbh(), $lg_name, $map, $marker_name,
                 $c->get_conf("basepath"),  $c->get_conf('tempfiles_subdir')."/cview",
                  );
	    my ( $image_path, $image_url ) =
	      $chromosome->get_image_filename();
	    my $chr_link =
	      qq|<img src="$image_url" usemap="#map$count" border="0" alt="" />|;
	    $chr_link .=
	      $chromosome->get_image_map("map$count") . "<br />";
	    $chr_link .= $map_name;
	    $count++;
	    if ($maps > 2) {
	      $maps = 1;
	      $location_html .= "</tr><tr>" ;
	    }
	    $maps++;
	    $location_html .= "<td>" . $chr_link . "</td>";
	  }
	}
      }
    }
}

#draw chromosome with marker-range for loci w/o associated marker, only a chromosome arm annotation
if ( scalar(@locus_marker_objs) == 0 && $lg_name ) {
  my $organism = $locus->get_common_name();
  my %org_hash = (
		  'Tomato'   => 9,    #F2 2000 map
		  'Potato'   => 3,
		  'Eggplant' => 6,
		  'Pepper'   => 10
		 );
  my $map_id      = $org_hash{$organism};
  my $map_factory = CXGN::Cview::MapFactory->new( $locus->get_dbh() );
  
  my $map = $map_factory->create( { map_id => $map_id } );
  if ($map) {
    my $map_name = $map->get_short_name();
    my ( $north, $south, $center ) = $map->get_centromere($lg_name);

    my $dummy_name;
    $dummy_name = "$arm arm" if $arm;
    my $chr_image =
      CXGN::Cview::ChrMarkerImage->new( "", 250, 150, $locus->get_dbh(),
						  $lg_name, $map, $dummy_name, $c->get_conf("basepath"), $c->get_conf('tempfiles_subdir')."/cview");

     my ($chr) = $chr_image->get_chromosomes();

    my $range_marker = CXGN::Cview::Marker::RangeMarker->new($chr);
    my ( $offset, $nrange, $srange );
    if ( $arm eq 'short' ) {
      $offset = $nrange = $srange = $center / 2;
    }
    elsif ( $arm eq 'long' ) {
      my $stelomere = $chr->get_length();
      $offset = ( $center + $stelomere ) / 2;
      $nrange = $srange = ( $stelomere - $center ) / 2;
    }
    $range_marker->set_offset($offset);    #center of north/south arm
    $range_marker->set_north_range($nrange);
    $range_marker->set_south_range($srange);
    $range_marker->set_marker_name($dummy_name);
    if ( !$dummy_name ) { $range_marker->hide_label(); }
    $range_marker->set_label_spacer(20);
    $range_marker->get_label()->set_name($dummy_name);
    $range_marker->get_label->set_stacking_level(2);
    $chr->add_marker($range_marker);
    my ( $image_path, $image_url ) = $chr_image->get_image_filename();
    my $chr_link =
      qq|<img src="$image_url" usemap="#chr_arm_map" border="0" alt="" />|;
    $chr_link .= $chr_image->get_image_map("chr_arm_map") . "<br />";
    $chr_link .= $map_name;
    $location_html .= "<td>" . $chr_link . "</td>";
  }
}
$location_html .= "</tr></table>";

 
#line 156 "/home/production/cxgn/sgn/mason/locus/map_location.mas"
$m->print( '

' );
#line 158 "/home/production/cxgn/sgn/mason/locus/map_location.mas"
$m->print(  $location_html  );
#line 158 "/home/production/cxgn/sgn/mason/locus/map_location.mas"
$m->print( '
' );
;return;
},
'declared_args' => {
  '$locus' => { default => undef },
  '$person_id' => { default => undef }
},
'load_time' => 1763528903,

)
;