#!/usr/local/bin/perl -w
#
# Author: Andrew Smith;
#
# Description: Code to create an ABI sequencer run module from tables in oracle;
#
#
#	my $module = ABIModule->new({
#                         		type => 'Platecheck',
#                         		id_method => 1,
#                         		filtername => 'A'
#                     			});
#
#######################################################################
# This software has been created by Genome Research Limited (GRL).    # 
# GRL hereby grants permission to use, copy, modify and distribute    # 
# this software and its documentation for non-commercial purposes     # 
# without fee at the user's own risk on the basis set out below.      #
# GRL neither undertakes nor accepts any duty whether contractual or  # 
# otherwise in connection with the software, its use or the use of    # 
# any derivative, and makes no representations or warranties, express #
# or implied, concerning the software, its suitability, fitness for   #
# a particular purpose or non-infringement.                           #
# In no event shall the authors of the software or GRL be responsible # 
# or liable for any loss or damage whatsoever arising in any way      # 
# directly or indirectly out of the use of this software or its       # 
# derivatives, even if advised of the possibility of such damage.     #
# Our software can be freely distributed under the conditions set out # 
# above, and must contain this copyright notice.                      #
#######################################################################
#
# $Id: ABIModule.pm,v 1.1 1999/07/16 14:20:10 as1 Exp $
#

use strict;
use WrapDBI;

package ABIModule;

sub new {
	my ( $type, $self ) = @_;
	bless($self,$type);

	$self->{DB} = WrapDBI->connect('dev1', {RaiseError => 1, AutoCommit => 0});

	if( !defined $self->{type} && ( $self->{type} ne 'Platecheck' || 'Prerun' || 'Run' )){
		die("ERROR: Module type must be defined as platecheck, prerunmodule or runmodule.");
	}
	
	if( !defined $self->{id_method} || !defined $self->{filtername} ){
		die("ERROR: id_method and filtername must be defined \n");
	}


	$self->{id_module} = $self->{DB}->fetch_scalar("
													select 	id_runmodule
													from 	runmethod_runmodule, steptypedict 
													where 	id_method = $self->{id_method}
													and 	steptypedict.description = '$self->{type}'
													and 	steptypedict.id_dict = step_type
													");

	

	$self->return_module_def();
	$self->compile_module();


	return $self->{module_string};
}

sub DESTROY {
	my ($self) = shift;
	$self->{DB}->disconnect;
}

sub compile_module {
	my ( $self ) = @_;
	
	foreach my $line ( @{$self->{module}} ){
		$self->{module_string} .= $line;

		next if( !exists $self->{oracle_defs}->{$line} );
		if( exists($self->{oracle_defs}->{$line}) && $self->{oracle_defs}->{$line} =~ /'0'/ ){
			$self->{module_string} .= $self->{oracle_defs}->{$line} ;
			next;
		}
		$self->{module_string} .= 	$self->number_to_module_string(
										$self->module_values_from_oracle( $self->{oracle_defs}->{$line} )
																  );
	}
	$self->null_terminate_string();
}


sub return_module_def {

	my ( $self ) = @_;

	my $platecheck = 0;

	$platecheck = 1 if $self->{type} eq 'Platecheck';

	my %module;
	my @module;

	$module[0] = "0x20,0x20,0x36,0x31,0x30,0x46,0x57,0x00,0x46,0x13,0x50,0x00,0x01,0x00,0x3c,";

    $module[1] = "'0','2','9',0x00,";
    $module[2] = "'0','2','9',0x00,";
    $module[3] = "'0','2','9',0x00,";
    $module[4] = "'0','0','6',0x00,";
    $module[5] = "'0','0','8',0x00,";
    $module[6] = "'0','0','9',0x00,";
    $module[7] = "'0','1','0',0x00,";
    $module[8] = "'1','0','0',0x00,";

    $module[9] = "'0','2','9',0x00,";
    $module[10] = "'0','2','9',0x00,";
    $module[10] = "'0','8','5',0x00," if ! $platecheck;
    $module[11] = "'0','2','9',0x00,";
    $module[12] = "'0','2','9',0x00,";
    $module[13] = "'0','2','9',0x00,";
    $module[14] = "'0','2','9',0x00,";
    $module[14] = "'0','0','5',0x00," if ! $platecheck;
    $module[15] = "'0','2','9',0x00,";
    $module[15] = "'0','0','4',0x00," if ! $platecheck;
    $module[16] = "'0','2','9',0x00,";
    $module[16] = "'0','0','3',0x00," if ! $platecheck;
    $module[17] = "'0','2','9',0x00,";
    $module[18] = "'0','2','9',0x00,";

    $module[19] = "'0','3','1',0x00,";
    $module[20] = "'0','3','0',0x00,";
    $module[21] = "'0','3','3',0x00,";
    $module[22] = "'0','3','2',0x00,";
    $module[23] = "'0','3','5',0x00,";
    $module[24] = "'0','3','4',0x00,";
    $module[25] = "'0','3','7',0x00,";
    $module[26] = "'0','3','6',0x00,";
    $module[27] = "'0','3','9',0x00,";
    $module[28] = "'0','3','8',0x00,";
    $module[29] = "'0','2','9',0x00,";
    $module[30] = "'0','2','9',0x00,";
    $module[31] = "'0','2','9',0x00,";
    $module[32] = "'0','7','6',0x00,";
    $module[33] = "'0','7','5',0x00,";
    $module[34] = "'0','4','2',0x00,";
    $module[35] = "'0','4','3',0x00,";
    $module[36] = "'0','4','4',0x00,";
    $module[37] = "'0','4','5',0x00,";
    $module[38] = "'0','2','9',0x00,";
    $module[39] = "'0','2','9',0x00,";
    $module[40] = "'0','2','9',0x00,";
    $module[41] = "'0','4','8',0x00,";
    $module[42] = "'0','9','5',0x00,";
    $module[43] = "'0','9','0',0x00,";
    $module[44] = "'0','9','1',0x00,";
    $module[45] = "'0','9','6',0x00,";
    $module[46] = "'0','9','2',0x00,";
    $module[47] = "'0','9','3',0x00,";
    $module[48] = "'0','9','4',0x00,";
    $module[49] = "'0','2','9',0x00,";
    $module[50] = "'0','2','9',0x00,";
    $module[51] = "'0','2','9',0x00,";
    $module[52] = "'0','2','9',0x00,";
    $module[53] = "'0','2','9',0x00,";
    $module[54] = "'0','8','0',0x00,";
    $module[55] = "'0','8','1',0x00,";
    $module[56] = "'0','2','9',0x00,";
    $module[57] = "'0','2','9',0x00,";
    $module[58] = "'0','2','9',0x00,";
    $module[59] = "'0','2','9',0x00,";


    $module{$module[7]}  =  "LASER_POWER";


    $module{$module[10]} =  "GEL_TEMP" if !$platecheck;
    $module{$module[14]} =  "EP_POWER" if !$platecheck;
    $module{$module[15]} =  "EP_CURRENT" if !$platecheck;
    $module{$module[16]} =  "EP_VOLTAGE" if !$platecheck;

    $module{$module[19]} =  "PRPBLANK";
    $module{$module[20]} =  "PRPSKIP1";
    $module{$module[21]} =  "PRPFILTER1";
    $module{$module[22]} =  "PRPSKIP2";
    $module{$module[23]} =  "PRPFILTER2";
    $module{$module[24]} =  "PRPSKIP3";
    $module{$module[25]} =  "PRPFILTER3";
    $module{$module[26]} =  "PRPSKIP4";
    $module{$module[27]} =  "PRPFILTER4";
    $module{$module[28]} =  "PRPSKIP5";
    $module{$module[32]} =  "CCDOFFSET";
    $module{$module[33]} =  "CCDGAIN";
    $module{$module[34]} =  "CCDVSS";
    $module{$module[35]} =  "CCDHSS";
    $module{$module[36]} =  "CCDVSR";
    $module{$module[37]} =  "CCDHSR";
    $module{$module[41]} =  "PIXELDENSITY";
    $module{$module[42]} =  "SPCHANNELTIME";
    $module{$module[43]} =  "SPLEFTDISTANCE";
    $module{$module[44]} =  "SPRIGHTDISTANCE";
    $module{$module[45]} =  "SPSKIP";
    $module{$module[46]} =  "SPPERIOD";
    $module{$module[47]} =  "SPSPEED";
    $module{$module[48]} =  "SPACCEL";
    $module{$module[54]} =  "DURATION_SECS"; #

    #
    # Records that are always zero'd
    #
    $module{"'0','2','9',0x00,"} = "'0','0','0','0','0',0x00,";
    $module{"'0','8','1',0x00,"} = "'0','0','0','0','0',0x00,";
    $module{"'0','0','6',0x00,"} = "'0','0','0','0','0',0x00,";
    $module{"'0','0','8',0x00,"} = "'0','0','0','0','0',0x00,";
    $module{"'0','0','9',0x00,"} = "'0','0','0','0','0',0x00,";
    $module{"'1','0','0',0x00,"} = "'0','0','0','0','0',0x00,";

	$self->{module} = \@module;
	$self->{oracle_defs} = \%module;

	return $self;
}
		
sub module_values_from_oracle {
	my ( $self, $column ) = @_;

	my $column_table = { 
						DURATION_SECS 	=> "RUNMETHOD_RUNMODULE",
						PRPBLANK 		=> "FILTERSET",
						PRPSKIP1		=> "FILTERSET",
						PRPFILTER1 		=> "FILTERSET",
						PRPSKIP2 		=> "FILTERSET",
						PRPFILTER2 		=> "FILTERSET",
						PRPSKIP3 		=> "FILTERSET",
						PRPFILTER3 		=> "FILTERSET",
						PRPSKIP4 		=> "FILTERSET",
						PRPFILTER4 		=> "FILTERSET",
						PRPSKIP5 		=> "FILTERSET"
					};

	if( exists($column_table->{$column}) && $column_table->{$column} eq 'RUNMETHOD_RUNMODULE' ){
		return $self->{DB}->fetch_scalar("
											select  $column
											from	$column_table->{$column}
											where 	id_method = $self->{id_method}
										");
	}

	if( exists($column_table->{$column})){
		return $self->{DB}->fetch_scalar("
											select 	$column 
											from 	$column_table->{$column}
											where 	filtername = '$self->{filtername}'
										");
	}

	return $self->{DB}->fetch_scalar("
										select 	$column 
										from 	runmodule 
										where 	id_runmodule = $self->{id_module}
									");
}


sub number_to_module_string {
	my ( $self, $number ) = @_;
	#
	# Pad to 5 characters then split and put into string;
	#
	my @digits = split(//,sprintf("%05d",$number));
	return sprintf("'%s','%s','%s','%s','%s',0x00,",$digits[0],$digits[1],$digits[2],$digits[3],$digits[4]);
}

sub null_terminate_string {
	my ( $self, $string ) = @_;
	$self->{module_string} = substr($self->{module_string},0,-1);
	$self->{module_string} .= '\0';
	return( $self );
}

1;

__END__

#
# $Log: ABIModule.pm,v $
# Revision 1.1  1999/07/16 14:20:10  as1
# Initial revision
#
#

