snipar.slmm module

class snipar.slmm.GradHessComponents(P_y, P_varcomp_mats)[source]

Bases: NamedTuple

P_varcomp_mats: Tuple[ndarray, ...]

Alias for field number 1

P_y: ndarray

Alias for field number 0

class snipar.slmm.LinearMixedModel(y: ndarray, varcomp_arr_lst: Tuple[Tuple[ndarray, ndarray, ndarray], ...], varcomps: Tuple[float, ...] | None = None, covar_X: ndarray | None = None, add_intercept: bool = False, add_jitter: bool = False)[source]

Bases: object

Wrapper of data and functions that compute estimates of variance components or SNP effects.

property P_attrs

Compute ingredients for gradient and hessian (Vinv_y, Vinv_varcomp_mats).

Returns:

GradHessComponents: a NameTuple holding the computation results

property V

Compute V.

Returns:

csc_matrix: V in sparse csc format

property V_logdet

Compute log determinant of V using LU.

Returns:

float: log determinant of V

property V_lu

Compute sparse LU factorization of V.

Returns:

SuperLU: wrapper object holding LU factorization of V

property Vinv_Z
property Vinv_e

Compute matrix-vector product of inverse of V and one-vector

Returns:

np.ndarray: Vinv_e

Vinv_mat(dense_mat: ndarray) ndarray[source]

Calculate matrix-matrix product of inverse of V and a dense matrix

Args:

dense_mat (np.ndarray): 2-d array in the dense format

Raises:

ValueError: dense_mat must be a 2-d array ValueError: dimensions of V and dense_mat must match

Returns:

np.ndarray: matrix product in the dense format

property Vinv_varcomp_mats

Compute matrix multiplications of inverse of V and all variance component matrices.

Returns:

Tuple[csc_matrix, …]: a tuple holding all Vinv_varcomp_mat

property Vinv_y

Compute matrix-vector product of inverse of V and y

Returns:

np.ndarray: Vinv_e

ai_reml() None[source]

Perform AI-REML algorithm to obtain maximum likelihood estimates of variance components.

dense_reml_loglik(method: typing_extensions.Literal[chol, cg] = 'cg') float[source]

Dense version of reml_loglik

Args:

method (Literal[‘chol’, ‘cg’], optional): a string specifying method for computing V inverse. Defaults to ‘cg’.

Raises:

RuntimeError: Conjugate gradient did not converge RuntimeError: Illigal input for conjugate gradient

Returns:

float: REML log likelihood

static fit_covar(y: ndarray, covar_X: ndarray) ndarray[source]
fit_snps_eff(gts: ndarray, fam_labels: ndarray, ignore_na_fams: bool = True, ignore_na_rows: bool = True) Tuple[ndarray, ndarray, ndarray][source]

Perform repeated OLS to estimate SNP effects and sampling variance-covariance.

Args:

gts (np.ndarray): 3-d array of genetic data.

Raises:

RuntimeError: should adjust for covariates if not yet. ValueError: gts should be 3-d.

Returns:

Tuple[np.ndarray, np.ndarray, np.ndarray]: 3 arrays of SNP effects, covarinaces and standard errors.

property grad

Compute gradient.

Returns:

np.ndarray: 1-d array of gradient

grid_search_reml() None[source]

Perform grid search on variance component parameters.

property hessian

Compute hessian.

Returns:

np.ndarray: 2-d n_varcomps-by-n_varcomps array

logger = <Logger snipar.slmm.LinearMixedModel (WARNING)>
property reml_loglik

Compute REML log likelihood

Returns:

float: REML log likelihood

scipy_optimize() None[source]

Perform LBFGS-B to optimize variance components.

static sp_solve_dense3d(sps_mat: csc_matrix, dense_mat: ndarray) ndarray[source]

Compute product of the inverse of given sparse matrix and given 3-d dense array; used for repeated OLS.

Args:

sps_mat (csc_matrix): 2-d sparse matrix. dense_mat (np.ndarray): 3-d dense array.

Raises:

ValueError: dense_mat should be 3-d. ValueError: 2nd dimensions of both inputs should match.

Returns:

np.ndarray: 3-d dense array.

static sp_solve_dense3d_lu(sps_mat: csc_matrix, dense_mat: ndarray) ndarray[source]

Compute product of the inverse of given sparse matrix and given 3-d dense array uisng LU; used for repeated OLS.

Args:

sps_mat (csc_matrix): 2-d sparse matrix. dense_mat (np.ndarray): 3-d dense array.

Raises:

ValueError: dense_mat should be 3-d. ValueError: 2nd dimensions of both inputs should match.

Returns:

np.ndarray: 3-d dense array.

test_grad_hessian() Tuple[float, ndarray, ndarray][source]

Calculate REML loglikelihood, grad and hessian in dense format for testing purposes.

Returns:

Tuple[float, np.ndarray, np.ndarray]: a tuple containing the three results

property varcomps: Tuple[float, ...]

Return optimized variance components.

Raises:

ValueError: self.optimized should be True

Returns:

Tuple[float, …]: a tuple of variance components

snipar.slmm.build_grm_arr(grm_path: str, id_dict: Dict[str, int], thres: float) Tuple[ndarray, ndarray, ndarray][source]

Build GRM data array and corresponding indices.

snipar.slmm.build_ibdrel_arr(ibdrel_path: str, id_dict: Dict[str, int], keep: ndarray, ignore_sib: bool = False, thres: float = 0.05) Tuple[ndarray, ndarray, ndarray][source]

Build ibd relatedness array (lower triangular entries) and corresponding indices from KING ibdseg output.

Args:

ibdrel_path (str): Path to ibdseg output. id_dict (IdDict): dictionary of id-index pairs. keep (Ids): list of ids to keep. ignore_sib (bool): whether to set sibling entries to 0. thres (float, optional): sparsity threshold. Defaults to 0.0205.

Returns:

SparseGRMRepr: sparse GRM representation.

snipar.slmm.build_sib_arr(fam_labels: ndarray) Tuple[ndarray, ndarray, ndarray][source]

Build lower-triangular nonzero entries of sibship matrix.

Args:

fam_labels (FamLabels): ndarray of family id strings corresponding to each individual.

Returns:

SparseGRMRepr: sparse GRM representation.

snipar.slmm.cached_property_depends_on(*args)[source]
snipar.slmm.match_grm_ids(ids: ndarray, fam_labels: ndarray, grm_path: str, grm_source: typing_extensions.Literal[gcta, ibdrel]) Tuple[ndarray, ndarray][source]

Match ids with GRM individual ids.

snipar.slmm.run_gcta_grm(plink_path: str, gcta_path: str, filename: str, output_path: str, keep: List[str] | None = None) None[source]

Build GRM using GCTM.

Args:
gcta_pathstr

path of gcta64 executable.

filenamestr

prefix of bed files; if ‘#’ is in it, create a file containing file names of 22 chromosomes.

output_pathstr

prefix of output path.

keepOptional[List]

if List, create a txt file with each row being containing one IID to keep.