annotate blast2html.py @ 73:67b1a319c6dc py2.6

First go at 2.6 compatibility The tests are running without errors, but they don't produce the same output as python3
author Jan Kanis <jan.code@jankanis.nl>
date Wed, 18 Jun 2014 16:21:45 +0200
parents 6ecbfebb9dd9
children 03e044b5bcc2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
1 #!/usr/bin/env python3
50
bfc82a8aa3c9 unicodify python sources
Jan Kanis <jan.code@jankanis.nl>
parents: 27
diff changeset
2 # -*- coding: utf-8 -*-
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
3
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
4 # Actually this program works with both python 2 and 3
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
5
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
6 # Copyright The Hyve B.V. 2014
72
6ecbfebb9dd9 doc changes
Jan Kanis <jan.code@jankanis.nl>
parents: 71
diff changeset
7 # License: GPL version 3 or (at your option) any higher version
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
8
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
9 from __future__ import unicode_literals, division
50
bfc82a8aa3c9 unicodify python sources
Jan Kanis <jan.code@jankanis.nl>
parents: 27
diff changeset
10
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
11 import sys
7
9e7927673089 intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents: 5
diff changeset
12 import math
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
13 import warnings
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
14 import six, codecs
18
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
15 from os import path
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
16 from itertools import repeat
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
17 import argparse
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
18 from lxml import objectify
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
19 import jinja2
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
20
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
21
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
22
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
23 _filters = dict()
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
24 def filter(func_or_name):
11
7660519f2dc9 proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents: 10
diff changeset
25 "Decorator to register a function as filter in the current jinja environment"
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
26 if isinstance(func_or_name, six.string_types):
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
27 def inner(func):
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
28 _filters[func_or_name] = func.__name__
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
29 return func
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
30 return inner
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
31 else:
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
32 _filters[func_or_name.__name__] = func_or_name.__name__
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
33 return func_or_name
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
34
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
35
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
36 def color_idx(length):
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
37 if length < 40:
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
38 return 0
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
39 elif length < 50:
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
40 return 1
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
41 elif length < 80:
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
42 return 2
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
43 elif length < 200:
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
44 return 3
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
45 return 4
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
46
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
47 @filter
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
48 def fmt(val, fmt):
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
49 return format(float(val), fmt)
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
50
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
51 @filter
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
52 def firsttitle(hit):
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
53 return hit.Hit_def.text.split('>')[0]
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
54
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
55 @filter
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
56 def othertitles(hit):
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
57 """Split a hit.Hit_def that contains multiple titles up, splitting out the hit ids from the titles."""
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
58 id_titles = hit.Hit_def.text.split('>')
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
59
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
60 titles = []
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
61 for t in id_titles[1:]:
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
62 fullid, title = t.split(' ', 1)
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
63 hitid, id = fullid.split('|', 2)[1:3]
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
64 titles.append(dict(id = id,
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
65 hitid = hitid,
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
66 fullid = fullid,
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
67 title = title))
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
68 return titles
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
69
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
70 @filter
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
71 def hitid(hit):
24
c8347745bbad use Iteration_message tag; also work with unexpected Hit_id values
Jan Kanis <jan.code@jankanis.nl>
parents: 23
diff changeset
72 hitid = hit.Hit_id.text
c8347745bbad use Iteration_message tag; also work with unexpected Hit_id values
Jan Kanis <jan.code@jankanis.nl>
parents: 23
diff changeset
73 s = hitid.split('|', 2)
c8347745bbad use Iteration_message tag; also work with unexpected Hit_id values
Jan Kanis <jan.code@jankanis.nl>
parents: 23
diff changeset
74 if len(s) >= 2:
c8347745bbad use Iteration_message tag; also work with unexpected Hit_id values
Jan Kanis <jan.code@jankanis.nl>
parents: 23
diff changeset
75 return s[1]
c8347745bbad use Iteration_message tag; also work with unexpected Hit_id values
Jan Kanis <jan.code@jankanis.nl>
parents: 23
diff changeset
76 return hitid
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
77
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
78 @filter
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
79 def seqid(hit):
24
c8347745bbad use Iteration_message tag; also work with unexpected Hit_id values
Jan Kanis <jan.code@jankanis.nl>
parents: 23
diff changeset
80 hitid = hit.Hit_id.text
c8347745bbad use Iteration_message tag; also work with unexpected Hit_id values
Jan Kanis <jan.code@jankanis.nl>
parents: 23
diff changeset
81 s = hitid.split('|', 2)
c8347745bbad use Iteration_message tag; also work with unexpected Hit_id values
Jan Kanis <jan.code@jankanis.nl>
parents: 23
diff changeset
82 if len(s) >= 3:
c8347745bbad use Iteration_message tag; also work with unexpected Hit_id values
Jan Kanis <jan.code@jankanis.nl>
parents: 23
diff changeset
83 return s[2]
c8347745bbad use Iteration_message tag; also work with unexpected Hit_id values
Jan Kanis <jan.code@jankanis.nl>
parents: 23
diff changeset
84 return hitid
c8347745bbad use Iteration_message tag; also work with unexpected Hit_id values
Jan Kanis <jan.code@jankanis.nl>
parents: 23
diff changeset
85
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
86
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
87 @filter
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
88 def alignment_pre(hsp):
68
fa8a93bdefd7 fix bug in calculations of alignment end
Jan Kanis <jan.code@jankanis.nl>
parents: 67
diff changeset
89 """Create the preformatted alignment blocks"""
71
371cd585e459 refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 69
diff changeset
90
371cd585e459 refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 69
diff changeset
91 # line break length
371cd585e459 refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 69
diff changeset
92 linewidth = 60
67
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
93
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
94 qfrom = int(hsp['Hsp_query-from'])
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
95 qto = int(hsp['Hsp_query-to'])
69
0c4ac210068b handle reverse matches
Jan Kanis <jan.code@jankanis.nl>
parents: 68
diff changeset
96 qframe = int(hsp['Hsp_query-frame'])
67
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
97 hfrom = int(hsp['Hsp_hit-from'])
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
98 hto = int(hsp['Hsp_hit-to'])
69
0c4ac210068b handle reverse matches
Jan Kanis <jan.code@jankanis.nl>
parents: 68
diff changeset
99 hframe = int(hsp['Hsp_hit-frame'])
71
371cd585e459 refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 69
diff changeset
100
67
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
101 qseq = hsp.Hsp_qseq.text
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
102 midline = hsp.Hsp_midline.text
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
103 hseq = hsp.Hsp_hseq.text
69
0c4ac210068b handle reverse matches
Jan Kanis <jan.code@jankanis.nl>
parents: 68
diff changeset
104
71
371cd585e459 refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 69
diff changeset
105 if not qframe in (1, -1):
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
106 warnings.warn("Error in BlastXML input: Hsp node {0} has a Hsp_query-frame of {1}. (should be 1 or -1)".format(nodeid(hsp), qframe))
69
0c4ac210068b handle reverse matches
Jan Kanis <jan.code@jankanis.nl>
parents: 68
diff changeset
107 qframe = -1 if qframe < 0 else 1
71
371cd585e459 refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 69
diff changeset
108 if not hframe in (1, -1):
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
109 warnings.warn("Error in BlastXML input: Hsp node {0} has a Hsp_hit-frame of {1}. (should be 1 or -1)".format(nodeid(hsp), hframe))
69
0c4ac210068b handle reverse matches
Jan Kanis <jan.code@jankanis.nl>
parents: 68
diff changeset
110 hframe = -1 if hframe < 0 else 1
67
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
111
69
0c4ac210068b handle reverse matches
Jan Kanis <jan.code@jankanis.nl>
parents: 68
diff changeset
112 def split(txt):
71
371cd585e459 refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 69
diff changeset
113 return [txt[i:i+linewidth] for i in range(0, len(txt), linewidth)]
69
0c4ac210068b handle reverse matches
Jan Kanis <jan.code@jankanis.nl>
parents: 68
diff changeset
114
71
371cd585e459 refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 69
diff changeset
115 for qs, mid, hs, offset in zip(split(qseq), split(midline), split(hseq), range(0, len(qseq), linewidth)):
67
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
116 yield (
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
117 "Query {0:>7} {1} {2}\n".format(qfrom+offset*qframe, qs, qfrom+(offset+len(qs)-1)*qframe) +
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
118 " {0:7} {1}\n".format('', mid) +
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
119 "Subject{0:>7} {1} {2}".format(hfrom+offset*hframe, hs, hfrom+(offset+len(hs)-1)*hframe)
67
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
120 )
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
121
69
0c4ac210068b handle reverse matches
Jan Kanis <jan.code@jankanis.nl>
parents: 68
diff changeset
122 if qfrom+(len(qseq)-1)*qframe != qto:
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
123 warnings.warn("Error in BlastXML input: Hsp node {0} qseq length mismatch: from {1} to {2} length {3}".format(
67
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
124 nodeid(hsp), qfrom, qto, len(qseq)))
69
0c4ac210068b handle reverse matches
Jan Kanis <jan.code@jankanis.nl>
parents: 68
diff changeset
125 if hfrom+(len(hseq)-1)*hframe != hto:
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
126 warnings.warn("Error in BlastXML input: Hsp node {0} hseq length mismatch: from {1} to {2} length {3}".format(
67
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
127 nodeid(hsp), hfrom, hto, len(hseq)))
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
128
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
129
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
130
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
131 @filter('len')
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
132 def blastxml_len(node):
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
133 if node.tag == 'Hsp':
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
134 return int(node['Hsp_align-len'])
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
135 elif node.tag == 'Iteration':
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
136 return int(node['Iteration_query-len'])
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
137 raise Exception("Unknown XML node type: "+node.tag)
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
138
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
139 @filter
53
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
140 def nodeid(node):
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
141 id = []
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
142 if node.tag == 'Hsp':
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
143 id.insert(0, node.Hsp_num.text)
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
144 node = node.getparent().getparent()
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
145 assert node.tag == 'Hit'
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
146 if node.tag == 'Hit':
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
147 id.insert(0, node.Hit_num.text)
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
148 node = node.getparent().getparent()
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
149 assert node.tag == 'Iteration'
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
150 if node.tag == 'Iteration':
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
151 id.insert(0, node['Iteration_iter-num'].text)
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
152 return '-'.join(id)
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
153 raise ValueError("The nodeid filter can only be applied to Hsp, Hit or Iteration nodes in a BlastXML document")
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
154
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
155
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
156 @filter
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
157 def asframe(frame):
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
158 if frame == 1:
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
159 return 'Plus'
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
160 elif frame == -1:
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
161 return 'Minus'
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
162 raise Exception("frame should be either +1 or -1")
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
163
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
164 def genelink(hit, type='genbank', hsp=None):
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
165 if not isinstance(hit, six.string_types):
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
166 hit = hitid(hit)
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
167 link = "http://www.ncbi.nlm.nih.gov/nucleotide/{0}?report={1}&log$=nuclalign".format(hit, type)
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
168 if hsp != None:
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
169 link += "&from={0}&to={1}".format(hsp['Hsp_hit-from'], hsp['Hsp_hit-to'])
16
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
170 return link
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
171
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
172
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
173 # javascript escape filter based on Django's, from https://github.com/dsissitka/khan-website/blob/master/templatefilters.py#L112-139
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
174 # I've removed the html escapes, since html escaping is already being performed by the template engine.
7
9e7927673089 intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents: 5
diff changeset
175
16
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
176 _base_js_escapes = (
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
177 ('\\', r'\u005C'),
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
178 ('\'', r'\u0027'),
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
179 ('"', r'\u0022'),
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
180 # ('>', r'\u003E'),
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
181 # ('<', r'\u003C'),
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
182 # ('&', r'\u0026'),
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
183 # ('=', r'\u003D'),
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
184 # ('-', r'\u002D'),
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
185 # (';', r'\u003B'),
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
186 # (u'\u2028', r'\u2028'),
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
187 # (u'\u2029', r'\u2029')
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
188 )
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
189
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
190 # Escape every ASCII character with a value less than 32. This is
18
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
191 # needed a.o. to prevent html parsers from jumping out of javascript
16
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
192 # parsing mode.
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
193 _js_escapes = (_base_js_escapes +
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
194 tuple(('%c' % z, '\\u%04X' % z) for z in range(32)))
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
195
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
196 @filter
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
197 def js_string_escape(value):
72
6ecbfebb9dd9 doc changes
Jan Kanis <jan.code@jankanis.nl>
parents: 71
diff changeset
198 """
6ecbfebb9dd9 doc changes
Jan Kanis <jan.code@jankanis.nl>
parents: 71
diff changeset
199 Javascript string literal escape. Note that this only escapes data
6ecbfebb9dd9 doc changes
Jan Kanis <jan.code@jankanis.nl>
parents: 71
diff changeset
200 for embedding within javascript string literals, not in general
6ecbfebb9dd9 doc changes
Jan Kanis <jan.code@jankanis.nl>
parents: 71
diff changeset
201 javascript snippets.
6ecbfebb9dd9 doc changes
Jan Kanis <jan.code@jankanis.nl>
parents: 71
diff changeset
202 """
16
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
203
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
204 value = six.text_type(value)
16
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
205
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
206 for bad, good in _js_escapes:
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
207 value = value.replace(bad, good)
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
208
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
209 return value
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
210
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
211 @filter
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
212 def hits(result):
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
213 # sort hits by longest hotspot first
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
214 return sorted(result.Iteration_hits.findall('Hit'),
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
215 key=lambda h: max(blastxml_len(hsp) for hsp in h.Hit_hsps.Hsp),
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
216 reverse=True)
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
217
7
9e7927673089 intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents: 5
diff changeset
218
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
219
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
220 class BlastVisualize:
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
221
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
222 colors = ('black', 'blue', 'green', 'magenta', 'red')
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
223
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
224 max_scale_labels = 10
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
225
18
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
226 def __init__(self, input, templatedir, templatename):
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
227 self.input = input
18
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
228 self.templatename = templatename
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
229
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
230 self.blast = objectify.parse(self.input).getroot()
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
231 self.loader = jinja2.FileSystemLoader(searchpath=templatedir, encoding='utf-8')
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
232 self.environment = jinja2.Environment(loader=self.loader,
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
233 lstrip_blocks=True, trim_blocks=True, autoescape=True)
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
234
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
235 self._addfilters(self.environment)
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
236
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
237
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
238 def _addfilters(self, environment):
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
239 for filtername, funcname in _filters.items():
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
240 try:
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
241 environment.filters[filtername] = getattr(self, funcname)
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
242 except AttributeError:
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
243 environment.filters[filtername] = globals()[funcname]
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
244
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
245 def render(self, output):
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
246 template = self.environment.get_template(self.templatename)
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
247
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
248 params = (('Query ID', self.blast["BlastOutput_query-ID"]),
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
249 ('Query definition', self.blast["BlastOutput_query-def"]),
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
250 ('Query length', self.blast["BlastOutput_query-len"]),
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
251 ('Program', self.blast.BlastOutput_version),
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
252 ('Database', self.blast.BlastOutput_db),
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
253 )
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
254
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
255 result = template.render(blast=self.blast,
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
256 iterations=self.blast.BlastOutput_iterations.Iteration,
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
257 colors=self.colors,
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
258 # match_colors=self.match_colors(),
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
259 # hit_info=self.hit_info(),
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
260 genelink=genelink,
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
261 params=params)
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
262 if six.PY2:
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
263 result = result.encode('utf-8')
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
264 output.write(result)
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
265
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
266 @filter
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
267 def match_colors(self, result):
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
268 """
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
269 An iterator that yields lists of length-color pairs.
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
270 """
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
271
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
272 query_length = blastxml_len(result)
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
273
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
274 percent_multiplier = 100 / query_length
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
275
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
276 for hit in hits(result):
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
277 # sort hotspots from short to long, so we can overwrite index colors of
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
278 # short matches with those of long ones.
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
279 hotspots = sorted(hit.Hit_hsps.Hsp, key=lambda hsp: blastxml_len(hsp))
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
280 table = bytearray([255]) * query_length
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
281 for hsp in hotspots:
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
282 frm = hsp['Hsp_query-from'] - 1
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
283 to = int(hsp['Hsp_query-to'])
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
284 table[frm:to] = repeat(color_idx(blastxml_len(hsp)), to - frm)
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
285
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
286 matches = []
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
287 last = table[0]
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
288 count = 0
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
289 for i in range(query_length):
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
290 if table[i] == last:
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
291 count += 1
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
292 continue
16
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
293 matches.append((count * percent_multiplier, self.colors[last] if last != 255 else 'transparent'))
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
294 last = table[i]
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
295 count = 1
16
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
296 matches.append((count * percent_multiplier, self.colors[last] if last != 255 else 'transparent'))
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
297
53
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
298 yield dict(colors=matches, hit=hit, defline=firsttitle(hit))
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
299
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
300 @filter
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
301 def queryscale(self, result):
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
302 query_length = blastxml_len(result)
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
303 skip = math.ceil(query_length / self.max_scale_labels)
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
304 percent_multiplier = 100 / query_length
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
305 for i in range(1, query_length+1):
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
306 if i % skip == 0:
23
6995a6f34f3f fix sectioning, add footer
Jan Kanis <jan.code@jankanis.nl>
parents: 22
diff changeset
307 yield dict(label = i, width = skip * percent_multiplier)
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
308 if query_length % skip != 0:
20
53cd304c5f26 Add index for multiple results; fix layout of query ruler for edge case
Jan Kanis <jan.code@jankanis.nl>
parents: 19
diff changeset
309 yield dict(label = query_length,
23
6995a6f34f3f fix sectioning, add footer
Jan Kanis <jan.code@jankanis.nl>
parents: 22
diff changeset
310 width = (query_length % skip) * percent_multiplier)
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
311
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
312 @filter
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
313 def hit_info(self, result):
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
314
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
315 query_length = blastxml_len(result)
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
316
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
317 for hit in hits(result):
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
318 hsps = hit.Hit_hsps.Hsp
7
9e7927673089 intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents: 5
diff changeset
319
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
320 cover = [False] * query_length
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
321 for hsp in hsps:
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
322 cover[hsp['Hsp_query-from']-1 : int(hsp['Hsp_query-to'])] = repeat(True, blastxml_len(hsp))
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
323 cover_count = cover.count(True)
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
324
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
325 def hsp_val(path):
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
326 return (float(hsp[path]) for hsp in hsps)
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
327
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
328 yield dict(hit = hit,
53
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
329 title = firsttitle(hit),
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
330 maxscore = "{0:.1f}".format(max(hsp_val('Hsp_bit-score'))),
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
331 totalscore = "{0:.1f}".format(sum(hsp_val('Hsp_bit-score'))),
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
332 cover = "{0:.0%}".format(cover_count / query_length),
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
333 e_value = "{0:.4g}".format(min(hsp_val('Hsp_evalue'))),
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
334 # FIXME: is this the correct formula vv?
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
335 ident = "{0:.0%}".format(float(min(hsp.Hsp_identity / blastxml_len(hsp) for hsp in hsps))),
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
336 accession = hit.Hit_accession)
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
337
27
4e6ac737ba17 improve the galaxy html stripping warning; make sure the tool can find the template from within galaxy
Jan Kanis <jan.code@jankanis.nl>
parents: 24
diff changeset
338
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
339 def main():
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
340 #import pdb; pdb.set_trace()
27
4e6ac737ba17 improve the galaxy html stripping warning; make sure the tool can find the template from within galaxy
Jan Kanis <jan.code@jankanis.nl>
parents: 24
diff changeset
341 default_template = path.join(path.dirname(__file__), 'blast2html.html.jinja')
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
342
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
343 parser = argparse.ArgumentParser(description="Convert a BLAST XML result into a nicely readable html page",
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
344 usage="{0} [-i] INPUT [-o OUTPUT]".format(sys.argv[0]))
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
345 input_group = parser.add_mutually_exclusive_group(required=True)
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
346 input_group.add_argument('positional_arg', metavar='INPUT', nargs='?', type=argparse.FileType(mode='r'),
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
347 help='The input Blast XML file, same as -i/--input')
53
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
348 input_group.add_argument('-i', '--input', type=argparse.FileType(mode='r'),
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
349 help='The input Blast XML file')
53
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
350 parser.add_argument('-o', '--output', type=argparse.FileType(mode='w'), default=sys.stdout,
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
351 help='The output html file')
18
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
352 # We just want the file name here, so jinja can open the file
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
353 # itself. But it is easier to just use a FileType so argparse can
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
354 # handle the errors. This introduces a small race condition when
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
355 # jinja later tries to re-open the template file, but we don't
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
356 # care too much.
53
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
357 parser.add_argument('--template', type=argparse.FileType(mode='r'), default=default_template,
18
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
358 help='The template file to use. Defaults to blast_html.html.jinja')
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
359
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
360 args = parser.parse_args()
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
361 if args.input == None:
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
362 args.input = args.positional_arg
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
363 if args.input == None:
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
364 parser.error('no input specified')
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
365
18
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
366 templatedir, templatename = path.split(args.template.name)
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
367 args.template.close()
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
368 if not templatedir:
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
369 templatedir = '.'
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
370
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
371 b = BlastVisualize(args.input, templatedir, templatename)
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
372 b.render(args.output)
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
373
11
7660519f2dc9 proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents: 10
diff changeset
374
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
375 if __name__ == '__main__':
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
376 main()
11
7660519f2dc9 proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents: 10
diff changeset
377