Mercurial > repos > jankanis > blast2html2
annotate visualise.py @ 10:2fbdf2eb27b4
All data is displayed now, still some formatting to do
author | Jan Kanis <jan.code@jankanis.nl> |
---|---|
date | Fri, 09 May 2014 18:16:48 +0200 |
parents | 9e7927673089 |
children | 7660519f2dc9 |
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 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
2 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
3 # Copyright The Hyve B.V. 2014 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
4 # License: GPL version 3 or higher |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
5 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
6 import sys |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
7 import math |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
8 import warnings |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
9 from itertools import repeat |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
10 from lxml import objectify |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
11 import jinja2 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
12 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
13 |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
14 blast = objectify.parse('blast xml example1.xml').getroot() |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
15 loader = jinja2.FileSystemLoader(searchpath='.') |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
16 environment = jinja2.Environment(loader=loader, lstrip_blocks=True, trim_blocks=True, autoescape=True) |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
17 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
18 def filter(func_or_name): |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
19 if isinstance(func_or_name, str): |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
20 def inner(func): |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
21 environment.filters[func_or_name] = func |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
22 return func |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
23 return inner |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
24 else: |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
25 environment.filters[func_or_name.__name__] = func_or_name |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
26 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
|
27 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
28 |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
29 def color_idx(length): |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
30 if length < 40: |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
31 return 0 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
32 elif length < 50: |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
33 return 1 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
34 elif length < 80: |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
35 return 2 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
36 elif length < 200: |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
37 return 3 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
38 return 4 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
39 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
40 colors = ['black', 'blue', 'green', 'magenta', 'red'] |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
41 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
42 environment.filters['color'] = lambda length: match_colors[color_idx(length)] |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
43 |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
44 @filter |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
45 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
|
46 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
|
47 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
48 @filter |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
49 def firsttitle(hit): |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
50 return hit.Hit_def.text.split('>')[0] |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
51 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
52 @filter |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
53 def othertitles(hit): |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
54 """Split a hit.Hit_def that contains multiple titles up, splitting out the hit ids from the titles.""" |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
55 id_titles = hit.Hit_def.text.split('>') |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
56 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
57 titles = [] |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
58 for t in id_titles[1:]: |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
59 fullid, title = t.split(' ', 1) |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
60 id = fullid.split('|', 2)[2] |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
61 titles.append(dict(id = id, |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
62 fullid = fullid, |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
63 title = title)) |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
64 return titles |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
65 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
66 @filter |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
67 def hitid(hit): |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
68 return hit.Hit_id.text.split('|', 2)[1] |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
69 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
70 @filter |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
71 def seqid(hit): |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
72 return hit.Hit_id.text.split('|', 2)[2] |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
73 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
74 @filter |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
75 def alignment_pre(hsp): |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
76 return ( |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
77 "Query {:>7s} {} {}\n".format(hsp['Hsp_query-from'], hsp.Hsp_qseq, hsp['Hsp_query-to']) + |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
78 " {:7s} {}\n".format('', hsp.Hsp_midline) + |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
79 "Subject {:>7s} {} {}".format(hsp['Hsp_hit-from'], hsp.Hsp_hseq, hsp['Hsp_hit-to'])) |
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 @filter('len') |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
82 def hsplen(node): |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
83 return int(node['Hsp_align-len']) |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
84 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
85 @filter |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
86 def asframe(frame): |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
87 if frame == 1: |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
88 return 'Plus' |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
89 elif frame == -1: |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
90 return 'Minus' |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
91 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
|
92 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
93 |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
94 query_length = int(blast["BlastOutput_query-len"]) |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
95 |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
96 hits = blast.BlastOutput_iterations.Iteration.Iteration_hits.Hit |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
97 # sort hits by longest hotspot first |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
98 ordered_hits = sorted(hits, |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
99 key=lambda h: max(hsplen(hsp) for hsp in h.Hit_hsps.Hsp), |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
100 reverse=True) |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
101 |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
102 def match_colors(): |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
103 """ |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
104 An iterator that yields lists of length-color pairs. |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
105 """ |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
106 |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
107 percent_multiplier = 100 / query_length |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
108 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
109 for hit in hits: |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
110 # sort hotspots from short to long, so we can overwrite index colors of |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
111 # short matches with those of long ones. |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
112 hotspots = sorted(hit.Hit_hsps.Hsp, key=lambda hsp: hsplen(hsp)) |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
113 table = bytearray([255]) * query_length |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
114 for hsp in hotspots: |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
115 frm = hsp['Hsp_query-from'] - 1 |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
116 to = int(hsp['Hsp_query-to']) |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
117 table[frm:to] = repeat(color_idx(hsplen(hsp)), to - frm) |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
118 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
119 matches = [] |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
120 last = table[0] |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
121 count = 0 |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
122 for i in range(query_length): |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
123 if table[i] == last: |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
124 count += 1 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
125 continue |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
126 matches.append((count * percent_multiplier, colors[last] if last != 255 else 'none')) |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
127 last = table[i] |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
128 count = 1 |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
129 matches.append((count * percent_multiplier, colors[last] if last != 255 else 'none')) |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
130 |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
131 yield dict(colors=matches, link="#hit"+hit.Hit_num.text, defline=firsttitle(hit)) |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
132 |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
133 |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
134 def queryscale(): |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
135 max_labels = 10 |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
136 skip = math.ceil(query_length / max_labels) |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
137 percent_multiplier = 100 / query_length |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
138 for i in range(1, query_length+1): |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
139 if i % skip == 0: |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
140 yield dict(label = i, width = skip * percent_multiplier) |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
141 if query_length % skip != 0: |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
142 yield dict(label = query_length, width = (query_length % skip) * percent_multiplier) |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
143 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
144 |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
145 def hit_info(): |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
146 |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
147 for hit in ordered_hits: |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
148 hsps = hit.Hit_hsps.Hsp |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
149 |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
150 cover = [False] * query_length |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
151 for hsp in hsps: |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
152 cover[hsp['Hsp_query-from']-1 : int(hsp['Hsp_query-to'])] = repeat(True, hsplen(hsp)) |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
153 cover_count = cover.count(True) |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
154 |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
155 def hsp_val(path): |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
156 return (hsp[path] for hsp in hsps) |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
157 |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
158 yield dict(title = firsttitle(hit), |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
159 link_id = hit.Hit_num, |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
160 maxscore = "{:.1f}".format(float(max(hsp_val('Hsp_bit-score')))), |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
161 totalscore = "{:.1f}".format(float(sum(hsp_val('Hsp_bit-score')))), |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
162 cover = "{:.0%}".format(cover_count / query_length), |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
163 e_value = "{:.4g}".format(float(min(hsp_val('Hsp_evalue')))), |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
164 # FIXME: is this the correct formula vv? |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
165 ident = "{:.0%}".format(float(min(hsp.Hsp_identity / hsplen(hsp) for hsp in hsps))), |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
166 accession = hit.Hit_accession) |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
167 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
168 |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
169 def main(): |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
170 template = environment.get_template('visualise.html.jinja') |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
171 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
172 params = (('Query ID', blast["BlastOutput_query-ID"]), |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
173 ('Query definition', blast["BlastOutput_query-def"]), |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
174 ('Query length', blast["BlastOutput_query-len"]), |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
175 ('Program', blast.BlastOutput_version), |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
176 ('Database', blast.BlastOutput_db), |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
177 ) |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
178 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
179 if len(blast.BlastOutput_iterations.Iteration) > 1: |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
180 warnings.warn("Multiple 'Iteration' elements found, showing only the first") |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
181 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
182 sys.stdout.write(template.render(blast=blast, |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
183 length=query_length, |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
184 hits=blast.BlastOutput_iterations.Iteration.Iteration_hits.Hit, |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
185 colors=colors, |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
186 match_colors=match_colors(), |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
187 queryscale=queryscale(), |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
188 hit_info=hit_info(), |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
189 params=params)) |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
190 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
191 main() |