|
| 1 | +<?xml version="1.0" encoding="UTF-8"?> |
| 2 | +<xsl:stylesheet |
| 3 | + version="1.0" |
| 4 | + xmlns:PDBx="http://pdbml.pdb.org/schema/pdbx-v50.xsd" |
| 5 | + xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
| 6 | + xmlns:svg="http://www.w3.org/2000/svg" |
| 7 | + xmlns:xlink="http://www.w3.org/1999/xlink" |
| 8 | + xmlns:h="http://www.w3.org/1999/xhtml" |
| 9 | + > |
| 10 | + |
| 11 | + <!-- |
| 12 | +
|
| 13 | +Motivation: |
| 14 | + transforms a pubchem xml result to SVG |
| 15 | +Author |
| 16 | + Pierre Lindenbaum PhD plindenbaum@yahoo.fr |
| 17 | + http://plindenbaum.blogspot.com |
| 18 | +Parameters: |
| 19 | + scale :=scale factor |
| 20 | + show-bounds := (true/false) |
| 21 | + xradius := scale factor for atoms |
| 22 | +--> |
| 23 | + |
| 24 | +<!-- ========================================================================= --> |
| 25 | +<xsl:output method='xml' indent='yes' omit-xml-declaration="no"/> |
| 26 | + |
| 27 | +<!-- ========================================================================= --> |
| 28 | +<!-- the margin of the SVG --> |
| 29 | +<xsl:param name="margin">100</xsl:param> |
| 30 | +<!-- the scale factor --> |
| 31 | +<xsl:param name="scale">30</xsl:param> |
| 32 | +<!-- the scale factor for atom radius--> |
| 33 | +<xsl:param name="xradius">1</xsl:param> |
| 34 | + |
| 35 | +<!-- array of atoms --> |
| 36 | +<xsl:variable name="a_array" select="/PDBx:datablock/PDBx:atom_siteCategory/PDBx:atom_site"/> |
| 37 | +<!-- number of atoms--> |
| 38 | +<xsl:variable name="count"><xsl:value-of select="count($a_array)"/></xsl:variable> |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +<xsl:variable name="x_array" select="$a_array/PDBx:Cartn_x"/> |
| 43 | +<xsl:variable name="y_array" select="$a_array/PDBx:Cartn_y"/> |
| 44 | +<xsl:variable name="z_array" select="$a_array/PDBx:Cartn_z"/> |
| 45 | + |
| 46 | + |
| 47 | +<xsl:variable name="min-x"> |
| 48 | + <xsl:for-each select="$a_array/PDBx:Cartn_x"> |
| 49 | + <xsl:sort select="." data-type="number" order="ascending" /> |
| 50 | + <xsl:if test="position() = 1"> |
| 51 | + <xsl:value-of select="." /> |
| 52 | + </xsl:if> |
| 53 | + </xsl:for-each> |
| 54 | +</xsl:variable> |
| 55 | + |
| 56 | +<xsl:variable name="max-x"> |
| 57 | + <xsl:for-each select="$a_array/PDBx:Cartn_x"> |
| 58 | + <xsl:sort select="." data-type="number" order="descending" /> |
| 59 | + <xsl:if test="position() = 1"> |
| 60 | + <xsl:value-of select="." /> |
| 61 | + </xsl:if> |
| 62 | + </xsl:for-each> |
| 63 | +</xsl:variable> |
| 64 | + |
| 65 | +<xsl:variable name="min-y"> |
| 66 | + <xsl:for-each select="$a_array/PDBx:Cartn_y"> |
| 67 | + <xsl:sort select="." data-type="number" order="ascending" /> |
| 68 | + <xsl:if test="position() = 1"> |
| 69 | + <xsl:value-of select="." /> |
| 70 | + </xsl:if> |
| 71 | + </xsl:for-each> |
| 72 | +</xsl:variable> |
| 73 | + |
| 74 | +<xsl:variable name="max-y"> |
| 75 | + <xsl:for-each select="$a_array/PDBx:Cartn_y"> |
| 76 | + <xsl:sort select="." data-type="number" order="descending" /> |
| 77 | + <xsl:if test="position() = 1"> |
| 78 | + <xsl:value-of select="." /> |
| 79 | + </xsl:if> |
| 80 | + </xsl:for-each> |
| 81 | +</xsl:variable> |
| 82 | + |
| 83 | +<xsl:variable name="min-z"> |
| 84 | + <xsl:for-each select="$a_array/PDBx:Cartn_z"> |
| 85 | + <xsl:sort select="." data-type="number" order="ascending" /> |
| 86 | + <xsl:if test="position() = 1"> |
| 87 | + <xsl:value-of select="." /> |
| 88 | + </xsl:if> |
| 89 | + </xsl:for-each> |
| 90 | +</xsl:variable> |
| 91 | + |
| 92 | +<xsl:variable name="max-z"> |
| 93 | + <xsl:for-each select="$a_array/PDBx:Cartn_z"> |
| 94 | + <xsl:sort select="." data-type="number" order="descending" /> |
| 95 | + <xsl:if test="position() = 1"> |
| 96 | + <xsl:value-of select="." /> |
| 97 | + </xsl:if> |
| 98 | + </xsl:for-each> |
| 99 | +</xsl:variable> |
| 100 | + |
| 101 | +<xsl:variable name="length-x" select="$max-x - $min-x"/> |
| 102 | +<xsl:variable name="length-y" select="$max-y - $min-y"/> |
| 103 | +<xsl:variable name="length-z" select="$max-z - $min-z"/> |
| 104 | + |
| 105 | + |
| 106 | +<xsl:variable name="frame-width" select="($margin * 2 )+ ($length-x * $scale)"/> |
| 107 | +<xsl:variable name="frame-height" select="($margin * 2 )+ ($length-y * $scale)"/> |
| 108 | + |
| 109 | +<xsl:template match="/"> |
| 110 | +<xsl:apply-templates select="PDBx:datablock"/> |
| 111 | +</xsl:template> |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +<xsl:template match="PDBx:datablock"> |
| 116 | + <xsl:element name="svg:svg"> |
| 117 | + <xsl:attribute name="version">1.0</xsl:attribute> |
| 118 | + <xsl:attribute name="width"><xsl:value-of select="$frame-width"/></xsl:attribute> |
| 119 | + <xsl:attribute name="height"><xsl:value-of select="$frame-height"/></xsl:attribute> |
| 120 | + <svg:title><xsl:value-of select="@datablockName"/></svg:title> |
| 121 | + <xsl:comment> |
| 122 | + Author: Pierre Lindenbaum PhD plindenbaum@yahoo.fr |
| 123 | + http://plindenbaum.blogspot.com |
| 124 | +</xsl:comment> |
| 125 | + |
| 126 | + <!-- DEFINITIONS --> |
| 127 | + <xsl:element name="svg:defs"> |
| 128 | + |
| 129 | + <xsl:call-template name="gradient"> |
| 130 | + <xsl:with-param name="gid">o</xsl:with-param> |
| 131 | + <xsl:with-param name="start">white</xsl:with-param> |
| 132 | + <xsl:with-param name="end">blue</xsl:with-param> |
| 133 | + <xsl:with-param name="r">18</xsl:with-param> |
| 134 | + </xsl:call-template> |
| 135 | + |
| 136 | + <xsl:call-template name="gradient"> |
| 137 | + <xsl:with-param name="gid">c</xsl:with-param> |
| 138 | + <xsl:with-param name="start">white</xsl:with-param> |
| 139 | + <xsl:with-param name="end">black</xsl:with-param> |
| 140 | + <xsl:with-param name="r">16</xsl:with-param> |
| 141 | + </xsl:call-template> |
| 142 | + |
| 143 | + <xsl:call-template name="gradient"> |
| 144 | + <xsl:with-param name="gid">h</xsl:with-param> |
| 145 | + <xsl:with-param name="start">lightgray</xsl:with-param> |
| 146 | + <xsl:with-param name="end">gray</xsl:with-param> |
| 147 | + <xsl:with-param name="r">10</xsl:with-param> |
| 148 | + </xsl:call-template> |
| 149 | + |
| 150 | + <xsl:call-template name="gradient"> |
| 151 | + <xsl:with-param name="gid">UN</xsl:with-param> |
| 152 | + <xsl:with-param name="start">green</xsl:with-param> |
| 153 | + <xsl:with-param name="end">red</xsl:with-param> |
| 154 | + <xsl:with-param name="r">10</xsl:with-param> |
| 155 | + </xsl:call-template> |
| 156 | + |
| 157 | + |
| 158 | + </xsl:element><!-- defs --> |
| 159 | + |
| 160 | +<xsl:element name="svg:rect"> |
| 161 | + <xsl:attribute name="style">stroke:lightgray;fill:white;</xsl:attribute> |
| 162 | + <xsl:attribute name="x">0</xsl:attribute> |
| 163 | + <xsl:attribute name="y">0</xsl:attribute> |
| 164 | + <xsl:attribute name="width"><xsl:value-of select="$frame-width - 1"/></xsl:attribute> |
| 165 | + <xsl:attribute name="height"><xsl:value-of select="$frame-height - 1"/></xsl:attribute> |
| 166 | +</xsl:element> |
| 167 | + |
| 168 | + <xsl:element name="svg:g"> |
| 169 | + |
| 170 | + <xsl:comment>BEGIN ATOMS</xsl:comment> |
| 171 | + <xsl:element name="svg:g"> |
| 172 | + <xsl:apply-templates select="PDBx:atom_siteCategory/PDBx:atom_site"/> |
| 173 | + </xsl:element> |
| 174 | + <xsl:comment>END ATOMS</xsl:comment> |
| 175 | + </xsl:element> |
| 176 | + |
| 177 | + |
| 178 | + </xsl:element><!-- svg --> |
| 179 | +</xsl:template> |
| 180 | + |
| 181 | +<xsl:template match="PDBx:atom_site"> |
| 182 | + |
| 183 | + <xsl:variable name="s"><xsl:value-of select="PDBx:auth_atom_id"/></xsl:variable> |
| 184 | + |
| 185 | + <xsl:element name="svg:use"> |
| 186 | + |
| 187 | + <xsl:attribute name="xlink:href">#atom<xsl:choose> |
| 188 | + <xsl:when test="$s='o' or $s='c' or $s='h'"><xsl:value-of select="$s"/></xsl:when> |
| 189 | + <xsl:otherwise>UN</xsl:otherwise> |
| 190 | + </xsl:choose></xsl:attribute> |
| 191 | + <xsl:attribute name="x"><xsl:call-template name="coord-x"><xsl:with-param name="atom" select="."/></xsl:call-template></xsl:attribute> |
| 192 | + <xsl:attribute name="y"><xsl:call-template name="coord-y"><xsl:with-param name="atom" select="."/></xsl:call-template></xsl:attribute> |
| 193 | + <xsl:attribute name="title"><xsl:value-of select="$s"/></xsl:attribute> |
| 194 | + </xsl:element> |
| 195 | + |
| 196 | + |
| 197 | +</xsl:template> |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | +<xsl:template name="gradient"> |
| 202 | +<xsl:param name="gid">did</xsl:param> |
| 203 | +<xsl:param name="start">rgb(200,200,200)</xsl:param> |
| 204 | +<xsl:param name="end">rgb(0,0,255)</xsl:param> |
| 205 | +<xsl:param name="r">0</xsl:param> |
| 206 | +<xsl:element name="svg:radialGradient"> |
| 207 | + <xsl:attribute name="id">radial<xsl:value-of select="$gid"/></xsl:attribute> |
| 208 | + <xsl:attribute name="cx">50%</xsl:attribute> |
| 209 | + <xsl:attribute name="cy">50%</xsl:attribute> |
| 210 | + <xsl:attribute name="r">50%</xsl:attribute> |
| 211 | + <xsl:attribute name="fx">30%</xsl:attribute> |
| 212 | + <xsl:attribute name="fy">30%</xsl:attribute> |
| 213 | + <xsl:element name="svg:stop"> |
| 214 | + <xsl:attribute name="offset">0%</xsl:attribute> |
| 215 | + <xsl:attribute name="style">stop-color:<xsl:value-of select="$start"/>;stop-opacity:0.8;</xsl:attribute> |
| 216 | + </xsl:element> |
| 217 | + <xsl:element name="svg:stop"> |
| 218 | + <xsl:attribute name="offset">100%</xsl:attribute> |
| 219 | + <xsl:attribute name="style">stop-color:<xsl:value-of select="$end"/>;stop-opacity:1;</xsl:attribute> |
| 220 | + </xsl:element> |
| 221 | +</xsl:element> |
| 222 | + |
| 223 | + |
| 224 | +<xsl:element name="svg:circle"> |
| 225 | + <xsl:attribute name="id">atom<xsl:value-of select="$gid"/></xsl:attribute> |
| 226 | + <xsl:attribute name="r"><xsl:value-of select="number($r) * $xradius"/></xsl:attribute> |
| 227 | + <xsl:attribute name="style">stroke:black;fill:url(#radial<xsl:value-of select="$gid"/>);</xsl:attribute> |
| 228 | +</xsl:element> |
| 229 | + |
| 230 | +</xsl:template> |
| 231 | + |
| 232 | +<xsl:template name="coord-x"> |
| 233 | +<xsl:param name="atom"/> |
| 234 | +<xsl:variable name="x" select="$atom/PDBx:Cartn_x"/> |
| 235 | +<xsl:value-of select="$margin + ($x - $min-x) * $scale"/> |
| 236 | +</xsl:template> |
| 237 | + |
| 238 | + |
| 239 | +<xsl:template name="coord-y"> |
| 240 | +<xsl:param name="atom"/> |
| 241 | +<xsl:variable name="y" select="$atom/PDBx:Cartn_y"/> |
| 242 | +<xsl:value-of select="$margin + ($y - $min-y) * $scale"/> |
| 243 | +</xsl:template> |
| 244 | + |
| 245 | + |
| 246 | + |
| 247 | +</xsl:stylesheet> |
0 commit comments