"""
Parse the fingerprint output of babel. 

e.g. you just ran

babel -ismi 123591_smiles.smi -ofpt -xfMACCS -xs > 123591_maccs.fpt

"""

import os
import sys
import csv

bit_dict = dict()

outfh = open('stitch_maccs_bits.fpt','w')
writer = csv.writer(outfh)

for line in open('stitch_maccs.fpt'):
    aesea_id = line.split('\t')[0][1:]
    
    bits = [x.split(':') for x in line.strip().split('\t')][1:]
    
    for pos, desc in bits:
        pos = int(pos)
        if not pos in bit_dict:
            bit_dict[pos] = desc.strip()
    
    bit_string = ' '.join([x[0] for x in bits])
    writer.writerow([aesea_id, bit_string])

outfh.close()

outfh = open('bit_dictionary.csv','w')
writer = csv.writer(outfh)

for pos in sorted(bit_dict.keys()):
    writer.writerow([pos, bit_dict[pos]])

outfh.close()
    