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