Skip to content

Commit

Permalink
🐛 fix counts_fragments_features
Browse files Browse the repository at this point in the history
  • Loading branch information
mbuttner committed May 30, 2024
1 parent 94917d2 commit 014be18
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions muon/_atac/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ def count_fragments_features(
raise ValueError("No column with strand for features could be found")
strand_col = features.columns.values[np.where(f_cols == chrom_col)[0][0]]

fragments = pysam.TabixFile(adata.uns["files"]["fragments"], parser=pysam.asBed())
fragments = pysam.TabixFile(adata.uns["files"]["fragments"])
try:
# List of lists matrix is quick and convenient to fill by row
mx = lil_matrix((n_features, n), dtype=int)
Expand All @@ -853,14 +853,15 @@ def count_fragments_features(
f_from = f[start_col] - extend_upstream
f_to = f[end_col] + extend_downstream

for fr in fragments.fetch(f.Chromosome, f_from, f_to):
for fr in fragments.fetch(f[chr_col], f_from, f_to, parser=pysam.asBed()):
try:
ind = adata.obs.index.get_loc(fr.name) # cell barcode (e.g. GTCAGTCAGTCAGTCA-1)
mx.rows[i].append(ind)
mx.data[i].append(int(fr.score)) # number of cuts per fragment (e.g. 2)
except:
pass

# The connection has to be closed
fragments.close()
# Faster to convert to csr first and then transpose
mx = mx.tocsr().transpose()

Expand Down

0 comments on commit 014be18

Please sign in to comment.