| 1 | // ========================================================= // |
|---|
| 2 | // // |
|---|
| 3 | // File : calculator.cxx // |
|---|
| 4 | // Purpose : perform SAI calculation // |
|---|
| 5 | // // |
|---|
| 6 | // Coded by Ralf Westram (coder@reallysoft.de) in Dec 19 // |
|---|
| 7 | // http://www.arb-home.de/ // |
|---|
| 8 | // // |
|---|
| 9 | // ========================================================= // |
|---|
| 10 | |
|---|
| 11 | #include "calculator.h" |
|---|
| 12 | #include <arbdbt.h> |
|---|
| 13 | |
|---|
| 14 | using namespace std; |
|---|
| 15 | |
|---|
| 16 | class SequenceHandler : virtual Noncopyable { // @@@ class SequenceHandler should be used wherever NOT_ALL_SAI_HAVE_DATA |
|---|
| 17 | GBDATA *gb_item; // item (e.g. SAI, species, ...) |
|---|
| 18 | char *ali; // alignment name |
|---|
| 19 | |
|---|
| 20 | GBDATA *gb_data; // data element containing sequence (e.g. 'ali_xxx/data') |
|---|
| 21 | GB_ERROR error; |
|---|
| 22 | bool createPossible; |
|---|
| 23 | |
|---|
| 24 | mutable SmartCharPtr buffer; // holds data (if GB_read_as_string has been used) |
|---|
| 25 | |
|---|
| 26 | void annotate_error() { |
|---|
| 27 | sai_assert(error); |
|---|
| 28 | char *fieldDescription = NULp; |
|---|
| 29 | if (gb_data) { |
|---|
| 30 | fieldDescription = GBS_global_string_copy("; field '%s'", GB_read_key_pntr(gb_data)); |
|---|
| 31 | } |
|---|
| 32 | error = GBS_global_string("%s (at %s '%s'; alignment '%s'%s)", |
|---|
| 33 | error, |
|---|
| 34 | GB_read_key_pntr(gb_item), |
|---|
| 35 | GBT_get_name_or_description(gb_item), |
|---|
| 36 | ali, |
|---|
| 37 | fieldDescription ? fieldDescription : ""); |
|---|
| 38 | free(fieldDescription); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | public: |
|---|
| 42 | SequenceHandler(GBDATA *gb_item_, const char *ali_) : |
|---|
| 43 | gb_item(gb_item_), |
|---|
| 44 | ali(strdup(ali_)), |
|---|
| 45 | gb_data(NULp), |
|---|
| 46 | error(NULp), |
|---|
| 47 | createPossible(true) |
|---|
| 48 | { |
|---|
| 49 | GBDATA *gb_ali = GB_entry(gb_item, ali); |
|---|
| 50 | if (!gb_ali) { |
|---|
| 51 | error = "has no entries"; |
|---|
| 52 | } |
|---|
| 53 | else { |
|---|
| 54 | gb_data = GB_entry(gb_ali, "data"); // try standard sequence entry first. |
|---|
| 55 | if (!gb_data && !GB_have_error()) { |
|---|
| 56 | if (strcmp(GB_read_key_pntr(gb_item), "extended") == 0) { |
|---|
| 57 | // these entries are only tried for SAI: |
|---|
| 58 | gb_data = GB_entry(gb_ali, "bits"); // e.g. occurs in 'markerline' |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | if (!gb_data) { |
|---|
| 63 | error = GB_have_error() |
|---|
| 64 | ? GB_await_error() |
|---|
| 65 | : "could not detect sequence data"; |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | if (!error) { |
|---|
| 70 | GB_TYPES type = GB_read_type(gb_data); |
|---|
| 71 | if (type != GB_STRING && type != GB_BITS) { |
|---|
| 72 | error = "cannot handle type as sequence- or associated-data"; |
|---|
| 73 | createPossible = false; |
|---|
| 74 | gb_data = NULp; // => !wasFound() |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | if (error) { |
|---|
| 79 | annotate_error(); |
|---|
| 80 | } |
|---|
| 81 | else { |
|---|
| 82 | sai_assert(wasFound()); |
|---|
| 83 | createPossible = false; |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | ~SequenceHandler() { free(ali); } |
|---|
| 87 | |
|---|
| 88 | GB_ERROR getError() const { |
|---|
| 89 | // used to retrieve error (after call to ctor) => cannot read data |
|---|
| 90 | // Nevertheless calling create() and then writing data may be a valid option! |
|---|
| 91 | return error; |
|---|
| 92 | } |
|---|
| 93 | bool wasFound() const { return gb_data != NULp; } // Note: "wasFound() && getError()" may occur! |
|---|
| 94 | |
|---|
| 95 | const char *read_char_pntr() const { |
|---|
| 96 | sai_assert(wasFound()); |
|---|
| 97 | GB_TYPES type = GB_read_type(gb_data); |
|---|
| 98 | if (type == GB_STRING) { |
|---|
| 99 | return GB_read_char_pntr(gb_data); |
|---|
| 100 | } |
|---|
| 101 | buffer.assign(GB_read_as_string(gb_data)); |
|---|
| 102 | return buffer.content(); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | char *read_string() const { |
|---|
| 106 | sai_assert(wasFound()); |
|---|
| 107 | return GB_read_as_string(gb_data); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | GB_ERROR write_string(const char *data) const { |
|---|
| 111 | sai_assert(wasFound()); |
|---|
| 112 | return GB_write_autoconv_string(gb_data, data); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | GB_ERROR create(GB_TYPES type, const char *fieldName) { |
|---|
| 116 | bool annotateError = true; |
|---|
| 117 | |
|---|
| 118 | if (createPossible) { |
|---|
| 119 | sai_assert(!wasFound()); // already have sequence |
|---|
| 120 | error = NULp; // superseeds any errors from ctor |
|---|
| 121 | gb_data = GBT_create_sequence_data(gb_item, ali, fieldName, type, 0); |
|---|
| 122 | if (!gb_data) error = GB_await_error(); |
|---|
| 123 | } |
|---|
| 124 | else { |
|---|
| 125 | if (getError()) { |
|---|
| 126 | annotateError = false; // keep existing and already annotated error |
|---|
| 127 | } |
|---|
| 128 | else if (wasFound()) { |
|---|
| 129 | error = "cannot create data over existing"; |
|---|
| 130 | } |
|---|
| 131 | else { |
|---|
| 132 | sai_assert(0); // sth is wrong here with program logic |
|---|
| 133 | error = "cannot create data (logic error)"; |
|---|
| 134 | } |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | if (error && annotateError) annotate_error(); |
|---|
| 138 | return error; |
|---|
| 139 | } |
|---|
| 140 | }; |
|---|
| 141 | |
|---|
| 142 | inline bool saiHasDataInAli(GBDATA *gb_sai_data, const char *saiName, const char *ali) { |
|---|
| 143 | GBDATA *gb_sai = GBT_expect_SAI_rel_SAI_data(gb_sai_data, saiName); |
|---|
| 144 | if (!gb_sai) { |
|---|
| 145 | GB_clear_error(); |
|---|
| 146 | return false; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | SequenceHandler saiseq(gb_sai, ali); |
|---|
| 150 | return saiseq.wasFound(); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | bool SaiCalculator::allSaiHaveDataInAlignment(const char *ali) { |
|---|
| 154 | bool seenMissingData = false; |
|---|
| 155 | GBDATA *gb_sai_data = GBT_get_SAI_data(gb_main); |
|---|
| 156 | |
|---|
| 157 | for (int i = 0; inputSaiNames[i] && !seenMissingData; ++i) { |
|---|
| 158 | seenMissingData = !saiHasDataInAli(gb_sai_data, inputSaiNames[i], ali); |
|---|
| 159 | } |
|---|
| 160 | return !seenMissingData; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | void SaiCalculator::run() { |
|---|
| 164 | sai_assert(!GB_have_error()); |
|---|
| 165 | |
|---|
| 166 | GBDATA *gb_sai_data = GBT_get_SAI_data(gb_main); |
|---|
| 167 | GBDATA *gb_target_sai = GBT_find_SAI_rel_SAI_data(gb_sai_data, outSaiName); |
|---|
| 168 | if (!gb_target_sai) { |
|---|
| 169 | if (GB_have_error()) { |
|---|
| 170 | error = GB_await_error(); |
|---|
| 171 | } |
|---|
| 172 | else { // create new SAI as target: |
|---|
| 173 | gb_target_sai = GB_create_container(gb_sai_data, "extended"); |
|---|
| 174 | error = gb_target_sai |
|---|
| 175 | ? GBT_write_string(gb_target_sai, "name", outSaiName) |
|---|
| 176 | : GB_await_error(); |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | if (!error) { |
|---|
| 181 | sai_assert(gb_target_sai); |
|---|
| 182 | |
|---|
| 183 | // @@@ DRY cases: |
|---|
| 184 | switch (scope) { |
|---|
| 185 | case SAS_SELECTED: { |
|---|
| 186 | char *ali = GBT_get_default_alignment(gb_main); |
|---|
| 187 | if (!ali) { |
|---|
| 188 | error = GB_await_error(); |
|---|
| 189 | } |
|---|
| 190 | else { |
|---|
| 191 | calc4ali(ali, gb_target_sai); |
|---|
| 192 | free(ali); |
|---|
| 193 | } |
|---|
| 194 | break; |
|---|
| 195 | } |
|---|
| 196 | case SAS_ALL: { |
|---|
| 197 | ConstStrArray aliName; |
|---|
| 198 | GBT_get_alignment_names(aliName, gb_main); |
|---|
| 199 | if (aliName.empty()) error = "No alignments found"; |
|---|
| 200 | |
|---|
| 201 | for (int a = 0; aliName[a] && !error; ++a) { |
|---|
| 202 | calc4ali(aliName[a], gb_target_sai); |
|---|
| 203 | } |
|---|
| 204 | break; |
|---|
| 205 | } |
|---|
| 206 | case SAS_COMMON: { |
|---|
| 207 | ConstStrArray aliName; |
|---|
| 208 | GBT_get_alignment_names(aliName, gb_main); |
|---|
| 209 | if (aliName.empty()) error = "No alignments found"; |
|---|
| 210 | |
|---|
| 211 | ConstStrArray commonAliName; |
|---|
| 212 | for (int a = 0; aliName[a] && !error; ++a) { |
|---|
| 213 | if (allSaiHaveDataInAlignment(aliName[a])) { |
|---|
| 214 | commonAliName.put(aliName[a]); |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | if (commonAliName.empty() && !error) error = "No alignments found where all SAI have data"; |
|---|
| 218 | |
|---|
| 219 | for (int a = 0; commonAliName[a] && !error; ++a) { |
|---|
| 220 | calc4ali(commonAliName[a], gb_target_sai); |
|---|
| 221 | } |
|---|
| 222 | break; |
|---|
| 223 | } |
|---|
| 224 | case SAS_TARGET: { |
|---|
| 225 | ConstStrArray aliName; |
|---|
| 226 | GBT_get_alignment_names(aliName, gb_main); |
|---|
| 227 | if (aliName.empty()) error = "No alignments found"; |
|---|
| 228 | |
|---|
| 229 | ConstStrArray existingAliName; |
|---|
| 230 | for (int a = 0; aliName[a] && !error; ++a) { |
|---|
| 231 | if (saiHasDataInAli(gb_sai_data, outSaiName, aliName[a])) { |
|---|
| 232 | existingAliName.put(aliName[a]); |
|---|
| 233 | } |
|---|
| 234 | } |
|---|
| 235 | if (existingAliName.empty() && !error) error = "No alignments found where target SAI has data"; |
|---|
| 236 | |
|---|
| 237 | for (int a = 0; existingAliName[a] && !error; ++a) { |
|---|
| 238 | calc4ali(existingAliName[a], gb_target_sai); |
|---|
| 239 | } |
|---|
| 240 | break; |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | // @@@ test gb_target_sai for "ali_..." subcontainers. If none exist = > raise error to avoid creation of SAI w/o any data. |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | void SaiCalculator::calc4ali(const char *ali, GBDATA *gb_target_sai) { |
|---|
| 250 | sai_assert(ali && ali[0]); // has to be defined and not empty |
|---|
| 251 | sai_assert(!error); |
|---|
| 252 | |
|---|
| 253 | GBDATA *gb_sai_data = GBT_get_SAI_data(gb_main); |
|---|
| 254 | string sai_comment = "CalcSAI: "; |
|---|
| 255 | |
|---|
| 256 | // collect input data: |
|---|
| 257 | StrArray saiData; |
|---|
| 258 | for (int i = 0; inputSaiNames[i] && !error; ++i) { |
|---|
| 259 | GBDATA *gb_sai = GBT_expect_SAI_rel_SAI_data(gb_sai_data, inputSaiNames[i]); |
|---|
| 260 | if (!gb_sai) { |
|---|
| 261 | error = GB_await_error(); |
|---|
| 262 | } |
|---|
| 263 | else { |
|---|
| 264 | SequenceHandler saiseq(gb_sai, ali); |
|---|
| 265 | if (saiseq.getError()) { |
|---|
| 266 | error = saiseq.getError(); |
|---|
| 267 | } |
|---|
| 268 | else if (saiseq.wasFound()) { |
|---|
| 269 | saiData.put(saiseq.read_string()); |
|---|
| 270 | } |
|---|
| 271 | else { |
|---|
| 272 | UNCOVERED(); |
|---|
| 273 | // @@@ somehow report sai has no data in alignment (caller shall fail if not found any data or if not enough SAI for operator) |
|---|
| 274 | } |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | if (i>0) sai_comment += '+'; |
|---|
| 278 | sai_comment += inputSaiNames[i]; |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | if (!error) { |
|---|
| 282 | SaiCalcEnv calcEnv(saiData, gb_main); |
|---|
| 283 | ErrorOrString result = op.apply(calcEnv); |
|---|
| 284 | |
|---|
| 285 | sai_comment += " | "; |
|---|
| 286 | sai_comment += op.get_description(); |
|---|
| 287 | |
|---|
| 288 | if (result.hasError()) { |
|---|
| 289 | error = result.getError(); |
|---|
| 290 | } |
|---|
| 291 | else { // write result to target SAI: |
|---|
| 292 | SequenceHandler targetSeq(gb_target_sai, ali); |
|---|
| 293 | |
|---|
| 294 | if (!targetSeq.wasFound()) { |
|---|
| 295 | error = targetSeq.create(GB_STRING, "data"); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | if (!error) { |
|---|
| 299 | string saidata = result.getValue(); |
|---|
| 300 | error = targetSeq.write_string(saidata.c_str()); |
|---|
| 301 | |
|---|
| 302 | if (!error) { |
|---|
| 303 | GBDATA *gb_ali = GB_entry(gb_target_sai, ali); |
|---|
| 304 | gb_assert(gb_ali); |
|---|
| 305 | if (gb_ali) { |
|---|
| 306 | // write SAI comment (operator type, source SAIs, ...) |
|---|
| 307 | error = GBT_write_string(gb_ali, "_TYPE", sai_comment.c_str()); |
|---|
| 308 | } |
|---|
| 309 | } |
|---|
| 310 | } |
|---|
| 311 | } |
|---|
| 312 | } |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | // -------------------------------------------------------------------------------- |
|---|
| 316 | |
|---|
| 317 | #ifdef UNIT_TESTS |
|---|
| 318 | |
|---|
| 319 | #include <arb_diff.h> |
|---|
| 320 | #include <arb_file.h> |
|---|
| 321 | #include <test_unit.h> |
|---|
| 322 | |
|---|
| 323 | void TEST_SequenceHandler() { |
|---|
| 324 | GB_shell shell; |
|---|
| 325 | GBDATA *gb_main = GB_open("TEST_prot_tiny.arb", "rw"); // ../../UNIT_TESTER/run/TEST_prot_tiny.arb |
|---|
| 326 | |
|---|
| 327 | { |
|---|
| 328 | GB_transaction ta(gb_main); |
|---|
| 329 | |
|---|
| 330 | GBDATA *gb_maxFreq = GBT_expect_SAI(gb_main, "MAX_FREQUENCY"); |
|---|
| 331 | GBDATA *gb_pvp = GBT_expect_SAI(gb_main, "POS_VAR_BY_PARSIMONY"); |
|---|
| 332 | GBDATA *gb_marker = GBT_expect_SAI(gb_main, "markerline"); |
|---|
| 333 | GBDATA *gb_CONS = GBT_expect_SAI(gb_main, "CONSENSUS"); |
|---|
| 334 | |
|---|
| 335 | SequenceHandler mfdna(gb_maxFreq, "ali_dna"); |
|---|
| 336 | SequenceHandler mfpro(gb_maxFreq, "ali_prot"); |
|---|
| 337 | SequenceHandler pvdna(gb_pvp, "ali_dna"); |
|---|
| 338 | SequenceHandler pvpro(gb_pvp, "ali_prot"); |
|---|
| 339 | SequenceHandler mldna(gb_marker, "ali_dna"); |
|---|
| 340 | SequenceHandler mlpro(gb_marker, "ali_prot"); |
|---|
| 341 | |
|---|
| 342 | TEST_EXPECT(mfdna.wasFound()); TEST_EXPECT_NO_ERROR(mfdna.getError()); |
|---|
| 343 | TEST_EXPECT(mfpro.wasFound()); TEST_EXPECT_NO_ERROR(mfpro.getError()); |
|---|
| 344 | TEST_EXPECT(pvdna.wasFound()); TEST_EXPECT_NO_ERROR(pvdna.getError()); |
|---|
| 345 | TEST_EXPECT(mldna.wasFound()); TEST_EXPECT_NO_ERROR(mldna.getError()); |
|---|
| 346 | TEST_EXPECT(mlpro.wasFound()); TEST_EXPECT_NO_ERROR(mlpro.getError()); |
|---|
| 347 | |
|---|
| 348 | TEST_REJECT(pvpro.wasFound()); TEST_EXPECT_ERROR_CONTAINS(pvpro.getError(), "has no entries (at extended 'POS_VAR_BY_PARSIMONY'; alignment 'ali_prot')"); |
|---|
| 349 | |
|---|
| 350 | TEST_EXPECT_CONTAINS(mfdna.read_char_pntr(), "8570068866840660555755577540000060080000008775740000000008876547667770006555555"); |
|---|
| 351 | TEST_EXPECT_EQUAL (mfpro.read_char_pntr(), "====050846555500000760006557055586550000055000555686654808508057566566754665055005800080550004500005666688====0="); |
|---|
| 352 | TEST_EXPECT_CONTAINS(pvdna.read_char_pntr(), "..---664--2662440-44-61662664462-----4--4------662440.........442241224552"); |
|---|
| 353 | TEST_EXPECT_CONTAINS(mldna.read_char_pntr(), "00111101111111110111100010001100111111111111111111010111111111111100111111111100000011111111001001111111111111111"); |
|---|
| 354 | TEST_EXPECT_EQUAL (mlpro.read_char_pntr(), "0000101101000011111111111001100011001111100111000111100111011101011011100110100110111111001110011110111111000010"); |
|---|
| 355 | |
|---|
| 356 | // test to modify handled sequence and then read modified: |
|---|
| 357 | TEST_EXPECT_NO_ERROR(mfpro.write_string(SmartCharPtr(GBS_string_eval(mfpro.read_char_pntr(), ":0=-")).content())); |
|---|
| 358 | // "====050846555500000760006557055586550000055000555686654808508057566566754665055005800080550004500005666688====0=" |
|---|
| 359 | TEST_EXPECT_EQUAL (mfpro.read_char_pntr(), "====-5-8465555-----76---6557-5558655-----55---5556866548-85-8-57566566754665-55--58---8-55---45----5666688====-="); |
|---|
| 360 | |
|---|
| 361 | TEST_EXPECT_NO_ERROR(mlpro.write_string(SmartCharPtr(GBS_string_eval(mlpro.read_char_pntr(), ":1=-:0=1:-=0")).content())); // invert |
|---|
| 362 | // "0000101101000011111111111001100011001111100111000111100111011101011011100110100110111111001110011110111111000010" |
|---|
| 363 | TEST_EXPECT_EQUAL(mlpro.read_char_pntr(), "1111010010111100000000000110011100110000011000111000011000100010100100011001011001000000110001100001000000111101"); |
|---|
| 364 | |
|---|
| 365 | TEST_EXPECT_NO_ERROR(mlpro.write_string("hello")); |
|---|
| 366 | TEST_EXPECT_EQUAL(mlpro.read_char_pntr(), "11111"); |
|---|
| 367 | |
|---|
| 368 | // test to create sequence data: |
|---|
| 369 | TEST_EXPECT_ERROR_CONTAINS(mfdna.create(GB_STRING, "data"), "cannot create data over existing (at extended 'MAX_FREQUENCY'; alignment 'ali_dna'; field 'data')"); |
|---|
| 370 | TEST_EXPECT_ERROR_CONTAINS(mldna.create(GB_STRING, "data"), "cannot create data over existing (at extended 'markerline'; alignment 'ali_dna'; field 'bits')"); |
|---|
| 371 | |
|---|
| 372 | TEST_REJECT(pvpro.wasFound()); // has no data yet |
|---|
| 373 | TEST_EXPECT_NO_ERROR(pvpro.create(GB_STRING, "data")); // create of new data entry (ali container is missing here) |
|---|
| 374 | TEST_EXPECT(pvpro.wasFound()); // now has data |
|---|
| 375 | // test write + read data into created GB_STRING entry: |
|---|
| 376 | TEST_EXPECT_EQUAL(pvpro.read_char_pntr(), "..."); // default when only created, but never written |
|---|
| 377 | TEST_EXPECT_NO_ERROR(pvpro.write_string( "10210310")); |
|---|
| 378 | TEST_EXPECT_EQUAL(pvpro.read_char_pntr(), "10210310"); |
|---|
| 379 | |
|---|
| 380 | { |
|---|
| 381 | // erase data entries from SAI "CONSENSUS": |
|---|
| 382 | GBDATA *gb_CONS_ali_dna = GB_entry(gb_CONS, "ali_dna"); |
|---|
| 383 | GBDATA *gb_CONS_ali_pro = GB_entry(gb_CONS, "ali_prot"); |
|---|
| 384 | |
|---|
| 385 | GBDATA *gb_data_dna = GB_entry(gb_CONS_ali_dna, "data"); |
|---|
| 386 | GBDATA *gb_data_pro = GB_entry(gb_CONS_ali_pro, "data"); |
|---|
| 387 | |
|---|
| 388 | TEST_EXPECT_NO_ERROR(GB_delete(gb_data_dna)); |
|---|
| 389 | TEST_EXPECT_NO_ERROR(GB_delete(gb_data_pro)); |
|---|
| 390 | |
|---|
| 391 | // create new data entry with wrong type |
|---|
| 392 | GBDATA *gb_int_data = GB_create(gb_CONS_ali_pro, "data", GB_INT); |
|---|
| 393 | TEST_EXPECT_NO_ERROR(GB_write_int(gb_int_data, 4711)); |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | SequenceHandler codna(gb_CONS, "ali_dna"); |
|---|
| 397 | SequenceHandler copro(gb_CONS, "ali_prot"); |
|---|
| 398 | |
|---|
| 399 | TEST_REJECT(codna.wasFound()); |
|---|
| 400 | TEST_REJECT(copro.wasFound()); |
|---|
| 401 | TEST_EXPECT_ERROR_CONTAINS(codna.getError(), "could not detect sequence data (at extended 'CONSENSUS'; alignment 'ali_dna')"); |
|---|
| 402 | TEST_EXPECT_ERROR_CONTAINS(copro.getError(), "cannot handle type as sequence- or associated-data (at extended 'CONSENSUS'; alignment 'ali_prot')"); |
|---|
| 403 | |
|---|
| 404 | TEST_REJECT(codna.wasFound()); // has no data yet |
|---|
| 405 | TEST_EXPECT_NO_ERROR(codna.create(GB_BITS, "bits")); // create of new data entry (ali container is present here) |
|---|
| 406 | TEST_EXPECT(codna.wasFound()); // now has data |
|---|
| 407 | // test write + read data into created GB_BITS entry: |
|---|
| 408 | TEST_EXPECT_NORESULT__NOERROREXPORTED(codna.read_char_pntr()); // default when only created, but never written |
|---|
| 409 | TEST_EXPECT_NO_ERROR(codna.write_string( "10210310")); |
|---|
| 410 | TEST_EXPECT_EQUAL(codna.read_char_pntr(), "10110110"); // string above was converted into bitstring |
|---|
| 411 | |
|---|
| 412 | // let create fail due to createPossible==false: |
|---|
| 413 | TEST_EXPECT_ERROR_CONTAINS(copro.create(GB_STRING, "data"), "cannot handle type as sequence- or associated-data (at extended 'CONSENSUS'; alignment 'ali_prot')"); // @@@ somehow indicate this is a followup-error?! |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | GB_close(gb_main); |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | #define TEST_EXPECT_MAKEOP(type,cfg) do{ \ |
|---|
| 420 | ErrorOrSaiOperatorPtr made = SaiOperator::make(type,cfg); \ |
|---|
| 421 | if (made.hasError()) { \ |
|---|
| 422 | TEST_EXPECT_NO_ERROR(made.getError().deliver()); \ |
|---|
| 423 | } \ |
|---|
| 424 | op = made.getValue(); \ |
|---|
| 425 | }while(0) |
|---|
| 426 | |
|---|
| 427 | static GB_ERROR tmp_all_but(GBDATA *gb_main, const char *keep) { |
|---|
| 428 | GB_transaction ta(gb_main); |
|---|
| 429 | GB_ERROR error = NULp; |
|---|
| 430 | for (GBDATA *gbd = GB_child(gb_main); gbd && !error; gbd = GB_nextChild(gbd)) { |
|---|
| 431 | if (strcmp(GB_read_key_pntr(gbd), keep) != 0) { |
|---|
| 432 | error = GB_set_temporary(gbd); |
|---|
| 433 | } |
|---|
| 434 | } |
|---|
| 435 | error = ta.close(error); |
|---|
| 436 | return error; |
|---|
| 437 | } |
|---|
| 438 | |
|---|
| 439 | static void clone_test_sai(GBDATA *gb_main, const char *name) { |
|---|
| 440 | GB_transaction ta(gb_main); |
|---|
| 441 | |
|---|
| 442 | GBDATA *gb_sai_data = GBT_get_SAI_data(gb_main); TEST_REJECT_NULL(gb_sai_data); |
|---|
| 443 | GBDATA *gb_sai = GBT_expect_SAI_rel_SAI_data(gb_sai_data, name); TEST_REJECT_NULL(gb_sai); |
|---|
| 444 | GBDATA *gb_clone_sai = GB_clone(gb_sai_data, gb_sai); TEST_REJECT_NULL(gb_clone_sai); |
|---|
| 445 | |
|---|
| 446 | GBDATA *gb_clone_name = gb_clone_sai ? GB_entry(gb_clone_sai, "name") : NULp; |
|---|
| 447 | TEST_REJECT_NULL(gb_clone_name); |
|---|
| 448 | |
|---|
| 449 | TEST_EXPECT_NO_ERROR(GB_write_string(gb_clone_name, GBS_global_string("cloned_%s", GB_read_char_pntr(gb_clone_name)))); |
|---|
| 450 | } |
|---|
| 451 | |
|---|
| 452 | static GB_ERROR clone_SAI(GBDATA *gb_main, const char *source_name, const char *dest_name) { |
|---|
| 453 | GB_ERROR error = NULL; |
|---|
| 454 | |
|---|
| 455 | GBDATA *gb_sai_data = GBT_get_SAI_data(gb_main); |
|---|
| 456 | GBDATA *gb_source = GBT_find_SAI_rel_SAI_data(gb_sai_data, source_name); |
|---|
| 457 | |
|---|
| 458 | if (!gb_source) error = "cant find source"; |
|---|
| 459 | else { |
|---|
| 460 | GBDATA *gb_dest_exists = GBT_find_SAI_rel_SAI_data(gb_sai_data, dest_name); |
|---|
| 461 | if (gb_dest_exists) error = "dest exists"; |
|---|
| 462 | else { |
|---|
| 463 | GBDATA *gb_dest = GB_create_container(gb_sai_data, "extended"); |
|---|
| 464 | if (!gb_dest) error = GB_await_error(); |
|---|
| 465 | else { |
|---|
| 466 | error = GB_copy_dropProtectMarksAndTempstate(gb_dest, gb_source); |
|---|
| 467 | if (!error) error = GBT_write_string(gb_dest, "name", dest_name); |
|---|
| 468 | } |
|---|
| 469 | } |
|---|
| 470 | } |
|---|
| 471 | |
|---|
| 472 | return error; |
|---|
| 473 | } |
|---|
| 474 | |
|---|
| 475 | void TEST_SaiCalculator() { |
|---|
| 476 | GB_shell shell; |
|---|
| 477 | GBDATA *gb_main = GB_open("TEST_prot_tiny.arb", "rw"); // ../../UNIT_TESTER/run/TEST_prot_tiny.arb |
|---|
| 478 | // other database with several SAI: ../../UNIT_TESTER/run/TEST_fields_ascii.arb |
|---|
| 479 | |
|---|
| 480 | ConstStrArray inputSais; |
|---|
| 481 | |
|---|
| 482 | SaiOperatorPtr op; |
|---|
| 483 | TEST_EXPECT_MAKEOP(SOP_TRANSLATE, "default='.'"); // creates 'op' from type and config (or fails) |
|---|
| 484 | |
|---|
| 485 | { |
|---|
| 486 | SaiCalculator calculator(gb_main, inputSais, *op, "unwrittenSAI", SAS_SELECTED); |
|---|
| 487 | TEST_EXPECT(calculator.hasError()); |
|---|
| 488 | TEST_EXPECT_ERROR_CONTAINS(calculator.getError().deliver(), "translator applies to single SAI only (have: 0)"); // @@@ clumsy message; test for multiple SAIs is in saiop.cxx@CLUMSYMSG |
|---|
| 489 | } |
|---|
| 490 | |
|---|
| 491 | inputSais.put("MAX_FREQUENCY"); // (only in "ali_dna") |
|---|
| 492 | { |
|---|
| 493 | SaiCalculator calculator(gb_main, inputSais, *op, "mf_translated", SAS_SELECTED); |
|---|
| 494 | TEST_REJECT(calculator.hasError()); |
|---|
| 495 | } |
|---|
| 496 | |
|---|
| 497 | // test markerline translation via ACI: |
|---|
| 498 | inputSais.clear(); |
|---|
| 499 | inputSais.put("markerline"); // (in "ali_dna" and "ali_prot") |
|---|
| 500 | TEST_EXPECT_MAKEOP(SOP_ACI, "aci='translate(\"01\",\"-x\")'"); |
|---|
| 501 | |
|---|
| 502 | { |
|---|
| 503 | SaiCalculator calculator(gb_main, inputSais, *op, "markerline_aci", SAS_SELECTED); |
|---|
| 504 | TEST_REJECT(calculator.hasError()); |
|---|
| 505 | } |
|---|
| 506 | |
|---|
| 507 | // clone markerline sai -> markerline_clone (using copy function as used in SAI admin): |
|---|
| 508 | { |
|---|
| 509 | GB_transaction ta(gb_main); |
|---|
| 510 | TEST_EXPECT_NO_ERROR(clone_SAI(gb_main, "markerline", "markerline_clone")); |
|---|
| 511 | } |
|---|
| 512 | |
|---|
| 513 | // apply inverse aci than above, saving over binary sai: |
|---|
| 514 | TEST_EXPECT_MAKEOP(SOP_ACI, "aci='translate(\"01\",\"10\")'"); |
|---|
| 515 | { |
|---|
| 516 | SaiCalculator calculator(gb_main, inputSais, *op, "markerline_clone", SAS_COMMON); |
|---|
| 517 | TEST_REJECT(calculator.hasError()); |
|---|
| 518 | |
|---|
| 519 | // additional tests: test sai has 'data' + test content of 'bits': |
|---|
| 520 | GB_transaction ta(gb_main); |
|---|
| 521 | |
|---|
| 522 | GBDATA *gb_sai = GBT_find_SAI(gb_main, "markerline_clone"); TEST_REJECT_NULL(gb_sai); |
|---|
| 523 | |
|---|
| 524 | GBDATA *gb_ali = GB_entry(gb_sai, "ali_prot"); TEST_REJECT_NULL(gb_ali); |
|---|
| 525 | GBDATA *gb_data = GB_entry(gb_ali, "data"); TEST_EXPECT_NULL(gb_data); // no longer writes into 'data' entry |
|---|
| 526 | GBDATA *gb_bits = GB_entry(gb_ali, "bits"); TEST_REJECT_NULL(gb_bits); |
|---|
| 527 | |
|---|
| 528 | SmartCharPtr bitstr = GB_read_as_string(gb_bits); |
|---|
| 529 | TEST_EXPECT_EQUAL(&*bitstr, "1111010010111100000000000110011100110000011000111000011000100010100100011001011001000000110001100001000000111101"); |
|---|
| 530 | } |
|---|
| 531 | |
|---|
| 532 | inputSais.put("POS_VAR_BY_PARSIMONY"); // (only in "ali_dna") |
|---|
| 533 | TEST_EXPECT_MAKEOP(SOP_ACI, "aci='remove(\"-=.0\")|len|srt(0=-)'"); |
|---|
| 534 | |
|---|
| 535 | { |
|---|
| 536 | SaiCalculator calculator(gb_main, inputSais, *op, "pvp_mix_marker1", SAS_SELECTED); |
|---|
| 537 | TEST_REJECT(calculator.hasError()); |
|---|
| 538 | } |
|---|
| 539 | { |
|---|
| 540 | SaiCalculator calculator(gb_main, inputSais, *op, "pvp_mix_marker2", SAS_ALL); |
|---|
| 541 | TEST_EXPECT(calculator.hasError()); |
|---|
| 542 | TEST_EXPECT_ERROR_CONTAINS(calculator.getError().deliver(), "has no entries (at extended 'POS_VAR_BY_PARSIMONY'; alignment 'ali_prot')"); |
|---|
| 543 | } |
|---|
| 544 | { |
|---|
| 545 | SaiCalculator calculator(gb_main, inputSais, *op, "pvp_mix_marker3", SAS_COMMON); |
|---|
| 546 | TEST_REJECT(calculator.hasError()); |
|---|
| 547 | } |
|---|
| 548 | |
|---|
| 549 | TEST_EXPECT_NO_ERROR(GBT_set_default_alignment(gb_main, "ali_prot")); |
|---|
| 550 | |
|---|
| 551 | inputSais.remove(1); // remove POS_VAR_BY_PARSIMONY |
|---|
| 552 | // (keep "markerline") |
|---|
| 553 | inputSais.put("CONSENSUS"); // (in "ali_dna" and "ali_prot") |
|---|
| 554 | TEST_EXPECT_MAKEOP(SOP_ACI, "aci='count(atAT);head(1)|mult|srt(0=-)'"); // only show set marker position for A and T. |
|---|
| 555 | { |
|---|
| 556 | SaiCalculator calculator(gb_main, inputSais, *op, "ml_cons1", SAS_SELECTED); // "ml_cons1" now has only data in "ali_prot" |
|---|
| 557 | TEST_REJECT(calculator.hasError()); |
|---|
| 558 | } |
|---|
| 559 | { |
|---|
| 560 | SaiCalculator calculator(gb_main, inputSais, *op, "ml_cons2", SAS_ALL); |
|---|
| 561 | TEST_EXPECT(calculator.hasError()); |
|---|
| 562 | TEST_EXPECT_ERROR_CONTAINS(calculator.getError().deliver(), "has no entries (at extended 'markerline'; alignment 'ali_dna_incomplete')"); // @@@ change error message: maybe better sth like "has no data"? |
|---|
| 563 | } |
|---|
| 564 | { |
|---|
| 565 | SaiCalculator calculator(gb_main, inputSais, *op, "ml_cons3", SAS_COMMON); |
|---|
| 566 | TEST_REJECT(calculator.hasError()); |
|---|
| 567 | } |
|---|
| 568 | |
|---|
| 569 | // @@@ provoke some "no data" error by combining "ml_cons1" (ali_prot only) + POS_VAR_BY_PARSIMONY (ali_dna only) with scope SAS_COMMON |
|---|
| 570 | |
|---|
| 571 | clone_test_sai(gb_main, "ml_cons1"); // data in 'ali_prot' only |
|---|
| 572 | clone_test_sai(gb_main, "markerline_aci"); // data in 'ali_dna' only |
|---|
| 573 | |
|---|
| 574 | // test SAS_TARGET (e.g. overwrite cloned SAIs with different operator): |
|---|
| 575 | { |
|---|
| 576 | SaiOperatorPtr oldOp = op; |
|---|
| 577 | |
|---|
| 578 | TEST_EXPECT_MAKEOP(SOP_ACI, "aci='count(gcGC);head(1)|mult|srt(0=-)'"); // only show set marker position for G and C. |
|---|
| 579 | { |
|---|
| 580 | SaiCalculator calculator(gb_main, inputSais, *op, "cloned_ml_cons1", SAS_TARGET); |
|---|
| 581 | TEST_REJECT(calculator.hasError()); |
|---|
| 582 | } |
|---|
| 583 | { |
|---|
| 584 | SaiCalculator calculator(gb_main, inputSais, *op, "cloned_markerline_aci", SAS_TARGET); |
|---|
| 585 | TEST_REJECT(calculator.hasError()); |
|---|
| 586 | } |
|---|
| 587 | |
|---|
| 588 | op = oldOp; |
|---|
| 589 | } |
|---|
| 590 | |
|---|
| 591 | // delete 'ali_dna_incomplete' (used in no SAI, only in one species) |
|---|
| 592 | { |
|---|
| 593 | GB_transaction ta(gb_main); |
|---|
| 594 | GBDATA *gb_ali_incomplete = GBT_get_alignment(gb_main, "ali_dna_incomplete"); |
|---|
| 595 | TEST_EXPECT_NO_ERROR(GB_delete(gb_ali_incomplete)); |
|---|
| 596 | } |
|---|
| 597 | { |
|---|
| 598 | // test again with SAS_ALL -> now it works |
|---|
| 599 | SaiCalculator calculator(gb_main, inputSais, *op, "ml_cons4", SAS_ALL); |
|---|
| 600 | TEST_REJECT(calculator.hasError()); |
|---|
| 601 | } |
|---|
| 602 | |
|---|
| 603 | // @@@ test errors where no data is found (or not enough data, e.g. 2 SAI expected by operator but only one found) |
|---|
| 604 | |
|---|
| 605 | // --------------------------------------- |
|---|
| 606 | // save DB and test its content: |
|---|
| 607 | const char *written_ascii = "TEST_calcSAI_written.arb"; |
|---|
| 608 | const char *expectd_ascii = "TEST_calcSAI_expectd.arb"; // ../../UNIT_TESTER/run/TEST_calcSAI_expectd.arb |
|---|
| 609 | |
|---|
| 610 | TEST_EXPECT_NO_ERROR(tmp_all_but(gb_main, "extended_data")); |
|---|
| 611 | TEST_EXPECT_NO_ERROR(GB_save_as(gb_main, written_ascii, "a")); |
|---|
| 612 | |
|---|
| 613 | // #define TEST_AUTO_UPDATE // uncomment to update expected result |
|---|
| 614 | #if defined(TEST_AUTO_UPDATE) |
|---|
| 615 | TEST_COPY_FILE(written_ascii, expectd_ascii); |
|---|
| 616 | #endif |
|---|
| 617 | TEST_EXPECT_TEXTFILE_DIFFLINES(written_ascii, expectd_ascii, 0); |
|---|
| 618 | TEST_EXPECT_ZERO_OR_SHOW_ERRNO(GB_unlink(written_ascii)); |
|---|
| 619 | |
|---|
| 620 | GB_close(gb_main); |
|---|
| 621 | } |
|---|
| 622 | |
|---|
| 623 | #endif // UNIT_TESTS |
|---|
| 624 | |
|---|
| 625 | // -------------------------------------------------------------------------------- |
|---|