Mercurial > repos > jankanis > blast2html
annotate blast2html.py @ 91:20639b08d781
fix LD_LIBRARY_PATH
author | Jan Kanis <jan.code@jankanis.nl> |
---|---|
date | Tue, 24 Jun 2014 17:59:12 +0200 |
parents | 9fb1a7d67317 |
children | 4378d11f0ed7 |
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 | 2 # -*- coding: utf-8 -*- |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
3 |
75 | 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 | 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 | 10 |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
11 import sys |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
12 import math |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
13 import warnings |
73
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
14 import six, codecs |
75 | 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 |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
18 import argparse |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
19 from lxml import objectify |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
20 import jinja2 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
21 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
22 |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
23 |
76
7d0d46168fd5
Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents:
75
diff
changeset
|
24 _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
|
25 def filter(func_or_name): |
11
7660519f2dc9
proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents:
10
diff
changeset
|
26 "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
|
27 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
|
28 def inner(func): |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
29 _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
|
30 return func |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
31 return inner |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
32 else: |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
33 _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
|
34 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
|
35 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
36 |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
37 def color_idx(length): |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
38 if length < 40: |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
39 return 0 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
40 elif length < 50: |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
41 return 1 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
42 elif length < 80: |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
43 return 2 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
44 elif length < 200: |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
45 return 3 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
46 return 4 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
47 |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
48 @filter |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
49 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
|
50 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
|
51 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
52 @filter |
76
7d0d46168fd5
Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents:
75
diff
changeset
|
53 def numfmt(val): |
7d0d46168fd5
Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents:
75
diff
changeset
|
54 """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
|
55 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
|
56 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
|
57 between python 2 and 3.""" |
7d0d46168fd5
Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents:
75
diff
changeset
|
58 fpart, ipart = math.modf(val) |
7d0d46168fd5
Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents:
75
diff
changeset
|
59 if fpart == 0: |
7d0d46168fd5
Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents:
75
diff
changeset
|
60 return str(int(val)) |
7d0d46168fd5
Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents:
75
diff
changeset
|
61 # 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
|
62 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
|
63 if s[-1] == '.': |
7d0d46168fd5
Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents:
75
diff
changeset
|
64 s += '0' |
7d0d46168fd5
Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents:
75
diff
changeset
|
65 return s |
7d0d46168fd5
Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents:
75
diff
changeset
|
66 |
7d0d46168fd5
Format all numbers in a predictable way
Jan Kanis <jan.code@jankanis.nl>
parents:
75
diff
changeset
|
67 @filter |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
68 def firsttitle(hit): |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
69 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
|
70 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
71 @filter |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
72 def othertitles(hit): |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
73 """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
|
74 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
|
75 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
76 titles = [] |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
77 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
|
78 fullid, title = t.split(' ', 1) |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
79 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
|
80 titles.append(dict(id = id, |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
81 hitid = hitid, |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
82 fullid = fullid, |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
83 title = title)) |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
84 return titles |
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 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
|
88 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
|
89 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
|
90 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
|
91 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
|
92 return hitid |
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 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
95 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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 return hitid |
c8347745bbad
use Iteration_message tag; also work with unexpected Hit_id values
Jan Kanis <jan.code@jankanis.nl>
parents:
23
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 alignment_pre(hsp): |
68
fa8a93bdefd7
fix bug in calculations of alignment end
Jan Kanis <jan.code@jankanis.nl>
parents:
67
diff
changeset
|
105 """Create the preformatted alignment blocks""" |
71 | 106 |
107 # line break length | |
108 linewidth = 60 | |
67
19c48f2ec775
wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents:
53
diff
changeset
|
109 |
19c48f2ec775
wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents:
53
diff
changeset
|
110 qfrom = int(hsp['Hsp_query-from']) |
19c48f2ec775
wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents:
53
diff
changeset
|
111 qto = int(hsp['Hsp_query-to']) |
69 | 112 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
|
113 hfrom = int(hsp['Hsp_hit-from']) |
19c48f2ec775
wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents:
53
diff
changeset
|
114 hto = int(hsp['Hsp_hit-to']) |
69 | 115 hframe = int(hsp['Hsp_hit-frame']) |
71 | 116 |
67
19c48f2ec775
wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents:
53
diff
changeset
|
117 qseq = hsp.Hsp_qseq.text |
19c48f2ec775
wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents:
53
diff
changeset
|
118 midline = hsp.Hsp_midline.text |
19c48f2ec775
wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents:
53
diff
changeset
|
119 hseq = hsp.Hsp_hseq.text |
69 | 120 |
71 | 121 if not qframe in (1, -1): |
73
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
122 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 | 123 qframe = -1 if qframe < 0 else 1 |
71 | 124 if not hframe in (1, -1): |
73
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
125 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 | 126 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
|
127 |
69 | 128 def split(txt): |
71 | 129 return [txt[i:i+linewidth] for i in range(0, len(txt), linewidth)] |
69 | 130 |
71 | 131 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
|
132 yield ( |
73
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
133 "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
|
134 " {0:7} {1}\n".format('', mid) + |
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
135 "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
|
136 ) |
19c48f2ec775
wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents:
53
diff
changeset
|
137 |
69 | 138 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
|
139 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
|
140 nodeid(hsp), qfrom, qto, len(qseq))) |
69 | 141 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
|
142 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
|
143 nodeid(hsp), hfrom, hto, len(hseq))) |
19c48f2ec775
wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents:
53
diff
changeset
|
144 |
19c48f2ec775
wrap alignments if they are too long
Jan Kanis <jan.code@jankanis.nl>
parents:
53
diff
changeset
|
145 |
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('len') |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
148 def blastxml_len(node): |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
149 if node.tag == 'Hsp': |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
150 return int(node['Hsp_align-len']) |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
151 elif node.tag == 'Iteration': |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
152 return int(node['Iteration_query-len']) |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
153 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
|
154 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
155 @filter |
53
4217bb9cf1d3
depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents:
51
diff
changeset
|
156 def nodeid(node): |
4217bb9cf1d3
depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents:
51
diff
changeset
|
157 id = [] |
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 == 'Hsp': |
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.Hsp_num.text) |
4217bb9cf1d3
depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents:
51
diff
changeset
|
160 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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 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
|
166 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
|
167 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
|
168 return '-'.join(id) |
4217bb9cf1d3
depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents:
51
diff
changeset
|
169 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
|
170 |
4217bb9cf1d3
depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents:
51
diff
changeset
|
171 |
4217bb9cf1d3
depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents:
51
diff
changeset
|
172 @filter |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
173 def asframe(frame): |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
174 if frame == 1: |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
175 return 'Plus' |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
176 elif frame == -1: |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
177 return 'Minus' |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
178 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
|
179 |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
180 def genelink(hit, type='genbank', hsp=None): |
73
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
181 if not isinstance(hit, six.string_types): |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
182 hit = hitid(hit) |
73
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
183 link = "http://www.ncbi.nlm.nih.gov/nucleotide/{0}?report={1}&log$=nuclalign".format(hit, type) |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
184 if hsp != None: |
73
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
185 link += "&from={0}&to={1}".format(hsp['Hsp_hit-from'], hsp['Hsp_hit-to']) |
16 | 186 return link |
187 | |
188 | |
189 # javascript escape filter based on Django's, from https://github.com/dsissitka/khan-website/blob/master/templatefilters.py#L112-139 | |
190 # 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
|
191 |
74
03e044b5bcc2
fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents:
73
diff
changeset
|
192 # The r'\u0027' syntax doesn't work the way we need to in python 2.6 with unicode_literals |
16 | 193 _base_js_escapes = ( |
74
03e044b5bcc2
fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents:
73
diff
changeset
|
194 ('\\', '\\u005C'), |
03e044b5bcc2
fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents:
73
diff
changeset
|
195 ('\'', '\\u0027'), |
03e044b5bcc2
fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents:
73
diff
changeset
|
196 ('"', '\\u0022'), |
03e044b5bcc2
fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents:
73
diff
changeset
|
197 # ('>', '\\u003E'), |
03e044b5bcc2
fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents:
73
diff
changeset
|
198 # ('<', '\\u003C'), |
03e044b5bcc2
fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents:
73
diff
changeset
|
199 # ('&', '\\u0026'), |
03e044b5bcc2
fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents:
73
diff
changeset
|
200 # ('=', '\\u003D'), |
03e044b5bcc2
fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents:
73
diff
changeset
|
201 # ('-', '\\u002D'), |
03e044b5bcc2
fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents:
73
diff
changeset
|
202 # (';', '\\u003B'), |
03e044b5bcc2
fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents:
73
diff
changeset
|
203 (u'\u2028', '\\u2028'), |
03e044b5bcc2
fix escaping of javascript literals
Jan Kanis <jan.code@jankanis.nl>
parents:
73
diff
changeset
|
204 (u'\u2029', '\\u2029') |
16 | 205 ) |
206 | |
207 # 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
|
208 # needed a.o. to prevent html parsers from jumping out of javascript |
16 | 209 # parsing mode. |
210 _js_escapes = (_base_js_escapes + | |
211 tuple(('%c' % z, '\\u%04X' % z) for z in range(32))) | |
212 | |
213 @filter | |
214 def js_string_escape(value): | |
72 | 215 """ |
216 Javascript string literal escape. Note that this only escapes data | |
217 for embedding within javascript string literals, not in general | |
218 javascript snippets. | |
219 """ | |
16 | 220 |
73
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
221 value = six.text_type(value) |
16 | 222 |
223 for bad, good in _js_escapes: | |
224 value = value.replace(bad, good) | |
225 | |
226 return value | |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
227 |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
228 @filter |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
229 def hits(result): |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
230 # sort hits by longest hotspot first |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
231 return sorted(result.Iteration_hits.findall('Hit'), |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
232 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
|
233 reverse=True) |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
234 |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
235 |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
236 |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
237 class BlastVisualize: |
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 colors = ('black', 'blue', 'green', 'magenta', 'red') |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
240 |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
241 max_scale_labels = 10 |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
242 |
18
4434ffab721a
add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents:
16
diff
changeset
|
243 def __init__(self, input, templatedir, templatename): |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
244 self.input = input |
18
4434ffab721a
add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents:
16
diff
changeset
|
245 self.templatename = templatename |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
246 |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
247 self.blast = objectify.parse(self.input).getroot() |
79
9fb1a7d67317
remove unneeded encoding parameter
Jan Kanis <jan.code@jankanis.nl>
parents:
78
diff
changeset
|
248 self.loader = jinja2.FileSystemLoader(searchpath=templatedir) |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
249 self.environment = jinja2.Environment(loader=self.loader, |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
250 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
|
251 |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
252 self._addfilters(self.environment) |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
253 |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
254 |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
255 def _addfilters(self, environment): |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
256 for filtername, funcname in _filters.items(): |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
257 try: |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
258 environment.filters[filtername] = getattr(self, funcname) |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
259 except AttributeError: |
75 | 260 try: |
261 environment.filters[filtername] = globals()[funcname] | |
262 except KeyError: | |
263 environment.filters[filtername] = getattr(builtins, funcname) | |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
264 |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
265 def render(self, output): |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
266 template = self.environment.get_template(self.templatename) |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
267 |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
268 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
|
269 ('Query definition', self.blast["BlastOutput_query-def"]), |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
270 ('Query length', self.blast["BlastOutput_query-len"]), |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
271 ('Program', self.blast.BlastOutput_version), |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
272 ('Database', self.blast.BlastOutput_db), |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
273 ) |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
274 |
73
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
275 result = template.render(blast=self.blast, |
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
276 iterations=self.blast.BlastOutput_iterations.Iteration, |
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
277 colors=self.colors, |
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
278 genelink=genelink, |
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
279 params=params) |
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
280 if six.PY2: |
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
281 result = result.encode('utf-8') |
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
282 output.write(result) |
12
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 @filter |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
285 def match_colors(self, result): |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
286 """ |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
287 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
|
288 """ |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
289 |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
290 query_length = blastxml_len(result) |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
291 |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
292 percent_multiplier = 100 / query_length |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
293 |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
294 for hit in hits(result): |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
295 # 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
|
296 # 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
|
297 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
|
298 table = bytearray([255]) * query_length |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
299 for hsp in hotspots: |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
300 frm = hsp['Hsp_query-from'] - 1 |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
301 to = int(hsp['Hsp_query-to']) |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
302 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
|
303 |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
304 matches = [] |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
305 last = table[0] |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
306 count = 0 |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
307 for i in range(query_length): |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
308 if table[i] == last: |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
309 count += 1 |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
310 continue |
16 | 311 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
|
312 last = table[i] |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
313 count = 1 |
16 | 314 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
|
315 |
53
4217bb9cf1d3
depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents:
51
diff
changeset
|
316 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
|
317 |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
318 @filter |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
319 def queryscale(self, result): |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
320 query_length = blastxml_len(result) |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
321 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
|
322 percent_multiplier = 100 / query_length |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
323 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
|
324 if i % skip == 0: |
23 | 325 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
|
326 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
|
327 yield dict(label = query_length, |
23 | 328 width = (query_length % skip) * percent_multiplier) |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
329 |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
330 @filter |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
331 def hit_info(self, result): |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
332 |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
333 query_length = blastxml_len(result) |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
334 |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
335 for hit in hits(result): |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
336 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
|
337 |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
338 cover = [False] * query_length |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
339 for hsp in hsps: |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
340 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
|
341 cover_count = cover.count(True) |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
342 |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
343 def hsp_val(path): |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
344 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
|
345 |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
346 yield dict(hit = hit, |
75 | 347 title = firsttitle(hit), |
73
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
348 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
|
349 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
|
350 cover = "{0:.0%}".format(cover_count / query_length), |
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
351 e_value = "{0:.4g}".format(min(hsp_val('Hsp_evalue'))), |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
352 # FIXME: is this the correct formula vv? |
78 | 353 # float(...) because non-flooring division doesn't work with lxml elements in python 2.6 |
75 | 354 ident = "{0:.0%}".format(float(min(float(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
|
355 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
|
356 |
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
|
357 |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
358 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
|
359 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
|
360 |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
361 parser = argparse.ArgumentParser(description="Convert a BLAST XML result into a nicely readable html page", |
73
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
362 usage="{0} [-i] INPUT [-o OUTPUT]".format(sys.argv[0])) |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
363 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
|
364 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
|
365 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
|
366 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
|
367 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
|
368 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
|
369 help='The output html file') |
18
4434ffab721a
add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents:
16
diff
changeset
|
370 # 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
|
371 # 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
|
372 # 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
|
373 # 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
|
374 # 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
|
375 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
|
376 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
|
377 |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
378 args = parser.parse_args() |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
379 if args.input == None: |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
380 args.input = args.positional_arg |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
381 if args.input == None: |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
382 parser.error('no input specified') |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
383 |
18
4434ffab721a
add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents:
16
diff
changeset
|
384 templatedir, templatename = path.split(args.template.name) |
4434ffab721a
add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents:
16
diff
changeset
|
385 args.template.close() |
4434ffab721a
add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents:
16
diff
changeset
|
386 if not templatedir: |
4434ffab721a
add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents:
16
diff
changeset
|
387 templatedir = '.' |
4434ffab721a
add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents:
16
diff
changeset
|
388 |
4434ffab721a
add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents:
16
diff
changeset
|
389 b = BlastVisualize(args.input, templatedir, templatename) |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
390 b.render(args.output) |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
391 |
11
7660519f2dc9
proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents:
10
diff
changeset
|
392 |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
393 if __name__ == '__main__': |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
394 main() |
11
7660519f2dc9
proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents:
10
diff
changeset
|
395 |