-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesmerDiag.xsl
469 lines (420 loc) · 19.1 KB
/
mesmerDiag.xsl
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:cml="http://www.xml-cml.org/schema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:me="http://www.chem.leeds.ac.uk/mesmer"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:set="http://exslt.org/sets"
xmlns:exsl="http://exslt.org/common"
xmlns:math="http://exslt.org/math"
extension-element-prefixes="set math"
exclude-result-prefixes="exsl">
<xsl:variable name ="diagheight" select="300"/>
<xsl:variable name ="xleft" select="20"/>
<xsl:variable name="xspacing" select="100"/> <!--x spacing of species-->
<xsl:variable name="spwidth" select="40"/> <!--width of species bars-->
<xsl:variable name="tswidth" select="30"/> <!--width of TS bars-->
<xsl:variable name="dysptitle" select="15"/> <!--downward displacement of species titles-->
<xsl:variable name="dyspenergy" select="-8"/> <!--downward displacement of species energies-->
<xsl:variable name="dytstitle" select="15"/> <!--upward displacement of transition state titles-->
<xsl:variable name="dytsenergy" select="5"/> <!--upward displacement of transition state energies-->
<xsl:variable name="dxtstitle" select="10"/> <!--leftward dx from mid point of reactant/product of ts title-->
<xsl:variable name ="debug">
<xsl:value-of select="count(//me:debugXSLT)"/>
</xsl:variable>
<xsl:variable name="TSEnergies"
select="key('molrefs',//me:transitionState/cml:molecule/@ref)//cml:property[@dictRef='me:ZPE']/cml:scalar" />
<!--The notional energy of a product set without an assigned energy.
This is just a magic number, with a value unlikely to actually occur-->
<xsl:variable name="dummyE" select="99999999"/>
<xsl:key name="molrefs" match="cml:molecule" use="@id"/>
<!--This contains the ref attributes of the first reactant and the first product molecule
for all reactions. Duplicates have been removed.-->
<xsl:variable name="distinctRandP"
select="set:distinct(//cml:reaction//cml:reactant[1]/cml:molecule/@ref
| //cml:reaction//cml:product[1]/cml:molecule/@ref)"/>
<!--Make a temporary tree which has <Energy> elements with the energies of each node in distinctRandP-->
<xsl:variable name="RandPEnergies">
<xsl:for-each select="$distinctRandP">
<!--The type of the current set- 'reactant' or 'product'-->
<xsl:variable name="RorP" select="local-name(../..)"/>
<!--Node set with one or two reactant or product elements-->
<xsl:variable name="bothSpecies"
select="../../../*[local-name()=$RorP]"/>
<xsl:variable name="bothEnergies"
select="key('molrefs',$bothSpecies/cml:molecule/@ref)//cml:property[@dictRef='me:ZPE']/cml:scalar" />
<!--The // in the above expression means that surrounding <cml:property> by a <cml:propertyList> is optional-->
<xsl:element name="Energy">
<xsl:choose>
<!--If the only or both species have energies specified, use their sum.
If not, use assume 0 for an unspecified reactant, and a special dummy value for the sum of products.-->
<xsl:when test="
boolean($RorP = 'reactant')
or
(boolean(key('molrefs',$bothSpecies[1]/cml:molecule/@ref)//cml:property[@dictRef='me:ZPE']/cml:scalar)
and
boolean(key('molrefs',$bothSpecies[count($bothSpecies)]/cml:molecule/@ref)//cml:property[@dictRef='me:ZPE']/cml:scalar))
">
<xsl:value-of select="sum($bothEnergies)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$dummyE"/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="okEnergies" select="exsl:node-set($RandPEnergies)/Energy[. != $dummyE] | $TSEnergies"/>
<xsl:variable name="Emin" select="math:min($okEnergies)"/>
<xsl:variable name="Emax" select="math:max($okEnergies)"/>
<!-- energyOffset (subtracted from each non-zero energy)
If the value of me:diagramEnergyOffset is 0, the lowest energy in the system will be labelled 0.0.
If a species whose energy is now labelled E needs to be set to 0.0, change the value of
me:diagramEnergyOffset to E.-->
<xsl:variable name="energyOffset">
<xsl:choose>
<xsl:when test="//me:diagramEnergyOffset">
<xsl:value-of select="$Emin + //me:diagramEnergyOffset"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="0"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!--Calculate an appropriate vertical scaling factor-->
<xsl:variable name="yscale">
<xsl:value-of select="($diagheight - 90) div ($Emax - $Emin)"/>
</xsl:variable>
<xsl:variable name ="ybase" select="$diagheight+($Emin * $yscale) - 60"/>
<xsl:template name="drawDiag" match="me:mesmer">
<xsl:variable name="wellsRatio">
<xsl:choose>
<xsl:when test="(count($distinctRandP) div 10) > 1.0">
<xsl:value-of select="count($distinctRandP) div 10"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<svg:svg version="1.1" font-family="Verdana" width="100%" preserveAspectRatio="none">
<xsl:attribute name="height">
<xsl:value-of select="$diagheight * $wellsRatio + 60" />
</xsl:attribute>
<!--Have viewBox only with large number of wells-->
<xsl:if test="$wellsRatio > 1.0">
<xsl:attribute name="viewBox">
<xsl:value-of select="
concat(0, ' ', 0, ' ',
count($distinctRandP)*$xspacing + 40, ' ', $diagheight + 60)" />
</xsl:attribute>
</xsl:if>
<xsl:if test="$debug!=0">
<xsl:call-template name="test3"/>
</xsl:if>
<!--Draw the energy levels of the modelled molecules-->
<xsl:call-template name="drawWells"/>
<!--Draw the energy levels of the transition states and the lines to reactant and product-->
<xsl:apply-templates select="cml:reactionList" mode="diagram"/>
<svg:text x="20" font-size="9">
<xsl:attribute name="y">
<xsl:value-of select="$diagheight"/>
</xsl:attribute>
<xsl:value-of select="'Energies in '"/>
<xsl:value-of select="//cml:property[@dictRef='me:ZPE']/cml:scalar/@units"/>
</svg:text>
</svg:svg>
</xsl:template>
<!--===================================================================-->
<xsl:template name="drawWells">
<svg:g style="stroke:teal;"> <!--All wells-->
<xsl:for-each select="$distinctRandP">
<xsl:variable name="pos" select="position()"/>
<!--The type of the current set- 'reactant' or 'product'-->
<xsl:variable name="RorP" select="local-name(../..)"/>
<!--Node set with one or two reactant or product elements-->
<xsl:variable name="bothSpecies"
select="../../../*[local-name()=$RorP]"/>
<xsl:variable name="bothEnergies"
select="key('molrefs',$bothSpecies/cml:molecule/@ref)//cml:property[@dictRef='me:ZPE']/cml:scalar" />
<xsl:variable name="yval">
<xsl:choose>
<xsl:when test="exsl:node-set($RandPEnergies)/Energy[number($pos)] != $dummyE">
<xsl:value-of select="$ybase - $yscale * exsl:node-set($RandPEnergies)/Energy[number($pos)]"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$diagheight - 100"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<svg:g> <!--This well-->
<xsl:if test="key('molrefs', .)/@active='false'">
<xsl:attribute name="class">inactive</xsl:attribute>
</xsl:if>
<!--Write names of species-->
<svg:text font-size="9" >
<xsl:attribute name="x" >
<xsl:number value="(position()-1)* $xspacing+ $xleft" />
</xsl:attribute>
<xsl:attribute name="y" >
<xsl:number value="$yval+$dysptitle" />
</xsl:attribute>
<xsl:for-each select="$bothSpecies">
<xsl:value-of select="cml:molecule/@ref"/>
<xsl:if test="position()!=last()">
<xsl:text> + </xsl:text>
</xsl:if>
</xsl:for-each>
</svg:text>
<!--Write energies-->
<xsl:if test="exsl:node-set($RandPEnergies)/Energy[number($pos)] != $dummyE">
<xsl:variable name="EWell" select="sum($bothEnergies)" />
<svg:text font-size="8" >
<xsl:attribute name="x" >
<xsl:number value="(position()-1)* $xspacing+ $xleft + 0.1 * $xspacing"/>
</xsl:attribute>
<xsl:attribute name="y" >
<xsl:number value="$yval+$dyspenergy" />
</xsl:attribute>
<xsl:value-of select="format-number($EWell - $energyOffset,'#.0')" />
</svg:text>
<!--Draw horizontal bar at the appropriate energy-->
<svg:line style="stroke-width:3">
<xsl:attribute name="x1">
<xsl:number value="(position()-1)* $xspacing+ $xleft" />
</xsl:attribute>
<xsl:attribute name="y1">
<xsl:value-of select="$yval"/>
</xsl:attribute>
<xsl:attribute name="x2">
<xsl:number value="(position()-1)* $xspacing+ $spwidth + $xleft"/>
</xsl:attribute>
<xsl:attribute name="y2">
<xsl:value-of select="$yval"/>
</xsl:attribute>
</svg:line>
</xsl:if>
</svg:g>
</xsl:for-each>
</svg:g>
</xsl:template>
<!--===================================================================-->
<xsl:template match="cml:reactionList" mode="diagram">
<xsl:for-each select="cml:reaction">
<xsl:variable name="reactantmol" select="key('molrefs', .//cml:reactant/cml:molecule/@ref)" />
<xsl:variable name="productmol" select="key('molrefs', .//cml:product/cml:molecule/@ref)" />
<xsl:variable name="TSmol" select="key('molrefs', me:transitionState/cml:molecule/@ref)" />
<xsl:variable name="EnergyTStemp" select="$TSmol//cml:property[@dictRef='me:ZPE']/cml:scalar"/>
<xsl:variable name="reactantIndex">
<xsl:for-each select="$distinctRandP">
<xsl:if test=".=$reactantmol/@id">
<xsl:value-of select="position()"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="reactantpos"><!-- middle of bar-->
<xsl:value-of select="($reactantIndex - 1)* $xspacing+ $xleft + $spwidth*0.5"/>
</xsl:variable>
<xsl:variable name="productIndex">
<xsl:for-each select="$distinctRandP">
<xsl:if test=".=$productmol/@id">
<xsl:value-of select="position()"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="productpos"><!-- middle of bar-->
<xsl:value-of select="($productIndex - 1)* $xspacing+ $xleft + $spwidth*0.5"/>
</xsl:variable>
<!-- offsets -ve when reactants are to the left of the products-->
<xsl:variable name="spoffset">
<xsl:choose>
<xsl:when test="$productpos - $reactantpos < 0">
<xsl:value-of select="-$spwidth*0.5"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$spwidth*0.5"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="tsoffset">
<xsl:choose>
<xsl:when test="$productpos - $reactantpos < 0">
<xsl:value-of select="-$tswidth*0.5"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$tswidth*0.5"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="yTStemp" select="$ybase - $yscale * $TSmol//cml:property[@dictRef='me:ZPE']/cml:scalar"/>
<xsl:variable name="isILT" select="me:MCRCMethod='MesmerILT' or me:MCRCMethod='SimpleILT'"/>
<xsl:variable name="EnergyTS">
<xsl:choose>
<xsl:when test="$TSmol">
<xsl:value-of select="$EnergyTStemp" />
</xsl:when>
<!--For ILT source reactions use the activationEnergy + energy of reactants-->
<!--Look first for alternative format(from OB)-->
<xsl:when test="$isILT and cml:rateParameters/cml:E[not(@reverse)]">
<xsl:value-of select="(cml:rateParameters/cml:E +
exsl:node-set($RandPEnergies)/*[number($reactantIndex)])"/>
</xsl:when>
<xsl:when test="$isILT and cml:rateParameters/cml:E[@reverse]">
<xsl:value-of select="(cml:rateParameters/cml:E +
exsl:node-set($RandPEnergies)/*[number($productIndex)])"/>
</xsl:when>
<xsl:when test="$isILT and me:activationEnergy[not(@reverse)]">
<xsl:value-of select="(me:activationEnergy +
exsl:node-set($RandPEnergies)/*[number($reactantIndex)])"/>
</xsl:when>
<xsl:when test="$isILT and me:activationEnergy[@reverse]">
<xsl:value-of select="(me:activationEnergy +
exsl:node-set($RandPEnergies)/*[number($productIndex)])"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$EnergyTStemp" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="yTS" select="$ybase - $yscale * $EnergyTS"/>
<!--Draw line from reactant, through TS to product-->
<svg:path stroke ="black" fill="none">
<xsl:if test="@active='false'">
<xsl:attribute name="class">inactive</xsl:attribute>
</xsl:if>
<!--Make line dashed if reactant and product are not adjacent-->
<xsl:if test = "$productpos - $reactantpos > $xspacing
or $productpos - $reactantpos < - $xspacing">
<xsl:attribute name="stroke-dasharray">
<xsl:value-of select="'4,2'"/>
</xsl:attribute>
</xsl:if>
<xsl:attribute name="d">
<xsl:value-of select="'M'" />
<xsl:value-of select="concat($reactantpos + $spoffset,' ')"/>
<xsl:value-of select="$ybase - $yscale * exsl:node-set($RandPEnergies)/Energy[number($reactantIndex)]"/>
<xsl:if test="$yTS">
<!--Only if there is a valid TS-->
<!--Draw lines and TS level-->
<xsl:value-of select="concat('L ', ($reactantpos+$productpos)*0.5 - $tsoffset, ' ')"/>
<xsl:value-of select="$yTS"/>
<xsl:value-of select="concat(' ', ($reactantpos+$productpos)*0.5 + $tsoffset, ' ')"/>
<xsl:value-of select="$yTS"/>
</xsl:if>
<xsl:value-of select="concat(' L',$productpos - $spoffset,' ')"/>
<xsl:choose>
<xsl:when test="exsl:node-set($RandPEnergies)/Energy[number($productIndex)] != $dummyE">
<xsl:value-of select="$ybase - $yscale * exsl:node-set($RandPEnergies)/Energy[number($productIndex)]"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$diagheight - 100"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</svg:path>
<xsl:if test="$yTS and string-length($TSmol/@id) < 40">
<!--Label TS if less than 40 characters-->
<svg:g>
<xsl:if test="@active='false'">
<xsl:attribute name="class">inactive</xsl:attribute>
</xsl:if>
<svg:text font-size="9" >
<xsl:attribute name="x" >
<xsl:number value="($reactantpos+$productpos -$tswidth * 0.7)*0.5" />
</xsl:attribute>
<xsl:attribute name="y" >
<xsl:number value="$yTS - $dytstitle" />
</xsl:attribute>
<xsl:value-of select="$TSmol/@id"/>
</svg:text>
<svg:text font-size="8" >
<xsl:attribute name="x" >
<xsl:number value="($reactantpos+$productpos -$tswidth * 0.7)*0.5" />
</xsl:attribute>
<xsl:attribute name="y" >
<xsl:number value="$yTS - $dytsenergy" />
</xsl:attribute>
<xsl:value-of select="format-number($EnergyTS - $energyOffset,'#.0')"/>
</svg:text>
</svg:g>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template name="test">
<!--Displays the unique reaction/product sets with the names of both species if present.-->
<xsl:for-each select="$distinctRandP">
<svg:text x="160">
<xsl:attribute name="y">
<xsl:value-of select="40+position()*20"/>
</xsl:attribute>
<xsl:value-of select="."/>
<xsl:choose>
<xsl:when test=". = ../../../cml:reactant[1]/cml:molecule/@ref">
<!--Above: better way to test whether the parent node is a cml:reactant or a cml:product?-->
<xsl:if test="count(../../../cml:reactant)>1">
<xsl:text> + </xsl:text>
</xsl:if>
<xsl:value-of select="../../../cml:reactant[2]/cml:molecule/@ref"/>
</xsl:when>
<xsl:otherwise>
<xsl:if test="count(../../../cml:product)>1">
<xsl:text> + </xsl:text>
</xsl:if>
<xsl:value-of select="../../../cml:product[2]/cml:molecule/@ref"/>
</xsl:otherwise>
</xsl:choose>
</svg:text>
</xsl:for-each>
</xsl:template>
<xsl:template name="test2">
<xsl:for-each select="//cml:reaction">
<svg:text x="160">
<xsl:attribute name="y">
<xsl:value-of select="40+position()*20"/>
</xsl:attribute>
<xsl:value-of select="cml:reactant[1]/cml:molecule/@ref"/>
<xsl:if test="count(cml:reactant)">
<xsl:text> + </xsl:text>
</xsl:if>
<xsl:value-of select="cml:reactant[2]/cml:molecule/@ref"/>
</svg:text>
</xsl:for-each>
</xsl:template>
<xsl:template name="test3">
<svg:text x="0">
<xsl:attribute name="y">
<xsl:value-of select="$diagheight"/>
</xsl:attribute>
$energyOffset=
<xsl:value-of select="$energyOffset"/>
$Emin=
<xsl:value-of select="$Emin"/>
Count RandP sets =
<xsl:value-of select="count($distinctRandP)"/>
Count TS =
<xsl:value-of select="count(key('molrefs',//me:transitionState/cml:molecule/@ref))"/>
Count TSEnergies =
<xsl:value-of select="count($TSEnergies)"/>
</svg:text>
<svg:text x="0">
<xsl:attribute name="y">
<xsl:value-of select="$diagheight + 20"/>
</xsl:attribute>
yscale =
<xsl:value-of select="$yscale"/>
Emax =
<xsl:value-of select="$Emax"/>
Emin =
<xsl:value-of select="$Emin"/>
Count energy levels =
<xsl:value-of select="count(exsl:node-set($RandPEnergies)/Energy | $TSEnergies)"/>
</svg:text>
<svg:text x="0">
<xsl:attribute name="y">
<xsl:value-of select="$diagheight + 40"/>
</xsl:attribute>
<xsl:for-each select="exsl:node-set($RandPEnergies)/Energy">
<xsl:value-of select="current()"/>,
</xsl:for-each>
</svg:text>
</xsl:template>
</xsl:stylesheet>