Mercurial > repos > jankanis > blast2html
annotate visualise.py @ 11:7660519f2dc9
proper layout for alignments, added some links
author | Jan Kanis <jan.code@jankanis.nl> |
---|---|
date | Mon, 12 May 2014 13:55:04 +0200 |
parents | 2fbdf2eb27b4 |
children | a459c754cdb5 |
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): |
11
7660519f2dc9
proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents:
10
diff
changeset
|
19 "Decorator to register a function as filter in the current jinja environment" |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
20 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
|
21 def inner(func): |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
22 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
|
23 return func |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
24 return inner |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
25 else: |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
26 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
|
27 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
|
28 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
29 |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
30 def color_idx(length): |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
31 if length < 40: |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
32 return 0 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
33 elif length < 50: |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
34 return 1 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
35 elif length < 80: |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
36 return 2 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
37 elif length < 200: |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
38 return 3 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
39 return 4 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
40 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
41 colors = ['black', 'blue', 'green', 'magenta', 'red'] |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
42 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
43 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
|
44 |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
45 @filter |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
46 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
|
47 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
|
48 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
49 @filter |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
50 def firsttitle(hit): |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
51 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
|
52 |
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 othertitles(hit): |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
55 """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
|
56 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
|
57 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
58 titles = [] |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
59 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
|
60 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
|
61 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
|
62 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
|
63 fullid = fullid, |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
64 title = title)) |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
65 return titles |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
66 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
67 @filter |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
68 def hitid(hit): |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
69 return hit.Hit_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
|
70 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
71 @filter |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
72 def seqid(hit): |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
73 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
|
74 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
75 @filter |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
76 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
|
77 return ( |
11
7660519f2dc9
proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents:
10
diff
changeset
|
78 "Query {:>7s} {} {}\n".format(hsp['Hsp_query-from'], hsp.Hsp_qseq, hsp['Hsp_query-to']) + |
7660519f2dc9
proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents:
10
diff
changeset
|
79 " {:7s} {}\n".format('', hsp.Hsp_midline) + |
7660519f2dc9
proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents:
10
diff
changeset
|
80 "Subject{:>7s} {} {}".format(hsp['Hsp_hit-from'], hsp.Hsp_hseq, hsp['Hsp_hit-to']) |
7660519f2dc9
proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents:
10
diff
changeset
|
81 ) |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
82 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
83 @filter('len') |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
84 def hsplen(node): |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
85 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
|
86 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
87 @filter |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
88 def asframe(frame): |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
89 if 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 'Plus' |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
91 elif frame == -1: |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
92 return 'Minus' |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
93 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
|
94 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
95 |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
96 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
|
97 |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
98 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
|
99 # 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
|
100 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
|
101 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
|
102 reverse=True) |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
103 |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
104 def match_colors(): |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
105 """ |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
106 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
|
107 """ |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
108 |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
109 percent_multiplier = 100 / query_length |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
110 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
111 for hit in hits: |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
112 # 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
|
113 # 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
|
114 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
|
115 table = bytearray([255]) * query_length |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
116 for hsp in hotspots: |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
117 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
|
118 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
|
119 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
|
120 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
121 matches = [] |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
122 last = table[0] |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
123 count = 0 |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
124 for i in range(query_length): |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
125 if table[i] == last: |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
126 count += 1 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
127 continue |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
128 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
|
129 last = table[i] |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
130 count = 1 |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
131 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
|
132 |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
133 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
|
134 |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
135 |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
136 def queryscale(): |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
137 max_labels = 10 |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
138 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
|
139 percent_multiplier = 100 / query_length |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
140 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
|
141 if i % 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 = i, width = skip * percent_multiplier) |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
143 if query_length % skip != 0: |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
144 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
|
145 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
146 |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
147 def hit_info(): |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
148 |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
149 for hit in ordered_hits: |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
150 hsps = hit.Hit_hsps.Hsp |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
151 |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
152 cover = [False] * query_length |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
153 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
|
154 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
|
155 cover_count = cover.count(True) |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
156 |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
157 def hsp_val(path): |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
158 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
|
159 |
11
7660519f2dc9
proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents:
10
diff
changeset
|
160 yield dict(hit = hit, |
7660519f2dc9
proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents:
10
diff
changeset
|
161 title = firsttitle(hit), |
10
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
162 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
|
163 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
|
164 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
|
165 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
|
166 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
|
167 # 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
|
168 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
|
169 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
|
170 |
2fbdf2eb27b4
All data is displayed now, still some formatting to do
Jan Kanis <jan.code@jankanis.nl>
parents:
7
diff
changeset
|
171 |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
172 def main(): |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
173 template = environment.get_template('visualise.html.jinja') |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
174 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
175 params = (('Query ID', blast["BlastOutput_query-ID"]), |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
176 ('Query definition', blast["BlastOutput_query-def"]), |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
177 ('Query length', blast["BlastOutput_query-len"]), |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
178 ('Program', blast.BlastOutput_version), |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
179 ('Database', blast.BlastOutput_db), |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
180 ) |
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 if len(blast.BlastOutput_iterations.Iteration) > 1: |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
183 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
|
184 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
185 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
|
186 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
|
187 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
|
188 colors=colors, |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
189 match_colors=match_colors(), |
7
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
190 queryscale=queryscale(), |
9e7927673089
intermediate commit before converting some tables to divs
Jan Kanis <jan.code@jankanis.nl>
parents:
5
diff
changeset
|
191 hit_info=hit_info(), |
5
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
192 params=params)) |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
193 |
1df2bfce5c24
first features are working, partial match table
Jan Kanis <jan.code@jankanis.nl>
parents:
diff
changeset
|
194 main() |
11
7660519f2dc9
proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents:
10
diff
changeset
|
195 |
7660519f2dc9
proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents:
10
diff
changeset
|
196 # http://www.ncbi.nlm.nih.gov/nucleotide/557804451?report=genbank&log$=nuclalign&blast_rank=1&RID=PHWP1JNZ014 |
7660519f2dc9
proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents:
10
diff
changeset
|
197 # http://www.ncbi.nlm.nih.gov/nuccore/557804451?report=graph&rid=PHWP1JNZ014[557804451]&tracks=[key:sequence_track,name:Sequence,display_name:Sequence,id:STD1,category:Sequence,annots:Sequence,ShowLabel:true][key:gene_model_track,CDSProductFeats:false][key:alignment_track,name:other%20alignments,annots:NG%20Alignments%7CRefseq%20Alignments%7CGnomon%20Alignments%7CUnnamed,shown:false]&v=752:2685&appname=ncbiblast&link_loc=fromSubj |
7660519f2dc9
proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents:
10
diff
changeset
|
198 |
7660519f2dc9
proper layout for alignments, added some links
Jan Kanis <jan.code@jankanis.nl>
parents:
10
diff
changeset
|
199 # http://www.ncbi.nlm.nih.gov/nucleotide/557804451?report=genbank&log$=nucltop&blast_rank=1&RID=PHWP1JNZ014 |