# zhilizheng/sbayesrc architecture

> Architecture diagram of the GitHub repository zhilizheng/sbayesrc, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/zhilizheng/sbayesrc
- Repository: https://github.com/zhilizheng/sbayesrc

## Overview

SBayesRC is an R package for annotation-aware polygenic prediction from GWAS summary statistics and block-LD references. Its user workflow optionally tidies and imputes summary data, runs the SBayesRC inference model to estimate SNP effects and functional architecture, and can calculate PRS or tune multi-ancestry PRS weights. LD preparation is a separate capability. The README documents these stages; sampled C++ shows the model, annotation, and LD internals, while the R workflow implementations were not sampled, so their detailed wiring is represented only where documented.

Main entry points: [`R/sbr_eigen.r`](https://github.com/zhilizheng/sbayesrc/blob/main/R/sbr_eigen.r) (SBayesRC runner), [`src/sbr_eigen_c.cpp`](https://github.com/zhilizheng/sbayesrc/blob/main/src/sbr_eigen_c.cpp) (Rcpp interface), [`R/tidy_ma.R`](https://github.com/zhilizheng/sbayesrc/blob/main/R/tidy_ma.R) (Tidy summary), [`R/imp_eig.r`](https://github.com/zhilizheng/sbayesrc/blob/main/R/imp_eig.r) (Impute SNPs)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_prep["Summary preparation"]
  node_tidy["Tidy summary<br/>[tidy_ma.R]"]
  node_impute["Impute SNPs<br/>[imp_eig.r]"]
end

subgraph group_model["Inference model"]
  node_sbayesrc["SBayesRC runner<br/>[sbr_eigen.r]"]
  node_bridge["Rcpp interface<br/>[sbr_eigen_c.cpp]"]
  node_sampler["MCMC sampler<br/>[SBayesRC.cpp]"]
  node_annotationmodel["Annotation model<br/>[AnnoProb.cpp]"]
end

subgraph group_scores["Risk scoring"]
  node_prs["PRS calculator<br/>[prs.R]"]
  node_multi["Multi-ancestry tuner<br/>[multi.R]"]
end

subgraph group_ld["LD support"]
  node_ldblocks["Block LD reader<br/>[BlockLDeig.cpp]"]
  node_ldprep["Cut LD blocks<br/>[cutLDs.R]"]
  node_ldcutter["LD cutter<br/>[cutLD.cpp]"]
end

node_user(("Analyst"))
node_gwas["GWAS summary"]
node_ldref["Block LD reference"]
node_annotations["Functional annotations"]
node_summarydata["Prepared summary"]
node_weights["SNP weights"]
node_architecture["Architecture estimates"]
node_genotypes["Genotypes"]
node_scoresout["PRS scores"]
node_tuningdata["Tuning IDs and phenotype"]
node_weightedprs["Weighted PRS"]

node_user -->|"optionally tidies"| node_tidy
node_gwas -->|"reads summary"| node_tidy
node_ldref -->|"matches SNPs"| node_tidy
node_tidy -->|"writes cleaned data"| node_summarydata
node_user -->|"optionally imputes"| node_impute
node_summarydata -->|"reads summary"| node_impute
node_ldref -->|"supplies LD"| node_impute
node_impute -->|"writes imputed data"| node_summarydata
node_user -->|"runs model"| node_sbayesrc
node_summarydata -->|"supplies summary"| node_sbayesrc
node_ldref -->|"supplies LD"| node_sbayesrc
node_annotations -->|"supplies annotations"| node_sbayesrc
node_sbayesrc -->|"calls native model"| node_bridge
node_bridge -->|"constructs and runs"| node_sampler
node_sampler -->|"reads block LD"| node_ldblocks
node_sampler -->|"samples annotation effects"| node_annotationmodel
node_bridge -->|"returns effects"| node_weights
node_bridge -->|"returns estimates"| node_architecture
node_user -->|"calculates scores"| node_prs
node_weights -->|"provides SNP effects"| node_prs
node_genotypes -->|"provides genotypes"| node_prs
node_prs -->|"writes scores"| node_scoresout
node_user -->|"tunes ancestries"| node_multi
node_scoresout -->|"provides ancestry scores"| node_multi
node_tuningdata -->|"provides tuning data"| node_multi
node_multi -->|"writes combined score"| node_weightedprs
node_user -->|"prepares LD"| node_ldprep
node_ldref -->|"reads LD blocks"| node_ldprep
node_ldprep -->|"calls native cutter"| node_ldcutter
node_ldcutter -->|"writes eigen blocks"| node_ldref

click node_tidy "https://github.com/zhilizheng/sbayesrc/blob/main/R/tidy_ma.R"
click node_impute "https://github.com/zhilizheng/sbayesrc/blob/main/R/imp_eig.r"
click node_sbayesrc "https://github.com/zhilizheng/sbayesrc/blob/main/R/sbr_eigen.r"
click node_bridge "https://github.com/zhilizheng/sbayesrc/blob/main/src/sbr_eigen_c.cpp"
click node_sampler "https://github.com/zhilizheng/sbayesrc/blob/main/src/SBayesRC.cpp"
click node_annotationmodel "https://github.com/zhilizheng/sbayesrc/blob/main/src/AnnoProb.cpp"
click node_ldblocks "https://github.com/zhilizheng/sbayesrc/blob/main/src/BlockLDeig.cpp"
click node_prs "https://github.com/zhilizheng/sbayesrc/blob/main/R/prs.R"
click node_multi "https://github.com/zhilizheng/sbayesrc/blob/main/R/multi.R"
click node_ldprep "https://github.com/zhilizheng/sbayesrc/blob/main/R/cutLDs.R"
click node_ldcutter "https://github.com/zhilizheng/sbayesrc/blob/main/src/cutLD.cpp"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_tidy,node_impute toneBlue
class node_sbayesrc,node_bridge,node_sampler,node_annotationmodel toneAmber
class node_prs,node_multi toneMint
class node_ldblocks,node_ldprep,node_ldcutter toneRose
class node_user,node_gwas,node_ldref,node_annotations,node_summarydata,node_weights,node_architecture,node_genotypes,node_scoresout,node_tuningdata,node_weightedprs toneIndigo
```

## Components

### Summary preparation

- [Tidy summary](https://github.com/zhilizheng/sbayesrc/blob/main/R/tidy_ma.R): `R/tidy_ma.R`
- [Impute SNPs](https://github.com/zhilizheng/sbayesrc/blob/main/R/imp_eig.r): `R/imp_eig.r`

### Inference model

- [SBayesRC runner](https://github.com/zhilizheng/sbayesrc/blob/main/R/sbr_eigen.r): `R/sbr_eigen.r`
- [Rcpp interface](https://github.com/zhilizheng/sbayesrc/blob/main/src/sbr_eigen_c.cpp): `src/sbr_eigen_c.cpp`
- [MCMC sampler](https://github.com/zhilizheng/sbayesrc/blob/main/src/SBayesRC.cpp): `src/SBayesRC.cpp`
- [Annotation model](https://github.com/zhilizheng/sbayesrc/blob/main/src/AnnoProb.cpp): `src/AnnoProb.cpp`

### Risk scoring

- [PRS calculator](https://github.com/zhilizheng/sbayesrc/blob/main/R/prs.R): `R/prs.R`
- [Multi-ancestry tuner](https://github.com/zhilizheng/sbayesrc/blob/main/R/multi.R): `R/multi.R`

### LD support

- [Block LD reader](https://github.com/zhilizheng/sbayesrc/blob/main/src/BlockLDeig.cpp): `src/BlockLDeig.cpp`
- [Cut LD blocks](https://github.com/zhilizheng/sbayesrc/blob/main/R/cutLDs.R): `R/cutLDs.R`
- [LD cutter](https://github.com/zhilizheng/sbayesrc/blob/main/src/cutLD.cpp): `src/cutLD.cpp`

### Other components

- **Analyst**
- **GWAS summary**
- **Block LD reference**
- **Functional annotations**
- **Prepared summary**
- **SNP weights**
- **Architecture estimates**
- **Genotypes**
- **PRS scores**
- **Tuning IDs and phenotype**
- **Weighted PRS**

## Connections

- Analyst → Tidy summary: optionally tidies
- GWAS summary → Tidy summary: reads summary
- Block LD reference → Tidy summary: matches SNPs
- Tidy summary → Prepared summary: writes cleaned data
- Analyst → Impute SNPs: optionally imputes
- Prepared summary → Impute SNPs: reads summary
- Block LD reference → Impute SNPs: supplies LD
- Impute SNPs → Prepared summary: writes imputed data
- Analyst → SBayesRC runner: runs model
- Prepared summary → SBayesRC runner: supplies summary
- Block LD reference → SBayesRC runner: supplies LD
- Functional annotations → SBayesRC runner: supplies annotations
- SBayesRC runner → Rcpp interface: calls native model
- Rcpp interface → MCMC sampler: constructs and runs
- MCMC sampler → Block LD reader: reads block LD
- MCMC sampler → Annotation model: samples annotation effects
- Rcpp interface → SNP weights: returns effects
- Rcpp interface → Architecture estimates: returns estimates
- Analyst → PRS calculator: calculates scores
- SNP weights → PRS calculator: provides SNP effects
- Genotypes → PRS calculator: provides genotypes
- PRS calculator → PRS scores: writes scores
- Analyst → Multi-ancestry tuner: tunes ancestries
- PRS scores → Multi-ancestry tuner: provides ancestry scores
- Tuning IDs and phenotype → Multi-ancestry tuner: provides tuning data
- Multi-ancestry tuner → Weighted PRS: writes combined score
- Analyst → Cut LD blocks: prepares LD
- Block LD reference → Cut LD blocks: reads LD blocks
- Cut LD blocks → LD cutter: calls native cutter
- LD cutter → Block LD reference: writes eigen blocks
