Exemple #1
0
def test__get_neighbor_directions():

    direction_dict = {
        (0, 0): 0,
        (1, 1): 0,
        (2, 2): 0,
        (3, 3): 0,
        (0, 1): 1,
        (1, 0): -1,
        (0, 2): 1,
        (2, 0): -1,
        (1, 3): 1,
        (3, 1): -1,
        (2, 3): 1,
        (3, 2): -1
    }

    gpm = GenotypePhenotypeMap(["00", "01", "10", "11"])
    gpm.get_neighbors()
    for i in range(len(gpm.neighbors)):
        edge = gpm.neighbors.edge[i]
        dir = gpm.neighbors.direction[i]
        assert direction_dict[edge] == dir

    # Flip map, should flip directions except 0
    gpm.wildtype = "11"
    for i in range(len(gpm.neighbors)):
        edge = gpm.neighbors.edge[i]
        dir = gpm.neighbors.direction[i]
        assert direction_dict[edge] == -dir
Exemple #2
0
def test_wildtype_setter(test_data):
    """
    This is a complicated setter that triggers recalcuation of the binary
    map.
    """

    for d in test_data:

        gpm = GenotypePhenotypeMap(wildtype=d["wildtype"],
                                   genotype=d["genotype"])

        # Trivial set (should not change)
        gpm.wildtype = d["wildtype"]
        assert np.array_equal(gpm.binary, d["binary"])
        assert gpm.mutant == d["mutant"]

        # Something with wrong dimension
        with pytest.raises(ValueError):
            gpm.wildtype = "1"

        # Feed in something that can't be interpreted as iterable
        with pytest.raises(ValueError):
            gpm.wildtype = 0

        # Feed in something with wrong dimension
        with pytest.raises(ValueError):
            gpm.wildtype = []

        # Feed in genotype with states not seen in the test datasets
        with pytest.raises(ValueError):
            gpm.wildtype = "".join(["Z" for _ in range(len(gpm.wildtype))])

        # Feed in wildtype as list, not str
        with pytest.raises(ValueError):
            gpm.wildtype = [s for s in gpm.wildtype]

        # Should work
        gpm.wildtype = d["mutant"]

        # Check that wildtype actually fhanged
        assert gpm.wildtype == d["mutant"]

        # Check mutant (should differ at all variable sites)
        for i in range(len(gpm.wildtype)):
            if len(d["mutations"][i]) != 1:
                assert gpm.mutant[i] != gpm.wildtype[i]

        # Make sure n_mutations has flipped properly.
        for i in range(len(gpm.genotype)):
            counts = sum([
                1 for j in range(len(gpm.genotype[i]))
                if gpm.genotype[i][j] != gpm.wildtype[j]
            ])
            assert gpm.n_mutations[i] == counts

        # Check to make sure the binary was recalculated. This tests whether
        # the encoding table and binary are different from before. Does *not*
        # check accuracy.  This check is done in the
        # test_utils.test_genotypes_to_binary function.
        binary = utils.genotypes_to_binary(gpm.genotype, gpm.encoding_table)
        assert np.array_equal(binary, gpm.binary)
        assert not np.array_equal(binary, d["binary"])