Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MRG: add some argparse utility wrappers #3

Merged
merged 4 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*~
__pycache__
sourmash_utils.egg-info
dist/
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "sourmash_utils"
description = "Python utilities for sourmash plugins and scripts"
readme = "README.md"
requires-python = ">=3.10"
version = "0.1"
version = "0.2"

dependencies = ["sourmash>=4.8.8,<5"]

Expand Down
50 changes: 49 additions & 1 deletion src/sourmash_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"Python utilities for sourmash plugins and scripts."
import sourmash
from sourmash import sourmash_args
from sourmash.cli import utils as sourmash_cli

__all__ = ['FracMinHash']
__all__ = ['FracMinHash', 'add_standard_minhash_args']

class FracMinHash(sourmash.MinHash):
"An updated MinHash class with nicer constructor arguments."
def __init__(self, *,
ksize=31,
scaled=1000,
Expand All @@ -16,6 +19,16 @@ def __init__(self, *,
assert 'dayhoff' not in kwargs
assert 'hp' not in kwargs

if scaled is None:
raise ValueError(f"must specify scaled")
else:
scaled = int(scaled)

if ksize is None:
raise ValueError(f"must specify ksize")
else:
ksize = int(ksize)

if moltype == 'DNA':
kwargs['is_protein'] = False
kwargs['dayhoff'] = False
Expand All @@ -34,3 +47,38 @@ def __init__(self, *,
scaled=scaled,
track_abundance=track_abundance,
**kwargs)

def __str__(self):
return f"k={self.ksize} scaled={self.scaled} moltype={self.moltype}"


def add_standard_minhash_args(parser):
sourmash_cli.add_construct_moltype_args(parser)
sourmash_cli.add_ksize_arg(parser, default=31)
sourmash_cli.add_scaled_arg(parser, default=1000)


def create_minhash_from_args(args, *, track_abundance=False, **defaults):
default_moltype = defaults.get('moltype')
moltype = sourmash_args.calculate_moltype(args, default=default_moltype)
ksize = args.ksize or defaults.get('ksize', 31)
scaled = args.scaled or defaults.get('scaled', 1000)

return FracMinHash(moltype=moltype,
ksize=ksize,
scaled=scaled,
track_abundance=track_abundance)


def load_index_and_select(filename, minhash_obj, *, raise_on_empty=True):
"""Load a sourmash Index object from filename, select sketches compatible
with minhash_obj.
"""
idx = sourmash.load_file_as_index(filename)
idx = idx.select(ksize=minhash_obj.ksize,
moltype=minhash_obj.moltype,
scaled=minhash_obj.scaled,
abund=minhash_obj.track_abundance)
if not idx:
raise ValueError(f"no matching sketches in '{filename}' for k={minhash_obj.ksize} moltype={minhash_obj.moltype} scaled={minhash_obj.scaled}")
return idx