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

View of /FigWebServices/check_variants.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (download) (annotate)
Thu Mar 20 12:32:41 2008 UTC (11 years, 8 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.10: +6 -6 lines
allow different format for roles, rules and definitions in check_variants.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/check_vc_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/check_vc_parms"))
	{
	    print TMP &Dumper($cgi);
	    close(TMP);
	}
    }
    exit;
}

my $html = [];
unshift @$html, "<TITLE>Check Variants</TITLE>\n";

my $subsys       = $cgi->param('subsystem');
my $roles        = $cgi->param('roles');
my $definitions  = $cgi->param('definitions');
my $rules        = $cgi->param('rules');

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

if ((! $subsys) || (@rolesI < 1) || (@rulesI < 1))
{
    my @sub = $fig->all_subsystems;
    push(@$html,$cgi->start_form(-action => "check_variants.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->submit( 'Compute Predicted Variant Codes' ),
	        $cgi->end_form
	 );
}
else
{
    my $col_headers_undef = ["Predicted Variant","Genome","Genus/Species"];
    my $tab_undef = [];
    my $col_headers_mismatch = ["Actual Variant","Predicted Variant","Genome","Genus/Species"];
    my $tab_mismatch = [];

    my $subO = new Subsystem($subsys,$fig);
    my %genomesS = map { $_ => 1 } $subO->get_genomes;

    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;
		push(@$html,$cgi->h2("successfully parsed $n rules"));
		my $role_to_pegs = {};
		foreach my $role (@roles)
		{
		    $role_to_pegs->{$role} = [ 
					       sort { &FIG::by_fig_id($a,$b) } 
					       $fig->prots_for_role($role)
					     ];
		}

		my $operational = 0;
		foreach my $genome (map { $_->[0] } 
				    sort { $a->[1] cmp $b->[1] } 
				    map { [$_,$fig->genus_species($_)] } 
				    $fig->genomes('complete'))
		{
		    my $vcT = &find_vc($encoding,$role_to_pegs,$abbrev_to_loc,\@rules,$fig,$genome);
		    if ($vcT > 0) { $operational++ }
		    
		    if (! $genomesS{$genome})
		    {
			push(@$tab_undef,[$vcT,$genome,$fig->genus_species($genome)]);
		    }
		    else
		    {
			my $vcS = $subO->get_variant_code_for_genome($genome);
			if ($vcT ne $vcS)
			{
			    push(@$tab_mismatch,[$vcS,$vcT,$genome,$fig->genus_species($genome)]);
			}
		    }
		}
		push(@$html,$cgi->h2("Got $operational operational variants"));

		if (@$tab_undef > 0)
		{
		    push(@$html,&HTML::make_table($col_headers_undef,$tab_undef,"Genomes to Be Added To Subsystem"),$cgi->br,$cgi->br);
		}
		
		if (@$tab_mismatch > 0)
		{
		    push(@$html,&HTML::make_table($col_headers_mismatch,$tab_mismatch,"Genomes With Mismatching Variant Codes"));
		}
	    }
	}
    }
}
&HTML::show_page($cgi,$html);


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

    my $vcT = undef;
    my $rule;

    my $role;
    my $relevant_genes = {};
    foreach $role (sort keys(%$role_to_pegs))
    {
	$relevant_genes->{$role} = &gather_genes($fig,$genome,$role,$role_to_pegs);
    }

    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;
    return $vcT;
}

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

    my @roles = ();
    foreach $_ (@$rolesI)
    {
	if ($_ =~ /^(\S+)\s+(means )?(\S.*\S)/)
	{
	    my($abbrev,$role) = ($1,$3);
	    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+(means )?(\S.*\S)/)
	{
	    my($abbrev,$bool) = ($1,$3);
	    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+(means )?(\S.*\S)\s*$/) &&
	    (($variant_code,$boolexp) = ($1,$3)) &&
	    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,$role,$role_to_pegs) = @_;

    return [sort { &FIG::by_fig_id($a,$b) }
	    grep { &FIG::genome_of($_) eq $genome }
	    @{$role_to_pegs->{$role}} ];
}

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]]) . ")";
	}
    }
}


MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3