#!/usr/local/bin/perl
###############################################################################
# $Id: Glyco_prediction.cgi 4670 2006-04-22 01:54:05Z dcampbel $
#
# 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.
###############################################################################
###############################################################################
# Get the script set up with everything it will need
###############################################################################
use strict;
use vars qw ($q $sbeams $sbeamsMOD $PROG_NAME
$current_contact_id $current_username $glyco_query_o);
use lib qw (../../lib/perl);
use CGI::Carp qw(fatalsToBrowser croak);
use Data::Dumper;
use SBEAMS::Connection qw($q $log);
use SBEAMS::Connection::Settings;
use SBEAMS::Connection::Tables;
use SBEAMS::Connection::DataTable;
use SBEAMS::Glycopeptide;
use SBEAMS::Glycopeptide::Settings;
use SBEAMS::Glycopeptide::Tables;
use SBEAMS::Glycopeptide::Get_glyco_seqs;
use SBEAMS::Glycopeptide::Glyco_query;
###############################################################################
# Global Variables
###############################################################################
#
$sbeams = new SBEAMS::Connection;
$sbeamsMOD = new SBEAMS::Glycopeptide;
$sbeamsMOD->setSBEAMS($sbeams);
$glyco_query_o = new SBEAMS::Glycopeptide::Glyco_query;
$glyco_query_o->setSBEAMS($sbeams);
$sbeams->setSBEAMS_SUBDIR($SBEAMS_SUBDIR);
my $base_url = "$CGI_BASE_DIR/$SBEAMS_SUBDIR/massSearch";
{ # Main
$log->debug( "Getting started? " );
# Authenticate or exit
exit unless ($current_username = $sbeams->Authenticate(
#permitted_work_groups_ref=>['Glycopeptide_user','Glycopeptide_admin', 'Glycopeptide_readonly'],
#connect_read_only=>1,
allow_anonymous_access=>1,
));
#### Read in the default input parameters
my %params;
$sbeams->parse_input_parameters( q=>$q, parameters_ref=>\%params );
$sbeams->processStandardParameters(parameters_ref=>\%params);
## get project_id to send to HTMLPrinter display
my $project_id = $sbeams->getCurrent_project_id();
my $page = $sbeams->getGifSpacer( 800 ) . " \n";
$log->debug( "Getting anywhere? " );
#### Decide what action to take based on information so far
if( $params{action} =~ /^search$/ ) {
$page .= print_form( \%params );
$page .= run_search( \%params );
} elsif( $params{action} =~ /^download$/ ) {
my $header = $sbeams->get_http_header( mode => 'tsvfull', filename => 'search_results.xls' );
print $header;
print run_search( \%params );
exit;
} else {
$page .= print_form( \%params );
}
# Display page
$sbeamsMOD->display_page_header(project_id => $project_id);
# $sbeams->printStyleSheet();
print $page;
my $url = $q->self_url();
$sbeamsMOD->display_page_footer();
} # end main
sub get_download_form {
my $params = shift;
my $url = $q->url( -full => 1 );
my $form = "
";
my $url = "Download table";
return $form . "\n" . $url;
# return "Download table";
}
sub run_search {
my $params = shift;
my $content;
for my $arg ( qw( id_list id_type ) ) {
unless ( $params->{$arg} ) {
$content .= "Missing required parameter: $arg \n";
return $content;
}
}
# for my $p ( keys( %$params ) ) { $content .= "$p => " . $params->{$p} . " \n"; }
my $id_list = $params->{id_list};
$id_list =~ s/\s+/ /gm;
my @ids = split( " ", $id_list );
my $pep_o = new SBEAMS::Glycopeptide::Get_peptide_seqs(glyco_obj => $sbeamsMOD);
my $results = $sbeamsMOD->runBulkSearch( ids => \@ids, %{$params} );
my $table = SBEAMS::Connection::DataTable->new( BORDER => 0 );
$table->addRow( ['IPI', 'SwissProt', 'Symbol', 'Sequence', 'Prophet', 'Mass', 'Tissues', 'Protein Name' ] );
$table->setRowAttr( ROWS => [1], BGCOLOR => '#C0D0C0' );
my $current;
my $prot_cnt = 1;
my $grp_row = 2;
my $bgcolor = '#FFFFFF';
for my $peptide ( @$results ) {
my ( $ipi,
$swiss_id,
$ipi_data_id,
$seq,
$symbol,
$protein_name,
$peptide_id,
$prophet_score,
$peptide_mass
) = @$peptide;
my $ipi_base = $ipi;
$current = $ipi_base unless $current;
my $tissues = $pep_o->identified_tissues( $peptide_id ) || '';
if ( $params->{action} ne 'download' ) {
$tissues = $sbeams->truncateStringWithMouseover( string => $tissues, len => 20 );
$protein_name= $sbeams->truncateStringWithMouseover( string => $protein_name, len => 40 );
$ipi = "$ipi";
}
$prophet_score = sprintf( "%0.2f", $prophet_score);
$peptide_mass = sprintf( "%0.4f", $peptide_mass);
$table->addRow( [ $ipi, $swiss_id, $symbol, $seq, $prophet_score, $peptide_mass, $tissues, $protein_name] );
# $log->debug( "ipi_base is $ipi_base, current is $current" );
if ( $ipi_base ne $current ) {
$prot_cnt++;
# $log->debug( "In da not current loop" );
my $current_row = $table->getRowNum();
# $log->info( "group row is $grp_row, current row is $current_row, bgcolor is $bgcolor" );
$table->setRowAttr( ROWS => [$grp_row..$current_row - 1], BGCOLOR => $bgcolor );
$grp_row = $current_row;
$bgcolor = ( $bgcolor eq '#E0E0E0' ) ? '#FFFFFF' : '#E0E0E0';
$current = $ipi_base;
}
my $current_row = $table->getRowNum();
$table->setRowAttr( ROWS => [$grp_row..$current_row], BGCOLOR => $bgcolor );
}
return "Query returned no results" unless @$results;
if ( $params->{action} eq 'download' ) {
$content = $table->asTSV();
$log->debug( "Content is " . length($content) . " characters long" );
} else {
my $prot = ( $prot_cnt > 1 ) ? 'proteins' : 'protein';
my $num = ( @ids > 1 ) ? 'numbers' : 'number';
my $pep = ( $table->getRowNum() > 2 ) ? 'peptides' : 'peptide';
$content .= "Search with " . scalar( @ids ) . " accession $num yeilded ";
$content .= $table->getRowNum() - 1 . " $pep from $prot_cnt $prot \n";
$content .= get_download_form($params);
$content .= $table->asHTML();
}
return $content;
}
sub print_form {
my $params = shift;
my $content = <<" END";
Search protein list
Enter a list of proteins to be searched.
END
# Table to hold form elements
my $f_table = SBEAMS::Connection::DataTable->new( BORDER => 1 );
# Hashes to hold form labels/fields
my $input_labels = get_input_labels($params);
my $input_fields = get_input_fields($params);
# Loop through and add items to the form table
for my $key ( qw( id_list id_type ) ) {
$f_table->addRow(["$input_labels->{$key}", $input_fields->{$key}]);
}
my @buttons = $sbeams->getFormButtons( types => [qw(submit reset)] );
$f_table->addRow( [join(" ", @buttons)] );
$f_table->setColAttr( COLS => [1], ROWS => [3], COLSPAN => 2 );
# $f_table->setColAttr( COLS => [1], ROWS => [1..6], ALIGN => 'RIGHT' );
$f_table->setColAttr( COLS => [1], ROWS => [3], ALIGN => 'CENTER' );
$f_table->setColAttr( COLS => [2], ROWS => [1..3], ALIGN => 'LEFT' );
$content .=<<" END";
END
return $content;
}
sub get_input_labels {
my $params = shift;
my %in = ( id_list => 'Protein List:',
id_type => 'Identifier type:',
);
return \%in;
}
sub get_input_fields {
my $params = shift;
my $id_list = $params->{id_list} || '';
$id_list = "";
my %id_chk = ( ipi => '', swp => '', sym => '' );
if ( $params->{id_type} ) {
for my $id_type ( qw( swp sym ipi ) ) {
$id_chk{$id_type} = 'checked' if $params->{id_type} eq $id_type;
}
} else {
$id_chk{ipi} = 'checked';
}
my $id_type_radio =<<" END";
IPI
Swiss Prot
Gene Symbol
END
my %in = (
id_list => $id_list,
id_type => $id_type_radio,
);
return \%in;
}