Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new file: bin/mols2html.py #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions bin/mols2html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python3

# create an HTML table to view all molecules in a web browser

import mols2grid, rdkit, sys
from rdkit import Chem

input_fn = sys.argv[1]
output_fn = sys.argv[2]

def sdf_read_mols(fn):
suppl = Chem.SDMolSupplier(fn)
return [mol for mol in suppl]

# in a proper .smi file: the first field is the SMILES string
# what's left on the right of it is the molecule's name
def mol_of_smi_line(line):
strip = line.strip()
words = strip.split()
smi = words[0]
return Chem.MolFromSmiles(smi)

def smi_read_mols(fn):
lines = open(fn, 'r').readlines()
return [mol_of_smi_line(line) for line in lines]

mols = []
# ----- SDF -----
if input_fn.endswith(".sdf"):
# with some CLI options, we could select a subset:
# top 50, i..j, last 50, etc.
mols = sdf_read_mols(input_fn)
# ----- MOL2 -----
elif input_fn.endswith(".mol2"):
print("MOL2 not supported by rdkit!", file=sys.stderr)
exit(1)
# ----- SMILES -----
elif input_fn.endswith(".smi"):
mols = smi_read_mols(input_fn)
else:
print("unsupported file type: %s" % input_fn, file=sys.stderr)
exit(1)

mols2grid.save(mols, output=output_fn, template="table", prerender=True)