-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_smiles.py
28 lines (21 loc) · 939 Bytes
/
csv_smiles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import csv
from rdkit.Chem import MolFromSmiles
from io import StringIO
from smiles import MOLECULE_PROPERTIES
CONDENSED_NAMES = ['Solubility', 'logP', 'Mwt', 'PSA', 'Rot.Bonds']
def get_csv_from_smiles(smiles_list, options):
# CSV writer expects a file object, not a string.
# StringIO can be used to store a string as a file-like object.
string_file = StringIO()
writer = csv.DictWriter(string_file, fieldnames=['SMILES', *CONDENSED_NAMES])
writer.writeheader()
for smiles in smiles_list:
molecule = MolFromSmiles(smiles)
row = {'SMILES': smiles}
if molecule is None:
row['SMILES'] = f"(invalid){smiles}"
for index, (key, value) in enumerate(MOLECULE_PROPERTIES.items()):
if key in options:
row[CONDENSED_NAMES[index]] = round(value(molecule), int(options['precision']))
writer.writerow(row)
return string_file.getvalue()