diff --git a/pygenometracks/readBed.py b/pygenometracks/readBed.py index 00a9b63f..d250b9e8 100644 --- a/pygenometracks/readBed.py +++ b/pygenometracks/readBed.py @@ -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', @@ -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 @@ -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 diff --git a/pygenometracks/tests/generateAllOutput.sh b/pygenometracks/tests/generateAllOutput.sh index ab88c178..545b302a 100644 --- a/pygenometracks/tests/generateAllOutput.sh +++ b/pygenometracks/tests/generateAllOutput.sh @@ -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 diff --git a/pygenometracks/tests/test_bed_and_gtf_tracks.py b/pygenometracks/tests/test_bed_and_gtf_tracks.py index 23441119..2743f038 100644 --- a/pygenometracks/tests/test_bed_and_gtf_tracks.py +++ b/pygenometracks/tests/test_bed_and_gtf_tracks.py @@ -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 @@ -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) diff --git a/pygenometracks/tests/test_data/bed_shuffle.ini b/pygenometracks/tests/test_data/bed_shuffle.ini new file mode 100644 index 00000000..6943691a --- /dev/null +++ b/pygenometracks/tests/test_data/bed_shuffle.ini @@ -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] diff --git a/pygenometracks/tests/test_data/browser_tracks_hic_small_test.ini b/pygenometracks/tests/test_data/browser_tracks_hic_small_test.ini index ca63e303..8364e64e 100644 --- a/pygenometracks/tests/test_data/browser_tracks_hic_small_test.ini +++ b/pygenometracks/tests/test_data/browser_tracks_hic_small_test.ini @@ -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] diff --git a/pygenometracks/tests/test_data/dm3_genes_withrgbandscore.bed.gz b/pygenometracks/tests/test_data/dm3_genes_withrgbandscore.bed.gz index d7542b7c..3346cc95 100644 Binary files a/pygenometracks/tests/test_data/dm3_genes_withrgbandscore.bed.gz and b/pygenometracks/tests/test_data/dm3_genes_withrgbandscore.bed.gz differ diff --git a/pygenometracks/tests/test_data/dm3_genes_withrgbandscore_shuffled.bed.gz b/pygenometracks/tests/test_data/dm3_genes_withrgbandscore_shuffled.bed.gz new file mode 100644 index 00000000..44a1de45 Binary files /dev/null and b/pygenometracks/tests/test_data/dm3_genes_withrgbandscore_shuffled.bed.gz differ diff --git a/pygenometracks/tests/test_data/master_bed_shuffle_chr1-0-500000.png b/pygenometracks/tests/test_data/master_bed_shuffle_chr1-0-500000.png new file mode 100644 index 00000000..1e17cc07 Binary files /dev/null and b/pygenometracks/tests/test_data/master_bed_shuffle_chr1-0-500000.png differ diff --git a/pygenometracks/tests/test_data/master_bed_shuffle_chrX-2500000-2600000.png b/pygenometracks/tests/test_data/master_bed_shuffle_chrX-2500000-2600000.png new file mode 100644 index 00000000..4b69ba64 Binary files /dev/null and b/pygenometracks/tests/test_data/master_bed_shuffle_chrX-2500000-2600000.png differ diff --git a/pygenometracks/tests/test_data/master_bed_shuffle_chrY-0-1000000.png b/pygenometracks/tests/test_data/master_bed_shuffle_chrY-0-1000000.png new file mode 100644 index 00000000..185448a3 Binary files /dev/null and b/pygenometracks/tests/test_data/master_bed_shuffle_chrY-0-1000000.png differ diff --git a/pygenometracks/utilities.py b/pygenometracks/utilities.py index 4eb1e1b5..2a829791 100644 --- a/pygenometracks/utilities.py +++ b/pygenometracks/utilities.py @@ -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') @@ -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()