@@ -0,0 +1,2924 @@
+# -*- coding: utf-8 -*-%0A%0A%22%22%22Chemistry utilities for BEL that require RDKit.%22%22%22%0A%0Aimport itertools as itt%0Afrom typing import Iterable, Mapping, Tuple%0A%0Aimport requests%0Afrom rdkit import Chem, DataStructs%0Afrom rdkit.Chem import MACCSkeys%0Afrom tqdm import tqdm%0A%0Afrom pybel import Abundance, BELGraph, BaseAbundance%0A%0A__all__ = %5B%0A    'add_chemical_similarities',%0A%5D%0A%0A%0Adef add_chemical_similarities(graph: BELGraph, cutoff: float = 0.7) -%3E None:%0A    %22%22%22Add chemical similarities to the graph.%22%22%22%0A    node_to_smiles = get_node_to_smiles(graph)%0A    similarities = get_similarity(node_to_smiles)%0A    for (source, target), similarity in tqdm(similarities.items(), desc='Updating graph'):%0A        if cutoff %3C similarity:%0A            graph.add_unqualified_edge(source, target, 'chemical_similarity')%0A%0A%0Adef get_node_to_smiles(graph: BELGraph) -%3E Mapping%5BBaseAbundance, str%5D:%0A    %22%22%22Get a mapping from nodes to SMILES strings.%22%22%22%0A    return dict(_help_iter_node_smiles(graph))%0A%0A%0Adef _help_iter_node_smiles(graph: BELGraph) -%3E Iterable%5BTuple%5BAbundance, str%5D%5D:%0A    for node in graph:%0A        if not isinstance(node, Abundance):%0A            continue%0A        if node.namespace.lower() == 'pubchem.compound':%0A            yield node, cid_to_smiles(node.identifier)%0A        elif node.namespace.lower() == 'smiles':%0A            yield node, node.identifier%0A%0A%0Adef cid_to_smiles(pubchem_id: str) -%3E str:%0A    %22%22%22Get the SMILES for chemicals in PubChem database.%0A%0A    :param pubchem_id: PubChem compound identifier%0A    :return: SMILES%0A    %22%22%22%0A    url = f%22http://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/%7Bpubchem_id%7D/property/canonicalSMILES/TXT%22%0A    response = requests.get(url)%0A    return response.text%0A%0A%0Adef get_similarity(node_to_smiles: Mapping%5BBaseAbundance, str%5D):%0A    %22%22%22Get the similarities between all pair combinations of chemicals in the list.%0A%0A    :param node_to_smiles: a dictionary with nodes as keys and smiles as values%0A    :return: a dictionary with the pair chemicals as key and similarity calculation as value%0A    %22%22%22%0A    fps = get_fingerprints(node_to_smiles)%0A    return %7B%0A        (source, target): DataStructs.FingerprintSimilarity(mol_1, mol_2)%0A        for (source, mol_1), (target, mol_2) in%0A        tqdm(itt.combinations(fps.items(), 2), desc='Calculating Similarities')%0A    %7D%0A%0A%0Adef get_fingerprints(node_to_smiles: Mapping%5BBaseAbundance, str%5D):%0A    %22%22%22Create a dictionary containing the fingerprints for every chemical.%0A%0A    :param node_to_smiles: a dictionary with nodes as keys and smiles as values%0A    :return: a dictionary with nodes as keys and the MACCSkeys fingerprints%0A    %22%22%22%0A    node_to_fingerprint = %7B%7D%0A    for pubchem_id, smiles in tqdm(node_to_smiles.items(), desc='Getting fingerprints'):%0A        mol_from_smile = Chem.MolFromSmiles(smiles)%0A        if mol_from_smile is None:%0A            continue%0A        node_to_fingerprint%5Bpubchem_id%5D = MACCSkeys.GenMACCSKeys(mol_from_smile)%0A    return node_to_fingerprint%0A

Add chemistry utilities