## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----eval=FALSE---------------------------------------------------------------
#  library(ExperimentHub)
#  
#  hub <- ExperimentHub()
#  
#  query(hub, "FlowSorted.Blood.EPIC")
#  
#  FlowSorted.Blood.EPIC <- hub[["EH1136"]]
#  
#  FlowSorted.Blood.EPIC

## ----eval=FALSE---------------------------------------------------------------
#  head(IDOLOptimizedCpGs)

## ----eval=FALSE---------------------------------------------------------------
#  head(IDOLOptimizedCpGs450klegacy)

## ----eval=FALSE---------------------------------------------------------------
#  # Step 1: Load the reference library to extract the artificial mixtures
#  
#  library(ExperimentHub)
#  hub <- ExperimentHub()
#  query(hub, "FlowSorted.Blood.EPIC")
#  FlowSorted.Blood.EPIC <- hub[["EH1136"]]
#  FlowSorted.Blood.EPIC
#  
#  # Step 2 separate the reference from the testing dataset
#  
#  RGsetTargets <- FlowSorted.Blood.EPIC[,
#               FlowSorted.Blood.EPIC$CellType == "MIX"]
#  
#  sampleNames(RGsetTargets) <- paste(RGsetTargets$CellType,
#                              seq_len(dim(RGsetTargets)[2]), sep = "_")
#  RGsetTargets
#  
#  # Step 3: Deconvolute using the IDOL L-DMR
#  
#  head (IDOLOptimizedCpGs)
#  
#  # If you need to deconvolute a 450k legacy dataset use
#  # IDOLOptimizedCpGs450klegacy instead
#  
#  # Do not run with limited RAM the normalization step requires a big amount of
#  # memory resources
#  
#  if (memory.limit()>8000){
#   countsEPIC<-estimateCellCounts2(RGsetTargets, compositeCellType = "Blood",
#                                  processMethod = "preprocessNoob",
#                                  probeSelect = "IDOL",
#                                  cellTypes = c("CD8T", "CD4T", "NK", "Bcell",
#                                  "Mono", "Neu"),
#                                  referencePlatform =
#                                  "IlluminaHumanMethylationEPIC",
#                                  referenceset = NULL,
#                                  IDOLOptimizedCpGs =IDOLOptimizedCpGs,
#                                  returnAll = FALSE)
#  
#  head(countsEPIC$counts)
#  }

## -----------------------------------------------------------------------------
library (FlowSorted.Blood.EPIC)
library(FlowSorted.CordBloodCombined.450k)
data("IDOLOptimizedCpGsCordBlood")
# Step 1: Load the reference library to extract the umbilical cord samples
library(ExperimentHub)
hub <- ExperimentHub()
myfiles <- query(hub, "FlowSorted.CordBloodCombined.450k")
FlowSorted.CordBloodCombined.450k <- myfiles[[1]]
FlowSorted.CordBloodCombined.450k

# Step 2 separate the reference from the testing dataset if you want to run 
# examples for estimations for this function example

RGsetTargets <- FlowSorted.CordBloodCombined.450k[,
                FlowSorted.CordBloodCombined.450k$CellType == "WBC"]
sampleNames(RGsetTargets) <- paste(RGsetTargets$CellType,
                                seq_len(dim(RGsetTargets)[2]), sep = "_")
RGsetTargets

# Step 3: use your favorite package for deconvolution.
# Deconvolute a target data set consisting of 450K DNA methylation 
# data profiled in blood, using your prefered method.
# You can use our IDOL optimized DMR library for the Cord Blood,  This object
# contains a vector of length 517 consisting of the IDs of the IDOL optimized
# CpG probes.  These CpGs are used as the backbone for deconvolution and were
# selected because their methylation signature differs across the six normal 
# leukocyte subtypes plus the nucleated red blood cells.

data (IDOLOptimizedCpGsCordBlood)
head (IDOLOptimizedCpGsCordBlood)
# We recommend using Noob processMethod = "preprocessNoob" in minfi for the 
# target and reference datasets. 
# Cell types included are "CD8T", "CD4T", "NK", "Bcell", "Mono", "Gran", 
# "nRBC"
# To use the IDOL optimized list of CpGs (IDOLOptimizedCpGsCordBlood) use 
# estimateCellCounts2 from FlowSorted.Blood.EPIC. 
# Do not run with limited RAM the normalization step requires a big amount 
# of memory resources. Use the parameters as specified below for 
# reproducibility.

if (memory.limit()>8000){
    countsUCB<-estimateCellCounts2(RGsetTargets, 
                                    compositeCellType = 
                                                "CordBloodCombined", 
                                    processMethod = "preprocessNoob",
                                    probeSelect = "IDOL", 
                                    cellTypes = c("CD8T", "CD4T", "NK",  
                                    "Bcell", "Mono", "Gran", "nRBC"), 
                                    referencePlatform = 
                                        "IlluminaHumanMethylation450k",
                                    referenceset = 
                                        "FlowSorted.CordBloodCombined.450k",
                                    IDOLOptimizedCpGs =
                                        IDOLOptimizedCpGsCordBlood, 
                                    returnAll = FALSE)
    
    head(countsUCB$counts)
}