[Bio] / FigKernelScripts / promote_orfs_to_pegs.pl Repository:
ViewVC logotype

View of /FigKernelScripts/promote_orfs_to_pegs.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (download) (as text) (annotate)
Wed Feb 13 14:54:27 2008 UTC (11 years, 10 months ago) by gdpusch
Branch: MAIN
CVS Tags: rast_rel_2009_05_18, rast_rel_2008_06_18, rast_rel_2008_06_16, rast_rel_2008_12_18, rast_rel_2008_07_21, rast_2008_0924, rast_rel_2008_04_23, rast_rel_2008_09_30, rast_rel_2009_0925, rast_rel_2010_0526, rast_rel_2010_0118, mgrast_rel_2008_0924, mgrast_rel_2008_1110_v2, rast_rel_2009_02_05, mgrast_rel_2008_0625, rast_rel_2008_10_09, rast_release_2008_09_29, mgrast_rel_2008_0806, mgrast_rel_2008_0923, mgrast_rel_2008_0919, rast_rel_2009_07_09, mgrast_rel_2008_1110, rast_rel_2008_09_29, mgrast_rel_2008_0917, rast_rel_2008_10_29, rast_rel_2009_03_26, rast_rel_2008_11_24, rast_rel_2008_08_07
Changes since 1.12: +29 -9 lines
Final clean-up. -- /gdp

# -*- perl -*-
########################################################################
# Copyright (c) 2003-2006 University of Chicago and Fellowship
# for Interpretations of Genomes. All Rights Reserved.
#
# This file is part of the SEED Toolkit.
#
# The SEED Toolkit is free software. You can redistribute
# it and/or modify it under the terms of the SEED Toolkit
# Public License.
#
# You should have received a copy of the SEED Toolkit Public License
# along with this program; if not write to the University of Chicago
# at info@ci.uchicago.edu or the Fellowship for Interpretation of
# Genomes at veronika@thefig.info or download a copy from
# http://www.theseed.org/LICENSE.TXT.
########################################################################

use strict;
use warnings;

use FIG;
my $fig = new FIG;

use NewGenome;
use ToCall;

$0 =~ m/([^\/]+)$/o;
my $self  = $1;
my $usage = "usage: promote_orfs_to_pegs To_Call_Dir FoundFamiliesFile";

if (@ARGV && ($ARGV[0] =~ m/-h(elp)?/)) {
    print STDERR "\n   $usage\n\n";
    exit (0);
}

my ($to_call_dir, $found_file);

(
 ($to_call_dir = shift @ARGV)
&&
 ($found_file  = shift @ARGV)
)
    || die "\n   $usage\n\n";

$to_call_dir =~ s/\/$//o;

my $to_call;
$to_call = NewGenome->new($to_call_dir, "all");

my @neighbors = map { m/^(\d+\.\d+)/o ? ($1) : () } `cat $to_call_dir/neighbors`;

open(CALLED_BY, ">>$to_call_dir/called_by")
    || die "could not open $to_call_dir/called_by";

#...Build file of "nearby" PEGs
if (@neighbors) {
    my $tmp_close = "$FIG_Config::temp/tmp_close_$$.fasta";
    system("/bin/rm -f $tmp_close*");
    
    foreach my $org (@neighbors) {
	print STDERR "Appending $org to $tmp_close\n" if $ENV{VERBOSE};
	system("cat $FIG_Config::organisms/$org/Features/peg/fasta >> $tmp_close");
    }
    &FIG::run("formatdb -i$tmp_close -pT");

    my @promoted_fids;
    my $tmp_sims = "$FIG_Config::temp/tmp_sims.$$";
    open(TMP_SIMS, ">$tmp_sims")
	|| die "Could not write-open $tmp_sims";
    foreach my $orf_id ($to_call->get_fids_for_type('orf')) {
	if (@neighbors) {
	    my $sims = &blast_against($to_call->get_feature_sequence($orf_id), $tmp_close);
	    
	    if (@$sims) {
		my $num_sims = (scalar @$sims);
		print STDERR "Promoting $orf_id based on $num_sims sims\n" if $ENV{VERBOSE};
		
		my $orf = &NewGenome::ORF::new('NewGenome::ORF', $to_call, $orf_id);
		my $annot  = [ qq(RAST), 
			       qq(Called by "$self" based on $num_sims sims.)
			       ];
		my $fid = $orf->promote_to_peg($sims, undef, $annot);
		if ($fid) {
		    push @promoted_fids, $fid;
		    @$sims = map { $_->[0] = $fid; $_ } @$sims;
		    print TMP_SIMS map { join("\t", @$_) . qq(\n) } @$sims;
		    
		    print CALLED_BY "$fid\tpromote_remaining_orfs (with sims)\n";
		    
		    print STDERR "Succeeded:\t$orf_id --> $fid\n";
		}
		else {
		    print STDERR "Could not promote ORF $orf_id to a PEG\n";
		}
	    }
	    else {
		print STDERR "No sims for $orf_id\n" if $ENV{VERBOSE};
	    }
	}
    }
    close(TMP_SIMS) || die "Could not close $tmp_sims";

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Disable the auto assignment of the fids we just called.
#=======================================================================
    if (0)
    {
	my $tmp_seqs = "$FIG_Config::temp/tmp_seqs.$$";
	open(TMP_SEQS, ">$tmp_seqs")
	    || die "Could not write-open $tmp_seqs";
	print TMP_SEQS map { join("\t", ($_, $to_call->get_feature_sequence($_))) . qq(\n) } @promoted_fids;
	close(TMP_SEQS) || die "Could not close $tmp_seqs";
	my @auto_assigns = `cat $tmp_seqs | auto_assign sims=$tmp_sims`;
	
	open(FOUND, ">>$found_file")
	    || die "Could not append-open FoundFamiliesFile $found_file";
	foreach my $entry (@auto_assigns) {
	    if ($entry =~ m/^(\S+)\t(.*)$/o) {
		if ($to_call->set_function($1, $2)) {
		    print FOUND "$1\tCLOSE_SIMS\t$2\n";
		}
		else {
		    die "Could not set function of $1 to $2";
		}
	    }
	    else {
		die "Could not parse auto-assignment: $entry";
	    }
	}
	close(FOUND) || die "Could not close $found_file";
	unlink($tmp_seqs)  || die "Could not remove $tmp_seqs";
    }
    
    unlink($tmp_sims)  || die "Could not remove $tmp_sims";
    unlink($tmp_close) || die "Could not remove $tmp_close";
}
#-----------------------------------------------------------------------


#...Promote any remaining unpromoted ORFs...
$to_call->promote_remaining_orfs(\*CALLED_BY);

close(CALLED_BY);

#...NOTE: export_features also writes assigned_functions
$to_call->export_features;


if (! -s "$to_call_dir/Features/orf/tbl") {
    # Hrm, weird NFS effects here. There was a failure of this even though the directory
    # was indeed empty.
    if (system("/bin/rm -fRv $to_call_dir/Features/orf")) {
	warn "First remove did not work; sleeping and trying again";
	sleep(30);
	system("/bin/rm -fRv $to_call_dir/Features/orf");
    }
}
else {
    die "Something is wrong --- $to_call_dir/Features/orf/tbl is non-empty";
}
exit(0);



sub blast_against {
    my ($seq, $db) = @_;
    my $seq_len = length($seq);
    
    my $tmp_seq = "$FIG_Config::temp/tmp_seq.$$";
    open( TMP, ">$tmp_seq" ) || die "Could not write-open $tmp_seq";
    print TMP  ">query\n$seq\n";
    close(TMP) || die "Could not close $tmp_seq";
    
    my $sims = [];
    @$sims = map { chomp $_;
		   my @x = split(/\t/, $_);
		   push @x, ($seq_len, $fig->translation_length($x[1]));
		   bless( [@x], "Sim" )
		   } `blastall -i $tmp_seq -d $db -p blastp -m8 -e1.0e-10`;
    
    unlink($tmp_seq) || die "Could not unlink $tmp_seq";
    return $sims;
}

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3