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

Benchmarks for seq_to_hashes in protein mode #1944

Merged
merged 3 commits into from
Apr 10, 2022
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: 0 additions & 1 deletion asv.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"branches": ["latest"],
"dvcs": "git",
"environment_type": "virtualenv",
"pythons": ["3.10"],
"env_dir": ".asv/env",
"results_dir": ".asv/results",
"html_dir": ".asv/html",
Expand Down
72 changes: 71 additions & 1 deletion src/core/benches/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,75 @@ fn add_sequence(c: &mut Criterion) {
});
}

criterion_group!(compute, add_sequence);
fn add_sequence_protein(c: &mut Criterion) {
let mut cp = ComputeParameters::default();
cp.set_protein(true);
cp.set_dna(false);
cp.set_scaled(200);
cp.set_ksizes(vec![30]);
let template_sig = Signature::from_params(&cp);

let mut data: Vec<u8> = vec![];
let (mut f, _) = niffler::from_path("../../tests/test-data/genome-s10.fa.gz").unwrap();
let _ = f.read_to_end(&mut data);

let data = data.repeat(10);

let data_upper = data.to_ascii_uppercase();
let data_lower = data.to_ascii_lowercase();
let data_errors: Vec<u8> = data
.iter()
.enumerate()
.map(|(i, x)| if i % 89 == 1 { b'N' } else { *x })
.collect();

let mut group = c.benchmark_group("add_sequence_protein");
group.sample_size(10);

group.bench_function("valid", |b| {
b.iter(|| {
let fasta_data = Cursor::new(data_upper.clone());
let mut sig = template_sig.clone();
let mut parser = parse_fastx_reader(fasta_data).unwrap();
while let Some(rec) = parser.next() {
sig.add_protein(&rec.unwrap().seq()).unwrap();
}
});
});

group.bench_function("lowercase", |b| {
b.iter(|| {
let fasta_data = Cursor::new(data_lower.clone());
let mut sig = template_sig.clone();
let mut parser = parse_fastx_reader(fasta_data).unwrap();
while let Some(rec) = parser.next() {
sig.add_protein(&rec.unwrap().seq()).unwrap();
}
});
});

group.bench_function("invalid kmers", |b| {
b.iter(|| {
let fasta_data = Cursor::new(data_errors.clone());
let mut sig = template_sig.clone();
let mut parser = parse_fastx_reader(fasta_data).unwrap();
while let Some(rec) = parser.next() {
sig.add_protein(&rec.unwrap().seq()).unwrap();
}
});
});

group.bench_function("force with valid kmers", |b| {
b.iter(|| {
let fasta_data = Cursor::new(data_upper.clone());
let mut sig = template_sig.clone();
let mut parser = parse_fastx_reader(fasta_data).unwrap();
while let Some(rec) = parser.next() {
sig.add_protein(&rec.unwrap().seq()).unwrap();
}
});
});
}

criterion_group!(compute, add_sequence, add_sequence_protein);
criterion_main!(compute);
38 changes: 30 additions & 8 deletions src/sourmash/command_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,15 @@ def __init__(self, args):

def __call__(self):
args = self.args
params = ComputeParameters(args.ksizes, args.seed, args.protein,
args.dayhoff, args.hp, args.dna,
args.num_hashes,
args.track_abundance, args.scaled)
params = ComputeParameters(ksizes=args.ksizes,
seed=args.seed,
protein=args.protein,
dayhoff=args.dayhoff,
hp=args.hp,
dna=args.dna,
num_hashes=args.num_hashes,
track_abundance=args.track_abundance,
scaled=args.scaled)
sig = SourmashSignature.from_params(params)
return [sig]

Expand Down Expand Up @@ -326,7 +331,17 @@ def save_sigs_to_location(siglist, save_sig):
class ComputeParameters(RustObject):
__dealloc_func__ = lib.computeparams_free

def __init__(self, ksizes, seed, protein, dayhoff, hp, dna, num_hashes, track_abundance, scaled):
def __init__(self,
*,
ksizes=(21, 31, 51),
ctb marked this conversation as resolved.
Show resolved Hide resolved
seed=42,
protein=False,
dayhoff=False,
hp=False,
dna=True,
num_hashes=500,
track_abundance=False,
scaled=0):
self._objptr = lib.computeparams_new()

self.seed = seed
Expand Down Expand Up @@ -359,8 +374,15 @@ def from_manifest_row(cls, row):
else:
ksize = row['ksize'] * 3

p = cls([ksize], DEFAULT_MMHASH_SEED, is_protein, is_dayhoff, is_hp, is_dna,
row['num'], row['with_abundance'], row['scaled'])
p = cls(ksizes=[ksize],
seed=DEFAULT_MMHASH_SEED,
protein=is_protein,
dayhoff=is_dayhoff,
hp=is_hp,
dna=is_dna,
num_hashes=row['num'],
track_abundance=row['with_abundance'],
scaled=row['scaled'])

return p

Expand Down Expand Up @@ -405,7 +427,7 @@ def to_param_str(self):
return ",".join(pi)

def __repr__(self):
return f"ComputeParameters({self.ksizes}, {self.seed}, {self.protein}, {self.dayhoff}, {self.hp}, {self.dna}, {self.num_hashes}, {self.track_abundance}, {self.scaled})"
return f"ComputeParameters(ksizes={self.ksizes}, seed={self.seed}, protein={self.protein}, dayhoff={self.dayhoff}, hp={self.hp}, dna={self.dna}, num_hashes={self.num_hashes}, track_abundance={self.track_abundance}, scaled={self.scaled})"

def __eq__(self, other):
return (self.ksizes == other.ksizes and
Expand Down
21 changes: 11 additions & 10 deletions src/sourmash/command_sketch.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,17 @@ def get_compute_params(self, *, split_ksizes=False):
if self.mult_ksize_by_3 and not def_dna:
ksizes = [ k*3 for k in ksizes ]

make_param = lambda ksizes: ComputeParameters(ksizes,
params_d.get('seed', def_seed),
def_protein,
def_dayhoff,
def_hp,
def_dna,
params_d.get('num', def_num),
params_d.get('track_abundance',
def_abund),
params_d.get('scaled', def_scaled))
make_param = lambda ksizes: ComputeParameters(
ksizes=ksizes,
seed=params_d.get('seed', def_seed),
protein=def_protein,
dayhoff=def_dayhoff,
hp=def_hp,
dna=def_dna,
num_hashes=params_d.get('num', def_num),
track_abundance=params_d.get('track_abundance',
def_abund),
scaled=params_d.get('scaled', def_scaled))

if split_ksizes:
for ksize in ksizes:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_sourmash_sketch.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,8 @@ def test_manifest_row_to_compute_parameters_4():


def test_bad_compute_parameters():
p = ComputeParameters([31], 42, 0, 0, 0, 0, 0, True, 1000)
p = ComputeParameters(ksizes=[31], seed=42, dna=0, protein=0, dayhoff=0,
hp=0, num_hashes=0, track_abundance=True, scaled=1000)
with pytest.raises(AssertionError):
p.moltype

Expand Down