| 1 | /* |
|---|
| 2 | Copyright (c) 2006-2018 Elmar Pruesse <elmar.pruesse@ucdenver.edu> |
|---|
| 3 | |
|---|
| 4 | This file is part of SINA. |
|---|
| 5 | SINA is free software: you can redistribute it and/or modify it under |
|---|
| 6 | the terms of the GNU General Public License as published by the Free |
|---|
| 7 | Software Foundation, either version 3 of the License, or (at your |
|---|
| 8 | option) any later version. |
|---|
| 9 | |
|---|
| 10 | SINA is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 11 | WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|---|
| 12 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|---|
| 13 | for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU General Public License |
|---|
| 16 | along with SINA. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 17 | |
|---|
| 18 | Additional permission under GNU GPL version 3 section 7 |
|---|
| 19 | |
|---|
| 20 | If you modify SINA, or any covered work, by linking or combining it |
|---|
| 21 | with components of ARB (or a modified version of that software), |
|---|
| 22 | containing parts covered by the terms of the |
|---|
| 23 | ARB-public-library-license, the licensors of SINA grant you additional |
|---|
| 24 | permission to convey the resulting work. Corresponding Source for a |
|---|
| 25 | non-source form of such a combination shall include the source code |
|---|
| 26 | for the parts of ARB used as well as that of the covered work. |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | #include "../query_arb.h" |
|---|
| 30 | #include "../tempfile.h" |
|---|
| 31 | #include "../log.h" |
|---|
| 32 | |
|---|
| 33 | // ARB needs either DEBUG or NDEBUG defined |
|---|
| 34 | #ifdef DEBUG |
|---|
| 35 | # define typeof __typeof__ |
|---|
| 36 | #else |
|---|
| 37 | # ifndef NDEBUG |
|---|
| 38 | # define NDEBUG 1 |
|---|
| 39 | # endif |
|---|
| 40 | #endif |
|---|
| 41 | |
|---|
| 42 | #include <arbdbt.h> |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | #define BOOST_TEST_MODULE query_arb_test |
|---|
| 46 | |
|---|
| 47 | #include <boost/test/unit_test.hpp> |
|---|
| 48 | namespace utf = boost::unit_test; |
|---|
| 49 | #include <boost/test/data/test_case.hpp> |
|---|
| 50 | namespace bdata = utf::data; |
|---|
| 51 | #include <boost/filesystem.hpp> |
|---|
| 52 | namespace fs = boost::filesystem; |
|---|
| 53 | |
|---|
| 54 | #include <spdlog/sinks/ostream_sink.h> |
|---|
| 55 | |
|---|
| 56 | using namespace sina; |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | struct GlobalFixture { |
|---|
| 60 | static GlobalFixture*& instance() { |
|---|
| 61 | static GlobalFixture* inst{nullptr}; |
|---|
| 62 | return inst; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | GlobalFixture() { |
|---|
| 66 | instance() = this; |
|---|
| 67 | |
|---|
| 68 | int argc = boost::unit_test::framework::master_test_suite().argc; |
|---|
| 69 | if (argc != 2) { |
|---|
| 70 | throw std::runtime_error("Need exactly 1 argument: the reference arb file"); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | char** argv = boost::unit_test::framework::master_test_suite().argv; |
|---|
| 74 | query_arb* ref = query_arb::getARBDB(argv[1]); |
|---|
| 75 | query_arb* tmp = query_arb::getARBDB(small_arb_path()); |
|---|
| 76 | |
|---|
| 77 | _ids = ref->getSequenceNames(); |
|---|
| 78 | std::sort(_ids.begin(), _ids.end()); |
|---|
| 79 | std::srand(1234); |
|---|
| 80 | std::random_shuffle(_ids.begin(), _ids.end()); |
|---|
| 81 | _ids.resize(n_seq); |
|---|
| 82 | _ids[0] = "MyoSeoul"; |
|---|
| 83 | |
|---|
| 84 | for (auto id : _ids) { |
|---|
| 85 | tmp->copySequence(*ref, id, false); |
|---|
| 86 | } |
|---|
| 87 | tmp->save(); |
|---|
| 88 | query_arb::closeOpenARBDBs(); |
|---|
| 89 | } |
|---|
| 90 | ~GlobalFixture() { |
|---|
| 91 | instance() = nullptr; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | const TempFile& small_arb_path() const { |
|---|
| 95 | return _small_arb_path; |
|---|
| 96 | } |
|---|
| 97 | const std::vector<std::string>& ids() const { |
|---|
| 98 | return _ids; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | TempFile _small_arb_path; |
|---|
| 102 | std::vector<std::string> _ids; |
|---|
| 103 | const size_t n_seq{50}; |
|---|
| 104 | }; |
|---|
| 105 | |
|---|
| 106 | struct CaseFixture { |
|---|
| 107 | CaseFixture() {} |
|---|
| 108 | ~CaseFixture() { |
|---|
| 109 | query_arb::closeOpenARBDBs(); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | const GlobalFixture& globalFixture() const { |
|---|
| 113 | return *GlobalFixture::instance(); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | query_arb* tmparb() { |
|---|
| 117 | if (_tmparb == nullptr) { |
|---|
| 118 | _tmparb = query_arb::getARBDB(tmpfile); |
|---|
| 119 | } |
|---|
| 120 | return _tmparb; |
|---|
| 121 | } |
|---|
| 122 | query_arb* smallarb() { |
|---|
| 123 | if (_smallarb == nullptr) { |
|---|
| 124 | _smallarb = query_arb::getARBDB(globalFixture().small_arb_path()); |
|---|
| 125 | } |
|---|
| 126 | return _smallarb; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | const std::vector<std::string>& ids() const { |
|---|
| 130 | return globalFixture().ids(); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | TempFile tmpfile; |
|---|
| 134 | query_arb *_tmparb{nullptr}, *_smallarb{nullptr}; |
|---|
| 135 | }; |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | struct what_starts_with { |
|---|
| 139 | explicit what_starts_with(const std::string& prefix) : _prefix(prefix) {} |
|---|
| 140 | bool operator()(const std::exception& e) const { |
|---|
| 141 | return std::string(e.what()).rfind(_prefix, 0) == 0; |
|---|
| 142 | } |
|---|
| 143 | std::string _prefix; |
|---|
| 144 | }; |
|---|
| 145 | |
|---|
| 146 | BOOST_GLOBAL_FIXTURE(GlobalFixture); |
|---|
| 147 | |
|---|
| 148 | BOOST_FIXTURE_TEST_SUITE(query_arb_test, CaseFixture); |
|---|
| 149 | |
|---|
| 150 | BOOST_AUTO_TEST_CASE(new_file) { |
|---|
| 151 | query_arb* arb = tmparb(); |
|---|
| 152 | arb->save(); |
|---|
| 153 | fs::exists(tmpfile); |
|---|
| 154 | query_arb::closeOpenARBDBs(); |
|---|
| 155 | fs::remove(tmpfile); |
|---|
| 156 | arb = query_arb::getARBDB(tmpfile); |
|---|
| 157 | arb->save(); |
|---|
| 158 | fs::exists(tmpfile); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | BOOST_AUTO_TEST_CASE(cache) { |
|---|
| 163 | query_arb *arb = smallarb(); |
|---|
| 164 | arb->loadCache(); |
|---|
| 165 | std::vector<cseq*> cache = arb->getCacheContents(); |
|---|
| 166 | BOOST_CHECK_EQUAL(cache.size(), globalFixture().n_seq); |
|---|
| 167 | |
|---|
| 168 | for (auto* c: cache) { |
|---|
| 169 | const cseq& d = arb->getCseq(c->getName()); |
|---|
| 170 | BOOST_CHECK_EQUAL(c, &d); |
|---|
| 171 | } |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | BOOST_AUTO_TEST_CASE(empty_filename) { |
|---|
| 175 | BOOST_CHECK_EXCEPTION( |
|---|
| 176 | query_arb::getARBDB(""), |
|---|
| 177 | query_arb_exception, |
|---|
| 178 | what_starts_with("Empty ARB database") |
|---|
| 179 | ); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | BOOST_AUTO_TEST_CASE(unable_to_open) { |
|---|
| 183 | TempFile notAnArbFile; |
|---|
| 184 | { |
|---|
| 185 | boost::filesystem::ofstream out(notAnArbFile); |
|---|
| 186 | out << "Nothing" << std::endl; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | BOOST_CHECK_EXCEPTION( |
|---|
| 190 | query_arb::getARBDB(notAnArbFile), |
|---|
| 191 | query_arb_exception, |
|---|
| 192 | what_starts_with("Unable to open") |
|---|
| 193 | ); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | BOOST_AUTO_TEST_CASE(unable_to_save) { |
|---|
| 197 | query_arb *arb = tmparb(); |
|---|
| 198 | TempFile write_protected; |
|---|
| 199 | { |
|---|
| 200 | boost::filesystem::ofstream out(write_protected); |
|---|
| 201 | } |
|---|
| 202 | boost::filesystem::permissions( |
|---|
| 203 | write_protected, |
|---|
| 204 | boost::filesystem::owner_read); |
|---|
| 205 | |
|---|
| 206 | std::ostringstream oss; |
|---|
| 207 | auto ostream_sink = std::make_shared<spdlog::sinks::ostream_sink_mt> (oss); |
|---|
| 208 | ostream_sink->set_level(spdlog::level::err); |
|---|
| 209 | Log::add_sink(ostream_sink); |
|---|
| 210 | arb->saveAs(write_protected); |
|---|
| 211 | Log::remove_sink(ostream_sink); |
|---|
| 212 | |
|---|
| 213 | BOOST_CHECK(oss.str().find("Error while trying to save") != std::string::npos); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | BOOST_AUTO_TEST_CASE(getFileName) { |
|---|
| 217 | query_arb *arb = smallarb(); |
|---|
| 218 | BOOST_CHECK_EQUAL(globalFixture().small_arb_path(), arb->getFileName()); |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | BOOST_AUTO_TEST_CASE(getAlignmentWidth) { |
|---|
| 222 | query_arb *arb = smallarb(); |
|---|
| 223 | BOOST_CHECK_EQUAL(50000, arb->getAlignmentWidth()); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | BOOST_AUTO_TEST_CASE(getSeqCount) { |
|---|
| 227 | query_arb *arb = smallarb(); |
|---|
| 228 | BOOST_CHECK_EQUAL(globalFixture().n_seq, arb->getSeqCount()); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | BOOST_AUTO_TEST_CASE(read_fail) { |
|---|
| 232 | query_arb *arb = smallarb(); |
|---|
| 233 | BOOST_CHECK_EXCEPTION( |
|---|
| 234 | arb->getCseq("doesnotexist"), |
|---|
| 235 | query_arb_exception, |
|---|
| 236 | what_starts_with("No sequence \"doesnotexist") |
|---|
| 237 | ); |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | BOOST_AUTO_TEST_CASE(empty_alignment) { |
|---|
| 241 | GBDATA* gbmain = GB_open(globalFixture().small_arb_path().path().native().c_str(), "rw"); |
|---|
| 242 | { |
|---|
| 243 | GB_transaction trans(gbmain); |
|---|
| 244 | GBDATA* gbspecs = GB_search(gbmain, "species_data", GB_CREATE_CONTAINER); |
|---|
| 245 | GBDATA* gbspec = GB_create_container(gbspecs, "species"); |
|---|
| 246 | GBDATA* gbname = GB_create(gbspec, "name", GB_STRING); |
|---|
| 247 | GB_write_string(gbname, "emptyspecies"); |
|---|
| 248 | } |
|---|
| 249 | GB_save_as(gbmain, tmpfile.path().native().c_str(), "b"); |
|---|
| 250 | GB_close(gbmain); |
|---|
| 251 | query_arb *arb = tmparb(); |
|---|
| 252 | |
|---|
| 253 | BOOST_CHECK_EXCEPTION( |
|---|
| 254 | arb->getCseq("emptyspecies"), |
|---|
| 255 | query_arb_exception, |
|---|
| 256 | what_starts_with("No alignment for sequence \"emptyspecies") |
|---|
| 257 | ); |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | BOOST_AUTO_TEST_CASE(read_write_ok) { |
|---|
| 262 | query_arb *arb = smallarb(); |
|---|
| 263 | const cseq &c = arb->getCseq(ids()[0]); |
|---|
| 264 | cseq d = c; |
|---|
| 265 | d.setName("new_sequence"); |
|---|
| 266 | arb->putCseq(c); |
|---|
| 267 | BOOST_CHECK_EQUAL(globalFixture().n_seq, arb->getSeqCount()); |
|---|
| 268 | arb->putCseq(d); |
|---|
| 269 | BOOST_CHECK_EQUAL(globalFixture().n_seq + 1, arb->getSeqCount()); |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | BOOST_AUTO_TEST_CASE(read_acc) { |
|---|
| 273 | query_arb *arb = smallarb(); |
|---|
| 274 | const cseq &c = arb->getCseq(ids()[0]); |
|---|
| 275 | BOOST_CHECK_EQUAL(c.get_attr<std::string>("acc", "none"), "none"); |
|---|
| 276 | arb->loadKey(c, "acc"); |
|---|
| 277 | BOOST_CHECK_EQUAL(c.get_attr<std::string>("acc"), "DQ536403"); |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | BOOST_AUTO_TEST_CASE(write_acc) { |
|---|
| 281 | query_arb *arb = smallarb(); |
|---|
| 282 | const cseq &c = arb->getCseq(ids()[0]); |
|---|
| 283 | cseq d = c; |
|---|
| 284 | cseq e = c; |
|---|
| 285 | |
|---|
| 286 | d.set_attr("acc", "12345678"); |
|---|
| 287 | arb->putCseq(d); |
|---|
| 288 | arb->loadKey(e, "acc"); |
|---|
| 289 | BOOST_CHECK_EQUAL(e.get_attr<std::string>("acc"), "12345678"); |
|---|
| 290 | |
|---|
| 291 | d.set_attr("acc", "ABCDE"); |
|---|
| 292 | arb->storeKey(d, "acc"); |
|---|
| 293 | arb->loadKey(e, "acc", true); |
|---|
| 294 | BOOST_CHECK_EQUAL(e.get_attr<std::string>("acc"), "ABCDE"); |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | BOOST_AUTO_TEST_CASE(write_int) { |
|---|
| 298 | query_arb *arb = smallarb(); |
|---|
| 299 | const cseq &c = arb->getCseq(ids()[0]); |
|---|
| 300 | cseq d = c; |
|---|
| 301 | cseq e = c; |
|---|
| 302 | |
|---|
| 303 | d.set_attr("acc", 123); |
|---|
| 304 | arb->storeKey(d, "acc"); |
|---|
| 305 | arb->loadKey(e, "acc", true); |
|---|
| 306 | BOOST_CHECK_EQUAL(e.get_attr<int>("acc"), 123); |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | BOOST_AUTO_TEST_CASE(write_float) { |
|---|
| 310 | query_arb *arb = smallarb(); |
|---|
| 311 | const cseq &c = arb->getCseq(ids()[0]); |
|---|
| 312 | cseq d = c; |
|---|
| 313 | cseq e = c; |
|---|
| 314 | |
|---|
| 315 | d.set_attr("acc", 2.3f); |
|---|
| 316 | arb->storeKey(d, "acc"); |
|---|
| 317 | arb->loadKey(e, "acc", true); |
|---|
| 318 | BOOST_CHECK_EQUAL(e.get_attr<float>("acc"), 2.3f); |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | /* |
|---|
| 322 | BOOST_AUTO_TEST_CASE(open_pt_db) |
|---|
| 323 | BOOST_AUTO_TEST_CASE(arb_status) |
|---|
| 324 | BOOST_AUTO_TEST_CASE(arb_error) |
|---|
| 325 | */ |
|---|
| 326 | |
|---|
| 327 | BOOST_AUTO_TEST_SUITE_END(); // rw_csv_test |
|---|
| 328 | |
|---|
| 329 | /* |
|---|
| 330 | Local Variables: |
|---|
| 331 | mode:c++ |
|---|
| 332 | c-file-style:"stroustrup" |
|---|
| 333 | c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) |
|---|
| 334 | indent-tabs-mode:nil |
|---|
| 335 | fill-column:99 |
|---|
| 336 | End: |
|---|
| 337 | */ |
|---|
| 338 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : |
|---|