comparison utils.py @ 0:bb5365381c8f draft

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
author bgruening
date Wed, 02 Oct 2019 12:57:54 -0400
parents
children 9b48456a96fe
comparison
equal deleted inserted replaced
-1:000000000000 0:bb5365381c8f
1 #!/usr/bin/env python
2 """
3 Utility functions for SuCOS and other RDKit modules
4 """
5
6 from __future__ import print_function
7 import sys, gzip
8 from rdkit import Chem
9
10 def log(*args, **kwargs):
11 """Log output to STDERR
12 """
13 print(*args, file=sys.stderr, **kwargs)
14
15 def open_file_for_reading(filename):
16 """Open the file gunzipping it if it ends with .gz."""
17 if filename.lower().endswith('.gz'):
18 return gzip.open(filename, 'rb')
19 else:
20 return open(filename, 'rb')
21
22 def open_file_for_writing(filename):
23 if filename.lower().endswith('.gz'):
24 return gzip.open(filename, 'at')
25 else:
26 return open(filename, 'w+')
27
28 def read_single_molecule(filename, index=1, format=None):
29 """Read a single molecule as a RDKit Mol object. This can come from a file in molfile or SDF format.
30 If SDF then you can also specify an index of the molecule that is read (default is the first)
31 """
32 mol = None
33 if format == 'mol' or filename.lower().endswith('.mol') or filename.lower().endswith('.mol.gz'):
34 file = open_file_for_reading(filename)
35 mol = Chem.MolFromMolBlock(file.read())
36 file.close()
37 elif format == 'sdf' or filename.lower().endswith('.sdf') or filename.lower().endswith('.sdf.gz'):
38 file = open_file_for_reading(filename)
39 supplier = Chem.ForwardSDMolSupplier(file)
40 for i in range(0,index):
41 if supplier.atEnd():
42 break
43 mol = next(supplier)
44 file.close()
45
46 if not mol:
47 raise ValueError("Unable to read molecule")
48
49 return mol