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 | calc4ali(ali, gb_target_sai); |
---|
188 | free(ali); |
---|
189 | break; |
---|
190 | } |
---|
191 | case SAS_ALL: { |
---|
192 | ConstStrArray aliName; |
---|
193 | GBT_get_alignment_names(aliName, gb_main); |
---|
194 | |
---|
195 | for (int a = 0; aliName[a] && !error; ++a) { |
---|
196 | calc4ali(aliName[a], gb_target_sai); |
---|
197 | } |
---|
198 | break; |
---|
199 | } |
---|
200 | case SAS_COMMON: { |
---|
201 | ConstStrArray aliName; |
---|
202 | GBT_get_alignment_names(aliName, gb_main); |
---|
203 | |
---|
204 | ConstStrArray commonAliName; |
---|
205 | for (int a = 0; aliName[a] && !error; ++a) { |
---|
206 | if (allSaiHaveDataInAlignment(aliName[a])) { |
---|
207 | commonAliName.put(aliName[a]); |
---|
208 | } |
---|
209 | } |
---|
210 | for (int a = 0; commonAliName[a] && !error; ++a) { |
---|
211 | calc4ali(commonAliName[a], gb_target_sai); |
---|
212 | } |
---|
213 | break; |
---|
214 | } |
---|
215 | case SAS_TARGET: { |
---|
216 | ConstStrArray aliName; |
---|
217 | GBT_get_alignment_names(aliName, gb_main); |
---|
218 | |
---|
219 | ConstStrArray existingAliName; |
---|
220 | for (int a = 0; aliName[a] && !error; ++a) { |
---|
221 | if (saiHasDataInAli(gb_sai_data, outSaiName, aliName[a])) { |
---|
222 | existingAliName.put(aliName[a]); |
---|
223 | } |
---|
224 | } |
---|
225 | for (int a = 0; existingAliName[a] && !error; ++a) { |
---|
226 | calc4ali(existingAliName[a], gb_target_sai); |
---|
227 | } |
---|
228 | break; |
---|
229 | } |
---|
230 | } |
---|
231 | } |
---|
232 | |
---|
233 | // @@@ test gb_target_sai for "ali_..." subcontainers. If none exist = > raise error to avoid creation of SAI w/o any data. |
---|
234 | } |
---|
235 | |
---|
236 | |
---|
237 | void SaiCalculator::calc4ali(const char *ali, GBDATA *gb_target_sai) { |
---|
238 | sai_assert(ali && ali[0]); // has to be defined and not empty |
---|
239 | sai_assert(!error); |
---|
240 | |
---|
241 | GBDATA *gb_sai_data = GBT_get_SAI_data(gb_main); |
---|
242 | string sai_comment = "CalcSAI: "; |
---|
243 | |
---|
244 | // collect input data: |
---|
245 | StrArray saiData; |
---|
246 | for (int i = 0; inputSaiNames[i] && !error; ++i) { |
---|
247 | GBDATA *gb_sai = GBT_expect_SAI_rel_SAI_data(gb_sai_data, inputSaiNames[i]); |
---|
248 | if (!gb_sai) { |
---|
249 | error = GB_await_error(); |
---|
250 | } |
---|
251 | else { |
---|
252 | SequenceHandler saiseq(gb_sai, ali); |
---|
253 | if (saiseq.getError()) { |
---|
254 | error = saiseq.getError(); |
---|
255 | } |
---|
256 | else if (saiseq.wasFound()) { |
---|
257 | saiData.put(saiseq.read_string()); |
---|
258 | } |
---|
259 | else { |
---|
260 | UNCOVERED(); |
---|
261 | // @@@ somehow report sai has no data in alignment (caller shall fail if not found any data or if not enough SAI for operator) |
---|
262 | } |
---|
263 | } |
---|
264 | |
---|
265 | if (i>0) sai_comment += '+'; |
---|
266 | sai_comment += inputSaiNames[i]; |
---|
267 | } |
---|
268 | |
---|
269 | if (!error) { |
---|
270 | SaiCalcEnv calcEnv(saiData, gb_main); |
---|
271 | ErrorOrString result = op.apply(calcEnv); |
---|
272 | |
---|
273 | sai_comment += " | "; |
---|
274 | sai_comment += op.get_description(); |
---|
275 | |
---|
276 | if (result.hasError()) { |
---|
277 | error = result.getError(); |
---|
278 | } |
---|
279 | else { // write result to target SAI: |
---|
280 | SequenceHandler targetSeq(gb_target_sai, ali); |
---|
281 | |
---|
282 | if (!targetSeq.wasFound()) { |
---|
283 | error = targetSeq.create(GB_STRING, "data"); |
---|
284 | } |
---|
285 | |
---|
286 | if (!error) { |
---|
287 | string saidata = result.getValue(); |
---|
288 | error = targetSeq.write_string(saidata.c_str()); |
---|
289 | |
---|
290 | if (!error) { |
---|
291 | GBDATA *gb_ali = GB_entry(gb_target_sai, ali); |
---|
292 | gb_assert(gb_ali); |
---|
293 | if (gb_ali) { |
---|
294 | // write SAI comment (operator type, source SAIs, ...) |
---|
295 | error = GBT_write_string(gb_ali, "_TYPE", sai_comment.c_str()); |
---|
296 | } |
---|
297 | } |
---|
298 | } |
---|
299 | } |
---|
300 | } |
---|
301 | } |
---|
302 | |
---|
303 | // -------------------------------------------------------------------------------- |
---|
304 | |
---|
305 | #ifdef UNIT_TESTS |
---|
306 | |
---|
307 | #include <arb_diff.h> |
---|
308 | #include <arb_file.h> |
---|
309 | #include <test_unit.h> |
---|
310 | |
---|
311 | void TEST_SequenceHandler() { |
---|
312 | GB_shell shell; |
---|
313 | GBDATA *gb_main = GB_open("TEST_prot_tiny.arb", "rw"); // ../../UNIT_TESTER/run/TEST_prot_tiny.arb |
---|
314 | |
---|
315 | { |
---|
316 | GB_transaction ta(gb_main); |
---|
317 | |
---|
318 | GBDATA *gb_maxFreq = GBT_expect_SAI(gb_main, "MAX_FREQUENCY"); |
---|
319 | GBDATA *gb_pvp = GBT_expect_SAI(gb_main, "POS_VAR_BY_PARSIMONY"); |
---|
320 | GBDATA *gb_marker = GBT_expect_SAI(gb_main, "markerline"); |
---|
321 | GBDATA *gb_CONS = GBT_expect_SAI(gb_main, "CONSENSUS"); |
---|
322 | |
---|
323 | SequenceHandler mfdna(gb_maxFreq, "ali_dna"); |
---|
324 | SequenceHandler mfpro(gb_maxFreq, "ali_prot"); |
---|
325 | SequenceHandler pvdna(gb_pvp, "ali_dna"); |
---|
326 | SequenceHandler pvpro(gb_pvp, "ali_prot"); |
---|
327 | SequenceHandler mldna(gb_marker, "ali_dna"); |
---|
328 | SequenceHandler mlpro(gb_marker, "ali_prot"); |
---|
329 | |
---|
330 | TEST_EXPECT(mfdna.wasFound()); TEST_EXPECT_NO_ERROR(mfdna.getError()); |
---|
331 | TEST_EXPECT(mfpro.wasFound()); TEST_EXPECT_NO_ERROR(mfpro.getError()); |
---|
332 | TEST_EXPECT(pvdna.wasFound()); TEST_EXPECT_NO_ERROR(pvdna.getError()); |
---|
333 | TEST_EXPECT(mldna.wasFound()); TEST_EXPECT_NO_ERROR(mldna.getError()); |
---|
334 | TEST_EXPECT(mlpro.wasFound()); TEST_EXPECT_NO_ERROR(mlpro.getError()); |
---|
335 | |
---|
336 | TEST_REJECT(pvpro.wasFound()); TEST_EXPECT_ERROR_CONTAINS(pvpro.getError(), "has no entries (at extended 'POS_VAR_BY_PARSIMONY'; alignment 'ali_prot')"); |
---|
337 | |
---|
338 | TEST_EXPECT_CONTAINS(mfdna.read_char_pntr(), "8570068866840660555755577540000060080000008775740000000008876547667770006555555"); |
---|
339 | TEST_EXPECT_EQUAL (mfpro.read_char_pntr(), "====050846555500000760006557055586550000055000555686654808508057566566754665055005800080550004500005666688====0="); |
---|
340 | TEST_EXPECT_CONTAINS(pvdna.read_char_pntr(), "..---664--2662440-44-61662664462-----4--4------662440.........442241224552"); |
---|
341 | TEST_EXPECT_CONTAINS(mldna.read_char_pntr(), "00111101111111110111100010001100111111111111111111010111111111111100111111111100000011111111001001111111111111111"); |
---|
342 | TEST_EXPECT_EQUAL (mlpro.read_char_pntr(), "0000101101000011111111111001100011001111100111000111100111011101011011100110100110111111001110011110111111000010"); |
---|
343 | |
---|
344 | // test to modify handled sequence and then read modified: |
---|
345 | TEST_EXPECT_NO_ERROR(mfpro.write_string(SmartCharPtr(GBS_string_eval(mfpro.read_char_pntr(), ":0=-")).content())); |
---|
346 | // "====050846555500000760006557055586550000055000555686654808508057566566754665055005800080550004500005666688====0=" |
---|
347 | TEST_EXPECT_EQUAL (mfpro.read_char_pntr(), "====-5-8465555-----76---6557-5558655-----55---5556866548-85-8-57566566754665-55--58---8-55---45----5666688====-="); |
---|
348 | |
---|
349 | TEST_EXPECT_NO_ERROR(mlpro.write_string(SmartCharPtr(GBS_string_eval(mlpro.read_char_pntr(), ":1=-:0=1:-=0")).content())); // invert |
---|
350 | // "0000101101000011111111111001100011001111100111000111100111011101011011100110100110111111001110011110111111000010" |
---|
351 | TEST_EXPECT_EQUAL(mlpro.read_char_pntr(), "1111010010111100000000000110011100110000011000111000011000100010100100011001011001000000110001100001000000111101"); |
---|
352 | |
---|
353 | TEST_EXPECT_NO_ERROR(mlpro.write_string("hello")); |
---|
354 | TEST_EXPECT_EQUAL(mlpro.read_char_pntr(), "11111"); |
---|
355 | |
---|
356 | // test to create sequence data: |
---|
357 | TEST_EXPECT_ERROR_CONTAINS(mfdna.create(GB_STRING, "data"), "cannot create data over existing (at extended 'MAX_FREQUENCY'; alignment 'ali_dna'; field 'data')"); |
---|
358 | TEST_EXPECT_ERROR_CONTAINS(mldna.create(GB_STRING, "data"), "cannot create data over existing (at extended 'markerline'; alignment 'ali_dna'; field 'bits')"); |
---|
359 | |
---|
360 | TEST_REJECT(pvpro.wasFound()); // has no data yet |
---|
361 | TEST_EXPECT_NO_ERROR(pvpro.create(GB_STRING, "data")); // create of new data entry (ali container is missing here) |
---|
362 | TEST_EXPECT(pvpro.wasFound()); // now has data |
---|
363 | // test write + read data into created GB_STRING entry: |
---|
364 | TEST_EXPECT_EQUAL(pvpro.read_char_pntr(), "..."); // default when only created, but never written |
---|
365 | TEST_EXPECT_NO_ERROR(pvpro.write_string( "10210310")); |
---|
366 | TEST_EXPECT_EQUAL(pvpro.read_char_pntr(), "10210310"); |
---|
367 | |
---|
368 | { |
---|
369 | // erase data entries from SAI "CONSENSUS": |
---|
370 | GBDATA *gb_CONS_ali_dna = GB_entry(gb_CONS, "ali_dna"); |
---|
371 | GBDATA *gb_CONS_ali_pro = GB_entry(gb_CONS, "ali_prot"); |
---|
372 | |
---|
373 | GBDATA *gb_data_dna = GB_entry(gb_CONS_ali_dna, "data"); |
---|
374 | GBDATA *gb_data_pro = GB_entry(gb_CONS_ali_pro, "data"); |
---|
375 | |
---|
376 | TEST_EXPECT_NO_ERROR(GB_delete(gb_data_dna)); |
---|
377 | TEST_EXPECT_NO_ERROR(GB_delete(gb_data_pro)); |
---|
378 | |
---|
379 | // create new data entry with wrong type |
---|
380 | GBDATA *gb_int_data = GB_create(gb_CONS_ali_pro, "data", GB_INT); |
---|
381 | TEST_EXPECT_NO_ERROR(GB_write_int(gb_int_data, 4711)); |
---|
382 | } |
---|
383 | |
---|
384 | SequenceHandler codna(gb_CONS, "ali_dna"); |
---|
385 | SequenceHandler copro(gb_CONS, "ali_prot"); |
---|
386 | |
---|
387 | TEST_REJECT(codna.wasFound()); |
---|
388 | TEST_REJECT(copro.wasFound()); |
---|
389 | TEST_EXPECT_ERROR_CONTAINS(codna.getError(), "could not detect sequence data (at extended 'CONSENSUS'; alignment 'ali_dna')"); |
---|
390 | TEST_EXPECT_ERROR_CONTAINS(copro.getError(), "cannot handle type as sequence- or associated-data (at extended 'CONSENSUS'; alignment 'ali_prot')"); |
---|
391 | |
---|
392 | TEST_REJECT(codna.wasFound()); // has no data yet |
---|
393 | TEST_EXPECT_NO_ERROR(codna.create(GB_BITS, "bits")); // create of new data entry (ali container is present here) |
---|
394 | TEST_EXPECT(codna.wasFound()); // now has data |
---|
395 | // test write + read data into created GB_BITS entry: |
---|
396 | TEST_EXPECT_NORESULT__NOERROREXPORTED(codna.read_char_pntr()); // default when only created, but never written |
---|
397 | TEST_EXPECT_NO_ERROR(codna.write_string( "10210310")); |
---|
398 | TEST_EXPECT_EQUAL(codna.read_char_pntr(), "10110110"); // string above was converted into bitstring |
---|
399 | |
---|
400 | // let create fail due to createPossible==false: |
---|
401 | 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?! |
---|
402 | } |
---|
403 | |
---|
404 | GB_close(gb_main); |
---|
405 | } |
---|
406 | |
---|
407 | #define TEST_EXPECT_MAKEOP(type,cfg) do{ \ |
---|
408 | ErrorOrSaiOperatorPtr made = SaiOperator::make(type,cfg); \ |
---|
409 | if (made.hasError()) { \ |
---|
410 | TEST_EXPECT_NO_ERROR(made.getError().deliver()); \ |
---|
411 | } \ |
---|
412 | op = made.getValue(); \ |
---|
413 | }while(0) |
---|
414 | |
---|
415 | static GB_ERROR tmp_all_but(GBDATA *gb_main, const char *keep) { |
---|
416 | GB_transaction ta(gb_main); |
---|
417 | GB_ERROR error = NULp; |
---|
418 | for (GBDATA *gbd = GB_child(gb_main); gbd && !error; gbd = GB_nextChild(gbd)) { |
---|
419 | if (strcmp(GB_read_key_pntr(gbd), keep) != 0) { |
---|
420 | error = GB_set_temporary(gbd); |
---|
421 | } |
---|
422 | } |
---|
423 | error = ta.close(error); |
---|
424 | return error; |
---|
425 | } |
---|
426 | |
---|
427 | static void clone_test_sai(GBDATA *gb_main, const char *name) { |
---|
428 | GB_transaction ta(gb_main); |
---|
429 | |
---|
430 | GBDATA *gb_sai_data = GBT_get_SAI_data(gb_main); TEST_REJECT_NULL(gb_sai_data); |
---|
431 | GBDATA *gb_sai = GBT_expect_SAI_rel_SAI_data(gb_sai_data, name); TEST_REJECT_NULL(gb_sai); |
---|
432 | GBDATA *gb_clone_sai = GB_clone(gb_sai_data, gb_sai); TEST_REJECT_NULL(gb_clone_sai); |
---|
433 | |
---|
434 | GBDATA *gb_clone_name = gb_clone_sai ? GB_entry(gb_clone_sai, "name") : NULp; |
---|
435 | TEST_REJECT_NULL(gb_clone_name); |
---|
436 | |
---|
437 | TEST_EXPECT_NO_ERROR(GB_write_string(gb_clone_name, GBS_global_string("cloned_%s", GB_read_char_pntr(gb_clone_name)))); |
---|
438 | } |
---|
439 | |
---|
440 | static GB_ERROR clone_SAI(GBDATA *gb_main, const char *source_name, const char *dest_name) { |
---|
441 | GB_ERROR error = NULL; |
---|
442 | |
---|
443 | GBDATA *gb_sai_data = GBT_get_SAI_data(gb_main); |
---|
444 | GBDATA *gb_source = GBT_find_SAI_rel_SAI_data(gb_sai_data, source_name); |
---|
445 | |
---|
446 | if (!gb_source) error = "cant find source"; |
---|
447 | else { |
---|
448 | GBDATA *gb_dest_exists = GBT_find_SAI_rel_SAI_data(gb_sai_data, dest_name); |
---|
449 | if (gb_dest_exists) error = "dest exists"; |
---|
450 | else { |
---|
451 | GBDATA *gb_dest = GB_create_container(gb_sai_data, "extended"); |
---|
452 | if (!gb_dest) error = GB_await_error(); |
---|
453 | else { |
---|
454 | error = GB_copy_dropProtectMarksAndTempstate(gb_dest, gb_source); |
---|
455 | if (!error) error = GBT_write_string(gb_dest, "name", dest_name); |
---|
456 | } |
---|
457 | } |
---|
458 | } |
---|
459 | |
---|
460 | return error; |
---|
461 | } |
---|
462 | |
---|
463 | void TEST_SaiCalculator() { |
---|
464 | GB_shell shell; |
---|
465 | GBDATA *gb_main = GB_open("TEST_prot_tiny.arb", "rw"); // ../../UNIT_TESTER/run/TEST_prot_tiny.arb |
---|
466 | // other database with several SAI: ../../UNIT_TESTER/run/TEST_fields_ascii.arb |
---|
467 | |
---|
468 | ConstStrArray inputSais; |
---|
469 | |
---|
470 | SaiOperatorPtr op; |
---|
471 | TEST_EXPECT_MAKEOP(SOP_TRANSLATE, "default='.'"); // creates 'op' from type and config (or fails) |
---|
472 | |
---|
473 | { |
---|
474 | SaiCalculator calculator(gb_main, inputSais, *op, "unwrittenSAI", SAS_SELECTED); |
---|
475 | TEST_EXPECT(calculator.hasError()); |
---|
476 | 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 |
---|
477 | } |
---|
478 | |
---|
479 | inputSais.put("MAX_FREQUENCY"); // (only in "ali_dna") |
---|
480 | { |
---|
481 | SaiCalculator calculator(gb_main, inputSais, *op, "mf_translated", SAS_SELECTED); |
---|
482 | TEST_REJECT(calculator.hasError()); |
---|
483 | } |
---|
484 | |
---|
485 | // test markerline translation via ACI: |
---|
486 | inputSais.clear(); |
---|
487 | inputSais.put("markerline"); // (in "ali_dna" and "ali_prot") |
---|
488 | TEST_EXPECT_MAKEOP(SOP_ACI, "aci='translate(\"01\",\"-x\")'"); |
---|
489 | |
---|
490 | { |
---|
491 | SaiCalculator calculator(gb_main, inputSais, *op, "markerline_aci", SAS_SELECTED); |
---|
492 | TEST_REJECT(calculator.hasError()); |
---|
493 | } |
---|
494 | |
---|
495 | // clone markerline sai -> markerline_clone (using copy function as used in SAI admin): |
---|
496 | { |
---|
497 | GB_transaction ta(gb_main); |
---|
498 | TEST_EXPECT_NO_ERROR(clone_SAI(gb_main, "markerline", "markerline_clone")); |
---|
499 | } |
---|
500 | |
---|
501 | // apply inverse aci than above, saving over binary sai: |
---|
502 | TEST_EXPECT_MAKEOP(SOP_ACI, "aci='translate(\"01\",\"10\")'"); |
---|
503 | { |
---|
504 | SaiCalculator calculator(gb_main, inputSais, *op, "markerline_clone", SAS_COMMON); |
---|
505 | TEST_REJECT(calculator.hasError()); |
---|
506 | |
---|
507 | // additional tests: test sai has 'data' + test content of 'bits': |
---|
508 | GB_transaction ta(gb_main); |
---|
509 | |
---|
510 | GBDATA *gb_sai = GBT_find_SAI(gb_main, "markerline_clone"); TEST_REJECT_NULL(gb_sai); |
---|
511 | |
---|
512 | GBDATA *gb_ali = GB_entry(gb_sai, "ali_prot"); TEST_REJECT_NULL(gb_ali); |
---|
513 | GBDATA *gb_data = GB_entry(gb_ali, "data"); TEST_EXPECT_NULL(gb_data); // no longer writes into 'data' entry |
---|
514 | GBDATA *gb_bits = GB_entry(gb_ali, "bits"); TEST_REJECT_NULL(gb_bits); |
---|
515 | |
---|
516 | SmartCharPtr bitstr = GB_read_as_string(gb_bits); |
---|
517 | TEST_EXPECT_EQUAL(&*bitstr, "1111010010111100000000000110011100110000011000111000011000100010100100011001011001000000110001100001000000111101"); |
---|
518 | } |
---|
519 | |
---|
520 | inputSais.put("POS_VAR_BY_PARSIMONY"); // (only in "ali_dna") |
---|
521 | TEST_EXPECT_MAKEOP(SOP_ACI, "aci='remove(\"-=.0\")|len|srt(0=-)'"); |
---|
522 | |
---|
523 | { |
---|
524 | SaiCalculator calculator(gb_main, inputSais, *op, "pvp_mix_marker1", SAS_SELECTED); |
---|
525 | TEST_REJECT(calculator.hasError()); |
---|
526 | } |
---|
527 | { |
---|
528 | SaiCalculator calculator(gb_main, inputSais, *op, "pvp_mix_marker2", SAS_ALL); |
---|
529 | TEST_EXPECT(calculator.hasError()); |
---|
530 | TEST_EXPECT_ERROR_CONTAINS(calculator.getError().deliver(), "has no entries (at extended 'POS_VAR_BY_PARSIMONY'; alignment 'ali_prot')"); |
---|
531 | } |
---|
532 | { |
---|
533 | SaiCalculator calculator(gb_main, inputSais, *op, "pvp_mix_marker3", SAS_COMMON); |
---|
534 | TEST_REJECT(calculator.hasError()); |
---|
535 | } |
---|
536 | |
---|
537 | TEST_EXPECT_NO_ERROR(GBT_set_default_alignment(gb_main, "ali_prot")); |
---|
538 | |
---|
539 | inputSais.remove(1); // remove POS_VAR_BY_PARSIMONY |
---|
540 | // (keep "markerline") |
---|
541 | inputSais.put("CONSENSUS"); // (in "ali_dna" and "ali_prot") |
---|
542 | TEST_EXPECT_MAKEOP(SOP_ACI, "aci='count(atAT);head(1)|mult|srt(0=-)'"); // only show set marker position for A and T. |
---|
543 | { |
---|
544 | SaiCalculator calculator(gb_main, inputSais, *op, "ml_cons1", SAS_SELECTED); // "ml_cons1" now has only data in "ali_prot" |
---|
545 | TEST_REJECT(calculator.hasError()); |
---|
546 | } |
---|
547 | { |
---|
548 | SaiCalculator calculator(gb_main, inputSais, *op, "ml_cons2", SAS_ALL); |
---|
549 | TEST_EXPECT(calculator.hasError()); |
---|
550 | 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"? |
---|
551 | } |
---|
552 | { |
---|
553 | SaiCalculator calculator(gb_main, inputSais, *op, "ml_cons3", SAS_COMMON); |
---|
554 | TEST_REJECT(calculator.hasError()); |
---|
555 | } |
---|
556 | |
---|
557 | // @@@ provoke some "no data" error by combining "ml_cons1" (ali_prot only) + POS_VAR_BY_PARSIMONY (ali_dna only) with scope SAS_COMMON |
---|
558 | |
---|
559 | clone_test_sai(gb_main, "ml_cons1"); // data in 'ali_prot' only |
---|
560 | clone_test_sai(gb_main, "markerline_aci"); // data in 'ali_dna' only |
---|
561 | |
---|
562 | // test SAS_TARGET (e.g. overwrite cloned SAIs with different operator): |
---|
563 | { |
---|
564 | SaiOperatorPtr oldOp = op; |
---|
565 | |
---|
566 | TEST_EXPECT_MAKEOP(SOP_ACI, "aci='count(gcGC);head(1)|mult|srt(0=-)'"); // only show set marker position for G and C. |
---|
567 | { |
---|
568 | SaiCalculator calculator(gb_main, inputSais, *op, "cloned_ml_cons1", SAS_TARGET); |
---|
569 | TEST_REJECT(calculator.hasError()); |
---|
570 | } |
---|
571 | { |
---|
572 | SaiCalculator calculator(gb_main, inputSais, *op, "cloned_markerline_aci", SAS_TARGET); |
---|
573 | TEST_REJECT(calculator.hasError()); |
---|
574 | } |
---|
575 | |
---|
576 | op = oldOp; |
---|
577 | } |
---|
578 | |
---|
579 | // delete 'ali_dna_incomplete' (used in no SAI, only in one species) |
---|
580 | { |
---|
581 | GB_transaction ta(gb_main); |
---|
582 | GBDATA *gb_ali_incomplete = GBT_get_alignment(gb_main, "ali_dna_incomplete"); |
---|
583 | TEST_EXPECT_NO_ERROR(GB_delete(gb_ali_incomplete)); |
---|
584 | } |
---|
585 | { |
---|
586 | // test again with SAS_ALL -> now it works |
---|
587 | SaiCalculator calculator(gb_main, inputSais, *op, "ml_cons4", SAS_ALL); |
---|
588 | TEST_REJECT(calculator.hasError()); |
---|
589 | } |
---|
590 | |
---|
591 | // @@@ test errors where no data is found (or not enough data, e.g. 2 SAI expected by operator but only one found) |
---|
592 | |
---|
593 | // --------------------------------------- |
---|
594 | // save DB and test its content: |
---|
595 | const char *written_ascii = "TEST_calcSAI_written.arb"; |
---|
596 | const char *expectd_ascii = "TEST_calcSAI_expectd.arb"; // ../../UNIT_TESTER/run/TEST_calcSAI_expectd.arb |
---|
597 | |
---|
598 | TEST_EXPECT_NO_ERROR(tmp_all_but(gb_main, "extended_data")); |
---|
599 | TEST_EXPECT_NO_ERROR(GB_save_as(gb_main, written_ascii, "a")); |
---|
600 | |
---|
601 | // #define TEST_AUTO_UPDATE // uncomment to update expected result |
---|
602 | #if defined(TEST_AUTO_UPDATE) |
---|
603 | TEST_COPY_FILE(written_ascii, expectd_ascii); |
---|
604 | #endif |
---|
605 | TEST_EXPECT_TEXTFILE_DIFFLINES(written_ascii, expectd_ascii, 0); |
---|
606 | TEST_EXPECT_ZERO_OR_SHOW_ERRNO(GB_unlink(written_ascii)); |
---|
607 | |
---|
608 | GB_close(gb_main); |
---|
609 | } |
---|
610 | |
---|
611 | #endif // UNIT_TESTS |
---|
612 | |
---|
613 | // -------------------------------------------------------------------------------- |
---|