# GenPerl module 
#

# POD documentation - main docs before the code

=head1 NAME

Genetics::Genotype

=head1 SYNOPSIS

  $gt = new Genetics::Genotype(name => '1-D12S91',
			       importID => 13,
			       dateCreated => $today,
			       Keywords => [ {name => "Test Data", 
					      dataType => "Boolean", 
					      value => 1}, 
					   ], 
			       isActive => 1,
			       icResult => "Pass",
			       dateCollected => "1993-11-13",
			       Subject => {name => "EAPed20.1", importID => 12},
			       Marker => {name => "D12S91", importID => 1},
			       AssayAttrs => [ {name => "lab",
						dataType=> "string",
						value => "Lab 6"}, 
					       {name => "machineID",
						dataType => "String",
						value => "ABC1234"}
					     ],
			       AlleleCalls => [ {alleleName => 3, 
						 alleleType => "Code", 
						 phase => "Maternal", 
						 AssayAttrs => [ {name => "peakHeight",
								  dataType => "Number",
								  value => 367} ]
						},
						{alleleName => 1, 
						 alleleType => "Code", 
						 phase => "Paternal", 
						 AssayAttrs => [ {name => "peakHeight",
								  dataType => "Number",
								  value => 435} ]
						},
					      ]
			      ) ;

See the GenPerl Tutorial for more information.

=head1 DESCRIPTION

A Perl class encapsulating data associated with GNOM Genotype objects.  
Genotype objects represent the experimentally determined set of alleles 
that an individual has at a genetic marker.

=head1 LICENSE

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

=head1 FEEDBACK

Currently, all feedback should be sent directly to the author.

=head1 AUTHOR - Steve Mathias

Email: mathias@genomica.com

Phone: (720) 565-4029

Address: Genomica Corporation 
         1745 38th Street
         Boulder, CO 80301

=head1 DETAILS

The rest of the documentation describes each of the object variables and 
methods. The names of internal variables and methods are preceded with an
underscore (_).

=cut

##################
#                #
# Begin the code #
#                #
##################

package Genetics::Genotype ;

BEGIN {
  $ID = "Genetics::Genotype" ;
  #$DEBUG = $main::DEBUG ;
  $DEBUG = 0 ;
  $DEBUG and warn "Debugging in $ID is on" ;
}

=head1 Imported Packages

 Genetics::Object       Superclass
 strict			Just to be anal
 vars			Global variables

=cut

use Genetics::Object ;
use strict ;
use vars qw(@ISA) ;

require		5.004 ;

@ISA = qw(Genetics::Object) ;

use vars qw($ID $DEBUG @ATTRS @REQD_ATTRS %DEFAULTS) ;

@ATTRS = qw(isActive icResult dateCollected 
	    Subject Marker Sample AlleleCalls AssayAttrs) ;

@REQD_ATTRS = qw(isActive 
		 Subject Marker AlleleCalls) ;

%DEFAULTS = () ;


=head1 Public methods

=head2 printXML

  Function  : Print an XML representation of the object.
  Argument  : An XML::Writer object being used to generate the XML.
  Returns   : String
  Scope     : Public
  Comments  : Calls Genetics::Object->printGeneralXML to generate XML elements
              common to all Genetics::Object objects.

=cut

sub printXML {
    my($self, $writer) = @_ ;
    my($class, $value, $hashPtr, $attrListPtr, $hashPtr2, $tag, $typeVal, 
       $type, $val) ;

    $class = ref $self ;
    $class =~ s/.*::// ;

    ## Common Attributes/Elements
    $self->printGeneralXML($writer) ;

    ## Genotype Elements    
    # SubjectRef
    $hashPtr = $self->field('Subject') ;
    $writer->startTag('SubjectRef') ;
    $writer->dataElement('Name', $$hashPtr{name}) ;
    $writer->dataElement('ID', $$hashPtr{id}) ;
    $writer->endTag('SubjectRef') ;
    # MarkerRef
    $hashPtr = $self->field('Marker') ;
    $writer->startTag('MarkerRef') ;
    $writer->dataElement('Name', $$hashPtr{name}) ;
    $writer->dataElement('ID', $$hashPtr{id}) ;
    $writer->endTag('MarkerRef') ;
    # IsActive
    $writer->dataElement('IsActive', $self->field('isActive')) ;
    # ManualICResult
    if (defined ($value = $self->field('manualICResult'))) {
	$writer->dataElement('ManualICResult', $value) ;
    }
    # GenotypetAssayMethod

    # AlleleCall
    if (defined ($value = $self->field('AlleleCall'))) {
	foreach $hashPtr (@$value) {
	    $writer->startTag('AlleleCall') ;
	    $writer->startTag('Allele') ;
	    $writer->dataElement('Name', $$hashPtr{alleleName}) ;
	    $writer->dataElement('Type', $$hashPtr{alleleType}) ;
	    $writer->endTag('Allele') ;
	    if (defined ($attrListPtr = $$hashPtr{Attr})) {
		foreach $hashPtr2 (@$attrListPtr) {
		    $writer->startTag('Attribute') ;
		    while (($tag, $typeVal) = each(%$hashPtr2)) {
			($type, $val) = split(/\s*=\s*/, $typeVal) ;
			$writer->dataElement('Name', $tag) ;
			$writer->dataElement('DataType', $type) ;
			$writer->dataElement('Value', $val) ;
		    }
		    $writer->endTag('Attribute') ;
		}
	    }
	    $writer->endTag('AlleleCall') ;
	}
    }
    $writer->endTag($class) ;

    return(1) ;
}

=head2 asHTML

  Function  : Generate an HTML representation of the object.
  Argument  : A Genetics::Genotype object and a scalar containing the name 
              of the GenPerl database in which the object is stored. 
  Returns   : Scalar containing the HTML text.
  Scope     : Public Instance Method
  Comments  : Calls Genetics::Object->_generalHTMLParam to generate HTML 
              elements common to all Genetics::Object objects.  An 
              HTML::Template object is used to actually generate the HTML.

=cut

sub asHTML {
  my($self, $db) = @_ ;
  my $tmpl = new HTML::Template(filename => "/home/slm/work/GenPerl/templates/genotype.tmpl") ;
  my(%param, $acListPtr, $acPtr, @alleles) ;

  %param = (DB => $db) ; # DB is the database the object is in.  It is used 
                         # to conscruct URL links to associated objects

  # Object data
  $self->_generalHTMLParam(\%param) ;

  # Genotype data
  $param{ISACTIVE} = $self->isActive() ;
  $self->icResult() and $param{ICRESULT} = $self->icResult() ;
  $self->dateCollected() and $param{DATECOLL} = $self->dateCollected() ;
  $param{SUBJECT} = $self->Subject->{name} ;
  $param{SUBJECTID} = $self->Subject->{id} ;
  $param{MARKER} = $self->Marker->{name} ;
  $param{MARKERID} = $self->Marker->{id} ;
  $acListPtr = $self->AlleleCalls() ;
  foreach $acPtr (@$acListPtr) {
    push @alleles, $$acPtr{alleleName} ;
  }
  $param{ALLELES} = join(", ", @alleles) ;

  $tmpl->param( %param ) ;

  return $tmpl->output() ;
}

1; 

