# -*-Perl-*- 
# 
#
# $Id: ReadNames.pm,v 1.4 1997/04/24 10:58:42 dn1 Exp $
#
# $Log: ReadNames.pm,v $
# Revision 1.4  1997/04/24  10:58:42  dn1
# Added copyright
#
# Revision 1.3  1996/07/31  10:55:57  sd
# Added long_terminator and long_terminator_reverse to
# suffix construction table. (method new).
#
# Revision 1.2  1996/07/26  14:10:46  sd
# Added comment re read type descriptions.
#
# Revision 1.1  1996/03/26  12:13:18  dmh
# Initial revision
#
#
#######################################################################
# 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.                      #
#######################################################################

package ReadNames;

##########################################
#
# Construct structure defining read suffixes
#
##########################################

sub new { 

    my $type = shift;

    # The descriptions in the following associative array 
    # must agree with those in ReadTypes->defineDescription
    my $self = {

	'short_forward' => {
	    'fixed'   => '.s1',
	    'it_list' => 'a-su-z',
	},
	    
	'short_reverse' => {
	    'fixed'   => '.r1',
	    'it_list' => 'a-su-z',
	},
	    
	'terminator' => {
	    'fixed'   => '.s1t',
	    'it_list' => 'a-z',
	},
	    
	'terminator_reverse' => {
	    'fixed'   => '.r1t',
	    'it_list' => 'a-z',
	},

	'long_forward' => {
	    'fixed'   => '.s1l',
	    'it_list' => 'a-su-z',
	},

	'long_reverse' => {
	    'fixed'   => '.r1l',
	    'it_list' => 'a-su-z',
	},
	
	'oligo_forward' => {
	    'fixed'   => '.s',
	    'it_list' => '1-9',
	},

	'oligo_reverse' => {
	    'fixed'   => '.r',
	    'it_list' => '1-9',
	},
	'long_terminator' => {
	    'fixed'   => '.s1lt',
	    'it_list' => 'a-z',
	},
	    
	'long_terminator_reverse' => {
	    'fixed'   => '.r1lt',
	    'it_list' => 'a-z',
	},
	
    };

    bless $self, $type;

}

##########################################
#
# Methods to form read name;
#
##########################################


sub constructSuffix {

    my ($type, $readType, $template, $iteration)=@_;

    $iterationList =  $type->expandIterationList($type->{$readType}->{'it_list'});

    if ($iteration>0) {
	$iterationId = substr($iterationList, $iteration-1, 1);
    } else {
	$iterationId='';
    }

    my $readName =  $template . $type->{$readType}->{'fixed'} . $iterationId;

    $readName;

}

sub expandIterationList {

    my ($type, $suffix) = @_;

    while ( $suffix =~ /(\S)-(\S)/ ) {

	$exp = $type->expand($1, $2);
	$suffix=~s/$&/$exp/;
    }
    
    $suffix;
}

sub expand {

    my($type, $from,$to)=@_;

    my $expansion='';

    if ($from =~ /^\w$/ && $to =~ /^\w$/) {

	$from = ord($from);
	$to = ord($to);

	my $i;
	for($i=$from; $i<=$to; $i++) {
	    $expansion .= pack('c',$i);
	}
    }
    else {	 

	my $i;
	for($i=$from; $i<=$to; $i++) {
	    $expansion .= $i;
	}  

    }

    $expansion;

}



1;



