Debian Codegraph @

hmmer/src/p7_domain.c

raw symbols SCIP
1 /* functions to manipulate P7_DOMAIN objects. Currently, most P7_DOMAIN objects are created/
2 * manipulated from within code that processes other data structures, but this is somewhat
3 * bad practice. For now, this file mostly contains serialization/deserialization code.
4 * NPC 2/8/19 [Mother Russia]
5 */
6
7 #include <p7_config.h>
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <string.h>
14
15 #include "easel.h"
16 #include "esl_vectorops.h"
17
18 #include "hmmer.h"
19
20 /* Function: p7_domain_Create_empty
21 * Synopsis: Creates an empty P7_DOMAIN object
22 *
23 * Purpose: Creates an empty P7_DOMAIN object (all internal values 0, all internal pointers NULL)
24 *
25 * Inputs: none
26 *
27 * Returns: The created object
28 *
29 * Throws: Returns NULL if unable to allocate memory
30 */
31
32 extern P7_DOMAIN *p7_domain_Create_empty(){
33 P7_DOMAIN *the_domain;
34 int status;
35
36 ESL_ALLOC(the_domain, sizeof(P7_DOMAIN));
37
38 the_domain->ienv = 0;
39 the_domain->jenv = 0;
40 the_domain->iali = 0;
41 the_domain->jali = 0;
42 the_domain->iorf = 0;
43 the_domain->jorf = 0;
44 the_domain->envsc = 0.0;
45 the_domain->domcorrection = 0.0;
46 the_domain->dombias = 0.0;
47 the_domain->oasc = 0.0;
48 the_domain->bitscore = 0.0;
49 the_domain->lnP = 0.0;
50 the_domain->is_reported = 0;
51 the_domain->is_included = 0;
52 the_domain->scores_per_pos = NULL;
53 the_domain->ad = NULL;
54
55 return the_domain;
56
57 ERROR:
58 return NULL;
59
60 }
61
62
63 /* Function: p7_domain_Destroy
64 * Synopsis: Frees all of the memory in a P7_DOMAIN object
65 *
66 * Purpose: Frees all of the memory in a P7_DOMAIN object, including memory in its internal data structures
67 *
68 * Inputs: obj: A pointer to the object to be destroyed
69 *
70 * Returns: Nothing
71 *
72 * Throws: Nothing
73 */
74
75 extern void p7_domain_Destroy(P7_DOMAIN *obj){
76 if(obj == NULL){ // no object to destroy
77 return;
78 }
79 if(obj->scores_per_pos != NULL){
80 free(obj->scores_per_pos);
81 }
82 if(obj->ad != NULL){
83 p7_alidisplay_Destroy(obj->ad);
84 }
85 free(obj);
86 return;
87 }
88
89 /* Function: p7_domain_Copy()
90 * Synopsis: Copy a domain.
91 *
92 * Purpose: Copies domain <src> to hit <dst>, where <dst> has already been
93 * allocated.
94 *
95 * Returns: <eslOK> on success.
96 *
97 * Throws: <eslEMEM> on allocation error.
98 */
99 extern int p7_domain_Copy(const P7_DOMAIN *src, P7_DOMAIN *dst){
100 int status = eslOK;
101 P7_ALIDISPLAY* ad = NULL;
102 float* scores_per_pos = NULL;
103
104 // allocate everything before editing <dst>
105 if (src->ad != NULL) {
106 if ((ad = p7_alidisplay_Clone(src->ad)) == NULL) ESL_XEXCEPTION(eslEMEM, "allocation failure");
107 if (src->scores_per_pos != NULL) {
108 ESL_ALLOC(scores_per_pos, sizeof(float) * src->ad->N);
109 esl_vec_FCopy(src->scores_per_pos, src->ad->N, scores_per_pos);
110 }
111 }
112
113 // allocation succeeded so we can update <dst>
114 memcpy(dst, src, sizeof(P7_DOMAIN));
115 dst->ad = ad;
116 dst->scores_per_pos = scores_per_pos;
117 return status;
118
119 ERROR:
120 free(ad);
121 free(scores_per_pos);
122 return status;
123 }
124
125 /* Function: p7_domain_Serialize
126 * Synopsis: Serializes a P7_DOMAIN object into a stream of bytes
127 *. that can be reliably transmitted over internet sockets
128 *
129 * Purpose: Converts an architecture-dependent P7_DOMAIN object into a contiguous stream
130 * of bytes with each field of the data structure in network byte order for transmission
131 * over sockets. The serialized byte stream may be part of a larger allocated buffer.
132 * If the provided buffer is NULL, allocates a new buffer large enough for the serialized object
133 * If the provided buffer is not large enough to hold the serialized object and its existing data, re-allocates
134 * a larger buffer
135 *
136 * Inputs: obj: A pointer to the P7_DOMAIN object to be serialized
137 * buf: Handle to the buffer that the object should be serialized into. If *buf is NULL,
138 * a new buffer will be allocated. buf == NULL is not allowed.
139 * n: Offset (in bytes) from the start of the buffer to where the serialized object should start.
140 * nalloc: size (in bytes) of the buffer passed in buf
141 *
142 *Returns: On success: returns eslOK, sets *buf to the base of the buffer containing the object
143 * if allocation or re-allocation was requried, sets *n to the offset from the start of the buffer
144 * to the first position after the serialized object and sets *nalloc to the new size of the buffer
145 * if allocation or re-allocation was required.
146 *
147 * Throws: Returns eslEMEM if unable to allocate or re-allocate memory. Returns eslEINVAL if obj == NULL, n == NULL, buf == NULL,
148 * or if *buf = NULL and either *n != 0 or *nalloc != 0
149 * Returns eslFAIL if a calculation fails a consistency check.
150 */
151
152 // base size is 2 ints bigger than required for the fixed-length members of the strucuture, one int for the serialized length,
153 // one int for the length of the scores_per_pos array (in floats)
154 #define SER_BASE_SIZE (4 * sizeof(int)) + (6 * sizeof(int64_t)) + (5 * sizeof(float)) + (sizeof(double))
155
156 extern int p7_domain_Serialize(const P7_DOMAIN *obj, uint8_t **buf, uint32_t *n, uint32_t *nalloc){
157
158 int status; // error variable used by ESL_ALLOC
159 uint32_t ser_size; // size of the structure when serialized
160 uint8_t *ptr; // current position within the buffer
161 uint32_t network_32bit; // hold 32-bit fields after conversion to network order
162 uint64_t network_64bit; // hold 64-bit fields after conversion to network order
163 int i;
164 // check to make sure we were passed a valid pointer
165 if(obj == NULL || buf == NULL || n == NULL){ // no object to serialize or nowhere to put a buffer pointer
166 return(eslEINVAL);
167 }
168
169 if(obj->scores_per_pos != NULL){
170 ser_size = SER_BASE_SIZE + (obj->ad->N * sizeof(float));
171 }
172 else{
173 ser_size = SER_BASE_SIZE;
174 }
175
176 // Now that we know how big the serialized data structure will be, determine if we have enough buffer space to hold it
177 if(*buf == NULL){ // have no buffer, so allocate one
178 if((*n != 0) || (*nalloc != 0)){ // non-zero values of these inputs make no sense if we don't have a buffer yet
179 return eslEINVAL;
180 }
181 ESL_ALLOC(*buf, ser_size);
182 *nalloc = ser_size;
183 }
184
185 if((*n + ser_size) > *nalloc){ //have a buffer, but it's not big enough
186 ESL_REALLOC(*buf, (*n + ser_size));
187 *nalloc = *n + ser_size;
188 }
189
190 ptr = *buf + *n; // pointer to start of region we'll serialize to
191
192 // Field 1: serialized size
193 network_32bit = esl_hton32(ser_size);
194 memcpy(ptr, &network_32bit, sizeof(int32_t));
195 ptr += sizeof(int32_t);
196
197 // Field 2: ienv
198 network_64bit = esl_hton64(obj->ienv);
199 memcpy(ptr, &network_64bit, sizeof(int64_t));
200 ptr += sizeof(int64_t);
201
202 // Field 3: jenv
203 network_64bit = esl_hton64(obj->jenv);
204 memcpy(ptr, &network_64bit, sizeof(int64_t));
205 ptr += sizeof(int64_t);
206
207 // Field 4: iali
208 network_64bit = esl_hton64(obj->iali);
209 memcpy(ptr, &network_64bit, sizeof(int64_t));
210 ptr += sizeof(int64_t);
211
212 // Field 5: jali
213 network_64bit = esl_hton64(obj->jali);
214 memcpy(ptr, &network_64bit, sizeof(int64_t));
215 ptr += sizeof(int64_t);
216
217 // Field 6: iorf
218 network_64bit = esl_hton64(obj->iorf);
219 memcpy(ptr, &network_64bit, sizeof(int64_t));
220 ptr += sizeof(int64_t);
221
222 // Field 7: jorf
223 network_64bit = esl_hton64(obj->jorf);
224 memcpy(ptr, &network_64bit, sizeof(int64_t));
225 ptr += sizeof(int64_t);
226
227 // Field 8: envsc
228 network_32bit = esl_hton32(*((uint32_t *) &(obj->envsc))); // ow, this hurts, but is probably the best way to get the
229 // bits that represent a float out of a structure. Boy, will it fail horribly on any architecture that doesn't use
230 // 32-bit floats
231 memcpy((void *) ptr, (void *) &network_32bit, sizeof(obj->envsc));
232 ptr += sizeof(obj->envsc);
233
234 // Field 9: domcorrection
235 network_32bit = esl_hton32(*((uint32_t *) &(obj->domcorrection)));
236 memcpy((void *) ptr, (void *) &network_32bit, sizeof(obj->domcorrection));
237 ptr += sizeof(obj->domcorrection);
238
239 // Field 10: dombias
240 network_32bit = esl_hton32(*((uint32_t *) &(obj->dombias)));
241 memcpy((void *) ptr, (void *) &network_32bit, sizeof(obj->dombias));
242 ptr += sizeof(obj->dombias);
243
244 // Field 11: oasc
245 network_32bit = esl_hton32(*((uint32_t *) &(obj->oasc)));
246 memcpy((void *) ptr, (void *) &network_32bit, sizeof(obj->oasc));
247 ptr += sizeof(obj->oasc);
248
249 // Field 12: bitscore
250 network_32bit = esl_hton32(*((uint32_t *) &(obj->bitscore)));
251 memcpy((void *) ptr, (void *) &network_32bit, sizeof(obj->bitscore));
252 ptr += sizeof(obj->bitscore);
253
254 // Field 13: lnP
255 network_64bit = esl_hton64(*((uint64_t *) &(obj->lnP)));
256 memcpy((void *) ptr, (void *) &network_64bit, sizeof(obj->lnP));
257 ptr += sizeof(obj->lnP);
258
259 // Field 14: is_reported
260 network_32bit = esl_hton32(obj->is_reported);
261 memcpy(ptr, &network_32bit, sizeof(int32_t));
262 ptr += sizeof(int32_t);
263
264 // Field 15: is_included
265 network_32bit = esl_hton32(obj->is_included);
266 memcpy(ptr, &network_32bit, sizeof(int32_t));
267 ptr += sizeof(int32_t);
268
269 //Handle the scores_per_pos_array
270 if(obj->scores_per_pos == NULL){ // No scores_per_pos, so just record its size as 0
271 network_32bit = esl_hton32(0);
272 memcpy(ptr, &network_32bit, sizeof(int32_t));
273 ptr += sizeof(int32_t);
274 }
275 else{ // There is a valid scores_per_pos array, and its size should be equal to the N of the enclosed alignment
276 int scores_per_pos_length = obj->ad->N;
277
278 network_32bit = esl_hton32(scores_per_pos_length); // add length of the array (in floats) to serialized object
279 memcpy(ptr, &network_32bit, sizeof(int32_t));
280 ptr += sizeof(int32_t);
281
282 for(i = 0; i < scores_per_pos_length; i++){ // serialise the array itself
283 network_32bit = esl_hton32(*((uint32_t *) &(obj->scores_per_pos[i])));
284 memcpy(ptr, &network_32bit, sizeof(int32_t));
285 ptr += sizeof(int32_t);
286 }
287 }
288
289 //sanity check that length of serialized object was what we expected
290 if (ser_size != ptr - (*buf + *n)){
291 ESL_EXCEPTION(eslFAIL,"Unexpected serialized object length found in p7_domain_Serialize\n");
292 }
293
294 *n = ptr - *buf; // update offset into buffer so that alidisplay_Serialize starts in the right place
295 // Finally, the P7_ALIDISPLAY object
296 int ser_return = p7_alidisplay_Serialize(obj->ad, buf, n, nalloc);
297
298 return ser_return; // if we get this far and the Serialize went well, return eslOK. Otherwise, return the error code from
299 // the serialize
300
301 ERROR:
302 return eslEMEM;
303 }
304
305 /* Function: p7_domain_Deserialize
306 * Synopsis: Derializes a P7_DOMAIN object from a stream of bytes in network order into
307 * a valid data structure
308 *
309 * Purpose: Deserializes a serialized P7_DOMAIN object from
310 *. buf starting at position position *pos.
311 *
312 * Inputs: buf: the buffer that the object should be de-serialized from
313 * pos: a pointer to the offset from the start of buf to the beginning of the object
314 * ret_obj: a P7_DOMAIN structure to deserialize the object into. May not be NULL. May either be an
315 * "empty" object created with p7_domain_Create_empty, or a P7_DOMAIN object containing valid data
316 *
317 * Returns: On success: returns eslOK, deserializes the P7_DOMAIN object into ret_object, and updates
318 *. pos to point to the position after the end of the P7_DOMAIN object.
319 *
320 * Throws: Returns eslEINVAL if ret_obj == NULL, buf == NULL, or N == NULL. Returnts eslEMEM if unable to allocate
321 * required memory in ret_obj. Returns eslFAIL if a calculation fails a consistency check.
322 */
323 extern int p7_domain_Deserialize(const uint8_t *buf, uint32_t *n, P7_DOMAIN *ret_obj){
324
325 uint8_t *ptr;
326 uint64_t network_64bit; // holds 64-bit values in network order
327 uint64_t host_64bit; //variable to hold 64-bit values after conversion to host order
328 uint32_t network_32bit; // holds 64-bit values in network order
329 uint32_t host_32bit; //variable to hold 64-bit values after conversion to host order
330 uint32_t obj_size; // How much space does the variable-length portion of the serialized object take up?
331 int status;
332 int i;
333
334 if(ret_obj == NULL || buf == NULL || n == NULL){
335 return eslEINVAL;
336 }
337
338 ptr = (uint8_t *) buf + *n;
339
340 //First field: Size of the serialized object. Copy out of buffer into scalar variable to deal with memory alignment, convert to
341 // host machine order
342 memcpy(&network_32bit, ptr, sizeof(uint32_t)); // Grab the bytes out of the buffer
343 obj_size = esl_ntoh32(network_32bit);
344 ptr += sizeof(uint32_t);
345
346 // Second field: ienv
347 memcpy(&network_64bit, ptr, sizeof(uint64_t));
348 ret_obj->ienv = esl_ntoh64(network_64bit);
349 ptr += sizeof(uint64_t);
350
351 // Third field: jenv
352 memcpy(&network_64bit, ptr, sizeof(uint64_t));
353 ret_obj->jenv = esl_ntoh64(network_64bit);
354 ptr += sizeof(uint64_t);
355
356 // Fourth field: iali
357 memcpy(&network_64bit, ptr, sizeof(uint64_t));
358 ret_obj->iali = esl_ntoh64(network_64bit);
359 ptr += sizeof(uint64_t);
360
361 // Fifth field: jali
362 memcpy(&network_64bit, ptr, sizeof(uint64_t));
363 ret_obj->jali = esl_ntoh64(network_64bit);
364 ptr += sizeof(uint64_t);
365
366 // Sixth field: iorf
367 memcpy(&network_64bit, ptr, sizeof(uint64_t));
368 ret_obj->iorf = esl_ntoh64(network_64bit);
369 ptr += sizeof(uint64_t);
370
371 // Seventh field: jorf
372 memcpy(&network_64bit, ptr, sizeof(uint64_t));
373 ret_obj->jorf = esl_ntoh64(network_64bit);
374 ptr += sizeof(uint64_t);
375
376 // Eighth field: envsc
377 memcpy(&network_32bit, ptr, sizeof(uint32_t)); // Grab the bytes out of the buffer
378 host_32bit = esl_ntoh32(network_32bit);
379 ret_obj->envsc = *((float *) &host_32bit);
380 ptr += sizeof(uint32_t);
381
382 // Ninth field: domcorrection
383 memcpy(&network_32bit, ptr, sizeof(uint32_t)); // Grab the bytes out of the buffer
384 host_32bit = esl_ntoh32(network_32bit);
385 ret_obj->domcorrection = *((float *) &host_32bit);
386 ptr += sizeof(uint32_t);
387
388 // Tenth field: dombias
389 memcpy(&network_32bit, ptr, sizeof(uint32_t)); // Grab the bytes out of the buffer
390 host_32bit = esl_ntoh32(network_32bit);
391 ret_obj->dombias = *((float *) &host_32bit);
392 ptr += sizeof(uint32_t);
393
394 // Eleventh field: oasc
395 memcpy(&network_32bit, ptr, sizeof(uint32_t)); // Grab the bytes out of the buffer
396 host_32bit = esl_ntoh32(network_32bit);
397 ret_obj->oasc = *((float *) &host_32bit);
398 ptr += sizeof(uint32_t);
399
400 // Ninth field: bitscore
401 memcpy(&network_32bit, ptr, sizeof(uint32_t)); // Grab the bytes out of the buffer
402 host_32bit = esl_ntoh32(network_32bit);
403 ret_obj->bitscore = *((float *) &host_32bit);
404 ptr += sizeof(uint32_t);
405
406 // Tenth field: lnP
407 memcpy(&network_64bit, ptr, sizeof(uint64_t)); // Grab the bytes out of the buffer
408 host_64bit = esl_ntoh64(network_64bit);
409 ret_obj->lnP = *((double *) &host_64bit);
410 ptr += sizeof(uint64_t);
411
412 // Eleventh field: is_reported
413 memcpy(&network_32bit, ptr, sizeof(uint32_t));
414 ret_obj->is_reported = esl_ntoh32(network_32bit);
415 ptr += sizeof(uint32_t);
416
417 // Twelfth field: is_included
418 memcpy(&network_32bit, ptr, sizeof(uint32_t));
419 ret_obj->is_included = esl_ntoh32(network_32bit);
420 ptr += sizeof(uint32_t);
421
422 // Thirteenth field: length of scores_per_pos array
423 memcpy(&network_32bit, ptr, sizeof(uint32_t));
424 int scores_per_pos_length = esl_ntoh32(network_32bit);
425 ptr += sizeof(uint32_t);
426
427 if(scores_per_pos_length > 0){ // there is a scores_per_pos array, so handle it
428 if(ret_obj->scores_per_pos != NULL){ // clear out any prevous scores_per_pos array, since we don't know how big it is
429 free(ret_obj->scores_per_pos);
430 }
431 ESL_ALLOC(ret_obj->scores_per_pos, scores_per_pos_length * sizeof(float));
432
433 for(i = 0; i < scores_per_pos_length; i++){
434 memcpy(&network_32bit, ptr, sizeof(uint32_t)); // Grab the bytes out of the buffer
435 host_32bit = esl_ntoh32(network_32bit);
436 ret_obj->scores_per_pos[i] = *((float *) &host_32bit);
437 ptr += sizeof(uint32_t);
438 }
439 }
440
441
442 if(ptr - (buf + *n) != obj_size){
443 ESL_EXCEPTION(eslFAIL, "Deserialized object size didn't match expected length in p7_domain_Deserialize\n");
444 }
445
446 *n = ptr - buf; //update index into the buffer
447
448 // finally, the enclosed P7_ALIDISPLAY object
449 if(ret_obj->ad == NULL){
450 ret_obj->ad =p7_alidisplay_Create_empty(); // need a structure to serialize into
451 }
452 return p7_alidisplay_Deserialize(buf, n, ret_obj->ad); // if this function terminates correctly, return eslOK,
453 // otherwise return its error code
454
455 ERROR:
456 return eslEMEM;
457 }
458 /*****************************************************************
459 * 2. Debugging Functions
460 *****************************************************************/
461
462 /* Function: p7_domain_TestSample
463 * Synopsis: Creates a P7_DOMAIN object that contains random data.
464 *
465 * Purpose: Creates a P7_DOMAIN object that contains random data. This data will be syntactically correct,
466 * but is not intended to be in any way a "reasonable" domain.
467 *
468 * Inputs: rng: the random-number generator to use in creating this object.
469 * ret_obj: pointer that is used to return the created object
470 *
471 * Returns: eslOK, and the created object in ret_obj
472 *
473 * Throws: Returns eslEMEM if unable to allocate or re-allocate memory. Returns eslEINVAL if ret_obj == NULL
474 */
475 extern int p7_domain_TestSample(ESL_RAND64 *rng, P7_DOMAIN **ret_obj){
476 int status;
477 int i;
478 if (ret_obj == NULL)
479 {
480 return eslEINVAL;
481 }
482 if(*ret_obj == NULL){
483 ESL_ALLOC(*ret_obj, sizeof(P7_DOMAIN));
484 }
485
486 P7_DOMAIN *the_domain = *ret_obj; // convenience pointer
487
488
489 the_domain->ienv = esl_rand64(rng);
490 the_domain->jenv = esl_rand64(rng);
491 the_domain->iali = esl_rand64(rng);
492 the_domain->jali = esl_rand64(rng);
493 the_domain->iorf = esl_rand64(rng);
494 the_domain->jorf = esl_rand64(rng);
495 the_domain->envsc = (float) esl_rand64_double(rng);
496 the_domain->domcorrection = (float) esl_rand64_double(rng);
497 the_domain->dombias = (float) esl_rand64_double(rng);
498 the_domain->oasc = (float) esl_rand64_double(rng);
499 the_domain->bitscore = (float) esl_rand64_double(rng);
500 the_domain->lnP = esl_rand64_double(rng);
501 the_domain->is_reported = esl_rand64_Roll(rng, 1);
502 the_domain->is_included = esl_rand64_Roll(rng, 1);
503
504 // sample an alignment with length ranging uniformly from 50-350
505 ESL_RANDOMNESS *rng2 = esl_randomness_Create((uint32_t) esl_rand64(rng)); // This is inefficient, but probably the best we can do
506 // until alidisplay_Sample gets converted to the new rng
507 if(p7_alidisplay_Sample(rng2, esl_rand64_Roll(rng, 300) + 50, &(the_domain->ad)) != eslOK){
508 return eslFAIL;
509 }
510 esl_randomness_Destroy(rng2);
511
512 if(esl_rand64_Roll(rng, 1)){ // 50% chance of having a scores_per_pos array
513 ESL_ALLOC(the_domain->scores_per_pos, the_domain->ad->N * sizeof(float));
514 for(i = 0; i < the_domain->ad->N; i++){
515 the_domain->scores_per_pos[i] = esl_rand64_double(rng);
516 }
517 }
518 else{ // array is empty
519 the_domain->scores_per_pos = NULL;
520 }
521
522 return eslOK; // If we make it here, everything went well
523
524 ERROR:
525 return eslEMEM;
526 }
527
528
529 /* Function: p7_domain_Compare
530 * Synopsis: Compares two P7_DOMAIN objects for equality within the specified tolerances
531 *
532 * Purpose: Compares two P7_DOMAIN objects. Integer fields are compared for equality. Floating-point fields are
533 * compared for equality within the specified relative and absolute tolerances
534 *
535 * Inputs: first: The first object to be compared
536 * second: The second object to be compared
537 * atol: The absolute tolerance to be used when comparing floating-point fields
538 * rtol: The relative tolerance to be used when comparing floating-point fields
539 *
540 * Returns: eslOK if the P7_DOMAIN inputs match, eslFAIL otherwise
541 *
542 * Throws: Nothing
543 */
544 extern int p7_domain_Compare(P7_DOMAIN *first, P7_DOMAIN *second, double atol, double rtol){
545 int i;
546 // compare all the fixed-length fields
547 if(first->ienv != second->ienv){
548 return eslFAIL;
549 }
550 if(first->jenv != second->jenv){
551 return eslFAIL;
552 }
553 if(first->iali != second->iali){
554 return eslFAIL;
555 }
556 if(first->jali != second->jali){
557 return eslFAIL;
558 }
559 if(first->iorf != second->iorf){
560 return eslFAIL;
561 }
562 if(first->jorf != second->jorf){
563 return eslFAIL;
564 }
565 if(esl_FCompare(first->envsc, second->envsc, (float) atol, (float) rtol) != eslOK){
566 return eslFAIL;
567 }
568 if(esl_FCompare(first->domcorrection, second->domcorrection, (float) atol, (float) rtol) != eslOK){
569 return eslFAIL;
570 }
571 if(esl_FCompare(first->dombias, second->dombias, (float) atol, (float) rtol) != eslOK){
572 return eslFAIL;
573 }
574 if(esl_FCompare(first->oasc, second->oasc, (float) atol, (float) rtol) != eslOK){
575 return eslFAIL;
576 }
577 if(esl_FCompare(first->bitscore, second->bitscore, (float) atol, (float) rtol) != eslOK){
578 return eslFAIL;
579 }
580 if(esl_DCompare(first->lnP, second->lnP, atol, rtol) != eslOK){
581 return eslFAIL;
582 }
583 if(first->lnP != second->lnP){
584 return eslFAIL;
585 }
586 if(first->is_reported != second->is_reported){
587 return eslFAIL;
588 }
589 if(first->is_included != second->is_included){
590 return eslFAIL;
591 }
592
593 // comparing scores_per_pos is a bit more challenging
594 if((first->scores_per_pos == NULL && second->scores_per_pos != NULL) ||
595 (first-> scores_per_pos != NULL && second->scores_per_pos == NULL)){
596 return eslFAIL; // can't be the same if one has scores-per_pos and the other doesn't
597 }
598
599 if((first->scores_per_pos != NULL) && (second->scores_per_pos != NULL)){ // bit redundant, since we've already failed if one
600 // scores_per_pos is NULL and the other isn't
601 if(first->ad->N != second->ad->N){
602 return eslFAIL; // can't be the same if the two domains contain scores_per_pos arrays of different length
603 }
604
605 for(i = 0; i < first->ad->N; i++){
606 if(esl_FCompare(first->scores_per_pos[i], second->scores_per_pos[i], (float) atol, (float) rtol) != eslOK){
607 return eslFAIL; // fail if any of the scores_per_pos array values mismatch
608 }
609 }
610 }
611 // Finally, compare the alidisplays. If they match, and we've gotten this far, we match
612 return p7_alidisplay_Compare(first->ad, second->ad);
613 }
614
615 /*****************************************************************
616 * 3. Unit tests
617 *****************************************************************/
618
619
620
621
622 #ifdef p7DOMAIN_TESTDRIVE
623
624 // Test that the _Serialize() function generates the correct errors when passed invalid arguments
625 static void utest_Serialize_error_conditions(){
626 int status; // Easel error code variable
627 P7_DOMAIN *foo = NULL;
628 uint8_t **buf = NULL;
629 uint32_t n;
630 uint32_t nalloc;
631 ESL_RAND64 *rng= NULL;
632 rng = esl_rand64_Create(0);
633
634 char msg[] = "utest_Serialize_error_conditions failed";
635
636 n = 0;
637 nalloc = 0;
638
639 if(p7_domain_TestSample(rng, &foo) != eslOK){
640 esl_fatal(msg);
641 }
642
643 // Test 1: _Serialize returns error if passed NULL buffer
644 buf = NULL;
645
646 if(p7_domain_Serialize(foo, buf, &n, &nalloc) != eslEINVAL){
647 esl_fatal(msg);
648 }
649 else{
650 //printf("null buffer check passed\n");
651 }
652
653 ESL_ALLOC(buf, sizeof(uint8_t *)); // set buf to valid value
654 *buf = NULL;
655
656 // Test 2: error on NULL n ptr
657 if(p7_domain_Serialize(foo, buf, NULL, &nalloc) != eslEINVAL){
658 esl_fatal(msg);
659 }
660 else{
661 //printf("invalid n check passed\n");
662 }
663
664 // Test 3: error on NULL object ptr
665 if(p7_domain_Serialize(NULL, buf, &n, &nalloc) != eslEINVAL){
666 esl_fatal(msg);
667 }
668 else{
669 //printf("invalid object check passed\n");
670 }
671
672 // Test 4: n != 0 and *buf == NULL
673 n = 3;
674 if(p7_domain_Serialize(foo, buf, &n, &nalloc) != eslEINVAL){
675 esl_fatal(msg);
676 }
677 else{
678 //printf("Non-zero n with NULL buffer check passed\n");
679 }
680
681 // Test 5: Nalloc != 0 and *buf == NULL
682 n = 0;
683 nalloc = 10;
684 if(p7_domain_Serialize(foo, buf, &n, &nalloc) != eslEINVAL){
685 esl_fatal(msg);
686 }
687 else{
688 //printf("Non-zero nalloc with NULL buffer check passed\n");
689 }
690
691 if(buf !=NULL && *buf != NULL){
692 free(*buf);
693 }
694 if(buf != NULL){
695 free(buf);
696 }
697
698 esl_rand64_Destroy(rng);
699 p7_domain_Destroy(foo);
700
701 return;
702
703 ERROR:
704 if (rng != NULL){
705 esl_rand64_Destroy(rng);
706 }
707
708 if(foo != NULL){
709 p7_domain_Destroy(foo);
710 }
711
712 if(buf !=NULL && *buf != NULL){
713 free(*buf);
714 }
715 if(buf != NULL){
716 free(buf);
717 }
718 esl_fatal(msg);
719 }
720
721 static void utest_Deserialize_error_conditions(){
722 P7_DOMAIN *sampled = NULL; // sampled alidisplay that we'll serialze
723 P7_DOMAIN *deserial = NULL; // alidisplay to hold the deserialized object
724 char msg[]="utest_Deserialize_error_conditions failed";
725 uint8_t *buf = NULL;
726 uint32_t n = 0, nalloc = 0;
727
728 deserial = p7_domain_Create_empty();
729 if(deserial == NULL){
730 esl_fatal(msg);
731 }
732
733 ESL_RAND64 *rng = esl_rand64_Create(0);
734 if(p7_domain_TestSample(rng, &sampled) != eslOK){
735 esl_fatal(msg);
736 }
737 esl_rand64_Destroy(rng);
738
739 if(p7_domain_Serialize(sampled, &buf, &n, &nalloc) != eslOK){ // serialize an object to deserialize
740 esl_fatal(msg);
741 }
742
743 // Test 1: error on buf == NULL;
744 if(p7_domain_Deserialize(NULL, &n, deserial) != eslEINVAL){
745 esl_fatal(msg);
746 }
747 //printf("Test 1 passed\n");
748
749 // Test 2: error on n == NULL
750 if(p7_domain_Deserialize(buf, NULL, deserial) != eslEINVAL){
751 esl_fatal(msg);
752 }
753 //printf("Test 2 passed\n");
754
755 // Test 3: error on serialized object == NULL
756 if(p7_domain_Deserialize(buf, &n, NULL) != eslEINVAL){
757 esl_fatal(msg);
758 }
759 //printf("Test 3 passed\n");
760 free(buf);
761 p7_domain_Destroy(deserial);
762 p7_domain_Destroy(sampled);
763 return;
764 }
765
766 static void utest_Serialize(int ntrials){
767 int i;
768 uint8_t **buf=NULL;
769 uint32_t n;
770 uint32_t nalloc;
771 P7_DOMAIN **serial=NULL, *deserial=NULL;
772 int status;
773 char msg[] = "utest_Serialize failed";
774
775 ESL_ALLOC(buf, sizeof(uint8_t *));
776 *buf = NULL;
777 n = 0;
778 nalloc = 0;
779
780 ESL_ALLOC(serial, ntrials * sizeof(P7_DOMAIN *));
781 for(i = 0; i< ntrials; i++){
782 serial[i] = NULL;
783 }
784
785 ESL_RAND64 *rng = esl_rand64_Create(0);
786
787 for(i = 0; i < ntrials; i++){
788 if(p7_domain_TestSample(rng, &(serial[i])) != eslOK){
789 esl_fatal(msg);
790 }
791 if(p7_domain_Serialize(serial[i], buf, &n, &nalloc) != eslOK){
792 esl_fatal(msg);
793 }
794 }
795
796 n = 0; // reset to start of buffer
797
798 deserial = p7_domain_Create_empty();
799 if(deserial == NULL){
800 esl_fatal(msg);
801 }
802
803 for(i = 0; i < ntrials; i++){
804 if(p7_domain_Deserialize(*buf, &n, deserial) != eslOK){
805 esl_fatal(msg);
806 }
807 if(p7_domain_Compare(serial[i], deserial, 1e-4, 1e-4) != eslOK){ // deserialized structure didn't match serialized
808 esl_fatal(msg);
809 }
810 p7_domain_Destroy(deserial);
811 deserial = p7_domain_Create_empty();
812 }
813 // haven't failed yet, so we've succeeded. Clean up and exit
814 free(*buf);
815 free(buf);
816 for(i = 0; i < ntrials; i++){
817 p7_domain_Destroy(serial[i]);
818 }
819 free(serial);
820 p7_domain_Destroy(deserial);
821 esl_rand64_Destroy(rng);
822
823 return;
824
825 ERROR:
826 if(buf != NULL){
827 if(*buf != NULL){
828 free(*buf);
829 }
830 free(buf);
831 }
832
833 if(serial != NULL){
834 for(i = 0; i < ntrials; i++){
835 if(serial[i] != NULL){
836 p7_domain_Destroy(serial[i]);
837 }
838 }
839 free(serial);
840 }
841
842 if(deserial != NULL){
843 p7_domain_Destroy(deserial);
844 }
845
846 esl_fatal(msg);
847
848 }
849
850 #endif // p7DOMAIN_TESTDRIVE
851
852
853 /*****************************************************************
854 * 4. Test driver
855 *****************************************************************/
856 #ifdef p7DOMAIN_TESTDRIVE
857
858 #include "esl_getopts.h"
859
860 int
861 main(int argc, char **argv)
862 {
863
864 utest_Serialize_error_conditions();
865 utest_Deserialize_error_conditions();
866 utest_Serialize(100);
867 return eslOK; // If we get here, test passed
868 }
869 #endif