Skip to content

Commit 749818c

Browse files
Dimandreasprlic
authored andcommitted
Change to wiki page
1 parent 4aa6562 commit 749818c

2 files changed

Lines changed: 800 additions & 3 deletions

File tree

_wikis/BioJava:CookBook:Blast:XML.md

Lines changed: 394 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,397 @@
22
title: BioJava:CookBook:Blast:XML
33
---
44

5-
How do I convert an XML BLAST result into HTML?
6-
-----------------------------------------------
5+
How do I convert an XML BLAST result into HTML page?
6+
----------------------------------------------------
7+
8+
BioJava contains several classes that allow us to parse both plain and
9+
XML BLAST output. Another way is to make a direct XML to HTML
10+
transformation using an XSL stylesheet. Any modern browser supports such
11+
a transformation directly, but they could produce a little bit different
12+
result. Here is an example how to do it on the "server side" - the
13+
typical task for the web server. First of all the simple java class that
14+
performs the transformation:
15+
16+
### BlastXML2HTML.java
17+
18+
<java> 01 public class BlastXML2HTML implements EntityResolver 02 { 03
19+
private BlastXML2HTML() {} 04 05 public static String toHTML(InputStream
20+
xml) throws IOException, TransformerException, SAXException 06 { 07
21+
Transformer transformer =
22+
TransformerFactory.newInstance().newTransformer(new 08
23+
StreamSource(BlastXML2HTML.class.getClassLoader().getResourceAsStream("blast.xsl")));
24+
09 10 transformer.setOutputProperty(OutputKeys.METHOD, "html"); 11 12
25+
StringWriter writer = new StringWriter(); 13 14 // avoid dtd
26+
validation... 15 XMLReader reader = XMLReaderFactory.createXMLReader();
27+
16 reader.setEntityResolver(new BlastXML2HTML()); 17 18
28+
transformer.transform(new SAXSource(reader, new InputSource(xml)), new
29+
StreamResult(writer)); 19 20 return writer.toString(); 21 } 22 23 public
30+
InputSource resolveEntity(String publicId, String systemId) throws
31+
SAXException, IOException 24 { 25 return new InputSource(new
32+
ByteArrayInputStream(new byte[0])); 26 } 27 28 public static void
33+
main(String[] args) throws Exception 29 { 30 InputStream in = new
34+
FileInputStream("C:/temp/blast.xml"); 31 FileOutputStream out = new
35+
FileOutputStream("C:/temp/blast.html"); 32 33
36+
out.write(toHTML(in).getBytes()); 34 out.close(); 35 } 36 } </java>
37+
38+
The only way to use this class is to call a static method toHTML()
39+
passing the xml stream as a parameter. You can see the main method as an
40+
example of usage - it takes "**blast.xml**" file and transforms it into
41+
"**blast.html**" one.
42+
43+
Actually this code is quite generic and can be used to transform any xml
44+
to any other document.
45+
46+
The most important thing here is the "**blast.xsl**" stylesheet. The
47+
code is looking for the stylesheet in the classpath, so one should be
48+
provided along with code.
49+
50+
Here is an example of such transformation stylesheet:
51+
52+
`
53+
<?xml version="1.0" encoding="ISO-8859-1"?>
54+
55+
<xsl:stylesheet version="1.0"
56+
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
57+
58+
<xsl:template match="/">
59+
<html>
60+
<body>
61+
<div>
62+
<xsl:apply-templates />
63+
</div>
64+
</body>
65+
</html>
66+
</xsl:template>
67+
68+
<xsl:template match="BlastOutput">
69+
<table style="width: 100%; white-space: nowrap; background-color: #66CCFF;">
70+
<tr>
71+
<td colspan="3" style="text-align: left; font-size: xx-large;">
72+
Sequence Similarity Report
73+
</td>
74+
</tr>
75+
<tr>
76+
<td style="text-align: right; font-size: large;">Search Program:</td>
77+
<td>
78+
<xsl:value-of select="BlastOutput_program"/>
79+
<xsl:value-of select="BlastOutput_version"/>
80+
</td>
81+
<td style="width: 100%">
82+
</td>
83+
</tr>
84+
<xsl:apply-templates select="BlastOutput_param"/>
85+
</table>
86+
<div style="width: 100%; text-align: center; font-size: x-large; background-color: #66FFCC">
87+
Detailed Analysis of Results
88+
</div>
89+
<xsl:apply-templates select="BlastOutput_iterations"/>
90+
</xsl:template>
91+
92+
<xsl:template match="BlastOutput_param">
93+
<tr>
94+
<td style="text-align: right; font-size: large;">
95+
Parameters:
96+
</td>
97+
<td style="text-align: left; font-size: medium;">
98+
<xsl:for-each select="Parameters/*">
99+
<xsl:variable name="lname" select="local-name()" />
100+
101+
<xsl:if test="$lname='Parameters_matrix'">
102+
Matrix:
103+
</xsl:if>
104+
<xsl:if test="$lname='Parameters_expect'">
105+
Expected:
106+
</xsl:if>
107+
<xsl:if test="$lname='Parameters_gap-open'">
108+
gap_open:
109+
</xsl:if>
110+
<xsl:if test="$lname='Parameters_gap-extend'">
111+
gap_extend:
112+
</xsl:if>
113+
114+
<xsl:value-of select="."/>
115+
</xsl:for-each>
116+
</td>
117+
</tr>
118+
</xsl:template>
119+
120+
<xsl:template match="BlastOutput_iterations/Iteration">
121+
<div style="width: 100%; background-color: #FFFFCC;">
122+
Iteration:
123+
<xsl:value-of select="Iteration_iter-num/."/>
124+
</div>
125+
<xsl:apply-templates select="Iteration_hits"/>
126+
</xsl:template>
127+
128+
<xsl:template match="Iteration_hits/Hit">
129+
<div style="width: 100%; background-color: #FFFF99;">
130+
Hit Id: <xsl:value-of select="Hit_id/."/>
131+
<br/>
132+
<xsl:value-of select="Hit_def/."/>
133+
<br/>
134+
Sequence length of hit = <xsl:value-of select="Hit_len/."/>
135+
</div>
136+
137+
<xsl:apply-templates select="Hit_hsps"/>
138+
</xsl:template>
139+
140+
<xsl:template match="Hit_hsps/Hsp">
141+
142+
<div style="width: 100%; background-color: #FFEEFF;">
143+
144+
High-scoring segment pair (HSP) group
145+
<br/>
146+
Score = <xsl:value-of select="Hsp_score/."/>,
147+
E = <xsl:value-of select="Hsp_evalue/."/>,
148+
149+
<xsl:variable name="length" select="Hsp_align-len/." />
150+
<xsl:variable name="identity" select="Hsp_identity/." />
151+
<xsl:variable name="positive" select="Hsp_positive/." />
152+
153+
Identities = <xsl:value-of select="$identity"/>/
154+
<xsl:value-of select="$length"/>
155+
(<xsl:value-of select="format-number($identity div $length, '##.#%')"/>),
156+
157+
Positives = <xsl:value-of select="$positive"/>/
158+
<xsl:value-of select="$length"/>
159+
(<xsl:value-of select="format-number($positive div $length, '##.#%')"/>),
160+
161+
Length = <xsl:value-of select="Hsp_align-len/."/>
162+
163+
</div>
164+
165+
<div style="width: 100%; text-align: left; font-family: courier;">
166+
167+
<xsl:call-template name='align'>
168+
<xsl:with-param name='qseq' select='Hsp_qseq/.'/>
169+
<xsl:with-param name='mseq' select='Hsp_midline/.'/>
170+
<xsl:with-param name='hseq' select='Hsp_hseq/.'/>
171+
</xsl:call-template>
172+
173+
</div>
174+
<br/>
175+
176+
</xsl:template>
177+
178+
<xsl:template name='align'>
179+
<xsl:param name='qseq'/>
180+
<xsl:param name='hseq'/>
181+
<xsl:param name='mseq'/>
182+
183+
<br/>
184+
<xsl:if test='string-length($qseq) != 0' >
185+
<xsl:choose>
186+
<xsl:when test='string-length($qseq) &gt; 80 '>
187+
<xsl:call-template name='align80'>
188+
<xsl:with-param name='qseq' select='substring($qseq, 1, 80)'/>
189+
<xsl:with-param name='hseq' select='substring($hseq, 1, 80)'/>
190+
<xsl:with-param name='mseq' select='substring($mseq, 1, 80)'/>
191+
</xsl:call-template>
192+
193+
<xsl:call-template name='align'>
194+
<xsl:with-param name='qseq' select='substring($qseq, 51, string-length($qseq) - 80)'/>
195+
<xsl:with-param name='hseq' select='substring($hseq, 51, string-length($hseq) - 80)'/>
196+
<xsl:with-param name='mseq' select='substring($mseq, 51, string-length($mseq) - 80)'/>
197+
</xsl:call-template>
198+
199+
</xsl:when>
200+
<xsl:otherwise>
201+
<xsl:call-template name='align80'>
202+
<xsl:with-param name='qseq' select='$qseq'/>
203+
<xsl:with-param name='hseq' select='$hseq'/>
204+
<xsl:with-param name='mseq' select='$mseq'/>
205+
</xsl:call-template>
206+
</xsl:otherwise>
207+
</xsl:choose>
208+
</xsl:if>
209+
210+
</xsl:template>
211+
212+
<xsl:template name='align80'>
213+
<xsl:param name='qseq'/>
214+
<xsl:param name='hseq'/>
215+
<xsl:param name='mseq'/>
216+
217+
&#xA0;&#xA0;&#xA0;
218+
<xsl:call-template name='highligth'>
219+
<xsl:with-param name='qseq' select='$qseq'/>
220+
<xsl:with-param name='hseq' select='$hseq'/>
221+
</xsl:call-template>
222+
<br/>
223+
224+
&#xA0;&#xA0;&#xA0;
225+
<xsl:call-template name="whitespace">
226+
<xsl:with-param name="text" select="$mseq"/>
227+
</xsl:call-template>
228+
<br/>
229+
230+
&#xA0;&#xA0;&#xA0;
231+
<xsl:call-template name='highligth'>
232+
<xsl:with-param name='qseq' select='$hseq'/>
233+
<xsl:with-param name='hseq' select='$qseq'/>
234+
</xsl:call-template>
235+
<br/>
236+
237+
</xsl:template>
238+
239+
<xsl:template name='highligth'>
240+
<xsl:param name='qseq'/>
241+
<xsl:param name='hseq'/>
242+
243+
<xsl:if test='string-length($qseq) != 0' >
244+
<xsl:variable name="q_char" select="substring($qseq, 1, 1)" />
245+
<xsl:variable name="h_char" select="substring($hseq, 1, 1)" />
246+
247+
<xsl:choose>
248+
<xsl:when test='$q_char = $h_char'>
249+
<xsl:value-of select="$q_char"/>
250+
</xsl:when>
251+
<xsl:otherwise>
252+
<xsl:call-template name='colorer'>
253+
<xsl:with-param name='char' select='$q_char'/>
254+
</xsl:call-template>
255+
</xsl:otherwise>
256+
</xsl:choose>
257+
258+
<xsl:call-template name='highligth'>
259+
<xsl:with-param name='qseq' select='substring($qseq, 2, string-length($qseq) - 1)'/>
260+
<xsl:with-param name='hseq' select='substring($hseq, 2, string-length($hseq) - 1)'/>
261+
</xsl:call-template>
262+
263+
</xsl:if>
264+
265+
</xsl:template>
266+
267+
<xsl:template name="colorer">
268+
<xsl:param name="char"/>
269+
270+
<xsl:choose>
271+
<xsl:when test="$char='A'">
272+
<font style='background-color: #C8FFC8;'>
273+
<xsl:value-of select="$char"/>
274+
</font>
275+
</xsl:when>
276+
<xsl:when test="$char='C'">
277+
<font style='background-color: #C8FFC8;'>
278+
<xsl:value-of select="$char"/>
279+
</font>
280+
</xsl:when>
281+
<xsl:when test="$char='L'">
282+
<font style='background-color: #C8FFC8;'>
283+
<xsl:value-of select="$char"/>
284+
</font>
285+
</xsl:when>
286+
<xsl:when test="$char='I'">
287+
<font style='background-color: #C8FFC8;'>
288+
<xsl:value-of select="$char"/>
289+
</font>
290+
</xsl:when>
291+
<xsl:when test="$char='V'">
292+
<font style='background-color: #C8FFC8;'>
293+
<xsl:value-of select="$char"/>
294+
</font>
295+
</xsl:when>
296+
<xsl:when test="$char='M'">
297+
<font style='background-color: #C8FFC8;'>
298+
<xsl:value-of select="$char"/>
299+
</font>
300+
</xsl:when>
301+
<xsl:when test="$char='G'">
302+
<font style='background-color: #DCC0FF;'>
303+
<xsl:value-of select="$char"/>
304+
</font>
305+
</xsl:when>
306+
<xsl:when test="$char='P'">
307+
<font style='background-color: #DCC0FF;'>
308+
<xsl:value-of select="$char"/>
309+
</font>
310+
</xsl:when>
311+
<xsl:when test="$char='S'">
312+
<font style='background-color: #FFFCA0;'>
313+
<xsl:value-of select="$char"/>
314+
</font>
315+
</xsl:when>
316+
<xsl:when test="$char='T'">
317+
<font style='background-color: #FFFCA0;'>
318+
<xsl:value-of select="$char"/>
319+
</font>
320+
</xsl:when>
321+
<xsl:when test="$char='N'">
322+
<font style='background-color: #FFFCA0;'>
323+
<xsl:value-of select="$char"/>
324+
</font>
325+
</xsl:when>
326+
<xsl:when test="$char='Q'">
327+
<font style='background-color: #FFFCA0;'>
328+
<xsl:value-of select="$char"/>
329+
</font>
330+
</xsl:when>
331+
<xsl:when test="$char='K'">
332+
<font style='background-color: #FFA2A2;'>
333+
<xsl:value-of select="$char"/>
334+
</font>
335+
</xsl:when>
336+
<xsl:when test="$char='R'">
337+
<font style='background-color: #FFA2A2;'>
338+
<xsl:value-of select="$char"/>
339+
</font>
340+
</xsl:when>
341+
<xsl:when test="$char='D'">
342+
<font style='background-color: #A2E2FF;'>
343+
<xsl:value-of select="$char"/>
344+
</font>
345+
</xsl:when>
346+
<xsl:when test="$char='E'">
347+
<font style='background-color: #A2E2FF;'>
348+
<xsl:value-of select="$char"/>
349+
</font>
350+
</xsl:when>
351+
<xsl:when test="$char='H'">
352+
<font style='background-color: #50FF50;'>
353+
<xsl:value-of select="$char"/>
354+
</font>
355+
</xsl:when>
356+
<xsl:when test="$char='Y'">
357+
<font style='background-color: #50FF50;'>
358+
<xsl:value-of select="$char"/>
359+
</font>
360+
</xsl:when>
361+
<xsl:when test="$char='W'">
362+
<font style='background-color: #50FF50;'>
363+
<xsl:value-of select="$char"/>
364+
</font>
365+
</xsl:when>
366+
<xsl:when test="$char='F'">
367+
<font style='background-color: #50FF50;'>
368+
<xsl:value-of select="$char"/>
369+
</font>
370+
</xsl:when>
371+
372+
</xsl:choose>
373+
</xsl:template>
374+
375+
376+
<xsl:template name="whitespace">
377+
<xsl:param name="text"/>
378+
379+
<xsl:choose>
380+
<xsl:when test="contains($text, ' ')">
381+
382+
<xsl:variable name="before" select="substring-before($text, ' ')"/>
383+
<xsl:variable name="after" select="substring-after($text, ' ')"/>
384+
385+
<xsl:value-of select="$before"/>
386+
<xsl:value-of select="'&#xA0;'"/>
387+
388+
<xsl:call-template name="whitespace">
389+
<xsl:with-param name="text" select="$after"/>
390+
</xsl:call-template>
391+
</xsl:when>
392+
<xsl:otherwise>
393+
<xsl:value-of select="$text"/>
394+
</xsl:otherwise>
395+
</xsl:choose>
396+
</xsl:template>
397+
</xsl:stylesheet>
398+
`

0 commit comments

Comments
 (0)