Nothing
#' Prepare Lesion and Expression Data for Waterfall Plots
#'
#' @description
#' Prepares matched lesion and expression data for a selected gene for use with
#' the \code{\link{alex.waterfall.plot}} function.
#'
#' @usage
#' alex.waterfall.prep(
#' alex.data,
#' alex.kw.results,
#' gene,
#' lsn.data
#' )
#'
#' @param alex.data Output from \code{\link{alex.prep.lsn.expr}}. A list
#' containing \code{"alex.expr"} (expression data), \code{"alex.lsn"}
#' (lesion-group data), and \code{"alex.row.mtch"} (matched Ensembl gene IDs).
#' The expression and lesion data contain genes in rows and subjects in columns,
#' with matching row and column order.
#'
#' @param alex.kw.results Kruskal-Wallis lesion-expression association results
#' returned by \code{\link{KW.hit.express}}. The data frame must contain
#' \code{"gene"} with Ensembl gene IDs and \code{"gene.name"} with gene symbols,
#' together with gene coordinates and the Kruskal-Wallis results.
#'
#' @param gene Gene of interest specified by either its gene symbol or Ensembl
#' gene ID. The value must match exactly one gene in \code{alex.kw.results}.
#'
#' @param lsn.data A data frame containing genomic lesion data in GRIN-compatible
#' format. It must contain the columns \code{"ID"} (subject ID), \code{"chrom"}
#' (chromosome), \code{"loc.start"} (lesion start position), \code{"loc.end"}
#' (lesion end position), and \code{"lsn.type"} (lesion type).
#'
#' @details
#' The function identifies the selected gene in the Kruskal-Wallis results and
#' extracts its matched lesion-group and expression values across subjects from
#' \code{alex.data}. It also identifies all lesions in \code{lsn.data} whose
#' genomic coordinates overlap the selected gene.
#'
#' The gene symbol is used to label the lesion and expression columns when
#' available. If the gene symbol is missing, the Ensembl gene ID is used
#' instead.
#'
#' @return
#' A list with the following components:
#' \item{gene.lsn.exp}{A data frame containing subject ID, lesion-group status,
#' and gene expression. The lesion and expression columns are named
#' \code{"<gene>.lsn"} and \code{"<gene>.RNA"}, respectively.}
#' \item{lsns}{A data frame containing all lesions in \code{lsn.data} that
#' overlap the genomic coordinates of the selected gene.}
#' \item{stats}{A one-row data frame containing the Kruskal-Wallis
#' lesion-expression association results for the selected gene.}
#' \item{gene.ID}{The gene symbol used to label the output columns, or the
#' Ensembl gene ID when a gene symbol is unavailable.}
#'
#' @export
#'
#' @references
#' Cao, X., Elsayed, A. H., & Pounds, S. B. (2023). Statistical Methods Inspired
#' by Challenges in Pediatric Cancer Multi-omics.
#'
#' @author
#' Abdelrahman Elsayed \email{abdelrahman.elsayed@stjude.org},
#' Stanley Pounds \email{stanley.pounds@stjude.org}
#'
#' @seealso
#' \code{\link{alex.prep.lsn.expr}},
#' \code{\link{KW.hit.express}},
#' \code{\link{alex.waterfall.plot}}
#'
#' @examples
#' data(expr_data)
#' data(lesion_data)
#' data(hg38_gene_annotation)
#'
#' # Prepare matched expression and lesion data
#' alex.data <- alex.prep.lsn.expr(expr_data,
#' lesion_data,
#' hg38_gene_annotation,
#' min.expr = 1,
#' min.pts.lsn = 5)
#'
#' # Run Kruskal-Wallis test
#' alex.kw.results <- KW.hit.express(alex.data,
#' hg38_gene_annotation,
#' min.grp.size = 5)
#'
#' # Prepare lesion and expression data for waterfall plot of WT1
#' WT1.waterfall.prep <- alex.waterfall.prep(alex.data,
#' alex.kw.results,
#' "WT1",
#' lesion_data)
alex.waterfall.prep=function(alex.data, # output of the alex.prep.lsn.expr function (list of three data table "alex.expr" with expression data, "alex.lsn" with lesion data and row.mtch)
alex.kw.results, # ALEX Kruskal-Wallis test results (output of the KW.hit.express function)
gene, # gene name or ensembl.ID
lsn.data) # lesion data in a GRIN compatible format
{
# Validate input data
if (!is.list(alex.data))
stop("alex.data must be the output from alex.prep.lsn.expr().")
required.alex.objects=c("alex.expr","alex.lsn","alex.row.mtch")
if (!all(required.alex.objects %in% names(alex.data)))
stop("alex.data must contain: alex.expr, alex.lsn, and alex.row.mtch.")
if (!is.data.frame(alex.kw.results))
stop("alex.kw.results must be a data frame.")
required.kw.cols=c("gene","gene.name","chrom","loc.start","loc.end")
if (!all(required.kw.cols %in% colnames(alex.kw.results)))
stop("alex.kw.results must contain the columns: ",paste(required.kw.cols,collapse=", "), ".")
if (!is.character(gene) || length(gene)!=1 || is.na(gene) || gene=="")
stop("gene must be a single gene symbol or Ensembl gene ID.")
if (!is.data.frame(lsn.data))
stop("lsn.data must be a data frame.")
required.lsn.cols=c("ID","chrom","loc.start","loc.end","lsn.type")
if (!all(required.lsn.cols %in% colnames(lsn.data)))
stop("lsn.data must contain the columns: ",paste(required.lsn.cols,collapse=", "), ".")
# Find the row of the KW test with results for the gene
int.row=which((alex.kw.results$gene==gene)|
(alex.kw.results$gene.name==gene))
if (length(int.row)!=1) stop("gene must match exactly ONE row of alex.kw.results")
ens.ID=alex.kw.results$gene[int.row]
gene.ID=alex.kw.results$gene.name[int.row]
# Use the Ensembl ID when gene name is unavailable
if (is.na(gene.ID) || gene.ID=="")
gene.ID=ens.ID
gene.lsn.exp=as.data.frame(colnames(alex.data$alex.lsn))
colnames(gene.lsn.exp)="ID"
# Add the lesion data for this gene to the gene.lsn.exp data
rownames(gene.lsn.exp)=gene.lsn.exp$ID
gene.lsn=paste0(gene.ID,".lsn")
lsn.row=which(rownames(alex.data$alex.lsn)==ens.ID)
if (length(lsn.row)!=1)
stop("The selected gene could not be matched to exactly one row of alex.data$alex.lsn.")
gene.lsn.exp[,gene.lsn]=""
lsn.ids=intersect(gene.lsn.exp$ID,colnames(alex.data$alex.lsn))
gene.lsn.exp[lsn.ids,gene.lsn]=unlist(alex.data$alex.lsn[lsn.row,lsn.ids])
# Add the expression data for this gene to the gene.lsn.exp data
gene.rna=paste0(gene.ID,".RNA")
gene.lsn.exp[,gene.rna]=NA
rna.ids=intersect(gene.lsn.exp$ID,colnames(alex.data$alex.expr))
rna.row=which(rownames(alex.data$alex.expr)==ens.ID)
if (length(rna.row)!=1)
stop("The selected gene could not be matched to exactly one row of alex.data$alex.expr.")
gene.lsn.exp[rna.ids,gene.rna]=unlist(alex.data$alex.expr[rna.row,rna.ids])
# Find lesions that overlap this gene
lsn.mtch=(lsn.data$chrom==alex.kw.results$chrom[int.row])&
(lsn.data$loc.end>=alex.kw.results$loc.start[int.row])&
(lsn.data$loc.start<=alex.kw.results$loc.end[int.row])
lsns=lsn.data[lsn.mtch,]
res=list(gene.lsn.exp=gene.lsn.exp, # data table with three columns that has patient ID, type of lesions that affect this gene if any, expression level of the gene
lsns=lsns, # all lesions that affect this gene of interest
stats=alex.kw.results[int.row,], # KW test results for the gene of interest
gene.ID=gene.ID) # gene name
return(res)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.