Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
data = self._data
not_array = isinstance(data, str)
if motif_length == 1:
counts = CategoryCounter(data)
else:
if len(data) % motif_length != 0:
warnings.warn(
"%s length not divisible by %s, truncating"
% (self.name, motif_length)
)
limit = (len(data) // motif_length) * motif_length
data = data[:limit]
if not_array:
counts = CategoryCounter(
data[i : i + motif_length] for i in range(0, limit, motif_length)
)
else:
counts = CategoryCounter(
tuple(v) for v in data.reshape(limit // motif_length, motif_length)
)
if not not_array:
for key in list(counts):
indices = [key] if motif_length == 1 else key
motif = self.alphabet.to_chars(indices).astype(str)
motif = "".join(motif)
counts[motif] = counts.pop(key)
exclude = []
if not include_ambiguity or not allow_gap:
is_degen = self.moltype.is_degenerate
d = Delegator(l)
c = copy(d)
assert c is not d
assert c._handler is d._handler
def test_deepcopy(self):
"""copy.deepcopy should work correctly on Delegator"""
l = ["a"]
d = Delegator(l)
c = deepcopy(d)
assert c is not d
assert c._handler is not d._handler
assert c._handler == d._handler
class FunctionWrapperTests(TestCase):
"""Tests of the FunctionWrapper class"""
def test_init(self):
"""FunctionWrapper should initialize with any callable"""
f = FunctionWrapper(str)
g = FunctionWrapper(id)
h = FunctionWrapper(iterable)
x = 3
self.assertEqual(f(x), "3")
self.assertEqual(g(x), id(x))
self.assertEqual(h(x), [3])
def test_copy(self):
"""copy should work for FunctionWrapper objects"""
f = FunctionWrapper(str)
c = copy(f)
def test_init(self):
"""Dict2D init should work as expected"""
# NOTE: currently only tests init from dict of dicts. Other initializers
# are tested in the test_guess_input* and test_from* methods
# should compare equal to the relevant dict
for d in [self.empty, self.single_same, self.single_diff, self.dense,
self.sparse]:
d2d = Dict2D(d)
self.assertEqual(d2d, d)
self.assertEqual(d2d.__class__, Dict2D)
# spot-check values
d2d = Dict2D(self.sparse)
self.assertEqual(d2d['a']['c'], 3)
self.assertEqual(d2d['d']['b'], 2)
self.assertEqual(len(d2d), 2)
self.assertEqual(len(d2d['a']), 2)
self.assertRaises(KeyError, d2d.__getitem__, 'c')
# check truth values
assert not Dict2D(self.empty)
assert Dict2D(self.single_same)
align.align_to_ref(),
align.progressive_align(model="GY94"),
sample.fixed_length(100),
sample.min_length(100),
io.write_seqs(os.getcwd()),
sample.omit_bad_seqs(),
sample.omit_degenerates(),
sample.take_codon_positions(1),
sample.take_named_seqs(),
sample.trim_stop_codons(gc=1),
]
return applications
class FastSlowDistTests(TestCase):
seqs1 = make_unaligned_seqs(_seqs1, moltype=DNA)
seqs2 = make_unaligned_seqs(_seqs2, moltype=DNA)
seqs3 = make_unaligned_seqs(_seqs3, moltype=DNA)
seqs4 = make_unaligned_seqs(_seqs4, moltype=DNA)
seqs5 = make_unaligned_seqs(_seqs5, moltype=PROTEIN)
def test_init(self):
"""tests if fast_slow_dist can be initialised correctly"""
fast_slow_dist = dist_app.fast_slow_dist(fast_calc="hamming", moltype="dna")
self.assertIsInstance(fast_slow_dist.fast_calc, HammingPair)
self.assertIsNone(fast_slow_dist._sm)
fast_slow_dist = dist_app.fast_slow_dist(distance="TN93")
self.assertIsInstance(fast_slow_dist.fast_calc, TN93Pair)
self.assertEqual(fast_slow_dist._sm.name, "TN93")
fast_slow_dist = dist_app.fast_slow_dist(distance="GTR")
def test_write(self):
"""exercising write method"""
data = [[3, 7], [2, 8], [5, 5]]
darr = DictArrayTemplate(list("ABC"), list("ab")).wrap(data)
with TemporaryDirectory(dir=".") as dirname:
outpath = os.path.join(dirname, "delme.tsv")
darr.write(outpath)
with open(outpath) as infile:
contents = [l.strip().split() for l in infile]
header = contents.pop(0)
self.assertEqual(header, ["dim-1", "dim-2", "value"])
got = {(k1, k2): int(v) for k1, k2, v in contents}
self.assertEqual(got, darr.to_dict(flatten=True))
def test_pickle_unpickle(self):
"""roundtrip via pickling"""
data = {
"edge.parent": {"NineBande": "root", "edge.1": "root",},
"x": {"NineBande": 1.0, "edge.1": 1.0,},
"length": {"NineBande": 4.0, "edge.1": 4.0,},
"y": {"NineBande": 3.0, "edge.1": 3.0,},
"z": {"NineBande": 6.0, "edge.1": 6.0,},
"edge.name": {"NineBande": "NineBande", "edge.1": "edge.1",},
}
t = Table(
data=data, max_width=50, index="edge.name", title="My title", legend="blah",
)
# via string
s = pickle.dumps(t)
r = pickle.loads(s)
self.assertEqual(str(t), str(r))
# via file
with TemporaryDirectory(".") as dirname:
path = pathlib.Path(dirname) / "table.pickle"
t.write(str(path))
r = load_table(path)
self.assertEqual(str(t), str(r))
def test_repr_html_(self):
"""should produce html"""
# no index
t = Table(header=self.t8_header, data=self.t8_rows)
_ = t._repr_html_()
# with an index
t = Table(header=self.t8_header, data=self.t8_rows, index="edge.name")
_ = t._repr_html_()
# data has tuples in an array
data = dict(
key=numpy.array([("a", "c"), ("b", "c"), ("a", "d")], dtype="O"),
count=[1, 3, 2],
)
t = Table(data=data)
_ = t._repr_html_()
def test_distinct_values(self):
"""test the table distinct_values method"""
t1 = Table(header=self.t1_header, data=self.t1_rows)
self.assertEqual(len(t1.distinct_values("chrom")), 2)
self.assertEqual(len(t1.distinct_values("stableid")), 10)
self.assertEqual(len(t1.distinct_values("length")), 10)
t2 = Table(header=self.t2_header, data=self.t2_rows)
self.assertEqual(len(t2.distinct_values("id")), 5)
self.assertEqual(len(t2.distinct_values("foo")), 3)
self.assertEqual(len(t2.distinct_values("bar")), 5)
d = t2.distinct_values("foo")
self.assertEqual(d, {"cab", "bca", "abc"})
def test_keys_are_str(self):
"""all column headers converted to str"""
t = Table(header=["col 1", 2], data=[[0, 1]])
self.assertEqual(t.header, ("col 1", "2"))
def test_specifying_space(self):
"""controls spacing in simple format"""
space = " "
t4 = Table(header=self.t1_header, data=self.t1_rows)
orig = len(str(t4).splitlines()[1])
t4 = Table(header=self.t1_header, data=self.t1_rows, space=space)
got1 = len(str(t4).splitlines()[1])
self.assertTrue(got1 > orig)
# repr is same
got2 = len(repr(t4).splitlines()[1])
self.assertEqual(got1, got2)