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

View of /FigKernelScripts/split_sequences_into_sets.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (download) (as text) (annotate)
Mon Aug 3 19:48:53 2009 UTC (10 years, 4 months ago) by overbeek
Branch: MAIN
add ability to split a set of sequences into alignable subsets

########################################################################
# -*- 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 FIG;
$min_sz  = 30;
$fullF   = "/tmp/seqs.$$";

$usage = "usage: split_sequences_into_sets OutputDirectory [SimilarityCutoff FracCoverage] [blastout=BlastFile] < full_seqs";

for ($i= $#ARGV; ($i >= 0); $i--)
{
    if ($ARGV[$i] =~ /blastout=(\S+)$/)
    {
	$blastout = $1;
	splice(@ARGV,$i,1);
    }
}

(
 ($output = shift @ARGV)
)
    || die $usage;

(! -e $output) || die "$output already exists -- check it (and delete it, if that is what you really wish)";
&FIG::verify_dir($output);

open(TMP,">$fullF") || die "could not open $fullF";
$/ = "\n>";
while (defined($_ = <STDIN>))
{
    chomp;
    if ($_ =~ /^>?(\S+)[^\n]*\n(.*)/s)
    {
	$id  =  $1;
	$seq =  $2;
	$seq =~ s/\s//gs;
	$seq =~ s/[uU]/x/g;
	$seq_of{$id} = $seq;
	print TMP ">$id\n$seq\n";
    }
}
close(TMP);
$/ = "\n";

if (@ARGV == 2)
{
    $sim_cutoff = shift @ARGV;
    $frac_cov   = shift @ARGV;
}
else
{
    $sim_cutoff = 1.0e-30;
    $frac_cov   = 0.70;
}

&run("$FIG_Config::ext_bin/formatdb -i $fullF -p T");

open(BLASTOUT,"$FIG_Config::ext_bin/blastall -i $fullF -d $fullF -FF -m 8 -p blastp -b 20000 -v 20000 |")
    || die "could not run blastall -i $fullF -d $fullF -FF -m 8 -p blastp -b 20000 -v 20000";
my @all = ();
while (defined($_ = <BLASTOUT>))
{
    if (($_ =~ /^(\S+)\t(\S+)\t(\S+\t){4}(\S+)\t(\S+)\t(\S+)\t(\S+)\s+(\S+)/) &&
	($1 ne $2) && 
	($8 <= $sim_cutoff) &&
	((@all == 0) || (($1 ne $all[$#all]->[0]) || ($2 ne $all[$#all]->[1]))))
    {
	push(@all,[$1,$2,$4,$5,$6,$7,$8]);
    }
}
close(BLASTOUT);

if (defined($blastout))
{
    open(BLASTOUT,">$blastout") || die "could not open $blastout";
    foreach $_ (@all)
    {
	print BLASTOUT join("\t",(@$_,length($seq_of{$_->[0]}),length($seq_of{$_->[1]}))),"\n";
    }
    close(BLASTOUT);
}

@sims = grep { ($_->[6] <= $sim_cutoff) }
        grep { ($_->[0] ne $_->[1]) &&
                ((($_->[3] - $_->[2]) / length($seq_of{$_->[0]})) >= $frac_cov) &&
		((($_->[5] - $_->[4]) / length($seq_of{$_->[1]})) >= $frac_cov)
             }
        @all;

if (@sims == 0)
{
    print STDERR "No similarities worth processing:\n"
	, join("\n", map { join(", ", @ { $_ }) } @all), "\n"
	, "--- deleting $output\n";
    system("rm -fR $output") && warn "Could not delete directory $output";
    exit(0);
}

foreach $sim (@sims)
{
    if (! $seen{"$sim->[0]\t$sim->[1]"})  # pick only the best similarity between two ids
    {
	push(@{$conn{$sim->[0]}},$sim);
	$seen{"$sim->[0]\t$sim->[1]"} = 1;
    }
}
undef %seen;

if (0)      # This is just for debugging -- display the similarities
{
    foreach $id (sort keys(%seq_of))
    {
	&print_sims($id,\@sims);
    }
}

# @ids = sort { (@{$conn{$b}} <=> @{$conn{$a}}) or (&cov($conn{$b}) <=> &cov($conn{$a})) } 
# sort by number of connections; within that on sum of coverage

@ids = sort { &cov($conn{$b}) <=> &cov($conn{$a}) } 
       keys(%seq_of);  
# sort by sum of coverage

@tmp = map { $x = $_; [$x,scalar @{$conn{$x}},&cov($conn{$x})] } @ids;
$n = 1;
@sets = ();

open(SZ,">$output/set.sizes") || die "could not open $output/set.sizes";

foreach $central_id (@ids)
{
    if (! $seen{$central_id})
    {
#	print STDERR "========\n";
#	print STDERR "picking central_id = $central_id\n";

	$set = &extract_set($central_id,$conn{$central_id},\%seq_of,\%seen);
#	print STDERR "========\n";
#	print STDERR "calculated set:", &Dumper($et);

	print SZ join("\t",($n,scalar @$set)),"\n";

	push(@sets,[$n,$set]);

	open(OUT,">$output/$n") || die "could not open $output/$n";
	$n++;
	foreach $tuple (@$set)
	{
	    ($id,$b,$e,$seq) = @$tuple;
	    $seen{$id} = 1;
	    print OUT ">$id\n$seq\n";
	}
	close(OUT);
    }
}
close(SZ);

@distances = &construct_distance_matrix(\@sets,\@all); # entries are [n1,n2,sc]
open(DIST,">$output/distance.matrix\n") || die "could not open $output/distance.matrix";
foreach $tuple (@distances)
{
    print DIST join("\t",@$tuple),"\n";
}
close(DIST);


system("rm $fullF\*");

sub possible_region {
    my($r1,$r2) = @_;

    $r2 = $r2 ? $r2 : [1,100000];

    my($b1,$e1) = @$r1;
    my($b2,$e2) = @$r2;

    my $b = ($b1 > $b2) ? $b1 : $b2;
    my $e = ($e1 < $e2) ? $e1 : $e2;

    if ((($e-$b)+1) >= $min_sz)
    {
	return [$b,$e];
    }
    return undef;
}

sub cov {
    my($sims) = @_;
    my($x,$n);

    $n = 0;
    foreach $x (@$sims)
    {
	$n += ($x->[3] - $x->[2]);
    }
    return $n;
}

sub run {
    my($cmd) = @_;

#   my @tmp = `date`; chomp @tmp; print STDERR "$tmp[0]: running $cmd\n";
    (system($cmd) == 0) || confess "FAILED: $cmd";
}

sub print_sims {
    my($id,$sims) = @_;
    my($sim,@tmp);

    print STDERR "$id\n";
    foreach $sim (@$sims)
    {
	if ($sim->[0] eq $id)
	{
	    @tmp = @$sim;
	    $tmp[0] = "";
	    print STDERR join("\t",@tmp),"\n";
	}
    }
}

sub extract_set {
    my($central_id,$sims,$seq_of,$seen) = @_;
    my($sim,$r1,$id1,$id2,$b1,$e1,$b2,$e2);

    my(@tuples) = ();

    my @sims = sort { (($b->[3] - $b->[2]) <=> ($a->[3] - $a->[2])) } @$sims;
    my $region = [1,length($seq_of->{$central_id})];

    foreach $sim (@sims)
    {
	($id1,$id2,$b1,$e1,$b2,$e2) = @$sim;
	if ((! $seen->{$id2}) && ($r1 = &possible_region([$b1,$e1],$region)))
	{
	    $region = $r1;
	}
    }

    my($b1F,$e1F) = @$region;
    my $seq = substr($seq_of->{$central_id},$b1F-1,($e1F-$b1F)+1);
    my $core_ln = length($seq);

    my $singleF = "/tmp/seq.$$";

    open(TMP,">$singleF") || die "could not open $singleF";
    print TMP ">$central_id\n$seq\n";
    close(TMP);

    @sims =  grep { ($_->[6] <= 1.0e-5) && (($_->[3] - $_->[2]) > (0.8 * $core_ln)) }
             map { $_ =~ /^(\S+)\t(\S+)\t(\S+\t){4}(\S+)\t(\S+)\t(\S+)\t(\S+)\s+(\S+)/;
                   [$1,$2,$4,$5,$6,$7,$8] }
             `$FIG_Config::ext_bin/blastall -i $singleF -d $fullF -FF -m 8 -p blastp -b 20000 -v 20000`;
    system "rm $singleF\*";
    undef %seen1;

    foreach $sim (@sims)
    {
	($id1,$id2,$b1,$e1,$b2,$e2) = @$sim;
	if ((! $seen->{$id2}) && (! $seen1{$id2}) && ((($e2-$b2)+1) >= $min_sz))
	{
            $seq = $seq_of{$id2};
##	    $seq = substr($seq_of{$id2},$b2-1,($e2-$b2)+1);  <<<
	    push(@tuples,[$id2,$b2,$e2,$seq]);
	    $seen1{$id2} = 1;
	}
    }
    return [@tuples];
}

sub construct_distance_matrix {
    my($sets,$sims) = @_;
    my($tuple1,$tuple2,$n,$set,$id1,$id2,$sc,$sim,%best,$key,$n1,$n2);
    my(@distances,$sofar);

    foreach $tuple1 (@$sets)
    {
	($n,$set) = @$tuple1;
	foreach $tuple2 (@$set)
	{
	    ($id,$b,$e,$seq) = @$tuple2;
	    $in{$id} = $n;
	}
    }
    foreach $sim (@$sims)
    {
	($id1,$id2,undef,undef,undef,undef,$sc) = @$sim;
	if ($in{$id1} && $in{$id2} && ($in{$id1} != $in{$id2}))
	{
	    $key = join("\t",($in{$id1},$in{$id2}));
	    if ((! ($sofar = $best{$key})) || ($sofar > $sc))
	    {
		$best{$key} = $sc;
	    }

	    $key = join("\t",($in{$id2},$in{$id1}));
	    if ((! ($sofar = $best{$key})) || ($sofar > $sc))
	    {
		$best{$key} = $sc;
	    }
	}
    }

    @distances = ();
    foreach $key (keys(%best))
    {
	($n1,$n2) = split(/\t/,$key);
	push(@distances,[$n1,$n2,$best{$key}]);
    }
    return sort { ($a->[0] <=> $b->[0]) or ($a->[1] <=> $b->[1]) } @distances;
}

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3