def setup(self): self.mh = MinHash(500, 21, track_abundance=False) self.sequences = load_sequences() self.populated_mh = MinHash(500, 21, track_abundance=False) for seq in self.sequences: self.populated_mh.add_sequence(seq)
def test_set_abundance(): a = MinHash(20, 10, track_abundance=False) with pytest.raises(RuntimeError) as e: a.set_abundances({1: 3, 2: 4}) assert "track_abundance=True when constructing" in e.value.args[0]
class TimeMinAbundanceSuite(TimeMinHashSuite): def setup(self): TimeMinHashSuite.setup(self) self.mh = MinHash(500, 21, track_abundance=True) self.populated_mh = MinHash(500, 21, track_abundance=True) for seq in self.sequences: self.populated_mh.add_sequence(seq) def time_get_mins_abundance(self): mh = self.populated_mh for i in range(500): mh.get_mins(with_abundance=True) def time_set_abundances(self): mh = self.mh mins = self.populated_mh.get_mins(with_abundance=True) for i in range(500): mh.set_abundances(mins) def time_set_abundances_noclear(self): mh = self.mh mins = self.populated_mh.get_mins(with_abundance=True) for i in range(500): mh.set_abundances(mins, clear=False)
def test_no_downsample_scaled_if_n(track_abundance): # make sure you can't set max_n and then downsample scaled mh = MinHash(2, 4, track_abundance=track_abundance) with pytest.raises(ValueError) as excinfo: mh.downsample_scaled(100000000) assert 'cannot downsample a standard MinHash' in str(excinfo.value)
def setup(self): self.mh = MinHash(500, 21, track_abundance=True) self.protein_mh = MinHash(500, 21, is_protein=True, track_abundance=True) self.sequences = load_sequences()
def setup(self): TimeMinHashSuite.setup(self) self.mh = MinHash(500, 21, track_abundance=True) self.populated_mh = MinHash(500, 21, track_abundance=True) for seq in self.sequences: self.populated_mh.add_sequence(seq)
def test_add_hash_with_abundance_2(): a = MinHash(20, 5, False, track_abundance=False) with pytest.raises(RuntimeError) as e: a.add_hash_with_abundance(10, 1) assert "track_abundance=True when constructing" in e.value.args[0]
def test_no_downsample_scaled_if_n(track_abundance): # make sure you can't set max_n and then downsample scaled mh = MinHash(2, 4, track_abundance=track_abundance) with pytest.raises(ValueError) as excinfo: mh.downsample_scaled(100000000) assert 'cannot downsample a standard MinHash' in str(excinfo)
def setup(self): self.mh = MinHash(500, 21, track_abundance=False) self.sequences = load_sequences(get_test_data('ecoli.genes.fna')) * 10 self.populated_mh = MinHash(500, 21, track_abundance=False) for seq in self.sequences: self.populated_mh.add_sequence(seq)
def test_mh_asymmetric_merge(track_abundance): # test merging two asymmetric (different size) MHs a = MinHash(20, 10, track_abundance=track_abundance) for i in range(0, 40, 2): a.add_hash(i) # different size: 10 b = MinHash(10, 10, track_abundance=track_abundance) for i in range(0, 80, 4): b.add_hash(i) c = a.merge(b) d = b.merge(a) assert len(a) == 20 assert len(b) == 10 assert len(c) == len(a) assert len(d) == len(b) # can't compare different sizes without downsampling with pytest.raises(TypeError): d.compare(a) a = a.downsample_n(d.num) print(a.get_mins()) print(d.get_mins()) assert d.compare(a) == 1.0 c = c.downsample_n(b.num) assert c.compare(b) == 1.0
def test_set_abundances_huge(): max_hash = 4000000 a = MinHash(0, 10, track_abundance=True, max_hash=max_hash) hashes = list(range(max_hash)) abundances = itertools.repeat(2) a.set_abundances(dict(zip(hashes, abundances)))
def test_div_zero_contained(track_abundance): # verify that empty MHs do not yield divide by zero errors for contained_by mh = MinHash(1, 4, track_abundance=track_abundance) mh2 = mh.copy_and_clear() mh.add_sequence('ATGC') assert mh.contained_by(mh2) == 0 assert mh2.contained_by(mh) == 0
def test_set_abundance_initialized(): a = MinHash(1, 4, track_abundance=False) a.add_sequence('ATGC') with pytest.raises(RuntimeError) as e: a.track_abundance = True assert "Can only set track_abundance=True if the MinHash is empty" in e.value.args[0]
def test_set_abundance_clear(): # on empty minhash, clear should have no effect a = MinHash(20, 5, False, track_abundance=True) b = MinHash(20, 5, False, track_abundance=True) a.set_abundances({1: 3, 2: 4}, clear=True) b.set_abundances({1: 3, 2: 4}, clear=False) assert a.get_mins() == b.get_mins()
def test_basic_dna_bad(track_abundance): # test behavior on bad DNA mh = MinHash(1, 4, track_abundance=track_abundance) with pytest.raises(ValueError) as e: mh.add_sequence('ATGR') print(e) assert 'invalid DNA character in input k-mer: ATGR' in str(e)
def test_basic_dna_bad(track_abundance): # test behavior on bad DNA mh = MinHash(1, 4, track_abundance=track_abundance) with pytest.raises(ValueError) as e: mh.add_sequence('ATGR') print(e) assert 'invalid DNA character in input k-mer: ATGR' in str(e.value)
def test_size_limit(track_abundance): # test behavior with size limit of 3 mh = MinHash(3, 4, track_abundance=track_abundance) mh.add_hash(10) mh.add_hash(20) mh.add_hash(30) assert mh.get_mins() == [10, 20, 30] mh.add_hash(5) # -> should push 30 off end assert mh.get_mins() == [5, 10, 20]
def test_abundance_simple(): a = MinHash(20, 5, False, track_abundance=True) a.add_sequence('AAAAA') assert a.get_mins() == [2110480117637990133] assert a.get_mins(with_abundance=True) == {2110480117637990133: 1} a.add_sequence('AAAAA') assert a.get_mins() == [2110480117637990133] assert a.get_mins(with_abundance=True) == {2110480117637990133: 2}
def test_reviving_minhash(): # simulate reading a MinHash from disk mh = MinHash(0, 21, max_hash=184467440737095520, seed=42, track_abundance=False) mins = (28945103950853965, 74690756200987412, 82962372765557409, 93503551367950366, 106923350319729608, 135116761470196737, 160165359281648267, 162390811417732001, 177939655451276972) for m in mins: mh.add_hash(m)
def test_mh_inplace_concat_asymmetric(track_abundance): # test merging two asymmetric (different size) MHs a = MinHash(20, 10, track_abundance=track_abundance) for i in range(0, 40, 2): a.add_hash(i) # different size: 10 b = MinHash(10, 10, track_abundance=track_abundance) for i in range(0, 80, 4): b.add_hash(i) c = a.__copy__() c += b d = b.__copy__() d += a assert len(a) == 20 assert len(b) == 10 assert len(c) == len(a) assert len(d) == len(b) try: d.compare(a) except TypeError as exc: assert 'must have same num' in str(exc) a = a.downsample_n(d.num) assert d.compare(a) == 1.0 # see: d += a, above. c = c.downsample_n(b.num) assert c.compare(b) == 0.5
def test_mh_angular_similarity_2(): # check actual angular similarity for a second non-trivial case a = MinHash(0, 20, max_hash=100, track_abundance=True) b = MinHash(0, 20, max_hash=100, track_abundance=True) a.set_abundances({1: 5, 3: 3, 5: 2, 8: 2, 70: 70}) b.set_abundances({1: 3, 3: 2, 5: 1, 6: 1, 8: 1, 10: 1, 70: 70}) assert round(a.similarity(b), 4) == 0.9728 # ignore_abundance => jaccard assert a.similarity(b, ignore_abundance=True) == 5. / 7.
def test_mh_count_common(track_abundance): a = MinHash(20, 10, track_abundance=track_abundance) for i in range(0, 40, 2): a.add_hash(i) b = MinHash(20, 10, track_abundance=track_abundance) for i in range(0, 80, 4): b.add_hash(i) assert a.count_common(b) == 10 assert b.count_common(a) == 10
def test_minhash_abund_capacity_increase(): # this targets bug #319, a segfault caused by invalidation of # std::vector iterators upon vector resizing. # this should set capacity to 1000 - see KmerMinHash constructor call # to 'reserve' when n > 0 for specific parameter. a = MinHash(0, 10, track_abundance=True, max_hash=5000) # 1001 is dependent on the value passed to reserve (currently 1000). for i in range(1001, 0, -1): a.add_hash(i)
def test_protein_dayhoff(track_abundance, dayhoff): # verify that we can hash protein/aa sequences mh = MinHash(10, 6, True, dayhoff=dayhoff, hp=False, track_abundance=track_abundance) mh.add_protein('AGYYG') assert len(mh.get_mins()) == 4
def test_mh_jaccard_similarity(): # check actual Jaccard value for a non-trivial case a = MinHash(0, 20, max_hash=50, track_abundance=False) b = MinHash(0, 20, max_hash=50, track_abundance=False) a.add_many([1, 3, 5, 8]) b.add_many([1, 3, 5, 6, 8, 10]) assert a.similarity(b) == 4. / 6.
class TimeMinHashSuite: def setup(self): self.mh = MinHash(500, 21, track_abundance=False) self.sequences = load_sequences(get_test_data('ecoli.genes.fna')) * 10 self.populated_mh = MinHash(500, 21, track_abundance=False) for seq in self.sequences: self.populated_mh.add_sequence(seq) def time_add_sequence(self): mh = self.mh sequences = self.sequences for seq in sequences: mh.add_sequence(seq) def time_get_mins(self): mh = self.populated_mh for i in range(500): mh.get_mins() def time_add_hash(self): mh = self.mh for i in range(10000): mh.add_hash(i) def time_compare(self): mh = self.mh other_mh = self.populated_mh for i in range(500): mh.compare(other_mh) def time_count_common(self): mh = self.mh other_mh = self.populated_mh for i in range(500): mh.count_common(other_mh) def time_merge(self): mh = self.mh other_mh = self.populated_mh for i in range(500): mh.merge(other_mh) def time_copy(self): mh = self.populated_mh for i in range(500): mh.__copy__() def time_concat(self): mh = self.mh other_mh = self.populated_mh for i in range(500): mh += other_mh
def test_minhash_abund_add(): # this targets part of bug #319, a segfault caused by invalidation of # std::vector iterators upon vector resizing - in this case, there # was also a bug in inserting into the middle of mins when scaled was set. a = MinHash(0, 10, track_abundance=True, max_hash=5000) n = 0 for i in range(10, 0, -1): a.add_hash(i) n += 1 assert len(a.get_mins()) == n print(len(a.get_mins()))
def test_translate_codon(track_abundance): # Ensure that translation occurs properly mh = MinHash(10, 6, is_protein=True) assert "S" == mh.translate_codon('TCT') assert "S" == mh.translate_codon('TC') assert "X" == mh.translate_codon("T") with pytest.raises(ValueError): mh.translate_codon("") mh.translate_codon("TCTA")
def test_minhash_abund_merge_flat_2(): # this targets a segfault caused by trying to merge # a signature with abundance and a signature without abundance. a = MinHash(0, 10, track_abundance=True, max_hash=5000) b = MinHash(0, 10, max_hash=5000) for i in range(0, 10, 2): a.add_hash(i) for j in range(0, 10, 3): b.add_hash(i) a.merge(b)
def test_mh_merge_check_length(track_abundance): a = MinHash(20, 10, track_abundance=track_abundance) for i in range(0, 40, 2): a.add_hash(i) b = MinHash(20, 10, track_abundance=track_abundance) for i in range(0, 80, 4): b.add_hash(i) c = a.merge(b) assert len(c.get_mins()) == 20
def test_consume_lowercase(track_abundance): a = MinHash(20, 10, track_abundance=track_abundance) b = MinHash(20, 10, track_abundance=track_abundance) a.add_sequence('TGCCGCCCAGCACCGGGTGACTAGGTTGAGCCATGATTAACCTGCAATGA'.lower()) b.add_sequence('TGCCGCCCAGCACCGGGTGACTAGGTTGAGCCATGATTAACCTGCAATGA') assert a.compare(b) == 1.0 assert b.compare(b) == 1.0 assert b.compare(a) == 1.0 assert a.compare(a) == 1.0
def test_mh_subtract(track_abundance): # test subtracting two identically configured minhashes a = MinHash(20, 10, track_abundance=track_abundance) for i in range(0, 40, 2): a.add_hash(i) b = MinHash(20, 10, track_abundance=track_abundance) for i in range(0, 80, 4): b.add_hash(i) assert a.subtract_mins(b) == set(range(2, 40, 4))
def test_set_abundance_clear_3(): a = MinHash(20, 5, False, track_abundance=True) a.add_hash(10) assert a.get_mins(with_abundance=True) == {10: 1} a.set_abundances({20: 1, 30: 4}, clear=False) assert a.get_mins(with_abundance=True) == {10: 1, 20: 1, 30: 4}
def test_clear_2(): a = MinHash(20, 5, False, track_abundance=False) a.add_hash(10) assert a.get_mins() == [10] a.clear() assert a.get_mins() == []
def test_clear(): a = MinHash(20, 5, False, track_abundance=True) a.add_hash(10) assert a.get_mins(with_abundance=True) == {10: 1} a.clear() assert a.get_mins(with_abundance=True) == {}
def test_scaled_property(track_abundance): scaled = 10000 a = MinHash(0, 10, track_abundance=track_abundance, max_hash=round(2**64 / scaled)) assert a.scaled == scaled
def test_mh_copy_and_clear_with_max_hash(track_abundance): # test basic creation of new, empty MinHash w/max_hash param set a = MinHash(0, 10, track_abundance=track_abundance, max_hash=20) for i in range(0, 40, 2): a.add_hash(i) b = a.copy_and_clear() assert a.ksize == b.ksize assert b.num == a.num assert b.max_hash == 20 assert not b.is_protein assert b.track_abundance == track_abundance assert b.seed == a.seed assert len(b.get_mins()) == 0 assert a.scaled == b.scaled assert b.scaled != 0
def test_pickle_scaled(track_abundance): a = MinHash(0, 10, track_abundance=track_abundance, scaled=922337203685477632) for i in range(0, 40, 2): a.add_hash(i) b = pickle.loads(pickle.dumps(a)) assert a.ksize == b.ksize assert b.num == a.num assert b.max_hash == a.max_hash assert b.max_hash == 20 assert not b.is_protein assert b.track_abundance == track_abundance assert b.seed == a.seed assert len(b.get_mins()) == len(a.get_mins()) assert len(b.get_mins()) == 11 assert a.scaled == b.scaled assert b.scaled != 0
class TimeMinAbundanceSuite(TimeMinHashSuite): def setup(self): TimeMinHashSuite.setup(self) self.mh = MinHash(500, 21, track_abundance=True) self.populated_mh = MinHash(500, 21, track_abundance=True) for seq in self.sequences: self.populated_mh.add_sequence(seq) def time_get_mins_abundance(self): mh = self.populated_mh for i in range(500): mh.get_mins(with_abundance=True) def time_set_abundances(self): mh = self.mh mins = self.populated_mh.get_mins(with_abundance=True) for i in range(500): mh.set_abundances(mins)
def test_mh_merge_check_length(track_abundance): a = MinHash(20, 10, track_abundance=track_abundance) for i in range(0, 40, 2): a.add_hash(i) b = MinHash(20, 10, track_abundance=track_abundance) for i in range(0, 80, 4): b.add_hash(i) c = a.merge(b) assert(len(c.get_mins()) == 20)
def test_bytes_protein(track_abundance): # verify that we can hash protein/aa sequences mh = MinHash(10, 6, True, track_abundance=track_abundance) mh.add_protein('AGYYG') mh.add_protein(u'AGYYG') mh.add_protein(b'AGYYG') assert len(mh.get_mins()) == 4
def test_basic_dna(track_abundance): # verify that MHs of size 1 stay size 1, & act properly as bottom sketches. mh = MinHash(1, 4, track_abundance=track_abundance) mh.add_sequence('ATGC') a = mh.get_mins() mh.add_sequence('GCAT') # this will not get added; hash > ATGC b = mh.get_mins() print(a, b) assert a == b assert len(b) == 1
def test_mh_merge(track_abundance): # test merging two identically configured minhashes a = MinHash(20, 10, track_abundance=track_abundance) for i in range(0, 40, 2): a.add_hash(i) b = MinHash(20, 10, track_abundance=track_abundance) for i in range(0, 80, 4): b.add_hash(i) c = a.merge(b) d = b.merge(a) assert len(c) == len(d) assert c.get_mins() == d.get_mins() assert c.compare(d) == 1.0 assert d.compare(c) == 1.0
def test_minhash_abund_merge_flat(): # this targets a segfault caused by trying to compute similarity # of a signature with abundance and a signature without abundance. # the correct behavior for now is to calculate simple Jaccard, # i.e. 'flatten' both of them. a = MinHash(0, 10, track_abundance=True, max_hash=5000) b = MinHash(0, 10, max_hash=5000) for i in range(0, 10, 2): a.add_hash(i) for j in range(0, 10, 3): b.add_hash(i) # these crashed, previously. assert a.similarity(b) == 0.2 assert b.similarity(a) == 0.2
def test_mh_inplace_concat(track_abundance): # test merging two identically configured minhashes a = MinHash(20, 10, track_abundance=track_abundance) for i in range(0, 40, 2): a.add_hash(i) b = MinHash(20, 10, track_abundance=track_abundance) for i in range(0, 80, 4): b.add_hash(i) c = a.__copy__() c += b d = b.__copy__() d += a assert len(c) == len(d) assert c.get_mins() == d.get_mins() assert c.compare(d) == 1.0 assert d.compare(c) == 1.0
def test_add_many(track_abundance): a = MinHash(0, 10, track_abundance=track_abundance, max_hash=5000) b = MinHash(0, 10, track_abundance=track_abundance, max_hash=5000) a.add_many(list(range(0, 100, 2))) a.add_many(list(range(0, 100, 2))) assert len(a) == 50 assert all(c % 2 == 0 for c in a.get_mins()) for h in range(0, 100, 2): b.add_hash(h) b.add_hash(h) assert len(b) == 50 assert a == b
def test_remove_many(track_abundance): a = MinHash(0, 10, track_abundance=track_abundance, max_hash=5000) a.add_many(list(range(0, 100, 2))) orig_sig = signature.SourmashSignature(a) orig_md5 = orig_sig.md5sum() a.remove_many(list(range(0, 100, 3))) new_sig = signature.SourmashSignature(a) new_md5 = new_sig.md5sum() assert orig_md5 == "f1cc295157374f5c07cfca5f867188a1" assert new_md5 == "dd93fa319ef57f4a019c59ee1a8c73e2" assert orig_md5 != new_md5 assert len(a) == 33 assert all(c % 6 != 0 for c in a.get_mins())
def test_bytes_dna(track_abundance): mh = MinHash(1, 4, track_abundance=track_abundance) mh.add_sequence('ATGC') mh.add_sequence(b'ATGC') mh.add_sequence(u'ATGC') a = mh.get_mins() mh.add_sequence('GCAT') # this will not get added; hash > ATGC mh.add_sequence(b'GCAT') # this will not get added; hash > ATGC mh.add_sequence(u'GCAT') # this will not get added; hash > ATGC b = mh.get_mins() print(a, b) assert a == b assert len(b) == 1