Skip to content

Commit

Permalink
Merge pull request #279 from lldelisle/bedgraphAsBigWigUntilNARegion
Browse files Browse the repository at this point in the history
modify get_scores to reach end_region
  • Loading branch information
lldelisle authored Oct 7, 2020
2 parents 536c485 + 657f645 commit ba4d45a
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions pygenometracks/tests/generateAllOutput.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ bin/pgt --tracks ./pygenometracks/tests/test_data/bedgraph_useMid.ini --region c
bin/pgt --tracks ./pygenometracks/tests/test_data/operation_bdg.ini --region X:2700000-3100000 --trackLabelFraction 0.2 --dpi 130 -o ./pygenometracks/tests/test_data/master_operation_bdg.png
bin/pgt --tracks ./pygenometracks/tests/test_data/bedgraph_withNA.ini --region X:2700000-3100000 --trackLabelFraction 0.2 --dpi 130 -o ./pygenometracks/tests/test_data/master_bedgraph_withNA.png
bin/pgt --tracks ./pygenometracks/tests/test_data/bedgraph_negative.ini --region X:2700000-3100000 --trackLabelFraction 0.2 --dpi 130 -o ./pygenometracks/tests/test_data/master_negative.png
bin/pgt --tracks ./pygenometracks/tests/test_data/bedgraph_end_not_covered.ini --region chr7:100-400 --trackLabelFraction 0.2 --dpi 130 -o ./pygenometracks/tests/test_data/master_bedgraph_end_not_covered.png

# test bigWigTrack:
bin/pgt --tracks ./pygenometracks/tests/test_data/bigwig.ini --region X:2700000-3100000 --trackLabelFraction 0.2 --dpi 130 -o ./pygenometracks/tests/test_data/master_bigwig.png
Expand Down
28 changes: 28 additions & 0 deletions pygenometracks/tests/test_bedGraphTrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@
with open(os.path.join(ROOT, "log1pm_bedgraph.ini"), 'w') as fh:
fh.write(log1p_with_neg)


bedgraph_end_not_covered = """
[bedgraph]
file = simple.bdg
height = 3
summary_method = max
[x-axis]
"""
with open(os.path.join(ROOT, "bedgraph_end_not_covered.ini"), 'w') as fh:
fh.write(bedgraph_end_not_covered)

tolerance = 13 # default matplotlib pixed difference tolerance


Expand Down Expand Up @@ -427,3 +439,19 @@ def test_bedgraph_neg_log1p():

os.remove(ini_file)
os.remove(os.path.join(ROOT, "bedgraph_chrx_2e6_5e6_m.bg"))


def test_bedgraph_end_not_covered():
region = "chr7:100-400"
outfile = NamedTemporaryFile(suffix='.png', prefix='bedgraph_end_not_covered_', delete=False)
args = "--tracks {ini} --region {region} --trackLabelFraction 0.2 " \
"--dpi 130 --outFileName {outfile}" \
"".format(ini=os.path.join(ROOT, "bedgraph_end_not_covered.ini"),
outfile=outfile.name, region=region).split()
pygenometracks.plotTracks.main(args)
print("saving test to {}".format(outfile.name))
res = compare_images(os.path.join(ROOT, 'master_bedgraph_end_not_covered.png'),
outfile.name, tolerance)
assert res is None, res

os.remove(outfile.name)
7 changes: 7 additions & 0 deletions pygenometracks/tests/test_data/bedgraph_end_not_covered.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

[bedgraph]
file = simple.bdg
height = 3
summary_method = max

[x-axis]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions pygenometracks/tests/test_data/simple.bdg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
track type=bedGraph name="400-404notIncluded"
chr7 100 200 1
chr7 200 300 2
chr7 300 350 3
chr7 350 399 4
chr7 405 450 5
9 changes: 7 additions & 2 deletions pygenometracks/tracks/BedGraphTrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,15 @@ def get_scores(self, chrom_region, start_region, end_region,
return_nans=True, tbx_var='self.tbx', inttree_var='self.interval_tree'):
"""
Retrieves the score (or scores or whatever fields are in a bedgraph like file) and the positions
for a given region.
for a given region. If return_nans is True the pos_list goes until at least end_region.
In case there is no item in the region. It returns [], []
Args:
chrom_region:
start_region:
end_region:
Returns:
tuple:
scores_list, post_list
scores_list, pos_list
"""
score_list = []
pos_list = []
Expand Down Expand Up @@ -288,6 +288,11 @@ def get_scores(self, chrom_region, start_region, end_region,
score_list.append(values)
pos_list.append((start, end))

# Add a last value if needed:
if prev_end < end_region and return_nans:
score_list.append(np.repeat(np.nan, self.num_fields))
pos_list.append((prev_end, end_region))

return score_list, pos_list

def plot(self, ax, chrom_region, start_region, end_region):
Expand Down

0 comments on commit ba4d45a

Please sign in to comment.