Skip to content

Instantly share code, notes, and snippets.

@fidelram
Created May 3, 2017 07:36
Show Gist options
  • Save fidelram/630bbcbd60be26e5a5fea54c422067ac to your computer and use it in GitHub Desktop.
Save fidelram/630bbcbd60be26e5a5fea54c422067ac to your computer and use it in GitHub Desktop.
Code to create the images needed to compose the hicExplorer spiral tracks (http://hicexplorer.readthedocs.io/en/latest/_images/hicex2.png). Once the plots are generated they need to be put together using GIMP, Photoshop, or similar program. To run, HiCExplorer and ImageMagick need to be installed
import sys
import os
import hicexplorer.trackPlot
dpi = 350
#dpi = 50
increment = int(1.0e6)
start = int(3e6)
frac_decrease = 0.15 # fraction of decrease per image
trp = hicexplorer.trackPlot.PlotTracks("tracks.ini", 80, dpi=dpi)
#trp = hicexplorer.trackPlot.PlotTracks("test/tracks.ini", 80, dpi=dpi)
hicexplorer.trackPlot.DEFAULT_WIDTH_RATIOS = (1, 0.00)
hicexplorer.trackPlot.DEFAULT_MARGINS = {'left': 0, 'right': 1, 'bottom': 0, 'top': 1}
angle = 45
count = 0
width_list = []
height_list = []
image_name_list = []
for start in range(3000000, int(7e6), increment):
end = start + increment
chrom='chr2R'
file_name = "/tmp/{}.png".format(count + 1)
file_name_convert = "images/{}.png".format(count + 1)
width, height = trp.plot(file_name, chrom, start, end, title='')
# transform inches to pixels
width = int(width * dpi)
height = int(height * dpi)
image_name_list.append(file_name_convert)
resize_width = width * (1 - frac_decrease)**count
width_list.append(resize_width)
height_list.append(resize_width)
count += 1
perspective = "Perspective '0,0,0,0 0,{0},0,{0} {1},0,{1},{2} {1},{0},{1},{0}'".format(height, width, height * frac_decrease)
# -transparent white
convert = "convert {} -distort {} -resize {}x -matte -virtual-pixel transparent miff:- | " \
"convert - -matte -virtual-pixel transparent -distort Arc '90 {}' {}".format(file_name, perspective,
resize_width,
angle, file_name_convert)
print convert
os.system(convert)
angle += 90
###
#
# the following code to make an spiral automatically does not work
# because the images are not properly placed. Some more work is needed
# the size of the final spiral is:
# x = width of the 3th image + the width of the 1st image
# y = height of first image + height of second image
"""
import commands
for img in image_name_list:
# get size of image
#import ipdb;ipdb.set_trace()
res = commands.getstatusoutput('identify -format "%w %h" {}'.format(img))
width, height = res[1].split()
width_list.append(int(width))
height_list.append(int(height))
size = "{x}x{y}".format(x=width_list[2] + width_list[0], y=height_list[0] + height_list[1])
convert_cmd = "convert -size {size} xc:none ".format(size=size)
origin = [width_list[2], 0]
for idx in range(len(width_list)):
# y for upper right is 0
# x for upper right is width_list[2]
convert_cmd += "{image} -geometry +{x}+{y} -composite ".format(image=image_name_list[idx],
x=origin[0],
y=origin[1])
if idx == len(width_list) - 1:
break
if idx % 4 == 0:
origin[0] += 2 * (width_list[idx] - width_list[idx + 1]) / 3
origin[1] += width_list[idx]
if idx % 4 == 1:
origin[0] -= width_list[idx + 1]
origin[1] -= (height_list[idx] - height_list[idx + 1]) / 2
if idx % 4 == 2:
origin[0] += (width_list[idx] - width_list[idx + 1]) / 2
origin[1] -= width_list[idx + 1]
if idx %4 == 3:
origin[0] += width_list[idx + 1]
origin[1] += (height_list[idx] - height_list[idx + 1]) / 2
os.system(convert_cmd + " test/composite.png")
"""
@wyim-pgl
Copy link

wyim-pgl commented May 4, 2017

Is there any example inputs?

Thank you so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment