Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vignette update + function addition #19

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ Meta
pkgdown/
*.rds
.vscode/settings.json
.DS_Store
10 changes: 7 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ Version: 1.7.0
Authors@R:
c(person("Victor", "Yuan", email = "victor.2wy@gmail.com",
role = c("aut", "cre")),
person(c("Wendy", "P."), "Robinson",
role = "ctb"))
person(c("Wendy", "P."), "Robinson",
role = c("aut", "ctb")),
person("Icíar", "Fernández-Boyano", email = "iciarfernandez@outlook.com",
role = c("aut", "ctb")))
URL:
https://victor.rbind.io/planet, http://github.com/wvictor14/planet
BugReports:
Expand All @@ -21,7 +23,9 @@ Imports:
methods,
tibble,
magrittr,
dplyr
dplyr,
ExperimentHub,
mixOmics
Suggests:
badger,
ggplot2,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export(pl_infer_age)
export(pl_infer_ethnicity)
export(predictAge)
export(predictEthnicity)
export(predictPreeclampsia)
importFrom(magrittr,"%>%")
importFrom(tibble,tibble)
4 changes: 2 additions & 2 deletions R/predictEthnicity.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#' @description Uses 1860 CpGs to predict self-reported ethnicity on placental
#' microarray data.
#'
#' @details Predicts self-reported ethnicity from 3 classes: Africans, Asians,
#' and Caucasians, using placental DNA methylation data measured on the Infinium
#' @details Predicts self-reported ethnicity from 3 groups: African, Asian,
#' and European, using placental DNA methylation data measured on the Infinium
#' 450k/EPIC methylation array. Will return membership probabilities that often
#' reflect genetic ancestry composition.
#'
Expand Down
91 changes: 91 additions & 0 deletions R/predictPreeclampsia.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#' @title predictPreeclampsia
#'
#' @description Uses 45 CpGs to predict early preeclampsia (PE delivered before or at 34 weeks of gestation)
#' on placental DNA methylation microarray data.
#'
#' @details Assigns the class labels "early-PE" or "normotensive" to each sample
#' and returns a class probability.
#'
#' # It is recommended that users apply beta-mixture quantile normalization (BMIQ) to their data
#' prior to prediction. This was the normalization method used on the training data.
#'
#' @param betas matrix or array of methylation values on the beta scale (0, 1),
#' where the variables are arranged in rows, and samples in columns.
#'
#' @param ... feeds into outersect function
#'
#' @return produces a list with components detailed in the `mixOmics::predict` R documentation
#'
#' @examples
#'
#' # To predict early preeclampsia on 450k/850k samples
#'
#' # Load data
#' # data(peBetas)
#' # predictPreeclampsia(peBetas, dist = "max.dist")
#'
#' @export predictPreeclampsia
#'

predictPreeclampsia <- function(betas, ...){

# read in data to generate model
eh = ExperimentHub::ExperimentHub()
mod = eh[['EH8090']]
trainCpGs = colnames(mod$X)

# check that there are no NAs in the predictors (or if there are, how many)
peCpGs = mixOmics::selectVar(mod)$name
pp <- intersect(colnames(betas), peCpGs)

if(length(pp) < length(peCpGs)){
stop(paste(
"Only", length(pp), "out of 45 predictive CpGs present. All 45 predictive CpGs are needed to run the function."
))
} else {
message(paste(length(pp), "of 45 predictive CpGs present."))
message("BMIQ normalization is recommended for best results. If choosing other method, it is recommended to compare results to predictions on BMIQ normalized data.")
}

# set up data for prediction

# if input data is missing any of the cpgs present in the training data, this function
# adds the ones that are missing as NAs
# necessary for `mixOmics::predict` to work

outersect = function(x, y) {
sort(c(x[!x%in%y],
y[!y%in%x]))
}

if(inherits(betas, 'matrix')){
} else if (inherits(betas, 'array')) {
} else {

# throw an error
print(paste0("Input data must be a matrix or an array"))
}

betasSubset <- betas[,colnames(betas) %in% trainCpGs]

# order
betasSubset <- betasSubset[drop=FALSE,, trainCpGs]

if(all(colnames(betasSubset) == trainCpGs) == FALSE){
stop()
} else

# predict
out <- mixOmics:::predict.mixo_spls(mod, betasSubset)

# get class probabilities
CP <- out$predict[,,1]
CP <- t(apply(as.matrix(CP), 1, function(data) exp(data)/sum(exp(data))))
CP <- as.data.frame(CP) %>% tibble::rownames_to_column("Sample_ID")
CP$PE_Status <- CP$comp1
CP <- CP %>%
dplyr::mutate(PE_Status = dplyr::case_when(EOPE > 0.55 ~ "EOPE",
EOPE < 0.55 ~ "Normotensive"))

return(CP)
}
3 changes: 2 additions & 1 deletion man/planet-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/predictEthnicity.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions man/predictPreeclampsia.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading