import sys
import numpy as np
import scanpy as sc
import scipy.sparse as sp
def main() -> None:
mtx_dir = sys.argv[1]
out_path = sys.argv[2]
target = None
no_log = "--no-log" in sys.argv[3:]
for a in sys.argv[3:]:
if a != "--no-log" and a.lower() != "median":
target = float(a)
adata = sc.read_10x_mtx(mtx_dir)
sc.pp.normalize_total(adata, target_sum=target)
if not no_log:
sc.pp.log1p(adata)
gc = sp.csr_matrix(adata.X).T.tocoo()
n_genes, n_cells = gc.shape
nnz = gc.nnz
order = np.lexsort((gc.col, gc.row))
with open(out_path, "w") as f:
f.write("%%MatrixMarket matrix coordinate real general\n")
f.write(f"{n_genes} {n_cells} {nnz}\n")
rows = gc.row[order]
cols = gc.col[order]
vals = gc.data[order]
for r, c, v in zip(rows, cols, vals):
f.write(f"{r + 1} {c + 1} {float(v)!r}\n")
if __name__ == "__main__":
main()