#!/usr/local/bin/perl
###############################################################################
# $Id$
#
# SBEAMS is Copyright (C) 2000-2021 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 lib qw (../../lib/perl);
use Data::Dumper;
use File::Basename;
use File::Copy qw( move copy );
use LWP::UserAgent;
use HTTP::Request;
use JSON;
use SBEAMS::Connection qw($q $log);
use SBEAMS::Connection::Settings;
use SBEAMS::Connection::Tables;
use SBEAMS::Connection::DataTable;
use SBEAMS::Connection::TabMenu;
use SBEAMS::PeptideAtlas;
use SBEAMS::PeptideAtlas::Settings;
use SBEAMS::PeptideAtlas::Tables;
###############################################################################
# Global Variables
###############################################################################
my $sbeams = new SBEAMS::Connection;
$sbeams->setSBEAMS_SUBDIR($SBEAMS_SUBDIR);
my $atlas = new SBEAMS::PeptideAtlas;
$atlas->setSBEAMS($sbeams);
my $json = new JSON;
# Read input parameters
my $params = process_params();
my $username = $sbeams->Authenticate( allow_anonymous_access => 1) || exit;
$sbeams->setSessionAttribute( key => 'PA_resource', value => 'DIAAtlas' );
# Get a list of accessible project_ids
my @project_ids = $sbeams->getAccessibleProjects();
my $project_ids = join( ",", @project_ids ) || '0';
my $show_image = 0;
my $tabMenu;
my %basespace = ( basespace_params => '' );
{ # Main
# Authenticate or exit
## get current settings
my $project_id = $sbeams->getCurrent_project_id();
if ( $params->{mode} eq 'get_public_libsets' ) {
$params->{output_format} ||= 'html';
unless ( $params->{output_format} eq 'json' ) {
print $sbeams->get_http_header();
}
get_public_libsets();
exit;
}
$params->{action} ||= 'trigger';
if ( $params->{appsessionuri} && $params->{authorization_code} ) {
$basespace{basespace_params} = qq~
{appsessionuri}>
~;
$basespace{appsessionuri} = $params->{appsessionuri};
$basespace{authorization_code} = $params->{authorization_code};
$basespace{url_params} = ";authorization_code=$params->{authorization_code};appsessionuri=$params->{appsessionuri}";
authenticate_to_basespace();
get_appsession_properties();
# $log->info( "mode is $params->{mode}" );
$params->{mode} ||= $basespace{mode};
$params->{format} ||= 'peakview';
# $log->info( "mode is $params->{mode}" );
}
$params->{mode} ||= 'download_libs';
my $program_name = ( $params->{mode} =~ /download/ ) ? 'DIA_library_download' : 'DIA_library_subset';
my $page = $sbeams->getGifSpacer( 700 ) . " \n";
# Get the HTML to display the tabs
$tabMenu = $atlas->getTabMenu(
parameters_ref => $params,
program_name => $program_name,
);
my $css = $sbeams->printStyleSheet( module_only => 1 );
$page .=<<" END";
END
# my $load_script = "set_toggle_box( 'protein_list_table' );sortables_init()";
if ( $params->{lib_id} ) {
$params->{mode} = 'subset_libs';
}
$params->{mode} ||= 'download_libs';
# die "header coming";
$atlas->display_page_header( tracker_type => 'swath' );
if ( $params->{mode} eq 'subset_libs' ) {
my $title = qq~
| Custom Library Download
~;
# $page .= $title;
$page .= $tabMenu;
$page .= get_status_box("Generating library, may take several minutes") unless $params->{action} eq 'trigger';
print $page;
$page = get_subset_form();
} elsif ( $params->{mode} eq 'download_libs' ) {
$page .= $tabMenu;
if ( $params->{action} eq 'download' ) {
$page .= get_status_box( "" );
print $page;
upload_to_basespace();
$page = '';
}
$page .= get_library_table();
}
$page .= " ";
# Print what we already have, speed up apparent page loading time.
print $page;
$atlas->display_page_footer();
$sbeams->setSessionAttribute( key => 'PA_resource', value => '' );
} # end main
sub get_status_box {
my $msg = shift || '';
my $wait = "";
return qq~
$wait $msg
~;
}
sub get_help_links {
my %title2text = ( 'Library' => "Source library file to customize.
† Note that libraries that have already had SWATHS applied can only be filtered by protein list",
'min_num_frags' => "Minimum number of fragment ions to include an assay.
Must be less than or equal to Max num fragments ",
'max_num_frags' => "Maximum number of fragment ions to use in an assay for a specific precursor.
Must be greater than or equal to Min num fragments ",
'basename' => "Base name for exported library files (optional). Default is library name. ",
'prec_min_mz' => "Minimum precursor ion m/z value for library entries ",
'prec_max_mz' => "Maximum precursor ion m/z value for library entries ",
'frag_min_mz' => "Minimum fragment ion m/z value for library entries ",
'frag_max_mz' => "Maximum fragment ion m/z value for library entries ",
'cmod_mass' => 'Generate transision with heavy-labeled versions of selected amino acids, Use ctrl-shift for multiple options.',
'cmod_opts' => $q->escape( 'Generate heavy transitions, light and heavy (L & H), or light only (default). Relevant only if one or more heavy label options are selected.' ),
'swaths_file' => "File of SWATH bins, format is lower mz bound 'tab' upper bound mz 'newline' ",
'no_swaths_file' => "SWATH file not needed, ion library filtered at analysis time (Peakview) ",
'swath_size' => "Size of each SWATH bin, default is 25 Th ",
'swath_overlap' => "Overlap between adjacent SWATH bins, on each side - so 1 Th will result in 2 Th overlap ",
'Proteins' => "File of protein accessions, one per line, with which to filter library. One accession space per organism allowed: Human - Uniprot, Mtb - Tuberculist (Rv)",
'domain_protein_list_id' => "Set of protein accessions from one of the Human Proteome Project Biology/Disease lists Note that this will only work with Human libraries" );
my %title2link;
for my $title ( keys( %title2text ) ) {
my $text = $title2text{$title};
$title2link{$title} = qq~
~;
}
return \%title2link;
}
sub get_protein_list_selector {
my $sql =<<" END";
SELECT title, protein_list_id
FROM $TBAT_DOMAIN_PROTEIN_LIST DPL
JOIN $TB_CONTACT C ON DPL.owner_contact_id = C.contact_id
WHERE project_id IN ( $project_ids )
ORDER BY title
END
my $sth = $sbeams->get_statement_handle( $sql );
my $select;
while( my @row = $sth->fetchrow_array() ) {
my $selected = ( $row[1] == $params->{domain_protein_list_id} ) ? 'selected' : '';
my $title = $row[0];
$title =~ s/known to be associated with human/associated with/g;
my $option = "\n";
$select .= $option;
}
$select .= "\n";
my $blank = ( $select =~ /selected/ ) ? '' : '';
$select = qq~