def calc_frequencies(genomes, bp_table, min_cov, min_per):
nucs = ['<STR_LIT:A>', '<STR_LIT:T>', '<STR_LIT>', '<STR_LIT:C>', '<STR_LIT:N>']<EOL>if bp_table is not False:<EOL><INDENT>bp_table = open(bp_table, '<STR_LIT:w>')<EOL>header = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>','<STR_LIT>', '<STR_LIT>','<STR_LIT>', '<STR_LIT>','<STR_LIT:A>', '<STR_LIT:T>', '<STR_LIT>', '<STR_LIT:C>', '<STR_LIT:N>', '<STR_LIT>', '<STR_LIT>']<EOL>print('<STR_LIT:\t>'.join(header), file=bp_table)<EOL><DEDENT>for genome, contigs in list(genomes.items()):<EOL><INDENT>for contig, samples in list(contigs.items()):<EOL><INDENT>for sample, stats in list(samples.items()):<EOL><INDENT>for pos, ps in enumerate(stats['<STR_LIT>'], <NUM_LIT:1>):<EOL><INDENT>coverage = sum([ps[nuc] for nuc in nucs])<EOL>for nuc in nucs:<EOL><INDENT>nuc_cov = ps[nuc]<EOL>if coverage == <NUM_LIT:0>:<EOL><INDENT>nuc_per = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>nuc_per = (float(nuc_cov)/coverage)*<NUM_LIT:100><EOL><DEDENT>if nuc_cov < min_cov or nuc_per < min_per:<EOL><INDENT>del ps[nuc]<EOL><DEDENT><DEDENT>ps = find_consensus(ps)<EOL>genomes[genome][contig][sample][pos] = ps<EOL>if bp_table is not False:<EOL><INDENT>out = [genome, contig, sample, pos]<EOL>for i in ['<STR_LIT>', '<STR_LIT>','<STR_LIT>', '<STR_LIT>','<STR_LIT:A>', '<STR_LIT:T>', '<STR_LIT>', '<STR_LIT:C>', '<STR_LIT:N>','<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>try:<EOL><INDENT>if i == '<STR_LIT>':<EOL><INDENT>out.append(ps[i][<NUM_LIT:0>])<EOL><DEDENT>elif i in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>out.append(len(ps[i]))<EOL><DEDENT>else:<EOL><INDENT>out.append(ps[i])<EOL><DEDENT><DEDENT>except:<EOL><INDENT>out.append('<STR_LIT>')<EOL><DEDENT><DEDENT>print('<STR_LIT:\t>'.join([str(i) for i in out]), file=bp_table)<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>return genomes<EOL>
print bp frequencies to table
genomes = {} # genomes[genome][contig][sample] = {'bp_stats':{}}