[Bio] / Sprout / DrugTargets.pm Repository:
ViewVC logotype

View of /Sprout/DrugTargets.pm

Parent Directory Parent Directory | Revision Log Revision Log

Revision 1.3 - (download) (as text) (annotate)
Wed Sep 21 21:05:15 2011 UTC (8 years, 6 months ago) by parrello
Branch: MAIN
Changes since 1.2: +0 -0 lines
Changes to support complexes.

#!/usr/bin/perl -w

# 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.
package DrugTargets;

    use strict;
    use Tracer;
    use CGI qw(-nosticky);
    use SHDrugSearch;
    use FIGRules;
    use TWiki::Func;
    use HTML::Template;
    use SFXlate;

my @styles = ("even", "odd");
my $odd;
my $lkpdb ='http://www.rcsb.org/pdb/explore.do?structureId=';

sub main {
    my ($cgi, $session, $varHash) = @_;
    $odd = 1;
    my $fig = SFXlate->new();
    #get the query paramaters
    my $target = $cgi->param("target");
    my %pegHash;
    my $org = $cgi->param("org");
    # Set the descriptive variables. These change key words in the introductory
    # text and page heading.
    $varHash->{target} = $target;
    $varHash->{org} = $org;
    # Compute the peg hash if we're organism-only.
    if ($org && ! $target) {
        Trace("Computing peg hash.") if T(3);
        %pegHash = get_attribute_hash($org);
    #The script operates
    &add_table_top($fig, $cgi, $varHash, $org, $target);
    if ($target){
        &get_table($fig, $cgi, $varHash, "$target.txt", \%pegHash);
        &get_table($fig, $cgi, $varHash, $org, \%pegHash);
    $varHash->{result} .= '</table>';
    print $cgi->header();
    my $page = MyPage($varHash);
    print $page;

=head3 MyPage

    my $html = MyPage($varHash);

Output a drug target page. The incoming hash contains parameters that are plugged
into a template.

=over 4

=item varHash

Hash containing the variable data to be placed on the page.

=item RETURN

Returns the page HTML to display.


The variables in the hash should be as follows.

=over 4

=item org

Organism name.

=item results

Table displaying the result data.

=item target

Type of target desired.

=item uc_first_target

Target type as a WikiWord.



sub MyPage {
    # Get the parameters.
    my ($varHash) = @_;
    # Get the text template. This is an HTML template, not a TWiki template.
    my $htmlTemplate = TWiki::Func::readAttachment('Main', 'CandidateTargetsPipeline', 'DrugTargets.tmpl');
    my $templateObject = HTML::Template->new(scalarref => \$htmlTemplate,
                                             die_on_bad_params => 0);
    # Next, we pass in the variable values.
    for my $varKey (keys %{$varHash}) {
        # Get the variable value.
        my $varValue = $varHash->{$varKey};
        # Check for an undefined value.
        if (! defined($varValue)) {
            # Treat it as a null string.
            $templateObject->param($varKey => "");
        } else {
            # Check for an array of scalars. We convert this into a string
            # for compatibility with earlier stuff. An array of hashes is
            # okay, because it's used for loops.
            if (ref $varValue eq 'ARRAY') {
                if (scalar @{$varValue} > 0 && ! ref $varValue->[0]) {
                    $varValue = join("\n", @{$varValue});
            # Record the parameter.
            Trace("Variable $varKey has value \"$varValue\".") if T(4);
            $templateObject->param($varKey => $varValue);
    # Finally, we produce the text.
    my $text = $templateObject->output();
    # Get the view template. This IS a TWiki template.
    my $template = TWiki::Func::loadTemplate('view');
    # Set the meta-variable values.
    $template =~ s/%TEXT%/$text/g;
    $template =~ s/%REVTITLE%//g;
    # Expand it.
    my $raw = TWiki::Func::expandCommonVariables($template, 'CandidateTargetsReport', 'Main');
    # Render it into HTML.
    my $retVal = TWiki::Func::renderText($raw, 'Main');
    # Clean the nops.
    $retVal =~ s/<nop>//g;
    # Return the result.
    return $retVal;

# This function adds the table header based on if we are looking at a specific target category
sub add_table_top{
    my ($fig, $cgi, $varHash, $org, $target) = @_;
    &add_words($fig, $cgi, $varHash, $org);
        $varHash->{result} .= ' <table width="90%" class="targets"> 
           <tr class=odd><th>Organism</th><th>Functional Role</th><th>Candidacy<th>PDB</th><th>Best Hit to Human</th></tr>';
    }elsif ($org) {
        $varHash->{result} .= '<table width="90%" class="targets">
         <tr class=odd><th>Organism</th><th>Functional Role</th><th>Category</th><th>PDB</th><th>Best Hit to Human</th></tr>'; 

# This funtion sets up the variables used by the template to display organism-related text.
# and how to navigate it
sub add_words{
    my ($fig, $cgi, $varHash, $org) = @_;
    $varHash->{orgName} = "";
        $varHash->{orgName} = $fig->genus_species($org);

# This Sub funtions to open up the table and keep track of the index of what row we are on
sub get_table{
    my($fig, $cgi, $varHash, $file, $pegHash)= @_;
    Open(\*TABLE, "<$FIG_Config::drug_directory/$file");
    Trace("Processing table $file.") if T(3);
    my $count = 0;
    while (! eof TABLE){
        my @entry = Tracer::GetLine(\*TABLE);
        &get_best_pdb($fig, $pegHash, $cgi, $varHash, @entry);
    Trace("$count entries found in $file.") if T(3);

# This Sub funtions to print the Table Row in the correct format. 
sub print_column{
    my($fig, $pegHash, $cgi, $varHash, $pdb, $color, $score,$peg, $func, $lit, $cat) = @_ ;
    my $genome = $peg;
    $genome =~ s/fig\|//;
    $genome =~ s/\.peg\.\d+//;
    my $which  = $fig->genus_species($genome);
    $pdb =~ s/pdb//;
    $pdb =~ s/\.ent//;
    $cat = changecat($cat);
    my $selfURL = $cgi->url(-relative => 1);
    my $org = $cgi->param('org') || "";
    my $target = $cgi->param('target') || "";
    Trace("Printing $color for $pdb of peg $peg. Org,target = \"$org,%target\".") if T(4);
    # We'll put the category/attribute column value in here if this row should be
    # displayed.
    my $caColumn;
    if ($org && $target){
        if ($peg =~ m/$org/){
            $caColumn = "<td><a href=\"$lit\">$cat</a></td>\n";
        $caColumn = "<td><a href=\"$lit\">$cat</a></td>\n";
        Trace("Peg hash is: $pegHash->{$peg}.") if T(4);
        if ($pegHash->{$peg}) {
            $caColumn = $pegHash->{$peg};
        } else {
            $caColumn = "<td></td>\n"; # <a href=\"../content/contactus.php\">Suggest</a></td>\n";
    if (defined $caColumn) {
        $odd = 1 - $odd;
        $varHash->{result} .= "<tr class=\"$styles[$odd]\"><td><em>$which</em><br>$peg</td>\n";
        $varHash->{result} .= "<td>$func (%SV{\"NMPDR\" id=\"$peg\"})</td>\n";
        $varHash->{result} .= $caColumn;
        $varHash->{result} .= "<td color=\"$color\"><a href=\"$lkpdb$pdb\">$pdb</a></td>\n";
        # Find the best hit to a human.
        my @bbhList = FIGRules::BatchBBHs($peg, 1e-15, '9606.3');
        if (@bbhList) {
            # Here we found one, so get the best one in the list.
            my @sorted = sort { $a->[2] <=> $b->[2] } @bbhList;
            my $bestPeg = $sorted[0]->[1];
            $varHash->{result} .= "<td>%SV{\"$sorted[0]->[2]\" id=\"$bestPeg\"}%</td>\n";
        } else {
            $varHash->{result} .= "<td>&nbsp;</td>\n";

# This sub changes the Candidacy code to something meaningful.
sub changecat{
    my($dog) = @_;
    Trace("Selecting category for $dog.") if T(4);
    my $cat = SHDrugSearch::GetCategory($dog);
    return $cat;

# This sub determines the attributes of each peg related to the given genome.
sub get_attribute_hash {
    my ($genome) = @_;
    my %files = (drug => "drug.txt", toxin => "toxin.txt", vaccine => "vaccine.txt");
    my %retVal;
    my $selfURL = "$FIG_Config::cgi_url/wiki/rest.cgi/NmpdrPlugin/drugs";
    for my $key (keys %files) {
        Open(\*INFILE, "$FIG_Config::drug_directory/$files{$key}");
        while (! eof(INFILE)){
            my @entry = Tracer::GetLine(\*INFILE);
            if ($entry[5] =~ /$genome/){
                Trace("$entry[5] added to hash.") if T(3);
                my $title = ucfirst $key;
                $retVal{$entry[5]} = "<td><a href=$selfURL?org=$genome&target=$key>$title</a></td>\n";
    return %retVal;

#-- we wish to desplay only the best pdbs and those that are relavent
sub get_best_pdb{
    my ($fig, $pegHash, $cgi, $varHash, @entry)= @_;
    Trace("Processing best PDB for $entry[5].") if T(4);
    my @pdbresults =("1ah7","1dhn","1tfu"); # we define what pdb actually have been docked well.
    foreach (@pdbresults){ #look through the results we have
        if ($entry[11] =~ m/$_/gi){ #ifwe have results for the free
            &print_column($fig, $pegHash, $cgi, $varHash, $entry[11],'red', $entry[13],$entry[5], $entry[9], $entry[3], $entry[1]); #we want to display this
        }elsif($entry[14] =~ m/$_/gi){ #if we have resutls for the bound
            &print_column($fig, $pegHash, $cgi, $varHash,$entry[14], 'red', $entry[16],$entry[5], $entry[9], $entry[3], $entry[1]); #we wnat to display this
    if ($entry[13] eq "0"){ #if the free pdb is perfect
        &print_column($fig, $pegHash, $cgi, $varHash, $entry[11], '', $entry[13],$entry[5], $entry[9], $entry[3], $entry[1]); #we want to display this
    }elsif($entry[16] eq '0'){ #if the bound  pdb is perfect
        &print_column($fig, $pegHash, $cgi, $varHash, $entry[14], '', $entry[16],$entry[5], $entry[9], $entry[3], $entry[1]); #we want to display this
    }elsif($entry[13] =~ /\d\.\d*E\-(\d+)/ ){ #if the eval is like 1.4E-45
        if (15 < $1) { #and the exponant is less than 1.0E-15 
            if($entry[16] =~ /\d\.\d*E\-(\d+)/ ){ #but we should check if the bound is any better
                if (15 < $1){ #and the xponant is less than 1.0-15 we need to do the comparison
                    my @free = split(/E\-/,$entry[13]);
                    my @bound = split(/E\-/,$entry[16]);
                    my $efree = $free[0] * 13 ** -$free[1];
                    my $ebound = $bound[0] * 13 ** -$bound[1];
                    if ($efree <= $ebound){ #if the free is less than the boutd
                        &print_column($fig, $pegHash, $cgi, $varHash, $entry[11], 'black', $entry[13],$entry[5], $entry[9], $entry[3], $entry[1]); #we want to display this
                        &print_column($fig, $pegHash, $cgi, $varHash, $entry[14], 'green', $entry[16],$entry[5], $entry[9], $entry[3], $entry[1]); #we want to display this
                }else{ #the bound exponent wasn't less than 15
                    &print_column($fig, $pegHash, $cgi, $varHash, $entry[11], 'blue', $entry[13],$entry[5], $entry[9], $entry[3], $entry[1]); #we want to display this
            }else{ #the bound pdb didn't even have an exponant
                &print_column($fig, $pegHash, $cgi, $varHash, $entry[11], 'yello', $entry[13],$entry[5], $entry[9], $entry[3], $entry[1]); #we want to display this
                return; #this takes care the if free is good and bound is bad
            }#a free pdb is no good becaus eval > 1e-15
        }#free pdb was no good because eval > 1e-15
    }elsif($entry[16] =~ /\d\.\d*E\-(\d+)/ ){ #we are now checking if the bound pdb is good
        if (15 < $1) {
            &print_column($fig, $pegHash, $cgi, $varHash, $entry[14], 'black', $entry[16],$entry[5], $entry[9], $entry[3], $entry[1]);
    } else {
        Trace("Peg $entry[5] not printed.") if T(4);

# -error interupt--these subs will probably go when bruce sees them
sub err {
    my ($msg) = @_; 
    $msg = "Error!" unless $msg;
    print (&colored($msg, "CC0033"));
    die "$msg\n";

sub colored {
    my ($text, $color) = @_;
    return "<font color=$color>$text</font>";


MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3