Skip to content

Commit

Permalink
adjust deletions plot to be overlayed with collapsed bed
Browse files Browse the repository at this point in the history
  • Loading branch information
lldelisle committed Jun 20, 2023
1 parent 9d65dbe commit aac0c4c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 17 deletions.
13 changes: 13 additions & 0 deletions pygenometracks/tests/test_bed_and_gtf_tracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,19 @@
labels = false
height = 1
[spacer]
[empty deletion]
file = empty.bed
display = deletions
height = 1
title = no deletion ovelayed with genes
[genes]
file = dm3_genes_withrgbandscore.bed.gz
overlay_previous = share-y
display = collapsed
[x-axis]
"""
with open(os.path.join(ROOT, "bed_deletions.ini"), 'w') as fh:
Expand Down
13 changes: 13 additions & 0 deletions pygenometracks/tests/test_data/bed_deletions.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,17 @@ color = red
labels = false
height = 1

[spacer]

[empty deletion]
file = empty.bed
display = deletions
height = 1
title = no deletion ovelayed with genes

[genes]
file = dm3_genes_withrgbandscore.bed.gz
overlay_previous = share-y
display = collapsed

[x-axis]
Binary file modified pygenometracks/tests/test_data/master_bed_deletions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pygenometracks/tests/test_data/master_bed_deletions_zoom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pygenometracks/tests/test_data/master_bed_displays.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 29 additions & 17 deletions pygenometracks/tracks/BedTrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
DEFAULT_DISPLAY_BED = 'stacked'
AROUND_REGION = 100000

EPSILON = 0.08


class BedTrack(GenomeTrack):
SUPPORTED_ENDINGS = ['bed', 'bed3', 'bed4', 'bed5', 'bed6', 'bed8',
Expand Down Expand Up @@ -401,13 +403,16 @@ def plot(self, ax, chrom_region, start_region, end_region):
chrom_region_before = chrom_region
chrom_region = change_chrom_names(chrom_region)
if chrom_region not in self.interval_tree.keys():
self.log.warning("*Warning*\nNo interval was found when "
"overlapping with both "
f"{chrom_region_before}:{start_region - AROUND_REGION}-{end_region + AROUND_REGION}"
f" and {chrom_region}:{start_region - AROUND_REGION}-{end_region + AROUND_REGION}"
" inside the bed file. "
"This will generate an empty track!!\n")
return
if self.properties['display'] != 'deletions':
self.log.warning("*Warning*\nNo interval was found when "
"overlapping with both "
f"{chrom_region_before}:{start_region - AROUND_REGION}-{end_region + AROUND_REGION}"
f" and {chrom_region}:{start_region - AROUND_REGION}-{end_region + AROUND_REGION}"
" inside the bed file. "
"This will generate an empty track!!\n")
return
else:
self.interval_tree[chrom_region] = IntervalTree()

genes_overlap = \
sorted(self.interval_tree[chrom_region][start_region:end_region])
Expand Down Expand Up @@ -626,8 +631,7 @@ def is_right_to(a, b):
" for the interval plotted"
f" ({chrom_region}:{start_region}-{end_region}).\n")

epsilon = 0.08
ymax = - epsilon
ymax = - EPSILON

# We set ymin and ymax to have genes centered epsilon from the border

Expand All @@ -637,21 +641,23 @@ def is_right_to(a, b):
elif self.properties['gene_rows'] is not None:
max_ypos = self.properties['gene_rows'] * self.row_scale

ymin = max_ypos + (1 + epsilon)
ymin = max_ypos + (1 + EPSILON)

self.log.debug(f"ylim {ymin},{ymax}")
# the axis is inverted (thus, ymax < ymin)
ax.set_ylim(ymin, ymax)

if self.properties['display'] == 'interleaved':
ax.set_ylim(2 + epsilon, ymax)
ax.set_ylim(2 + EPSILON, ymax)
elif self.properties['display'] == 'collapsed':
ax.set_ylim(1 + epsilon, ymax)
ax.set_ylim(1 + EPSILON, ymax)

if self.properties['orientation'] == 'inverted':
ylims = ax.get_ylim()
ax.set_ylim(ylims[1], ylims[0])

self.log.debug(f"ylim {ax.get_ylim()}")

def plot_label(self, label_ax, width_dpi, h_align='left'):
if h_align == 'left':
label_ax.text(0.05, 1, self.properties['title'],
Expand Down Expand Up @@ -1183,14 +1189,20 @@ def plot_deletions(self, ax, genes_overlap, start_region, end_region):
" "
"
label
genes_overlap can be empty, then it plots
_____________________________
"""
valid_regions = 0
back_bone = []
last_plotted = start_region
y1 = 1
y2 = 0
y3 = -0.5
y1 = 0.5
y2 = -0.5
y3 = -1
min_y = y2
x1 = start_region
x2 = start_region
x3 = start_region

for region in genes_overlap:
"""
x1 x2 x3
Expand Down Expand Up @@ -1242,8 +1254,8 @@ def plot_deletions(self, ax, genes_overlap, start_region, end_region):
linewidth=self.properties['line_width'])
if valid_regions == 0:
self.log.warning(f"No regions found for section {self.properties['section_name']}.\n")
# Set ylim:
ax.set_ylim(min_y - 0.05, ax.get_ylim()[1])
# Set ylim (to be compatible with collapsed genes):
ax.set_ylim(min_y - 0.05, 1 + EPSILON)

def _plot_small_arrow(self, ax, xpos, ypos_top, ypos_bottom, strand, bed):
"""
Expand Down

0 comments on commit aac0c4c

Please sign in to comment.