Skip to content

Commit

Permalink
Remove gambit.sigs.convert module
Browse files Browse the repository at this point in the history
  • Loading branch information
jlumpe committed Jul 25, 2024
1 parent e516aaa commit 366f6d6
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 240 deletions.
45 changes: 45 additions & 0 deletions src/gambit/sigs/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,48 @@ def calc_file_signatures(kspec: KmerSpec,
assert all(sig is not None for sig in sigs)

return SignatureList(sigs, kspec)


def dense_to_sparse(vec: Sequence[bool]) -> KmerSignature:
"""Convert k-mer set from dense bit vector to sparse coordinate representation.
Parameters
----------
vec
Boolean vector indicating which k-mers are present.
Returns
-------
numpy.ndarray
Sorted array of coordinates of k-mers present in vector. Data type will be ``numpy.intp``.
See Also
--------
.sparse_to_dense
"""
return np.flatnonzero(vec)


def sparse_to_dense(k_or_kspec: Union[int, KmerSpec], coords: KmerSignature) -> np.ndarray:
"""Convert k-mer set from sparse coordinate representation back to dense bit vector.
Parameters
----------
k_or_kspec
Value of k or a :class:`.KmerSpec` instance.
coords
Sparse coordinate array.
Returns
-------
numpy.ndarray
Dense k-mer bit vector.
See Also
--------
.dense_to_sparse
"""
idx_len = k_or_kspec.nkmers if isinstance(k_or_kspec, KmerSpec) else nkmers(k_or_kspec)
vec = np.zeros(idx_len, dtype=np.bool_)
vec[coords] = 1
return vec
144 changes: 0 additions & 144 deletions src/gambit/sigs/convert.py

This file was deleted.

2 changes: 1 addition & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from gambit.kmers import KmerSpec, kmer_to_index
from gambit.seq import seq_to_bytes, revcomp
from gambit.sigs import KmerSignature, SignatureArray
from gambit.sigs.convert import dense_to_sparse, sparse_to_dense
from gambit.sigs.calc import dense_to_sparse, sparse_to_dense
from gambit.db import Taxon


Expand Down
28 changes: 27 additions & 1 deletion tests/sigs/test_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from Bio import SeqIO
from Bio.Seq import Seq

from gambit.sigs.calc import calc_signature, calc_file_signature, calc_file_signatures
from gambit.sigs.calc import calc_signature, calc_file_signature, calc_file_signatures, \
dense_to_sparse, sparse_to_dense
from gambit.kmers import KmerSpec, index_to_kmer
from gambit.seq import SEQ_TYPES, revcomp, SequenceFile
import gambit.util.io as ioutil
Expand Down Expand Up @@ -171,3 +172,28 @@ def test_calc_file_signatures(self, record_sets, files, concurrency):
sigs2 = calc_file_signatures(KSPEC, files, progress=pconf, concurrency=concurrency)

assert sigarray_eq(sigs, sigs2)


def test_dense_sparse_conversion():
"""Test conversion between dense and sparse representations of k-mer coordinates."""

for k in range(1, 10):

kspec = KmerSpec(k, 'ATGAC')

# Create dense signature with every 3rd k-mer
vec = np.zeros(kspec.nkmers, dtype=bool)
vec[np.arange(vec.size) % 3 == 0] = True

# Convert to sparse
sig = dense_to_sparse(vec)

assert len(sig) == vec.sum()
for index in sig:
assert vec[index]

# Check sorted
assert np.all(np.diff(sig) > 0)

# Check converting back
assert np.array_equal(vec, sparse_to_dense(kspec, sig))
92 changes: 0 additions & 92 deletions tests/sigs/test_convert.py

This file was deleted.

2 changes: 1 addition & 1 deletion tests/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from gambit.metric import jaccard, jaccarddist, jaccard_bits, jaccard_generic, jaccarddist_array, \
jaccarddist_matrix, jaccarddist_pairwise, num_pairs, SCORE_DTYPE, BOUNDS_DTYPE
from gambit.sigs.convert import sparse_to_dense
from gambit.sigs.calc import sparse_to_dense
from gambit.sigs import SignatureArray, SignatureList, dump_signatures, load_signatures
from gambit.kmers import KmerSpec
from gambit.util.progress import check_progress
Expand Down
2 changes: 1 addition & 1 deletion tests/test_tests_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from gambit.kmers import KmerSpec, kmer_to_index, nkmers
from gambit.seq import revcomp
from gambit.sigs.convert import dense_to_sparse
from gambit.sigs.calc import dense_to_sparse
from gambit.util.progress import get_progress
from . import common

Expand Down

0 comments on commit 366f6d6

Please sign in to comment.