Mercurial > repos > jankanis > blast2html
annotate blast2html.py @ 104:a22c909c9b57
stream output
author | Jan Kanis <jan.code@jankanis.nl> |
---|---|
date | Mon, 07 Jul 2014 11:29:10 +0200 |
parents | 86bcf17f50ef |
children | b3b5ee557170 |
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 |
95
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
18 from collections import defaultdict |
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): |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
222 # sort hits by longest hotspot first |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
223 return sorted(result.Iteration_hits.findall('Hit'), |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
224 key=lambda h: max(blastxml_len(hsp) for hsp in h.Hit_hsps.Hsp), |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
225 reverse=True) |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
226 |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
227 |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
228 |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
229 class BlastVisualize: |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
230 |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
231 colors = ('black', 'blue', 'green', 'magenta', 'red') |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
232 |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
233 max_scale_labels = 10 |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
234 |
95
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
235 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
|
236 self.input = input |
18
4434ffab721a
add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents:
16
diff
changeset
|
237 self.templatename = templatename |
95
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
238 self.genelinks = genelinks |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
239 |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
240 self.blast = objectify.parse(self.input).getroot() |
79
9fb1a7d67317
remove unneeded encoding parameter
Jan Kanis <jan.code@jankanis.nl>
parents:
78
diff
changeset
|
241 self.loader = jinja2.FileSystemLoader(searchpath=templatedir) |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
242 self.environment = jinja2.Environment(loader=self.loader, |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
243 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
|
244 |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
245 self._addfilters(self.environment) |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
246 |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
247 |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
248 def _addfilters(self, environment): |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
249 for filtername, funcname in _filters.items(): |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
250 try: |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
251 environment.filters[filtername] = getattr(self, funcname) |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
252 except AttributeError: |
75 | 253 try: |
254 environment.filters[filtername] = globals()[funcname] | |
255 except KeyError: | |
256 environment.filters[filtername] = getattr(builtins, funcname) | |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
257 |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
258 def render(self, output): |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
259 template = self.environment.get_template(self.templatename) |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
260 |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
261 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
|
262 ('Query definition', self.blast["BlastOutput_query-def"]), |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
263 ('Query length', self.blast["BlastOutput_query-len"]), |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
264 ('Program', self.blast.BlastOutput_version), |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
265 ('Database', self.blast.BlastOutput_db), |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
266 ) |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
267 |
104 | 268 result = template.stream(blast=self.blast, |
73
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
269 iterations=self.blast.BlastOutput_iterations.Iteration, |
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
270 colors=self.colors, |
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
271 params=params) |
104 | 272 |
273 result.dump(output) | |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
274 |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
275 @filter |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
276 def match_colors(self, result): |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
277 """ |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
278 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
|
279 """ |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
280 |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
281 query_length = blastxml_len(result) |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
282 |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
283 percent_multiplier = 100 / query_length |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
284 |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
285 for hit in hits(result): |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
286 # 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
|
287 # 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
|
288 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
|
289 table = bytearray([255]) * query_length |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
290 for hsp in hotspots: |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
291 frm = hsp['Hsp_query-from'] - 1 |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
292 to = int(hsp['Hsp_query-to']) |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
293 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
|
294 |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
295 matches = [] |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
296 last = table[0] |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
297 count = 0 |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
298 for i in range(query_length): |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
299 if table[i] == last: |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
300 count += 1 |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
301 continue |
16 | 302 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
|
303 last = table[i] |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
304 count = 1 |
16 | 305 matches.append((count * percent_multiplier, self.colors[last] if last != 255 else 'transparent')) |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
306 |
53
4217bb9cf1d3
depend on python 3; fix internal links with multiple iterations
Jan Kanis <jan.code@jankanis.nl>
parents:
51
diff
changeset
|
307 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
|
308 |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
309 @filter |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
310 def queryscale(self, result): |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
311 query_length = blastxml_len(result) |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
312 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
|
313 percent_multiplier = 100 / query_length |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
314 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
|
315 if i % skip == 0: |
23 | 316 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
|
317 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
|
318 yield dict(label = query_length, |
23 | 319 width = (query_length % skip) * percent_multiplier) |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
320 |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
321 @filter |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
322 def hit_info(self, result): |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
323 |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
324 query_length = blastxml_len(result) |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
325 |
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
326 for hit in hits(result): |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
327 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
|
328 |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
329 cover = [False] * query_length |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
330 for hsp in hsps: |
19
67ddcb807b7d
make it work with multiple queries
Jan Kanis <jan.code@jankanis.nl>
parents:
18
diff
changeset
|
331 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
|
332 cover_count = cover.count(True) |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
333 |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
334 def hsp_val(path): |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
335 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
|
336 |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
337 yield dict(hit = hit, |
75 | 338 title = firsttitle(hit), |
73
67b1a319c6dc
First go at 2.6 compatibility
Jan Kanis <jan.code@jankanis.nl>
parents:
72
diff
changeset
|
339 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
|
340 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
|
341 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
|
342 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
|
343 # FIXME: is this the correct formula vv? |
78 | 344 # 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
|
345 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
|
346 |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
347 @filter |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
348 def genelink(self, hit, text=None, clas=None, display_nolink=True): |
96 | 349 """Create a html link from a hit node to a configured gene bank webpage. |
350 text: The text of the link, defaults to the hit_id | |
351 clas: extra css classes that will be added to the <a> element | |
352 display_nolink: boolean, if false don't display anything if no link can be created. Default True. | |
353 """ | |
354 | |
95
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
355 if text is None: |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
356 text = hitid(hit) |
96 | 357 |
95
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
358 db = hit.getroottree().getroot().BlastOutput_db |
96 | 359 |
95
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
360 if isinstance(self.genelinks, six.string_types): |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
361 template = self.genelinks |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
362 else: |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
363 template = self.genelinks.get(db) |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
364 if template is None: |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
365 return text if display_nolink else '' |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
366 args = dict(id=hitid(hit).split('|'), |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
367 fullid=hitid(hit), |
104 | 368 defline=str(hit.Hit_def).split(' ', 1)[0].split('|'), |
369 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
|
370 accession=str(hit.Hit_accession)) |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
371 try: |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
372 link = template.format(**args) |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
373 except Exception as e: |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
374 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
|
375 return text if display_nolink else '' |
96 | 376 |
99
8f02008a5f20
look at all blast*.loc files; python2.6 compat fix
Jan Kanis <jan.code@jankanis.nl>
parents:
98
diff
changeset
|
377 classattr = 'class="{0}" '.format(jinja2.escape(clas)) if clas is not None else '' |
8f02008a5f20
look at all blast*.loc files; python2.6 compat fix
Jan Kanis <jan.code@jankanis.nl>
parents:
98
diff
changeset
|
378 return jinja2.Markup("<a {0}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
|
379 |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
380 |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
381 def read_genelinks(dir): |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
382 links = {} |
99
8f02008a5f20
look at all blast*.loc files; python2.6 compat fix
Jan Kanis <jan.code@jankanis.nl>
parents:
98
diff
changeset
|
383 # blastdb.loc, blastdb_p.loc, blastdb_d.loc, etc. |
101 | 384 files = sorted(glob.glob(path.join(dir, 'blastdb*.loc'))) |
385 # reversed, so blastdb.loc will take precedence | |
386 for f in reversed(files): | |
95
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
387 try: |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
388 f = open(path.join(dir, f)) |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
389 for l in f.readlines(): |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
390 if l.strip().startswith('#'): |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
391 continue |
98
e780606b7c25
test new command line parameters, fix small bug
Jan Kanis <jan.code@jankanis.nl>
parents:
96
diff
changeset
|
392 line = l.rstrip('\n').split('\t') |
95
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
393 try: |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
394 links[line[2]] = line[3] |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
395 except IndexError: |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
396 continue |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
397 f.close() |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
398 except OSError: |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
399 continue |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
400 if not links: |
103 | 401 if not files: |
402 warnings.warn("No gene bank link templates found (no blastdb*.loc files found in {0})".format(dir)) | |
403 else: | |
404 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
|
405 return links |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
406 |
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
|
407 |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
408 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
|
409 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
|
410 |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
411 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
|
412 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
|
413 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
|
414 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
|
415 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
|
416 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
|
417 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
|
418 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
|
419 help='The output html file') |
18
4434ffab721a
add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents:
16
diff
changeset
|
420 # 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
|
421 # 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
|
422 # 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
|
423 # 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
|
424 # 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
|
425 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
|
426 help='The template file to use. Defaults to blast_html.html.jinja') |
95
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
427 |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
428 dblink_group = parser.add_mutually_exclusive_group() |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
429 dblink_group.add_argument('--genelink-template', default='http://www.ncbi.nlm.nih.gov/nucleotide/{accession}?report=genbank&log$=nuclalign', |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
430 help="""A link template to link hits to a gene bank webpage. The template string is a |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
431 Python format string. It can contain the following replacement elements: {id[N]}, {fullid}, |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
432 {defline[N]}, {fulldefline}, {accession}, where N is a number. id[N] and defline[N] will be |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
433 replaced by the Nth element of the id or defline, where '|' is the field separator. |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
434 |
95
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
435 The default is 'http://www.ncbi.nlm.nih.gov/nucleotide/{accession}?report=genbank&log$=nuclalign', |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
436 which is a link to the NCBI nucleotide database.""") |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
437 |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
438 dblink_group.add_argument('--db-config-dir', |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
439 help="""The directory where databases are configured in blastdb*.loc files. These files |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
440 are consulted for creating a gene bank link. The files should be tab-separated tables (with lines |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
441 starting with '#' ignored), where the third field of a line should be a database path and the fourth |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
442 a genebank link template conforming to the --genelink-template option syntax. |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
443 |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
444 This option is incompatible with --genelink-template.""") |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
445 |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
446 args = parser.parse_args() |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
447 if args.input == None: |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
448 args.input = args.positional_arg |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
449 if args.input == None: |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
450 parser.error('no input specified') |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
451 |
104 | 452 if six.PY2: |
453 # The argparse.FileType wrapper doesn't support an encoding | |
454 # argument or such, so for python 2 we need to wrap or reopen | |
455 # the output. The input files are already read as utf-8 by the | |
456 # respective libraries. | |
457 | |
458 # One option is using codecs, but the codecs' writelines() | |
459 # method doesn't support streaming but collects all output and | |
460 # writes at once. On the other hand the io module is slower | |
461 # (though not significantly). | |
462 | |
463 # args.output = codecs.getwriter('utf-8')(args.output) | |
464 args.output = io.open(args.output.name, 'w') | |
465 | |
18
4434ffab721a
add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents:
16
diff
changeset
|
466 templatedir, templatename = path.split(args.template.name) |
4434ffab721a
add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents:
16
diff
changeset
|
467 args.template.close() |
4434ffab721a
add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents:
16
diff
changeset
|
468 if not templatedir: |
4434ffab721a
add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents:
16
diff
changeset
|
469 templatedir = '.' |
4434ffab721a
add a parameter for the template
Jan Kanis <jan.code@jankanis.nl>
parents:
16
diff
changeset
|
470 |
95
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
471 if args.db_config_dir is None: |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
472 genelinks = args.genelink_template |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
473 elif not path.isdir(args.db_config_dir): |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
474 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
|
475 else: |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
476 genelinks = read_genelinks(args.db_config_dir) |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
477 |
4378d11f0ed7
implement configurable gene bank links
Jan Kanis <jan.code@jankanis.nl>
parents:
79
diff
changeset
|
478 b = BlastVisualize(args.input, templatedir, templatename, genelinks) |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
479 b.render(args.output) |
104 | 480 args.output.close() |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
481 |
11
7660519f2dc9
proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents:
10
diff
changeset
|
482 |
12
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
483 if __name__ == '__main__': |
a459c754cdb5
add links, refactor, proper commandline arguments
Jan Kanis <jan.code@jankanis.nl>
parents:
11
diff
changeset
|
484 main() |
11
7660519f2dc9
proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents:
10
diff
changeset
|
485 |