...
 
Commits (2)
......@@ -12,6 +12,7 @@ import tempfile
import numpy as np
import matplotlib.colors as mc
import matplotlib.pyplot as plt
# Note that we currently assume the use of 1000 for band widths to make life
......@@ -39,6 +40,8 @@ band_stroke_color = white
band_transparency = 4
</ideogram>
{colors:s}
{ticks:s}
<image>
......@@ -662,7 +665,7 @@ class CircosHandler(object):
if np.isnan(val):
continue
if isinstance(linkcolors, tuple):
if isinstance(linkcolors, (tuple, list)):
if val == 0:
color = linkcolors[0]
elif val < 0:
......@@ -686,6 +689,89 @@ class CircosHandler(object):
return '\n'.join(s)
def parse_colors(self, linkcolors, defaultlinkcolor):
"""
Parse hex colours into something usable by circos
"""
# Map (r, g, b) tuples to colour names
colour_dict = {}
colidx = 0
if isinstance(linkcolors, (tuple, list)):
lc = []
for colour in linkcolors:
if colour.startswith('#'):
# Parse as #RGB and add to colour_dict
r, g, b = mc.hex2color(colour)
r = int(r * 255)
g = int(g * 255)
b = int(b * 255)
if (r, g, b) not in colour_dict.keys():
colour_dict[(r, g, b)] = 'customcolor{}'.format(colidx)
colidx += 1
lc.append(colour_dict[(r, g, b)])
else:
# Leave as it is
lc.append(colour)
lc = tuple(lc)
elif isinstance(linkcolors, np.ndarray):
lc = linkcolors.copy()
for row in range(lc.shape[0]):
for col in range(lc.shape[1]):
colour = lc[row, col]
if colour is not None and colour.startswith('#'):
# Parse as #RGB and add to colour_dict
r, g, b = mc.hex2color(colour)
r = int(r * 255)
g = int(g * 255)
b = int(b * 255)
if (r, g, b) not in colour_dict.keys():
colour_dict[(r, g, b)] = 'customcolor{}'.format(colidx)
colidx += 1
lc[row, col] = colour_dict[(r, g, b)]
elif linkcolors is None:
lc = None
else:
raise Exception("Cannot parse link colors {}".format(linkcolors))
if defaultlinkcolor is None:
dlc = None
elif defaultlinkcolor.startswith("#"):
# Parse as #RGB and add to colour_dict
r, g, b = mc.hex2color(defaultlinkcolor)
r = int(r * 255)
g = int(g * 255)
b = int(b * 255)
if (r, g, b) not in colour_dict.keys():
colour_dict[(r, g, b)] = 'customcolor{}'.format(colidx)
colidx += 1
dlc = colour_dict[(r, g, b)]
else:
dlc = defaultlinkcolor
# Work out what to put in the colors section (if anything)
colour_text = ""
if len(colour_dict) > 0:
colour_text = """<colors>\n"""
for k, v in colour_dict.items():
colour_text += "{} = {},{},{}\n".format(v, k[0], k[1], k[2])
colour_text += """</colors>\n"""
return lc, dlc, colour_text
def gen_circos_files(self, data, karyotype_name, output_name,
radius=1500,
circos_path='/etc/circos',
......@@ -741,11 +827,13 @@ class CircosHandler(object):
linkcolors = kwargs.get('linkcolors', None)
defaultlinkcolor = kwargs.get('defaultlinkcolor', 'black')
lc, dlc, colors = self.parse_colors(linkcolors, defaultlinkcolor)
circle_radius = kwargs.get('circle_prop', 0.85)
# First of all, create the .txt links file
f = open(linkspath, 'w')
f.write(self.links(data, linkcolors, defaultlinkcolor))
f.write(self.links(data, lc, dlc))
f.close()
# Now the config file - pass all keyword arguments down
......@@ -755,6 +843,7 @@ class CircosHandler(object):
f.write(CIRCOS_CONF_TEMPLATE.format(karyotype=karyotype_name,
ticks=ticks,
radius=radius,
colors=colors,
circospath=circos_path,
linksname=linksname,
outputimage=imagename,
......