-
Notifications
You must be signed in to change notification settings - Fork 1
/
xsd2merge_pdbml_sifts.xsl
270 lines (219 loc) · 12.4 KB
/
xsd2merge_pdbml_sifts.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
<?xml version="1.0" encoding="UTF-8"?>
<xsl2:stylesheet
version="2.0"
xmlns:xsl2="http://www.w3.org/1999/XSL/Transform"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsl2:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl2:strip-space elements="*"/>
<xsl2:template match="/">
<xsl2:text disable-output-escaping="yes">
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:align="http://www.ebi.ac.uk/pdbe/docs/sifts/alignment.xsd"
xmlns:data="http://www.ebi.ac.uk/pdbe/docs/sifts/dataTypes.xsd"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:sifts="http://www.ebi.ac.uk/pdbe/docs/sifts/eFamily.xsd"
xmlns:PDBx="http://pdbml.pdb.org/schema/pdbx-v50.xsd"
exclude-result-prefixes="align data dc rdf sifts">
</xsl2:text>
<xsl2:apply-templates/>
<xsl2:text disable-output-escaping="yes">
</xsl:stylesheet></xsl2:text>
</xsl2:template>
<xsl2:variable name="merge_categories">pdbx_sifts_unp_segments pdbx_sifts_xref_db_segments</xsl2:variable>
<xsl2:template match="/xsd:schema">
<xsl2:text disable-output-escaping="yes">
<xsl:param name="sifts_file" required="yes"/>
<xsl:param name="sifts_entry" select="document($sifts_file)/sifts:entry"/>
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="datablock" select="/PDBx:datablock"/>
<xsl:variable name="entry_id"><xsl:value-of select="$datablock/PDBx:entryCategory/PDBx:entry/@id"/></xsl:variable>
<xsl:variable name="datablock_name"><xsl:value-of select="concat($entry_id,'-noatom')"/></xsl:variable>
<xsl:variable name="pdb_id"><xsl:value-of select="translate($sifts_entry/@dbAccessionId,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/></xsl:variable>
<xsl:template match="/">
<xsl:if test="$entry_id!=$pdb_id">
<xsl:call-template name="error_handler">
<xsl:with-param name="terminate">yes</xsl:with-param>
<xsl:with-param name="error_message">
Unmatched entry ID in both documents (<xsl:value-of select="$entry_id"/> and <xsl:value-of select="$pdb_id"/>).
</xsl:with-param>
</xsl:call-template>
</xsl:if>
<PDBx:datablock datablockName="{$datablock_name}" xsi:schemaLocation="http://pdbml.pdb.org/schema/pdbx-v50.xsd pdbx-v50.xsd">
<xsl:apply-templates select="PDBx:datablock/*[not(</xsl2:text>
<xsl2:for-each select="tokenize($merge_categories,' ')">
<xsl2:text disable-output-escaping="yes">local-name()='</xsl2:text><xsl2:value-of select="."/><xsl2:text disable-output-escaping="yes">Category'</xsl2:text>
<xsl2:if test="position()!=last()"><xsl2:text disable-output-escaping="yes"> or </xsl2:text></xsl2:if>
</xsl2:for-each>
<xsl2:text disable-output-escaping="yes">)]"/>
</xsl2:text>
<xsl2:for-each select="tokenize($merge_categories,' ')">
<xsl2:text disable-output-escaping="yes">
<xsl:call-template name="merge_</xsl2:text><xsl2:value-of select="."/><xsl2:text disable-output-escaping="yes">"/>
</xsl2:text>
</xsl2:for-each>
<xsl2:text disable-output-escaping="yes">
</PDBx:datablock>
</xsl:template>
</xsl2:text>
<xsl2:call-template name="categories"/>
<xsl2:text disable-output-escaping="yes">
<xsl:template match="PDBx:*" mode="category-element">
<xsl:element name="{name()}">
<xsl:apply-templates select="@*|node()" mode="data-item"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*" mode="category-element">
<xsl:copy/>
</xsl:template>
<xsl:template match="PDBx:*" mode="data-item">
<xsl:element name="{name()}">
<xsl:apply-templates select="@*|node()" mode="data-item"/>
</xsl:element>
</xsl:template>
<xsl:template match="node()" mode="data-item">
<xsl:copy/>
</xsl:template>
<xsl:template match="@*" mode="data-item">
<xsl:copy/>
</xsl:template>
<xsl:template name="error_handler">
<xsl:param name="error_message"/>
<xsl:param name="terminate">no</xsl:param>
<xsl:choose>
<xsl:when test="$terminate='yes'">
<xsl:message terminate="yes">
<xsl:text>ERROR in merge_pdbml_info.xsl: </xsl:text>
<xsl:value-of select="$error_message"/>
</xsl:message>
</xsl:when>
<xsl:otherwise>
<span style="font-weight: bold; color: red">
<xsl:text>ERROR: </xsl:text>
<xsl:value-of select="$error_message"/>
</span>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="extract_pdbx_sifts_unp_segments">
<xsl:for-each select="$sifts_entry/sifts:entity">
<xsl:variable name="asym_id"><xsl:value-of select="@entityId"/></xsl:variable>
<xsl:variable name="entity_id"><xsl:value-of select="$datablock/PDBx:pdbx_poly_seq_schemeCategory/PDBx:pdbx_poly_seq_scheme[@asym_id=$asym_id][1]/@entity_id"/></xsl:variable>
<xsl:for-each select="sifts:segment">
<xsl:variable name="segment_id"><xsl:value-of select="position()"/></xsl:variable>
<xsl:variable name="seq_id_start"><xsl:value-of select="@start"/></xsl:variable>
<xsl:variable name="seq_id_end"><xsl:value-of select="@end"/></xsl:variable>
<xsl:variable name="seq_id_range"><xsl:value-of select="number(@end) - number(@start) + 1"/></xsl:variable>
<xsl:variable name="max_range">
<xsl:for-each select="sifts:listMapRegion/sifts:mapRegion/sifts:db[@dbSource='UniProt']">
<xsl:sort select="number(@end) - number(@start)" data-type="number" order="descending"/>
<xsl:if test="position()=1"><xsl:value-of select="number(@end) - number(@start)"/></xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="sifts:listMapRegion/sifts:mapRegion/sifts:db[@dbSource='UniProt']">
<xsl:variable name="instance_id"><xsl:value-of select="position()"/></xsl:variable>
<xsl:variable name="unp_acc"><xsl:value-of select="@dbAccessionId"/></xsl:variable>
<PDBx:pdbx_sifts_unp_segments entity_id="{$entity_id}" asym_id="{$asym_id}" unp_acc="{$unp_acc}" segment_id="{$segment_id}" instance_id="{$instance_id}">
<xsl:variable name="range"><xsl:value-of select="number(@end) - number(@start) + 1"/></xsl:variable>
<PDBx:unp_start><xsl:value-of select="@start"/></PDBx:unp_start>
<PDBx:unp_end><xsl:value-of select="@end"/></PDBx:unp_end>
<PDBx:seq_id_start><xsl:value-of select="$seq_id_start"/></PDBx:seq_id_start>
<PDBx:seq_id_end><xsl:value-of select="$seq_id_end"/></PDBx:seq_id_end>
<xsl:choose>
<xsl:when test="number(@end) - number(@start)=$max_range">
<PDBx:best_mapping>Y</PDBx:best_mapping>
</xsl:when>
<xsl:otherwise>
<PDBx:best_mapping>N</PDBx:best_mapping>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="$seq_id_range > $range">
<PDBx:identity>1.000</PDBx:identity>
</xsl:when>
<xsl:otherwise>
<PDBx:identity><xsl:value-of select="format-number($seq_id_range div $range,'0.000')"/></PDBx:identity>
</xsl:otherwise>
</xsl:choose>
</PDBx:pdbx_sifts_unp_segments>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
<xsl:template name="extract_pdbx_sifts_xref_db_segments">
<xsl:for-each select="$sifts_entry/sifts:entity">
<xsl:variable name="asym_id"><xsl:value-of select="@entityId"/></xsl:variable>
<xsl:variable name="entity_id"><xsl:value-of select="$datablock/PDBx:pdbx_poly_seq_schemeCategory/PDBx:pdbx_poly_seq_scheme[@asym_id=$asym_id][1]/@entity_id"/></xsl:variable>
<xsl:for-each select="sifts:segment">
<xsl:variable name="segment_id"><xsl:value-of select="position()"/></xsl:variable>
<xsl:for-each select="sifts:listMapRegion/sifts:mapRegion/sifts:db[@dbSource!='PDB' and @dbSource!='UniProt' and @dbSource!='EC']">
<xsl:variable name="seq_id_start"><xsl:value-of select="../@start"/></xsl:variable>
<xsl:variable name="seq_id_end"><xsl:value-of select="../@end"/></xsl:variable>
<xsl:variable name="instance_id"><xsl:value-of select="position()"/></xsl:variable>
<xsl:variable name="xref_db"><xsl:value-of select="@dbSource"/></xsl:variable>
<xsl:variable name="xref_db_acc">
<xsl:choose>
<xsl:when test="contains(@dbAccessionId,':') and @dbSource!='GO'">
<xsl:value-of select="substring-after(@dbAccessionId,':')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@dbAccessionId"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<PDBx:pdbx_sifts_xref_db_segments entity_id="{$entity_id}" asym_id="{$asym_id}" xref_db="{$xref_db}" xref_db_acc="{$xref_db_acc}" segment_id="{$segment_id}" instance_id="{$instance_id}" seq_id_start="{$seq_id_start}" seq_id_end="{$seq_id_end}">
<!-- v4.356 or older
<PDBx:seq_id_start><xsl:value-of select="$seq_id_start"/></PDBx:seq_id_start>
<PDBx:seq_id_end><xsl:value-of select="$seq_id_end"/></PDBx:seq_id_end>
-->
</PDBx:pdbx_sifts_xref_db_segments>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl2:text>
<xsl2:for-each select="tokenize($merge_categories,' ')">
<xsl2:text disable-output-escaping="yes">
<xsl:template name="merge_</xsl2:text><xsl2:value-of select="."/><xsl2:text disable-output-escaping="yes">">
<xsl:if test="PDBx:datablock/PDBx:</xsl2:text><xsl2:value-of select="."/><xsl2:text disable-output-escaping="yes">Category or $sifts_entry/sifts:entity/sifts:segment</xsl2:text><xsl2:if test=".='pdbx_sifts_unp_segments'">/sifts:listMapRegion/sifts:mapRegion/sifts:db[@dbSource='UniProt']</xsl2:if><xsl2:if test=".='pdbx_sifts_xref_db_segments'">/sifts:listMapRegion/sifts:mapRegion/sifts:db[@dbSource!='PDB' and @dbSource!='UniProt' and @dbSource!='EC']</xsl2:if><xsl2:text disable-output-escaping="yes">">
<xsl:element name="PDBx:</xsl2:text><xsl2:value-of select="."/><xsl2:text disable-output-escaping="yes">Category">
<xsl:choose>
<xsl:when test="PDBx:datablock/PDBx:</xsl2:text><xsl2:value-of select="."/><xsl2:text disable-output-escaping="yes">Category">
<xsl:apply-templates select="PDBx:datablock/PDBx:</xsl2:text><xsl2:value-of select="."/><xsl2:text disable-output-escaping="yes">/*" mode="category-element"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="extract_</xsl2:text><xsl2:value-of select="."/><xsl2:text disable-output-escaping="yes">"/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:if>
</xsl:template>
</xsl2:text>
</xsl2:for-each>
<xsl2:text disable-output-escaping="yes">
<xsl:template match="*[@xsi:nil='true']"/>
<xsl:template match="*|text()|@*"/>
</xsl2:text>
</xsl2:template>
<xsl2:template name="categories">
<xsl2:for-each select="xsd:complexType[@name='datablockType']/xsd:all/xsd:element">
<xsl2:call-template name="category">
<xsl2:with-param name="name" select="@name"/>
</xsl2:call-template>
</xsl2:for-each>
</xsl2:template>
<xsl2:template name="category">
<xsl2:param name="name"/>
<xsl2:text disable-output-escaping="yes">
<xsl:template match="PDBx:</xsl2:text><xsl2:value-of select="$name"/><xsl2:text disable-output-escaping="yes">">
<xsl:element name="{name()}">
<xsl:apply-templates mode="category-element"/>
</xsl:element>
</xsl:template>
</xsl2:text>
</xsl2:template>
</xsl2:stylesheet>