---
title: "MethScope-MRMP"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{MethScope-MRMP}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

## Generate an MRMP reference

MethScope includes pre-defined MRMP references, and you can also build your own from a methylation atlas.

### Prerequisites

1. Install **YAME** for `.cg` processing: <https://zhou-lab.github.io/YAME/>
2. Prepare a reference methylation `.cg` file (for example, an atlas or merged reference panel)
3. Install and load `MethScope`

```{r, eval=FALSE}
library(MethScope)
```

### Step 1: Binarize the reference `.cg` file

Convert methylation proportions into binary strings. The default threshold is 0.5; adjust with `-b` if needed.

```{bash, eval=FALSE}
yame rowop example.cg -o binstring > example_binstring
```

### Step 2: Build MRMP pattern definitions

Use `GenerateReference()` to identify recurrent patterns.  
`min_CG` controls the minimum CpGs per MRMP (default: `50`).

```{r, eval=FALSE}
GenerateReference(binary_file = "example_binstring", min_CG = 50)
```

This step generates a `patterns.txt` file.

### Step 3: Pack patterns into a `.cm` reference

Pack `patterns.txt` into a `.cm` file that can be passed as `reference_pattern` in `GenerateInput()`.

```{bash, eval=FALSE}
yame pack -f s patterns.txt patterns.cm
```

## Build MRMP references from cell-type pseudobulks

If cell-type labels are available, pseudobulked profiles can produce robust cell-type-informed MRMP references.

### Step 1: Create pseudobulk profiles per cell type

Prepare a text file (`samples.txt`) containing sample IDs for one cell type, then aggregate with `musum`.

```{bash, eval=FALSE}
yame subset -l samples.txt example.cg | yame rowop -o musum - example_pseudobulk.cg
```

### Step 2: Repeat per cell type and build MRMPs

Run the same workflow above (`binstring -> GenerateReference -> yame pack`) on each pseudobulk or combined pseudobulk panel.

For running a full parallelized multi-cell-type script, please refer to:
<https://github.com/zhou-lab/MethScope/blob/main/analysis/GenerateReference.sh>

After generating `patterns.cm`, use it as the custom MRMP reference in downstream MethScope analyses.
