def calc_frequencies(genomes, bp_table, min_cov, min_per):
    """
    print bp frequencies to table
    genomes = {} # genomes[genome][contig][sample] = {'bp_stats':{}}
    """
    nucs = ['A', 'T', 'G', 'C', 'N']
    if bp_table is not False:
        bp_table = open(bp_table, 'w')
        header = ['#genome', 'contig', 'sample', 'position', \
                    'reference', 'ref. frequency', \
                    'consensus', 'con. frequency', \
                    'A', 'T', 'G', 'C', 'N', '# insertions', '# deletions']
        print('\t'.join(header), file=bp_table)
    for genome, contigs in list(genomes.items()):
        for contig, samples in list(contigs.items()):
            for sample, stats in list(samples.items()):
                for pos, ps in enumerate(stats['bp_stats'], 1):
                    coverage = sum([ps[nuc] for nuc in nucs])
                    for nuc in nucs:
                        # make sure support for base passes thresholds
                        nuc_cov = ps[nuc]
                        if coverage == 0:
                            nuc_per = 0
                        else:
                            nuc_per = (float(nuc_cov)/coverage)*100
                        if nuc_cov < min_cov or nuc_per < min_per:
                            del ps[nuc]
                    ps = find_consensus(ps)
                    genomes[genome][contig][sample][pos] = ps
                    if bp_table is not False:
                        out = [genome, contig, sample, pos]
                        for i in ['ref', 'reference frequency', \
                                  'consensus', 'consensus frequency', \
                                  'A', 'T', 'G', 'C', 'N', \
                                  'In', 'Del']:
                            try:
                                if i == 'consensus':
                                    out.append(ps[i][0])
                                elif i in ['In', 'Del']:
                                    out.append(len(ps[i]))
                                else:
                                    out.append(ps[i])
                            except:
                                out.append('n/a')
                        print('\t'.join([str(i) for i in out]), file=bp_table)
    return genomes
def calc_frequencies ( genomes , bp_table , min_cov , min_per ) : nucs = [ 'A' , 'T' , 'G' , 'C' , 'N' ] if bp_table is not False : bp_table = open ( bp_table , 'w' ) header = [ '#genome' , 'contig' , 'sample' , 'position' , 'reference' , 'ref. frequency' , 'consensus' , 'con. frequency' , 'A' , 'T' , 'G' , 'C' , 'N' , '# insertions' , '# deletions' ] print ( '\t' . join ( header ) , file = bp_table ) for genome , contigs in list ( genomes . items ( ) ) : for contig , samples in list ( contigs . items ( ) ) : for sample , stats in list ( samples . items ( ) ) : for pos , ps in enumerate ( stats [ 'bp_stats' ] , 1 ) : coverage = sum ( [ ps [ nuc ] for nuc in nucs ] ) for nuc in nucs : nuc_cov = ps [ nuc ] if coverage == 0 : nuc_per = 0 else : nuc_per = ( float ( nuc_cov ) / coverage ) * 100 if nuc_cov < min_cov or nuc_per < min_per : del ps [ nuc ] ps = find_consensus ( ps ) genomes [ genome ] [ contig ] [ sample ] [ pos ] = ps if bp_table is not False : out = [ genome , contig , sample , pos ] for i in [ 'ref' , 'reference frequency' , 'consensus' , 'consensus frequency' , 'A' , 'T' , 'G' , 'C' , 'N' , 'In' , 'Del' ] : try : if i == 'consensus' : out . append ( ps [ i ] [ 0 ] ) elif i in [ 'In' , 'Del' ] : out . append ( len ( ps [ i ] ) ) else : out . append ( ps [ i ] ) except : out . append ( 'n/a' ) print ( '\t' . join ( [ str ( i ) for i in out ] ) , file = bp_table ) return genomes
print bp frequencies to table
    genomes = {} # genomes[genome][contig][sample] = {'bp_stats':{}}