def write_csv()

in packages/miew/tools/pdbfreq.py [0:0]


def write_csv():
  # create subtotals
  stats_total = defaultdict(int)
  stats_props_total = defaultdict(set)
  for filename, stats, stats_props in files:
    for tag, count in stats.items():
      stats_total[tag] = max(stats_total[tag], count)
    for prop, values in stats_props.items():
      stats_props_total[prop] = stats_props_total[prop].union(values)
  files.append(('.ALL', stats_total, stats_props_total))
  
  # add unique properties to stats
  for filename, stats, stats_props in files:
    for prop, values in stats_props.items():
      stats[prop] = len(values)

  with open('pdbfreq_tags.csv', 'w') as f:
    keys = sorted(stats_total.keys())
    f.write('filename,%s\n' % ','.join(keys))
    for filename, stats, stats_props in sorted(files, key = lambda x: x[0]):
      if 'REMARK 350 BIOMTn' in stats.keys():
        stats['REMARK 350 BIOMTn'] /= 3
      if 'REMARK 290 SMTRYn' in stats.keys():
        stats['REMARK 290 SMTRYn'] /= 3
      f.write('%s,%s\n' % (filename, ','.join(str(stats[key]) for key in keys)))

  hrule = '-' * 78 + '\n'
  with open('pdbfreq_fields.txt', 'w') as f:
    tags = sorted(tag_stats.keys())
    for tag in tags:
      props = tag_props[tag]
      for key in sorted(props.keys()):
        f.write(hrule)
        f.write('%s.%s\n' % (tag, key))
        f.write(hrule)
        values_dict = tag_stats[tag][key]
        values = sorted(values_dict.keys())
        for value in values:
          f.write('%-5s : %s\n' % (value, ', '.join(sorted(values_dict[value]))))