## ----include = FALSE----------------------------------------------------------
options(device = "png")
knitr::opts_chunk$set(
  fig.ext = "png",
  collapse = TRUE,
  comment = "#>"
)

## ----setup, warning=FALSE, message=FALSE--------------------------------------
library(MethScope)

## ----eval=FALSE---------------------------------------------------------------
# # Paths to the GitHub example .cg file and mouse brain .cm reference
# example_file <- "inst/extdata/example.cg"
# reference_pattern <- "inst/extdata/mm10_Liu2021.cm"
# input_pattern <- GenerateInput(example_file, reference_pattern)

## ----eval=FALSE---------------------------------------------------------------
# model <- Liu2021_MouseBrain_P1000()
# prediction_result <- PredictCellType(model, input_pattern)

## ----eval=FALSE---------------------------------------------------------------
# example_label <- read.csv("inst/extdata/example_label.csv")
# cell_type_label <- example_label$label
# stopifnot(length(cell_type_label) == nrow(input_pattern))

## ----eval=FALSE---------------------------------------------------------------
# trained_model <- Input_training(
#   input_pattern,
#   cell_type_label
# )

## ----eval=FALSE---------------------------------------------------------------
# trained_model_cv <- Input_training(
#   input_pattern,
#   cell_type_label,
#   cross_validation = TRUE
# )

## ----eval=FALSE---------------------------------------------------------------
# umap_plot <- PlotUMAP(input_pattern, prediction_result)
# 
# # cell_type_label is the true label vector from inst/extdata/example_label.csv
# # The labels follow the .cg.idx sample order, so keep the .idx file next to the .cg file.
# PlotConfusion(prediction_result, cell_type_label)
# PlotF1(prediction_result, cell_type_label)

## ----eval=FALSE---------------------------------------------------------------
# reference_pattern <- "mm10_Liu2021.cm"
# reference_input <- readRDS("inst/extdata/mm10_Liu2021_ref.rds")
# 
# # Imputation is needed before deconvolution if input_pattern contains missing values.
# input_pattern <- imputeRowMean(input_pattern)
# 
# cell_proportion <- nnls_deconv(reference_input, input_pattern)

## ----eval=FALSE---------------------------------------------------------------
# library(Seurat)
# Pattern.obj <- CreateSeuratObject(counts = t(input_pattern), assay = "DNAm")
# VariableFeatures(Pattern.obj) <- rownames(Pattern.obj[['DNAm']])
# DefaultAssay(Pattern.obj) <- "DNAm"
# Pattern.obj <- NormalizeData(Pattern.obj, assay = "DNAm", verbose = FALSE)
# Pattern.obj <- ScaleData(Pattern.obj, assay = "DNAm", verbose = FALSE)
# # You can also directly use the initial counts matrix
# Pattern.obj@assays$DNAm@layers$scale.data <- as.matrix(Pattern.obj@assays$DNAm@layers$counts)
# Pattern.obj <- RunPCA(Pattern.obj, assay = "DNAm", reduction.name = "mpca", verbose = FALSE)
# Pattern.obj <- FindNeighbors(Pattern.obj, reduction = "mpca", dims = 1:30)
# Pattern.obj <- FindClusters(Pattern.obj, verbose = FALSE, resolution = 0.7)
# Pattern.obj <- RunUMAP(Pattern.obj, reduction = "mpca", reduction.name = "meth.umap", dims = 1:30)

## ----eval=FALSE---------------------------------------------------------------
# x <- read.table("input_pattern.txt", header = FALSE)
# 
# y <- read.table("example_0.txt", header = FALSE)
# y <- t(y)  # samples x CpGs

## ----eval=FALSE---------------------------------------------------------------
# import torch
# import torch.nn as nn
# 
# class BottleneckDecoder(nn.Module):
#     def __init__(self, input_dim=num_of_patterns, hidden_dim=512, output_dim=10000):
#         super().__init__()
#         self.net = nn.Sequential(
#             nn.Linear(input_dim, hidden_dim),
#             nn.ReLU(),
#             nn.BatchNorm1d(hidden_dim),
#             nn.Linear(hidden_dim, output_dim)
#         )
#     def forward(self, x):
#         return self.net(x)

