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

View of /FigKernelScripts/ali_tree.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (download) (as text) (annotate)
Mon Aug 27 18:27:33 2007 UTC (12 years, 2 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.3: +19 -0 lines
add strip comments argument on function_of

########################################################################
# -*- 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 Carp;
use Data::Dumper;
use gjoseqlib;

use FIG;
my $fig = new FIG;

use AliTree;
use AliTrees;

# usage: ali_tree [DIR]

my $default_dir;
if (@ARGV > 0)
{
    $default_dir = shift @ARGV;
}
else
{
    $default_dir = "$FIG_Config::data/AlignmentsAndTrees";
}

use AliTrees;
my $ali_trees = new AliTrees($fig,$default_dir);

my $ali_tree;
my($dir);
while ($req = &get_req)
{
    if ($req =~ /^\s*set\s+(\d+\.\d+\.peg\.\S+)\s*$/)
    {
	$id = $1;
	$ali_tree = new AliTree($id,$fig,$default_dir);
	$ali_tree
	    || warn "could not handle $id in $default_dir\n";
    }
    elsif ($req =~ /^\s*set_all\s*$/)
    {
	$ali_trees->set_all;
	print "set_all completed\n";
    }
    elsif ($req =~ /^\s*all_alignments\s*$/)
    {
	foreach $_ ($ali_trees->all_alignments)
	{
	    print "$_\n";
	}
	print "\n";
    }
    elsif ($req =~ /^\s*delete_ali\s+(\S+)\s*$/)
    {
	$ali = $1;
	$rc = $ali_trees->delete_ali($ali);
	print &Dumper($rc);
	print "\n";
    }
    elsif ($req =~ /^\s*add_ali\s+(\S+)\s*$/)
    {
	$dir_to_add = $1;
	$rc = $ali_trees->add_ali($dir_to_add);
	print &Dumper($rc);
	print "\n";
    }
    elsif ($req =~ /^\s*alignments_containing_peg\s+(\S+)\s*$/)
    {
	$peg = $1;
	foreach $_ ($ali_trees->alignments_containing_peg($peg))
	{
	    print "$_\n";
	}
	print "\n";
    }
    elsif ($req =~ /^\s*functions_in(\s+(\S+))?\s*$/)
    {
	$ali_tree = $2 ? new AliTree($2,$fig,$default_dir) : $ali_tree;
	my $ali = $ali_tree->id;

	my($pegM,$funcM);
	if ($ali =~ /^(\d+\.\d+\.peg\.\d+)/)
	{
	    $pegM = 'fig|' . $1;
	    $funcM = $fig->function_of($pegM);
	    print "$ali\t$pegM\t$funcM\n\n";
	}
	foreach $_ ($ali_tree->functions_in)
	{
	    print join("\t",@$_),"\n";
	}
	print "\n";
    }
    elsif ($req =~ /^\s*pegs_in_alignment(\s+(\S+))?\s*$/)
    {
	$ali_tree = $2 ? new AliTree($2,$fig,$default_dir) : $ali_tree;
	if (! $ali_tree) 
	{ 
	    print "you need to specify an alignment\n\n";
	}
	else
	{
	    my $h = $ali_tree->pegs_in_alignment($ali_tree->id);
	    foreach $peg (sort { &FIG::by_fig_id($a,$b) } keys(%$h))
	    {
		my $coords = $h->{$peg};
		print join("\t",($peg,@$coords)),"\n";
	    }
	}
	print "\n";
    }
    elsif ($req =~ /^\s*merge\s+(\S+)\s+(\S+)\s*$/)
    {
	$aliID1 = $1;
	$aliID2 = $2;
	$aliID3 = $ali_trees->merge_ali($aliID1,$aliID2);
	print defined($aliID3) ? $aliID3 : "failed to merge $aliID1 and $aliID2","\n\n";
    }
    elsif ($req =~ /^\s*indel_ratio(\s+(\S+))?\s*$/)
    {
	$ali_tree = $2 ? new AliTree($2,$fig,$default_dir) : $ali_tree;
	if (! $ali_tree) 
	{ 
	    print "you need to specify an alignment\n\n";
	}
	else
	{
	    print $ali_tree->indel_ratio,"\n";
	}
	print "\n";
    }
    elsif ($req =~ /^\s*overlaps(\s+(\S+))?\s*$/)
    {
	$ali_tree = $2 ? new AliTree($2,$fig,$default_dir) : $ali_tree;
	if (! $ali_tree) 
	{ 
	    print "you need to specify an alignment\n\n";
	}
	else
	{
	    $sz = $ali_tree->sz;
	    print "Overlaps for ",$ali_tree->id,", which contains $sz seqauences\n\n";
	    $overlaps = $ali_tree->overlaps;
	    @tmp = ();
	    foreach $ali1 (keys(%$overlaps))
	    {
		my $ali_tree1 = new AliTree($ali1,$fig,$default_dir);
		push(@tmp,[$ali1,scalar @{$overlaps->{$ali1}},$ali_tree1->sz]);
	    }
	    @tmp = sort { $b->[1] <=> $a->[1] } @tmp;
	    foreach $tuple (@tmp)
	    {
		print join("\t",@$tuple),"\n";
	    }
	}
	print "\n";
    }
    elsif ($req =~ /^\s*tree(\s+(\S+))?\s*$/)
    {
	$ali_tree = $2 ? new AliTree($2,$fig,$default_dir) : $ali_tree;
	if (! $ali_tree) 
	{ 
	    print "you need to specify an alignment\n\n";
	}
	else
	{
	    $id = $ali_tree->id;
	    $tree = $ali_tree->tree;
	    if (-s "$default_dir/Library/$id/tree.newick")
	    {
		system "sketch_tree -a $default_dir/Library/$id/tree.newick";
	    }
	    else
	    {
		print "no tree for $id\n";
	    }
	}
	print "\n";
    }
    elsif ($req =~ /^\s*sz(\s+(\S+))?\s*$/)
    {
	$ali_tree = $2 ? new AliTree($2,$fig,$default_dir) : $ali_tree;
	if (! $ali_tree) 
	{ 
	    print "you need to specify an alignment\n\n";
	}
	else
	{
	    print $ali_tree->sz;;
	}
	print "\n";
    }
    elsif ($req =~ /^\s*display(\s+(\S+))?\s*$/)
    {
	$ali_tree = $2 ? new AliTree($2,$fig,$default_dir) : $ali_tree;
	if (! $ali_tree) 
	{ 
	    print "you need to specify an alignment\n\n";
	}
	else
	{
	    print $ali_tree->display;
	}
	print "\n";
    }
    elsif ($req =~ /^\s*html(\s+(\S+))?\s*$/)
    {
	$ali_tree = $2 ? new AliTree($2,$fig,$default_dir) : $ali_tree;
	if (! $ali_tree) 
	{ 
	    print "you need to specify an alignment\n\n";
	}
	else
	{
	    my $id = $ali_tree->id;
	    my $tmpdir = "$FIG_Config::fig/Tmp/Alignments";
	    &FIG::verify_dir($tmpdir);
	    if (! -s "$tmpdir/$id.html")
	    {
		if (open(HTML,">$tmpdir/$id.html"))
		{
		    print HTML $ali_tree->html,"\n";
		    close(HTML);
		}
	    }
	}
	print "\n";
    }
    elsif ($req =~ /^\s*load_db\s*$/)
    {
	$ali_trees->load_db;
    }
    elsif ($req =~ /^\s*h\s*$/ || $req =~ /^\s*help\s*$/)
    {
     &help;
    }
    else
    {
	print "invalid command\n";
    }
    print "\n";
}

sub get_req {
    my($x);

    print "?? ";
    $x = <STDIN>;
    while (defined($x) && ($x =~ /^h$/i) )
    { 
	&help;
	print "?? ";
	$x = <STDIN>;
    }
    
    if ((! defined($x)) || ($x =~ /^\s*[qQxX]/))
    {
	return "";
    }
    else
    {
        if ($echo)
	{
	    print ">> $x\n";
	}
	return $x;
    }
}


sub help {
    print <<END;
    
    add_ali                          Ali
    alignments_containing_peg        PEG
    all_alignments
    delete_ali                       Ali
    display                          [Ali]
    functions_in                     [Ali]
    h                               
    html                             Ali
    indel_ratio                      [Ali]
    load_db                        
    merge                            Ali1 Ali2 [creates Ali1-n and displays the computed ID]
    overlaps                         [Ali]
    pegs_in_alignment                [Ali]
    set                              Ali
    set_all                          
    sz                               [Ali]
    tree                             [Ali]
END
}

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3