genome-tools 3.0.0__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.
- genome_tools/__init__.py +10 -0
- genome_tools/__main__.py +0 -0
- genome_tools/bed.py +61 -0
- genome_tools/bed_overlaps.py +87 -0
- genome_tools/data/__init__.py +5 -0
- genome_tools/data/anndata.py +92 -0
- genome_tools/data/extractors.py +488 -0
- genome_tools/data/footprints/__init__.py +20 -0
- genome_tools/data/footprints/dataset.py +127 -0
- genome_tools/data/footprints/loaders.py +229 -0
- genome_tools/data/footprints/samplers.py +61 -0
- genome_tools/data/gc_matching.py +284 -0
- genome_tools/data/pwm.py +135 -0
- genome_tools/data/utils.py +164 -0
- genome_tools/genomic_interval.py +362 -0
- genome_tools/helpers.py +104 -0
- genome_tools/plotting/__init__.py +494 -0
- genome_tools/plotting/colors/__init__.py +1 -0
- genome_tools/plotting/colors/cm.py +1186 -0
- genome_tools/plotting/colors/palettes.py +115 -0
- genome_tools/plotting/connectors.py +269 -0
- genome_tools/plotting/contour_plots.py +228 -0
- genome_tools/plotting/gene_annotation.py +211 -0
- genome_tools/plotting/ideogram.py +110 -0
- genome_tools/plotting/modular_plot/README.md +718 -0
- genome_tools/plotting/modular_plot/README_develop.md +336 -0
- genome_tools/plotting/modular_plot/__init__.py +12 -0
- genome_tools/plotting/modular_plot/api.py +500 -0
- genome_tools/plotting/modular_plot/helpers/__init__.py +0 -0
- genome_tools/plotting/modular_plot/helpers/species_mapper.py +282 -0
- genome_tools/plotting/modular_plot/interval_plotter.py +600 -0
- genome_tools/plotting/modular_plot/loaders/__init__.py +0 -0
- genome_tools/plotting/modular_plot/loaders/basic.py +111 -0
- genome_tools/plotting/modular_plot/loaders/dhs.py +43 -0
- genome_tools/plotting/modular_plot/loaders/footprint.py +348 -0
- genome_tools/plotting/modular_plot/loaders/hotspot3.py +88 -0
- genome_tools/plotting/modular_plot/loaders/prediction.py +197 -0
- genome_tools/plotting/modular_plot/loaders/sequence.py +254 -0
- genome_tools/plotting/modular_plot/loaders/variant.py +268 -0
- genome_tools/plotting/modular_plot/plot_components/__init__.py +0 -0
- genome_tools/plotting/modular_plot/plot_components/abstract.py +93 -0
- genome_tools/plotting/modular_plot/plot_components/basic.py +132 -0
- genome_tools/plotting/modular_plot/plot_components/dhs.py +145 -0
- genome_tools/plotting/modular_plot/plot_components/footprint.py +189 -0
- genome_tools/plotting/modular_plot/plot_components/hotspot3.py +151 -0
- genome_tools/plotting/modular_plot/plot_components/prediction.py +219 -0
- genome_tools/plotting/modular_plot/plot_components/sequence.py +90 -0
- genome_tools/plotting/modular_plot/plot_components/variant.py +288 -0
- genome_tools/plotting/modular_plot/utils.py +88 -0
- genome_tools/plotting/sequence.py +409 -0
- genome_tools/plotting/utils.py +264 -0
- genome_tools/utils/__init__.py +1 -0
- genome_tools/utils/sampling.py +230 -0
- genome_tools/utils/sequence.py +33 -0
- genome_tools/utils/signal.py +63 -0
- genome_tools-3.0.0.dist-info/METADATA +888 -0
- genome_tools-3.0.0.dist-info/RECORD +59 -0
- genome_tools-3.0.0.dist-info/WHEEL +5 -0
- genome_tools-3.0.0.dist-info/top_level.txt +1 -0
genome_tools/__init__.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Toolkit for genomic intervals: read data for an interval (genome_tools.data.extractors),
|
|
3
|
+
manipulate intervals (genome_tools.genomic_interval) and plot them (genome_tools.plotting).
|
|
4
|
+
|
|
5
|
+
A guide for coding agents: AGENTS.md at the repository root
|
|
6
|
+
(https://github.com/vierstralab/genome-tools/blob/master/AGENTS.md).
|
|
7
|
+
"""
|
|
8
|
+
__all__ = ["bed", "GenomicInterval", "helpers"]
|
|
9
|
+
|
|
10
|
+
from .genomic_interval import *
|
genome_tools/__main__.py
ADDED
|
File without changes
|
genome_tools/bed.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Copyright 2015 Jeff Vierstra
|
|
2
|
+
|
|
3
|
+
from . import GenomicInterval
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def bed3_iterator(filehandle):
|
|
7
|
+
"""
|
|
8
|
+
Generator that parses BED3 format from a string iterator
|
|
9
|
+
|
|
10
|
+
Returns:
|
|
11
|
+
GenomicInterval
|
|
12
|
+
"""
|
|
13
|
+
for line in filehandle:
|
|
14
|
+
fields = line.strip().split()
|
|
15
|
+
|
|
16
|
+
chrom = fields[0]
|
|
17
|
+
start = int(fields[1])
|
|
18
|
+
end = int(fields[2])
|
|
19
|
+
|
|
20
|
+
yield GenomicInterval(chrom, start, end)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def bed5_iterator(filehandle):
|
|
24
|
+
"""
|
|
25
|
+
Generator that parses BED5 format from a string iterator
|
|
26
|
+
|
|
27
|
+
Returns:
|
|
28
|
+
GenomicInterval
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
for line in filehandle:
|
|
32
|
+
fields = line.strip().split()
|
|
33
|
+
|
|
34
|
+
chrom = fields[0]
|
|
35
|
+
start = int(fields[1])
|
|
36
|
+
end = int(fields[2])
|
|
37
|
+
name = fields[3]
|
|
38
|
+
score = float(fields[4])
|
|
39
|
+
|
|
40
|
+
yield GenomicInterval(chrom, start, end, name, score)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def bed6_iterator(filehandle):
|
|
44
|
+
"""
|
|
45
|
+
Generator that parses BED6 format from a string iterator
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
GenomicInterval
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
for line in filehandle:
|
|
52
|
+
fields = line.strip().split()
|
|
53
|
+
|
|
54
|
+
chrom = fields[0]
|
|
55
|
+
start = int(fields[1])
|
|
56
|
+
end = int(fields[2])
|
|
57
|
+
name = fields[3]
|
|
58
|
+
score = float(fields[4])
|
|
59
|
+
strand = fields[5]
|
|
60
|
+
|
|
61
|
+
yield GenomicInterval(chrom, start, end, name, score, strand)
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
import pyranges as pr
|
|
3
|
+
import numpy as np
|
|
4
|
+
from tqdm.auto import tqdm
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def to_pyranges(df: pd.DataFrame):
|
|
8
|
+
|
|
9
|
+
coords = {'#chr': 'Chromosome', 'chrom': 'Chromosome', 'start': 'Start', 'end': 'End'}
|
|
10
|
+
|
|
11
|
+
df = df[[c for c in coords if c in df.columns]].rename(columns=coords)
|
|
12
|
+
assert list(df.columns) == ['Chromosome', 'Start', 'End'], \
|
|
13
|
+
"DataFrame must have one of '#chr'/'chrom' and 'start', 'end' columns"
|
|
14
|
+
return df.reset_index(drop=True)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _overlap_indicator(ref_pr: pr.PyRanges, map_df: pd.DataFrame, n: int) -> np.ndarray:
|
|
18
|
+
hits = ref_pr.overlap(pr.PyRanges(to_pyranges(map_df))).df
|
|
19
|
+
out = np.zeros(n, dtype=bool)
|
|
20
|
+
if '_ref_idx' in hits.columns: # no overlaps gives a DataFrame without columns
|
|
21
|
+
out[hits['_ref_idx'].to_numpy()] = True
|
|
22
|
+
return out
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def overlaps_any(ref_df: pd.DataFrame, map_df: pd.DataFrame = None, **map_dfs: pd.DataFrame):
|
|
26
|
+
"""
|
|
27
|
+
For each row of ref_df, True if it overlaps at least one interval in map_df(s), else False.
|
|
28
|
+
Uses bed coordinates (half-open).
|
|
29
|
+
|
|
30
|
+
Usage:
|
|
31
|
+
overlaps_any(peaks, tss_df) -> pd.Series (bool, name='overlap')
|
|
32
|
+
overlaps_any(peaks, tss=tss_df, enh=enh_df) -> pd.DataFrame, one bool column per kwarg
|
|
33
|
+
|
|
34
|
+
Pass either map_df or keyword maps, not both.
|
|
35
|
+
Raises ValueError if no map is provided.
|
|
36
|
+
|
|
37
|
+
Requires pyranges (pip install "genome-tools[pyranges]").
|
|
38
|
+
"""
|
|
39
|
+
if map_df is None and not map_dfs:
|
|
40
|
+
raise ValueError(
|
|
41
|
+
"No map DataFrame(s) provided. Pass either a single positional map_df "
|
|
42
|
+
"or one or more keyword map DataFrames (e.g. tss=tss_df, enh=enh_df)."
|
|
43
|
+
)
|
|
44
|
+
if map_df is not None and map_dfs:
|
|
45
|
+
raise ValueError(
|
|
46
|
+
"Both positional map_df and keyword map DataFrames "
|
|
47
|
+
f"({', '.join(map_dfs)}) were provided. Please use only one of them."
|
|
48
|
+
)
|
|
49
|
+
ref = to_pyranges(ref_df)
|
|
50
|
+
ref['_ref_idx'] = ref.index
|
|
51
|
+
ref_pr = pr.PyRanges(ref) # built once, reused for every map
|
|
52
|
+
n = len(ref)
|
|
53
|
+
|
|
54
|
+
if map_dfs:
|
|
55
|
+
out = {}
|
|
56
|
+
for name, m in tqdm(map_dfs.items(), total=len(map_dfs), desc='overlaps_any'):
|
|
57
|
+
out[name] = _overlap_indicator(ref_pr, m, n)
|
|
58
|
+
return pd.DataFrame(out, index=ref_df.index)
|
|
59
|
+
|
|
60
|
+
return pd.Series(_overlap_indicator(ref_pr, map_df, n), index=ref_df.index, name='overlap')
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def distance_to_closest(ref_df: pd.DataFrame, map_df: pd.DataFrame):
|
|
64
|
+
"""
|
|
65
|
+
For each row of ref_df, distance (bp) to the closest interval in map_df.
|
|
66
|
+
0 if overlapping, NaN if map_df has no intervals on that chromosome.
|
|
67
|
+
Distances follow pyranges/bedtools convention: book-ended intervals are 1 bp apart.
|
|
68
|
+
|
|
69
|
+
Requires pyranges (pip install "genome-tools[pyranges]").
|
|
70
|
+
|
|
71
|
+
Returns:
|
|
72
|
+
pd.Series aligned to ref_df.index
|
|
73
|
+
"""
|
|
74
|
+
ref = to_pyranges(ref_df)
|
|
75
|
+
ref['_ref_idx'] = ref.index
|
|
76
|
+
|
|
77
|
+
nearest = pr.PyRanges(ref).nearest(pr.PyRanges(to_pyranges(map_df))).df
|
|
78
|
+
if '_ref_idx' not in nearest.columns: # map has no intervals on any ref chromosome
|
|
79
|
+
dist = pd.Series(dtype=float)
|
|
80
|
+
else:
|
|
81
|
+
# ties give several rows per ref interval with the same distance
|
|
82
|
+
dist = nearest.groupby('_ref_idx')['Distance'].min()
|
|
83
|
+
return pd.Series(
|
|
84
|
+
dist.reindex(ref.index).to_numpy(),
|
|
85
|
+
index=ref_df.index,
|
|
86
|
+
name='distance',
|
|
87
|
+
)
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import zarr
|
|
2
|
+
import anndata as ad
|
|
3
|
+
import dask.array as da
|
|
4
|
+
import numpy as np
|
|
5
|
+
import scipy.sparse as sp
|
|
6
|
+
|
|
7
|
+
from genome_tools.helpers import load_from_file
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _read_zarr_group(dat):
|
|
11
|
+
|
|
12
|
+
if isinstance(dat, zarr.Array):
|
|
13
|
+
return da.from_zarr(dat)
|
|
14
|
+
elif isinstance(dat, zarr.Group):
|
|
15
|
+
enc = dat.attrs.get("encoding-type", "")
|
|
16
|
+
if enc == "dataframe":
|
|
17
|
+
return ad.experimental.read_elem(dat) # returns pd.DataFrame
|
|
18
|
+
else:
|
|
19
|
+
return ad.experimental.sparse_dataset(dat).to_memory()
|
|
20
|
+
else:
|
|
21
|
+
raise ValueError('Not a zarr array or zarr group')
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def read_zarr_backed(filename, lazy_attrs=('layers', 'varm')):
|
|
25
|
+
d = {}
|
|
26
|
+
attributes = ["layers", "varm", "obsm", "obsp", "varp", "uns"]
|
|
27
|
+
df_attributes = ["obs", "var"]
|
|
28
|
+
|
|
29
|
+
with zarr.open(filename, "r") as f:
|
|
30
|
+
if "encoding-type" in f.attrs:
|
|
31
|
+
attributes.extend(df_attributes)
|
|
32
|
+
|
|
33
|
+
if 'X' in f:
|
|
34
|
+
d['X'] = _read_zarr_group(f['X'])
|
|
35
|
+
|
|
36
|
+
for ad_attr in attributes:
|
|
37
|
+
if ad_attr not in f:
|
|
38
|
+
continue
|
|
39
|
+
elif ad_attr in lazy_attrs:
|
|
40
|
+
d[ad_attr] = {}
|
|
41
|
+
for layer in f[ad_attr]:
|
|
42
|
+
d[ad_attr][layer] = _read_zarr_group(f[ad_attr][layer])
|
|
43
|
+
else:
|
|
44
|
+
d[ad_attr] = ad.experimental.read_elem(f[ad_attr])
|
|
45
|
+
|
|
46
|
+
adata = ad.AnnData(**d)
|
|
47
|
+
|
|
48
|
+
return adata
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def convert_to_sparse_if_sufficiently_sparse(matrix, threshold=0.7):
|
|
52
|
+
"""
|
|
53
|
+
Convert a matrix to sparse format if the proportion of zero values exceeds the threshold.
|
|
54
|
+
"""
|
|
55
|
+
if sp.issparse(matrix):
|
|
56
|
+
return matrix
|
|
57
|
+
|
|
58
|
+
sparsity = np.count_nonzero(matrix == 0) / matrix.size
|
|
59
|
+
return sp.csr_matrix(matrix) if sparsity > threshold else matrix
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def add_matrices_to_anndata(adata, matrices_mapping: dict, mask=None):
|
|
63
|
+
"""
|
|
64
|
+
Load each matrix file in matrices_mapping and add it as a layer to adata, keyed by its mapping key.
|
|
65
|
+
Each matrix file is expected to be var x obs on disk (transposed to obs x var to match adata).
|
|
66
|
+
"""
|
|
67
|
+
for matrix_name, matrix in matrices_mapping.items():
|
|
68
|
+
if matrix_name in adata.layers:
|
|
69
|
+
print(f"Layer {matrix_name} already exists in anndata. Skipping.")
|
|
70
|
+
continue
|
|
71
|
+
matrix = load_from_file(matrix).T
|
|
72
|
+
if matrix.dtype == np.float64:
|
|
73
|
+
matrix = matrix.astype(np.float32)
|
|
74
|
+
|
|
75
|
+
if matrix.shape[0] != adata.shape[0]:
|
|
76
|
+
raise ValueError(f"Matrix {matrix_name} has {matrix.shape[0]} samples, but the number of samples in anndata is {adata.shape[0]}")
|
|
77
|
+
if mask is not None:
|
|
78
|
+
assert mask.sum() == matrix.shape[1]
|
|
79
|
+
data = np.empty(adata.shape, dtype=np.float32)
|
|
80
|
+
data[:] = np.nan
|
|
81
|
+
data[:, mask] = matrix
|
|
82
|
+
matrix = data
|
|
83
|
+
|
|
84
|
+
adata.layers[matrix_name] = convert_to_sparse_if_sufficiently_sparse(matrix)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def get_mask_from_column_name(adata, mask_name):
|
|
88
|
+
if mask_name != "":
|
|
89
|
+
mask = adata.var[mask_name]
|
|
90
|
+
else:
|
|
91
|
+
mask = np.ones(adata.var.shape[0], dtype=bool)
|
|
92
|
+
return mask
|