Function and Manual
image.png
https://github.com/cole-trapnell-lab/monocle-release/blob/master/R/expr_models.R
Source Code
#' Fit smooth spline curves and return the response matrix
#'
#' This function will fit smooth spline curves for the gene expression dynamics along pseudotime in a gene-wise manner and return
#' the corresponding response matrix. This function is build on other functions (fit_models and responseMatrix) and used in calILRs and calABCs functions
#'
#' @param cds a CellDataSet object upon which to perform this operation
#' @param new_data a data.frame object including columns (for example, Pseudotime) with names specified in the model formula. The values in the data.frame should be consist with the corresponding values from cds object.
#' @param trend_formula a formula string specifying the model formula used in fitting the spline curve for each gene/feature.
#' @param relative_expr a logic flag to determine whether or not the relative gene expression should be used
#' @param response_type the response desired, as accepted by VGAM's predict function
#' @param cores the number of cores to be used while testing each gene for differential expression
#' @importFrom Biobase fData
#' @return a data frame containing the data for the fitted spline curves.
#' @export
#'
genSmoothCurves <- function(cds, new_data, trend_formula = "~sm.ns(Pseudotime, df = 3)",
relative_expr = T, response_type="response", cores = 1) {
expressionFamily <- cds@expressionFamily
if(cores > 1) {
expression_curve_matrix <- mcesApply(cds, 1, function(x, trend_formula, expressionFamily, relative_expr, new_data, fit_model_helper, responseMatrix,
calculate_NB_dispersion_hint, calculate_QP_dispersion_hint){
environment(fit_model_helper) <- environment()
environment(responseMatrix) <- environment()
model_fits <- fit_model_helper(x, modelFormulaStr = trend_formula, expressionFamily = expressionFamily,
relative_expr = relative_expr, disp_func = cds@dispFitInfo[['blind']]$disp_func)
if(is.null(model_fits))
expression_curve <- as.data.frame(matrix(rep(NA, nrow(new_data)), nrow = 1))
else
expression_curve <- as.data.frame(responseMatrix(list(model_fits), newdata = new_data, response_type=response_type))
colnames(expression_curve) <- row.names(new_data)
expression_curve
#return(expression_curve)
}, required_packages=c("BiocGenerics", "Biobase", "VGAM", "plyr"), cores=cores,
trend_formula = trend_formula, expressionFamily = expressionFamily, relative_expr = relative_expr, new_data = new_data,
fit_model_helper = fit_model_helper, responseMatrix = responseMatrix, calculate_NB_dispersion_hint = calculate_NB_dispersion_hint,
calculate_QP_dispersion_hint = calculate_QP_dispersion_hint
)
expression_curve_matrix <- as.matrix(do.call(rbind, expression_curve_matrix))
return(expression_curve_matrix)
}
else {
expression_curve_matrix <- smartEsApply(cds, 1, function(x, trend_formula, expressionFamily, relative_expr, new_data){
environment(fit_model_helper) <- environment()
environment(responseMatrix) <- environment()
model_fits <- fit_model_helper(x, modelFormulaStr = trend_formula, expressionFamily = expressionFamily,
relative_expr = relative_expr, disp_func = cds@dispFitInfo[['blind']]$disp_func)
if(is.null(model_fits))
expression_curve <- as.data.frame(matrix(rep(NA, nrow(new_data)), nrow = 1))
else
expression_curve <- as.data.frame(responseMatrix(list(model_fits), new_data, response_type=response_type))
colnames(expression_curve) <- row.names(new_data)
expression_curve
},
convert_to_dense=TRUE,
trend_formula = trend_formula, expressionFamily = expressionFamily, relative_expr = relative_expr, new_data = new_data
)
expression_curve_matrix <- as.matrix(do.call(rbind, expression_curve_matrix))
row.names(expression_curve_matrix) <- row.names(fData(cds))
return(expression_curve_matrix)
}
}
1. cds
The monocle2 object used
2. new_data
new_data <- data.frame(Pseudotime = pData(cds_subset)$Pseudotime)
cds_subset <- newCellDataSet(as.matrix(exprs_data),
phenoData = new("AnnotatedDataFrame", data = pData),
featureData = new("AnnotatedDataFrame", data = fData),
expressionFamily=cds@expressionFamily,
lowerDetectionLimit=cds@lowerDetectionLimit)
pData(cds_subset)$State <- as.factor(pData(cds_subset)$State)
pData(cds_subset)$Size_Factor <- Size_Factor
cds_subset@dispFitInfo <- cds@dispFitInfo

image.png



网友评论