-
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.
- Loading branch information
1 parent
aeda561
commit 44e9599
Showing
3 changed files
with
54 additions
and
48 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,59 @@ | ||
#!/usr/bin/env python | ||
import requests | ||
|
||
|
||
def mol_to_smile_molpro(molecules): | ||
""" | ||
Args: | ||
List | ||
Returns: | ||
Dict | ||
Args: | ||
List | ||
Example: Attached sample_mols.json file | ||
""" | ||
|
||
url = "https://translator.broadinstitute.org/molecular_data_provider/compound/by_id" | ||
url = "https://molepro.transltr.io/molecular_data_provider/compound/by_id" | ||
headers = {"accept": "application/json", "Content-Type": "application/json"} | ||
|
||
data_mol = list(set(molecules)) | ||
smiles = {} | ||
|
||
response = requests.post(url, headers=headers, json=data_mol) | ||
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() | ||
# print(json_response) | ||
collec_url = json_response["url"] | ||
temp_collec_response = requests.get(collec_url) | ||
if response.status_code == 200: | ||
collec_response = temp_collec_response.json() | ||
# print('\n') | ||
# print(collec_response['elements'][0]['identifiers']) | ||
if json_response["size"] > 0: | ||
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()): | ||
# if 'smiles' in collec_response['elements'][i]['identifiers'].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" | ||
|
||
# Recursion: re-attempt to retrieve the SMILES for those (from the initial data_mol) | ||
# for which the retrieval was not successful in the first attempt | ||
if len(list(smiles.keys())) < len(data_mol): | ||
diff = [mol for mol in data_mol if mol not in list(smiles.keys())] | ||
diff_smiles = mol_to_smile_molpro(diff) | ||
if len(diff_smiles) > 0: | ||
smiles.update(diff_smiles) | ||
else: | ||
print(f"Error: {response.status_code} - {response.text}") | ||
# 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