annotate blast2html.py @ 50:bfc82a8aa3c9

unicodify python sources
author Jan Kanis <jan.code@jankanis.nl>
date Wed, 21 May 2014 18:17:03 +0200
parents 4e6ac737ba17
children b15a20c2372a
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 -*-
bfc82a8aa3c9 unicodify python sources
Jan Kanis <jan.code@jankanis.nl>
parents: 27
diff changeset
3 #
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
4 # Actually runs under either python 2 or 3
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
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
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
7 # License: GPL version 3 or higher
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
8
50
bfc82a8aa3c9 unicodify python sources
Jan Kanis <jan.code@jankanis.nl>
parents: 27
diff changeset
9 from __future__ import unicode_literals
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
18
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
14 from os import path
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
15 from itertools import repeat
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
16 import argparse
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
17 from lxml import objectify
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
18 import jinja2
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
19
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
20
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
21
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
22 _filters = {}
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
23 def filter(func_or_name):
11
7660519f2dc9 proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents: 10
diff changeset
24 "Decorator to register a function as filter in the current jinja environment"
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
25 if isinstance(func_or_name, str):
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
26 def inner(func):
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
27 _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
28 return func
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
29 return inner
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
30 else:
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
31 _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
32 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
33
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
34
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
35 def color_idx(length):
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
36 if length < 40:
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
37 return 0
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
38 elif length < 50:
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
39 return 1
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
40 elif length < 80:
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
41 return 2
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
42 elif length < 200:
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
43 return 3
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
44 return 4
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
45
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
46 @filter
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
47 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
48 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
49
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
50 @filter
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
51 def firsttitle(hit):
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
52 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
53
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
54 @filter
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
55 def othertitles(hit):
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
56 """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
57 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
58
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
59 titles = []
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
60 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
61 fullid, title = t.split(' ', 1)
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
62 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
63 titles.append(dict(id = id,
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
64 hitid = hitid,
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
65 fullid = fullid,
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
66 title = title))
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
67 return titles
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
68
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
69 @filter
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
70 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
71 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
72 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
73 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
74 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
75 return hitid
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
76
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
77 @filter
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
78 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
79 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
80 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
81 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
82 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
83 return hitid
c8347745bbad use Iteration_message tag; also work with unexpected Hit_id values
Jan Kanis <jan.code@jankanis.nl>
parents: 23
diff changeset
84
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
85
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
86 @filter
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
87 def alignment_pre(hsp):
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
88 return (
15
648b3b7437da minor update
Jan Kanis <jan.code@jankanis.nl>
parents: 14
diff changeset
89 "Query {:>7s} {} {}\n".format(hsp['Hsp_query-from'].text, hsp.Hsp_qseq, hsp['Hsp_query-to']) +
11
7660519f2dc9 proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents: 10
diff changeset
90 " {:7s} {}\n".format('', hsp.Hsp_midline) +
15
648b3b7437da minor update
Jan Kanis <jan.code@jankanis.nl>
parents: 14
diff changeset
91 "Subject{:>7s} {} {}".format(hsp['Hsp_hit-from'].text, hsp.Hsp_hseq, hsp['Hsp_hit-to'])
11
7660519f2dc9 proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents: 10
diff changeset
92 )
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
93
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
94 @filter('len')
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
95 def blastxml_len(node):
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
96 if node.tag == 'Hsp':
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
97 return int(node['Hsp_align-len'])
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
98 elif node.tag == 'Iteration':
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
99 return int(node['Iteration_query-len'])
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
100 raise Exception("Unknown XML node type: "+node.tag)
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
101
10
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
102
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
103 @filter
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
104 def asframe(frame):
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
105 if frame == 1:
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
106 return 'Plus'
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
107 elif frame == -1:
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
108 return 'Minus'
2fbdf2eb27b4 All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents: 7
diff changeset
109 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
110
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
111 def genelink(hit, type='genbank', hsp=None):
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
112 if not isinstance(hit, str):
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
113 hit = hitid(hit)
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
114 link = "http://www.ncbi.nlm.nih.gov/nucleotide/{}?report={}&log$=nuclalign".format(hit, type)
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
115 if hsp != None:
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
116 link += "&from={}&to={}".format(hsp['Hsp_hit-from'], hsp['Hsp_hit-to'])
16
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
117 return link
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
118
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
119
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
120 # 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
121 # 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
122
16
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
123 _base_js_escapes = (
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
124 ('\\', r'\u005C'),
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
125 ('\'', r'\u0027'),
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
126 ('"', r'\u0022'),
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
127 # ('>', r'\u003E'),
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
128 # ('<', r'\u003C'),
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
129 # ('&', r'\u0026'),
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
130 # ('=', r'\u003D'),
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
131 # ('-', r'\u002D'),
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
132 # (';', r'\u003B'),
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
133 # (u'\u2028', r'\u2028'),
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
134 # (u'\u2029', r'\u2029')
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
135 )
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
136
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
137 # 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
138 # 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
139 # parsing mode.
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
140 _js_escapes = (_base_js_escapes +
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
141 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
142
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
143 @filter
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
144 def js_string_escape(value):
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
145 """Escape javascript string literal escapes. Note that this only works
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
146 within javascript string literals, not in general javascript
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
147 snippets."""
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
148
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
149 value = str(value)
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
150
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
151 for bad, good in _js_escapes:
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
152 value = value.replace(bad, good)
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
153
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
154 return value
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
155
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
156 @filter
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
157 def hits(result):
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
158 # sort hits by longest hotspot first
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
159 return sorted(result.Iteration_hits.findall('Hit'),
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
160 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
161 reverse=True)
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
162
7
9e7927673089 intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents: 5
diff changeset
163
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
164
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
165 class BlastVisualize:
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
166
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
167 colors = ('black', 'blue', 'green', 'magenta', 'red')
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
168
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
169 max_scale_labels = 10
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
170
18
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
171 def __init__(self, input, templatedir, templatename):
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
172 self.input = input
18
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
173 self.templatename = templatename
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
174
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
175 self.blast = objectify.parse(self.input).getroot()
50
bfc82a8aa3c9 unicodify python sources
Jan Kanis <jan.code@jankanis.nl>
parents: 27
diff changeset
176 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
177 self.environment = jinja2.Environment(loader=self.loader,
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
178 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
179
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
180 self._addfilters(self.environment)
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
181
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
182
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
183 def _addfilters(self, environment):
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
184 for filtername, funcname in _filters.items():
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
185 try:
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
186 environment.filters[filtername] = getattr(self, funcname)
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
187 except AttributeError:
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
188 environment.filters[filtername] = globals()[funcname]
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
189
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
190 def render(self, output):
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
191 template = self.environment.get_template(self.templatename)
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
192
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
193 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
194 ('Query definition', self.blast["BlastOutput_query-def"]),
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
195 ('Query length', self.blast["BlastOutput_query-len"]),
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
196 ('Program', self.blast.BlastOutput_version),
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
197 ('Database', self.blast.BlastOutput_db),
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
198 )
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
199
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
200 output.write(template.render(blast=self.blast,
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
201 iterations=self.blast.BlastOutput_iterations.Iteration,
18
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
202 colors=self.colors,
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
203 # match_colors=self.match_colors(),
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
204 # hit_info=self.hit_info(),
18
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
205 genelink=genelink,
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
206 params=params))
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
207
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
208 @filter
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
209 def match_colors(self, result):
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
210 """
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
211 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
212 """
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
213
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
214 query_length = blastxml_len(result)
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
215
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
216 percent_multiplier = 100 / query_length
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
217
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
218 for hit in hits(result):
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
219 # 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
220 # 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
221 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
222 table = bytearray([255]) * query_length
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
223 for hsp in hotspots:
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
224 frm = hsp['Hsp_query-from'] - 1
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
225 to = int(hsp['Hsp_query-to'])
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
226 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
227
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
228 matches = []
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
229 last = table[0]
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
230 count = 0
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
231 for i in range(query_length):
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
232 if table[i] == last:
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
233 count += 1
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
234 continue
16
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
235 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
236 last = table[i]
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
237 count = 1
16
db7e4ee3be03 fix validation, reindent
Jan Kanis <jan.code@jankanis.nl>
parents: 15
diff changeset
238 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
239
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
240 yield dict(colors=matches, link="#hit"+hit.Hit_num.text, defline=firsttitle(hit))
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
241
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
242 @filter
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
243 def queryscale(self, result):
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
244 query_length = blastxml_len(result)
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
245 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
246 percent_multiplier = 100 / query_length
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
247 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
248 if i % skip == 0:
23
6995a6f34f3f fix sectioning, add footer
Jan Kanis <jan.code@jankanis.nl>
parents: 22
diff changeset
249 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
250 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
251 yield dict(label = query_length,
23
6995a6f34f3f fix sectioning, add footer
Jan Kanis <jan.code@jankanis.nl>
parents: 22
diff changeset
252 width = (query_length % skip) * percent_multiplier)
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 @filter
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
255 def hit_info(self, result):
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
256
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
257 query_length = blastxml_len(result)
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
258
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
259 for hit in hits(result):
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
260 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
261
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
262 cover = [False] * query_length
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
263 for hsp in hsps:
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
264 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
265 cover_count = cover.count(True)
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
266
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
267 def hsp_val(path):
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
268 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
269
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
270 yield dict(hit = hit,
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
271 title = firsttitle(hit),
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
272 link_id = hit.Hit_num,
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
273 maxscore = "{:.1f}".format(max(hsp_val('Hsp_bit-score'))),
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
274 totalscore = "{:.1f}".format(sum(hsp_val('Hsp_bit-score'))),
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
275 cover = "{:.0%}".format(cover_count / query_length),
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
276 e_value = "{:.4g}".format(min(hsp_val('Hsp_evalue'))),
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
277 # FIXME: is this the correct formula vv?
19
67ddcb807b7d make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents: 18
diff changeset
278 ident = "{:.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
279 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
280
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
281
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
282 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
283 default_template = path.join(path.dirname(__file__), 'blast2html.html.jinja')
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
284
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
285 parser = argparse.ArgumentParser(description="Convert a BLAST XML result into a nicely readable html page",
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
286 usage="{} [-i] INPUT [-o OUTPUT]".format(sys.argv[0]))
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
287 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
288 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
289 help='The input Blast XML file, same as -i/--input')
50
bfc82a8aa3c9 unicodify python sources
Jan Kanis <jan.code@jankanis.nl>
parents: 27
diff changeset
290 input_group.add_argument('-i', '--input', type=argparse.FileType(mode='r', encoding='utf-8'),
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
291 help='The input Blast XML file')
50
bfc82a8aa3c9 unicodify python sources
Jan Kanis <jan.code@jankanis.nl>
parents: 27
diff changeset
292 parser.add_argument('-o', '--output', type=argparse.FileType(mode='w', encoding='utf-8'), default=sys.stdout,
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
293 help='The output html file')
18
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
294 # 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
295 # 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
296 # 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
297 # 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
298 # care too much.
50
bfc82a8aa3c9 unicodify python sources
Jan Kanis <jan.code@jankanis.nl>
parents: 27
diff changeset
299 parser.add_argument('--template', type=argparse.FileType(mode='r', encoding='utf-8'), default=default_template,
18
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
300 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
301
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
302 args = parser.parse_args()
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
303 if args.input == None:
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
304 args.input = args.positional_arg
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
305 if args.input == None:
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
306 parser.error('no input specified')
5
1df2bfce5c24 first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff changeset
307
18
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
308 templatedir, templatename = path.split(args.template.name)
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
309 args.template.close()
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
310 if not templatedir:
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
311 templatedir = '.'
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
312
4434ffab721a add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents: 16
diff changeset
313 b = BlastVisualize(args.input, templatedir, templatename)
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
314 b.render(args.output)
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
315
11
7660519f2dc9 proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents: 10
diff changeset
316
12
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
317 if __name__ == '__main__':
a459c754cdb5 add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents: 11
diff changeset
318 main()
11
7660519f2dc9 proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents: 10
diff changeset
319