recode-h5ad 0.1.0__tar.gz

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.
@@ -0,0 +1,62 @@
1
+ Metadata-Version: 2.4
2
+ Name: recode_h5ad
3
+ Version: 0.1.0
4
+ Summary: Recode H5AD file changing sparsity format and cell sorting
5
+ Author-email: Gabriel Hoffman <gabriel.hoffman@gmail.com>
6
+ Project-URL: Homepage, https://github.com/GabrielHoffman/recode_h5ad
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Operating System :: OS Independent
10
+ Requires-Python: >=3.8
11
+ Description-Content-Type: text/markdown
12
+
13
+
14
+ We developed the recode_h5ad script that uses the AnnData library in order to:
15
+
16
+ - Convert read count matrix to gene-major format
17
+ - Sort cells by annotated class and biological sample identifier
18
+ - Create variable libSize storing the number of reads for each cell. If raw/X exists, computed from this. Otherwise computed from X
19
+ - Write to new H5AD file using LZF compression
20
+
21
+ The result is a valid H5AD file supported by standard tools, but just optimized for gene-centric access.
22
+
23
+ Here, we download the script and examine the arguments:
24
+
25
+ ```sh
26
+ recode_h5ad -h
27
+ usage: recode_h5ad [-h] --input INPUT --output OUTPUT [--ondisk]
28
+ [--sortBy SORTBY] [--compression {None,None,gzip,lzf,zstd}]
29
+ [--format {CSR,CSC}] [--noLibSize]
30
+
31
+ Convert an AnnData .h5ad file so that X (and raw/X) is stored in CSC sparse
32
+ format (v aug.26.2026)
33
+
34
+ options:
35
+ -h, --help show this help message and exit
36
+ --input INPUT Input .h5ad file
37
+ --output OUTPUT Output .h5ad file
38
+ --ondisk Use file-backed mode to reduce memory usage
39
+ --sortBy SORTBY Cols to sort by in _decreasing_ order of importance
40
+ --compression {None,None,gzip,lzf,zstd}
41
+ Optional compression for output file (gzip, lzf,
42
+ zstd). Default: None
43
+ --format {CSR,CSC} Store sparse count matrix (X and raw/X) in CSR or CSC
44
+ format. CSR allows faster access to cells, CSC gives
45
+ faster access to genes. Default: CSR
46
+ --noLibSize Skip computing libSize for each cell
47
+ ```
48
+
49
+ ### Example
50
+ Now, we convert an H5AD file to CSC (i.e. gene-major) format, sorting the cells by class, subclass and SampleID, and saving using LZF compression.
51
+
52
+ ```bash
53
+ # H5AD=(Original H5AD file)
54
+ # OUTFILE=(New H5AD file)
55
+
56
+ recode_h5ad.py \
57
+ --input $H5AD \
58
+ --sortBy class,subclass,SampleID \
59
+ --format CSC \
60
+ --compression lzf \
61
+ --out $OUTFILE
62
+ ```
@@ -0,0 +1,50 @@
1
+
2
+ We developed the recode_h5ad script that uses the AnnData library in order to:
3
+
4
+ - Convert read count matrix to gene-major format
5
+ - Sort cells by annotated class and biological sample identifier
6
+ - Create variable libSize storing the number of reads for each cell. If raw/X exists, computed from this. Otherwise computed from X
7
+ - Write to new H5AD file using LZF compression
8
+
9
+ The result is a valid H5AD file supported by standard tools, but just optimized for gene-centric access.
10
+
11
+ Here, we download the script and examine the arguments:
12
+
13
+ ```sh
14
+ recode_h5ad -h
15
+ usage: recode_h5ad [-h] --input INPUT --output OUTPUT [--ondisk]
16
+ [--sortBy SORTBY] [--compression {None,None,gzip,lzf,zstd}]
17
+ [--format {CSR,CSC}] [--noLibSize]
18
+
19
+ Convert an AnnData .h5ad file so that X (and raw/X) is stored in CSC sparse
20
+ format (v aug.26.2026)
21
+
22
+ options:
23
+ -h, --help show this help message and exit
24
+ --input INPUT Input .h5ad file
25
+ --output OUTPUT Output .h5ad file
26
+ --ondisk Use file-backed mode to reduce memory usage
27
+ --sortBy SORTBY Cols to sort by in _decreasing_ order of importance
28
+ --compression {None,None,gzip,lzf,zstd}
29
+ Optional compression for output file (gzip, lzf,
30
+ zstd). Default: None
31
+ --format {CSR,CSC} Store sparse count matrix (X and raw/X) in CSR or CSC
32
+ format. CSR allows faster access to cells, CSC gives
33
+ faster access to genes. Default: CSR
34
+ --noLibSize Skip computing libSize for each cell
35
+ ```
36
+
37
+ ### Example
38
+ Now, we convert an H5AD file to CSC (i.e. gene-major) format, sorting the cells by class, subclass and SampleID, and saving using LZF compression.
39
+
40
+ ```bash
41
+ # H5AD=(Original H5AD file)
42
+ # OUTFILE=(New H5AD file)
43
+
44
+ recode_h5ad.py \
45
+ --input $H5AD \
46
+ --sortBy class,subclass,SampleID \
47
+ --format CSC \
48
+ --compression lzf \
49
+ --out $OUTFILE
50
+ ```
@@ -0,0 +1,25 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0", "anndata>=0.13.0", "hdf5plugin>=7.0.0", "argparse", "numpy", "scipy", "pathlib", "importlib", "packaging"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "recode_h5ad"
7
+ version = "0.1.0"
8
+ authors = [
9
+ { name="Gabriel Hoffman", email="gabriel.hoffman@gmail.com" },
10
+ ]
11
+ description = "Recode H5AD file changing sparsity format and cell sorting"
12
+ readme = "README.md"
13
+ requires-python = ">=3.8"
14
+ classifiers = [
15
+ "Programming Language :: Python :: 3",
16
+ "License :: OSI Approved :: MIT License",
17
+ "Operating System :: OS Independent",
18
+ ]
19
+
20
+ [project.scripts]
21
+ recode_h5ad = "recode_h5ad.main:main"
22
+
23
+ [project.urls]
24
+ Homepage = "https://github.com/GabrielHoffman/recode_h5ad"
25
+
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
File without changes
@@ -0,0 +1,184 @@
1
+ ##!/usr/bin/env python3
2
+
3
+ import sys
4
+ import argparse
5
+ import anndata as ad
6
+ import numpy as np
7
+ import scipy.sparse as sp
8
+ from pathlib import Path
9
+ import importlib
10
+ from packaging.version import parse
11
+ import hdf5plugin
12
+
13
+ def main():
14
+
15
+ # Define arguments
16
+ #-------------------
17
+
18
+ parser = argparse.ArgumentParser(
19
+ description="Convert an AnnData .h5ad file so that X (and raw/X) is stored in CSC sparse format (v aug.26.2026)"
20
+ )
21
+
22
+ parser.add_argument("--input",
23
+ type=Path,
24
+ required=True,
25
+ help="Input .h5ad file")
26
+
27
+ parser.add_argument("--output",
28
+ type=Path,
29
+ required=True,
30
+ help="Output .h5ad file")
31
+
32
+ parser.add_argument("--ondisk",
33
+ action="store_true",
34
+ help="Use file-backed mode to reduce memory usage")
35
+
36
+ parser.add_argument("--sortBy",
37
+ default=None,
38
+ help="Cols to sort by in _decreasing_ order of importance")
39
+
40
+ parser.add_argument(
41
+ "--compression",
42
+ default=None,
43
+ choices=[None, "None", "gzip", "lzf", "zstd"],
44
+ help="Optional compression for output file (gzip, lzf, zstd). Default: None")
45
+
46
+ parser.add_argument(
47
+ "--format",
48
+ default="CSR",
49
+ choices=["CSR", "CSC"],
50
+ help="Store sparse count matrix (X and raw/X) in CSR or CSC format. CSR allows faster access to cells, CSC gives faster access to genes. Default: CSR")
51
+
52
+ parser.add_argument("--noLibSize",
53
+ action="store_true",
54
+ help="Skip computing libSize for each cell")
55
+
56
+ # parse args
57
+ args = parser.parse_args()
58
+
59
+ # if --ondisk set backed to "r"
60
+ backed = None
61
+ if args.ondisk:
62
+ backed = "r"
63
+
64
+ if args.compression == "None":
65
+ args.compression = None
66
+
67
+ print("Script version:", "aug.26.2026")
68
+
69
+ version = importlib.metadata.version("anndata")
70
+ print("AnnData version:", version)
71
+
72
+ if parse(version) >= parse("0.13.0"):
73
+ # for compatibility with anndataR
74
+ ad.settings.allow_write_nullable_strings = False
75
+
76
+ # read file
77
+ print("Read file...")
78
+ adata = ad.read_h5ad(args.input, backed=backed)
79
+
80
+ # Replace forward slashes in column names of data.obs
81
+ adata.obs.columns = adata.obs.columns.str.replace("/", "_", regex=False)
82
+
83
+ # Find counts entry
84
+ if adata.X is not None:
85
+ print("Using AnnData X matrix...")
86
+ else:
87
+ if 'X' in adata.layers:
88
+ print("Using AnnData layers/X matrix...")
89
+ adata.X = adata.layers['X']
90
+ elif 'counts' in adata.layers:
91
+ print("Using AnnData layers/counts matrix...")
92
+ adata.X = adata.layers['counts']
93
+
94
+ # sort cells by type
95
+ if args.sortBy != None:
96
+
97
+ print("Sorting...")
98
+ fields = args.sortBy.split(",")
99
+
100
+ # check if all fields are present
101
+ missing = set(fields) - set(adata.obs.columns)
102
+
103
+ if missing:
104
+ print(f"Missing columns: {missing}")
105
+ sys.exit(2)
106
+
107
+ # get sorted order
108
+ # reverse since 1st sorted index is the last one for lexsort
109
+ idx = np.lexsort(
110
+ keys = tuple(adata.obs[c].to_numpy() for c in fields[::-1]) )
111
+
112
+ # apply reordering
113
+ adata = adata[idx,:]
114
+
115
+ if args.format == "CSC":
116
+
117
+ if not args.noLibSize or not sp.isspmatrix_csc(adata.X):
118
+ if backed is None:
119
+ adata = adata.copy()
120
+ else:
121
+ adata = adata.to_memory()
122
+
123
+ if not args.noLibSize:
124
+ # compute library size for each cell
125
+ print("Compute libSize...")
126
+ if adata.raw is not None:
127
+ adata.obs['libSize'] = adata.raw.X.sum(axis=1)
128
+ else:
129
+ adata.obs['libSize'] = adata.X.sum(axis=1)
130
+
131
+ # if matrix is not a CSC of doubles
132
+ if not (sp.isspmatrix_csc(adata.X) and adata.X.dtype == np.float64):
133
+ print("Converting .X to CSC sparse format...")
134
+ # convert matrix type
135
+ adata.X = sp.csc_matrix(adata.X, dtype=np.float64)
136
+
137
+ if adata.raw is not None:
138
+ print("Converting .row.X to CSC sparse format...")
139
+ raw = adata.raw.to_adata()
140
+ raw.X = sp.csc_matrix(raw.X, dtype=np.float64)
141
+ adata.raw = raw
142
+
143
+ if args.format == "CSR":
144
+ # X
145
+ if not sp.isspmatrix_csr(adata.X):
146
+ print("Converting .X to CSR sparse format...")
147
+ if backed is None:
148
+ adata = adata.copy()
149
+ else:
150
+ adata = adata.to_memory()
151
+
152
+ # convert matrix type
153
+ adata.X = sp.csr_matrix(adata.X, dtype=np.float64)
154
+
155
+ if adata.raw is not None:
156
+ print("Converting .row.X to CSR sparse format...")
157
+ raw = adata.raw.to_adata()
158
+ raw.X = sp.csr_matrix(raw.X, dtype=np.float64)
159
+ adata.raw = raw
160
+
161
+ if not args.noLibSize:
162
+ # compute library size for each cell
163
+ print("Compute libSize...")
164
+ if adata.raw is not None:
165
+ adata.obs['libSize'] = adata.raw.X.sum(axis=1)
166
+ else:
167
+ adata.obs['libSize'] = adata.X.sum(axis=1)
168
+
169
+ if args.output.is_file():
170
+ args.output.unlink(missing_ok=True)
171
+
172
+ print("Writing H5AD...")
173
+
174
+ compressMethod = args.compression
175
+
176
+ if compressMethod == "zstd":
177
+ compressMethod = hdf5plugin.FILTERS["zstd"]
178
+
179
+ adata.write_h5ad( args.output, compression=compressMethod )
180
+
181
+
182
+
183
+ if __name__ == "__main__":
184
+ main()
@@ -0,0 +1,62 @@
1
+ Metadata-Version: 2.4
2
+ Name: recode_h5ad
3
+ Version: 0.1.0
4
+ Summary: Recode H5AD file changing sparsity format and cell sorting
5
+ Author-email: Gabriel Hoffman <gabriel.hoffman@gmail.com>
6
+ Project-URL: Homepage, https://github.com/GabrielHoffman/recode_h5ad
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Operating System :: OS Independent
10
+ Requires-Python: >=3.8
11
+ Description-Content-Type: text/markdown
12
+
13
+
14
+ We developed the recode_h5ad script that uses the AnnData library in order to:
15
+
16
+ - Convert read count matrix to gene-major format
17
+ - Sort cells by annotated class and biological sample identifier
18
+ - Create variable libSize storing the number of reads for each cell. If raw/X exists, computed from this. Otherwise computed from X
19
+ - Write to new H5AD file using LZF compression
20
+
21
+ The result is a valid H5AD file supported by standard tools, but just optimized for gene-centric access.
22
+
23
+ Here, we download the script and examine the arguments:
24
+
25
+ ```sh
26
+ recode_h5ad -h
27
+ usage: recode_h5ad [-h] --input INPUT --output OUTPUT [--ondisk]
28
+ [--sortBy SORTBY] [--compression {None,None,gzip,lzf,zstd}]
29
+ [--format {CSR,CSC}] [--noLibSize]
30
+
31
+ Convert an AnnData .h5ad file so that X (and raw/X) is stored in CSC sparse
32
+ format (v aug.26.2026)
33
+
34
+ options:
35
+ -h, --help show this help message and exit
36
+ --input INPUT Input .h5ad file
37
+ --output OUTPUT Output .h5ad file
38
+ --ondisk Use file-backed mode to reduce memory usage
39
+ --sortBy SORTBY Cols to sort by in _decreasing_ order of importance
40
+ --compression {None,None,gzip,lzf,zstd}
41
+ Optional compression for output file (gzip, lzf,
42
+ zstd). Default: None
43
+ --format {CSR,CSC} Store sparse count matrix (X and raw/X) in CSR or CSC
44
+ format. CSR allows faster access to cells, CSC gives
45
+ faster access to genes. Default: CSR
46
+ --noLibSize Skip computing libSize for each cell
47
+ ```
48
+
49
+ ### Example
50
+ Now, we convert an H5AD file to CSC (i.e. gene-major) format, sorting the cells by class, subclass and SampleID, and saving using LZF compression.
51
+
52
+ ```bash
53
+ # H5AD=(Original H5AD file)
54
+ # OUTFILE=(New H5AD file)
55
+
56
+ recode_h5ad.py \
57
+ --input $H5AD \
58
+ --sortBy class,subclass,SampleID \
59
+ --format CSC \
60
+ --compression lzf \
61
+ --out $OUTFILE
62
+ ```
@@ -0,0 +1,9 @@
1
+ README.md
2
+ pyproject.toml
3
+ src/recode_h5ad/__init__.py
4
+ src/recode_h5ad/main.py
5
+ src/recode_h5ad.egg-info/PKG-INFO
6
+ src/recode_h5ad.egg-info/SOURCES.txt
7
+ src/recode_h5ad.egg-info/dependency_links.txt
8
+ src/recode_h5ad.egg-info/entry_points.txt
9
+ src/recode_h5ad.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ recode_h5ad = recode_h5ad.main:main
@@ -0,0 +1 @@
1
+ recode_h5ad