forked from dfhampshire/RInChI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrinchi_changes.py
executable file
·188 lines (165 loc) · 8.81 KB
/
rinchi_changes.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python3
"""
RInChI Changes Analysis Script
------------------------------
This script analyses RInChI(s) for changes in properties.
Modifications:
- C. Allen
- B. Hammond 2014
- D.F. Hampshire 2017
Interface completely rewritten. New features added. Based on prior work.
"""
import argparse
import json
from collections import Counter
from rinchi_tools import Molecule, Reaction, _external, database, utils
from rinchi_tools.utils import Hashable
def changes_ops(args, parser):
"""
Executes the changes operations.
Args:
args: The output of the ``parser.parse_args()``. The command line arguments.
parser: An ``ArgumentParser`` object
"""
if args.key:
try:
if args.input.startswith("Long-RInChIKey"):
args.input = database.sql_key_to_rinchi(args.input, _external.RINCHI_DATABASE, args.arg2)
args.rinchi = True
else:
raise ValueError
except ValueError:
raise ValueError("Could not find Long-RInChIKey in database")
if args.rinchi:
# Opens the file if the input is file containing a single RInChI. Otherwise it treats the input as RInChI itself
if args.filein:
try:
with open(args.input) as f:
args.input = f.readline()
print(args.input)
if args.list:
print(args.input)
except IOError:
pass
r = Reaction(args.input)
if args.ringcount:
out = r.change_across_reaction(Molecule.get_ring_count)
print(utils.counter_to_print_string(out, "Ring Count"))
if args.formula:
out = r.change_across_reaction(Molecule.get_formula)
print(utils.counter_to_print_string(out, "Formula"))
if args.valence:
out = r.change_across_reaction(Molecule.get_valence_count)
print(utils.counter_to_print_string(out, "Valence Count"))
if args.hybrid:
out = r.change_across_reaction(Molecule.get_hybrid_count)
print(utils.counter_to_print_string(out, "Hybridisation Count"))
if args.ringcountelements:
out = r.change_across_reaction(Molecule.get_ring_count_inc_elements)
print(utils.counter_to_print_string(out, "Ring Count Elements"))
if args.ringcountold:
out = r.ring_change()
print(utils.counter_to_print_string(out, "Ring Count (Old Method)"))
if args.stereoold:
out = r.stereo_change(args.stereoold.get('wd', None), args.stereoold.get('sp2', None),
args.stereoold.get('sp3', None))
print(utils.counter_to_print_string(out, "Stereocentre Count (Old Method)"))
elif args.batch:
master_counter = {'ringcount': Counter(), 'formula': Counter(), 'ringcountelements': Counter(),
'valence': Counter(), 'hybrid': Counter(), 'ringcount_old': Counter(),
'stereo_old': Counter()}
with open(args.input) as data:
for rinchi in data:
r = Reaction(rinchi)
if args.list:
print('\n__________________\n')
print(rinchi.strip())
if args.ringcount:
# Count the change in ring populations across the reactions
ringcount = r.change_across_reaction(Molecule.get_ring_count)
if ringcount and args.list:
print(utils.counter_to_print_string(ringcount, "Ring Count"))
if ringcount:
master_counter['ringcount'][Hashable(ringcount)] += 1
if args.ringcountelements:
# Count the change in rings returning the change in elemental structure of the rings e.g. (
# CCCCCN : 1) would indicate the reaction forms a pyridine ring
ringcountelements = r.change_across_reaction(Molecule.get_ring_count_inc_elements)
if ringcountelements and args.list:
print(utils.counter_to_print_string(ringcountelements, "Ring Count Elements"))
if ringcountelements:
master_counter['ringcountelements'][Hashable(ringcountelements)] += 1
if args.formula:
formula = r.change_across_reaction(Molecule.get_formula)
if formula and args.list:
print(utils.counter_to_print_string(formula, "Formula Change"))
if formula:
master_counter['formula'][Hashable(formula)] += 1
if args.valence:
valence = r.change_across_reaction(Molecule.get_valence_count)
if valence and args.list:
print(utils.counter_to_print_string(valence, "Valence Count"))
if valence:
master_counter['valence'][Hashable(valence)] += 1
if args.hybrid:
hybrid = r.change_across_reaction(Molecule.get_hybrid_count)
if hybrid and args.list:
print(utils.counter_to_print_string(hybrid, "Hybridisation Change Count"))
if hybrid:
master_counter['hybrid'][Hashable(hybrid)] += 1
if args.ringcountold:
ringcountold = r.ring_change()
if ringcountold and args.list:
print(utils.counter_to_print_string(ringcountold, "Ring Count (Old Method)"))
if ringcountold:
master_counter['ringcount_old'][Hashable(ringcountold)] += 1
if args.stereoold: # TODO fix this
stereoold = r.stereo_change(args.stereoold.get('wd', None), args.stereoold.get('sp2', None),
args.stereoold.get('sp3', None))
if stereoold and args.list:
print(utils.counter_to_print_string(stereoold, "Stereo Change Count (Old Method)"))
if stereoold:
master_counter['stereo_old'][Hashable(stereoold)] += 1
print('\nStats\n-----')
for key, value in master_counter.items():
if value:
print(utils.counter_to_print_string(value, key))
else:
parser.print_help()
def add_changes(subparser):
"""
Adds the arguments for the changes operation to the ``ArgumentParser`` object.
Args:
subparser: An ``ArgumentParser`` object
"""
assert isinstance(subparser, argparse.ArgumentParser)
subparser.add_argument("input", help="The file or string containing RInChI(s) or Long Key to be processed")
# Add process arguments
action = subparser.add_mutually_exclusive_group(required=True)
action.add_argument('-b', '--batch', action='store_true', help='Process multiple RInChIs')
action.add_argument('-r', '--rinchi', action='store_true', help='Process a single RInChI')
action.add_argument("-k", "--key", action="store_true", help="Process a RInChI key")
# Add file options
file_opt = subparser.add_argument_group("File options")
file_opt.add_argument("--list", action="store_true",
help="List RInChIs along with results. Otherwise returns count populations")
file_opt.add_argument("--filein", action="store_true", help="Assert that the input is a file")
# Add operation arguments
operation = subparser.add_argument_group("Operation")
operation.add_argument("--ringcount", action="store_true", help="Change in ring populations by size")
operation.add_argument("--formula", action="store_true", help="Change in formula across a reaction")
operation.add_argument("--valence", action="store_true", help="Change in valence across reaction")
operation.add_argument("--hybrid", action="store_true", help="Change in hybridisation of C atoms across reaction")
operation.add_argument("--ringcountelements", action="store_true",
help="Change in ring populations by ring elements")
operation.add_argument("--ringcountold", action="store_true", help="Change in ring populations. Old method")
operation.add_argument("--stereoold", nargs='?', type=json.loads,
help="Change stereocentres. Old method. Takes an argument as a dictionary "
"such as {'sp2':True,'sp3':False,'wd':True} for options to "
"1. Count sp2 centres 2. Count sp3 centre 3. Well defined stereocentres only")
if __name__ == "__main__":
role = "RInChI Analysis and Manipulation"
parser = argparse.ArgumentParser(description=role)
add_changes(parser)
args = parser.parse_args()
changes_ops(args, parser)