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

View of /FigKernelScripts/split_and_trim_sequences.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (download) (as text) (annotate)
Wed May 9 14:35:43 2007 UTC (12 years, 9 months ago) by overbeek
Branch: MAIN
CVS Tags: mgrast_dev_08112011, rast_rel_2009_05_18, mgrast_dev_08022011, rast_rel_2014_0912, rast_rel_2008_06_18, myrast_rel40, rast_rel_2008_06_16, mgrast_dev_05262011, rast_rel_2008_12_18, mgrast_dev_04082011, rast_rel_2008_07_21, rast_rel_2010_0928, rast_2008_0924, mgrast_version_3_2, mgrast_dev_12152011, rast_rel_2008_04_23, mgrast_dev_06072011, rast_rel_2008_09_30, rast_rel_2009_0925, rast_rel_2010_0526, rast_rel_2014_0729, mgrast_dev_02212011, rast_rel_2010_1206, mgrast_release_3_0, mgrast_dev_03252011, rast_rel_2010_0118, mgrast_rel_2008_0924, mgrast_rel_2008_1110_v2, rast_rel_2009_02_05, rast_rel_2011_0119, mgrast_rel_2008_0625, mgrast_release_3_0_4, mgrast_release_3_0_2, mgrast_release_3_0_3, mgrast_release_3_0_1, mgrast_dev_03312011, mgrast_release_3_1_2, mgrast_release_3_1_1, mgrast_release_3_1_0, mgrast_dev_04132011, rast_rel_2008_10_09, mgrast_dev_04012011, rast_release_2008_09_29, mgrast_rel_2008_0806, mgrast_rel_2008_0923, mgrast_rel_2008_0919, rast_rel_2009_07_09, rast_rel_2010_0827, mgrast_rel_2008_1110, myrast_33, rast_rel_2011_0928, rast_rel_2008_09_29, mgrast_rel_2008_0917, rast_rel_2008_10_29, mgrast_dev_04052011, mgrast_dev_02222011, rast_rel_2009_03_26, mgrast_dev_10262011, rast_rel_2008_11_24, rast_rel_2008_08_07, HEAD
Changes since 1.13: +4 -2 lines
fix problem when repeats occur

# -*- 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.$$";
$singleF = "/tmp/seq.$$";

$usage = "usage: split_and_trim_sequences 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);

&make_sure("formatdb","blastall");

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.80;
}

&run("formatdb -i $fullF -p T");

open(BLASTOUT,"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 $b-$e\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\* $singleF\*");

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 make_sure {
    my(@progs) = @_;

    my $prog;
    foreach $prog (@progs)
    {
	my @tmp = `which $prog`;
	if ($tmp[0] =~ /^no $prog/)
	{
	    print STDERR $tmp[0];
	    exit(1);
	}
    }
}

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);

    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] }
             `blastall -i $singleF -d $fullF -FF -m 8 -p blastp -b 20000 -v 20000`;

    undef %seen1;
    foreach $sim (@sims)
    {
	($id1,$id2,$b1,$e1,$b2,$e2) = @$sim;
	if ((! $seen->{$id2}) && (! $seen1{$id2}) && ((($e2-$b2)+1) >= $min_sz))
	{
	    $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