#! /usr/local/bin/perl

#############################################################
#
# Generalised seach module for cross_match
#
#############################################################


#############################################################
# &CROSS_MATCH(consensus,vector)
#
# [1] Find the overlap using Phil Green's cross_match program
# [2] Produces less (but larger) aligned fragments with no
# appreciable loss of accuracy that &BLAST(). Hence, it is the
# default.
#     
#############################################################
sub CROSS_MATCH {		   
    local(@files) = @_;
    local(@results) = ();

    warn "$prog: Using cross_match\n" if $debug;

    $prog = "cross_match" if ! $prog;

    local($tempfile) = "/tmp/_$prog.crossmatch.$$";

    # Run cross_match
    #system("cross_match @files -minscore 100 > $tempfile.cm 2> /dev/null");

    system("cross_match @files > $tempfile.cm 2> /dev/null");
    warn "$prog: cross_match returned error code $?\n" if ($? != 0);

    # parse
    open(OUT,"$tempfile.cm") || warn "$prog: could not open cross_match output file: $tempfile.cm\n";

    while (<OUT>) {
	next unless /\(\d+\).*\(\d+\)/;
	tr/()//d;

	print $_ if $debug;

	@f = split;
	if (/\sC\s/) {
	    # match on complementary strand
	    push(@results,[$f[4],$f[5],$f[6],($f[6]+$f[7]),$f[9],$f[11],$f[12],($f[10]+$f[11])]);
	} else {
	    push(@results,[$f[4],$f[5],$f[6],($f[6]+$f[7]),$f[8],$f[9],$f[10],($f[10]+$f[11])]);
	}
    }

    close(OUT);

    $log_del = shift @files; # needed to remove .log file created!

    unlink("$tempfile.cm");
    unlink("$log_del.log");

    return \@results;
}


1;
#
