#! /usr/local/bin/perl
#
# 13-Feb-1995 Created -- RD
# 14-Feb-1995 Added output to file to writeAceFile -- SD
# 14-Feb-1995 Added &revCompSeq() -- SD
# 15-Feb-1995 Supress empty opjects in writeAceFile -- RD
# 15-Feb-1995 More efficient revCompSeq() -- RD
# 17-Feb-1995 Append lines to class object -- RD
# 22-Feb-1995 eof() check in readAceFile was on STDIN not $file -- SD
# 09-Mar-1995 local($x);
# 15-Mar-1995 Added subroutines theContigs and theReads -- SD
# 10-Apr-1995 Added subroutine trashRead -- SD
# 27-Apr-1995 Tried to improve efficiency of writeAceFile -- SD
#
#######################################################################
# 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.                      #
#######################################################################

###################################################

sub readAceFile {
#
# read a .ace file into %Class - remove whitespace from DNA
#
    local($file) = @_ ;
    local($x);
    while (<$file>) {
        s/\/\/.*// ;			# ace file comments
        if ( /^DNA\s+(\S+)/ ) {
            $name = $1 ;
	    $x = "";
	    while (<$file>) {
		last if /^\s*$/ ;
		s/\s+//g ;		# remove whitespace
		$x .= $_ ;
	    }
	    $DNA{$name} = $x ;
	}
	elsif ( /^(\S+)\s+(\S+)/ ) {
	    $class = $1 ;
	    $name = $2 ;
	    if (! defined($$class{$name})) { $$class{$name} = ""; } # Keep perl -w happy
	    $x = "";
	    while (<$file>) {
		last if /^\s*$/ ;
		s/\\\n$// ;		# continuation lines
		$x .= $_ ;
	    }
	    $$class{$name} .= $x ;
	}
	if (eof($file)) { last ; }
    }
}

###################################################

sub writeAceFile {
#
# write a .ace file to first argument
#
    local($file) = @_ ;
    local($key);

    foreach $key (keys %DNA) {
	next unless $DNA{$key};
	print $file "\nDNA ",$key,"\n";
        $len = length ($DNA{$key});
	for ($i = 0 ; $i < $len ; $i += 60) {
	    print $file substr ($DNA{$key}, $i, 60), "\n" ;
	}
    }

    foreach $key (keys %Sequence) {
	next unless $Sequence{$key};
	print $file "\nSequence ",$key,"\n";
	print $file $Sequence{$key};
    }
}

###################################################

sub revCompSeq { # -- SD
# 
# Reverse complement a sequence
#
    local($seq) = @_ ;
    $seq = reverse ($seq) ;
    $seq =~ tr/acgtACGT/tgcaTGCA/ ;
    $seq ;
}

###################################################

sub theContigs {
    #
    # Returns a list of contigs
    #
    grep($Sequence{$_} =~ /Is_contig\n/,keys %Sequence);
}

###################################################

sub theReads {
    #
    # Returns a list of readings
    #
    grep($Sequence{$_} =~ /Is_read\n/,keys %Sequence);
}


###################################################

sub trashRead {
    #
    # Delete a reading from relevant structures
    #
    local( $read ) = @_;
    undef $Sequence{$read};
    undef $DNA{$read};

}


###################################################


1 ;	# required return value

############# end of file ###############
# Local Variables:
# mode:perl
# End:
############# end of file ###############
