#!/usr/local/bin/perl ############################################################################### # Program : Search # $Id$ # # Description : Searches the search_key table for matching elements # # SBEAMS is Copyright (C) 2000-2005 Institute for Systems Biology # 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::Connection::TabMenu; use SBEAMS::PeptideAtlas; use SBEAMS::PeptideAtlas::Settings; use SBEAMS::PeptideAtlas::Tables; $sbeams = new SBEAMS::Connection; $sbeamsMOD = new SBEAMS::PeptideAtlas; $sbeamsMOD->setSBEAMS($sbeams); $sbeams->setSBEAMS_SUBDIR($SBEAMS_SUBDIR); ############################################################################### # 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=>['PeptideAtlas_user','PeptideAtlas_admin', 'PeptideAtlas_readonly'], #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 "???") { # Some action } else { 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 $CATEGORY="Search PeptideAtlas"; my $PROGRAM_FILE_NAME = $PROG_NAME; my $base_url = "$CGI_BASE_DIR/$SBEAMS_SUBDIR/$PROGRAM_FILE_NAME"; my $help_url = "$CGI_BASE_DIR/help_popup.cgi"; #### If the apply action was to recall a previous resultset, do it my %rs_params = $sbeams->parseResultSetParams('q' => $q); my $n_params_found = 0; if ($apply_action eq "VIEWRESULTSET") { $sbeams->readResultSet( resultset_file=>$rs_params{set_name}, resultset_ref=>$resultset_ref, query_parameters_ref=>\%parameters, resultset_params_ref=>\%rs_params, ); $n_params_found = 99; } #### Get the passed parameters my $search_key = $parameters{"search_key"}; my $organism_name = $parameters{"organism_name"}; my $build_type_name = $parameters{"build_type_name"}; #### If a new search_key was supplied, store it if ($search_key) { $sbeams->setSessionAttribute( key => 'PeptideAtlas_search_key', value => $search_key, ); #### Else see if we had one stored } else { $search_key = $sbeams->getSessionAttribute( key => 'PeptideAtlas_search_key', ); } #### Build the list of build type names my $build_type_selection = getBuildTypeSelection( organism_name => $organism_name, build_type_name => $build_type_name, ); #### This is needed for displaying the page header my $project_id = $sbeamsMOD->getProjectID( atlas_build_id => $parameters{atlas_build_id} ); #### Show current user context information #print "
\n" if ($sbeams->output_mode() eq 'html'); #$sbeams->printUserContext(); #### Get the HTML to display the tabs my $tabMenu = $sbeamsMOD->getTabMenu( parameters_ref => \%parameters, program_name => $PROG_NAME, ); #### If the output_mode is HTML, then display the form my $buffer = ''; if ($sbeams->output_mode() eq 'html') { $buffer .= $q->start_form(-method=>"POST", -action=>"$base_url", ); $buffer .= "
".$tabMenu->asHTML(); $buffer .= "

"; $buffer .= qq~


~; #$buffer .= qq~~; $buffer .= $q->textfield(-name=>"search_key", -default=>$search_key, -size=>60, -maxlength=>60, ); $buffer .= "   "; $buffer .= $q->submit(-name => "action", -value => 'QUERY', -label => 'GO'); $buffer .= qq~
(e.g. ENSP00000222219, IPI00010348, NP_001366, Hs.167531, %BAT%, DNASE2,
\%helicase\%, Q9HCC0, MCCC2_HUMAN, PAp00000097, AAVEEGIVLGGGCALLR )
Build type:

~; $buffer .= $q->endform; } ######################################################################### #### Process all the constraints #### If search_key was not selected, stop here unless ($parameters{search_key}) { if ($sbeams->output_mode() eq 'html') { $sbeamsMOD->display_page_header(project_id => $project_id); print $buffer; } else { $sbeams->reportException( state => 'ERROR', type => 'INSUFFICIENT CONSTRAINTS', message => 'You must provide an search_key', ); } return; } #### Build SEARCH_KEY constraint my $search_key_clause = $sbeams->parseConstraint2SQL( constraint_column=>"search_key_name", constraint_type=>"plain_text", constraint_name=>"Search Key", constraint_value=>$parameters{search_key} ); return if ($search_key_clause eq '-1'); #### Define the desired columns in the query #### [friendly name used in url_cols,SQL,displayed column title] my @column_array = ( ["search_key_name","SK.search_key_name","Search Key"], ["search_key_type","SK.search_key_type","search_key_type"], ["build_type","(CASE WHEN DAB.organism_specialized_build IS NULL THEN O.organism_name ELSE DAB.organism_specialized_build END)","Build Type"], ["resource_name","SK.resource_name","Identifier"], ["resource_type","SK.resource_type","resource_type"], ["resource_n_matches","SK.resource_n_matches","N Peptides"], ["resource_url","SK.resource_url","resource_url"], ["organism_id","O.organism_id","organism_id"], ); #### Build the columns part of the SQL statement my %colnameidx = (); my @column_titles = (); my $columns_clause = $sbeams->build_SQL_columns_list( column_array_ref=>\@column_array, colnameidx_ref=>\%colnameidx, column_titles_ref=>\@column_titles ); #### Define the SQL statement to find the biosequence $sql = qq~ SELECT $columns_clause FROM $TBAT_SEARCH_KEY SK JOIN $TB_ORGANISM O ON (O.organism_id = SK.organism_id) JOIN $TBAT_DEFAULT_ATLAS_BUILD DAB ON ( SK.atlas_build_id = DAB.atlas_build_id AND DAB.organism_id IS NOT NULL ) WHERE 1 = 1 $search_key_clause AND SK.atlas_build_id IN ( $build_type_selection->{atlas_build_ids} ) ORDER BY search_key_name ~; #### Define the hypertext links for columns that need them %url_cols = ( 'Identifier' => "$CGI_BASE_DIR/PeptideAtlas/\%$colnameidx{resource_url}V", 'Search Key_OPTIONS' => {allow_wrap=>1}, ); #### Define the hidden columns %hidden_cols = ( 'search_key_type' => 1, 'resource_type' => 1, 'resource_url' => 1, 'organism_id' => 1, ); ######################################################################### #### If QUERY or VIEWRESULTSET was selected, display the data if ($apply_action =~ /(QUERY|GO|VIEWRESULTSET)/) { #### Show the SQL that will be or was executed $sbeams->display_sql(sql=>$sql) if ($show_sql); #### If the action contained QUERY, then fetch the results from #### the database if ($apply_action =~ /(QUERY|GO)/i) { #### Fetch the results from the database server $sbeams->fetchResultSet( sql_query=>$sql, resultset_ref=>$resultset_ref, ); #### Store the resultset and parameters to disk resultset cache $rs_params{set_name} = "SETME"; $sbeams->writeResultSet( resultset_file_ref=>\$rs_params{set_name}, resultset_ref=>$resultset_ref, query_parameters_ref=>\%parameters, resultset_params_ref=>\%rs_params, query_name=>"$SBEAMS_SUBDIR/$PROGRAM_FILE_NAME", ); } #### Determine how many results came back my $n_results = scalar(@{$resultset_ref->{data_ref}}); #### If exactly one result came back, then redirect to the one hit if ($n_results == 1) { print "Status: 302 Resource Temporarily Moved\n"; print "Location: $CGI_BASE_DIR/PeptideAtlas/". $resultset_ref->{data_ref}->[0]->[6]."\n\n"; exit; } #### Otherwise, display the page header to show the match results $sbeamsMOD->display_page_header(project_id => $project_id); if ($sbeams->output_mode() eq 'html') { print $buffer; print "

"; } my $cleaned_search_key = $search_key; $cleaned_search_key =~ s/^%//; $cleaned_search_key =~ s/%$//; #### If 0 results came back, then inform the user if ($n_results == 0) { print "There were no matches in the index to match your search key '$search_key'
"; if ($search_key !~ /%/) { print qq~

You can broaden the search by placing wildcards around your search key like this: $cleaned_search_key%, %$cleaned_search_key%
~; } return(0); } print qq~
Your search returned $n_results matches. Please choose one from below. Or you can broaden the search by placing wildcards around your search key like this: $cleaned_search_key%, %$cleaned_search_key%

~; #### Display the resultset $sbeams->displayResultSet( resultset_ref=>$resultset_ref, query_parameters_ref=>\%parameters, rs_params_ref=>\%rs_params, url_cols_ref=>\%url_cols, hidden_cols_ref=>\%hidden_cols, max_widths=>\%max_widths, column_titles_ref=>\@column_titles, base_url=>$base_url, ); #### Display the resultset controls $sbeams->displayResultSetControls( resultset_ref=>$resultset_ref, query_parameters_ref=>\%parameters, rs_params_ref=>\%rs_params, base_url=>$base_url, ); #### If QUERY was not selected, then tell the user to enter some parameters } else { if ($sbeams->invocation_mode() eq 'http') { $sbeamsMOD->display_page_header(project_id => $project_id); print $buffer; print "Click [GO] to execute the search
\n"; } else { print "Set action=GO to execute the search\n"; } } } # end handle_request ############################################################################### # getBuildTypeSelection ############################################################################### sub getBuildTypeSelection { my $SUB_NAME = 'getBuildTypeSelection'; my %args = @_; #### Decode the argument list my $organism_name = $args{'organism_name'}; my $build_type_name = $args{'build_type_name'}; #### If no organism was supplied, see if it was cached for this session unless ($organism_name) { $organism_name = $sbeams->getSessionAttribute( key => 'PeptideAtlas_organism_name', ); } #### If no build_type_name was supplied, see if it's cached for this session unless ($build_type_name) { $build_type_name = $sbeams->getSessionAttribute( key => 'PeptideAtlas_build_type_name', ); } #### Or default to Any unless ($build_type_name) { $build_type_name = $organism_name || 'Any'; } #### Get a list of accessible project_ids my @accessible_project_ids = $sbeams->getAccessibleProjects(); my $accessible_project_ids = join( ",", @accessible_project_ids ) || '0'; #### Get a hash of available organisms via atlas builds my $sql = qq~ SELECT DISTINCT DAB.atlas_build_id,O.organism_name,DAB.organism_specialized_build FROM $TBAT_ATLAS_BUILD AB JOIN $TBAT_DEFAULT_ATLAS_BUILD DAB ON (AB.atlas_build_id = DAB.atlas_build_id) JOIN $TBAT_BIOSEQUENCE_SET BS ON (BS.biosequence_set_id = AB.biosequence_set_id) JOIN $TB_ORGANISM O ON (O.organism_id = BS.organism_id) WHERE AB.project_id IN ( $accessible_project_ids ) AND AB.record_status!='D' AND DAB.record_status!='D' ORDER BY organism_name,DAB.organism_specialized_build ~; #print "Content-type: text/html\n\n
$sql
\n"; my @build_types = $sbeams->selectSeveralColumns($sql); my @options = ('Any'); my %options = ('Any'=>'Any'); my %atlas_build_ids; #### Build the list of available organisms foreach my $build_type ( @build_types ) { my $build_type_name = $build_type->[2] || $build_type->[1]; push(@options,$build_type_name); $options{$build_type_name} = $build_type_name; $atlas_build_ids{$build_type_name} = $build_type->[0]; } #### Build the option list HTML my $optionlist = ''; my $matched_parameter = 0; foreach my $key ( @options ) { my $flag = ''; if ($key eq $build_type_name) { $flag = 'SELECTED'; $matched_parameter++; } $optionlist .= "\n"; }; #### Also make sure the user selected a valid option unless ($matched_parameter) { $build_type_name = 'Any'; } #### Get the atlas_build_ids my $atlas_build_ids = $atlas_build_ids{$build_type_name}; if ($build_type_name eq 'Any') { $atlas_build_ids = join(",",values(%atlas_build_ids)); } #### Build a data structure to return my %tmp = ( HTML_optionlist => $optionlist, build_type_name => $build_type_name, atlas_build_ids => $atlas_build_ids, ); #### Store the selected organism_name in the session cache if ($build_type_name) { $sbeams->setSessionAttribute( key => 'PeptideAtlas_build_type_name', value => $build_type_name, ); } return \%tmp; } # end getBuildTypeSelection