MIT License

Copyright (c) 2015 Matthew Aaron Robinson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE


In [28]:
# MIT license
#  written by Matthew Aaron Robinson

def get_allele_counts(freq_AA, freq_Aa, freq_aa):
    """
    Return the number of dominant 'A' alleles
    and the number of recessive 'a' alleles
    in the frequencies.
    """
    num_dominant = freq_AA * 2 + freq_Aa
    num_recessive = freq_aa * 2 + freq_Aa
    return (num_dominant, num_recessive)

def get_pq(freq_AA, freq_Aa, freq_aa):
    """
    Return a 'p' value and 'q'
    value that conforms
    to the Hardy-Weinberg equation
    using frequencies of possible
    genotypes.
    """
    num_dominant, num_recessive = \
        get_allele_counts(freq_AA, freq_Aa, freq_aa)
    total = num_dominant + num_recessive
    
    p = float(num_dominant) / float(total)
    q = float(num_recessive) / float(total)
    
    return (p,q)

def get_hardy_weinberg_sum(freq_AA, freq_Aa, freq_aa):
    """
    Print out a one or approximately one if
    the frequencies are correct.
    Use this in conjuction with an assert
    statement to check data validity.
    """
    p, q = get_pq(freq_AA, freq_Aa, freq_aa)
    return p**2 + 2*p*q + q**2

def print_data(freq_AA, freq_Aa, freq_aa):
    """
    Print out a complete form of known data.
    """
    print("frequency AA:         ", freq_AA)
    print("frequency Aa:         ", freq_Aa)
    print("frequency aa:         ", freq_aa)
    
    num_dominant, num_recessive = \
        get_allele_counts(freq_AA, freq_Aa, freq_aa)
    print("frequency dominant:   ", num_dominant)
    print("frequency recessive:  ", num_recessive)
    
    p, q = get_pq(freq_AA, freq_Aa, freq_aa)
    print("p = ", p)
    print("q = ", q)
    
    sumpq = p**2 + 2*p*q + q**2
    print("Hardy-Weinberg check: ", sumpq)

In [29]:
freq_AA = 6
freq_Aa = 9
freq_aa = 1

NUM_STUDENTS = 16

print_data(freq_AA, freq_Aa, freq_aa)

assert freq_AA + freq_Aa + freq_aa == NUM_STUDENTS,\
    "Totals are not correct according to class data."
assert get_hardy_weinberg_sum(
    freq_AA, freq_Aa, freq_aa)==1.0,\
    "Hardy-Weinberg relationship with these numbers does not produce one!"


('frequency AA:         ', 6)
('frequency Aa:         ', 9)
('frequency aa:         ', 1)
('frequency dominant:   ', 21)
('frequency recessive:  ', 11)
('p = ', 0.65625)
('q = ', 0.34375)
('Hardy-Weinberg check: ', 1.0)

In [ ]: