Skip to content

Commit 15445ae

Browse files
Add second integration test and fix bugs discovered
1 parent f228de0 commit 15445ae

4 files changed

Lines changed: 143 additions & 27 deletions

File tree

sofos.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,10 @@ class SofosHistogram {
6565
void ZeroCounts();
6666

6767
size_t num_rows() const {
68-
assert(prior_.size() == size_ + 1);
69-
assert(observed_.size() == size_ + 1);
70-
assert(posterior_.size() == size_ + 1);
68+
assert(prior_.size() == posterior_.size());
69+
assert(observed_.size() == posterior_.size());
7170

72-
return size_ + 1;
71+
return posterior_.size();
7372
}
7473

7574
std::array<double, 3> row(size_t i) const {

test/run_tests.bash

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/bash
22

3-
SOFOS=$1
3+
SOFOS="${1:-../sofos}"
44

5-
bash test-01.bash "${SOFOS}"
5+
bash test-01.bash "${SOFOS}" || exit 1
6+
7+
bash test-02.bash "${SOFOS}" || exit 1

test/test-02.bash

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/bin/bash
2+
#
3+
# library(rmutil)
4+
# f = function(n,a,b) dbetabinom(0:n,n,a/(a+b),a+b)
5+
6+
echo "Running Integration Test: folded+refalt ..."
7+
8+
SOFOS="${1:-../sofos}"
9+
VCF=test-default.vcf
10+
11+
output=$(${SOFOS} -q -a 0.1 -b 1 -n 10 -f -r ${VCF})
12+
13+
hnum=$(echo "${output}" | grep '^#' | wc -l)
14+
if [[ 10 -ne "${hnum}" ]]; then
15+
echo " ERROR: Heading has ${hnum} lines instead of 10."
16+
exit 1
17+
fi
18+
header=$(echo "${output}" | awk 'NR==11')
19+
if [[ $header != "Number,Prior,Observed,Posterior" ]]; then
20+
echo " ERROR: Header has wrong column names."
21+
exit 1
22+
fi
23+
24+
nrow=$(echo "${output}" | grep -v '^#' | awk -F, 'END {print NR}')
25+
if [[ 7 -ne "${nrow}" ]]; then
26+
echo " ERROR: Body has wrong number of rows."
27+
exit 1
28+
fi
29+
30+
# Check posterior
31+
expected="0.613042662838015
32+
0.481731622579739
33+
0.354864195501706
34+
0.259140144535515
35+
0.200677862510758
36+
0.0905435120342663"
37+
38+
echo "${output}" | grep -v '^#' | awk -F, 'NR>1 {print $4}' \
39+
| paste -d, - <(echo "${expected}") \
40+
| awk -F, -v m=1e-7 'function abs(v) {return v < 0 ? -v : v} {a=abs(($1-$2))/$2; if(a > m){m = a} } END{exit (m > 1e-7) }'
41+
if [[ $? -ne 0 ]]; then
42+
echo " ERROR: Maximum relative error in posterior exceeds 1e-7."
43+
exit 1
44+
fi
45+
46+
# Check prior
47+
expected="1.52304554473485
48+
0.172084774253705
49+
0.106856638049549
50+
0.0851179668121595
51+
0.076115591667104
52+
0.0367794844826313"
53+
54+
echo "${output}" | grep -v '^#' | awk -F, 'NR>1 {print $2}' \
55+
| paste -d, - <(echo "${expected}") \
56+
| awk -F, -v m=1e-7 'function abs(v) {return v < 0 ? -v : v} {a=abs(($1-$2))/$2; if(a > m){m = a} } END{exit (m > 1e-7) }'
57+
if [[ $? -ne 0 ]]; then
58+
echo " ERROR: Maximum relative error in prior exceeds 1e-7."
59+
exit 1
60+
fi
61+
62+
# Check obs
63+
expected="0
64+
0
65+
2
66+
0
67+
0
68+
0
69+
"
70+
71+
echo "${output}" | grep -v '^#' | awk -F, 'NR>1 {print $3}' \
72+
| paste -d, - <(echo "${expected}") \
73+
| awk -F, -v m=1e-7 'function abs(v) {return v < 0 ? -v : v} {a=abs(($1-$2)); if(a > m){m = a} } END{exit (m > 1e-7) }'
74+
if [[ $? -ne 0 ]]; then
75+
echo " ERROR: Maximum error in obs exceeds 1e-7."
76+
exit 1
77+
fi
78+
79+
# Check indexes
80+
expected="0
81+
1
82+
2
83+
3
84+
4
85+
5"
86+
87+
echo "${output}" | grep -v '^#' | awk -F, 'NR>1 {print $1}' \
88+
| paste -d, - <(echo "${expected}") \
89+
| awk -F, -v m=1e-7 'function abs(v) {return v < 0 ? -v : v} {a=abs(($1-$2)); if(a > m){m = a} } END{exit (m > 1e-7) }'
90+
if [[ $? -ne 0 ]]; then
91+
echo " ERROR: Maximum error in obs exceeds 1e-7."
92+
exit 1
93+
fi

unittest.cc

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -790,30 +790,52 @@ TEST_CASE("output_header generates a header containing program information") {
790790
TEST_CASE("output_body generates a csv table containing column names and values") {
791791
sofos_params_t params;
792792
params.size = 4;
793-
Sofos sofos{params};
794-
std::stringstream str;
795-
output_body(str, sofos);
796793

797-
std::vector<std::vector<std::string>> table;
798-
std::string line;
799-
while(std::getline(str, line, '\n')) {
800-
std::stringstream row;
801-
row.str(line);
802-
std::string token;
803-
std::vector<std::string> tokens;
804-
while(std::getline(row, token, ',')) {
805-
tokens.push_back(token);
794+
std::stringstream str;
795+
auto parse_table = [&]() -> std::vector<std::vector<std::string>> {
796+
std::vector<std::vector<std::string>> table;
797+
std::string line;
798+
while(std::getline(str, line, '\n')) {
799+
std::stringstream row;
800+
row.str(line);
801+
std::string token;
802+
std::vector<std::string> tokens;
803+
while(std::getline(row, token, ',')) {
804+
tokens.push_back(token);
805+
}
806+
table.push_back(tokens);
806807
}
807-
table.push_back(tokens);
808-
}
808+
return table;
809+
};
809810

810-
REQUIRE(table.size() == 6);
811-
CHECK(table[0].size() == 4);
812-
CHECK(table[1].size() == 4);
813-
CHECK(table[2].size() == 4);
814-
CHECK(table[3].size() == 4);
815-
CHECK(table[4].size() == 4);
816-
CHECK(table[5].size() == 4);
811+
SECTION("when histogram is unfolded"){
812+
params.flag_folded = false;
813+
Sofos sofos{params};
814+
sofos.FinishHistogram();
815+
output_body(str, sofos);
816+
auto table = parse_table();
817+
818+
REQUIRE(table.size() == 6);
819+
CHECK(table[0].size() == 4);
820+
CHECK(table[1].size() == 4);
821+
CHECK(table[2].size() == 4);
822+
CHECK(table[3].size() == 4);
823+
CHECK(table[4].size() == 4);
824+
CHECK(table[5].size() == 4);
825+
}
826+
SECTION("when histogram is folded"){
827+
params.flag_folded = true;
828+
Sofos sofos{params};
829+
sofos.FinishHistogram();
830+
output_body(str, sofos);
831+
auto table = parse_table();
832+
833+
REQUIRE(table.size() == 4);
834+
CHECK(table[0].size() == 4);
835+
CHECK(table[1].size() == 4);
836+
CHECK(table[2].size() == 4);
837+
CHECK(table[3].size() == 4);
838+
}
817839
}
818840

819841
TEST_CASE("Retrieving values from a VCF file") {

0 commit comments

Comments
 (0)