R/getTransitionScores.R

#// **********************************************************************************************
#//                         getTransitionScores.R
#// **********************************************************************************************
#//
#// 
#// **********************************************************************************************
#// @Maintainer: Justin Sing
#// @Author: Justin Sing


#' @export
#' @title Extract Transition Information from .osw results file
#' @description This function can be used to extract Transition Id and Transition Posterior Error Probability information from the OSW results file obtained from
#' running OpenSwathWorkflow
#' 
#' @param oswfile A character vector of the absolute path and filename of the osw results file. (Must be .osw format)
#' @param run_name A character vector for extraction of a specific run, this should be the same name as the file name in the .OSW RUN table.
#' @param precursor_id A numeric value for a specific precursor id to extract information for. (Default: '')
#' @param peptide_id A string vector indicatig a specific peptide to extract information for. (Default: '')
#' @return A data.table containing Transition IDs and Transition Posterior Error Probabilities, and other useful information. Similar to getOSWData.R.
#' 
#' @author Justin Sing \url{https://github.com/singjc}
#' 
getTransitionScores_ <- function ( oswfile,
run_name,
precursor_id='',
peptide_id=''
) {

# Load Required Libraries
library(dplyr)
library(dbplyr)

# Connect to database
osw_db <- DBI::dbConnect( RSQLite::SQLite(), oswfile )

# Add to query statement for extracting data for a specific run, using run id.
if ( run_name != '' ){
	run_id_df = getRunID_( oswfile, run_name )
	run_id_query = sprintf( "INNER JOIN RUN ON RUN.ID = FEATURE.RUN_ID AND FEATURE.RUN_ID=(%s)", df$ID )
} else {
	run_id_query = 'INNER JOIN RUN ON RUN.ID = FEATURE.RUN_ID'
}

# Add to query statement for extracting data for only a specific precursor, or extract everything
if ( precursor_id != '' ){
	precursor_query = sprintf( "INNER JOIN FEATURE.PRECURSOR_ID = PRECURSOR.ID AND PRECURSOR.ID=(%s)", precursor_id )
} else {
	precursor_query = 'INNER JOIN FEATURE ON FEATURE.PRECURSOR_ID = PRECURSOR.ID'
}

# Add to query statement for extracting data for only a specific peptide, or extract everything
if ( peptide_id !='' ){
	peptide_query = sprintf( "INNER JOIN PEPTIDE ON PRECURSOR_PEPTIDE_MAPPING.PEPTIDE_ID = PEPTIDE.ID AND PEPTIDE.UNMODIFIED_SEQUENCE=('%s')", peptide_id )	
} else {
	peptide_query = 'INNER JOIN PEPTIDE ON PRECURSOR_PEPTIDE_MAPPING.PEPTIDE_ID = PEPTIDE.ID'}

# Construct Final Query Statement
stmt = sprintf( 
"SELECT PEPTIDE.MODIFIED_SEQUENCE || '_' || PRECURSOR.ID AS transition_group_id,
       PRECURSOR.DECOY AS decoy,
       RUN.ID AS run_id,
       RUN.FILENAME AS filename,
       FEATURE.EXP_RT AS RT,
       PRECURSOR.LIBRARY_RT AS lib_RT,
       FEATURE.ID AS id,
       PEPTIDE.UNMODIFIED_SEQUENCE AS Sequence,
       PEPTIDE.MODIFIED_SEQUENCE AS FullPeptideName,
       PRECURSOR.CHARGE AS Charge,
       PRECURSOR.PRECURSOR_MZ AS mz,
       FEATURE.LEFT_WIDTH AS leftWidth,
       FEATURE.RIGHT_WIDTH AS rightWidth,
       SCORE_TRANSITION.FEATURE_ID as transition_feature_id,
	   SCORE_TRANSITION.PEP as transition_pep,
	   SCORE_TRANSITION.PVALUE as transition_pval,
	   SCORE_TRANSITION.QVALUE as transition_qval,
	   SCORE_TRANSITION.RANK as transition_rank,
	   SCORE_TRANSITION.SCORE as tranistion_score,
	   SCORE_TRANSITION.TRANSITION_ID as transition_id
FROM PRECURSOR
INNER JOIN PRECURSOR_PEPTIDE_MAPPING ON PRECURSOR.ID = PRECURSOR_PEPTIDE_MAPPING.PRECURSOR_ID
%s
%s
%s
LEFT JOIN FEATURE_TRANSITION ON FEATURE_TRANSITION.FEATURE_ID = FEATURE.ID
LEFT JOIN SCORE_TRANSITION ON SCORE_TRANSITION.FEATURE_ID = FEATURE.ID
ORDER BY transition_group_id,
         transition_rank;", peptide_query, precursor_query, run_id_query )
    
# Query Database
df_osw <- collect( tbl(osw_db, sql( stmt )) )

# Disconnect form database
DBI:dbDisconnect( osw_db )

return( df_osw )
}
Roestlab/mstools documentation built on Nov. 26, 2019, 2:07 a.m.