#!/usr/local/bin/perl
###############################################################################
# Program : GetAnnotations
# Author : Eric Deutsch
# $Id$
#
# Description : This CGI program that allows users to
# browse through annotated proteins very simply
#
# SBEAMS is Copyright (C) 2000-2010 Eric Deutsch
# This program is governed by the terms of the GNU General Public License (GPL)
# version 2 as published by the Free Software Foundation. It is provided
# WITHOUT ANY WARRANTY. See the full description of GPL terms in the
# LICENSE file distributed with this software.
#
###############################################################################
###############################################################################
# Set up all needed modules and objects
###############################################################################
use strict;
use Getopt::Long;
use FindBin;
use lib "$FindBin::Bin/../../lib/perl";
use vars qw ($sbeams $sbeamsMOD $q $current_contact_id $current_username
$PROG_NAME $USAGE %OPTIONS $QUIET $VERBOSE $DEBUG $DATABASE
$TABLE_NAME $PROGRAM_FILE_NAME $CATEGORY $DB_TABLE_NAME
@MENU_OPTIONS);
use SBEAMS::Connection qw($q $log);
use SBEAMS::Connection::Settings;
use SBEAMS::Connection::Tables;
use SBEAMS::ProteinStructure;
use SBEAMS::ProteinStructure::Settings;
use SBEAMS::ProteinStructure::Tables;
use SBEAMS::Microarray::Tables;
$sbeams = new SBEAMS::Connection;
$sbeamsMOD = new SBEAMS::ProteinStructure;
$sbeamsMOD->setSBEAMS($sbeams);
$sbeams->setSBEAMS_SUBDIR($SBEAMS_SUBDIR);
use CGI;
use CGI::Carp qw(fatalsToBrowser croak);
$q = new CGI;
###############################################################################
# Set program name and usage banner for command like use
###############################################################################
$PROG_NAME = $FindBin::Script;
$USAGE = <Authenticate() and exit if it fails or continue if it works.
###############################################################################
sub main {
#### Do the SBEAMS authentication and exit if a username is not returned
exit unless ($current_username = $sbeams->Authenticate(
permitted_work_groups_ref=>['ProteinStructure_user',
'ProteinStructure_admin','ProteinStructure_readonly','Admin'],
#connect_read_only=>1,
#allow_anonymous_access=>1,
));
#### Read in the default input parameters
my %parameters;
my $n_params_found = $sbeams->parse_input_parameters(
q=>$q,parameters_ref=>\%parameters);
#$sbeams->printDebuggingInfo($q);
#### Process generic "state" parameters before we start
$sbeams->processStandardParameters(parameters_ref=>\%parameters);
#### Decide what action to take based on information so far
if ($parameters{action} eq "xxxx") {
} elsif ($parameters{action} eq "download") {
}else {
$sbeamsMOD->display_page_header(
navigation_bar=>$parameters{navigation_bar});
handle_request(ref_parameters=>\%parameters);
$sbeamsMOD->display_page_footer();
}
} # end main
###############################################################################
# Handle Request
###############################################################################
sub handle_request {
my %args = @_;
#### Process the arguments list
my $ref_parameters = $args{'ref_parameters'}
|| die "ref_parameters not passed";
my %parameters = %{$ref_parameters};
#### Define some generic varibles
my ($i,$element,$key,$value,$line,$result,$sql);
#### Define some variables for a query and resultset
my %resultset = ();
my $resultset_ref = \%resultset;
my (%url_cols,%hidden_cols,%max_widths,$show_sql);
#### Read in the standard form values
my $apply_action = $parameters{'action'} || $parameters{'apply_action'};
my $TABLE_NAME = $parameters{'QUERY_NAME'};
#### Set some specific settings for this program
my $PROGRAM_FILE_NAME="GetHaloAnnotations";
my $base_url = "$CGI_BASE_DIR/$SBEAMS_SUBDIR/$PROGRAM_FILE_NAME";
#### Get the columns and input types for this table/query
my @columns = ( 'search_scope','search_key' );
my %input_types = ( 'optionlist','text' );
#### Read the input parameters for each column
my $n_params_found = $sbeams->parse_input_parameters(
q=>$q,parameters_ref=>\%parameters,
columns_ref=>\@columns,input_types_ref=>\%input_types);
#### If the apply action was to recall a previous resultset, do it
my %rs_params = $sbeams->parseResultSetParams(q=>$q);
if ($apply_action eq "VIEWRESULTSET") {
$sbeams->readResultSet(resultset_file=>$rs_params{set_name},
resultset_ref=>$resultset_ref,query_parameters_ref=>\%parameters);
$n_params_found = 99;
}
#### Set some reasonable defaults if no parameters supplied
unless ($n_params_found) {
}
#### Apply any parameter adjustment logic
#$parameters{display_options} = 'ShowSQL';
#### Display the user-interaction input form
if ($sbeams->output_mode() eq 'html') {
my @options = ( 'All','GeneSymbol','ORFName',
'FullGeneName','ECNumbers','Aliases',
'RedundantORFs' );
my %options = ( 'GeneSymbol' => 'Gene Symbol',
'ORFName' => 'ORF Name',
'FullGeneName' => 'Full Gene Name',
'ECNumbers' => 'EC Number',
'Aliases' => 'Aliases',
'RedundantORFs' => 'Redundant ORFs',
'All' => 'All Attributes',
);
#### Build the option list
my $optionlist = '';
foreach my $key ( @options ) {
my $flag = '';
$flag = 'SELECTED' if ($parameters{search_scope} eq $key);
$optionlist .= "\n";
};
unless( $parameters{table_only} ) {
print qq~
~;
## PREPEND summary links
print " $counter Results Returned ";
$table_html = qq~
-Download Tab-delimited Summary
~ . $table_html;
$table_html = qq~
-Legend and index for transcriptome structure plots
~ . $table_html;
if (@p_ids) {
my $p_list = join ",", @p_ids;
$table_html = qq~
-View all Protein Entries
~ . $table_html;
}
if (@d_ids) {
my $d_list = join ",", @d_ids;
$table_html = qq~
-View all DNA Entries
~ . $table_html;
}
if (@array_expression_genes) {
my $array_expression_ids = join "%3B", @array_expression_genes;
$table_html = qq~
-View Microarray data -Download Microarray data
~ . $table_html;
}
#### Print HTML
print $table_html;
# output a \n"
}
###############################################################################
# evalSQL: Callback for translating global table variables to names
###############################################################################
sub evalSQL {
my $sql = shift;
return eval "\"$sql\"";
} # end evalSQL
sub verify_biosequence_set_ids {
my %args = @_;
my $ids = $args{'ids'} || die "biosequence_set_ids need to be passed.";
my $sql = qq~
SELECT biosequence_set_id,project_id
FROM $TBPS_BIOSEQUENCE_SET
WHERE biosequence_set_id IN ( $ids )
AND record_status != 'D'
~;
my %project_ids = $sbeams->selectTwoColumnHash($sql);
my @accessible_project_ids = $sbeams->getAccessibleProjects();
my %accessible_project_ids;
foreach my $id ( @accessible_project_ids ) {
$accessible_project_ids{$id} = 1;
}
my @input_ids = split(',',$ids);
my @verified_ids;
foreach my $id ( @input_ids )
{
## requested bio seq set id exists:
if (exists $project_ids{$id} )
{
my $okay = 0;
if ($project_ids{$id} eq "")
{
## let it through, bioseq set has no project id
$okay = 1;
}
## if have a project_id but it isn't accessible, print message
if ( ($okay == 0) && ! defined($accessible_project_ids{$project_ids{$id}}) )
{
## id exists, but is not accessible project
$sbeams->reportException(
state => 'ERROR',
type => 'PERMISSION DENIED',
message => "Your current privilege settings do not allow you to access biosequence_set_id = $id. See project owner to gain permission.",
);
} elsif (($okay == 0) && (exists $accessible_project_ids{$project_ids{$id}}))
{
$okay = 1;
}
if ($okay == 1)
{
push(@verified_ids,$id);
}
} else
{
#### requested biosequence_set_id doesn't exist
$sbeams->reportException(
state => 'ERROR',
type => 'BAD CONSTRAINT',
message => "Non-existent biosequence_set_id = $id specified",
);
}
}
return @verified_ids;
}
################################################################################
# searchExternal: A method to search an external file for any matching info
###############################################################################
sub searchExternal {
my %args = @_;
#### Process the arguments list
my $query_parameters_ref = $args{'query_parameters_ref'};
#### Determine which external data source to search
my %abbreviations = (
'3' => 'Hm', # Hm
'2' => 'Halo', # Halobac
);
my $abbreviation = $abbreviations{$query_parameters_ref->{protein_biosequence_set_id}};
unless ($abbreviation) {
print "ERROR: Unable to find a file for this dataset \n";
return 0;
}
#### Search both the DomainHits and Biosequences files
my %biosequence_accessions;
foreach my $filetype ( qw (DomainHits Biosequences) ) {
#### Open the file
my $file = "${abbreviation}_$filetype.tsv";
my $fullfile = "/net/dblocal/www/html/sbeams/var/$SBEAMS_SUBDIR/$file";
open(INFILE,$fullfile) || die("ERROR: Unable to open $fullfile");
#### Parse header line
my $line = ;
$line =~ s/[\r\n]//g;
my @column_list = split("\t",$line);
#### Convert the array into a hash of names to column numbers
my $i = 0;
my %column_hash;
foreach my $element (@column_list) {
$column_hash{$element} = $i;
$i++;
}
my $col = $column_hash{'biosequence_name'};
unless ($col) {
print "ERROR: Could not find column 'biosequence_accession' ";
$col = 0;
}
#### Get the search_spec
my $search_spec = $query_parameters_ref->{search_key};
$search_spec =~ s/\./\\./g;
my @specs = split(/[\s+,;]/,$search_spec);
#### Search through the file looking for matches
while ($line = ) {
$line =~ s/[\r\n]//g;
my $match = 0;
foreach my $spec (@specs) {
if ($line =~ /$spec/i) {
$match = 1;
last;
}
}
#### If there was a match, save this accession
if ($match) {
my @columns = split("\t",$line);
$biosequence_accessions{$columns[$col]}++;
}
}
close(INFILE);
}
# print join(";",keys(%biosequence_accessions)),"\n";
return join(";",keys(%biosequence_accessions));
} # end searchExternal