00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 import pyp_utils
00022
00023
00024
00025 global BASE_DEMOGRAPHIC_YEAR
00026 global BASE_DEMOGRAPHIC_UNIT
00027 global SEX_CODE_MAP
00028 BASE_DEMOGRAPHIC_YEAR = 1900
00029 BASE_DEMOGRAPHIC_UNIT = 'year'
00030 SEX_CODE_MAP = {'m':'Male','f':'Female','u':'Unk'}
00031
00032
00033
00034
00035
00036
00037 def set_base_year(year=1900):
00038 """
00039 set_base_year() defines a global variable, BASE_DEMOGRAPHIC_YEAR.
00040 """
00041 global BASE_DEMOGRAPHIC_YEAR
00042 BASE_DEMOGRAPHIC_YEAR = year
00043
00044
00045
00046
00047
00048
00049 def set_age_units(units='year'):
00050 """
00051 set_age_units() defines a global variable, BASE_DEMOGRAPHIC_UNIT.
00052 """
00053 _units = ['year','month','day']
00054 global BASE_DEMOGRAPHIC_UNIT
00055 if units in _units:
00056 BASE_DEMOGRAPHIC_UNIT = units
00057 else:
00058 BASE_DEMOGRAPHIC_UNIT = 'year'
00059
00060
00061
00062
00063
00064
00065
00066 def age_distribution(pedobj,sex=1):
00067 """
00068 age_distribution() computes histograms of the age distribution of
00069 males and females in the population. You can also stratify by
00070 sex to get individual histograms.
00071 """
00072 age_dict = {}
00073 age_freq_total = 0.0
00074 if pedobj.pedigree[0].age == -999:
00075 if pedobj.pedigree[0].igen == -999:
00076 pyp_utils.set_generation(pedobj.pedigree)
00077 pyp_utils.set_age(pedobj.pedigree)
00078 if not sex:
00079 for i in range(len(pedobj.pedigree)):
00080 try:
00081 age_dict[pedobj.pedigree[i].age] = age_dict[pedobj.pedigree[i].age] + 1
00082 except KeyError:
00083 age_dict[pedobj.pedigree[i].age] = 1
00084 age_hist = pyp_utils.simple_histogram_dictionary(age_dict)
00085 if pedobj.kw['debug_messages']:
00086 print '-'*80
00087 print 'Population Age Distribution'
00088 print '-'*80
00089 print '\tAge\tCount\tFrequency\tHistogram'
00090 for key in age_dict.keys():
00091 age_freq_total = age_freq_total + float(age_dict[key])/float(len(pedobj.pedigree))
00092 print '\t%s\t%s\t%s\t%s' % (key,age_dict[key],float(age_dict[key])/float(len(pedobj.pedigree)),age_hist[key])
00093 print '\tTOTAL\t%s\t%s' % (len(pedobj.pedigree),age_freq_total)
00094 print '-'*80
00095 else:
00096 males = []
00097 females = []
00098 unknowns = []
00099 male_dict = {}
00100 female_dict = {}
00101 unknown_dict = {}
00102 for i in range(len(pedobj.pedigree)):
00103 if pedobj.pedigree[i].sex == 'm':
00104 males.append(pedobj.pedigree[i])
00105 elif pedobj.pedigree[i].sex == 'f':
00106 females.append(pedobj.pedigree[i])
00107 else:
00108 unknowns.append(pedobj.pedigree[i])
00109 for m in range(len(males)):
00110 try:
00111 male_dict[males[m].age] = male_dict[males[m].age] + 1
00112 except KeyError:
00113 male_dict[males[m].age] = 1
00114 for f in range(len(females)):
00115 try:
00116 female_dict[females[f].age] = female_dict[females[f].age] + 1
00117 except KeyError:
00118 female_dict[females[f].age] = 1
00119 for u in range(len(unknowns)):
00120 try:
00121 unknown_dict[unknowns[u].age] = unknown_dict[unknowns[u].age] + 1
00122 except KeyError:
00123 unknown_dict[unknowns[u].age] = 1
00124 male_hist = pyp_utils.simple_histogram_dictionary(male_dict)
00125 female_hist = pyp_utils.simple_histogram_dictionary(female_dict)
00126 unknown_hist = pyp_utils.simple_histogram_dictionary(unknown_dict)
00127 if pedobj.kw['messages'] == 'verbose':
00128 print '-'*80
00129 print 'Population Age Distribution by Sex'
00130 print '-'*80
00131 age_freq_total = 0.0
00132 print 'Males'
00133 print '\tAge\tCount\tFrequency\tHistogram'
00134 for key in male_dict.keys():
00135 age_freq_total = age_freq_total + float(male_dict[key])/float(len(males))
00136 print '\t%s\t%s\t%s\t%s' % (key,male_dict[key],float(male_dict[key])/float(len(males)),male_hist[key])
00137 print '\tTOTAL\t%s\t%s' % (len(males),age_freq_total)
00138 print '-'*80
00139 age_freq_total = 0.0
00140 print 'Females'
00141 print '\tAge\tCount\tFrequency\tHistogram'
00142 for key in female_dict.keys():
00143 age_freq_total = age_freq_total + float(female_dict[key])/float(len(females))
00144 print '\t%s\t%s\t%s\t%s' % (key,female_dict[key],float(female_dict[key])/float(len(females)),female_hist[key])
00145 print '\tTOTAL\t%s\t%s' % (len(females),age_freq_total)
00146 print '-'*80
00147 age_freq_total = 0.0
00148 print 'Unknowns'
00149 print '\tAge\tCount\tFrequency\tHistogram'
00150 for key in unknown_dict.keys():
00151 age_freq_total = age_freq_total + float(unknown_dict[key])/float(len(unknowns))
00152 print '\t%s\t%s\t%s\t%s' % (key,unknown_dict[key],float(unknown_dict[key])/float(len(unknowns)),unknown_hist[key])
00153 print '\tTOTAL\t%s\t%s' % (len(unknowns),age_freq_total)
00154 print '-'*80
00155
00156
00157
00158
00159
00160
00161 def sex_ratio(pedobj):
00162 """
00163 sex_ratio() returns a dictionary containing the proportion of males and females in
00164 the population.
00165 """
00166 sexratiodict = {}
00167 for s in SEX_CODE_MAP.keys():
00168 sexratiodict[s] = 0
00169 for i in range(len(pedobj.pedigree)):
00170 if sexratiodict.has_key(pedobj.pedigree[i].sex):
00171 sexratiodict[pedobj.pedigree[i].sex] = sexratiodict[pedobj.pedigree[i].sex] + 1
00172 else:
00173 sexratiodict[pedobj.pedigree[i].sex] = 1
00174 if pedobj.kw['messages'] == 'verbose':
00175 print '-'*80
00176 print 'Overall Sex Ratio'
00177 print '-'*80
00178 print '(n = %s)' % (len(pedobj.pedigree))
00179 print 'Sex\tCount\tFrequency'
00180 for s in sexratiodict.keys():
00181 print '%s:\t%s\t%s' % (SEX_CODE_MAP[s],sexratiodict[s],float(sexratiodict[s])/float(len(pedobj.pedigree)))
00182 print '-'*80
00183 if int(sexratiodict['u']) > 0:
00184 marginal = sexratiodict['m'] + sexratiodict['f']
00185 print 'Conditional Sex Ratio'
00186 print '-'*80
00187 print '(n = %s)' % (marginal)
00188 print 'Sex\tCount\tFrequency'
00189 print '%s:\t%s\t%s' % (SEX_CODE_MAP['m'],sexratiodict['m'],float(sexratiodict['m'])/marginal)
00190 print '%s:\t%s\t%s' % (SEX_CODE_MAP['f'],sexratiodict['f'],float(sexratiodict['f'])/marginal)
00191 return sexratiodict
00192
00193
00194
00195
00196
00197
00198
00199 def founders_by_year(pedobj):
00200 """
00201 founders_by_year() returns a dictionary containing the number of founders in each
00202 birthyear.
00203 """
00204 founderbyyeardict = {}
00205 if 'b' not in pedobj.kw['pedformat'] and 'y' not in pedobj.kw['pedformat']:
00206
00207
00208 founderbyyeardict[BASE_DEMOGRAPHIC_YEAR] = pedobj.metadata.num_unique_founders
00209 else:
00210 for _f in pedobj.metadata.unique_founder_list:
00211 _by = pedobj.pedigree[int(_f)-1].by
00212
00213 try:
00214 founderbyyeardict[_by] = founderbyyeardict[_by] + 1
00215 except KeyError:
00216 founderbyyeardict[_by] = 1
00217
00218
00219
00220 _years = founderbyyeardict.keys()
00221 _years.sort()
00222
00223 for _f in range(_years[0],_years[-1]):
00224 try:
00225 _c = founderbyyeardict[_f]
00226 except KeyError:
00227 founderbyyeardict[_f] = 0
00228
00229 return founderbyyeardict