# Path: e3fp_paper/sea_utils/util.py
# def targets_to_dict(targets_file, affinity=None):
#     """Read targets file to a dict. Optionally filter by affinity."""
#     targets_dict = read_targets(targets_file)
#     if affinity is not None:
#         filtered_targets_dict = dict([(k, v) for k, v
#                                       in targets_dict.iteritems()
#                                       if str(k.group) == str(affinity)])
#         targets_dict = filtered_targets_dict
#     return targets_dict
# 
# def dict_to_targets(targets_file, targets_dict):
#     """Write targets dict to file."""
#     write_targets(targets_file, targets_dict)


import os
import cPickle as pkl
import pandas as pd
from itertools import product
from rdkit.Chem import MolToSmiles
from rdkit.Chem.rdMolTransforms import CanonicalizeMol
from seacore.util.library import SetValue
from python_utilities.io_tools import smart_open, touch_dir
from e3fp.conformer.util import smiles_to_dict, dict_to_smiles, \
    mol_from_smiles, mol_from_mol2, mol_to_sdf, mol_to_standardised_mol
from e3fp_paper.sea_utils.util import targets_to_dict, dict_to_targets
    # Save query ligands to SDF files and get SMILES
    pdb_smiles_dict = {}
    skip_pairs = set()
    touch_dir(PDB_CONF_DIR)
    for mol_name, mol2_file in pdb_mol2_files.items():
        mol = mol_from_mol2(mol2_file, mol_name)
        smiles = MolToSmiles(mol, isomericSmiles=True)
        pdb_smiles_dict[mol_name] = smiles
        CanonicalizeMol(mol)
        sdf_file = os.path.join(PDB_CONF_DIR, "{}.sdf.bz2".format(mol_name))
        mol_to_sdf(mol, sdf_file)
    dict_to_smiles(PDB_SMILES_FILE, pdb_smiles_dict)

    # Build filtered CHEMBL targets dict
    chembl_smiles_cids = {k.split("-")[0] for k in chembl_smiles_dict.keys()}
    chembl_cids = set()
    if not os.path.isfile(CHEMBL_TARGETS):
        filtered_chembl_targets_dict = {}
        for k, v in pdb_targets_dict.items():
            chembl_set_value = chembl_targets_dict[k]
            chembl_pdb_cids = set.union(*[
                set(pdb_to_chembl_mol_map.get(x, [])) for x in v.cids])
            cids = [x for x in chembl_set_value.cids
                    if (x not in chembl_pdb_cids and
                        x in chembl_smiles_cids)]
            chembl_cids.update(set(cids))
            set_value = SetValue(chembl_set_value.name, cids,
                                 chembl_set_value.description)
            filtered_chembl_targets_dict[k] = set_value


# Path: e3fp_paper/sea_utils/util.py
# def targets_to_dict(targets_file, affinity=None):
#     """Read targets file to a dict. Optionally filter by affinity."""
#     targets_dict = read_targets(targets_file)
#     if affinity is not None:
#         filtered_targets_dict = dict([(k, v) for k, v
#                                       in targets_dict.iteritems()
#                                       if str(k.group) == str(affinity)])
#         targets_dict = filtered_targets_dict
#     return targets_dict
# 
# def dict_to_targets(targets_file, targets_dict):
#     """Write targets dict to file."""
#     write_targets(targets_file, targets_dict)


# Path: project/pdb_enrichment/create_inputs.py
import os
import cPickle as pkl
import pandas as pd
from itertools import product
from rdkit.Chem import MolToSmiles
from rdkit.Chem.rdMolTransforms import CanonicalizeMol
from seacore.util.library import SetValue
from python_utilities.io_tools import smart_open, touch_dir
from e3fp.conformer.util import smiles_to_dict, dict_to_smiles, \
    mol_from_smiles, mol_from_mol2, mol_to_sdf, mol_to_standardised_mol
from e3fp_paper.sea_utils.util import targets_to_dict, dict_to_targets

    # Save query ligands to SDF files and get SMILES
    pdb_smiles_dict = {}
    skip_pairs = set()
    touch_dir(PDB_CONF_DIR)
    for mol_name, mol2_file in pdb_mol2_files.items():
        mol = mol_from_mol2(mol2_file, mol_name)
        smiles = MolToSmiles(mol, isomericSmiles=True)
        pdb_smiles_dict[mol_name] = smiles
        CanonicalizeMol(mol)
        sdf_file = os.path.join(PDB_CONF_DIR, "{}.sdf.bz2".format(mol_name))
        mol_to_sdf(mol, sdf_file)
    dict_to_smiles(PDB_SMILES_FILE, pdb_smiles_dict)

    # Build filtered CHEMBL targets dict
    chembl_smiles_cids = {k.split("-")[0] for k in chembl_smiles_dict.keys()}
    chembl_cids = set()
    if not os.path.isfile(CHEMBL_TARGETS):
        filtered_chembl_targets_dict = {}
        for k, v in pdb_targets_dict.items():
            chembl_set_value = chembl_targets_dict[k]
            chembl_pdb_cids = set.union(*[
                set(pdb_to_chembl_mol_map.get(x, [])) for x in v.cids])
            cids = [x for x in chembl_set_value.cids
                    if (x not in chembl_pdb_cids and
                        x in chembl_smiles_cids)]
            chembl_cids.update(set(cids))
            set_value = SetValue(chembl_set_value.name, cids,
                                 chembl_set_value.description)
            filtered_chembl_targets_dict[k] = set_value


        dict_to_targets(CHEMBL_TARGETS, filtered_chembl_targets_dict)