Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not require sorted bed #259

Merged
merged 2 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions pygenometracks/readBed.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ def __init__(self, file_handle):
else:
self.get_bed_interval(fields, is_first_line=True)
self.file_handle.seek(0)
self.prev_chrom = None
self.prev_start = -1
self.prev_line = None

# list of bed fields
self.fields = ['chromosome', 'start', 'end',
Expand Down Expand Up @@ -81,15 +78,6 @@ def next(self):
line = self.get_no_comment_line()

bed = self.get_bed_interval(line)
if self.prev_chrom == bed.chromosome:
assert self.prev_start <= bed.start, \
"Bed file not sorted. Please use a sorted bed file.\n" \
f"File: {self.file_handle.name}\n" \
f"Previous line: {self.prev_line}\n Current line{line} "

self.prev_chrom = bed.chromosome
self.prev_start = bed.start
self.prev_line = line

return bed

Expand All @@ -100,15 +88,6 @@ def __next__(self):
line = self.get_no_comment_line()

bed = self.get_bed_interval(line)
if self.prev_chrom == bed.chromosome:
assert self.prev_start <= bed.start, \
"Bed file not sorted. Please use a sorted bed file.\n" \
f"File: {self.file_handle.name}\n" \
f"Previous line: {self.prev_line}\n Current line{line}"

self.prev_chrom = bed.chromosome
self.prev_start = bed.start
self.prev_line = line

return bed

Expand Down
1 change: 1 addition & 0 deletions pygenometracks/tests/generateAllOutput.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ bin/pgt --tracks ./pygenometracks/tests/test_data/bed_all_labels_inside.ini --re
bin/pgt --tracks ./pygenometracks/tests/test_data/bed_all_labels_inside.ini --region X:3215000-3240000 --trackLabelFraction 0.2 --width 38 --dpi 130 --trackLabelHAlign right --decreasingXAxis -o ./pygenometracks/tests/test_data/master_bed_all_label_inside_dec.png
bin/pgt --tracks pygenometracks/tests/test_data/bed_unusual_formats.ini --region X:20000-40000 --trackLabelFraction 0.2 --width 38 --dpi 130 -o pygenometracks/tests/test_data/master_bed_unusual_formats.png
bin/pgt --tracks ./pygenometracks/tests/test_data/bed_colormap_genes.ini --region X:3000000-3300000 --trackLabelFraction 0.2 --width 38 --dpi 130 -o ./pygenometracks/tests/test_data/master_bed_colormap_genes.png
bin/pgt --tracks ./pygenometracks/tests/test_data/bed_shuffle.ini --BED ./pygenometracks/tests/test_data/regions_chr1XY.bed --trackLabelFraction 0.2 --width 38 --dpi 130 -o ./pygenometracks/tests/test_data/master_bed_shuffle.png

# test_bedGraphMatrixTrack:
bin/pgt --tracks ./pygenometracks/tests/test_data/bedgraph.ini --region X:2850000-3150000 --trackLabelFraction 0.2 --dpi 130 -o ./pygenometracks/tests/test_data/master_bedgraph.png
Expand Down
52 changes: 52 additions & 0 deletions pygenometracks/tests/test_bed_and_gtf_tracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,35 @@
with open(os.path.join(ROOT, "bed_colormap_genes.ini"), 'w') as fh:
fh.write(browser_tracks)

browser_tracks = """
[genes]
file = dm3_genes_withrgbandscore.bed.gz
title = bed color = Reds
color = Reds
height = 4

[spacer]

[genes]
file = dm3_genes_withrgbandscore_shuffled.bed.gz
title = bed color = Reds bed is not sorted
color = Reds
height = 4

[spacer]

[genes]
file = dm3_genes_withrgbandscore_shuffled.bed.gz
title = bed color = Reds global_max_row = true and bed is not sorted
color = Reds
global_max_row = true
height = 4

[x-axis]
"""
with open(os.path.join(ROOT, "bed_shuffle.ini"), 'w') as fh:
fh.write(browser_tracks)

tolerance = 13 # default matplotlib pixed difference tolerance


Expand Down Expand Up @@ -822,3 +851,26 @@ def test_plot_tracks_bed_scores():
assert res is None, res

os.remove(outfile.name)


def test_bed_shuffle():
extension = '.png'

outfile = NamedTemporaryFile(suffix=extension, prefix='pyGenomeTracks_test_',
delete=False)
ini_file = os.path.join(ROOT, "bed_shuffle.ini")
bed_file = os.path.join(ROOT, 'regions_chr1XY.bed')
args = f"--tracks {ini_file} --BED {bed_file} "\
"--trackLabelFraction 0.2 --width 38 --dpi 130 "\
f"--outFileName {outfile.name}".split()
pygenometracks.plotTracks.main(args)
for region in ['chr1:0-500000', 'chrX:2500000-2600000', 'chrY:0-1000000']:
region_str = region.replace(':', '-')
output_file = outfile.name[:-4] + '_' + region_str + extension
expected_file = os.path.join(ROOT, 'master_bed_shuffle_'
+ region_str + extension)
res = compare_images(expected_file,
output_file, tolerance)
assert res is None, res

os.remove(output_file)
25 changes: 25 additions & 0 deletions pygenometracks/tests/test_data/bed_shuffle.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

[genes]
file = dm3_genes_withrgbandscore.bed.gz
title = bed color = Reds
color = Reds
height = 4

[spacer]

[genes]
file = dm3_genes_withrgbandscore_shuffled.bed.gz
title = bed color = Reds bed is not sorted
color = Reds
height = 4

[spacer]

[genes]
file = dm3_genes_withrgbandscore_shuffled.bed.gz
title = bed color = Reds global_max_row = true and bed is not sorted
color = Reds
global_max_row = true
height = 4

[x-axis]
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ file_type = hic_matrix
height = 5
show_masked_bins = true

[spacer]

[genes]
file = dm3_genes_withrgbandscore_shuffled.bed.gz
[x-axis]
Binary file modified pygenometracks/tests/test_data/dm3_genes_withrgbandscore.bed.gz
Binary file not shown.
Binary file not shown.
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 0 additions & 7 deletions pygenometracks/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ def file_to_intervaltree(file_name, plot_regions=None):
file_h = opener(file_to_open)
line_number = 0
valid_intervals = 0
prev_chrom = None
prev_start = -1
prev_line = None
interval_tree = {}
min_value = float('Inf')
max_value = -float('Inf')
Expand Down Expand Up @@ -138,10 +135,6 @@ def file_to_intervaltree(file_name, plot_regions=None):
f"an integer.\nError message: {detail}"
raise InputError(msg)

if prev_chrom == chrom:
assert prev_start <= start, \
f"Bed file not sorted. Please use a sorted bed file.\n{prev_line}{line} "

if chrom not in interval_tree:
interval_tree[chrom] = IntervalTree()

Expand Down