-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from TranslatorSRI/novelty
Novelty
- Loading branch information
Showing
9 changed files
with
661 additions
and
10 deletions.
There are no files selected for viewing
Empty file.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import requests | ||
|
||
|
||
def mol_to_smile_molpro(molecules): | ||
""" | ||
Args: | ||
List | ||
Returns: | ||
Dict | ||
""" | ||
|
||
url = "https://molepro.transltr.io/molecular_data_provider/compound/by_id" | ||
headers = {"accept": "application/json", "Content-Type": "application/json"} | ||
|
||
smiles = {} | ||
|
||
data_mol = list(set(molecules)) | ||
# print(f'init data: {len(data_mol)}') | ||
while data_mol: | ||
# print(f'before: {len(data_mol)}') | ||
data_mol_before = len(data_mol) | ||
response = requests.post(url, headers=headers, json=data_mol) | ||
|
||
if response.status_code == 200: | ||
json_response = response.json() | ||
collec_url = json_response["url"] | ||
temp_collec_response = requests.get(collec_url) | ||
if temp_collec_response.status_code == 200: | ||
collec_response = temp_collec_response.json() | ||
|
||
for i in range(json_response["size"]): | ||
key_list = ["identifiers"] | ||
if set(key_list).issubset(collec_response["elements"][i].keys()): | ||
identifiers = collec_response["elements"][i]["identifiers"] | ||
smile = identifiers.get("smiles", "No SMILES could be found") | ||
smiles[data_mol[i]] = smile | ||
else: | ||
smiles[data_mol[i]] = "No identifiers could be found" | ||
|
||
# Remove molecules with successfully retrieved smiles from data_mol | ||
data_mol = [mol for mol in data_mol if mol not in smiles] | ||
data_mol_after = len(data_mol) | ||
# print(f'after: {len(data_mol)}') | ||
|
||
if data_mol_after == data_mol_before: | ||
break | ||
|
||
else: | ||
print( | ||
f"Error: {temp_collec_response.status_code} - {temp_collec_response.text}" | ||
) | ||
break | ||
else: | ||
print(f"Error: {response.status_code} - {response.text}") | ||
break | ||
|
||
return smiles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
def find_known_results(message): | ||
inferring_sources = [ | ||
"infores:aragorn", | ||
"infores:arax", | ||
"infores:biothings-explorer", | ||
"infores:improving-agent", | ||
"infores:robokop", | ||
"infores:unsecret-agent", | ||
] | ||
known_result_ids = [] | ||
unknown_result_ids = [] | ||
results = message["results"] | ||
knowledge_graph = message["knowledge_graph"] | ||
for idres, result in enumerate(results): | ||
for analysis in result.get("analyses") or []: | ||
for eb in analysis["edge_bindings"].values(): | ||
for element in eb: | ||
edge_id = element["id"] | ||
edge = knowledge_graph["edges"][edge_id] | ||
for source in edge["sources"]: | ||
if source["resource_role"] == "primary_knowledge_source": | ||
if source["resource_id"] not in inferring_sources: | ||
known_result_ids.append(idres) | ||
break | ||
else: | ||
unknown_result_ids.append(idres) | ||
return known_result_ids, unknown_result_ids |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#!/usr/bin/env python | ||
|
||
from rdkit import Chem | ||
from rdkit import DataStructs | ||
from rdkit.Chem import AllChem | ||
|
||
|
||
def find_nearest_neighbors( | ||
unknown_smiles_dict, known_smiles_dict, similarity_cutoff, num_neighbors | ||
): | ||
""" | ||
Returns: | ||
Dict | ||
Args: | ||
unknown_smiles_dict: Dict | ||
known_smiles_dict: Dict | ||
similarity_cutoff: float: 0 | ||
num_neighbors: int: 1 | ||
""" | ||
unknown_smiles = { | ||
key: value | ||
for key, value in unknown_smiles_dict.items() | ||
if value != "No SMILES could be found" | ||
} | ||
known_smiles = { | ||
key: value | ||
for key, value in known_smiles_dict.items() | ||
if value != "No SMILES could be found" | ||
} | ||
|
||
# Convert input SMILES to a molecule | ||
known_mols = {} | ||
for key, value in known_smiles.items(): | ||
known_mol = Chem.MolFromSmiles(value) | ||
if known_mol is None: | ||
raise ValueError("Invalid SMILES string for", key) | ||
else: | ||
known_mols.update({key: known_mol}) | ||
nearest_neighbor_mapping = {} | ||
for unknownkey, value in unknown_smiles.items(): | ||
query_mol = Chem.MolFromSmiles(value) | ||
if query_mol is None: | ||
raise ValueError("Invalid SMILES string") | ||
|
||
# Calculate fingerprints for the query molecule | ||
query_fp = AllChem.GetMorganFingerprint(query_mol, 2) | ||
|
||
# Calculate similarity scores between the query molecule and all molecules in the dataset | ||
similarities = [] | ||
for key, mol in known_mols.items(): | ||
fp = AllChem.GetMorganFingerprint(mol, 2) | ||
similarity = DataStructs.TanimotoSimilarity(query_fp, fp) | ||
similarities.append((key, similarity)) | ||
|
||
# Sort the similarities in descending order | ||
similarities.sort(key=lambda x: x[1], reverse=True) | ||
|
||
# Retrieve the nearest neighbors | ||
neighbors = [] | ||
for i in range(min(num_neighbors, len(similarities))): | ||
index, similarity = similarities[i] | ||
if similarity >= similarity_cutoff: | ||
neighbors.append((index, similarity)) | ||
nearest_neighbor_mapping.update({unknownkey: neighbors}) | ||
return nearest_neighbor_mapping |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters