Example #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
Example #2
0
def test_getters(test_data):
    """
    Test sundry getters.
    """

    for d in test_data:

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

        assert gpm.length == d["length"]
        assert gpm.num_genotypes == d["num_genotypes"]
        assert gpm.wildtype == d["wildtype"]
        assert gpm.mutant == d["mutant"]

        # Make sure that the .data is the object, *not* a copy of ._data
        assert gpm.data is gpm._data
        assert gpm.encoding_table is gpm._encoding_table

        for i in range(len(gpm.mutations)):
            assert np.array_equal(gpm.mutations[i], d["mutations"][i])

        assert np.array_equal(gpm.genotype, d["genotype"])
        assert np.array_equal(gpm.binary, d["binary"])
        assert np.array_equal(gpm.phenotype, d["phenotype"])
        assert np.array_equal(gpm.uncertainty, d["uncertainty"])

        # Make sure initial values of neighors are set properly
        assert gpm.neighbors is None
        assert gpm.neighbor_function is None
        assert gpm.neighbor_cutoff is None

        # Run get_neighbors call, which will now populate neighbors,
        # neighbor_function, and neighbor_cutoff
        gpm.get_neighbors()
        assert isinstance(gpm.neighbors, pd.DataFrame)
        assert gpm._neighbors is gpm.neighbors
        assert gpm.neighbor_function == "hamming"
        assert gpm.neighbor_cutoff == 1

        gpm.get_neighbors(cutoff=17)
        assert isinstance(gpm.neighbors, pd.DataFrame)
        assert gpm._neighbors is gpm.neighbors
        assert gpm.neighbor_function == "hamming"
        assert gpm.neighbor_cutoff == 17
Example #3
0
def test__trim_neighbors(test_data):

    for d in test_data:

        # Build map again.
        gpm = GenotypePhenotypeMap(genotype=d["genotype"])

        # Get the neighbors
        gpm.get_neighbors()
        num_neighbors_initial = len(gpm.neighbors)

        # Delete a node
        gpm._data = gpm._data.iloc[np.arange(len(gpm._data) - 1, dtype=int)]

        # Since we removed a node, we should have rebuilt neighbors. The length
        # of the neighbors will thus be shorter
        assert len(gpm.neighbors) < num_neighbors_initial
Example #4
0
def test_get_neighbors():

    for use_cython in [False, True]:

        print("Use cython?", use_cython)

        # Check hamming = 1
        gpm = GenotypePhenotypeMap(genotype=["SG", "PF", "SF", "PG"])
        gpm.get_neighbors("hamming", cutoff=1, use_cython=use_cython)

        new_edges = list(gpm._neighbors.loc[:, "edge"])
        new_edges.sort()
        hamming_edges = [(0, 0), (0, 2), (2, 0), (0, 3), (3, 0), (1, 1),
                         (1, 2), (2, 1), (1, 3), (3, 1), (2, 2), (3, 3)]

        hamming_edges.sort()
        for i in range(len(new_edges)):
            assert np.array_equal(hamming_edges[i], new_edges[i])

        # Check hamming = 2
        gpm = GenotypePhenotypeMap(genotype=["SG", "PF", "SF", "PG"])
        gpm.get_neighbors("hamming", cutoff=2, use_cython=use_cython)

        new_edges = list(gpm._neighbors.loc[:, "edge"])
        new_edges.sort()
        hamming_edges = [(0, 0), (0, 1), (1, 0), (0, 2), (2, 0), (0, 3),
                         (3, 0), (1, 1), (1, 2), (2, 1), (1, 3), (3, 1),
                         (2, 2), (2, 3), (3, 2), (3, 3)]
        hamming_edges.sort()
        for i in range(len(new_edges)):
            assert np.array_equal(hamming_edges[i], new_edges[i])

        # Check amino acid codon = 1
        gpm = GenotypePhenotypeMap(genotype=["SG", "PF", "SF", "PG"])
        gpm.get_neighbors("codon", cutoff=1, use_cython=use_cython)

        new_edges = list(gpm._neighbors.loc[:, "edge"])
        new_edges.sort()
        codon_edges = [(0, 0), (0, 3), (3, 0), (1, 1), (1, 2), (2, 1), (2, 2),
                       (3, 3)]
        codon_edges.sort()
        for i in range(len(new_edges)):
            assert np.array_equal(codon_edges[i], new_edges[i])