#!/usr/local/bin/perl -w
###############################################################################
# Program : GetPeptideLocations
#
# Description : Get locations for user-defined set of peptides
###############################################################################
$|++;
## Setup objects and globals
use strict;
use Getopt::Long;
use FindBin;
use lib "$FindBin::Bin/../../lib/perl";
use vars qw ( $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;
use SBEAMS::PeptideAtlas::BestPeptideSelector;
# Set up Atlas objects
my $sbeams = new SBEAMS::Connection;
my $atlas = new SBEAMS::PeptideAtlas;
$atlas->setSBEAMS($sbeams);
$sbeams->setSBEAMS_SUBDIR( 'PeptideAtlas' );
my $best_peptide = new SBEAMS::PeptideAtlas::BestPeptideSelector;
$best_peptide->setAtlas( $atlas );
$best_peptide->setSBEAMS( $sbeams );
my $pabst_build_id;
my $is_html = 0;
main();
exit(0);
# Main Program
sub main
{
# Authenticate and exit if a username is not returned
my $current_username = $sbeams->Authenticate( allow_anonymous_access => 0 );
exit unless $current_username;
$is_html = ( $sbeams->output_mode() eq 'html' ) ? 1 : 0;
#### Read in the default input parameters
my %parameters;
$parameters{uploaded_file_not_saved} = 1;
my $n_params_found = $sbeams->parse_input_parameters(
q=>$q,parameters_ref=>\%parameters);
# Process generic "state" parameters before we start
$sbeams->processStandardParameters(parameters_ref=>\%parameters);
# This will look for mod-specific params and do the right thing
$atlas->processModuleParameters(parameters_ref=>\%parameters);
# Decide what action to take based on information so far
print $sbeams->get_http_header( mode => 'html' );
print_style();
# $sbeams->printCGIParams($q);
if ( $parameters{peptides} || $parameters{plates} ) {
print_locations( \%parameters );
}
print_form( %parameters );
} # end main
sub print_form {
my %args = @_;
my $fuzz_checked = ( $args{fuzzy} ) ? 'checked' : '';
my $tmp_checked = ( $args{tmp_plate} ) ? 'checked' : '';
my %type_checked = ( m => 'checked', w => '', b => '' );
if ( $args{type} eq 'Both' ) {
$type_checked{m} = '';
$type_checked{b} = 'checked';
} elsif ( $args{type} eq 'Working' ) {
$type_checked{m} = '';
$type_checked{w} = 'checked';
}
my $order_select = get_order_select();
print qq~
~;
} # end handle_request
sub print_style {
print qq~
~;
}
sub annotate {
my %parameters = @_;
# 0 ID
# 1 Tran_Plate
# 2 Tran_well
# 3 Plate
# 4 well
# 5 Symbol
# 6 QC_Positive
# 7 Lysate_SKU
# 8 protein_mw
# 9 RCclone
# 10 ProteinID
# 11 Accn
# 12 locus_id
# 13 swissprot
if ( $parameters{upload_file} ) {
my %map;
while ( my $line = ) {
chomp $line;
my @line = split( "\t", $line, -1 );
my $map_acc = $line[9];
$map_acc =~ s/RC2//;
$map{$map_acc} = $line[13] || $line[12] || 'na';
}
my $basename = $parameters{upload_file};
if ( $basename =~ /\.\w+$/ ) {
$basename =~ s/\.\w+/\.map/;
} else {
$basename .= '.map';
}
## upload the file to a file handler
my $fh = $q->upload('upload_file');
if (!$fh && $q->cgi_error && $is_html) {
# print $q->header(-status=>$q->cgi_error);
}
my $max_cnt = 100000;
my $cnt = 0;
# print $header;
while ( my $line = <$fh> ) {
chomp $line;
$line =~ s/\r//g;
my @line = split( "\t", $line, -1 );
if ( $cnt++ ) {
my $map_id = $line[0];
$map_id =~ s/TP3//;
my $sp_id = $map{$map_id} || 'NoMap';
push @line, $sp_id;
} else {
push @line, 'SwissProt';
}
print join( "\t", @line ) . "\n";
}
} else {
print_form();
}
exit;
} # annotate fetch_peptides;
sub print_locations {
my $params = shift;
if ( !$params->{peptides} && !$params->{plates} ) {
die "Choans";
print "
Must provide either peptides or plates to search
\n";
}
my $peptides = $params->{peptides};
$params->{peptide_string} = '';
$peptides =~ s/\s+/ /gm;
my @peptides = split /\s+/, $peptides;
my %peptides;
for my $peptide ( @peptides ) {
$peptides{$peptide}++;
}
my $peptide_and = '';
if ( $params->{peptides} && $params->{peptides} =~ /\w/ ) {
$peptide_and = 'AND sequence IN (';
my $sep = '';
for my $seq ( keys( %peptides ) ) {
$peptide_and .= $sep . "'$seq'";
$sep = ',';
$params->{peptide_string} .= "$seq\n";
}
$peptide_and .= ")\n";
}
my $plates = $params->{plates};
$params->{plate_string} = '';
$plates =~ s/\s+/ /gm;
my @plates = split /\s+/, $plates;
my %plates;
for my $plate ( @plates ) {
$plates{$plate}++;
}
my $plate_and = '';
if ( $params->{plates} && $params->{plates} =~ /\w/ ) {
my $sep = '';
for my $plate ( keys( %plates ) ) {
if ( $params->{fuzzy} ) {
if ( !$sep ) {
$plate_and .= "AND plate_barcode_id LIKE '%" . $plate . "'\n";
} else {
$plate_and .= "OR plate_barcode_id LIKE '%" . $plate . "'\n";
}
} else {
if ( !$sep ) {
$plate_and .= 'AND plate_barcode_id IN (';
}
$plate_and .= $sep . "'$plate'";
$params->{plate_string} .= "$plate\n";
}
$sep = ',';
}
$plate_and .= ")\n" unless $params->{fuzzy};
}
my $order_and = '';
if ( $params->{Order} && $params->{Order} ne 'All' ) {
$order_and = "AND orders.name = '$params->{Order}'";
}
open PEPS, "/net/db/projects/PeptideAtlas/MRMAtlas/current_libs/peptides2proteins.tsv" || die "Unable to open peptide file";
my %pep2prot;
while ( my $line = ) {
chomp $line;
my @line = split( /\t/, $line );
$pep2prot{$line[0]} = $line[1];
}
my $driver = 'DBI:mysql:mrmatlas_lims:mslims';
my $user = 'mrm_ro';
my $pass = 'Tsr3#@gfA356!&5';
my %error = ( PrintError => 1, RaiseError => 1);
my $dbh = DBI->connect($driver,$user,$pass,\%error);
my @headings = qw( sequence well_barcode_id freezer shelf rack box row col mw ssr isb_index peptides.status orders.name plate_barcode_id plate_type conc amountMol );
my $heading_str = join( ', ', @headings );
my $sql = qq~
SELECT $heading_str
FROM plates JOIN orders ON plates.order_id = orders.id
JOIN locations ON locations.id = plates.location_id
JOIN plate_peptides ON plate_peptides.plate_id = plates.id
JOIN peptides ON peptides.id = plate_peptides.peptide_id
LEFT JOIN vendor_plate_detail ON vendor_plate_detail.plate_peptide_id = plate_peptides.id
WHERE 1 = 1
$peptide_and
$plate_and
$order_and
ORDER BY orders.id ASC, plate_barcode_id ASC, row, col
~;
#SELECT sequence, mw, pp.volume as 'pp_vol', plate_barcode_id, o.name, vendor, amountMg, amountMol, conc, vpd.volume as 'vol'
#FROM peptides p JOIN plate_peptides pp ON p.id = pp.peptide_id
# JOIN plates pl ON pl.id = pp.plate_id
# JOIN orders o ON o.id = pl.order_id
# JOIN vendor_plate_detail vpd ON vpd.plate_peptide_id = pp.id
#limit 10 ;
$headings[15] = 'mg/ml';
$headings[16] = 'nmol';
my @wells;
if ( $params->{tmp_plate} ) {
$headings[17] = 'tmp_well';
for my $row ( 'A'..'H' ) {
for my $col ( 1..12 ) {
my $pcol = ( $col > 9 ) ? $col : '0' . $col;
push @wells, $row . $pcol;
}
}
}
my $sth = $dbh->prepare( $sql );
$sth->execute();
my @peptides;
push @headings, 'Protein ACC';
my $tmp_plate = 1;
while ( my @row = $sth->fetchrow_array() ) {
my $prot = $pep2prot{$row[0]} || '';
$row[10] = ucfirst( lc($row[10]) );
if ( $params->{type} =~ /working/i ) {
next if $row[14] =~ /master/i;
} elsif ( $params->{type} =~ /master/i ) {
next if $row[14] =~ /working/i;
}
if ( $params->{tmp_plate} ) {
my $well = shift @wells;
my $plate_well = $tmp_plate . '_' . $well;
$tmp_plate++ if $well eq 'H12';
push @row, $plate_well;
push @wells, $well;
}
push @peptides, [@row, $prot];
}
unshift @peptides, \@headings;
my $align = [ qw( left right left right right left left left left left left left ) ];
my $help_text = '';
my $change_on = 12;
my ( $html, $rs_name ) = $atlas->encodeSectionTable( header => 1,
width => '600',
align => $align,
rs_headings => \@headings,
rows => \@peptides,
rows_to_show => 25,
max_rows => 10,
help_text => $help_text,
chg_bkg_idx => $change_on,
set_download => 'Download peptides',
file_prefix => 'best_peptides_',
bg_color => '#EAEAEA',
sortable => 1,
table_id => 'pabst',
close_table => 1,
);
print qq~
~;
}
sub get_order_select {
my $driver = 'DBI:mysql:mrmatlas_lims:mslims';
my $user = 'mrm_ro';
my $pass = 'Tsr3#@gfA356!&5';
my %error = ( PrintError => 1, RaiseError => 1);
my $dbh = DBI->connect($driver,$user,$pass,\%error);
#DBI:mysql:$DB_DATABASE:$DB_SERVER
my $sql = qq~
SELECT DISTINCT orders.name
FROM plates JOIN orders ON plates.order_id = orders.id
JOIN plate_peptides ON plate_peptides.plate_id = plates.id
JOIN peptides ON peptides.id = plate_peptides.peptide_id
WHERE plate_type = 'MASTER'
ORDER BY orders.name ASC
~;
my $sth = $dbh->prepare( $sql );
$sth->execute();
my $select = qq~
\n";
$log->debug( $select );
return $select;
exit;
# my $dbh = DBI->connect($DB_DRIVER,$DB_RO_USER,$DB_RO_PASS,\%error_attr);
# mysql -umrm_ro -hmslims -P3306 -p'Tsr3#@gfA356!&5' -Dmrmatlas_lims
}
__DATA__