localqtl 0.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- localqtl/__init__.py +50 -0
- localqtl/_torch_utils.py +44 -0
- localqtl/cis/__init__.py +11 -0
- localqtl/cis/common.py +124 -0
- localqtl/cis/independent.py +675 -0
- localqtl/cis/mapper.py +157 -0
- localqtl/cis/nominal.py +424 -0
- localqtl/cis/permutations.py +375 -0
- localqtl/cis/postproc.py +159 -0
- localqtl/finemap.py +153 -0
- localqtl/genotypeio.py +402 -0
- localqtl/haplotypeio.py +417 -0
- localqtl/iosinks.py +304 -0
- localqtl/pgen.py +211 -0
- localqtl/phenotypeio.py +101 -0
- localqtl/preproc.py +78 -0
- localqtl/regression_kernels.py +258 -0
- localqtl/stats.py +217 -0
- localqtl/utils.py +87 -0
- localqtl-0.1.1.dist-info/METADATA +221 -0
- localqtl-0.1.1.dist-info/RECORD +22 -0
- localqtl-0.1.1.dist-info/WHEEL +4 -0
localqtl/__init__.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""Top-level package exports for :mod:`localqtl`.
|
|
2
|
+
|
|
3
|
+
This module re-exports the most commonly used classes/functions so that they
|
|
4
|
+
remain accessible from ``localqtl`` after the project was reorganized into a
|
|
5
|
+
package with dedicated submodules (``cis``, ``preproc``, ``finemap`` …).
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from . import cis
|
|
9
|
+
from . import finemap
|
|
10
|
+
from . import genotypeio
|
|
11
|
+
from . import haplotypeio
|
|
12
|
+
from . import iosinks
|
|
13
|
+
from . import pgen
|
|
14
|
+
from . import phenotypeio
|
|
15
|
+
from . import preproc
|
|
16
|
+
from . import regression_kernels
|
|
17
|
+
from . import stats
|
|
18
|
+
from . import utils
|
|
19
|
+
|
|
20
|
+
from .cis import CisMapper, map_independent, map_nominal, map_permutations
|
|
21
|
+
from .genotypeio import InputGeneratorCis, PlinkReader
|
|
22
|
+
from .haplotypeio import InputGeneratorCisWithHaps, RFMixReader
|
|
23
|
+
from .phenotypeio import read_phenotype_bed
|
|
24
|
+
from .pgen import PgenReader
|
|
25
|
+
from .utils import gpu_available
|
|
26
|
+
|
|
27
|
+
__all__ = [
|
|
28
|
+
"map_nominal",
|
|
29
|
+
"map_permutations",
|
|
30
|
+
"map_independent",
|
|
31
|
+
"CisMapper",
|
|
32
|
+
"read_phenotype_bed",
|
|
33
|
+
"gpu_available",
|
|
34
|
+
"PlinkReader",
|
|
35
|
+
"RFMixReader",
|
|
36
|
+
"PgenReader",
|
|
37
|
+
"InputGeneratorCis",
|
|
38
|
+
"InputGeneratorCisWithHaps",
|
|
39
|
+
"cis",
|
|
40
|
+
"finemap",
|
|
41
|
+
"phenotypeio",
|
|
42
|
+
"genotypeio",
|
|
43
|
+
"haplotypeio",
|
|
44
|
+
"iosinks",
|
|
45
|
+
"pgen",
|
|
46
|
+
"preproc",
|
|
47
|
+
"regression_kernels",
|
|
48
|
+
"stats",
|
|
49
|
+
"utils",
|
|
50
|
+
]
|
localqtl/_torch_utils.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Optional, Union
|
|
4
|
+
|
|
5
|
+
import torch
|
|
6
|
+
|
|
7
|
+
TensorLike = Union[torch.Tensor, torch.nn.Parameter]
|
|
8
|
+
DeviceLike = Union[str, torch.device]
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def resolve_device(device: DeviceLike) -> torch.device:
|
|
12
|
+
"""Normalize device specifications to a :class:`torch.device`."""
|
|
13
|
+
return device if isinstance(device, torch.device) else torch.device(device)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def move_to_device(
|
|
17
|
+
tensor: TensorLike,
|
|
18
|
+
device: torch.device,
|
|
19
|
+
*,
|
|
20
|
+
non_blocking: Optional[bool] = None,
|
|
21
|
+
) -> torch.Tensor:
|
|
22
|
+
"""Move ``tensor`` to ``device`` avoiding redundant copies.
|
|
23
|
+
|
|
24
|
+
Parameters
|
|
25
|
+
----------
|
|
26
|
+
tensor : torch.Tensor
|
|
27
|
+
Tensor to move.
|
|
28
|
+
device : torch.device
|
|
29
|
+
Destination device.
|
|
30
|
+
non_blocking : bool, optional
|
|
31
|
+
When ``True`` and ``device`` is CUDA, allow asynchronous copies if the
|
|
32
|
+
source tensor is in pinned memory. If ``None`` (default), the flag is
|
|
33
|
+
enabled automatically for CUDA destinations.
|
|
34
|
+
"""
|
|
35
|
+
if tensor.device == device:
|
|
36
|
+
return tensor
|
|
37
|
+
|
|
38
|
+
if non_blocking is None:
|
|
39
|
+
non_blocking = device.type == "cuda"
|
|
40
|
+
|
|
41
|
+
return tensor.to(device=device, non_blocking=non_blocking)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
__all__ = ["resolve_device", "move_to_device"]
|
localqtl/cis/__init__.py
ADDED
localqtl/cis/common.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import numpy as np
|
|
3
|
+
import pandas as pd
|
|
4
|
+
from typing import Optional, Tuple, List
|
|
5
|
+
|
|
6
|
+
from .._torch_utils import move_to_device, resolve_device
|
|
7
|
+
from ..regression_kernels import Residualizer
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"dosage_vector_for_covariate",
|
|
11
|
+
"residualize_matrix_with_covariates",
|
|
12
|
+
"residualize_batch",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _coerce_tensor(value, device: torch.device) -> torch.Tensor:
|
|
17
|
+
return move_to_device(value, device) if torch.is_tensor(value) else torch.as_tensor(
|
|
18
|
+
value, dtype=torch.float32, device=device
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _make_gen(seed: int | None, device: str) -> torch.Generator | None:
|
|
23
|
+
if seed is None:
|
|
24
|
+
return None
|
|
25
|
+
g = torch.Generator(device=device)
|
|
26
|
+
g.manual_seed(int(seed) & ((1 << 63) - 1))
|
|
27
|
+
return g
|
|
28
|
+
|
|
29
|
+
def dosage_vector_for_covariate(genotype_df: pd.DataFrame, variant_id: str,
|
|
30
|
+
sample_order: pd.Index, missing: float | int | None) -> np.ndarray:
|
|
31
|
+
"""Fetch a dosage row aligned to samples; impute 'missing' to mean of observed."""
|
|
32
|
+
if not genotype_df.columns.equals(sample_order):
|
|
33
|
+
row = genotype_df.loc[variant_id, sample_order].to_numpy(dtype=np.float32, copy=True)
|
|
34
|
+
else:
|
|
35
|
+
row = genotype_df.loc[variant_id].to_numpy(dtype=np.float32, copy=True)
|
|
36
|
+
if missing is not None:
|
|
37
|
+
mm = (row == missing)
|
|
38
|
+
if mm.any():
|
|
39
|
+
if (~mm).any():
|
|
40
|
+
row[mm] = row[~mm].mean(dtype=np.float32)
|
|
41
|
+
else:
|
|
42
|
+
row[mm] = 0.0
|
|
43
|
+
return row
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def residualize_matrix_with_covariates(
|
|
47
|
+
Y: torch.Tensor, C: Optional[pd.DataFrame], device: str
|
|
48
|
+
) -> Tuple[torch.Tensor, Optional[Residualizer]]:
|
|
49
|
+
"""
|
|
50
|
+
Residualize (features x samples) matrix Y against covariates C across samples.
|
|
51
|
+
Returns residualized Y and a Residualizer (or None if no covariates).
|
|
52
|
+
"""
|
|
53
|
+
if C is None:
|
|
54
|
+
return Y, None
|
|
55
|
+
dev = resolve_device(device)
|
|
56
|
+
C_t = torch.tensor(C.values, dtype=torch.float32, device=dev)
|
|
57
|
+
rez = Residualizer(C_t)
|
|
58
|
+
(Y_resid,) = rez.transform(move_to_device(Y, dev), center=True)
|
|
59
|
+
return Y_resid, rez
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def residualize_batch(
|
|
63
|
+
y, G: torch.Tensor, H: Optional[torch.Tensor],
|
|
64
|
+
rez: Optional[Residualizer], center: bool = True, group: bool = False,
|
|
65
|
+
) -> Tuple[object, torch.Tensor, Optional[torch.Tensor]]:
|
|
66
|
+
"""
|
|
67
|
+
Residualize y, G, and optional H with the same Residualizer.
|
|
68
|
+
- If group=False: y is a single vector (n,), returns (y_resid 1D, G_resid, H_resid).
|
|
69
|
+
- If group=True: y is a stack (k x n) or list of length k, returns (list_of_k 1D tensors, G_resid, H_resid).
|
|
70
|
+
"""
|
|
71
|
+
if rez is not None and hasattr(rez, "Q_t") and getattr(rez.Q_t, "numel", lambda: 0)() > 0:
|
|
72
|
+
dev = resolve_device(rez.Q_t.device)
|
|
73
|
+
else:
|
|
74
|
+
dev = resolve_device(G.device)
|
|
75
|
+
G = move_to_device(G, dev)
|
|
76
|
+
if H is not None:
|
|
77
|
+
H = move_to_device(H, dev)
|
|
78
|
+
|
|
79
|
+
if rez is None:
|
|
80
|
+
# Pass-through; normalize return type for group mode
|
|
81
|
+
if group:
|
|
82
|
+
if isinstance(y, (list, tuple)):
|
|
83
|
+
y_list = [torch.as_tensor(yi) if not torch.is_tensor(yi) else yi
|
|
84
|
+
for yi in y]
|
|
85
|
+
else:
|
|
86
|
+
Y = y if torch.is_tensor(y) else torch.as_tensor(y)
|
|
87
|
+
y_list = [Y] if Y.ndim == 1 else [Y[i, :] for i in range(Y.shape[0])]
|
|
88
|
+
return y_list, G, H
|
|
89
|
+
return (y if torch.is_tensor(y) else torch.as_tensor(y, dtype=torch.float32)), G, H
|
|
90
|
+
|
|
91
|
+
# Build Y on the same device as G/rez
|
|
92
|
+
if group:
|
|
93
|
+
if isinstance(y, (list, tuple)):
|
|
94
|
+
Y = torch.stack([_coerce_tensor(yi, dev) for yi in y], dim=0)
|
|
95
|
+
else:
|
|
96
|
+
Y = _coerce_tensor(y, dev)
|
|
97
|
+
if Y.ndim == 1:
|
|
98
|
+
Y = Y.unsqueeze(0)
|
|
99
|
+
else:
|
|
100
|
+
Y = _coerce_tensor(y, dev)
|
|
101
|
+
if Y.ndim == 1:
|
|
102
|
+
Y = Y.unsqueeze(0)
|
|
103
|
+
|
|
104
|
+
# Prepare matrices for one-pass residualization
|
|
105
|
+
mats: List[torch.Tensor] = [G]
|
|
106
|
+
H_shape = None
|
|
107
|
+
if H is not None:
|
|
108
|
+
m, n, pH = H.shape
|
|
109
|
+
H_shape = (m, n, pH)
|
|
110
|
+
mats.append(H.reshape(m * pH, n))
|
|
111
|
+
|
|
112
|
+
mats_resid = rez.transform(*mats, Y, center=center)
|
|
113
|
+
|
|
114
|
+
G_resid = mats_resid[0]
|
|
115
|
+
idx, H_resid = 1, None
|
|
116
|
+
if H is not None:
|
|
117
|
+
H_resid = mats_resid[idx].reshape(H_shape)
|
|
118
|
+
idx += 1
|
|
119
|
+
Y_resid = mats_resid[idx]
|
|
120
|
+
|
|
121
|
+
if group:
|
|
122
|
+
return [Y_resid[i, :] for i in range(Y_resid.shape[0])], G_resid, H_resid
|
|
123
|
+
else:
|
|
124
|
+
return Y_resid.squeeze(0), G_resid, H_resid
|