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

View of /FigWebServices/ACH_resolution.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (download) (annotate)
Mon Jul 21 11:59:53 2008 UTC (11 years, 7 months ago) by overbeek
Branch: MAIN
CVS Tags: mgrast_dev_08112011, rast_rel_2009_05_18, mgrast_dev_08022011, rast_rel_2014_0912, myrast_rel40, mgrast_dev_05262011, rast_rel_2008_12_18, mgrast_dev_04082011, rast_rel_2010_0928, rast_2008_0924, mgrast_version_3_2, mgrast_dev_12152011, 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: +1 -0 lines
format display of comments to be surrounded by <pre>

# -*- 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;
use DBKernel;

my $fig = new FIG;

my $achDB = DBKernel->new('mysql', 'fig_anno_v5', 'ach', undef, undef, 'anno-3.nmpdr.org');

use HTML;
use strict;
use URI::Escape;

use CGI;
my $cgi = new CGI;

if (0)
{
    my $VAR1;
    eval(join("",`cat /tmp/ACH_res_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/ACH_res_parms"))
	{
	    print TMP &Dumper($cgi);
	    close(TMP);
	}
    }
    exit;
}

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

my $nmpdr_user       = $cgi->param('nmpdr_user');
my $non_nmpdr_user   = $cgi->param('non_nmpdr_user');
my $request          = $cgi->param('request');
my $curator          = $cgi->param('curator');

if ($request eq "download")
{
    &download($fig,$cgi,$html);
}
elsif (((! $nmpdr_user) && (! $non_nmpdr_user)) || ($nmpdr_user && $non_nmpdr_user))
{
    &get_user($fig,$cgi,$html);
}
elsif ($non_nmpdr_user && (! $request))
{
    &process_non_nmpdr($fig,$cgi,$html,$non_nmpdr_user);
}
elsif ($nmpdr_user && (! $request))
{
    &process_nmpdr($fig,$cgi,$html,$nmpdr_user);
}
elsif ($nmpdr_user && ($request eq "show_role_diffs"))
{
    &show_role_diffs($fig,$cgi,$html,$nmpdr_user,$curator);
}
elsif ($nmpdr_user && ($request eq "set_role_diffs"))
{
    &set_role_diffs($fig,$cgi,$html);
}
elsif ($nmpdr_user && ($request eq "show_peg_diffs"))
{
    &show_peg_diffs($fig,$cgi,$html,$nmpdr_user,$curator);
}
elsif (($nmpdr_user || $non_nmpdr_user) && ($request eq "show_comments"))
{
    &show_comments($fig,$cgi,$html);
}
elsif (($nmpdr_user || $non_nmpdr_user) && ($request eq "create_comment"))
{
    &create_comment($fig,$cgi,$html);
}
elsif (($nmpdr_user || $non_nmpdr_user) && ($request eq "set_comment"))
{
    &set_comment($fig,$cgi,$html);
}
else
{
    push(@$html,$cgi->h1("What now for $non_nmpdr_user"));
}
&HTML::show_page($cgi,$html, 1, undef, undef, undef, undef, { no_fig_search => 1 });

sub process_nmpdr {
    my($fig,$cgi,$html,$nmpdr_user) = @_;

    my $by_curator = &get_diffs_by_curator($fig);
    my $col_hdrs = ["Curator","Number of Unhandled Differences"];
    my $tab      = [];
    foreach my $curator (sort keys(%$by_curator))
    {
	my $diffs = $by_curator->{$curator};
	my $n     = @$diffs;
	push(@$tab,[$curator,"<a href=./ACH_resolution.cgi?nmpdr_user=$nmpdr_user&curator=$curator&request=show_role_diffs>$n</a>"]);
    }
    push(@$html,&HTML::make_table($col_hdrs,$tab,"Roles that Need to Be Checked"));
    push(@$html,"<hr>");
    &make_conflicts_table($fig,$cgi,$html,$nmpdr_user);
}

sub get_diffs_by_curator {
    my($fig) = @_;

    my %by_curator;
    my @diffs = &unhandled_diffs($fig);
    foreach my $diff (@diffs)
    {
	my($expertF,$ourF) = @$diff;
	my @curators = &function_to_curators($fig,$ourF);
	foreach my $curator (@curators)
	{
	    push(@{$by_curator{$curator}},[$expertF,$ourF]);
	}
    }
    return \%by_curator;
}

sub function_to_curators {
    my($fig,$func) = @_;

    my %curators;
    my @subs = grep { $fig->usable_subsystem($_) } $fig->function_to_subsystems($func);
    foreach my $sub (@subs)
    {
	my $curator = $fig->subsystem_curator($sub);
	if ($curator)
	{
	    $curators{$curator} = 1;
	}
	else
	{
	    print STDERR "$sub has no curator?\n";
	}
    }
    return sort keys(%curators);
}

sub unhandled_diffs {
    my($fig) = @_;

    my $values = $achDB->SQL("SELECT expertF,ourF FROM ACHres_diffs");

    my %diffH;
    foreach my $tuple (@$values)
    {
	$diffH{join("\t",@$tuple)} = 1;
    }

    $values = $achDB->SQL("SELECT DISTINCT expertF,ourF FROM ACHres");
    my @todo = grep { ! $diffH{join("\t",@$_)} } @$values;
}

sub get_user {
    my($fig,$cgi,$html) = @_;

    push(@$html, $cgi->start_form(-action => "ACH_resolution.cgi",
				  -method => 'post'),
	         "<br><a href=ACH_resolution.cgi?request=download>Download Synonyms</a><br><br>",
	         'NMPDR User: ',
	         $cgi->textfield(-name => "nmpdr_user", -size => 10, -value => ''),
	         $cgi->br,$cgi->br,
	         'or non_NMPDR_user ',
	         $cgi->textfield(-name => "non_nmpdr_user", -size => 10, -value => ''),
	         $cgi->br,
	         $cgi->submit( 'Declare User' ),
	         $cgi->end_form
	 );

}
    
sub show_role_diffs {
    my($fig,$cgi,$html,$nmpdr_user,$curator) = @_;

    my $by_curator = &get_diffs_by_curator($fig);
    my @to_show = @{$by_curator->{$curator}};

    my $col_hdrs = ["Different","Same","Ignore",'Expert Function','Our Function'];
    my $tab      = [];
    foreach my $pair (@to_show)
    {
	my($expertF,$ourF) = @$pair;
	my $key = &uri_escape("$expertF\t$ourF");
	my($dB,$sB,$iB) = $cgi->radio_group( -name     => "choices:$key",
					     -values   => ['d','s','i'],
					     -default  => '',
					     -override => 1);
	push(@$tab,[$dB,$sB,$iB,$expertF,$ourF]);
    }
    push(@$html,$cgi->start_form( -action => "ACH_resolution.cgi", -method => 'post'),
	        &HTML::make_table($col_hdrs,$tab,"Check These Roles"),
	        $cgi->hidden( -name => 'nmpdr_user', -value => $nmpdr_user),
	        $cgi->hidden( -name => 'request', -value => 'set_role_diffs', override => 1),
	        $cgi->br,$cgi->submit('record choices'),
	        $cgi->end_form
	 );
}

sub set_role_diffs {
    my($fig,$cgi,$html) = @_;

    my @params = grep { $_ =~ /^choices/ } $cgi->param();
    my $n = 0;
    foreach my $param (@params)
    {
	my $value = $cgi->param($param);
	my $key   = &uri_unescape($param);
	if ($key =~ /^choices:(.*)\t(.*)/)
	{
	    my($expertF,$ourF) = ($1,$2);
	    my $expertFQ = quotemeta $expertF;
	    my $ourFQ    = quotemeta $ourF;
	    if ($achDB->SQL("INSERT INTO ACHres_diffs (expertF,ourF,status) VALUES ( '$expertFQ' , '$ourFQ' , '$value' )"))
	    {
		$n++;
	    }
	}
    }
    push(@$html,$cgi->h1("Recorded $n Decisions"));
}

sub make_conflicts_table {
    my($fig,$cgi,$html,$nmpdr_user) = @_;

    my $by_curator = &get_peg_diffs_by_curator($fig);
    my $col_hdrs = ["Curator","Number of PEG Differences"];
    my $tab      = [];
    foreach my $curator (sort keys(%$by_curator))
    {
	my $diffs = $by_curator->{$curator};
	my $n     = &num_diffs($diffs);
	push(@$tab,[$curator,"<a href=./ACH_resolution.cgi?nmpdr_user=$nmpdr_user&curator=$curator&request=show_peg_diffs>$n</a>"]);
    }
    push(@$html,"<br><hr>\n");
    push(@$html,&HTML::make_table($col_hdrs,$tab,'Remaining PEG Differences'));
}

sub num_diffs {
    my($xL) = @_;

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

sub get_peg_diffs_by_curator {
    my ($fig) = @_;

    my %by_curator;
    my @diffs = &get_role_conflicts($fig);
    foreach my $diff (@diffs)
    {
	my($expert,$expertF,$ourF,$pegs) = @$diff;
	my @curators = &function_to_curators($fig,$ourF);
	foreach my $curator (@curators)
	{
	    push(@{$by_curator{$curator}},[$expert,$expertF,$ourF,$pegs]);
	}
    }
    return \%by_curator;
}

sub get_role_conflicts {
    my($fig) = @_;

    my $conflicts = $achDB->SQL("SELECT expertF,ourF FROM ACHres_diffs WHERE status = 'd'");
    my @grouped = ();
    foreach my $tuple (@$conflicts)
    {
	my($expertF,$ourF) = @$tuple;
        my $expertFQ = quotemeta $expertF;
        my $ourFQ    = quotemeta $ourF;
        my $pegs = $achDB->SQL("SELECT peg,expert,expert_id FROM ACHres WHERE (expertF = '$expertFQ') AND (ourF = '$ourFQ')");
	my @sorted = sort { ($a->[1] cmp $b->[1]) or ($a->[0] cmp $b->[0]) or ($a->[2] cmp $b->[2]) } @$pegs;
	my $x;
	while (@sorted > 0)
	{
	    my $group = [];
	    my $curr = $sorted[0]->[1];
	    while ((@sorted > 0) && ($sorted[0]->[1] eq $curr))
	    {
		my $x = shift @sorted;

		my $peg = $x->[0];
		my $func = $fig->function_of($peg);
		if ($ourF eq $func)
		{
		    my @subsys = grep { $fig->usable_subsystem($_) } $fig->peg_to_subsystems($peg);
		    if (@subsys > 0)
		    {
			push(@$group,[$peg,$x->[2]]);
		    }
		}
	    }
	    if (@$group > 0)
	    {
		push(@grouped,[$curr,$expertF,$ourF,$group]);
	    }
	}
    }
    @grouped = &filter_expert_funcs($fig,\@grouped);
    return @grouped;
}

sub filter_expert_funcs {
    my($fig,$grouped) = @_;

    my @pegs = ();
    my %pegs_to_check;
    my($tuple1,$tuple2);
    foreach $tuple1 (@$grouped)
    {
	my($expert,$expertF,$ourF,$grouped_pegs) = @$tuple1;
	foreach $tuple2 (@$grouped_pegs)
	{
	    my($peg,$expert_id) = @$tuple2;
	    $pegs_to_check{$peg} = 1;
	}
    }

    my @expert_assertions = &FIG::get_expert_assertions([keys(%pegs_to_check)]);
    my %assertions;
    foreach $tuple1 (@expert_assertions)
    {
	my($peg,$who,$expertF) = @$tuple1;
	$assertions{$peg}->{$expertF}->{$who} = 1;
    }

    my @groups1 = ();
    foreach $tuple1 (@$grouped)
    {
	my($expert,$expertF,$ourF,$grouped_pegs) = @$tuple1;
	my $grouped_pegs2 = [];
	foreach $tuple2 (@$grouped_pegs)
	{
	    my($peg,$expert_id) = @$tuple2;
	    if ($assertions{$peg}->{$expertF}->{$expert})
	    {
		push(@$grouped_pegs2,[$peg,$expert_id]);
	    }
	}
	if (@$grouped_pegs2 > 0)
	{
	    push(@groups1,[$expert,$expertF,$ourF,$grouped_pegs2]);
	}
    }
    return @groups1;
}
    

sub show_peg_diffs {
    my($fig,$cgi,$html,$nmpdr_user,$curator) = @_;

    my $by_curator = &get_peg_diffs_by_curator($fig);
    my @to_show = @{$by_curator->{$curator}};

    my $col_hdrs = ["Expert",'Expert Function','PEG','Our Function','',''];
    my $tab      = [];
    foreach my $tuple (@to_show)
    {
	my($expert,$expertF,$ourF,$group) = @$tuple;
	my $ourFQ = &uri_escape($ourF);

	foreach my $pair (@$group)
	{
	    my($peg,$expert_id) = @$pair;
	    my $comments = $achDB->SQL("SELECT comment from ACHres_comments WHERE peg = '$peg'");
	    my $get_comments = (@$comments > 0) ? "<a href=./ACH_resolution.cgi?nmpdr_user=$nmpdr_user&peg=$peg&request=show_comments>show comments</a>" : "";

	    my $create_comment = "<a href=./ACH_resolution.cgi?nmpdr_user=$nmpdr_user&peg=$peg&function=$ourFQ&request=create_comment>enter comment</a>";
	    push(@$tab,[$expert,$expertF,
			&anno_link($cgi,$peg),$ourF,
			$get_comments,$create_comment]);
	}
    }
    push(@$html,&HTML::make_table($col_hdrs,$tab,"Check These PEGs"));
}

sub create_comment {
    my($fig,$cgi,$html) = @_;

    my $peg = $cgi->param('peg');
    my $func = &uri_unescape($cgi->param('function'));
    my $protein = $cgi->param('protein');

    push(@$html,$cgi->start_form( -action => "./ACH_resolution.cgi", -method => 'post'),
	        $cgi->hidden( -name => 'nmpdr_user', -value => $nmpdr_user),
	        $cgi->hidden( -name => 'non_nmpdr_user', -value => $non_nmpdr_user),
	        $cgi->hidden( -name => 'peg', -value => $peg),
	        $cgi->hidden( -name => 'protein', -value => $protein),
	        $cgi->hidden( -name => 'function', -value => $func),
	        $cgi->hidden( -name => 'request', -value => 'set_comment', override => 1),
	        "<br><br>Enter a Comment to be associated with why $peg has the function<br><br>  <b> $func</b><br><br>\n",
	        $cgi->textarea( -name => 'comment', -rows => 20, -cols => 100, -override => 1),
	        $cgi->br,$cgi->submit('record comment'),
	        $cgi->end_form
	 );
}

sub set_comment {
    my($fig,$cgi,$html) = @_;

    my $n = 0;
    my $peg = $cgi->param('peg');
    my $function = &uri_unescape($cgi->param('function'));
    my $nmpdr_user = $cgi->param('nmpdr_user');
    my $non_nmpdr_user = $cgi->param('non_nmpdr_user');
    
    my $func = $fig->function_of($peg);
    my $comment = $cgi->param('comment');
    if ($comment && (($nmpdr_user && ($func eq $function)) || ($non_nmpdr_user && ($func ne $function))))
    {
	my $commentQ = quotemeta $comment;
	my $functionQ = quotemeta $function;
	my $user = $nmpdr_user ? $nmpdr_user : $non_nmpdr_user;
	if ($achDB->SQL("INSERT INTO ACHres_comments (peg,func,who,comment) VALUES ( '$peg','$functionQ','$user','$commentQ' )"))
	{
	    push(@$html,$cgi->h1("entered a comment for $peg"));
	}
    }
}

sub show_comments {
    my($fig,$cgi,$html) = @_;

    my $peg = $cgi->param('peg');
    my $protein = $cgi->param('protein');
    my $col_hdrs = ['Function','Who','Comment'];
    my $tab = [];
    my $tuples = $achDB->SQL("SELECT func,who,comment FROM ACHres_comments WHERE peg = '$peg'");
    foreach my $tuple (@$tuples)
    {
	$tuple->[2] = "<pre>\n" . $tuple->[2] . "</pre>";
	push(@$tab,$tuple);
    }
    my $title = $cgi->param('nmpdr_user') ? "Comments for $peg" : "Comments for $peg/$protein";
    push(@$html,&HTML::make_table($col_hdrs,$tab,$title));
}

sub anno_link {
    my($cgi,$peg) = @_;

    my $user = $cgi->param('nmpdr_user');
    return "<a href=\"http://anno-3.nmpdr.org/anno/FIG/protein.cgi?user=$user&prot=$peg\">$peg</a>";
}

sub process_non_nmpdr {
    my($fig,$cgi,$html,$non_nmpdr_user) = @_;

    my @diffs = &get_peg_diffs_by_expert($fig,$cgi,$html,$non_nmpdr_user);

    my $col_hdrs = ['Expert Function','Protein','SEED ID','Our Function','',''];
    my $tab      = [];
    foreach my $tuple (@diffs)
    {
	my($expert,$expertF,$ourF,$group) = @$tuple;
	my $expertFQ = &uri_escape($expertF);

	foreach my $pair (@$group)
	{
	    my($peg,$expert_id) = @$pair;
	    my $expert_idL = $expert_id;
	    $expert_idL =~ s/;/<br>/g;
	    my $pegURL = &HTML::alias_url($peg,"SEED");
	    my $pegL = "<a href=$pegURL>$peg</a>";
	    my $expert_idL = &HTML::set_prot_links($cgi,$expert_idL);
	    my $comments = $achDB->SQL("SELECT comment from ACHres_comments WHERE peg = '$peg'");
	    my $get_comments = (@$comments > 0) ? "<a href=./ACH_resolution.cgi?non_nmpdr_user=$non_nmpdr_user&peg=$peg&protein=$expert_id&request=show_comments>show comments</a>" : "";

	    my $create_comment = "<a href=./ACH_resolution.cgi?non_nmpdr_user=$non_nmpdr_user&peg=$peg&function=$expertFQ&protein=$expert_id&request=create_comment>enter comment</a>";
	    push(@$tab,[$expertF,
			$expert_idL,$pegL,$ourF,
			$get_comments,$create_comment]);
	}
    }
    push(@$html,&HTML::make_table($col_hdrs,$tab,"$non_nmpdr_user, please check These proteins"));
}

sub get_peg_diffs_by_expert {
    my ($fig,$cgi,$html,$non_nmpdr_user) = @_;

    my @diffs = &get_role_conflicts($fig);
    return grep { $_->[0] eq $non_nmpdr_user } @diffs;
}

sub download {
    my($fig,$cgi,$html) = @_;

    my $syn = $achDB->SQL("SELECT expertF,ourF FROM ACHres_diffs WHERE status = 's'");
    my $col_hdrs = ['Expert Function','Our Function'];
    my $tab      = [];

    foreach my $tuple (@$syn)
    {
	push(@$tab,$tuple);
    }
    push(@$html,&HTML::make_table($col_hdrs,$tab,"Synonyms"));
    push(@$html,"<hr>\n");
    push(@$html,"<br><br><pre>\n");
    foreach my $tuple (@$syn)
    {
	push(@$html,join("\t",@$tuple),"\n");
    }
    push(@$html,"</pre>\n");
}



MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3