forked from ypevzner/chemspl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchemspl.py
162 lines (137 loc) · 7.15 KB
/
chemspl.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
from rdkit.Chem import AllChem as Chem
import rdkit
from lxml import etree as ET
from lxml import objectify
from lxml.builder import E
from lxml.builder import ElementMaker
import uuid
import numpy as np
from datetime import datetime
import re
#builds RDKit reaction object from reaction SPL file
def read_reaction_spl(spl_file):
et=ET.parse(spl_file,parser=ET.XMLParser(encoding="UTF-8"))
root=et.getroot()
#remove namespace so I don't have to bother with it for now
for elem in root.getiterator():
if not (
isinstance(elem, ET._Comment)
or isinstance(elem, ET._ProcessingInstruction)
):
# Remove a namespace URI in the element's name
elem.tag = ET.QName(elem).localname
root_xml=ET.tostring(et)
for reaction in root.xpath("./component/structuredBody/component/section/subject2/specification/component/processStep/component/processStep"):
interactors=reaction.xpath("./interactor")
reactants_molfiles=[]
products_molfiles=[]
for interactor in interactors:
interactor_molfile = interactor.xpath("./identifiedSubstance/identifiedSubstance/moiety/subjectOf/characteristic/value[@mediaType='application/x-mdl-molfile']")[0].text
if interactor.get("typeCode") == "CSM":
reactants_molfiles.append(interactor_molfile)
elif interactor.get("typeCode") == "PRD":
products_molfiles.append(interactor_molfile)
rxn = Chem.ChemicalReaction()
for reactant_molfile in reactants_molfiles:
rxn.AddReactantTemplate(Chem.MolFromMolBlock(reactant_molfile))
for product_molfile in products_molfiles:
rxn.AddProductTemplate(Chem.MolFromMolBlock(product_molfile))
return rxn
urn="urn:hl7-org:v3"
xsi = "http://www.w3.org/2001/XMLSchema-instance"
schemaLocation="urn:hl7-org:v3 https://www.accessdata.fda.gov/spl/schema/spl.xsd"
def numbers_only(val):
result = re.sub('[^0-9]','', val)
return result
def make_zero_based_index(val):
return int(numbers_only(val))-1
#returns a tuple representing atoms in an inchi canonical order
def get_inchi_atom_order(mol):
inchi_aux_info=Chem.inchi.MolToInchiAndAuxInfo(mol)[1]
atom_order_string = re.search("/N:([0-9,;]+)",inchi_aux_info).group()
return tuple([make_zero_based_index(i) for i in atom_order_string.split(",")])
def create_interactor_spl(reaction, type_code, index_in_reaction,code):
if code == "reactant":
template = reaction.GetReactantTemplate(index_in_reaction)
elif code == "product":
template = reaction.GetProductTemplate(index_in_reaction-reaction.GetNumReactantTemplates())
canonically_ordered_template = Chem.RenumberAtoms(template, get_inchi_atom_order(template))
nsmap={None: urn, 'xsi': xsi}
qname=ET.QName(xsi,"type")
molfile_value_element = ET.Element('value',{qname: "ED"})
molfile_value_element.set("mediaType","application/x-mdl-molfile")
molfile_value_element.text = ET.CDATA(Chem.MolToMolBlock(canonically_ordered_template))
inchi_value_element = ET.Element('value',{qname: "ED"})
inchi_value_element.set("mediaType","application/x-inchi")
inchi_value_element.text = Chem.MolToInchi(canonically_ordered_template)
inchikey_value_element = ET.Element('value',{qname: "ED"})
inchikey_value_element.set("mediaType","application/x-inchi-key")
inchikey_value_element.text = Chem.MolToInchiKey(canonically_ordered_template)
return E("interactor",E("priorityNumber",value=str(index_in_reaction)),
E("functionCode",code=code,codeSystem="1.3.6.1.4.1.32366.1.1"),
E("identifiedSubstance",
E("identifiedSubstance",
E("moiety",
E("partMoiety"),
E("subjectOf",
E("characteristic",
E("code",displayName="Chemical Structure",codeSystem="2.16.840.1.113883.3.26.1.1",code="C103240"),
molfile_value_element
)
),
E("subjectOf",
E("characteristic",
E("code",displayName="Chemical Structure",codeSystem="2.16.840.1.113883.3.26.1.1",code="C103240"),
inchi_value_element
)
),
E("subjectOf",
E("characteristic",
E("code",displayName="Chemical Structure",codeSystem="2.16.840.1.113883.3.26.1.1",code="C103240"),
inchikey_value_element
)
)))),
typeCode=type_code)
#creates a string representing an entire SPL document for a given reaction
def make_reaction_spl(rxn):
interactors=[]
for interactor_index in range(rxn.GetNumReactantTemplates()):
interactors.append(create_interactor_spl(rxn, "CSM", interactor_index,"reactant"))
for interactor_index in range(rxn.GetNumReactantTemplates(), rxn.GetNumReactantTemplates()+rxn.GetNumProductTemplates()):
interactors.append(create_interactor_spl(rxn, "PRD", interactor_index,"product"))
reaction_spl = E("component",E("processStep",*interactors))
document_begin = """<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="https://www.accessdata.fda.gov/spl/stylesheet/spl.xsl" type="text/xsl"?>
<document xmlns="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3 https://www.accessdata.fda.gov/spl/schema/spl.xsd">
<id root=\"""" + str(uuid.uuid4()) + """\"/>
<effectiveTime value=\"""" + datetime.now().strftime("%Y%m%d%H%M%S") + """\"/>
<setIf root=\"""" + str(uuid.uuid4()) + """\"/>
<versionNumber value="1"/>
<component>
<structuredBody>
<component>
<section>
<id root=\"""" + str(uuid.uuid4()) + """\"/>
<code/>
<title/>
<text/>
<effectiveTime value="20210928133013"/>
<subject2>
<specification>
<code nullFlavor="NI"/>
<component>
<processStep>
"""
document_end = """ </processStep>
</component>
</specification>
</subject2>
</section>
</component>
</structuredBody>
</component>
</document>"""
ET.cleanup_namespaces(reaction_spl)
ET.indent(reaction_spl, space=" ", level=10)
complete_spl_document = document_begin + ET.tostring(reaction_spl,pretty_print=True).decode().replace('value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"',"value") + document_end
return complete_spl_document