annotate blast2html.py @ 118:7f3f8c10f44b

fix for Rikilt issues 8, 11, 12, 14
author Jan Kanis <jan.code@jankanis.nl>
date Thu, 31 Jul 2014 13:09:30 +0200
parents f5066973029a
children 591dc9c24824
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
75
4d2c25baf5a3 Fix rounding errors
Jan Kanis <jan.code@jankanis.nl>
parents: 74
diff changeset
4 # Actually this program works with both python 2 and 3, tested against python 2.6
73
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
104
a22c909c9b57 stream output
Jan Kanis <jan.code@jankanis.nl>
parents: 103
diff changeset
14 import six, codecs, io
75
4d2c25baf5a3 Fix rounding errors
Jan Kanis <jan.code@jankanis.nl>
parents: 74
diff changeset
15 from six.moves import builtins
18
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
16 from os import path
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
17 from itertools import repeat
115
0c2a03f9740b make external gene bank name configurable
Jan Kanis <jan.code@jankanis.nl>
parents: 110
diff changeset
18 from collections import defaultdict, namedtuple
99
8f02008a5f20 look at all blast*.loc files; python2.6 compat fix
Jan Kanis <jan.code@jankanis.nl>
parents: 98
diff changeset
19 import glob
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
20 import argparse
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
21 from lxml import objectify
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
22 import jinja2
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
23
95
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
24 builtin_str = str
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
25 str = six.text_type
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
26
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
27
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
28
76
7d0d46168fd5 Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents: 75
diff changeset
29 _filters = dict(float='float')
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
30 def filter(func_or_name):
11
7660519f2dc9 proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents: 10
diff changeset
31 "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
32 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
33 def inner(func):
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
34 _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
35 return func
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
36 return inner
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
37 else:
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
38 _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
39 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
40
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
41
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
42 def color_idx(length):
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
43 if length < 40:
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
44 return 0
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
45 elif length < 50:
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
46 return 1
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
47 elif length < 80:
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
48 return 2
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
49 elif length < 200:
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
50 return 3
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
51 return 4
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
52
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
53 @filter
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
54 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
55 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
56
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
57 @filter
76
7d0d46168fd5 Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents: 75
diff changeset
58 def numfmt(val):
7d0d46168fd5 Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents: 75
diff changeset
59 """Format numbers in decimal notation, but without excessive trailing 0's.
7d0d46168fd5 Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents: 75
diff changeset
60 Default python float formatting will use scientific notation for some values,
7d0d46168fd5 Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents: 75
diff changeset
61 or append trailing zeros with the 'f' format type, and the number of digits differs
7d0d46168fd5 Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents: 75
diff changeset
62 between python 2 and 3."""
7d0d46168fd5 Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents: 75
diff changeset
63 fpart, ipart = math.modf(val)
7d0d46168fd5 Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents: 75
diff changeset
64 if fpart == 0:
7d0d46168fd5 Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents: 75
diff changeset
65 return str(int(val))
7d0d46168fd5 Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents: 75
diff changeset
66 # round to 10 to get identical representations in python 2 and 3
7d0d46168fd5 Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents: 75
diff changeset
67 s = format(round(val, 10), '.10f').rstrip('0')
7d0d46168fd5 Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents: 75
diff changeset
68 if s[-1] == '.':
7d0d46168fd5 Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents: 75
diff changeset
69 s += '0'
7d0d46168fd5 Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents: 75
diff changeset
70 return s
7d0d46168fd5 Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents: 75
diff changeset
71
7d0d46168fd5 Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents: 75
diff changeset
72 @filter
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
73 def firsttitle(hit):
96
02b795b784e1 fix bug; add comments
Jan Kanis <jan.code@jankanis.nl>
parents: 95
diff changeset
74 return str(hit.Hit_def).split('>')[0]
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
75
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
76 @filter
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
77 def othertitles(hit):
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
78 """Split a hit.Hit_def that contains multiple titles up, splitting out the hit ids from the titles."""
96
02b795b784e1 fix bug; add comments
Jan Kanis <jan.code@jankanis.nl>
parents: 95
diff changeset
79 id_titles = str(hit.Hit_def).split('>')
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
80
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
81 titles = []
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
82 for t in id_titles[1:]:
95
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
83 id, title = t.split(' ', 1)
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
84 titles.append(argparse.Namespace(Hit_id = id,
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
85 Hit_def = title,
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
86 Hit_accession = '',
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
87 getroottree = hit.getroottree))
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
88 return titles
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
89
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
90 @filter
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
91 def hitid(hit):
95
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
92 return str(hit.Hit_id)
24
c8347745bbad use Iteration_message tag; also work with unexpected Hit_id values
Jan Kanis <jan.code@jankanis.nl>
parents: 23
diff changeset
93
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
94
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
95 @filter
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
96 def alignment_pre(hsp):
68
fa8a93bdefd7 fix bug in calculations of alignment end
Jan Kanis <jan.code@jankanis.nl>
parents: 67
diff changeset
97 """Create the preformatted alignment blocks"""
71
371cd585e459 refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 69
diff changeset
98
371cd585e459 refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 69
diff changeset
99 # line break length
371cd585e459 refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 69
diff changeset
100 linewidth = 60
67
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
101
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
102 qfrom = int(hsp['Hsp_query-from'])
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
103 qto = int(hsp['Hsp_query-to'])
69
0c4ac210068b handle reverse matches
Jan Kanis <jan.code@jankanis.nl>
parents: 68
diff changeset
104 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
105 hfrom = int(hsp['Hsp_hit-from'])
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
106 hto = int(hsp['Hsp_hit-to'])
69
0c4ac210068b handle reverse matches
Jan Kanis <jan.code@jankanis.nl>
parents: 68
diff changeset
107 hframe = int(hsp['Hsp_hit-frame'])
71
371cd585e459 refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 69
diff changeset
108
67
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
109 qseq = hsp.Hsp_qseq.text
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
110 midline = hsp.Hsp_midline.text
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
111 hseq = hsp.Hsp_hseq.text
69
0c4ac210068b handle reverse matches
Jan Kanis <jan.code@jankanis.nl>
parents: 68
diff changeset
112
71
371cd585e459 refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 69
diff changeset
113 if not qframe in (1, -1):
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
114 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
115 qframe = -1 if qframe < 0 else 1
71
371cd585e459 refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 69
diff changeset
116 if not hframe in (1, -1):
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
117 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
118 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
119
69
0c4ac210068b handle reverse matches
Jan Kanis <jan.code@jankanis.nl>
parents: 68
diff changeset
120 def split(txt):
71
371cd585e459 refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 69
diff changeset
121 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
122
71
371cd585e459 refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 69
diff changeset
123 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
124 yield (
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
125 "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
126 " {0:7} {1}\n".format('', mid) +
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
127 "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
128 )
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
129
69
0c4ac210068b handle reverse matches
Jan Kanis <jan.code@jankanis.nl>
parents: 68
diff changeset
130 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
131 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
132 nodeid(hsp), qfrom, qto, len(qseq)))
69
0c4ac210068b handle reverse matches
Jan Kanis <jan.code@jankanis.nl>
parents: 68
diff changeset
133 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
134 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
135 nodeid(hsp), hfrom, hto, len(hseq)))
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
136
19c48f2ec775 wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents: 53
diff changeset
137
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('len')
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
140 def blastxml_len(node):
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
141 if node.tag == 'Hsp':
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
142 return int(node['Hsp_align-len'])
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
143 elif node.tag == 'Iteration':
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
144 return int(node['Iteration_query-len'])
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
145 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
146
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
147 @filter
53
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
148 def nodeid(node):
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
149 id = []
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 == 'Hsp':
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.Hsp_num.text)
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
152 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
153 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
154 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
155 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
156 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
157 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
158 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
159 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
160 return '-'.join(id)
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
161 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
162
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
163
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
164 @filter
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
165 def asframe(frame):
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
166 if frame == 1:
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
167 return 'Plus'
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
168 elif frame == -1:
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
169 return 'Minus'
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
170 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
171
95
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
172 # def genelink(hit, type='genbank', hsp=None):
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
173 # if not isinstance(hit, six.string_types):
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
174 # hit = hitid(hit)
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
175 # link = "http://www.ncbi.nlm.nih.gov/nucleotide/{0}?report={1}&log$=nuclalign".format(hit, type)
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
176 # if hsp != None:
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
177 # link += "&from={0}&to={1}".format(hsp['Hsp_hit-from'], hsp['Hsp_hit-to'])
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
178 # return link
16
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
179
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
180
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
181 # 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
182 # 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
183
74
03e044b5bcc2 fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents: 73
diff changeset
184 # The r'\u0027' syntax doesn't work the way we need to in python 2.6 with unicode_literals
16
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
185 _base_js_escapes = (
74
03e044b5bcc2 fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents: 73
diff changeset
186 ('\\', '\\u005C'),
03e044b5bcc2 fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents: 73
diff changeset
187 ('\'', '\\u0027'),
03e044b5bcc2 fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents: 73
diff changeset
188 ('"', '\\u0022'),
03e044b5bcc2 fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents: 73
diff changeset
189 # ('>', '\\u003E'),
03e044b5bcc2 fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents: 73
diff changeset
190 # ('<', '\\u003C'),
03e044b5bcc2 fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents: 73
diff changeset
191 # ('&', '\\u0026'),
03e044b5bcc2 fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents: 73
diff changeset
192 # ('=', '\\u003D'),
03e044b5bcc2 fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents: 73
diff changeset
193 # ('-', '\\u002D'),
03e044b5bcc2 fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents: 73
diff changeset
194 # (';', '\\u003B'),
03e044b5bcc2 fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents: 73
diff changeset
195 (u'\u2028', '\\u2028'),
03e044b5bcc2 fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents: 73
diff changeset
196 (u'\u2029', '\\u2029')
16
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
197 )
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
198
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
199 # 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
200 # 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
201 # parsing mode.
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
202 _js_escapes = (_base_js_escapes +
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
203 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
204
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
205 @filter
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
206 def js_string_escape(value):
72
6ecbfebb9dd9 doc changes
Jan Kanis <jan.code@jankanis.nl>
parents: 71
diff changeset
207 """
6ecbfebb9dd9 doc changes
Jan Kanis <jan.code@jankanis.nl>
parents: 71
diff changeset
208 Javascript string literal escape. Note that this only escapes data
6ecbfebb9dd9 doc changes
Jan Kanis <jan.code@jankanis.nl>
parents: 71
diff changeset
209 for embedding within javascript string literals, not in general
6ecbfebb9dd9 doc changes
Jan Kanis <jan.code@jankanis.nl>
parents: 71
diff changeset
210 javascript snippets.
6ecbfebb9dd9 doc changes
Jan Kanis <jan.code@jankanis.nl>
parents: 71
diff changeset
211 """
16
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
212
95
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
213 value = str(value)
16
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
214
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
215 for bad, good in _js_escapes:
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
216 value = value.replace(bad, good)
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
217
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
218 return value
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
219
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
220 @filter
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
221 def hits(result):
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
222 # sort hits by longest hotspot first
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
223 return sorted(result.Iteration_hits.findall('Hit'),
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
224 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
225 reverse=True)
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
226
110
e17aae23cc1c report query parameters for each query
Jan Kanis <jan.code@jankanis.nl>
parents: 109
diff changeset
227 @filter('params')
e17aae23cc1c report query parameters for each query
Jan Kanis <jan.code@jankanis.nl>
parents: 109
diff changeset
228 def result_params(iteration):
e17aae23cc1c report query parameters for each query
Jan Kanis <jan.code@jankanis.nl>
parents: 109
diff changeset
229 return (('Query number', iteration['Iteration_iter-num']),
e17aae23cc1c report query parameters for each query
Jan Kanis <jan.code@jankanis.nl>
parents: 109
diff changeset
230 ('Query ID', iteration['Iteration_query-ID']),
e17aae23cc1c report query parameters for each query
Jan Kanis <jan.code@jankanis.nl>
parents: 109
diff changeset
231 ('Definition line', iteration['Iteration_query-def']),
e17aae23cc1c report query parameters for each query
Jan Kanis <jan.code@jankanis.nl>
parents: 109
diff changeset
232 ('Length', blastxml_len(iteration)))
7
9e7927673089 intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents: 5
diff changeset
233
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
234
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
235 class BlastVisualize:
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
236
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
237 colors = ('black', 'blue', 'green', 'magenta', 'red')
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
238
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
239 max_scale_labels = 10
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
240
116
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
241 def __init__(self, input, templatedir, templatename, genelinks):
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
242 self.input = input
18
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
243 self.templatename = templatename
95
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
244 self.genelinks = genelinks
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
245
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
246 self.blast = objectify.parse(self.input).getroot()
79
9fb1a7d67317 remove unneeded encoding parameter
Jan Kanis <jan.code@jankanis.nl>
parents: 78
diff changeset
247 self.loader = jinja2.FileSystemLoader(searchpath=templatedir)
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
248 self.environment = jinja2.Environment(loader=self.loader,
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
249 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
250
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
251 self._addfilters(self.environment)
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
252
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
253
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
254 def _addfilters(self, environment):
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
255 for filtername, funcname in _filters.items():
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
256 try:
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
257 environment.filters[filtername] = getattr(self, funcname)
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
258 except AttributeError:
75
4d2c25baf5a3 Fix rounding errors
Jan Kanis <jan.code@jankanis.nl>
parents: 74
diff changeset
259 try:
4d2c25baf5a3 Fix rounding errors
Jan Kanis <jan.code@jankanis.nl>
parents: 74
diff changeset
260 environment.filters[filtername] = globals()[funcname]
4d2c25baf5a3 Fix rounding errors
Jan Kanis <jan.code@jankanis.nl>
parents: 74
diff changeset
261 except KeyError:
4d2c25baf5a3 Fix rounding errors
Jan Kanis <jan.code@jankanis.nl>
parents: 74
diff changeset
262 environment.filters[filtername] = getattr(builtins, funcname)
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
263
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
264 def render(self, output):
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
265 template = self.environment.get_template(self.templatename)
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
266
110
e17aae23cc1c report query parameters for each query
Jan Kanis <jan.code@jankanis.nl>
parents: 109
diff changeset
267 params = (('Program', self.blast.BlastOutput_version),
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
268 ('Database', self.blast.BlastOutput_db),
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
269 )
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
270
104
a22c909c9b57 stream output
Jan Kanis <jan.code@jankanis.nl>
parents: 103
diff changeset
271 result = template.stream(blast=self.blast,
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
272 iterations=self.blast.BlastOutput_iterations.Iteration,
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
273 colors=self.colors,
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
274 params=params)
104
a22c909c9b57 stream output
Jan Kanis <jan.code@jankanis.nl>
parents: 103
diff changeset
275
a22c909c9b57 stream output
Jan Kanis <jan.code@jankanis.nl>
parents: 103
diff changeset
276 result.dump(output)
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
277
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
278 @filter
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
279 def match_colors(self, result):
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
280 """
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
281 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
282 """
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
283
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
284 query_length = blastxml_len(result)
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
285
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
286 percent_multiplier = 100 / query_length
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
287
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
288 for hit in hits(result):
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
289 # 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
290 # 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
291 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
292 table = bytearray([255]) * query_length
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
293 for hsp in hotspots:
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
294 frm = hsp['Hsp_query-from'] - 1
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
295 to = int(hsp['Hsp_query-to'])
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
296 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
297
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
298 matches = []
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
299 last = table[0]
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
300 count = 0
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
301 for i in range(query_length):
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
302 if table[i] == last:
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
303 count += 1
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
304 continue
16
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
305 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
306 last = table[i]
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
307 count = 1
16
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
308 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
309
53
4217bb9cf1d3 depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents: 51
diff changeset
310 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
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 queryscale(self, result):
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
314 query_length = blastxml_len(result)
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
315 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
316 percent_multiplier = 100 / query_length
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
317 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
318 if i % skip == 0:
23
6995a6f34f3f fix sectioning, add footer
Jan Kanis <jan.code@jankanis.nl>
parents: 22
diff changeset
319 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
320 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
321 yield dict(label = query_length,
23
6995a6f34f3f fix sectioning, add footer
Jan Kanis <jan.code@jankanis.nl>
parents: 22
diff changeset
322 width = (query_length % skip) * percent_multiplier)
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
323
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
324 @filter
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
325 def hit_info(self, result):
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
326
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
327 query_length = blastxml_len(result)
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
328
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
329 for hit in hits(result):
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
330 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
331
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
332 cover = [False] * query_length
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
333 for hsp in hsps:
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
334 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
335 cover_count = cover.count(True)
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
336
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
337 def hsp_val(path):
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
338 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
339
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
340 yield dict(hit = hit,
75
4d2c25baf5a3 Fix rounding errors
Jan Kanis <jan.code@jankanis.nl>
parents: 74
diff changeset
341 title = firsttitle(hit),
73
67b1a319c6dc First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents: 72
diff changeset
342 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
343 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
344 cover = "{0:.0%}".format(cover_count / query_length),
118
7f3f8c10f44b fix for Rikilt issues 8, 11, 12, 14
Jan Kanis <jan.code@jankanis.nl>
parents: 116
diff changeset
345 e_value = "{0:.4}".format(float(min(hsp_val('Hsp_evalue')))),
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
346 # FIXME: is this the correct formula vv?
78
c0804e6443c6 comment
Jan Kanis <jan.code@jankanis.nl>
parents: 76
diff changeset
347 # float(...) because non-flooring division doesn't work with lxml elements in python 2.6
95
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
348 ident = "{0:.0%}".format(float(min(float(hsp.Hsp_identity) / blastxml_len(hsp) for hsp in hsps))))
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
349
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
350 @filter
115
0c2a03f9740b make external gene bank name configurable
Jan Kanis <jan.code@jankanis.nl>
parents: 110
diff changeset
351 def genelink(self, hit, text=None, text_from='hitid', cssclass=None, display_nolink=True):
96
02b795b784e1 fix bug; add comments
Jan Kanis <jan.code@jankanis.nl>
parents: 95
diff changeset
352 """Create a html link from a hit node to a configured gene bank webpage.
115
0c2a03f9740b make external gene bank name configurable
Jan Kanis <jan.code@jankanis.nl>
parents: 110
diff changeset
353 text: The text of the link. If not set applies text_from.
0c2a03f9740b make external gene bank name configurable
Jan Kanis <jan.code@jankanis.nl>
parents: 110
diff changeset
354 text_from: string, if text is not specified, take it from specified source. Either 'hitid' (default) or 'dbname'.
0c2a03f9740b make external gene bank name configurable
Jan Kanis <jan.code@jankanis.nl>
parents: 110
diff changeset
355 cssclass: extra css classes that will be added to the <a> element
96
02b795b784e1 fix bug; add comments
Jan Kanis <jan.code@jankanis.nl>
parents: 95
diff changeset
356 display_nolink: boolean, if false don't display anything if no link can be created. Default True.
02b795b784e1 fix bug; add comments
Jan Kanis <jan.code@jankanis.nl>
parents: 95
diff changeset
357 """
02b795b784e1 fix bug; add comments
Jan Kanis <jan.code@jankanis.nl>
parents: 95
diff changeset
358
95
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
359 db = hit.getroottree().getroot().BlastOutput_db
96
02b795b784e1 fix bug; add comments
Jan Kanis <jan.code@jankanis.nl>
parents: 95
diff changeset
360
116
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
361 template = self.genelinks[db].template
115
0c2a03f9740b make external gene bank name configurable
Jan Kanis <jan.code@jankanis.nl>
parents: 110
diff changeset
362
0c2a03f9740b make external gene bank name configurable
Jan Kanis <jan.code@jankanis.nl>
parents: 110
diff changeset
363 if text is None:
0c2a03f9740b make external gene bank name configurable
Jan Kanis <jan.code@jankanis.nl>
parents: 110
diff changeset
364 if text_from == 'hitid':
0c2a03f9740b make external gene bank name configurable
Jan Kanis <jan.code@jankanis.nl>
parents: 110
diff changeset
365 text = hitid(hit)
0c2a03f9740b make external gene bank name configurable
Jan Kanis <jan.code@jankanis.nl>
parents: 110
diff changeset
366 elif text_from == 'dbname':
116
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
367 text = self.genelinks[db].dbname
115
0c2a03f9740b make external gene bank name configurable
Jan Kanis <jan.code@jankanis.nl>
parents: 110
diff changeset
368 else:
0c2a03f9740b make external gene bank name configurable
Jan Kanis <jan.code@jankanis.nl>
parents: 110
diff changeset
369 raise ValueError("Unknown value for text_from: '{0}'. Use 'hitid' or 'dbname'.".format(text_from))
0c2a03f9740b make external gene bank name configurable
Jan Kanis <jan.code@jankanis.nl>
parents: 110
diff changeset
370
95
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
371 if template is None:
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
372 return text if display_nolink else ''
115
0c2a03f9740b make external gene bank name configurable
Jan Kanis <jan.code@jankanis.nl>
parents: 110
diff changeset
373
95
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
374 args = dict(id=hitid(hit).split('|'),
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
375 fullid=hitid(hit),
104
a22c909c9b57 stream output
Jan Kanis <jan.code@jankanis.nl>
parents: 103
diff changeset
376 defline=str(hit.Hit_def).split(' ', 1)[0].split('|'),
a22c909c9b57 stream output
Jan Kanis <jan.code@jankanis.nl>
parents: 103
diff changeset
377 fulldefline=str(hit.Hit_def).split(' ', 1)[0],
95
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
378 accession=str(hit.Hit_accession))
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
379 try:
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
380 link = template.format(**args)
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
381 except Exception as e:
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
382 warnings.warn('Error in formatting gene bank link {} with {}: {}'.format(template, args, e))
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
383 return text if display_nolink else ''
96
02b795b784e1 fix bug; add comments
Jan Kanis <jan.code@jankanis.nl>
parents: 95
diff changeset
384
115
0c2a03f9740b make external gene bank name configurable
Jan Kanis <jan.code@jankanis.nl>
parents: 110
diff changeset
385 classattr = 'class="{0}" '.format(jinja2.escape(cssclass)) if cssclass is not None else ''
118
7f3f8c10f44b fix for Rikilt issues 8, 11, 12, 14
Jan Kanis <jan.code@jankanis.nl>
parents: 116
diff changeset
386 return jinja2.Markup("<a {0}target=\"_top\" href=\"{1}\">{2}</a>".format(classattr, jinja2.escape(link), jinja2.escape(text)))
95
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
387
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
388
115
0c2a03f9740b make external gene bank name configurable
Jan Kanis <jan.code@jankanis.nl>
parents: 110
diff changeset
389 genelinks_entry = namedtuple('genelinks_entry', 'dbname template')
116
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
390 def read_blastdb(dir, default):
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
391 links = defaultdict(lambda: default)
99
8f02008a5f20 look at all blast*.loc files; python2.6 compat fix
Jan Kanis <jan.code@jankanis.nl>
parents: 98
diff changeset
392 # blastdb.loc, blastdb_p.loc, blastdb_d.loc, etc.
101
5bfaa3ee1f27 better warning message
Jan Kanis <jan.code@jankanis.nl>
parents: 99
diff changeset
393 files = sorted(glob.glob(path.join(dir, 'blastdb*.loc')))
5bfaa3ee1f27 better warning message
Jan Kanis <jan.code@jankanis.nl>
parents: 99
diff changeset
394 # reversed, so blastdb.loc will take precedence
5bfaa3ee1f27 better warning message
Jan Kanis <jan.code@jankanis.nl>
parents: 99
diff changeset
395 for f in reversed(files):
95
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
396 try:
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
397 f = open(path.join(dir, f))
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
398 for l in f.readlines():
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
399 if l.strip().startswith('#'):
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
400 continue
98
e780606b7c25 test new command line parameters, fix small bug
Jan Kanis <jan.code@jankanis.nl>
parents: 96
diff changeset
401 line = l.rstrip('\n').split('\t')
95
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
402 try:
116
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
403 links[line[2]] = genelinks_entry(dbname=line[3] or default.dbname, template=line[4])
95
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
404 except IndexError:
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
405 continue
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
406 f.close()
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
407 except OSError:
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
408 continue
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
409 if not links:
103
86bcf17f50ef improve warning messages
Jan Kanis <jan.code@jankanis.nl>
parents: 102
diff changeset
410 if not files:
86bcf17f50ef improve warning messages
Jan Kanis <jan.code@jankanis.nl>
parents: 102
diff changeset
411 warnings.warn("No gene bank link templates found (no blastdb*.loc files found in {0})".format(dir))
86bcf17f50ef improve warning messages
Jan Kanis <jan.code@jankanis.nl>
parents: 102
diff changeset
412 else:
86bcf17f50ef improve warning messages
Jan Kanis <jan.code@jankanis.nl>
parents: 102
diff changeset
413 warnings.warn("No gene bank link templates found in {0}".format(', '.join(files)))
95
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
414 return links
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
415
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
416
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
417 def main():
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
418 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
419
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
420 parser = argparse.ArgumentParser(description="Convert a BLAST XML result into a nicely readable html page",
116
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
421 usage="{0} [-i] INPUT [-o OUTPUT] [--genelink-template URL_TEMPLATE] [--dbname DBNAME]".format(sys.argv[0]))
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
422 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
423 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
424 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
425 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
426 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
427 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
428 help='The output html file')
18
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
429 # 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
430 # 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
431 # 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
432 # 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
433 # 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
434 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
435 help='The template file to use. Defaults to blast_html.html.jinja')
115
0c2a03f9740b make external gene bank name configurable
Jan Kanis <jan.code@jankanis.nl>
parents: 110
diff changeset
436
116
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
437 parser.add_argument('--dbname', type=str, default='Gene Bank',
115
0c2a03f9740b make external gene bank name configurable
Jan Kanis <jan.code@jankanis.nl>
parents: 110
diff changeset
438 help="The link text to use for external links to a gene bank database. Defaults to 'Gene Bank'")
116
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
439 parser.add_argument('--genelink-template', metavar='URL_TEMPLATE',
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
440 default='http://www.ncbi.nlm.nih.gov/nucleotide/{accession}?report=genbank&log$=nuclalign',
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
441 help="""A link template to link hits to a gene bank webpage. The template string is a
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
442 Python format string. It can contain the following replacement elements: {id[N]}, {fullid},
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
443 {defline[N]}, {fulldefline}, {accession}, where N is a number. id[N] and defline[N] will be
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
444 replaced by the Nth element of the id or defline, where '|' is the field separator.
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
445
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
446 The default is 'http://www.ncbi.nlm.nih.gov/nucleotide/{accession}?report=genbank&log$=nuclalign',
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
447 which is a link to the NCBI nucleotide database.""")
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
448
116
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
449 parser.add_argument('--db-config-dir',
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
450 help="""The directory where databases are configured in blastdb*.loc files. These files
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
451 are consulted for creating a gene bank link. The files should conform to the format that
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
452 Galaxy's BLAST expect, i.e. tab-separated tables (with lines starting with '#' ignored),
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
453 with two extra fields. The third field of a line should be a database path and the fourth
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
454 a genebank link template conforming to the --genelink-template option syntax. Entries in
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
455 these config files override links specified using --genelink-template and --dbname.""")
95
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
456
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
457 args = parser.parse_args()
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
458 if args.input == None:
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
459 args.input = args.positional_arg
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
460 if args.input == None:
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
461 parser.error('no input specified')
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
462
104
a22c909c9b57 stream output
Jan Kanis <jan.code@jankanis.nl>
parents: 103
diff changeset
463 if six.PY2:
a22c909c9b57 stream output
Jan Kanis <jan.code@jankanis.nl>
parents: 103
diff changeset
464 # The argparse.FileType wrapper doesn't support an encoding
105
b3b5ee557170 update test
Jan Kanis <jan.code@jankanis.nl>
parents: 104
diff changeset
465 # argument, so for python 2 we need to wrap or reopen the
b3b5ee557170 update test
Jan Kanis <jan.code@jankanis.nl>
parents: 104
diff changeset
466 # output. The input files are already read as utf-8 by the
104
a22c909c9b57 stream output
Jan Kanis <jan.code@jankanis.nl>
parents: 103
diff changeset
467 # respective libraries.
107
ee2b105d772a minor fix
Jan Kanis <jan.code@jankanis.nl>
parents: 105
diff changeset
468 #
104
a22c909c9b57 stream output
Jan Kanis <jan.code@jankanis.nl>
parents: 103
diff changeset
469 # One option is using codecs, but the codecs' writelines()
a22c909c9b57 stream output
Jan Kanis <jan.code@jankanis.nl>
parents: 103
diff changeset
470 # method doesn't support streaming but collects all output and
105
b3b5ee557170 update test
Jan Kanis <jan.code@jankanis.nl>
parents: 104
diff changeset
471 # writes at once (see Python issues #5445 and #21910). On the
b3b5ee557170 update test
Jan Kanis <jan.code@jankanis.nl>
parents: 104
diff changeset
472 # other hand the io module is slower (though not
b3b5ee557170 update test
Jan Kanis <jan.code@jankanis.nl>
parents: 104
diff changeset
473 # significantly).
104
a22c909c9b57 stream output
Jan Kanis <jan.code@jankanis.nl>
parents: 103
diff changeset
474
a22c909c9b57 stream output
Jan Kanis <jan.code@jankanis.nl>
parents: 103
diff changeset
475 # args.output = codecs.getwriter('utf-8')(args.output)
107
ee2b105d772a minor fix
Jan Kanis <jan.code@jankanis.nl>
parents: 105
diff changeset
476 # def fixed_writelines(iter, self=args.output):
ee2b105d772a minor fix
Jan Kanis <jan.code@jankanis.nl>
parents: 105
diff changeset
477 # for i in iter:
ee2b105d772a minor fix
Jan Kanis <jan.code@jankanis.nl>
parents: 105
diff changeset
478 # self.write(i)
ee2b105d772a minor fix
Jan Kanis <jan.code@jankanis.nl>
parents: 105
diff changeset
479 # args.output.writelines = fixed_writelines
ee2b105d772a minor fix
Jan Kanis <jan.code@jankanis.nl>
parents: 105
diff changeset
480
ee2b105d772a minor fix
Jan Kanis <jan.code@jankanis.nl>
parents: 105
diff changeset
481 args.output.close()
109
ea3bc0b9a3d9 fix (again) output encoding
Jan Kanis <jan.code@jankanis.nl>
parents: 107
diff changeset
482 args.output = io.open(args.output.name, 'w', encoding='utf-8')
115
0c2a03f9740b make external gene bank name configurable
Jan Kanis <jan.code@jankanis.nl>
parents: 110
diff changeset
483
18
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
484 templatedir, templatename = path.split(args.template.name)
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
485 args.template.close()
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
486 if not templatedir:
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
487 templatedir = '.'
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
488
116
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
489 defaultentry = genelinks_entry(args.dbname, args.genelink_template)
95
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
490 if args.db_config_dir is None:
116
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
491 genelinks = defaultdict(lambda: defaultentry)
95
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
492 elif not path.isdir(args.db_config_dir):
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
493 parser.error('db-config-dir does not exist or is not a directory')
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
494 else:
116
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
495 genelinks = read_blastdb(args.db_config_dir, default=defaultentry)
95
4378d11f0ed7 implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents: 79
diff changeset
496
116
f5066973029a refactor
Jan Kanis <jan.code@jankanis.nl>
parents: 115
diff changeset
497 b = BlastVisualize(args.input, templatedir, templatename, genelinks=genelinks)
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
498 b.render(args.output)
104
a22c909c9b57 stream output
Jan Kanis <jan.code@jankanis.nl>
parents: 103
diff changeset
499 args.output.close()
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
500
11
7660519f2dc9 proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents: 10
diff changeset
501
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
502 if __name__ == '__main__':
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
503 main()
11
7660519f2dc9 proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents: 10
diff changeset
504