Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,11 @@ export(getGenome)
export(getGenotypeCovariates)
export(getGenotypes)
export(getH2)
export(getH2Se)
export(getImissCutoff)
export(getInSample)
export(getIntercept)
export(getInterceptSe)
export(getKeepIndel)
export(getKeepVariants)
export(getLbf)
Expand All @@ -153,6 +156,7 @@ export(getMethodNames)
export(getMixtureWeights)
export(getN)
export(getNRef)
export(getNSnps)
export(getP)
export(getPhenotypeCovariates)
export(getPhenotypes)
Expand Down Expand Up @@ -181,6 +185,7 @@ export(getSusieFit)
export(getSusieResult)
export(getTauBlocks)
export(getTopLoci)
export(getTraitName)
export(getTraitNames)
export(getTraitPosition)
export(getTraitRun)
Expand Down Expand Up @@ -365,8 +370,11 @@ exportMethods(getGenome)
exportMethods(getGenotypeCovariates)
exportMethods(getGenotypes)
exportMethods(getH2)
exportMethods(getH2Se)
exportMethods(getImissCutoff)
exportMethods(getInSample)
exportMethods(getIntercept)
exportMethods(getInterceptSe)
exportMethods(getKeepIndel)
exportMethods(getKeepVariants)
exportMethods(getLbf)
Expand All @@ -384,6 +392,7 @@ exportMethods(getMethodNames)
exportMethods(getMixtureWeights)
exportMethods(getN)
exportMethods(getNRef)
exportMethods(getNSnps)
exportMethods(getP)
exportMethods(getPhenotypeCovariates)
exportMethods(getPhenotypes)
Expand All @@ -402,7 +411,6 @@ exportMethods(getScoreStats)
exportMethods(getSe)
exportMethods(getSignificantQtls)
exportMethods(getSnpIdx)
exportMethods(getSnpInfo)
exportMethods(getStandardized)
exportMethods(getStudy)
exportMethods(getSumStats)
Expand All @@ -411,6 +419,7 @@ exportMethods(getSusieAlpha)
exportMethods(getSusieFit)
exportMethods(getTauBlocks)
exportMethods(getTopLoci)
exportMethods(getTraitName)
exportMethods(getTraitNames)
exportMethods(getTraitPosition)
exportMethods(getTraitRun)
Expand Down Expand Up @@ -552,6 +561,7 @@ importFrom(purrr,
map_dfr,
map_int,
map_lgl,
partial,
reduce,
set_names
)
Expand Down
73 changes: 70 additions & 3 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -1162,9 +1162,14 @@ setGeneric("getFullFit", function(x, ...) standardGeneric("getFullFit"))
setGeneric("getCvFits", function(x, ...) standardGeneric("getCvFits"))

#' @title Get Method Names
#' @description Extract method names from a collection class.
#' @param x A \code{FineMappingResult} or \code{TwasWeights} object.
#' @description Extract the method name(s) an object was produced with: one
#' per entry for a collection, a single name for one estimate.
#' @param x A \code{FineMappingResult}, \code{TwasWeights},
#' \code{CtwasResult} or \code{H2Estimate} object.
#' @return Character vector.
#' @examples
#' data(h2EstimateExample)
#' getMethodNames(h2EstimateExample)
#' @export
setGeneric("getMethodNames", function(x) standardGeneric("getMethodNames"))

Expand Down Expand Up @@ -1441,9 +1446,16 @@ setGeneric("getScaleResiduals", function(x) {

#' @title Get SNP Info
#' @description Return the cached SNP metadata data.frame (columns: SNP, CHR,
#' BP, A1, A2, optionally MAF).
#' BP, A1, A2, fileIdx, optionally MAF).
#' @param x An object carrying cached SNP metadata.
#' @return A data.frame.
#' @details The genotype handle is the seed layer behind a panel and
#' is not part of the public interface; obtain a panel from
#' \code{readGenotypes()} and ask it directly. This table is the READ-PATH
#' view: unlike the panel's \code{rowRanges()} it carries \code{fileIdx},
#' the on-disk variant position that keeps reads correct after a handle has
#' been row-subset. Callers that only need chrom/pos/alleles should use
#' \code{rowRanges()} on the panel instead.
#' @keywords internal
setGeneric("getSnpInfo", function(x) standardGeneric("getSnpInfo"))

Expand Down Expand Up @@ -1708,6 +1720,61 @@ setGeneric("getTauBlocks", function(x) standardGeneric("getTauBlocks"))
#' @export
setGeneric("getH2", function(x) standardGeneric("getH2"))

#' @title Get Heritability Standard Error
#' @description Return the standard error of the global SNP heritability
#' estimate carried by an \code{H2Estimate}.
#' @param x An \code{H2Estimate}.
#' @return Numeric (length 1).
#' @examples
#' data(h2EstimateExample)
#' getH2Se(h2EstimateExample)
#' @export
setGeneric("getH2Se", function(x) standardGeneric("getH2Se"))

#' @title Get LD-Score Regression Intercept
#' @description Return the regression intercept carried by an
#' \code{H2Estimate}. Values above 1 indicate confounding or sample overlap
#' rather than polygenic signal.
#' @param x An \code{H2Estimate}.
#' @return Numeric (length 1).
#' @examples
#' data(h2EstimateExample)
#' getIntercept(h2EstimateExample)
#' @export
setGeneric("getIntercept", function(x) standardGeneric("getIntercept"))

#' @title Get Intercept Standard Error
#' @description Return the standard error of the regression intercept carried
#' by an \code{H2Estimate}.
#' @param x An \code{H2Estimate}.
#' @return Numeric (length 1).
#' @examples
#' data(h2EstimateExample)
#' getInterceptSe(h2EstimateExample)
#' @export
setGeneric("getInterceptSe", function(x) standardGeneric("getInterceptSe"))

#' @title Get Variant Count
#' @description Return the number of variants the heritability estimate was
#' computed over.
#' @param x An \code{H2Estimate}.
#' @return Integer (length 1).
#' @examples
#' data(h2EstimateExample)
#' getNSnps(h2EstimateExample)
#' @export
setGeneric("getNSnps", function(x) standardGeneric("getNSnps"))

#' @title Get Trait Name
#' @description Return the trait label carried by an \code{H2Estimate}.
#' @param x An \code{H2Estimate}.
#' @return Character (length 1).
#' @examples
#' data(h2EstimateExample)
#' getTraitName(h2EstimateExample)
#' @export
setGeneric("getTraitName", function(x) standardGeneric("getTraitName"))

# Internal generics for the unified joint-analysis engine (see R/JointGroup.R
# and dev/jointSpecification-s4-refactor.md). Not exported: the engine and its
# fitters are package-internal machinery.
Expand Down
2 changes: 1 addition & 1 deletion R/GenotypeHandle.R
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ GenotypeHandle <- function(
}

#' @rdname getSnpInfo
#' @export
#' @keywords internal
setMethod("getSnpInfo", "GenotypeHandle", function(x) x@snpInfo)

#' @rdname getFormat
Expand Down
24 changes: 24 additions & 0 deletions R/H2Estimate.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,30 @@ setClass(
#' @export
setMethod("getH2", "H2Estimate", function(x) x@h2)

#' @rdname getH2Se
#' @export
setMethod("getH2Se", "H2Estimate", function(x) x@h2Se)

#' @rdname getIntercept
#' @export
setMethod("getIntercept", "H2Estimate", function(x) x@intercept)

#' @rdname getInterceptSe
#' @export
setMethod("getInterceptSe", "H2Estimate", function(x) x@interceptSe)

#' @rdname getNSnps
#' @export
setMethod("getNSnps", "H2Estimate", function(x) x@nSnps)

#' @rdname getTraitName
#' @export
setMethod("getTraitName", "H2Estimate", function(x) x@traitName)

#' @rdname getMethodNames
#' @export
setMethod("getMethodNames", "H2Estimate", function(x) x@method)

#' @rdname getTauBlocks
#' @export
setMethod("getTauBlocks", "H2Estimate", function(x) x@tauBlocks)
Expand Down
59 changes: 46 additions & 13 deletions R/JointGroup.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ setClass(
setClass(
"IndividualJointGroup",
contains = "JointGroup",
representation(X = "matrix", Y = "matrix", pos = "numeric"),
representation(X = "matrix", Y = "matrix", traitPos = "numeric"),
validity = function(object) {
errors <- character()
if (nrow(object@X) != nrow(object@Y)) {
Expand All @@ -74,10 +74,16 @@ setClass(
if (ncol(object@Y) != nrow(object@conditions)) {
errors <- c(errors, "ncol(Y) must equal nrow(conditions)")
}
if (length(object@pos) > 0L && length(object@pos) != ncol(object@Y)) {
if (
length(object@traitPos) > 0L &&
length(object@traitPos) != ncol(object@Y)
) {
errors <- c(
errors,
"when set, 'pos' must have one entry per Y column"
str_c(
"when set, 'traitPos' must have one entry per TRAIT ",
"(Y column), not per variant"
)
)
}
if (length(errors) == 0L) TRUE else errors
Expand All @@ -88,21 +94,26 @@ setClass(
setClass(
"SumStatsJointGroup",
contains = "JointGroup",
representation(Z = "matrix", R = "matrix", N = "numeric"),
# The LD REFERENCE, not the LD matrix: R is variants x variants, and
# every group in an enumeration would hold its own dense copy long before
# any of them is fitted. The sketch is a lazy panel, so the matrix is
# derived once per group at fit time -- which is what the univariate RSS
# path in fineMappingPipeline already does.
representation(Z = "matrix", ldSketch = "LdSketchOrNULL", N = "numeric"),
validity = function(object) {
errors <- character()
if (nrow(object@R) != ncol(object@R)) {
errors <- c(errors, "'R' (LD) must be square")
if (ncol(object@Z) != nrow(object@conditions)) {
errors <- c(errors, "ncol(Z) must equal nrow(conditions)")
}
if (nrow(object@Z) != nrow(object@R)) {
if (is.null(rownames(object@Z))) {
errors <- c(
errors,
"'Z' rows (variants) must match the 'R' dimension"
str_c(
"'Z' must carry the variant ids as rownames: they are ",
"what the LD matrix is derived over"
)
)
}
if (ncol(object@Z) != nrow(object@conditions)) {
errors <- c(errors, "ncol(Z) must equal nrow(conditions)")
}
if (length(errors) == 0L) TRUE else errors
}
)
Expand Down Expand Up @@ -159,14 +170,36 @@ setClass("TwasJointPipeline", contains = "JointPipeline")
# @noRd
.jgY <- function(g) g@Y

# The genomic midpoint of each TRAIT, one per Y column -- fsusie's functional
# domain coordinate, not a variant position. Named apart from `pos`, which
# everywhere else in the package means the position of a variant.
# @noRd
.jgPos <- function(g) g@pos
.jgTraitPos <- function(g) g@traitPos

# @noRd
.jgZ <- function(g) g@Z

# @noRd
.jgR <- function(g) g@R
.jgLdSketch <- function(g) g@ldSketch

# The variants a summary-statistics group covers, in Z's row order -- which is
# the order the derived LD matrix comes back in.
# @noRd
.jgVariantIds <- function(g) rownames(.jgZ(g))

# The group's LD matrix, derived from its sketch.
#
# Callers must derive ONCE per group and pass the result down: the per-
# condition entry builder runs once per Z column, so calling this from there
# would recompute the same matrix for every condition.
# @noRd
.jgLdMatrix <- function(g) {
.ldFromSketch(
.jgLdSketch(g),
.jgVariantIds(g),
label = "jointEngine"
)
}

# @noRd
.jgN <- function(g) g@N
Expand Down
Loading