#!/usr/bin/perl -w
# run GeneNeighborScores.pl for every genome

use strict;
use lib "../Perl/modules";
use Genome;
use GenomicsUtils;
use Getopt::Long;

my $minScaffoldK = 300;
my $maxSep = 5000;
my $out = "";
my $debug = 0;
my $cluster="";
my $gnc="";
my $geneWindow = 1;
my $host=undef;

(GetOptions('out=s' => \$out,
	    'cluster=s' => \$cluster,
	    'gnc=s' => \$gnc,
	    'host=s' => \$host,
	    'minScaffoldK=i' => \$minScaffoldK,
	    'maxSep=i' => \$maxSep,
	    'geneWindow=i' => \$geneWindow,
	    'debug=i' => \$debug)
 && @ARGV == 0 && $out ne "" && $gnc ne "" && $cluster ne "")
    || die ("Usage: GeneNeighborScoresAll.pl -gnc=genomeSim -cluster=similarGenomes"
            . " -out=s [-geneWindow=1 -host=s -minScaffoldK=i -maxSep=i -debug=i]\n");
die "Bad cluster" if !(-f $cluster);

GenomicsUtils::setDebug($debug) if $debug > 0;
connect($host);
my @genomes = Genome::getGenomeList();
foreach my $genome (@genomes) {
    my $taxon = $genome->taxonomyId();
    if (! -e "$gnc$taxon.genomes") {
	print STDERR "Skipping: taxon $taxon -- no .genomes file\n";
	next;
    }
    my $cmd = join(" ", "./GeneNeighborScores.pl",
		   "-gnc=$gnc$taxon.genomes",
		   "-cluster=$cluster",
		   "-taxon=$taxon",
		   "-minScaffoldK=$minScaffoldK",
		   "-maxSep=$maxSep",
		   "-geneWindow=$geneWindow",
		   "-debug=$debug",
		   (defined $host ? "-host=$host" : ""),
		   ">",
		   "$out$taxon.scores");
    print STDERR "Running: $cmd\n";
    system($cmd) == 0 || die "Failed executing $cmd";
}
