[Bio] / FigKernelPackages / ProtSims.pm Repository:
ViewVC logotype

View of /FigKernelPackages/ProtSims.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (download) (as text) (annotate)
Sat Mar 6 23:54:11 2010 UTC (9 years, 9 months ago) by overbeek
Branch: MAIN
CVS Tags: rast_rel_2010_0526, rast_rel_2010_0827
Changes since 1.11: +2 -2 lines
allow use of just blast

# This is a SAS component

#
# 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.
#
package ProtSims;

use strict;
use Sim;
use Data::Dumper;
use Carp;
use gjoseqlib;

my $blat_cmd = "blat";
my $blastall_cmd = "blastall";
my $formatdb_cmd = "formatdb";

BEGIN {
    eval {
	require FIG_Config;
	if (defined $FIG_Config::ext_bin) {
	    $blat_cmd = "$FIG_Config::ext_bin/blat";
	    $blastall_cmd = "$FIG_Config::ext_bin/blastall";
	    $formatdb_cmd = "$FIG_Config::ext_bin/formatdb";
	}
    };
}

sub blastP {
    my($q,$db,$min_hits,$use_blast) = @_;

    my(@q,@db);

    my $qF;
    if (ref $q)
    {
	$qF = "/tmp/query.$$";
	&gjoseqlib::print_alignment_as_fasta($qF,$q);
    }
    else
    {
	$qF = $q;
	if (-e $qF)
	{
	    @q = &gjoseqlib::read_fasta($qF);
	    $q = \@q;
	}
	else
	{
	    die "$qF is missing";
	}
    }

    my $dbF;
    if (ref $db)
    {
	$dbF = "/tmp/db.$$";
	&gjoseqlib::print_alignment_as_fasta($dbF,$db);
	system "$formatdb_cmd -i $dbF";
    }
    else
    {
	$dbF = $db;
	if (-e $dbF)
	{
	    if (! ((-e "$dbF.psq") && ((-M "$dbF.psq") < (-M $dbF))))
	    {
		system "formatdb_cmd -i $dbF";
	    }
	    @db = &gjoseqlib::read_fasta($dbF);
	    $db = \@db;
	}
	else
	{
	    die "$dbF is missing";
	}
    }

    my $tmpF = "/tmp/sim.out.$$";
    my $cmd = $use_blast ? "touch $tmpF" : "$blat_cmd $dbF $qF -prot -out=blast8 $tmpF > /dev/null";
    #print STDERR "$cmd\n";
    my $rc = system $cmd;
    if ($rc != 0)
    {
	die "ProtSims::blastP: blat run failed with rc=$rc: $cmd\n";
    }

    my @sims1 = ();
    open(BLAT,"<$tmpF") || die "could not open $tmpF";
    my $sim = <BLAT>;
    while ($sim && ($sim=~ /^(\S+\t\S+)/))
    {
	my $pegs = $1;
	my @pieces = ();
	while ($sim && ($sim=~ /^((\S+\t\S+).*\S)/) && ($2 eq $pegs))
	{
	    push(@pieces,[split(/\t/,$1)]);
	    $sim = <BLAT>;
	}
	push(@sims1,&condense(\@pieces));
    }
    close(BLAT);
    unlink $tmpF;
    #print STDERR &Dumper(sims1 => \@sims1);

    my @rerun = ();
    my @sims  = ();
    my $qI    = 0;
    my $simI  = 0;
    while ($qI < @$q)
    {
	my $peg = $q->[$qI]->[0];
	#print STDERR "processing $peg\n";
	my $hits = 0;
	my $simI1 = $simI;
	while (($simI1 < @sims1) && ($sims1[$simI1]->[0] eq $peg)) 
	{
	    if (($simI1 == $#sims1) || ($sims1[$simI1]->[1] ne $sims1[$simI1+1]->[1]))
	    {
		$hits++;
	    }
	    $simI1++;
	}
	#print STDERR "hits=$hits min_hits=$min_hits\n";
	if ($hits >= $min_hits)
	{
	    push(@sims,@sims1[$simI..$simI1-1]);
	}
	else
	{
	    push(@rerun,$q->[$qI]);
	}
	$simI = $simI1;
	$qI++;
    }
    #print STDERR &Dumper(rerun => \@rerun);

    if (@rerun > 0)
    {
	my $tmpQ = "/tmp/tmpQ.$$";
	&gjoseqlib::print_alignment_as_fasta($tmpQ,\@rerun);
	my $cmd = "$blastall_cmd -m 8 -i $tmpQ -d $dbF -FF -p blastp -e 1e-5";
	#print STDERR "$cmd\n";
	open(BL, "$cmd|") or die "ProtSims::blastP: pipe to blast failed with $!: $cmd\n";
	my @blastout = map { chomp; [split(/\s+/,$_)] } <BL>;
	close(BL);
	push(@sims,@blastout);
	unlink $tmpQ;
    }

    my %lnQ   = map { $_->[0] => length($_->[2]) } @$q;
    my %lnDB  = map { $_->[0] => length($_->[2]) } @$db;
    @sims = map { push(@$_,$lnQ{$_->[0]},$lnDB{$_->[1]}); bless($_,'Sim') } @sims;

    if ($qF      eq "/tmp/query.$$")   { unlink $qF  }
    if ($dbF     eq "/tmp/db.$$")      { unlink($dbF,"$dbF.psq","$dbF.pin","$dbF.phr") }
    return sort { ($a->id1 cmp $b->id1) or ($a->psc <=> $b->psc) or ($a->id2 cmp $b->id2) } @sims;
}

sub condense {
    my($pieces) = @_;

    my $best_sc = $pieces->[0]->[10];
    my @sims = sort { ($a->[6] <=> $b->[6]) } @$pieces;
    while (@sims > 1)
    {
	my $gap1 = $sims[1]->[6] - $sims[0]->[7];
	my $gap2 = $sims[1]->[8] - $sims[0]->[9];
	my $diff = abs($gap1 - $gap2);
	if (($gap1 <= 300) && ($gap2 <= 300) && ($diff <= (0.1 * &min($gap1,$gap2))))
	{
	    $sims[0]->[7] = $sims[1]->[7];
	    $sims[0]->[9] = $sims[1]->[9];
	}
	splice(@sims,1,1);
    }
    $sims[0]->[10] = $best_sc;
    return $sims[0];
}

sub min {
    my($x,$y) = @_;
    return ($x <= $y) ? $x : $y;
}

1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3