Skip to content

Commit

Permalink
bbi.fetch with summary="sum" (#12)
Browse files Browse the repository at this point in the history
* Sum aggregation mode

* Missed a line

* Test

* Use higher zoom level in test

* Actually do sum in test

* Improve test

* Whitespace
  • Loading branch information
keller-mark authored Jun 12, 2020
1 parent 6064b91 commit 0c3ebbf
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bbi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
__all__ = ['is_bbi', 'is_bigwig', 'is_bigbed', 'info', 'zooms', 'chromsizes',
'fetch', 'stackup', 'fetch_intervals']

__version__ = '0.2.2'
__version__ = '0.2.3'
1 change: 1 addition & 0 deletions bbi/cbbi.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ cdef extern from "bbiFile.h":
bbiSumMin = 2
bbiSumCoverage = 3
bbiSumStandardDeviation = 4
bbiSumSum = 5

cdef struct bbiSummary:
bbiSummary *next
Expand Down
4 changes: 4 additions & 0 deletions bbi/cbbi.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ cpdef dict BBI_SUMMARY_TYPES = {
'min': bbiSumMin,
'cov': bbiSumCoverage,
'std': bbiSumStandardDeviation,
'sum': bbiSumSum,
}


Expand Down Expand Up @@ -254,6 +255,7 @@ def info(str inFile):
'std': sqrt(var_from_sums(summ.sumData,
summ.sumSquares,
summ.validCount)),
'sum': summ.sumData,
}
}
bbiFileClose(&bbi)
Expand Down Expand Up @@ -731,6 +733,8 @@ cdef inline void array_query_summarized(
val = sqrt(var_from_sums(el.sumData,
el.sumSquares,
el.validCount))
elif summaryType == bbiSumSum:
val = el.sumData
else:
raise RuntimeError
out[i] = val
Expand Down
1 change: 1 addition & 0 deletions include/bbiFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ enum bbiSummaryType
bbiSumMin = 2, /* Minimum value */
bbiSumCoverage = 3, /* Bases in region containing actual data. */
bbiSumStandardDeviation = 4, /* Standard deviation in window. */
bbiSumSum = 5, /* Sum value */
};

enum bbiSummaryType bbiSummaryTypeFromString(char *string);
Expand Down
7 changes: 7 additions & 0 deletions src/bbiRead.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ else if (sameWord(string, "coverage") || sameWord(string, "dataCoverage"))
return bbiSumCoverage;
else if (sameWord(string, "std"))
return bbiSumStandardDeviation;
else if (sameWord(string, "sum"))
return bbiSumSum;
else
{
errAbort("Unknown bbiSummaryType %s", string);
Expand All @@ -302,6 +304,8 @@ switch (type)
return "coverage";
case bbiSumStandardDeviation:
return "std";
case bbiSumSum:
return "sum";
default:
errAbort("Unknown bbiSummaryType %d", (int)type);
return NULL;
Expand Down Expand Up @@ -691,6 +695,9 @@ if (ret)
case bbiSumStandardDeviation:
val = calcStdFromSums(el->sumData, el->sumSquares, el->validCount);
break;
case bbiSumSum:
val = el->sumData;
break;
default:
internalErr();
val = 0.0;
Expand Down
4 changes: 4 additions & 0 deletions tests/test_bbi.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ def test_fetch_summary_stats(path):
path, 'chr21', 20000000, 20001000, bins=10, summary='max'
).max()
assert np.isclose(vmax, np.max(values))
vsum = bbi.fetch(path, 'chr21', 20000000, 20001000, bins=100, summary='sum')
values_sum_every_ten = np.reshape(values, (-1, 10)).sum(axis=-1)
assert len(vsum) == len(values_sum_every_ten)
assert np.allclose(vsum, values_sum_every_ten)

with pytest.raises(ValueError):
bbi.fetch(path, 'chr21', 20000000, 20001000, bins=10, summary='foo')
Expand Down

0 comments on commit 0c3ebbf

Please sign in to comment.