#!/usr/local/bin/perl use strict; use Getopt::Long; use FindBin; use Data::Dumper; use List::MoreUtils qw(uniq); 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 CGI::Carp qw(fatalsToBrowser croak); 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::ConsensusSpectrum; use SBEAMS::PeptideAtlas::ModificationHelper; use SBEAMS::PeptideAtlas::Utilities; use SBEAMS::PeptideAtlas::ProtInfo qw(is_uniprot_identifier); use SBEAMS::Proteomics::Tables; use SBEAMS::BioLink::Tables; $sbeams = new SBEAMS::Connection; $sbeamsMOD = new SBEAMS::PeptideAtlas; $sbeamsMOD->setSBEAMS($sbeams); $sbeams->setSBEAMS_SUBDIR($SBEAMS_SUBDIR); my $htmlmode; my $current_page = { organism => '', atlas_build_id => '' }; ############################################################################### # 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'], # connect_read_only=>1, allow_anonymous_access=>1, )); #### 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); #$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 { my $project_id = $sbeamsMOD->getProjectID( atlas_build_id => $parameters{atlas_build_id} ); $sbeamsMOD->display_page_header( project_id => $project_id, use_tabbed_panes => 1 ); handle_request(ref_parameters=>\%parameters); $sbeamsMOD->display_page_footer( use_tabbed_panes => 1 ); } } # 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}; #### Show current user context information #$sbeams->printUserContext(); #### Get the HTML to display the tabs my $tabMenu = $sbeamsMOD->getTabMenu( parameters_ref => \%parameters, program_name => $PROG_NAME, ); if ($sbeams->output_mode() eq 'html') { print $tabMenu->asHTML(); print ""; } #### Get the current atlas_build_id based on parameters or session my $atlas_build_id = $sbeamsMOD->getCurrentAtlasBuildID( parameters_ref => \%parameters, ); if (defined($atlas_build_id) && $atlas_build_id < 0) { return; } $parameters{atlas_build_id} = $atlas_build_id; #### Define some generic variables 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="Get PTM Spectra"; 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); if ($apply_action =~ /VIEWRESULTSET|VIEWPLOT/ ) { $sbeams->readResultSet( resultset_file=>$rs_params{set_name}, resultset_ref=>$resultset_ref, query_parameters_ref=>\%parameters ); } #### Build ATLAS_BUILD constraint my $atlas_build_clause = $sbeams->parseConstraint2SQL( constraint_column=>"AB.atlas_build_id", constraint_type=>"int_list", constraint_name=>"Atlas Build", constraint_value=>$parameters{atlas_build_id} ); return if ($atlas_build_clause eq '-1'); my $chimera_clause = ''; if ($parameters{ptm_type} =~ /\_respect/){ $chimera_clause = 'AND S.chimera_level > 1'; $parameters{ptm_type} =~ s/_respect//; } my $ptm_type_clause = $sbeams->parseConstraint2SQL( constraint_column=>"SPI.ptm_type", constraint_type=>"plain_text", constraint_name=>"PTM Type", constraint_value=>$parameters{ptm_type} ); return if ($ptm_type_clause eq '-1'); my $protein_site_clause = ''; if ($parameters{site}){ $parameters{site} =~ s/\s//g; if ($parameters{site} =~ /\D/){ $sbeams->reportException( state => 'ERROR', type => 'CONSTRAINT ERROR', message => 'protein site should be numbers', ); return; } $protein_site_clause = qq~ AND PM.start_in_biosequence <= $parameters{site} AND PM.end_in_biosequence >= $parameters{site} ~; } my $biosequence_name_clause = $sbeams->parseConstraint2SQL( constraint_column=>"BS.biosequence_name", constraint_type=>"plain_text", constraint_name=>"biosequence_name", constraint_value=>$parameters{biosequence_name} ); return if ($biosequence_name_clause eq '-1'); my %colnameidx = (); my @column_titles = (); my @column_array = ( ["biosequence_name", "BS.biosequence_name","Biosequence Name"], ["site", "''", "site"], ["residue", "''", "residue"], ["Spectrum", "SI.spectrum_identification_id","Spectrum"], ["start_in_biosequence", "PM.start_in_biosequence", "Peptide Mapping Start"], ["modpep", "modified_peptide_sequence", "Modified Sequence"], ["chg", "MPI.peptide_charge","Charge"], #["instrument_name", "I.instrument_name","Instr"], ["probability", "SI.probability","Prob"], ["site_ptm_score", "''", "PTM Score"], ["ptm_sequence", "SPI.ptm_sequence","PTM Sequence"], ["ptm_lability","SPI.ptm_lability", "PTM Lability"], ["ptm_norm_info_gain", "SPI.ptm_norm_info_gain", "PTM Normalized Information Content"], ["frg_type", "FRG.FRAGMENTATION_TYPE","Fragmentation Type"], ["precursor_intensity", "S.precursor_intensity", "Precursor Intensity (x10^6)"], ["total_ion_current", "S.total_ion_current", "TIC (x10^6)"], ["signal_to_noise", "S.signal_to_noise", "S/N"], ["scan_time", "scan_time", "RT"], ["collision_energy","collision_energy", "CE"], ["spectrum_name", "S.spectrum_name","Spectrum Name"], ["ptm_type", "SPI.ptm_type","PTM Type"], ["sample_tag", "SMP.sample_tag", "Experiment Tag"], ["sample_id", "SMP.sample_id","Expt"], ); my $columns_clause = $sbeams->build_SQL_columns_list( column_array_ref=>\@column_array, colnameidx_ref=>\%colnameidx, column_titles_ref=>\@column_titles ); my $sql = qq~ select distinct $columns_clause FROM $TBAT_PEPTIDE_INSTANCE PI JOIN $TBAT_PEPTIDE_MAPPING PM ON (PM.peptide_instance_id = PI.peptide_instance_id) JOIN $TBAT_MODIFIED_PEPTIDE_INSTANCE MPI ON ( PI.peptide_instance_id = MPI.peptide_instance_id ) JOIN $TBAT_ATLAS_BUILD AB ON ( PI.atlas_build_id = AB.atlas_build_id ) JOIN $TBAT_SPECTRUM_IDENTIFICATION SI ON ( MPI.modified_peptide_instance_id = SI.modified_peptide_instance_id ) JOIN $TBAT_SPECTRUM_PTM_IDENTIFICATION SPI ON ( SI.spectrum_identification_id = SPI.spectrum_identification_id) JOIN $TBAT_SPECTRUM S ON ( SI.spectrum_id = S.spectrum_id ) LEFT JOIN $TBAT_FRAGMENTATION_TYPE FRG ON (FRG.FRAGMENTATION_TYPE_ID = S.FRAGMENTATION_TYPE_ID) JOIN $TBAT_SAMPLE SMP ON ( S.sample_id = SMP.sample_id ) JOIN $TBAT_BIOSEQUENCE BS ON (BS.BIOSEQUENCE_ID = PM.MATCHED_BIOSEQUENCE_ID) WHERE 1=1 $atlas_build_clause $protein_site_clause $ptm_type_clause $biosequence_name_clause $chimera_clause ~; my $show_sql = 0; #### Apply any parameter adjustment logic if ( $parameters{display_options} =~ /ShowSQL/ ) { $show_sql = 1; } $hidden_cols{'RT'} =1; $hidden_cols{'CE'} =1; #$hidden_cols{'ptm_type'} =1; $hidden_cols{'Expt'} = 1; $sbeams->display_sql( sql => $sql,use_tabbed_panes => 0 ) if ($show_sql); ######################################################################### #### If QUERY or VIEWRESULTSET was selected, display the data if ($apply_action =~ /QUERY|VIEWRESULTSET|VIEWPLOT/i ) { if ($apply_action =~ /QUERY/){ $sbeams->fetchResultSet( sql_query=>$sql, resultset_ref=>$resultset_ref, use_caching => 0 ); postProcessResultset( rs_params_ref=>\%rs_params, resultset_ref=>$resultset_ref, query_parameters_ref=>\%parameters, column_titles_ref=>\@column_titles, colnameidx_ref => \%colnameidx, hidden_cols => \%hidden_cols); $rs_params{set_name} = "SETME"; my %write_params = ( rs_table => $TBAT_ATLAS_BUILD, key_field => 'atlas_build_id', key_value => $parameters{atlas_build_id} ); $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", column_titles_ref=>\@column_titles, # %write_params ); } #### Construct table help my $obs_help = ''; # get_table_help( 'spectra' ); #### 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, use_tabbed_panes => 1, column_titles_ref=>\@column_titles, column_help=>$obs_help, base_url=>$base_url, ); #### Display the resultset controls $sbeams->displayResultSetControls( resultset_ref=>$resultset_ref, query_parameters_ref=>\%parameters, rs_params_ref=>\%rs_params, use_tabbed_panes => 1, base_url=>$base_url, ); # #### Display a plot of data from the resultset # $sbeams->displayResultSetPlot_plotly( # rs_params_ref=>\%rs_params, # resultset_ref=>$resultset_ref, # query_parameters_ref=>\%parameters, # column_titles_ref=>\@column_titles, # use_tabbed_panes => 1, # mouseover_column => 'peptide_sequence', # mouseover_url => $url_cols{'Peptide Sequence'}, # mouseover_tag => '%1V', # base_url=>$base_url, # ); print qq~ ~; }else{ if ($sbeams->invocation_mode() eq 'http') { print "

Select parameters above and press QUERY

\n"; } else { print "You need to supply some parameters to contrain the query\n"; } } } # # my $spectra_display = $sbeamsMOD->get_individual_spectra_display( # column_titles_ref=>\@column_titles, # colnameidx_ref => \%colnameidx, # resultset_ref=> $resultset_ref, # hidden_cols_ref=>\%hidden_cols ); # # # ############################################################################# # #### PTM Spectra # ############################################################################# # if (%ptm_peps && $htmlmode){ # $sbeamsMOD->display_spectra_ptm_table ( # ptm_identification_ref => \%ptm_peps, # column_titles_ref=>\@column_titles, # colnameidx_ref => \%colnameidx, # resultset_ref=> $resultset_ref, # hidden_cols_ref=>\%hidden_cols, # ptm_score_summary_ref => \%ptm_score_summary ); # } # # } #}# end handle_request ################################################################################# sub postProcessResultset { my %args = @_; my ($i,$element,$key,$value,$line,$result,$sql); #### Process the arguments list my $resultset_ref = $args{'resultset_ref'}; my $rs_params_ref = $args{'rs_params_ref'}; my $query_parameters_ref = $args{'query_parameters_ref'}; my $column_titles_ref = $args{'column_titles_ref'}; my $colnameidx_ref = $args{'colnameidx_ref'}; my $min_ptm_score = $query_parameters_ref->{min} || 0; my $max_ptm_score = $query_parameters_ref->{max} || 1.1; my $nochoice_id = $query_parameters_ref->{nochoice} || 0; my $query_site = $query_parameters_ref->{site}; my $residue = $query_parameters_ref->{residue}; #-------------------------------------------------- # ["biosequence_name", "BS.biosequence_name","Biosequence Name"], # ["site", "''", "site"], # ["residue", "''", "residue"], # ["spectrum_name", "S.spectrum_name","Spectrum Name"], # ["Spectrum", "SI.spectrum_identification_id","Spectrum"], # ["start_in_biosequence", "PM.start_in_biosequence", "Peptide Mapping Start"], # ["modpep", "modified_peptide_sequence", "Modified Sequence"], # ["chg", "MPI.peptide_charge","Charge"], # ["probability", "SI.probability","Prob"], # ["ptm_sequence", "SPI.ptm_sequence","PTM Sequence"], # ["ptm_lability","SPI.ptm_lability", "PTM Lability"], # ["frg_type", "FRG.FRAGMENTATION_TYPE","Fragmentation Type"], # ["precursor_intensity", "S.precursor_intensity", "Precursor Intensity (x10^6)"], # ["total_ion_current", "S.total_ion_current", "TIC (x10^6)"], # ["signal_to_noise", "S.signal_to_noise", "S/N"], # ["scan_time", "scan_time", "RT"], # ["collision_energy","collision_energy", "CE"], # ["ptm_type", "SPI.ptm_type","PTM Type"], # ["sample_tag", "SMP.sample_tag", "Experiment Tag"], # ["sample_id", "SMP.sample_id","Expt"], #-------------------------------------------------- my $protein_idx =$colnameidx_ref->{biosequence_name}; my $offset_idx = $colnameidx_ref->{site}; my $residue_idx = $colnameidx_ref->{residue}; my $name_idx = $colnameidx_ref->{spectrum_name}; my $spec_idx = $colnameidx_ref->{"Spectrum"}; my $mseq_idx = $colnameidx_ref->{"modpep"}; my $chrg_idx = $colnameidx_ref->{"chg"}; my $smpl_idx = $colnameidx_ref->{"sample_id"}; my $prob_idx = $colnameidx_ref->{"probability"}; my $ptm_idx = $colnameidx_ref->{"ptm_sequence"}; my $ptm_lability_idx = $colnameidx_ref->{"ptm_lability"}; my $frag_idx = $colnameidx_ref->{"frg_type"}; my $it_idx = $colnameidx_ref->{"precursor_intensity"}; my $tic_idx = $colnameidx_ref->{"total_ion_current"}; my $sn_idx = $colnameidx_ref->{"signal_to_noise"}; my $rt_idx = $colnameidx_ref->{"scan_time"}; my $ce_idx = $colnameidx_ref->{"collision_energy"}; my $ptm_type_idx = $colnameidx_ref->{"ptm_type"}; my $start_idx = $colnameidx_ref->{"start_in_biosequence"}; my $sample_tag_idx = $colnameidx_ref->{"sample_tag"}; my $site_ptm_score_idx = $colnameidx_ref->{"site_ptm_score"}; my $has_ptm_score = 0; my $n_rows = scalar(@{$resultset_ref->{data_ref}}); my %processed_spec=(); my @processed_data; for (my $i=0; $i<$n_rows; $i++) { #### Loops through resultset and do formatting my $pep = $resultset_ref->{data_ref}->[$i]; $pep->[$offset_idx] = $query_site; $pep->[$residue_idx] = $residue; $pep->[$name_idx] =~ /(\S+)\.(\d+?)\.(\d+?)\.(\d)/; $processed_spec{$pep->[$name_idx] . '-'. $pep->[$smpl_idx] }++; my $mzml_basename = $1; my $precursor_charge = $4; $mzml_basename =~ s/\.mzML$//; #remove .mzML if it's there my $mzml_fname = "$mzml_basename.mzML"; my $rt; $pep->[$prob_idx] = sprintf("%0.3f", $pep->[$prob_idx]) unless $pep->[$prob_idx] == 1; $pep->[$prob_idx] = 1 if $pep->[$prob_idx] == 1; my $build_id = $query_parameters_ref->{atlas_build_id}; my $img = ""; $pep->[$spec_idx] = qq~$img~; my $modified_pepseq = $pep->[$mseq_idx]; my $start_in_biosequence = $pep->[$start_idx]; # $pep->[$mseq_idx] = $sbeamsMOD->formatMassMods($pep->[$mseq_idx]); ## filter using offset and min_score my $pass = 0; if ($pep->[$ptm_idx]){ $pep->[$ptm_type_idx] =~ /^(.*):(.*)$/; my $ptm_residues=$1; my $ptm_name = $2; my $ptm_sequence = $pep->[$ptm_idx]; my @elms = split(/\)/, $ptm_sequence); ### filter no choice ids my $modified_pepseq_w_n = $modified_pepseq; next if ($residue eq 'n' && $modified_pepseq !~ /^\[/); if ($ptm_residues =~ /n/ && $modified_pepseq !~ /^n/){ $modified_pepseq_w_n = 'n'. $modified_pepseq_w_n; } my @m = $ptm_sequence =~ /([${ptm_residues}])/g; my @n = $modified_pepseq_w_n =~ /([${ptm_residues}]\[${ptm_name}\])/g; ## for human phospho 2022-04 build if ($modified_pepseq_w_n =~ /[${ptm_residues}]\[/ && $modified_pepseq_w_n !~ /${ptm_name}/){ @n = $modified_pepseq_w_n =~ /([${ptm_residues}]\[)/g; } my $m = scalar @m; my $n = scalar @n; my $nochoice = 0; if ($n == $m){ if (! $nochoice_id){ next; } $nochoice = 1; } if($ptm_sequence =~ /^n/ && $ptm_sequence !~ /n\(/){ $ptm_sequence=~ s/n//; } my $cur_pos = $pep->[$start_idx]-1; foreach my $str (@elms){ if ($str =~ /(.*)\((.*)/){ my $aas = $1; my $ptm_score = $2; $cur_pos = $cur_pos + length($aas); #print "$ptm_sequence $query_site $aas $pep->[$start_idx] $cur_pos min_score=$min_ptm_score $ptm_score
"; if ($query_site && $cur_pos == $query_site){ if ($nochoice ){ if ($nochoice_id ){ $pass = 1 ; $pep->[$site_ptm_score_idx] = $ptm_score; } }else{ next if ($nochoice_id); if ($ptm_score >= $min_ptm_score && $ptm_score < $max_ptm_score){ $pep->[$site_ptm_score_idx] = $ptm_score; $pass =1; } } last; } $cur_pos-- if ($aas eq 'n'); } else { $cur_pos = $cur_pos + length($str); } } } next if(! $pass); $pep->[$it_idx] = ( $pep->[$it_idx] ) ? sprintf("%.2f", $pep->[$it_idx]/1000000) : $sbeams->makeInactiveText('n/a'); $pep->[$tic_idx] = ( $pep->[$tic_idx] ) ? sprintf("%.2f", $pep->[$tic_idx]/1000000): $sbeams->makeInactiveText('n/a'); $pep->[$sn_idx] = ( $pep->[$sn_idx] ) ? sprintf("%.0f", $pep->[$sn_idx]) : $sbeams->makeInactiveText('n/a'); $pep->[$rt_idx] = ( $pep->[$rt_idx] ) ? sprintf( "%0.1f", $pep->[$rt_idx]/60 ) : $sbeams->makeInactiveText('n/a'); $pep->[$ce_idx] = ( $pep->[$ce_idx] ) ? sprintf( "%0.1f", $pep->[$ce_idx] ) : $sbeams->makeInactiveText('n/a'); #if ($processed_spec{$pep->[$name_idx]. '-'. $pep->[$smpl_idx]} == 1){ push @processed_data, $resultset_ref->{data_ref}->[$i]; #} } # end for each pep $resultset_ref->{data_ref} = \@processed_data; if ($has_ptm_score){ #sort_ptm_peps($ptm_peps_ref); } }