Esempio n. 1
0
    def build(self):
        """ Build the phenotype map from epistatic interactions.

        For a multiplicative model, this means log-transforming the phenotypes
        first, using linear system of equations to construct the log-phenotypes,
        then back-transforming the phenotypes to non-log space.
        """
        # Get model type:
        if self.model_type == "local":
            encoding = {"1": 1, "0": 0}
            # Build phenotypes from binary representation of space
            self.X = generate_dv_matrix(self.binary.genotypes,
                                        self.epistasis.labels,
                                        encoding=encoding)
            log_phenotypes = np.dot(self.X, self.epistasis.log.values)
        elif self.model_type == "global":
            encoding = {"1": 1, "0": -1}
            # Build phenotypes from binary representation of space
            self.X = generate_dv_matrix(self.binary.genotypes,
                                        self.epistasis.labels,
                                        encoding=encoding)
            log_phenotypes = np.dot(self.X, self.epistasis.log.values)
        else:
            raise Exception("Invalid model type given.")
        # Unlog the phenotypes
        self.phenotypes = self.base**log_phenotypes
Esempio n. 2
0
    def X_constructor(self,
                      genotypes=None,
                      coeff_labels=None,
                      mutations=None,
                      **kwargs):
        """A helper method that constructs an X matrix for this model. Attaches
        an `EpistasisMap` object to the `epistasis` attribute of the model.

        The simplest way to construct X is to give a set of binary genotypes and
        epistatic labels. If not given, will try to infer these features from an
        attached genotype-phenotype map. If no genotype-phenotype map is attached,
        raises an exception.

        Parameters
        ----------
        genotypes : list
            list of genotypes.
        coeff_labels: list
            list of lists. Each sublist contains site-indices that represent
            participants in that epistatic interaction.
        mutations : dict
            mutations dictionary mapping sites to alphabet at the site.
        """
        # First check genotypes are available
        if genotypes is None:
            try:
                genotypes = self.gpm.binary.genotypes
            except AttributeError:
                raise AttributeError(
                    "genotypes must be given, because no GenotypePhenotypeMap is attached to this model."
                )
        # Build epistasis map
        if coeff_labels is None:
            # See if an epistasis map was already created
            if hasattr(self, "epistasis") is False:
                # Mutations dictionary given? if not, try to infer one.
                if mutations is None:
                    try:
                        mutations = self.gpm.mutations
                    except AttributeError:
                        mutations = extract_mutations_from_genotypes(genotypes)
                # Construct epistasis mapping
                self.epistasis = EpistasisMap.from_mutations(
                    mutations, self.order, model_type=self.model_type)
        else:
            self.epistasis = EpistasisMap.from_labels(
                coeff_labels, model_type=self.model_type)
        # Construct the X matrix (convert to binary if necessary).
        try:
            return generate_dv_matrix(genotypes,
                                      self.epistasis.labels,
                                      model_type=self.model_type)
        except:
            mapping = self.gpm.map("complete_genotypes",
                                   "binary.complete_genotypes")
            binaries = [mapping[g] for g in genotypes]
            return generate_dv_matrix(binaries,
                                      self.epistasis.labels,
                                      model_type=self.model_type)
Esempio n. 3
0
 def build(self):
     """ Build the phenotype map from epistatic interactions. """
     # Allocate phenotype numpy array
     _phenotypes = np.zeros(self.n, dtype=float)
     # Get model type:
     self.X = generate_dv_matrix(self.binary.genotypes,
                                 self.epistasis.labels,
                                 model_type=self.model_type)
     self.phenotypes = np.dot(self.X, self.epistasis.values)
Esempio n. 4
0
 def p_additive(self):
     """Get the additive phenotypes"""
     orders = self.epistasis.getorder
     labels = list(orders[0].labels) + list(orders[1].labels)
     vals = list(orders[0].values) + list(orders[1].values)
     x = generate_dv_matrix(self.binary.genotypes,
                            labels,
                            model_type=self.model_type)
     return np.dot(x, vals)
Esempio n. 5
0
 def build(self, *args):
     """ Build nonlinear map from epistasis and function.
     """
     # Get model type:
     self.X = generate_dv_matrix(self.binary.genotypes,
                                 self.epistasis.labels,
                                 model_type=self.model_type)
     _phenotypes = np.dot(self.X, self.epistasis.values)
     self.phenotypes = self.function(_phenotypes, *self.parameters.values)
Esempio n. 6
0
 def build(self):
     """ Build the phenotype map from epistatic interactions. """
     # Allocate phenotype numpy array
     _phenotypes = np.zeros(self.n, dtype=float)
     # Get model type:
     if self.model_type == "local":
         encoding = {"1": 1, "0": 0}
         # Build phenotypes from binary representation of space
         self.X = generate_dv_matrix(self.binary.genotypes,
                                     self.epistasis.labels,
                                     encoding=encoding)
         self.phenotypes = np.dot(self.X, self.epistasis.values)
     elif self.model_type == "global":
         encoding = {"1": 1, "0": -1}
         # Build phenotypes from binary representation of space
         self.X = generate_dv_matrix(self.binary.genotypes,
                                     self.epistasis.labels,
                                     encoding=encoding)
         self.phenotypes = np.dot(self.X, self.epistasis.values)
     else:
         raise Exception("Invalid model type given.")
Esempio n. 7
0
 def p_additive(self):
     """Get the additive phenotypes"""
     if self.model_type == "local":
         encoding = {"1": 1, "0": 0}
     else:
         encoding = {"1": 1, "0": -1}
     orders = self.epistasis.getorder
     labels = list(orders[0].labels) + list(orders[1].labels)
     vals = list(orders[0].values) + list(orders[1].values)
     x = generate_dv_matrix(self.binary.genotypes,
                            labels,
                            encoding=encoding)
     return np.dot(x, vals)
Esempio n. 8
0
    def __init__(self,
                 wildtype,
                 genotypes,
                 phenotypes,
                 stdeviations=None,
                 log_transform=False,
                 mutations=None,
                 n_replicates=1,
                 model_type="local",
                 logbase=np.log10):
        # Populate Epistasis Map
        super(LinearEpistasisModel, self).__init__(wildtype,
                                                   genotypes,
                                                   phenotypes,
                                                   stdeviations=stdeviations,
                                                   log_transform=log_transform,
                                                   mutations=mutations,
                                                   n_replicates=n_replicates,
                                                   logbase=logbase)

        # Define the encoding for different models
        model_types = {
            "local": {
                "encoding": {
                    "1": 1,
                    "0": 0
                },  # Decomposition matrix encoding
                "inverse": 1.0  # Inverse functions coefficient
            },
            "global": {
                "encoding": {
                    "1": 1,
                    "0": -1
                },
                "inverse": 1.0
            }
        }
        self.model_type = model_type
        # Set order of model.
        self.order = len(self.mutations)
        # Build EpistasisMap
        self.epistasis.order = self.order
        # Set encoding from model_type given
        self.encoding = model_types[model_type]["encoding"]
        # Generate basis matrix for mutant cycle approach to epistasis.
        self.X = generate_dv_matrix(self.binary.genotypes,
                                    self.epistasis.labels,
                                    encoding=self.encoding)
        # Calculate the inverse of the matrix
        self.X_inv = np.linalg.inv(
            self.X)  #* model_types[model_type]["inverse"]
Esempio n. 9
0
    def predict(self):
        """ Infer the phenotypes from model.

        Returns
        -------
        genotypes : array
            array of genotypes -- in same order as phenotypes
        phenotypes : array
            array of quantitative phenotypes.
        """
        phenotypes = np.zeros(len(self._model.complete_genotypes), dtype=float)
        binaries = self._model.binary.complete_genotypes
        X = generate_dv_matrix(binaries,
                               self._model.epistasis.labels,
                               encoding=self._model.encoding)
        popt = self._model.parameters.get_params()
        phenotypes = self._model.function(self.linear(), *popt)
        return phenotypes