comparison utils.py @ 6:b8725fec8c7b draft default tip

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
author bgruening
date Wed, 14 Apr 2021 09:30:48 +0000
parents f80cfac80c53
children
comparison
equal deleted inserted replaced
5:12725d4b90f3 6:b8725fec8c7b
2 """ 2 """
3 Utility functions for SuCOS and other RDKit modules 3 Utility functions for SuCOS and other RDKit modules
4 """ 4 """
5 5
6 from __future__ import print_function 6 from __future__ import print_function
7 import sys, gzip 7
8 import gzip
9 import sys
10
8 from rdkit import Chem 11 from rdkit import Chem
9 12
13
10 def log(*args, **kwargs): 14 def log(*args, **kwargs):
11 """Log output to STDERR 15 """Log output to STDERR"""
12 """
13 print(*args, file=sys.stderr, **kwargs) 16 print(*args, file=sys.stderr, **kwargs)
17
14 18
15 def open_file_for_reading(filename): 19 def open_file_for_reading(filename):
16 """Open the file gunzipping it if it ends with .gz.""" 20 """Open the file gunzipping it if it ends with .gz."""
17 if filename.lower().endswith('.gz'): 21 if filename.lower().endswith(".gz"):
18 return gzip.open(filename, 'rb') 22 return gzip.open(filename, "rb")
19 else: 23 else:
20 return open(filename, 'rb') 24 return open(filename, "rb")
25
21 26
22 def open_file_for_writing(filename): 27 def open_file_for_writing(filename):
23 if filename.lower().endswith('.gz'): 28 if filename.lower().endswith(".gz"):
24 return gzip.open(filename, 'at') 29 return gzip.open(filename, "at")
25 else: 30 else:
26 return open(filename, 'w+') 31 return open(filename, "w+")
32
27 33
28 def read_single_molecule(filename, index=1, format=None): 34 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. 35 """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) 36 If SDF then you can also specify an index of the molecule that is read (default is the first)
31 """ 37 """
32 mol = None 38 mol = None
33 if format == 'mol' or filename.lower().endswith('.mol') or filename.lower().endswith('.mol.gz'): 39 if (
40 format == "mol"
41 or filename.lower().endswith(".mol")
42 or filename.lower().endswith(".mol.gz")
43 ):
34 file = open_file_for_reading(filename) 44 file = open_file_for_reading(filename)
35 mol = Chem.MolFromMolBlock(file.read()) 45 mol = Chem.MolFromMolBlock(file.read())
36 file.close() 46 file.close()
37 elif format == 'sdf' or filename.lower().endswith('.sdf') or filename.lower().endswith('.sdf.gz'): 47 elif (
48 format == "sdf"
49 or filename.lower().endswith(".sdf")
50 or filename.lower().endswith(".sdf.gz")
51 ):
38 file = open_file_for_reading(filename) 52 file = open_file_for_reading(filename)
39 supplier = Chem.ForwardSDMolSupplier(file) 53 supplier = Chem.ForwardSDMolSupplier(file)
40 for i in range(0,index): 54 for i in range(0, index):
41 if supplier.atEnd(): 55 if supplier.atEnd():
42 break 56 break
43 mol = next(supplier) 57 mol = next(supplier)
44 file.close() 58 file.close()
45 59