-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraph_Theory_Approach.py
More file actions
157 lines (107 loc) · 3.15 KB
/
Graph_Theory_Approach.py
File metadata and controls
157 lines (107 loc) · 3.15 KB
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
import subprocess
import json
import itertools
import networkx as nx
from rdkit import Chem
MAX_VALENCE = {
"C": 4,
"O":2,
"N":3
}
def degree_of_unsaturation(C, H, N):
DOU=int((2*C+2-H+N)/2)
return (2*C + 2 - H) // 2
def generate_topologies(n_atoms, max_degree):
"""
Uses geng to generate all connected, non-isomorphic graphs
"""
cmd = ["geng", "-c", f"-D{max_degree}", str(n_atoms)]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, text=True)
# These lines are for testing purposes DO NOT REMOVE
# cmd = ["/usr/local/bin/geng", "-c", f"-D{max_degree}", str(n_atoms)]
# proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, text=True)
graphs = []
for line in proc.stdout:
G = nx.from_graph6_bytes(line.strip().encode())
graphs.append(G)
return graphs
def valid_bond_assignments(G, dou):
"""
Enumerate all valid bond-order assignments for a topology
"""
V = G.number_of_nodes()
E = G.number_of_edges()
rings = E - V + 1
pi_needed = dou - rings
if pi_needed < 0:
return []
edges = list(G.edges())
valid = []
for double_edges in itertools.combinations(edges, pi_needed):
bond_order = {e: 1 for e in edges}
for e in double_edges:
bond_order[e] = 2
valence = {i: 0 for i in G.nodes()}
for (i, j), order in bond_order.items():
valence[i] += order
valence[j] += order
if all(valence[i] <= 4 for i in valence):
valid.append(bond_order)
return valid
def graph_to_mol(G, bond_orders):
mol = Chem.RWMol()
for _ in G.nodes():
mol.AddAtom(Chem.Atom("C"))
for (i, j), order in bond_orders.items():
if order == 1:
mol.AddBond(i, j, Chem.BondType.SINGLE)
elif order == 2:
mol.AddBond(i, j, Chem.BondType.DOUBLE)
mol = mol.GetMol()
try:
Chem.SanitizeMol(mol)
except:
return None
return mol
def generate_isomers(C, H, N, outfile):
dou = degree_of_unsaturation(C, H,N)
max_degree = 4
graphs = generate_topologies(C, max_degree)
smiles_set = set()
molecules = []
for G in graphs:
assignments = valid_bond_assignments(G, dou)
for bond_orders in assignments:
mol = graph_to_mol(G, bond_orders)
if mol is None:
continue
smi = Chem.MolToSmiles(mol, canonical=True)
if smi in smiles_set:
continue
smiles_set.add(smi)
molecules.append(smi)
with open(outfile, "w") as f:
json.dump(molecules, f, indent=2)
print(f"Generated {len(molecules)} isomers")
import sys
import re
X=sys.argv[1]
# PNG_PATH=sys.argv[2]
tokens = re.findall(r'([CHON])(\d*)', X)
c = 0
h = 0
o=0
n=0
for elem, num in tokens:
count = int(num) if num else 1
if elem == 'C':
c = count
elif elem == 'H':
h = count
elif elem=="O":
o=count
elif elem=="N":
n=count
if __name__ == "__main__":
generate_isomers(C=c, H=h,N=n, outfile="molecules.json")
# subprocess.run(["python", "Depicter.py",PNG_PATH])