aegis_sim.submodels.genetics.ploider

 1import numpy as np
 2
 3
 4class Ploider:
 5    """ """
 6
 7    def init(self, REPRODUCTION_MODE, DOMINANCE_FACTOR, PLOIDY):
 8        self.REPRODUCTION_MODE = REPRODUCTION_MODE
 9        self.DOMINANCE_FACTOR = DOMINANCE_FACTOR
10        self.y = PLOIDY
11
12        if REPRODUCTION_MODE == "sexual":
13            assert PLOIDY == 2, f"If reproduction is sexual, ploidy can only be 2, not {PLOIDY}."
14
15    def diploid_to_haploid(self, loci):
16        """Merge two arrays encoding two chromatids into one array.
17
18        Arguments:
19            loci: A bool numpy array with shape (population size, ploidy, genome length, BITS_PER_LOCUS)
20
21        Returns:
22            A bool numpy array with shape (population size, genome length, BITS_PER_LOCUS)
23        """
24
25        assert len(loci.shape) == 4, len(loci.shape)  # e.g. (45, 2, 250, 8)
26        assert loci.shape[1] == 2, loci.shape[1]  # ploidy
27
28        # TODO handle polyploidy too
29        # compute homozygous (0, 1) or heterozygous (0.5)
30
31        # Three options: both 1, heterozygous, both 0
32
33        # If at least one is 1 then 1; otherwise 0
34        arr = np.logical_or(loci[:, 0], loci[:, 1]).astype(np.float64)
35
36        # Heterozygous
37        is_heterozygous = np.logical_xor(loci[:, 0], loci[:, 1])
38        arr[is_heterozygous] = self.DOMINANCE_FACTOR
39
40        assert len(arr.shape) == 3, len(arr.shape)
41
42        return arr
43
44
45ploider = Ploider()
class Ploider:
 5class Ploider:
 6    """ """
 7
 8    def init(self, REPRODUCTION_MODE, DOMINANCE_FACTOR, PLOIDY):
 9        self.REPRODUCTION_MODE = REPRODUCTION_MODE
10        self.DOMINANCE_FACTOR = DOMINANCE_FACTOR
11        self.y = PLOIDY
12
13        if REPRODUCTION_MODE == "sexual":
14            assert PLOIDY == 2, f"If reproduction is sexual, ploidy can only be 2, not {PLOIDY}."
15
16    def diploid_to_haploid(self, loci):
17        """Merge two arrays encoding two chromatids into one array.
18
19        Arguments:
20            loci: A bool numpy array with shape (population size, ploidy, genome length, BITS_PER_LOCUS)
21
22        Returns:
23            A bool numpy array with shape (population size, genome length, BITS_PER_LOCUS)
24        """
25
26        assert len(loci.shape) == 4, len(loci.shape)  # e.g. (45, 2, 250, 8)
27        assert loci.shape[1] == 2, loci.shape[1]  # ploidy
28
29        # TODO handle polyploidy too
30        # compute homozygous (0, 1) or heterozygous (0.5)
31
32        # Three options: both 1, heterozygous, both 0
33
34        # If at least one is 1 then 1; otherwise 0
35        arr = np.logical_or(loci[:, 0], loci[:, 1]).astype(np.float64)
36
37        # Heterozygous
38        is_heterozygous = np.logical_xor(loci[:, 0], loci[:, 1])
39        arr[is_heterozygous] = self.DOMINANCE_FACTOR
40
41        assert len(arr.shape) == 3, len(arr.shape)
42
43        return arr
def init(self, REPRODUCTION_MODE, DOMINANCE_FACTOR, PLOIDY):
 8    def init(self, REPRODUCTION_MODE, DOMINANCE_FACTOR, PLOIDY):
 9        self.REPRODUCTION_MODE = REPRODUCTION_MODE
10        self.DOMINANCE_FACTOR = DOMINANCE_FACTOR
11        self.y = PLOIDY
12
13        if REPRODUCTION_MODE == "sexual":
14            assert PLOIDY == 2, f"If reproduction is sexual, ploidy can only be 2, not {PLOIDY}."
def diploid_to_haploid(self, loci):
16    def diploid_to_haploid(self, loci):
17        """Merge two arrays encoding two chromatids into one array.
18
19        Arguments:
20            loci: A bool numpy array with shape (population size, ploidy, genome length, BITS_PER_LOCUS)
21
22        Returns:
23            A bool numpy array with shape (population size, genome length, BITS_PER_LOCUS)
24        """
25
26        assert len(loci.shape) == 4, len(loci.shape)  # e.g. (45, 2, 250, 8)
27        assert loci.shape[1] == 2, loci.shape[1]  # ploidy
28
29        # TODO handle polyploidy too
30        # compute homozygous (0, 1) or heterozygous (0.5)
31
32        # Three options: both 1, heterozygous, both 0
33
34        # If at least one is 1 then 1; otherwise 0
35        arr = np.logical_or(loci[:, 0], loci[:, 1]).astype(np.float64)
36
37        # Heterozygous
38        is_heterozygous = np.logical_xor(loci[:, 0], loci[:, 1])
39        arr[is_heterozygous] = self.DOMINANCE_FACTOR
40
41        assert len(arr.shape) == 3, len(arr.shape)
42
43        return arr

Merge two arrays encoding two chromatids into one array.

Arguments: loci: A bool numpy array with shape (population size, ploidy, genome length, BITS_PER_LOCUS)

Returns: A bool numpy array with shape (population size, genome length, BITS_PER_LOCUS)

ploider = <Ploider object>