Skip to content

Commit

Permalink
Bug fix: fix bed start and end in bigwig_outlier_bed (galaxyproject#6312
Browse files Browse the repository at this point in the history
)

* fix all beds by padding the region end in the output bed file to suit UCSC math

* update tests with padded bed region ends

* flake8 pacification

* odd. test outputs pass here - will test biocontainers...x

* add 1 to start instead

* hmmm. tests passing here...

* updating tests and test data

* having strange problems getting planemo to update existing but wrong test outputs

* using a specialised bed file -> bigwig

chr1 	0 	10 	100 	100 	+
chr1 	20 	30 	100 	100 	+
chr1 	40 	50 	100 	100 	+
chr1 	60 	70 	100 	100 	+
chr1 	200 	300 	100 	100 	+
chr1 	300 	400 	0 	0 	+
chr1 	400 	1000 	100 	100 	+

* add fakery

* trying again with refreshed test data.
Not sure what's going on...passes here.

* placate flake8

* wrong fake.bed sample...

* fix multiple bigwigs

* more flake8 fixing.

* remove bogus test parameter...

* add nice histogram test

* restore some histogram table reports - just use the old merlin
note the odd offsets. caused me some grief.

* reduce ram bloat by iterating instead of using a list comprehension for large bed texts.
  • Loading branch information
fubar2 authored Sep 15, 2024
1 parent 982fe89 commit 06c54e8
Show file tree
Hide file tree
Showing 18 changed files with 2,471 additions and 4,883 deletions.
13 changes: 7 additions & 6 deletions tools/bigwig_outlier_bed/bigwig_outlier_bed.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def processVals(self, bw, isTop):
else:
bwex = np.r_[False, bw <= self.bwbot, False]
bwexd = np.diff(bwex)
bwexdnz = bwexd.nonzero()[0]
bwexdnz = bwexd.nonzero()[0] # start and end transition of each segment - nice!
bwregions = np.reshape(bwexdnz, (-1, 2))
return bwregions

Expand All @@ -155,10 +155,9 @@ def writeBed(self, bed, bedfname):
potentially multiple
"""
bed.sort()
beds = ["%s\t%d\t%d\t%s\t%d" % x for x in bed]
with open(bedfname, "w") as bedf:
bedf.write("\n".join(beds))
bedf.write("\n")
for b in bed:
bedf.write("%s\t%d\t%d\t%s\t%d\n" % b)

def makeTableRow(self, bw, bwlabel, chr):
"""
Expand Down Expand Up @@ -194,7 +193,6 @@ def makeBed(self):
restab = []
bwlabels = self.bwlabels
bwnames = self.bwnames
bwnames.sort()
reshead = "bigwig\tcontig\tn\tmean\tstd\tmin\tmax\tqtop\tqbot"
for i, bwname in enumerate(bwnames):
bwlabel = bwlabels[i].replace(" ", "")
Expand Down Expand Up @@ -251,8 +249,11 @@ def makeBed(self):
self.bwbot = np.quantile(bw, self.qlo)
bwlo = self.processVals(bw, isTop=False)
for j, seg in enumerate(bwlo):
seglen = seg[1] - seg[0]
if seg[1] - seg[0] >= self.bedwin:
score = -1 * np.sum(bw[seg[0]:seg[1]]) / float(seglen)
score = (
-1 * np.sum(bw[seg[0]:seg[1]]) / float(seglen)
)
bedlo.append(
(
chr,
Expand Down
57 changes: 29 additions & 28 deletions tools/bigwig_outlier_bed/bigwig_outlier_bed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<macros>
<token name="@TOOL_VERSION@">0.2.0</token>
<token name="@PYTHON_VERSION@">3.12.3</token>
<token name="@VERSION_SUFFIX@">0</token>
<token name="@VERSION_SUFFIX@">1</token>
</macros>
<edam_topics>
<edam_topic>topic_0157</edam_topic>
Expand All @@ -25,13 +25,13 @@
</required_files>
<version_command><![CDATA[python -c "import pybigtools; from importlib.metadata import version; print(version('pybigtools'))"]]></version_command>
<command><![CDATA[python '${__tool_directory__}/bigwig_outlier_bed.py'
--bigwig
#for bw in $bigwig:
'$bw'
#set bwns = [x.name for x in $bigwig]
#set bwfs = [x for x in $bigwig]
#for $bwf in $bwfs:
--bigwig '$bwf'
#end for
--bigwiglabels
#for bw in $bigwig:
'$bw.name'
#for $bwn in $bwns:
--bigwiglabels '$bwn'
#end for
--outbeds '$outbeds'
#if $outbeds in ['outhilo', 'outall']:
Expand Down Expand Up @@ -94,29 +94,29 @@
<test expect_num_outputs="1">
<output name="bedouthilo" value="bedouthilo_sample" compare="diff" lines_diff="0"/>
<param name="outbeds" value="outhilo"/>
<param name="bigwig" value="bigwig_sample"/>
<param name="bigwig" value="fake.bigwig"/>
<param name="minwin" value="10"/>
<param name="qhi" value="0.99"/>
<param name="qhi" value="0.6"/>
<param name="qlo" value="0.01"/>
<param name="tableout" value="donotmake"/>
</test>
<test expect_num_outputs="1">
<output name="tableoutfile" value="table_only_sample" compare="diff" lines_diff="0"/>
<param name="outbeds" value="outtab"/>
<param name="bigwig" value="bigwig_sample,1.bigwig"/>
<param name="bigwig" value="fake.bigwig"/>
<param name="minwin" value="10"/>
<param name="qhi" value="0.99"/>
<param name="qlo" value="0.01"/>
<param name="qhi" value="0.7"/>
<param name="qlo" value="0.3"/>
<param name="tableout" value="create"/>
</test>
<test expect_num_outputs="2">
<output name="bedouthilo" value="bedouthilo_sample" compare="diff" lines_diff="0"/>
<output name="bedouthilo" value="bedouthilo_sample_2" compare="diff" lines_diff="0"/>
<output name="tableoutfile" value="table_sample" compare="diff" lines_diff="0"/>
<param name="outbeds" value="outhilo"/>
<param name="bigwig" value="bigwig_sample"/>
<param name="minwin" value="10"/>
<param name="qhi" value="0.99"/>
<param name="qlo" value="0.01"/>
<param name="qhi" value="0.7"/>
<param name="qlo" value="0.3"/>
<param name="tableout" value="create"/>
</test>
<test expect_num_outputs="2">
Expand All @@ -125,7 +125,7 @@
<param name="outbeds" value="outhi"/>
<param name="bigwig" value="bigwig_sample"/>
<param name="minwin" value="10"/>
<param name="qhi" value="0.99"/>
<param name="qhi" value="0.69"/>
<param name="qlo" value=""/>
<param name="tableout" value="create"/>
</test>
Expand All @@ -134,24 +134,25 @@
<output name="bedoutlo" value="bedoutlo_sample" compare="diff" lines_diff="0"/>
<output name="tableoutfile" value="table3_sample" compare="diff" lines_diff="0"/>
<param name="outbeds" value="outlohi"/>
<param name="bigwig" value="bigwig_sample"/>
<param name="minwin" value="1"/>
<param name="qhi" value="0.9"/>
<param name="qlo" value="0.1"/>
<param name="bigwig" value="fake.bigwig"/>
<param name="minwin" value="10"/>
<param name="qhi" value="0.5"/>
<param name="qlo" value="0.5"/>
<param name="tableout" value="create"/>
</test>
<test expect_num_outputs="4">
<output name="bedouthilo" value="bedouthilo2_sample" compare="diff" lines_diff="0"/>
<output name="bedoutlo" value="bedoutlo2_sample" compare="diff" lines_diff="0"/>
<test expect_num_outputs="2">
<output name="bedouthi" value="bedouthi2_sample" compare="diff" lines_diff="0"/>
<output name="tableoutfile" value="table2_sample" compare="diff" lines_diff="0"/>
<param name="outbeds" value="outall"/>
<param name="bigwig" value="bigwig_sample,1.bigwig"/>
<param name="minwin" value="1"/>
<param name="qhi" value="0.9"/>
<param name="qlo" value="0.1"/>
<param name="outbeds" value="outhi"/>
<param name="bigwig" value="fake.bigwig,bigwig_sample"/>
<param name="minwin" value="10"/>
<param name="qhi" value="0.5"/>
<param name="qlo" value="0.5"/>
<param name="tableout" value="create"/>
</test>



</tests>
<help><![CDATA[
Expand Down
Loading

0 comments on commit 06c54e8

Please sign in to comment.