Exemplo n.º 1
0
def get_response_content(fs):
    # get the mutation process nucleotide distribution
    nt_distribution = SnippetUtil.get_distribution(fs.nucleotides,
            'nucleotide', nt_ordered)
    # get the selection process amino acid energies
    aa_to_energy = SnippetUtil.get_dictionary(fs.aminoacids,
            'amino acid', 'energy', aa_ordered)
    # create the direct protein rate matrix object
    nt_distribution_list = [nt_distribution[nt] for nt in nt_ordered]
    aa_energy_list = [aa_to_energy[aa] for aa in aa_ordered]
    rate_matrix_object = DirectProtein.DirectProteinRateMatrix(fs.kappa,
            nt_distribution_list, aa_energy_list)
    # write the response
    out = StringIO()
    if fs.srm:
        # write the scaled rate matrix
        rate_matrix_object.normalize()
        row_major_rate_matrix = rate_matrix_object.get_row_major_rate_matrix()
        print >> out, MatrixUtil.m_to_string(row_major_rate_matrix)
    elif fs.urm:
        # write the unscaled rate matrix
        row_major_rate_matrix = rate_matrix_object.get_row_major_rate_matrix()
        print >> out, MatrixUtil.m_to_string(row_major_rate_matrix)
    elif fs.cstat:
        # write the codon stationary distribution
        codon_distribution = rate_matrix_object.get_codon_distribution()
        for codon in codons_ordered:
            print >> out, codon, ':', codon_distribution[codon]
    elif fs.astat:
        # write the amino acid stationary distribution
        aa_distribution = rate_matrix_object.get_aa_distribution()
        for aa in aa_ordered:
            print >> out, aa, ':', aa_distribution[aa]
    elif fs.nstat:
        # write the nucleotide stationary distribution
        nt_distribution = rate_matrix_object.get_nt_distribution()
        for nt in nt_ordered:
            print >> out, nt, ':', nt_distribution[nt]
    elif fs.sf:
        # write the rate matrix scaling factor
        print >> out, rate_matrix_object.get_expected_rate()
    # return the response
    return out.getvalue() + '\n'
Exemplo n.º 2
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()