def output_per_position_stats(filename, all_contig_data, nof_vcfs):
    """
    Output stats per position in the aligment.

    Parameters:
    -----------
    filename: str
        name of file to write to
    all_contig_data: dict
        all data as described abobe in main
    nof_vcfs: int
        number of samples excluding reference

    Returns:
    --------
    0
    """
    with open(filename, 'wb') as f:
        f.write('contig,position,mutations,n_frac,n_gaps\n')
        for contig, con_data in all_contig_data.iteritems():
            all_contig_pos = set()
            for sam in con_data.keys():
                for x in con_data[sam].values():
                    all_contig_pos.update(x)
            for pos in sorted(all_contig_pos):
                mut = 0.0
                ns = 0.0
                gaps = 0.0
                for sam in con_data.keys():
                    if sam == 'reference':
                        continue
                    for m in ['A', 'C', 'G', 'T']:
                        try:
                            mut += 1.0 if pos in con_data[sam][m] else 0.0
                        except KeyError:
                            pass
                    try:
                        ns += 1.0 if pos in con_data[sam]['N'] else 0.0
                    except KeyError:
                        pass
                    try:
                        gaps += 1.0 if pos in con_data[sam]['-'] else 0.0
                    except KeyError:
                        pass
                f.write('%s,%i,%0.5f,%0.5f,%0.5f\n' % (contig, pos, mut /
                    nof_vcfs, ns / nof_vcfs, gaps / nof_vcfs))
    return 0
