Mercurial > repos > bgruening > sucos_docking_scoring
annotate utils.py @ 6:b24259b0b48b 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:29:36 +0000 |
parents | 1f19b95adc5c |
children |
rev | line source |
---|---|
0
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
1 #!/usr/bin/env python |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
2 """ |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
3 Utility functions for SuCOS and other RDKit modules |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
4 """ |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
5 |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
6 from __future__ import print_function |
6
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
7 |
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
8 import gzip |
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
9 import sys |
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
10 |
0
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
11 from rdkit import Chem |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
12 |
6
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
13 |
0
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
14 def log(*args, **kwargs): |
6
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
15 """Log output to STDERR""" |
0
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
16 print(*args, file=sys.stderr, **kwargs) |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
17 |
6
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
18 |
0
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
19 def open_file_for_reading(filename): |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
20 """Open the file gunzipping it if it ends with .gz.""" |
6
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
21 if filename.lower().endswith(".gz"): |
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
22 return gzip.open(filename, "rb") |
0
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
23 else: |
6
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
24 return open(filename, "rb") |
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
25 |
0
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
26 |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
27 def open_file_for_writing(filename): |
6
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
28 if filename.lower().endswith(".gz"): |
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
29 return gzip.open(filename, "at") |
0
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
30 else: |
6
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
31 return open(filename, "w+") |
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
32 |
0
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
33 |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
34 def read_single_molecule(filename, index=1, format=None): |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
35 """Read a single molecule as a RDKit Mol object. This can come from a file in molfile or SDF format. |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
36 If SDF then you can also specify an index of the molecule that is read (default is the first) |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
37 """ |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
38 mol = None |
6
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
39 if ( |
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
40 format == "mol" |
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
41 or filename.lower().endswith(".mol") |
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
42 or filename.lower().endswith(".mol.gz") |
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
43 ): |
0
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
44 file = open_file_for_reading(filename) |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
45 mol = Chem.MolFromMolBlock(file.read()) |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
46 file.close() |
6
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
47 elif ( |
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
48 format == "sdf" |
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
49 or filename.lower().endswith(".sdf") |
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
50 or filename.lower().endswith(".sdf.gz") |
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
51 ): |
0
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
52 file = open_file_for_reading(filename) |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
53 supplier = Chem.ForwardSDMolSupplier(file) |
6
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
54 for i in range(0, index): |
0
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
55 if supplier.atEnd(): |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
56 break |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
57 mol = next(supplier) |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
58 file.close() |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
59 |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
60 if not mol: |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
61 raise ValueError("Unable to read molecule") |
1f19b95adc5c
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit ef86cfa5f7ab5043de420511211579d03df58645"
bgruening
parents:
diff
changeset
|
62 |
6
b24259b0b48b
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 05dc325ce687441e5d3bdbdedcc0e3529cd5e070"
bgruening
parents:
0
diff
changeset
|
63 return mol |