Exemplo n.º 1
0
def get_response_content(fs):
    # get the nucleotide distribution
    nt_to_weight = SnippetUtil.get_distribution(fs.nucleotides,
            'nucleotide', nt_letters)
    # get the amino acid distribution
    aa_to_weight = SnippetUtil.get_distribution(fs.aminoacids,
            'amino acid', aa_letters)
    # get results
    mutation_distribution = [nt_to_weight[nt] for nt in nt_letters]
    aa_distribution = [aa_to_weight[aa] for aa in aa_letters]
    pair = DirectProtein.get_nt_distribution_and_aa_energies(
            mutation_distribution, aa_distribution)
    nt_distribution, aa_energies = pair
    # write something
    out = StringIO()
    # write the stationary nucleotide distribution
    print >> out, 'nucleotide stationary distribution:'
    for nt, value in zip(nt_letters, nt_distribution):
        print >> out, '%s : %s' % (nt, value)
    print >> out, ''
    # write the amino acid energies
    print >> out, 'amino acid energies:'
    for aa, value in zip(aa_letters, aa_energies):
        print >> out, '%s : %s' % (aa, value)
    # return the response
    return out.getvalue()
Exemplo n.º 2
0
 def evaluate(self, X):
     """
     @param X: the three vars defining the mutation process nt distribution.
     @return: a tuple which is the zero vector when the guess was right.
     """
     if len(X) != 3:
         raise ValueError("incorrect number of parameters")
     x, y, z = X
     mutation_nt_weights = (1, math.exp(x), math.exp(y), math.exp(z))
     mutation_nt_dist = normalized(mutation_nt_weights)
     if not almost_equals(sum(mutation_nt_dist), 1.0):
         msg_a = "detected possibly invalid objective function in put: "
         msg_b = str(X)
         raise ValueError(msg_a + msg_b)
     pair = DirectProtein.get_nt_distribution_and_aa_energies(mutation_nt_dist, self.aa_dist)
     stationary_nt_dist, aa_energies = pair
     evaluation = tuple(math.log(a / b) for a, b in zip(self.nt_dist[:3], stationary_nt_dist[:3]))
     return evaluation, aa_energies
Exemplo n.º 3
0
 def __call__(self, X):
     """
     The input is a triple.
     These three variables define the mutation process
     nucleotide distribution
     @param X: a triple
     @return: the zero vector when the guess was right
     """
     self.guesses.append(X)
     if len(X) != 3:
         raise ValueError('incorrect number of parameters')
     x, y, z = X
     mutation_nt_weights = (1, math.exp(x), math.exp(y), math.exp(z))
     mutation_nt_dist = normalized(mutation_nt_weights)
     pair = DirectProtein.get_nt_distribution_and_aa_energies(
             mutation_nt_dist, self.aa_dist)
     stationary_nt_dist, aa_energies = pair
     return tuple(math.log(a/b)
             for a, b in zip(self.nt_dist[:3], stationary_nt_dist[:3]))
Exemplo n.º 4
0
 def __call__(self, X):
     """
     The input is a triple.
     These three variables define the mutation process
     nucleotide distribution
     @param X: a triple
     @return: the zero vector when the guess was right
     """
     self.guesses.append(X)
     if len(X) != 3:
         raise ValueError('incorrect number of parameters')
     x, y, z = X
     mutation_nt_weights = (1, math.exp(x), math.exp(y), math.exp(z))
     mutation_nt_dist = normalized(mutation_nt_weights)
     pair = DirectProtein.get_nt_distribution_and_aa_energies(
         mutation_nt_dist, self.aa_dist)
     stationary_nt_dist, aa_energies = pair
     return tuple(
         math.log(a / b)
         for a, b in zip(self.nt_dist[:3], stationary_nt_dist[:3]))
Exemplo n.º 5
0
 def evaluate(self, X):
     """
     @param X: the three vars defining the mutation process nt distribution.
     @return: a tuple which is the zero vector when the guess was right.
     """
     if len(X) != 3:
         raise ValueError('incorrect number of parameters')
     x, y, z = X
     mutation_nt_weights = (1, math.exp(x), math.exp(y), math.exp(z))
     mutation_nt_dist = normalized(mutation_nt_weights)
     if not almost_equals(sum(mutation_nt_dist), 1.0):
         msg_a ='detected possibly invalid objective function in put: '
         msg_b = str(X)
         raise ValueError(msg_a + msg_b)
     pair = DirectProtein.get_nt_distribution_and_aa_energies(
             mutation_nt_dist, self.aa_dist)
     stationary_nt_dist, aa_energies = pair
     evaluation = tuple(math.log(a/b)
             for a, b in zip(self.nt_dist[:3], stationary_nt_dist[:3]))
     return evaluation, aa_energies
Exemplo n.º 6
0
def get_response_content(fs):
    # get the nucleotide distribution
    nt_to_weight = SnippetUtil.get_distribution(fs.nucleotides, 'nucleotide',
                                                nt_letters)
    # get the amino acid distribution
    aa_to_weight = SnippetUtil.get_distribution(fs.aminoacids, 'amino acid',
                                                aa_letters)
    # get distributions in convenient list form
    stationary_nt_distribution = [nt_to_weight[nt] for nt in nt_letters]
    aa_distribution = [aa_to_weight[aa] for aa in aa_letters]
    codon_distribution = []
    implied_stationary_nt_distribution = []
    if fs.corrected:
        # define the objective function
        objective_function = MyObjective(aa_distribution,
                                         stationary_nt_distribution)
        initial_guess = (0, 0, 0)
        iterations = 20
        best = scipy.optimize.nonlin.broyden2(objective_function,
                                              initial_guess, iterations)
        x, y, z = best
        best_mutation_weights = (1, math.exp(x), math.exp(y), math.exp(z))
        best_mutation_distribution = normalized(best_mutation_weights)
        # Given the mutation distribution and the amino acid distribution,
        # get the stationary distribution.
        result = DirectProtein.get_nt_distribution_and_aa_energies(
            best_mutation_distribution, aa_distribution)
        implied_stationary_nt_distribution, result_aa_energies = result
        # Get the codon distribution;
        # kappa doesn't matter because we are only concerned
        # with stationary distributions
        kappa = 1.0
        dpm = DirectProtein.DirectProteinRateMatrix(
            kappa, best_mutation_distribution, result_aa_energies)
        codon_distribution = dpm.get_stationary_distribution()
    elif fs.hb:
        # get the codon distribution
        unnormalized_codon_distribution = []
        for codon in codons:
            aa = Codon.g_codon_to_aa_letter[codon]
            sibling_codons = Codon.g_aa_letter_to_codons[aa]
            codon_aa_weight = aa_to_weight[aa]
            codon_nt_weight = np.prod([nt_to_weight[nt] for nt in codon])
            sibling_nt_weight_sum = sum(
                np.prod([nt_to_weight[nt] for nt in sibling])
                for sibling in sibling_codons)
            weight = codon_aa_weight * codon_nt_weight
            weight /= sibling_nt_weight_sum
            unnormalized_codon_distribution.append(weight)
        codon_distribution = normalized(unnormalized_codon_distribution)
        nt_to_weight = dict(zip(nt_letters, [0] * 4))
        for codon, p in zip(codons, codon_distribution):
            for nt in codon:
                nt_to_weight[nt] += p
        implied_stationary_nt_distribution = normalized(nt_to_weight[nt]
                                                        for nt in nt_letters)
    # start the output text string
    out = StringIO()
    # write the codon stationary distribution
    print >> out, 'estimated codon stationary distribution:'
    for codon, p in zip(codons, codon_distribution):
        print >> out, '%s : %s' % (codon, p)
    print >> out, ''
    # write the nucleotide stationary distribution
    print >> out, 'implied nucleotide stationary distribution:'
    for nt, p in zip(nt_letters, implied_stationary_nt_distribution):
        print >> out, '%s : %s' % (nt, p)
    # return the response
    return out.getvalue()
Exemplo n.º 7
0
def get_response_content(fs):
    # get the nucleotide distribution
    nt_to_weight = SnippetUtil.get_distribution(fs.nucleotides,
            'nucleotide', nt_letters)
    # get the amino acid distribution
    aa_to_weight = SnippetUtil.get_distribution(fs.aminoacids,
            'amino acid', aa_letters)
    # get distributions in convenient list form
    stationary_nt_distribution = [nt_to_weight[nt] for nt in nt_letters]
    aa_distribution = [aa_to_weight[aa] for aa in aa_letters]
    codon_distribution = []
    implied_stationary_nt_distribution = []
    if fs.corrected:
        # define the objective function
        objective_function = MyObjective(aa_distribution,
                stationary_nt_distribution)
        initial_guess = (0, 0, 0)
        iterations = 20
        best = scipy.optimize.nonlin.broyden2(objective_function,
                initial_guess, iterations)
        x, y, z = best
        best_mutation_weights = (1, math.exp(x), math.exp(y), math.exp(z))
        best_mutation_distribution = normalized(best_mutation_weights)
        # Given the mutation distribution and the amino acid distribution,
        # get the stationary distribution.
        result = DirectProtein.get_nt_distribution_and_aa_energies(
                best_mutation_distribution, aa_distribution)
        implied_stationary_nt_distribution, result_aa_energies = result
        # Get the codon distribution;
        # kappa doesn't matter because we are only concerned
        # with stationary distributions
        kappa = 1.0
        dpm = DirectProtein.DirectProteinRateMatrix(
                kappa, best_mutation_distribution, result_aa_energies)
        codon_distribution = dpm.get_stationary_distribution()
    elif fs.hb:
        # get the codon distribution
        unnormalized_codon_distribution = []
        for codon in codons:
            aa = Codon.g_codon_to_aa_letter[codon]
            sibling_codons = Codon.g_aa_letter_to_codons[aa]
            codon_aa_weight = aa_to_weight[aa]
            codon_nt_weight = np.prod([nt_to_weight[nt] for nt in codon])
            sibling_nt_weight_sum = sum(np.prod([nt_to_weight[nt]
                for nt in sibling]) for sibling in sibling_codons)
            weight = codon_aa_weight * codon_nt_weight
            weight /= sibling_nt_weight_sum
            unnormalized_codon_distribution.append(weight)
        codon_distribution = normalized(unnormalized_codon_distribution)
        nt_to_weight = dict(zip(nt_letters, [0]*4))
        for codon, p in zip(codons, codon_distribution):
            for nt in codon:
                nt_to_weight[nt] += p
        implied_stationary_nt_distribution = normalized(nt_to_weight[nt]
                for nt in nt_letters)
    # start the output text string
    out = StringIO()
    # write the codon stationary distribution
    print >> out, 'estimated codon stationary distribution:'
    for codon, p in zip(codons, codon_distribution):
        print >> out, '%s : %s' % (codon, p)
    print >> out, ''
    # write the nucleotide stationary distribution
    print >> out, 'implied nucleotide stationary distribution:'
    for nt, p in zip(nt_letters, implied_stationary_nt_distribution):
        print >> out, '%s : %s' % (nt, p)
    # return the response
    return out.getvalue()
Exemplo n.º 8
0
    objective_function = MyCodonObjective(aa_distribution, observed_nt_stationary_distribution)
    initial_stationary_guess = halpern_bruno_nt_estimate(nt_to_probability, aa_to_probability)
    A, C, G, T = initial_stationary_guess
    initial_guess = (math.log(C / A), math.log(G / A), math.log(T / A))
    iterations = 20
    try:
        best = scipy.optimize.nonlin.broyden2(objective_function, initial_guess, iterations)
    except Exception, e:
        debugging_information = objective_function.get_history()
        raise HandlingError(str(e) + "\n" + debugging_information)
    x, y, z = best
    best_mutation_weights = (1, math.exp(x), math.exp(y), math.exp(z))
    best_mutation_distribution = normalized(best_mutation_weights)
    # Given the mutation distribution and the amino acid distribution,
    # get the stationary distribution.
    result = DirectProtein.get_nt_distribution_and_aa_energies(best_mutation_distribution, aa_distribution)
    result_stationary_nt_dist, result_aa_energies = result
    # make a results string
    out = StringIO()
    # write the stationary nucleotide distribution of the mutation process
    print >> out, "mutation nucleotide stationary distribution:"
    for nt, probability in zip(nt_letters, best_mutation_distribution):
        print >> out, "%s : %s" % (nt, probability)
    # write the centered amino acid energies
    print >> out, ""
    print >> out, "amino acid energies:"
    for aa, energy in zip(aa_letters, result_aa_energies):
        print >> out, "%s : %s" % (aa, energy)
    # return the response
    return out.getvalue()
Exemplo n.º 9
0
            aa_to_probability)
    A, C, G, T = initial_stationary_guess
    initial_guess = (math.log(C/A), math.log(G/A), math.log(T/A))
    iterations = 20
    try:
        best = scipy.optimize.nonlin.broyden2(objective_function,
                initial_guess, iterations)
    except Exception, e:
        debugging_information = objective_function.get_history()
        raise HandlingError(str(e) + '\n' + debugging_information)
    x, y, z = best
    best_mutation_weights = (1, math.exp(x), math.exp(y), math.exp(z))
    best_mutation_distribution = normalized(best_mutation_weights)
    # Given the mutation distribution and the amino acid distribution,
    # get the stationary distribution.
    result = DirectProtein.get_nt_distribution_and_aa_energies(
            best_mutation_distribution, aa_distribution)
    result_stationary_nt_dist, result_aa_energies = result
    # make a results string
    out = StringIO()
    # write the stationary nucleotide distribution of the mutation process
    print >> out, 'mutation nucleotide stationary distribution:'
    for nt, probability in zip(nt_letters, best_mutation_distribution):
        print >> out, '%s : %s' % (nt, probability)
    # write the centered amino acid energies
    print >> out, ''
    print >> out, 'amino acid energies:'
    for aa, energy in zip(aa_letters, result_aa_energies):
        print >> out, '%s : %s' % (aa, energy)
    # return the response
    return out.getvalue()