PolyGeniusModel

PolyGeniusModel R6 class representing a single PRS model

Description

PolyGeniusModel is an R6 class for working with Polygenic Risk Score (PRS) models. It stores a tibble of variants alongside package- and user-defined metadata (e.g., GWAS metadata, generation logs, liftover logs).

Methods that make PolyGeniusModel behave like a data frame/tibble: subsetting ([), field/column access ($), and tab completion.

Subset the variants tibble by rows (i) and/or columns (j). A deep clone is made; the original model is unchanged.

If name matches a column in variants, returns that column; otherwise returns the public field/method with that name, or NULL if absent.

Returns candidate names from both the variants columns and public fields/methods.

Methods apply verbs to variants of a deep-cloned model and return the modified model (except summarise(), which returns the summarised tibble).

Lightweight wrappers that delegate to the underlying variants tibble.

Usage

PolyGeniusModel(variants, name, build, gwas = list(), ...)

readPolyGeniusModel(path, sep = "\t")

## S3 method for class 'PolyGeniusModel'
x[i, j, drop = FALSE]

## S3 method for class 'PolyGeniusModel'
x$name

## S3 method for class 'PolyGeniusModel'
.DollarNames(x, pattern = "")

## S3 method for class 'PolyGeniusModel'
filter(.data, ...)

## S3 method for class 'PolyGeniusModel'
select(.data, ...)

## S3 method for class 'PolyGeniusModel'
mutate(.data, ...)

## S3 method for class 'PolyGeniusModel'
arrange(.data, ...)

## S3 method for class 'PolyGeniusModel'
slice(.data, ...)

## S3 method for class 'PolyGeniusModel'
rename(.data, ...)

## S3 method for class 'PolyGeniusModel'
transmute(.data, ...)

## S3 method for class 'PolyGeniusModel'
group_by(
  .data,
  ...,
  .add = FALSE,
  .drop = dplyr::group_by_drop_default(.data$variants)
)

## S3 method for class 'PolyGeniusModel'
ungroup(.data, ...)

## S3 method for class 'PolyGeniusModel'
summarise(.data, ...)

nrow.PolyGeniusModel(x)

ncol.PolyGeniusModel(x)

## S3 method for class 'PolyGeniusModel'
dim(x)

## S3 method for class 'PolyGeniusModel'
length(x)

rownames.PolyGeniusModel(x)

colnames.PolyGeniusModel(x)

## S3 method for class 'PolyGeniusModel'
dimnames(x)

Arguments

variants

A data.frame or tibble of variant rows. Must include columns chr, position, ea, nea, beta.

name

Element to extract.

build

Genome build label (character).

gwas

Optional named list of GWAS metadata (default list()).

Additional named fields to attach to the model (stored on self).

path

Path written by ⁠$save()⁠.

sep

Field separator used in the file (default ).

x

A PolyGeniusModel object.

i

Optional row index (integer/logical/character).

j

Optional column index (integer/logical/character).

drop

Logical; if TRUE, may simplify to a vector when selecting a single column.

pattern

Optional regex to filter names (default ““ for all).

Details

The object is designed to feel like a tibble for its variants field: you can subset rows/columns with [; access variant columns via $; and apply dplyr-like verbs that operate on a deep-cloned model so the original remains unchanged.

Fields

  • variants (tibble): Must include required columns chr, position, ea, nea, beta.

  • name (character): Model identifier.

  • build (character): Genome build.

  • Any other user-defined fields (lists, data frames, vectors, etc.).

Key methods

  • ⁠$append(field, key = NULL, value)⁠: Append/set metadata in a list-like field.

  • ⁠$save(path, sep = “\t”)⁠: Write a header with JSON-encoded metadata plus a TSV of variants.

  • ⁠$print()⁠: Rich, human-readable summary (via cli).

Value

A new PolyGeniusModel instance.

A PolyGeniusModel instance reconstructed from file.

A new PolyGeniusModel with subset variants.

A vector (variant column), a field/method, or NULL.

Character vector of matching names.

For summarise(), returns a tibble (not a PolyGeniusModel).

I/O

  • PolyGeniusModel() (constructor) creates a new model instance.

  • readPolyGeniusModel() loads a model saved by ⁠$save()⁠.

Operators and helpers

  • [ subsets the variants table (returns a new model).

  • $ returns variant columns or model fields.

  • .DollarNames supports tab-completion for both fields and columns.

dplyr verbs

  • filter(), select(), mutate(), arrange(), slice(), rename(), transmute()

  • group_by(), ungroup(), summarise() (summarise returns a tibble)

Matrix-like operations

  • nrow(), ncol(), dim(), length(), rownames(), colnames(), dimnames()

Public fields

variants

Tibble of variants; must have chr, position, ea, nea, beta

name

Model identifier

build

Genome build

Methods

Public methods


Method new()

Initialize a new PolyGeniusModel

Usage
PolyGeniusModel.class$new(variants, name, build, gwas = list(), ...)
Arguments
variants

A data.frame/tibble of variant rows. Must include columns chr, position, ea, nea, beta.

name

Character model identifier.

build

Genome build label (character).

gwas

Optional named list of GWAS metadata (default list()).

Additional named fields to attach to the model (stored on self).


Method append()

Append or set an entry in a list-like field on the model

Usage
PolyGeniusModel.class$append(field, key = NULL, value)
Arguments
field

Field name on self.

key

Optional element name inside the list field; if NULL, replaces the field.

value

Value to assign.

Returns

The model (invisibly).


Method save()

Save model to a TSV with JSON metadata header

Usage
PolyGeniusModel.class$save(path, sep = "\t")
Arguments
path

Path to write.

sep

Field separator for variants TSV (default ).

Returns

path (invisibly).


Method print()

Pretty print

Usage
PolyGeniusModel.class$print(...)
Arguments

Arguments passed to tibble printing (e.g., n, width, etc.).


Method clone()

The objects of this class are cloneable with this method.

Usage
PolyGeniusModel.class$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples

## Not run: 
library(tibble)
variants <- tibble::tibble(
  chr = c("1","1","2"),
  position = c(101, 202, 303),
  ea = c("A","G","T"),
  nea = c("G","A","C"),
  beta = c(0.1, -0.2, 0.05)
)

m <- PolyGeniusModel(variants, name = "toy", build = "GRCh38")
m$append("gwas", "study", "Test GWAS")
m

# dplyr-like verbs:
m2 <- filter(m, beta > 0)
nrow(m2)       # 2

# Subset first row and two columns:
m3 <- m[1, c("chr","position")]

# Save & reload:
tf <- tempfile(fileext = ".pgm.tsv")
m$save(tf)
m4 <- readPolyGeniusModel(tf)

## End(Not run)

## Not run: 
m2 <- m[1:10, c("chr","position")]

## End(Not run)
## Not run: 
ids <- m$id       # variant column
nm  <- m$name     # field

## End(Not run)
## Not run: 
.DollarNames.PolyGeniusModel(m, "^pos")

## End(Not run)