[Bio] / FortyEightMeta / mg_postproc_taxa_sims.pl Repository:
ViewVC logotype

View of /FortyEightMeta/mg_postproc_taxa_sims.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (download) (as text) (annotate)
Fri Mar 23 20:41:31 2007 UTC (13 years, 3 months ago) by olson
Branch: MAIN
initial checking of metagenome 48 stuff

#
# Postprocess the metagenome LSU/SSU/Greengene sims runs.
#

use DB_File;
use Data::Dumper;

use Job48;
use strict;
use FIG;
use FIGV;
use FIG_Config;
use File::Basename;
use File::Copy;
use GenomeMeta;
use Carp 'croak';
use FortyEightMeta::ExpandSims;

@ARGV == 1 or die "Usage: $0 job-dir\n";

my $blast2gff_exe = "$FIG_Config::bin/blast2gff";
-x $blast2gff_exe or &fatal("Executable $blast2gff_exe missing");

my $correct_contigs_exe = "$FIG_Config::bin/correct_gff_missing_contigs";
-x $correct_contigs_exe or &fatal("Executable $correct_contigs_exe missing");

my $gff2seed_exe = "$FIG_Config::bin/gff2seed";
-x $gff2seed_exe or &fatal("Executable $gff2seed_exe missing");

my $get_ss_conns_exe = "$FIG_Config::bin/get_ss_connections";
-x $get_ss_conns_exe or &fatal("Executable $get_ss_conns_exe missing");

my $summarize_exe = "$FIG_Config::bin/summarize_taxonomy_by_blast";
-x $summarize_exe or &fatal("Executable $summarize_exe missing");

my $jobdir = shift;

my $job_id = basename($jobdir);
my $job = new Job48($job_id);

my $meta = $job->meta;

print "Running job! $jobdir\n";

$meta->set_metadata("status.sims_postprocess", "in_progress");

&process_rdp();
&create_seed_gff();
&install_seed_gff();

$meta->set_metadata("status.sims_postprocess", "complete");
$meta->set_metadata("sims_postprocess.running", "no");

exit(0);

#
# Process the seed blastx results into an organism directory.
#
sub create_seed_gff
{
    my $genome = $job->genome_id();

    #
    # consolidate all the blastx generated sims into a single file
    #

    my $sims = "$jobdir/proc/sims.seed.raw";
    consolidate_sims("$jobdir/proc/sims.seed", $sims);

    my $gff = "$jobdir/proc/$genome.gff";
    my $out_sims = "$jobdir/proc/$genome.sims";

    #
    # Verify we have the inputs we need for generating the orgdir.
    #

    my $fasta = $meta->get_metadata("preprocess.fasta_file");
    ($fasta and -f $fasta) or &fatal("fasta not found: '$fasta'");

    my $taxonomy = &FIG::file_head("$jobdir/TAXONOMY", 1);
    my $project = &FIG::file_head("$jobdir/PROJECT", 1);
    chomp $taxonomy;
    chomp $project;

    #
    # Start the process.
    #

    my @args = (-f => $fasta,
		-s => $out_sims,
		-o => $gff,
		-gs => $job->genome_name(),
		-taxonomy => $taxonomy,
		-proj => $project,
		-g => $genome,
		$sims);

    my $pid = open(BOUT, "-|");
    if ($pid == 0)
    {
	exec { $blast2gff_exe } $blast2gff_exe, @args;
	die "Failed: $!: $blast2gff_exe @args";
    }

    print "Started pid=$pid $blast2gff_exe @args\n";

    while (<BOUT>)
    {
	print "Blast2gff: $_";
    }

    if (!close(BOUT))
    {
	&fatal("Failed with \$!=$! \$?=$?: $blast2gff_exe @args");
    }
}

#
# Unpack the gff file into the organism directory, do the rest of the post-install cleanup.

#
sub install_seed_gff
{
    my $genome = $job->genome_id();
    my $gff = "$jobdir/proc/$genome.gff";
    my $out_sims = "$jobdir/proc/$genome.sims";

    -f $gff or &fatal("gff file $gff does not exist");
    
    makedir("$jobdir/rp");
    my $orgdir = "$jobdir/rp/$genome";

    my $fasta = $meta->get_metadata("preprocess.fasta_file");
    ($fasta and -f $fasta) or &fatal("fasta not found: '$fasta'");

    #
    # seed2gff won't run if the orgdir exists
    #

    if (-d $orgdir)
    {
	my $new = "$orgdir." . time;
	print "Moving $orgdir to $new\n";
	rename $orgdir, $new or &fatal("Cannot rename $orgdir to $new: $!");
    }

    #
    # Create seed dir
    #
    my @args = ("-debug", $gff, "$jobdir/rp");
    &run_pipe(\*STDOUT, $gff2seed_exe, @args);

    #
    # Copy in missing contigs
    #

    &run_pipe(\*STDOUT, $correct_contigs_exe, "-orgdir", $orgdir, $fasta, $genome);

    #
    # Copy sims.
    #
    copy($out_sims, "$orgdir/similarities");

    #
    # Expand sims.
    #

    my $fig = new FIGV($orgdir);

    my $peg_syns = "$FIG_Config::fortyeight_data/peg.synonyms";
    
    my $syns_fh = new FileHandle($peg_syns);
    $syns_fh or &fatal("cannot open $peg_syns: $!");

    my $syns = load_peg_syns($syns_fh);

    my $exp_sims = "$orgdir/expanded_similarities";
    eval {
	expand_sims($fig, $out_sims, $exp_sims, $syns);
    };
    if ($@)
    {
	&fatal("error in expand_sims: $@");
    }

    #
    # Set up to compute subsys mappings.
    #

    makedir("$orgdir/Subsystems");
    my $bindings = "$orgdir/Subsystems/bindings";
    my $subsystems = "$orgdir/Subsystems/subsystems";

    &touch($bindings);
    &touch($subsystems);

    my $fh = new FileHandle(">$orgdir/ss.attributes");
    $fh or &fatal("Cannot write $orgdir/ss.attributes: $!");
    
    &run_pipe($fh,
	      $get_ss_conns_exe,
	      "-figv", $bindings, $subsystems,
	      "-orgdir", $orgdir,
	      "-g", $genome);
    close($fh);

    my $fh = new FileHandle(">$orgdir/taxa_summary_by_blast");
    $fh or &fatal("Cannot write $orgdir/taxa_summary_by_blast: $!");
    
    &run_pipe($fh,
	      $summarize_exe,
	      "-orgdir", $orgdir,
	      $genome);
    close($fh);
}
    
#
# If we are running here, we should have generated sims in the proc/sims.{16s,gg,lsu,ssu} directories.
# Scan the task.list files in each of them, and concatenate the output into files proc/sims.WHAT.raw.
#
sub process_rdp
{
    for my $name (qw(16s gg lsu ssu))
    {
	my $dir = "$jobdir/proc/sims.$name";
	my $sims = "$jobdir/proc/sims.$name.raw";
	
	consolidate_sims($dir, $sims);
	#
	# Now we can invoke the appropriate analysis.
	#
	
	if ($name eq 'gg')
	{
	    my $rc = system("$FIG_Config::bin/blast2taxa", $sims);
	    if ($rc != 0)
	    {
		&fatal("error running $FIG_Config::bin/blast2taxa $sims");
	    }
	}
	elsif ($name eq 'lsu' or $name eq 'ssu' or $name eq '16s')
	{
	    my $db = $name;
	    $db = "rdp" if $name eq '16s';
	    
	    my @cmd = ("$FIG_Config::bin/16s_taxa_mysql",
		       '-d', $db,
		       '-b', $sims,
		       '-c', '1e-5',
		       '-l', '50',
		       '-ah', "$sims.allhits",
		       '-bh', "$sims.besthits");
	    print "Run @cmd\n";
	    my $rc = system(@cmd);
	    if ($rc != 0)
	    {
		&fatal("error rc=$rc running @cmd");
	    }
	}
    }
}

sub consolidate_sims
{
    my($dir, $sims) = @_;
    if (! -d $dir)
    {
	&fatal("Sim directory $dir not found");
    }
    
    open(TL, "<$dir/task.list") or &fatal("Cannot open $dir/task.list: $!");
    
    #
    # Open output file.
    #
    open(O, ">$sims") or &fatal("Cannot create $sims: $!");
	
    while (<TL>)
    {
	chomp;
	my ($id, $in, $nr, $flags, $out, $err) = split(/\t/);
	
	if (! -f $out)
	{
	    &fatal("Sims output file $out missing for sims dir $dir");
	}
	
	copy($out, \*O);
    }
    close(O);
}
    
sub run_pipe
{
    my($outfh, $cmd, @args) = @_;

    my $pid = open(BOUT, "-|");
    if ($pid == 0)
    {
	exec { $cmd } $cmd, @args;
	die "Failed: $!: $cmd @args";
    }

    print "Started pid=$pid $cmd @args\n";

    while (<BOUT>)
    {
	print $outfh $_;
    }

    if (!close(BOUT))
    {
	&fatal("Failed with \$!=$! \$?=$?: $cmd @args");
    }
}

sub makedir
{
    my($dir) = @_;

    if (! -d $dir)
    {
	mkdir($dir, 0777) or &fatal("mkdir $dir failed: $!");
    }
    chmod 0777, $dir;
}

sub touch
{
    my($file) = @_;
    my $fh = new FileHandle(">$file") or &fatal("touch: cannot open $file for writing: $!");
    close($fh);
}

sub fatal
{
    my($msg) = @_;

    $meta->add_log_entry($0, ['fatal error', $msg]);
    $meta->set_metadata("status.sims_postprocess", "error");

    croak "$0: $msg";
}
    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3