-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfdi_to_svg.py
192 lines (156 loc) · 6.56 KB
/
fdi_to_svg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""
Convert an FDI file to an SVG
Note - this is strictly restricted to the FDI files I've been using...
"""
import svgwrite
import re
import sys
from collections import defaultdict
import math
import logging
logger = logging.getLogger(__name__)
taxon_re = re.compile("TAXON_NAME;([^;]+);TAXON_NAME_HEIGHT;([^;]+);TAXON_NAME_COLOR;[0-9]+;TAXON_FREQUENCY;[0-9]+;TAXON_ORIG_FREQUENCY;[0-9]+;TAXON_GEOGRAPHY;;TAXON_PHENOTYPE;;TAXON_LINEAGE;;TAXON_GROUP1;Group1;;TAXON_GROUP2;Group2;;TAXON_GROUP3;Group3;;TAXON_X;([^;]+);TAXON_Y;([^;]+);TAXON_COLOR_PIE1;([^;]+);TAXON_PIE_FREQUENCY1;[0-9]+;TAXON_STYLE_PIE1;SOLID;TAXON_LINE_WIDTH;[0-9]+;TAXON_LINE_COLOR;[0-9]+;TAXON_LINE_STYLE;SOLID;TAXON_ACTIVE;(TRUE|FALSE)")
link_re = re.compile("LINK_TAXON1;([^;]+);LINK_TAXON2;([^;]+);.*")
class Taxon(object):
def __init__(self, name, fontsize, x, y, colour, weight):
self.name = name
self.fontsize = fontsize
self.x = x
self.y = y
self.colour = colour
self.weight = weight
colmap = {'65535': 'yellow',
'255': 'red'}
def parse(fdi_f):
with open(fdi_f) as f:
in_params = True
in_taxa = False
in_links = False
in_post = False
params = {}
taxa = {}
taxa_equivalencies = defaultdict(list)
links = []
for line in f:
line = line.strip()
if not line:
continue
logger.debug(line)
if in_params:
if line.startswith('TAXON_NAME'):
in_params = False
in_taxa = True
else:
k, v = line.split(';', 1)
params[k] = v
if in_taxa:
if line.startswith('NUMBER_OF_LINKS'):
in_taxa = False
in_links = True
continue
match = taxon_re.match(line)
if not match:
raise ValueError("Didn't understand this line: {}".format(line))
name, fontsize, x, y, colour, active = match.groups()
if active != 'TRUE':
logger.warning("Taxon {} is not active - it will have been "
"subsumed into another taxon".format(name))
continue
t = Taxon(name, int(fontsize), int(x), int(y), colmap[colour], 1)
taxa[t.name] = t
if in_links:
if not line.startswith('LINK_TAXON'):
in_post = True
else:
match = link_re.match(line)
if not match:
raise ValueError("Didn't understand this line: {}".format(line))
links.append((match.group(1), match.group(2)))
if in_post:
# Final metadata
if line.startswith('EQUIVALENT_TAXA'):
a, b = line.split(';', 2)[1:]
taxa_equivalencies[a].append(b)
elif ';' in line:
k, v = line.split(';', 1)
params[k] = v
# Sort out taxa weights
for t, v in taxa_equivalencies.items():
taxa[t].weight = len(v) + 1
taxa[t].name = ', '.join([taxa[t].name.strip()] + [x.strip() for x in v])
return params, taxa, links
def convert(fdi_f, svg_f, shrink_factor=1):
params, taxa, links = parse(fdi_f)
min_x = 10e10
max_x = 0
min_y = 10e10
max_y = 0
for t in taxa.values():
if t.x > max_x:
max_x = t.x
if t.y > max_y:
max_y = t.y
if t.x < min_x:
min_x = t.x
if t.y < min_y:
min_y = t.y
size_x = max_x - min_x
size_y = max_y - min_y
padding = size_x * 0.1
offset_x = padding // 2 - min_x
offset_y = padding // 2 - min_y
sizemult = size_x * shrink_factor / 600.0
logger.info("Magic size multiplication faction: {}".format(sizemult))
logger.debug(str((min_x, max_x, min_y, max_y)))
dwg = svgwrite.Drawing(filename=svg_f, size=(size_x + padding, size_y + padding))
for start, end in links:
line = svgwrite.shapes.Line(start=(taxa[start].x + offset_x, taxa[start].y + offset_y),
end=(taxa[end].x + offset_x, taxa[end].y + offset_y),
stroke='#333333')
dwg.add(line)
for t in taxa.values():
if t.name.startswith('mv'):
# median vector
radius = int(int(params['MED_RADIUS']) * sizemult)
else:
radius = int(int(params['MIN_CIRC_RADIUS']) * sizemult * math.sqrt(float(t.weight)))
g = svgwrite.container.Group()
circle = svgwrite.shapes.Circle(center=(t.x + offset_x, t.y + offset_y),
r=radius + 1,
fill='black')
g.add(circle)
innercircle = svgwrite.shapes.Circle(center=(t.x + offset_x, t.y + offset_y),
r=radius,
fill=t.colour)
g.add(innercircle)
if not t.name.startswith('mv'):
t = svgwrite.text.Text(t.name.strip(),
x=[t.x + offset_x + radius * 2],
y=[t.y + offset_y],
font_size=int(t.fontsize * sizemult),
font_family='ariel')
g.add(t)
dwg.add(g)
dwg.save()
logger.info("Written {}".format(svg_f))
if __name__ == "__main__":
import argparse
import os.path
parser = argparse.ArgumentParser(description="Convert an FDI file into an SVG")
parser.add_argument('fdifile', help='FDI input filename')
parser.add_argument('--shrink', default=1, type=float, help='Shrink factor to apply to circles (e.g. 0.5). Default is 1.')
parser.add_argument('-v', '--verbose', action="store_true", help="Tell me what's going on")
args = parser.parse_args()
h1 = logging.StreamHandler(sys.stderr)
rootLogger = logging.getLogger()
rootLogger.addHandler(h1)
formatter = logging.Formatter('[%(asctime)s] [%(process)s] [%(filename)s:%(lineno)s] [%(levelname)s] %(message)s')
h1.setFormatter(formatter)
if args.verbose:
rootLogger.setLevel(logging.DEBUG)
logger.debug("Verbose mode")
else:
rootLogger.setLevel(logging.INFO)
logger.debug("Run with --verbose for debug mode")
svgfile = "{}.svg".format(os.path.splitext(args.fdifile)[0])
convert(args.fdifile, svgfile, args.shrink)