Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/deeptools/pyGenomeTracks
Browse files Browse the repository at this point in the history
…into pRegionForAll
  • Loading branch information
lldelisle committed Jul 5, 2020
2 parents a68dbbe + 1f9f4fd commit 63e103d
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 5 deletions.
2 changes: 2 additions & 0 deletions pygenometracks/tests/generateAllOutput.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ bin/pgt --tracks ./pygenometracks/tests/test_data/browser_tracks_hic.ini --regio
bin/pgt --tracks ./pygenometracks/tests/test_data/browser_tracks_hic_small_test.ini --BED ./pygenometracks/tests/test_data/regions_chr1XY.bed --trackLabelFraction 0.23 --width 38 -o ./pygenometracks/tests/test_data/master_plot_hic_small_test.png
bin/pgt --tracks ./pygenometracks/tests/test_data/browser_tracks_hic_small_test.ini --region 1:0-200000 --trackLabelFraction 0.23 --width 38 -o ./pygenometracks/tests/test_data/master_plot_hic_small_test.png
bin/pgt --tracks ./pygenometracks/tests/test_data/browser_tracks_hic_small_test.ini --region chrM:0-20000 --trackLabelFraction 0.23 --width 38 -o ./pygenometracks/tests/test_data/master_plot_hic_small_test_chrM.png
bin/pgt --tracks ./pygenometracks/tests/test_data/browser_tracks_hic_small_test.ini --region chr1:0-5000 --trackLabelFraction 0.23 --width 38 --dpi 130 -o ./pygenometracks/tests/test_data/master_hic_small_test_small_region.png
bin/pgt --tracks ./pygenometracks/tests/test_data/browser_tracks_hic_small_test_2.ini --region chr1:0-100000 --trackLabelFraction 0.23 --width 38 --dpi 130 -o ./pygenometracks/tests/test_data/master_hic_small_test_2.png

# test_logScale:
bin/pgt --tracks ./pygenometracks/tests/test_data/log1p.ini --region X:2700000-3100000 --trackLabelFraction 0.2 --dpi 130 -o ./pygenometracks/tests/test_data/master_log1p.png
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

[hic matrix]
file = small_test2.cool
title = cool with few interactions depth=10kb
file_type = hic_matrix
depth = 10000


[x-axis]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions pygenometracks/tests/test_hiCMatrixTracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,20 @@
with open(os.path.join(ROOT, "browser_tracks_hic_log-log.ini"), 'w') as fh:
fh.write(browser_tracks_with_hic)

browser_tracks_with_hic_small_2 = """
[hic matrix]
file = small_test2.cool
title = cool with few interactions depth=10kb
file_type = hic_matrix
depth = 10000
[x-axis]
"""

with open(os.path.join(ROOT, "browser_tracks_hic_small_test_2.ini"), 'w') as fh:
fh.write(browser_tracks_with_hic_small_2)

tolerance = 13 # default matplotlib pixed difference tolerance


Expand Down Expand Up @@ -420,3 +434,39 @@ def test_plot_tracks_with_hic_small_above_chr_length():
assert res is None, res

os.remove(outfile.name)


def test_plot_hic_depth_smaller_binsize():

outfile = NamedTemporaryFile(suffix='.png', prefix='pyGenomeTracks_test_',
delete=False)
args = "--tracks {0} --region chr1:0-100000 "\
"--trackLabelFraction 0.23 --width 38 " \
"--dpi 130 --outFileName {1}" \
"".format(os.path.join(ROOT, 'browser_tracks_hic_small_test_2.ini'),
outfile.name).split()
pygenometracks.plotTracks.main(args)
res = compare_images(os.path.join(ROOT, 'master_hic_small_test_2.png'),
outfile.name, tolerance)
print("saving test to {}".format(outfile.name))
assert res is None, res

os.remove(outfile.name)


def test_plot_hic_plotting_region_smaller_binsize():

outfile = NamedTemporaryFile(suffix='.png', prefix='pyGenomeTracks_test_',
delete=False)
args = "--tracks {0} --region chr1:0-5000 "\
"--trackLabelFraction 0.23 --width 38 " \
"--dpi 130 --outFileName {1}" \
"".format(os.path.join(ROOT, 'browser_tracks_hic_small_test.ini'),
outfile.name).split()
pygenometracks.plotTracks.main(args)
res = compare_images(os.path.join(ROOT, 'master_hic_small_test_small_region.png'),
outfile.name, tolerance)
print("saving test to {}".format(outfile.name))
assert res is None, res

os.remove(outfile.name)
15 changes: 10 additions & 5 deletions pygenometracks/tracks/HiCMatrixTrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ def set_properties_defaults(self):

self.hic_ma.cut_intervals = new_intervals
binsize = self.hic_ma.getBinSize()
max_depth_in_bins = int(self.properties['depth'] / binsize)
# Need to be sure that you keep at least one bin even if the depth is
# smaller than the binsize
max_depth_in_bins = max(1, int(self.properties['depth'] / binsize))

# work only with the lower matrix
# and remove all pixels that are beyond
Expand Down Expand Up @@ -273,7 +275,9 @@ def plot(self, ax, chrom_region, region_start, region_end):

region_len = region_end - region_start
depth = min(self.properties['depth'], int(region_len * 1.25))
depth_in_bins = int(1.5 * region_len / self.hic_ma.getBinSize())
# Need to be sure that you keep at least one bin even if the depth is
# smaller than the binsize
depth_in_bins = max(1, int(1.5 * region_len / self.hic_ma.getBinSize()))

if depth < self.properties['depth']:
log.warning("The depth was set to {} which is more than 125%"
Expand Down Expand Up @@ -316,12 +320,13 @@ def plot(self, ax, chrom_region, region_start, region_end):
vmin = self.properties['min_value']
else:
if depth_in_bins > matrix.shape[0]:
depth_in_bins = matrix.shape[0] - 5
# Make sure you keep one bin
depth_in_bins = max(1, matrix.shape[0] - 5)

# if the region length is large with respect to the chromosome length, the diagonal may have
# very few values or none. Thus, the following lines reduce the number of bins until the
# diagonal is at least length 5
num_bins_from_diagonal = int(region_len / self.hic_ma.getBinSize())
# diagonal is at least length 5 but make sure you have at least one value:
num_bins_from_diagonal = max(1, int(region_len / self.hic_ma.getBinSize()))
for num_bins in range(0, num_bins_from_diagonal)[::-1]:
distant_diagonal_values = matrix.diagonal(num_bins)
if len(distant_diagonal_values) > 5:
Expand Down

0 comments on commit 63e103d

Please sign in to comment.