# -*-Perl-*- 
# 
#
# $Id: FinFormat.pm,v 1.3 1997/04/24 10:12:34 dn1 Exp $
#
# $Log: FinFormat.pm,v $
# Revision 1.3  1997/04/24  10:12:34  dn1
# Added copyright
#
# Revision 1.2  1996/04/15  14:03:40  badger
# Read lists now correctly sorted
#
# Revision 1.1  1996/03/26  12:12:40  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 FinFormat;

##########################################
#
# Construct...
#
##########################################

sub new { 

    my $type=shift;

    my $self={};
    $type->setDefaults($self);

    my %args = @_;
    foreach (keys%args) {
        $self->{$_} = $args{$_};
    }

    bless $self, $type;

}

sub setDefaults {

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

    chop($param_ref->{'date'}=`date +%d-%m-%y`);
    $param_ref->{'finisher'} = $ENV{'USER'};

}

##########################################
#
# Methods
#
##########################################


##########################################
# formatReadList($str_ref, $list_ref)
#
# $list_ref - reference to @array containing
# single list of full read names.
# 
# $str_str - scalar ref to string on which
# to append formatted output.
# 
# Format read list to give "\n" between 
# different templates. Number each line.
#
##########################################
sub formatReadList {

    my ($type, $str_ref, $list_ref) = @_;

    my $lastplate = '';
    my $i=0;
    my $read;

    foreach $read (sort @{$list_ref}) {

	my ($plate, $well, $suffix, $other)=FinFormat::getParts($read);

	if ($plate ne $lastplate) {
	    $$str_ref .= "\n";
	}
	$lastplate=$plate;
       			    
	$$str_ref .= sprintf "%3d. %s\n",$i+1,$read;
	$i++;
    }



}

##########################################
# ($plate, $well, $suffix, $other) = getParts($read)
#
# Disolve read into component parts.
#
##########################################
sub getParts { 

    my ($read)=@_;

    if ( $read=~ /^(\S+)([a-h]\d{1,2})\.(\S+)(.*)$/ ) {
	return($1, $2, $3, $4);
    }
    
    return ('null');

}

##########################################
# makeTitleStr($project, $finisher, $count, $type);
# 
# 
##########################################
sub makeTitleStr {

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

    my ($project, $finisher, $count, $type, $date)
	= ($type->{'project'}, 
	   $type->{'finisher'}, 
	   $type->{'count'}, 
	   $type->{'type'},
	   $type->{'date'});  

    $$str_ref .= "\n$project/$finisher/$count/$type/$date\n";

}


1;

