# tessamacnish/haplovar architecture

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

- Interactive diagram: https://gitdiagram.com/tessamacnish/haplovar
- Repository: https://github.com/tessamacnish/haplovar

## Overview

#### Purpose and runtime

HaploVar is an R package for converting SNP genotype data plus linkage disequilibrium (LD) information into local haplotypes and haplotype-variant encodings for GWAS or genomic-selection pipelines. It runs in R as a library package; package exports are defined in `NAMESPACE`, with package metadata and dependencies in `DESCRIPTION`.

#### Input boundary

The pipeline consumes an in-memory VCF-like genotype object and an LD matrix. Example packaged inputs are `data/vcf.rda` and `data/LD.rda`; input structure/access is represented by `R/vcf.R` and `R/LD.R`. VCF and LD generation/import are external concerns: the README identifies `crosshap` for reading data and PLINK or `snpStats` for LD calculation.

#### Local haplotype definition

`R/define_haplotypes.R` is the primary local-haplotype entry point. It groups SNPs into haplotypes using the LD matrix and an `epsilon` threshold. `R/collate_define_haplotypes.R` is the consolidation stage that assembles or normalizes haplotype definitions into output tables suitable for downstream variant construction.

#### Global haplotype definition

`R/define_haplotypes_globally.R` provides an alternate global-definition path, likely applying haplotype logic across the full data scope rather than the standard local workflow. Its result remains a haplotype grouping boundary before genotype/variant encoding.

#### Haplotype variant construction

`R/haplotype_variants.R` is the main end-to-end variant-generation entry point: it takes VCF, LD, and haplotype parameters, derives haplotype states, and formats them for downstream analysis. `R/collate_haplotype_variants.R` performs the assembly/collation stage between haplotype calls and final encoded output.

#### Global variant path

`R/haplotype_variants_global.R` parallels the standard variant workflow for globally defined haplotypes. Together with `R/define_haplotypes_globally.R`, it forms a separate control path for callers needing global rather than local haplotype treatment.

#### Output boundary and consumers

The principal output is a sample-by-haplotype-variant representation. Format selection in `R/haplotype_variants.R` supports genomic-selection-oriented output (README example: format 3, saved as CSV) and GWAS-oriented VCF-like output (format 6, tab-delimited). HaploVar itself does not run association or prediction models; those are downstream external pipelines.

Main entry points: [`R/vcf.R`](https://github.com/tessamacnish/haplovar/blob/main/R/vcf.R) (VCF-like genotypes), [`R/LD.R`](https://github.com/tessamacnish/haplovar/blob/main/R/LD.R) (LD matrix), [`R/haplotype_variants.R`](https://github.com/tessamacnish/haplovar/blob/main/R/haplotype_variants.R) (Generate haplotype variants), [`R/haplotype_variants_global.R`](https://github.com/tessamacnish/haplovar/blob/main/R/haplotype_variants_global.R) (Generate global variants)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_package["R Package"]
  node_namespace["Exports<br/>R namespace"]
  node_description["Package metadata"]
end

subgraph group_inputs["Input Boundary"]
  node_external_import{{"External genotype / LD tools<br/>external tooling"}}
  node_vcf_data["VCF-like genotypes<br/>in-memory input<br/>[vcf.R]"]
  node_ld_matrix["LD matrix<br/>in-memory input<br/>[LD.R]"]
  node_example_vcf[("Example genotypes<br/>packaged data<br/>[vcf.rda]")]
  node_example_ld[("Example LD<br/>packaged data<br/>[LD.rda]")]
end

subgraph group_local["Local Haplotype Path"]
  node_define_local["Define local haplotypes<br/>haplotype grouping"]
  node_collate_definitions["Collate haplotype definitions<br/>definition assembly"]
  node_build_variants["Generate haplotype variants<br/>variant workflow"]
  node_collate_variants["Collate variant encoding<br/>output assembly"]
end

subgraph group_global["Global Haplotype Path"]
  node_define_global["Define global haplotypes<br/>global haplotype grouping"]
  node_build_global_variants["Generate global variants<br/>global variant workflow"]
end

subgraph group_outputs["Output &amp; Consumers"]
  node_variant_output["Sample × haplotype variants<br/>encoded output"]
  node_downstream{{"GWAS / genomic selection<br/>external consumers"}}
end

node_namespace -->|"exports"| node_define_local
node_namespace -->|"exports"| node_build_variants
node_namespace -->|"exports"| node_define_global
node_namespace -->|"exports"| node_build_global_variants
node_external_import -.->|"supplies"| node_vcf_data
node_external_import -.->|"calculates"| node_ld_matrix
node_example_vcf -.->|"example"| node_vcf_data
node_example_ld -.->|"example"| node_ld_matrix
node_vcf_data -->|"genotypes"| node_define_local
node_ld_matrix -->|"LD + epsilon"| node_define_local
node_define_local -->|"haplotype calls"| node_collate_definitions
node_vcf_data -->|"genotypes"| node_build_variants
node_ld_matrix -->|"LD"| node_build_variants
node_collate_definitions -->|"definitions"| node_build_variants
node_build_variants -->|"haplotype states"| node_collate_variants
node_collate_variants -->|"encoded variants"| node_variant_output
node_vcf_data -->|"genotypes"| node_define_global
node_ld_matrix -->|"LD"| node_define_global
node_define_global -->|"global definitions"| node_build_global_variants
node_vcf_data -->|"genotypes"| node_build_global_variants
node_ld_matrix -->|"LD"| node_build_global_variants
node_build_global_variants -->|"global encoding"| node_variant_output
node_variant_output -->|"consumed by"| node_downstream

click node_namespace "https://github.com/tessamacnish/haplovar/blob/main/NAMESPACE"
click node_description "https://github.com/tessamacnish/haplovar/blob/main/DESCRIPTION"
click node_vcf_data "https://github.com/tessamacnish/haplovar/blob/main/R/vcf.R"
click node_ld_matrix "https://github.com/tessamacnish/haplovar/blob/main/R/LD.R"
click node_example_vcf "https://github.com/tessamacnish/haplovar/blob/main/data/vcf.rda"
click node_example_ld "https://github.com/tessamacnish/haplovar/blob/main/data/LD.rda"
click node_define_local "https://github.com/tessamacnish/haplovar/blob/main/R/define_haplotypes.R"
click node_collate_definitions "https://github.com/tessamacnish/haplovar/blob/main/R/collate_define_haplotypes.R"
click node_build_variants "https://github.com/tessamacnish/haplovar/blob/main/R/haplotype_variants.R"
click node_collate_variants "https://github.com/tessamacnish/haplovar/blob/main/R/collate_haplotype_variants.R"
click node_define_global "https://github.com/tessamacnish/haplovar/blob/main/R/define_haplotypes_globally.R"
click node_build_global_variants "https://github.com/tessamacnish/haplovar/blob/main/R/haplotype_variants_global.R"

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_namespace,node_description toneBlue
class node_external_import,node_vcf_data,node_ld_matrix,node_example_vcf,node_example_ld toneAmber
class node_define_local,node_collate_definitions,node_build_variants,node_collate_variants toneMint
class node_define_global,node_build_global_variants toneRose
class node_variant_output,node_downstream toneIndigo
```

## Components

### R Package

- [Exports](https://github.com/tessamacnish/haplovar/blob/main/NAMESPACE): R namespace · `NAMESPACE`
- [Package metadata](https://github.com/tessamacnish/haplovar/blob/main/DESCRIPTION): R package metadata · `DESCRIPTION`

### Input Boundary

- **External genotype / LD tools**: external tooling · crosshap imports genotypes; PLINK or snpStats calculate LD.
- [VCF-like genotypes](https://github.com/tessamacnish/haplovar/blob/main/R/vcf.R): in-memory input · `R/vcf.R`
- [LD matrix](https://github.com/tessamacnish/haplovar/blob/main/R/LD.R): in-memory input · `R/LD.R`
- [Example genotypes](https://github.com/tessamacnish/haplovar/blob/main/data/vcf.rda): packaged data · `data/vcf.rda`
- [Example LD](https://github.com/tessamacnish/haplovar/blob/main/data/LD.rda): packaged data · `data/LD.rda`

### Local Haplotype Path

- [Define local haplotypes](https://github.com/tessamacnish/haplovar/blob/main/R/define_haplotypes.R): haplotype grouping · Groups SNPs using LD and epsilon. · `R/define_haplotypes.R`
- [Collate haplotype definitions](https://github.com/tessamacnish/haplovar/blob/main/R/collate_define_haplotypes.R): definition assembly · `R/collate_define_haplotypes.R`
- [Generate haplotype variants](https://github.com/tessamacnish/haplovar/blob/main/R/haplotype_variants.R): variant workflow · Main end-to-end local variant entry point. · `R/haplotype_variants.R`
- [Collate variant encoding](https://github.com/tessamacnish/haplovar/blob/main/R/collate_haplotype_variants.R): output assembly · `R/collate_haplotype_variants.R`

### Global Haplotype Path

- [Define global haplotypes](https://github.com/tessamacnish/haplovar/blob/main/R/define_haplotypes_globally.R): global haplotype grouping · `R/define_haplotypes_globally.R`
- [Generate global variants](https://github.com/tessamacnish/haplovar/blob/main/R/haplotype_variants_global.R): global variant workflow · `R/haplotype_variants_global.R`

### Output & Consumers

- **Sample × haplotype variants**: encoded output · Supports genomic-selection CSV and GWAS-oriented VCF-like tabular output.
- **GWAS / genomic selection**: external consumers · Association and prediction models run outside HaploVar.

## Connections

- Exports → Define local haplotypes: exports
- Exports → Generate haplotype variants: exports
- Exports → Define global haplotypes: exports
- Exports → Generate global variants: exports
- External genotype / LD tools → VCF-like genotypes: supplies
- External genotype / LD tools → LD matrix: calculates
- Example genotypes → VCF-like genotypes: example
- Example LD → LD matrix: example
- VCF-like genotypes → Define local haplotypes: genotypes
- LD matrix → Define local haplotypes: LD + epsilon
- Define local haplotypes → Collate haplotype definitions: haplotype calls
- VCF-like genotypes → Generate haplotype variants: genotypes
- LD matrix → Generate haplotype variants: LD
- Collate haplotype definitions → Generate haplotype variants: definitions
- Generate haplotype variants → Collate variant encoding: haplotype states
- Collate variant encoding → Sample × haplotype variants: encoded variants
- VCF-like genotypes → Define global haplotypes: genotypes
- LD matrix → Define global haplotypes: LD
- Define global haplotypes → Generate global variants: global definitions
- VCF-like genotypes → Generate global variants: genotypes
- LD matrix → Generate global variants: LD
- Generate global variants → Sample × haplotype variants: global encoding
- Sample × haplotype variants → GWAS / genomic selection: consumed by
