[Bio] / FigWebServices / find_poss_subsys_instances.cgi Repository:
ViewVC logotype

View of /FigWebServices/find_poss_subsys_instances.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (download) (annotate)
Sun Nov 18 15:09:30 2007 UTC (12 years 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.1: +55 -57 lines
fixes to find_poss_subsys_instances.cgi

# -*- 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;
my $fig = new FIG;

use Subsystem;

use HTML;
use strict;
use Carp;

use CGI;
my $cgi = new CGI;

if (0)
{
    my $VAR1;
    eval(join("",`cat /tmp/find_subsys_parms`));
    $cgi = $VAR1;
#    print STDERR &Dumper($cgi);
}

if (0)
{
    print $cgi->header;
    my @params = $cgi->param;
    print "<pre>\n";
    foreach $_ (@params)
    {
	print "$_\t:",join(",",$cgi->param($_)),":\n";
    }

    if (0)
    {
	if (open(TMP,">/tmp/find_subsys_parms"))
	{
	    print TMP &Dumper($cgi);
	    close(TMP);
	}
    }
    exit;
}

use FigFams;
my $figfams = new FigFams($fig);

use ToCall;

my $html = [];
unshift @$html, "<TITLE>Find Instances of a Subsystem</TITLE>\n";

my $subsys       = $cgi->param('subsystem');
my $roles        = $cgi->param('roles');
my $definitions  = $cgi->param('definitions');
my $rules        = $cgi->param('rules');
my @orgs         = $cgi->param('korgs');
@orgs            = map { $_ =~ /(\d+\.\d+)/; $1 } @orgs;
my(@rolesI,@rulesI,@definitionsI);
if ($roles =~ /\S/)
{
    $roles     =~ tr/\r/\n/;
    @rolesI     = grep { $_ } split(/\n/,$roles);
}

if ($definitions =~ /\S/)
{
    $definitions     =~ tr/\r/\n/;
    @definitionsI     = grep { $_ } split(/\n/,$definitions);
}

if ($rules =~ /\S/)
{
    $rules     =~ tr/\r/\n/;
    @rulesI     = grep { $_ } split(/\n/,$rules);
}

my %org_labels;

if ((! $subsys) || (@rolesI < 1) || (@rulesI < 1) || (@orgs < 1))
{
    my @orgs = ();
    foreach my $org ($fig->genomes('complete'))
    {
	my $label = &compute_genome_label($fig, $org);
	$org_labels{$org} = $label;
	push(@orgs, $org);
    }
    @orgs = sort { lc( $org_labels{$a} ) cmp lc( $org_labels{$b} ) } @orgs;
    my @org_names = map { $org_labels{$_} } @orgs;

    my @sub = $fig->all_subsystems;
    push(@$html,$cgi->start_form(-action => "find_poss_subsys_instances.cgi", -method => 'get'),
                $cgi->scrolling_list( -name   => 'subsystem',
		  	              -values => [ sort @sub ],
				       -size   => 1
				       ),$cgi->br,
	        $cgi->h2('Roles'),$cgi->textarea( -name => 'roles', -rows => 20, columns => 200), $cgi->br, $cgi->hr,$cgi->br,
	        $cgi->h2('Definitions'),$cgi->textarea( -name => 'definitions', -rows => 20, columns => 200),  $cgi->hr,$cgi->br, $cgi->br,
	        $cgi->h2('Rules'),$cgi->textarea( -name => 'rules', -rows => 20, columns => 200), $cgi->br,  $cgi->hr,$cgi->br,
	        $cgi->scrolling_list( -name   => 'korgs',
				      -values => [ @orgs ],
				      -labels => \%org_labels,
				      -size   => 10,
                                      ), $cgi->br,
	        $cgi->submit( 'Compute Predicted Variant Codes' ),
	        $cgi->end_form
	 );
}
else
{
    my %genomesS = map { $_ => 1 } @orgs;

    my $encoding = [[],0];   # Encoding is a 2-tuple [Memory,NxtAvail]
    my $abbrev_to_loc = {};

    my @roles = &load_roles($cgi,$html,$encoding,$abbrev_to_loc,\@rolesI);
    if (@roles < 1)
    {
	push(@$html,$cgi->h1("Roles are invalid"));
    }
    else
    {
	my $rc = &load_definitions($cgi,$html,$encoding,$abbrev_to_loc,\@definitionsI);
	if (! $rc)
	{
	    push(@$html,$cgi->h1("Definitions are invalid"));
	}
	else
	{
	    my @rules = &parse_rules($cgi,$html,$encoding,$abbrev_to_loc,\@rulesI);
	    if (@rules < 1)
	    {
		push(@$html,$cgi->h1("Rules are invalid"));
	    }
	    else
	    {
		my $n = @rules;
		my $operational = 0;
		foreach my $genome (map { $_->[0] } 
				    sort { $a->[1] cmp $b->[1] } 
				    map { [$_,$fig->genus_species($_)] } 
				    @orgs)
		{
		    my($vcT,$tab) = &find_vc($encoding,\@roles,$figfams,$abbrev_to_loc,\@rules,$fig,$genome,$cgi);
		    my $gs = $fig->genus_species($genome);
		    push(@$html,$cgi->h1("$vcT for $genome [$gs]"),$tab);
		}
	    }
	}
    }
}
&HTML::show_page($cgi,$html);


sub find_vc {
    my($encoding,$roles,$figfams,$abbrev_to_loc,$rules,$fig,$genome,$cgi) = @_;

    my $vcT = undef;
    my $rule;

    my $role;
    my $relevant_genes = {};
    my $tab = [];
    foreach $role (@$roles)
    {
	$relevant_genes->{$role} = &gather_genes($fig,$genome,$figfams,$role);
	foreach my $x (@{$relevant_genes->{$role}})
	{
	    my($peg,$func) = @$x;
	    my $link = &HTML::fid_link($cgi,$peg,1);
	    push(@$tab,[$link,$func]);
	}
    }

    my($i,$matched);
    for ($i=0, $matched=undef; (! defined($matched)) && ($i < @$rules); $i++)
    {
	$matched = &is_rule_true($rules->[$i],$relevant_genes);
    }

    my $vcT = defined($matched) ? $matched : -1;
    my $pegs = &HTML::make_table(["PEG","Proposed Function"],$tab,"PEGs with Relevant Functions");
    return ($vcT,$pegs);
}

sub load_roles {
    my($cgi,$html,$encoding,$abbrev_to_loc,$rolesI) = @_;

    my @roles = ();
    foreach $_ (@$rolesI)
    {
	if ($_ =~ /^(\S+)\s+(\S.*\S)/)
	{
	    my($abbrev,$role) = ($1,$2);
	    my $loc = &add_to_encoding($encoding,['role',$role]);
	    $abbrev_to_loc->{$abbrev} = $loc;
	    push(@roles,$role);
	}
	elsif ($_ =~ /\S/)
	{
	    push(@$html,$cgi->h1("Invalid Role: $_"));
	}
    }
    return @roles;
}

sub add_to_encoding {
    my($encoding,$val) = @_;

    my($mem,$nxt) = @$encoding;
    $mem->[$nxt] = $val;
    $encoding->[1]++;
    return $nxt;
}

sub load_definitions {
    my($cgi,$html,$encoding,$abbrev_to_loc,$defI) = @_;

    my $rc = 1;
    foreach my $def (@$defI)
    {
	if ($def =~ /^(\S+)\s+(\S.*\S)/)
	{
	    my($abbrev,$bool) = ($1,$2);
	    my $loc = &parse_bool($bool,$encoding,$abbrev_to_loc);
	    $abbrev_to_loc->{$abbrev} = $loc;
	}
	elsif ($def =~ /\S/)
	{
	    push(@$html,$cgi->h1("Invalid Definition: $def"));
	    $rc = 0;
	}
    }
    return $rc;
}

sub parse_rules {
    my($cgi,$html,$encoding,$abbrev_to_loc,$rulesI) = @_;

    my @rules = ();
    foreach $_ (@$rulesI)
    {
	my($boolexp,$variant_code,$loc);
	if (($_ =~ /^\s*(\S+)\s+(\S.*\S)\s*$/) &&
	    (($variant_code,$boolexp) = ($1,$2)) &&
	    defined($loc = &parse_bool($boolexp,$encoding,$abbrev_to_loc)))
	{
	    push(@rules,[$variant_code,[$encoding->[0],$loc]]);
	}
	elsif ($_ =~ /\S/)
	{
	    push(@$html,$cgi->h1("Invalid rule: $_"));
	}
    }
    return @rules;
}

sub parse_bool {
    my($s,$encoding,$abbrev_to_loc) = @_;

    my $input = $s;
    my $abbrev;
    foreach $abbrev (sort { length($b) <=> length($a) } keys(%$abbrev_to_loc))
    {
	my $loc = $abbrev_to_loc->{$abbrev};
	my $abbrevQ = quotemeta $abbrev;
	while ($s =~ s/(^|[\s\{,(])($abbrevQ)($|[\s\},)])/$1<$loc>$3/) {}
    }
    my $got = 0;
    while ($s !~ /^\s*<\d+>\s*$/)
    {
	my $nxt = $encoding->[1];
	if ($s =~ s/\(\s*(<\d+>)\s*\)/$1/)
	{
	    $got = 1;
	}
	elsif ($s =~ s/not\s+<(\d+)>/<$nxt>/)
	{
	    &add_to_encoding($encoding,["not",$1]);
	    $got = 1;
	}
	elsif ($s =~ s/<(\d+)>\s+and\s+<(\d+)>/<$nxt>/)
	{
	    &add_to_encoding($encoding,["and",$1,$2]);
	    $got = 1;
	}
	elsif ($s =~ s/<(\d+)>\s+or\s+<(\d+)>/<$nxt>/)
	{
	    &add_to_encoding($encoding,["or",$1,$2]);
	    $got = 1;
	}
	elsif ($s =~ s/<(\d+)>\s+->\s+<(\d+)>/<$nxt>/)
	{
	    &add_to_encoding($encoding,["->",$1,$2]);
	    $got = 1;
	}

	elsif ($s =~ s/(\d+)\s+of\s+\{\s*(<\d+>(,\s*<\d+>)*)\s*\}/<$nxt>/)
	{
	    my $n = $1;
	    my $args = $2;
	    my @args = map { $_ =~ /<(\d+)>/; $1 } split(/,\s*/,$args);
	    &add_to_encoding($encoding,["of",$n,[@args]]);
	    $got = 1;
	}
	last if (! $got);
    }
    return ($s =~ /^\s*<(\d+)>\s*$/) ? $1 : undef;
}

sub gather_genes {
    my($fig,$genome,$figfams,$role) = @_;
    my($fam_id,$peg,$hit,%found);

#   print STDERR "Gathering for role=$role\n";

    my $genomeO = new ToCall("$FIG_Config::organisms/$genome");
    my @fam_ids = $figfams->families_with_functional_role($role);
#   print STDERR "Families for role: ",join(",",@fam_ids),"\n";

    foreach $fam_id (@fam_ids)
    {
	my $figfam = new FigFam($fig,$fam_id);
	my $query;

	foreach $query ($figfam->representatives)
	{
	    foreach $hit ($genomeO->candidate_orfs(-seq => $query))
	    {
#		print STDERR "        candidate=",$hit->get_fid,"\n";
		my $hit_seq = $hit->seq;
		my($ok,undef) = $figfam->should_be_member($hit_seq);
 		if ($ok)
		{
#		    print STDERR "             match\n";
		    $found{$hit->get_fid} = $figfam->family_function;
		}
	    }
	}
    }
    return [sort { &FIG::by_fig_id($a->[0],$b->[0]) } map { [$_,$found{$_}] } keys(%found)];
}

sub is_rule_true {
    my($rule,$relevant_genes) = @_;

    my($variant,$exp) = @$rule;
    return &is_true_exp($exp,$relevant_genes) ? $variant : undef;
}

sub is_true_exp {
    my($bool,$relevant_genes) = @_;

    my($nodes,$root) = @$bool;
    my $val = $nodes->[$root];
    if (! ref  $val) 
    { 
	return &is_true_exp([$nodes,$val],$relevant_genes);
    }
    else
    {
	my $op = $val->[0];

	if ($op eq 'role')
	{
	    my $x;
	    return (($x = $relevant_genes->{$val->[1]}) && (@$x > 0)) ? 1 : 0;
	}
	elsif ($op eq "of")
	{
	    my $truth_value;
	    my $count = 0;
	    foreach $truth_value (map { &is_true_exp([$nodes,$_],$relevant_genes) } @{$val->[2]})
	    {
		if ($truth_value) { $count++ }
	    }
	    return $val->[1] <= $count;
	}
	elsif ($op eq "not")
	{
	    return &is_true_exp([$nodes,$val->[1]],$relevant_genes) ? 0 : 1;
	}
	else
	{
	    my $v1 = &is_true_exp([$nodes,$val->[1]],$relevant_genes);
	    my $v2 = &is_true_exp([$nodes,$val->[2]],$relevant_genes);
	    if ($op eq "and") { return $v1 && $v2 };
	    if ($op eq "or")  { return $v1 || $v2 };
	    if ($op eq "->")  { return ((not $v1) || $v2) }
	    else 
	    {
		print STDERR &Dumper($val);
		die "invalid expression";
	    }
	}
    }
}

sub print_bool {
    my($bool) = @_;

    my $s = &printable_bool($bool);
    print $s,"\n";
}

sub printable_bool {
    my($bool) = @_;

    my($nodes,$root) = @$bool;
    my $val = $nodes->[$root];

    if (! ref  $val) 
    { 
	return &printable_bool([$nodes,$val]);
    }
    else
    {
	my $op = $val->[0];

	if ($op eq 'role')
	{
	    return $val->[1];
	}
	elsif ($op eq "of")
	{
	    my @expanded_args = map { &printable_bool([$nodes,$_]) } @{$val->[2]};
	    my $args = join(',',@expanded_args);
	    return "$val->[1] of {$args}";
	}
	elsif ($op eq "not")
	{
	    return "($op " .  &printable_bool([$nodes,$val->[1]]) . ")";
	}
	else
	{
	    return "(" . &printable_bool([$nodes,$val->[1]]) . " $op " . &printable_bool([$nodes,$val->[2]]) . ")";
	}
    }
}

sub compute_genome_label
{
    my($fig, $org) = @_;
    my $label;

    my $gs = $fig->genus_species($org);
    if ($fig->genome_domain($org) ne "Environmental Sample")
    {
	my $gc=$fig->number_of_contigs($org);
	$label = "$gs ($org) [$gc contigs]";
    }
    else 
    {
	$label = "$gs ($org)";
    }
    return $label;
}


MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3