Last active
August 29, 2015 14:02
-
-
Save toniher/2b1e96297ade920728b8 to your computer and use it in GitHub Desktop.
XSL transformation 1.0 stylesheet for NCBI PSIBLAST XML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version='1.0' encoding="ISO-8859-1" ?> | |
<!-- | |
XSLT 1.0 Stylesheet for converting NCBI PSIBLAST XML into JSON with different string params as input | |
Usage: xsltproc psiblast.xsl output.xml > output.json | |
Toni Hermoso Pulido @toniher, 2014 - GPLv 3 or later | |
Reference http://plindenbaum.blogspot.com.es/2008/05/ncbi-blast-xslt-xhtml-svg.html | |
--> | |
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0' > | |
<xsl:output method="text" /> | |
<xsl:param name="database" >blastdb</xsl:param> | |
<xsl:param name="type" >blast</xsl:param> | |
<xsl:param name="id" ></xsl:param> | |
<xsl:param name="seqtype" >prot</xsl:param> | |
<xsl:param name="maxiters" >1</xsl:param> | |
<xsl:param name="username" >Anonymous</xsl:param> | |
<xsl:param name="date" ></xsl:param> | |
<xsl:param name="md5" ></xsl:param> | |
<xsl:param name="ref" ></xsl:param> | |
<xsl:template match="/"> | |
<xsl:apply-templates select="BlastOutput"/> | |
</xsl:template> | |
<xsl:template match="BlastOutput">{ | |
"_id":"<xsl:value-of select="$id" />", | |
"type":"<xsl:value-of select="$type" />", | |
"username":"<xsl:value-of select="$username" />", | |
"db":"<xsl:value-of select="$database"/>",<!-- db:<xsl:apply-templates select="BlastOutput_db" mode="text"/>, --> | |
"program":<xsl:apply-templates select="BlastOutput_program" mode="text"/>,<!-- name:<xsl:apply-templates select="BlastOutput_query-def" mode="text"/>, --> | |
"seqtype":"<xsl:value-of select="$seqtype" />", | |
"maxiters":"<xsl:value-of select="$maxiters" />", | |
"date":"<xsl:value-of select="$date" />", | |
"ref":"<xsl:value-of select="$ref" />", | |
<xsl:apply-templates select="BlastOutput_param/Parameters"/>, | |
"results": [ | |
{ | |
"iters":[ <xsl:for-each select="BlastOutput_iterations/Iteration"> | |
<xsl:if test="position()!=1">,</xsl:if> | |
<xsl:apply-templates select="."/> | |
</xsl:for-each> | |
], | |
"length":<xsl:apply-templates select="BlastOutput_query-len"/>, | |
<!--"sequence":"<xsl:value-of select="$sequence" />", | |
"name":"<xsl:value-of select="$id" />",--> | |
"md5":"<xsl:value-of select="$md5" />" | |
} | |
] | |
}</xsl:template> | |
<xsl:template match="Parameters">"params": { | |
"expect":<xsl:apply-templates select="Parameters_expect" />, | |
"gap_open":<xsl:apply-templates select="Parameters_gap-open"/>, | |
"gap_extend":<xsl:apply-templates select="Parameters_gap-extend"/>, | |
"filter":<xsl:apply-templates select="Parameters_filter" mode="text"/> | |
}</xsl:template> | |
<!-- Iteration --> | |
<xsl:template match="Iteration"> | |
{ | |
"num":<xsl:apply-templates select="Iteration_iter-num" />, | |
"hits":[ | |
<xsl:for-each select="Iteration_hits/Hit"> | |
<xsl:if test="position()!=1">,</xsl:if> | |
<xsl:apply-templates select="."/> | |
</xsl:for-each> | |
] | |
} | |
</xsl:template> | |
<!-- Hit --> | |
<xsl:template match="Hit">{ | |
"num":<xsl:apply-templates select="Hit_num"/>,<xsl:apply-templates select="Hit_id"/><xsl:apply-templates select="Hit_def"/> | |
"length":<xsl:apply-templates select="Hit_len" />, | |
"hsps":[ | |
<xsl:for-each select="Hit_hsps/Hsp"> | |
<xsl:if test="position()!=1">,</xsl:if> | |
<xsl:apply-templates select="."/> | |
</xsl:for-each> | |
] | |
} | |
</xsl:template> | |
<xsl:template match="Hsp">{ | |
"num":<xsl:apply-templates select="Hsp_num" />, | |
"bit_score":<xsl:apply-templates select="Hsp_bit-score" />, | |
"score":<xsl:apply-templates select="Hsp_score" />, | |
"evalue":<xsl:apply-templates select="Hsp_evalue" />, | |
"qstart":<xsl:apply-templates select="Hsp_query-from" />, | |
"qend":<xsl:apply-templates select="Hsp_query-to" />, | |
"hstart":<xsl:apply-templates select="Hsp_hit-from" />, | |
"hend":<xsl:apply-templates select="Hsp_hit-to" />, | |
"query_frame":<xsl:apply-templates select="Hsp_query-frame" />, | |
"hit_frame":<xsl:apply-templates select="Hsp_hit-frame" />, | |
"identity":<xsl:apply-templates select="Hsp_identity" />, | |
"positive":<xsl:apply-templates select="Hsp_positive" />, | |
"gaps":<xsl:apply-templates select="Hsp_gaps" />, | |
"length":<xsl:apply-templates select="Hsp_align-len" />, | |
"qseq":<xsl:apply-templates select="Hsp_qseq" mode="text"/>, | |
<!--"midline":<xsl:apply-templates select="Hsp_midline" mode="text"/>, Remove for particular case--> | |
"hseq":<xsl:apply-templates select="Hsp_hseq" mode="text"/> | |
} | |
</xsl:template> | |
<xsl:template match="Hit_id"> <!-- Let's get all id and gi if possible (in case NCBI DB) --> | |
"id":"<xsl:value-of select="."/>",<xsl:if test="starts-with(.,'gi|')"><xsl:variable name="gi" select="substring-before(substring-after(.,'gi|'),'|')"/> | |
"gi":<xsl:value-of select="$gi"/>,</xsl:if> | |
</xsl:template> | |
<xsl:template match="Hit_def"><xsl:variable name="name" select="substring-before(.,' [')"/> | |
"name":"<xsl:value-of select="translate($name, '\', '')"/>",</xsl:template> | |
<xsl:template match="*" mode="text"> | |
<xsl:text>"</xsl:text> | |
<xsl:call-template name="escape"> | |
<xsl:with-param name="s" select="."/> | |
</xsl:call-template> | |
<xsl:text>"</xsl:text> | |
</xsl:template> | |
<!-- Escaping quotes for all - dangerous in JSON | |
http://stackoverflow.com/questions/16239431/escaping-value-of-xml-attribute-with-xsltproc --> | |
<xsl:template name="escape"> | |
<xsl:param name="s"/> | |
<xsl:choose> | |
<xsl:when test="contains($s,'"')"> | |
<xsl:value-of select="substring-before($s,'"')"/> | |
<xsl:text>\"</xsl:text> | |
<xsl:call-template name="escape"> | |
<xsl:with-param name="s" select="substring-after($s,'"')"/> | |
</xsl:call-template> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:value-of select='$s'/> | |
</xsl:otherwise> | |
</xsl:choose> | |
</xsl:template> | |
</xsl:stylesheet> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment