Debian Codegraph @

hmmer/src/p7_hit.c

raw symbols SCIP
1 /* Functions to manipulate P7_HIT objects.
2 * Contents: 1) Serialization and Deserialization routines
3 * 2) Unit Tests
4 * 3) Test Driver
5 * NPC 2/13/19 [The soothing whir of the air filter]
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
17 #include "hmmer.h"
18
19 /*****************************************************************
20 * 1. Functions to manipulate P7_HIT objects
21 *****************************************************************/
22
23
24 /* Function: p7_hit_Create_empty
25 * Synopsis: Creates an empty P7_HIT object
26 *
27 * Purpose: Creates an empty P7_HIT object (all internal values 0, all internal pointers NULL).
28 * This is mostly useful as a way of creating a structure that a P7_HIT can be deserialized into
29 *
30 * Inputs: none
31 *
32 * Returns: The created object
33 *
34 * Throws: Returns NULL if unable to allocate memory
35 */
36 extern P7_HIT *p7_hit_Create_empty(){
37 int status;
38
39 P7_HIT *the_hit;
40
41 ESL_ALLOC(the_hit, sizeof(P7_HIT));
42
43 the_hit->name = NULL;
44 the_hit->acc = NULL;
45 the_hit->desc = NULL;
46 the_hit->window_length = 0;
47 the_hit->sortkey = 0.0;
48 the_hit->score = 0.0;
49 the_hit->pre_score = 0.0;
50 the_hit->sum_score = 0.0;
51 the_hit->lnP = 0.0;
52 the_hit->pre_lnP = 0.0;
53 the_hit->sum_lnP = 0.0;
54 the_hit->nexpected = 0.0;
55 the_hit->nregions = 0;
56 the_hit->nclustered = 0;
57 the_hit->noverlaps = 0;
58 the_hit->nenvelopes = 0;
59 the_hit->ndom = 0;
60 the_hit->flags = 0;
61 the_hit->nreported = 0;
62 the_hit->nincluded = 0;
63 the_hit->best_domain = 0;
64 the_hit->seqidx = 0;
65 the_hit->subseq_start = 0;
66 the_hit->dcl = NULL;
67 the_hit->offset = 0;
68
69 return the_hit;
70 ERROR:
71 return NULL;
72 }
73
74 /* Function: p7_hit_Destroy
75 * Synopsis: Frees all of the memory in a P7_HIT object
76 *
77 * Purpose: Frees all of the memory in a P7_HIT object, including memory in its internal data structures
78 *
79 * Inputs: obj: A pointer to the object to be destroyed
80 *
81 * Returns: Nothing
82 *
83 * Throws: Nothing
84 */
85
86 extern void p7_hit_Destroy(P7_HIT *the_hit){
87 int i;
88 if (the_hit == NULL)
89 {
90 return;
91 }
92
93 if(the_hit->name != NULL){
94 free(the_hit->name);
95 }
96
97 if(the_hit->acc != NULL){
98 free(the_hit->acc);
99 }
100
101 if(the_hit->desc != NULL){
102 free(the_hit->desc);
103 }
104
105
106
107 // need to do this manually rather than calling p7_domain_Destroy because we have an array of hits as one record
108 if(the_hit->dcl !=NULL){
109 for(i = 0; i < the_hit->ndom; i++){
110 if(the_hit->dcl[i].scores_per_pos != NULL){
111 free(the_hit->dcl[i].scores_per_pos);
112 }
113 if(the_hit->dcl[i].ad != NULL){
114 p7_alidisplay_Destroy(the_hit->dcl[i].ad);
115 }
116 }
117 }
118
119 if(the_hit->dcl != NULL){
120 free(the_hit->dcl);
121 }
122
123 free(the_hit);
124
125 return;
126 }
127
128 /* Function: p7_hit_Copy()
129 * Synopsis: Copy a hit.
130 *
131 * Purpose: Copies hit <src> to hit <dst>, where <dst> has already been
132 * allocated.
133 *
134 * Returns: <eslOK> on success.
135 *
136 * Throws: <eslEMEM> on allocation error.
137 */
138 extern int p7_hit_Copy(const P7_HIT *src, P7_HIT *dst){
139 int i;
140 int status;
141 char *name = NULL, *acc = NULL, *desc = NULL;
142 P7_DOMAIN *dcl = NULL;
143
144 // make all failible allocations to local variables first
145 if (src->name != NULL) {
146 if ((status = esl_strdup(src->name, -1, &name)) != eslOK) goto ERROR;
147 }
148 if (src->acc != NULL) {
149 if ((status = esl_strdup(src->acc, -1, &acc)) != eslOK) goto ERROR;
150 }
151 if (src->desc != NULL) {
152 if ((status = esl_strdup(src->desc, -1, &desc)) != eslOK) goto ERROR;
153 }
154 if (src->dcl != NULL) {
155 ESL_ALLOC(dcl, sizeof(P7_DOMAIN) * src->ndom);
156 for (i = 0; i < src->ndom; i++) {
157 dcl[i].ad = NULL;
158 dcl[i].scores_per_pos = NULL;
159 }
160 for (i = 0; i < src->ndom; i++) {
161 if ((status = p7_domain_Copy(&(src->dcl[i]), &(dcl[i]))) != eslOK) goto ERROR;
162 }
163 }
164
165 // update <dst> after all allocations succeeded
166 memcpy(dst, src, sizeof(P7_HIT));
167 dst->name = name;
168 dst->acc = acc;
169 dst->desc = desc;
170 dst->dcl = dcl;
171 return eslOK;
172
173 ERROR:
174 free(name);
175 free(acc);
176 free(desc);
177 if (dcl != NULL) {
178 for (i = 0; i < src->ndom; i++) {
179 free(dcl[i].ad);
180 free(dcl[i].scores_per_pos);
181 }
182 free(dcl);
183 }
184 return status;
185 }
186
187
188 /* Function: p7_HIT_Serialize
189 * Synopsis: Serializes a P7_HIT object into a stream of bytes
190 *. that can be reliably transmitted over internet sockets
191 *
192 * Purpose: Converts an architecture-dependent P7_HIT object into a contiguous stream
193 * of bytes with each field of the data structure in network byte order for transmission
194 * over sockets. The serialized byte stream may be part of a larger allocated buffer.
195 * If the provided buffer is NULL, allocates a new buffer large enough for the serialized object
196 * If the provided buffer is not large enough to hold the serialized object and its existing data, re-allocates
197 * a larger buffer
198 *
199 * Inputs: obj: A pointer to the P7_HIT object to be serialized
200 * buf: Handle to the buffer that the object should be serialized into. If *buf is NULL,
201 * a new buffer will be allocated. buf == NULL is not allowed.
202 * n: Offset (in bytes) from the start of the buffer to where the serialized object should start.
203 * nalloc: size (in bytes) of the buffer passed in buf
204 *
205 *Returns: On success: returns eslOK, sets *buf to the base of the buffer containing the object
206 * if allocation or re-allocation was requried, sets *n to the offset from the start of the buffer
207 * to the first position after the serialized object and sets *nalloc to the new size of the buffer
208 * if allocation or re-allocation was required.
209 *
210 * Throws: Returns eslEMEM if unable to allocate or re-allocate memory. Returns eslEINVAL if obj == NULL, n == NULL, buf == NULL,
211 *. or if *buf = NULL and either *n != 0 or *nalloc != 0. Returns eslFAIL if a consistency check fails.
212 */
213
214 // base size is 1 int (serialized size) + 1 byte (presence flags) longer than the sum of the fixed-width elements in the structure
215 #define SER_BASE_SIZE (10 * sizeof(int)) + (4 * sizeof(double)) + (4 * sizeof(float)) + sizeof(uint32_t) + (2 * sizeof(int64_t)) + 1
216 #define ACC_PRESENT (1 << 0)
217 #define DESC_PRESENT (1 << 1)
218
219 extern int p7_hit_Serialize(const P7_HIT *obj, uint8_t **buf, uint32_t *n, uint32_t *nalloc){
220
221 int status; // error variable used by ESL_ALLOC
222 int name_size, acc_size, desc_size;
223 uint32_t ser_size; // size of the structure when serialized
224 uint8_t *ptr; // current position within the buffer
225 uint32_t network_32bit; // hold 32-bit fields after conversion to network order
226 uint64_t network_64bit; // hold 64-bit fields after conversion to network order
227 uint8_t presence_flags = 0;
228 int i;
229
230 // check to make sure we were passed a valid pointer
231 if(obj == NULL || buf == NULL || n == NULL || ((*buf == NULL) && ((*n != 0) || (*nalloc != 0)))) {
232 // no object to serialize, nowhere to put a buffer pointer, or NULL buffer and non-zero offset or buffer size
233 return(eslEINVAL);
234 }
235
236 ser_size = SER_BASE_SIZE;
237
238 // name string is mandatory, don't need to check for it
239 name_size = strlen(obj->name) +1;
240 ser_size += name_size;
241
242 if(obj->acc != NULL){
243 acc_size = strlen(obj->acc) + 1;
244 ser_size += acc_size;
245 presence_flags += ACC_PRESENT;
246 }
247 else{
248 acc_size = 0;
249 }
250
251 if(obj->desc != NULL){
252 desc_size = strlen(obj->desc) + 1;
253 ser_size += desc_size;
254 presence_flags += DESC_PRESENT;
255 }
256 else{
257 desc_size = 0;
258 }
259
260 // Note: dcl array isn't considered part of the base object for purposes of serializing. Each of its P7_DOMAIN objects
261 // are serialized as separate objects after the serialized base object
262
263 // Now that we know how big the serialized data structure will be, determine if we have enough buffer space to hold it
264 if(*buf == NULL){ // have no buffer, so allocate one
265 ESL_ALLOC(*buf, ser_size);
266 *nalloc = ser_size;
267 }
268
269 if((*n + ser_size) > *nalloc){ //have a buffer, but it's not big enough
270 ESL_REALLOC(*buf, (*n + ser_size));
271 *nalloc = *n + ser_size;
272 }
273
274 ptr = *buf + *n; // pointer to start of region we'll serialize to
275
276 // Field 1: serialized size
277 network_32bit = esl_hton32(ser_size);
278 memcpy(ptr, &network_32bit, sizeof(int32_t));
279 ptr += sizeof(int32_t);
280
281 // Field 2: window_length
282 network_32bit = esl_hton32(obj->window_length);
283 memcpy(ptr, &network_32bit, sizeof(int32_t));
284 ptr += sizeof(int32_t);
285
286 // Field 3: sortkey
287 network_64bit = esl_hton64(*((uint64_t *) &(obj->sortkey)));
288 memcpy((void *) ptr, (void *) &network_64bit, sizeof(obj->sortkey));
289 ptr += sizeof(obj->sortkey);
290
291 // Field 4: score
292 network_32bit = esl_hton32(*((uint32_t *) &(obj->score)));
293 memcpy((void *) ptr, (void *) &network_32bit, sizeof(obj->score));
294 ptr += sizeof(obj->score);
295
296 // Field 5: pre_score
297 network_32bit = esl_hton32(*((uint32_t *) &(obj->pre_score)));
298 memcpy((void *) ptr, (void *) &network_32bit, sizeof(obj->pre_score));
299 ptr += sizeof(obj->pre_score);
300
301 // Field 6: sum_score
302 network_32bit = esl_hton32(*((uint32_t *) &(obj->sum_score)));
303 memcpy((void *) ptr, (void *) &network_32bit, sizeof(obj->sum_score));
304 ptr += sizeof(obj->sum_score);
305
306 // Field 7: lnP
307 network_64bit = esl_hton64(*((uint64_t *) &(obj->lnP)));
308 memcpy((void *) ptr, (void *) &network_64bit, sizeof(obj->lnP));
309 ptr += sizeof(obj->lnP);
310
311 // Field 8: pre_lnP
312 network_64bit = esl_hton64(*((uint64_t *) &(obj->pre_lnP)));
313 memcpy((void *) ptr, (void *) &network_64bit, sizeof(obj->pre_lnP));
314 ptr += sizeof(obj->pre_lnP);
315
316 // Field 9: sum_lnP
317 network_64bit = esl_hton64(*((uint64_t *) &(obj->sum_lnP)));
318 memcpy((void *) ptr, (void *) &network_64bit, sizeof(obj->sum_lnP));
319 ptr += sizeof(obj->sum_lnP);
320
321 // Field 10: nexpected
322 network_32bit = esl_hton32(*((uint32_t *) &(obj->nexpected)));
323 memcpy((void *) ptr, (void *) &network_32bit, sizeof(obj->nexpected));
324 ptr += sizeof(obj->nexpected);
325
326 // Field 11: nregions
327 network_32bit = esl_hton32(obj->nregions);
328 memcpy(ptr, &network_32bit, sizeof(int32_t));
329 ptr += sizeof(int32_t);
330
331 // Field 12: nclustered
332 network_32bit = esl_hton32(obj->nclustered);
333 memcpy(ptr, &network_32bit, sizeof(int32_t));
334 ptr += sizeof(int32_t);
335
336 // Field 13: noverlaps
337 network_32bit = esl_hton32(obj->noverlaps);
338 memcpy(ptr, &network_32bit, sizeof(int32_t));
339 ptr += sizeof(int32_t);
340
341 // Field 14: nenvelopes
342 network_32bit = esl_hton32(obj->nenvelopes);
343 memcpy(ptr, &network_32bit, sizeof(int32_t));
344 ptr += sizeof(int32_t);
345
346 // Field 15: ndom
347 network_32bit = esl_hton32(obj->ndom);
348 memcpy(ptr, &network_32bit, sizeof(int32_t));
349 ptr += sizeof(int32_t);
350
351 // Field 16: flags
352 network_32bit = esl_hton32(obj->flags);
353 memcpy(ptr, &network_32bit, sizeof(int32_t));
354 ptr += sizeof(int32_t);
355
356 // Field 17: nreported
357 network_32bit = esl_hton32(obj->nreported);
358 memcpy(ptr, &network_32bit, sizeof(int32_t));
359 ptr += sizeof(int32_t);
360
361 // Field 18: nincluded
362 network_32bit = esl_hton32(obj->nincluded);
363 memcpy(ptr, &network_32bit, sizeof(int32_t));
364 ptr += sizeof(int32_t);
365
366 // Field 19: best_domain
367 network_32bit = esl_hton32(obj->best_domain);
368 memcpy(ptr, &network_32bit, sizeof(int32_t));
369 ptr += sizeof(int32_t);
370
371 // Field 20: seqidx
372 network_64bit = esl_hton64(obj->seqidx);
373 memcpy(ptr, &network_64bit, sizeof(int64_t));
374 ptr += sizeof(int64_t);
375
376 // Field 21: subseq_start;
377 network_64bit = esl_hton64(obj->subseq_start);
378 memcpy(ptr, &network_64bit, sizeof(int64_t));
379 ptr += sizeof(int64_t);
380
381 // Field 22: presence flags
382 memcpy(ptr, &presence_flags, 1);
383 ptr += 1;
384
385
386 // field 23: name string
387 strcpy((char *) ptr, obj->name);
388 ptr += name_size;
389
390 // field 24: acc string, if present
391 if(obj->acc != NULL){
392 strcpy((char *) ptr, obj->acc);
393 ptr += acc_size;
394 }
395
396 //field 25: desc string, if present
397 if(obj->desc != NULL){
398 strcpy((char *) ptr, obj->desc);
399 ptr += desc_size;
400 }
401
402 // sanity check
403 if(ptr -(*buf + *n) != ser_size){
404 ESL_EXCEPTION(eslFAIL, "Size of serialized object did not match expectation in p7_hit_Serialize\n");
405 }
406
407 *n = ptr - *buf; // update n to point to end of serialized region
408
409 for(i = 0; i < obj->ndom; i++){
410 status = p7_domain_Serialize(&(obj->dcl[i]), buf, n, nalloc);
411 if(status != eslOK){
412 return status;
413 }
414 }
415
416 return eslOK;
417 ERROR:
418 return eslEMEM;
419 }
420
421 /* Function: p7_hit_Deserialize
422 * Synopsis: Derializes a P7_HIT object from a stream of bytes in network order into
423 * a valid data structure
424 *
425 * Purpose: Deserializes a serialized P7_HIT object from
426 *. buf starting at position *n.
427 *
428 * Inputs: buf: the buffer that the object should be de-serialized from
429 * pos: a pointer to the offset from the start of buf to the beginning of the object
430 * ret_obj: a P7_HIT structure to deserialize the object into. May not be NULL. May either be an
431 * "empty" object created with p7_hit_Create_empty, or a P7_HIT object containing valid data
432 *
433 * Returns: On success: returns eslOK, deserializes the P7_HIT object into ret_object, and updates
434 *. n to point to the position after the end of the P7_HIT object.
435 *
436 * Throws: Returns eslEINVAL if ret_obj == NULL, buf == NULL, or n == NULL. Returnts eslEMEM if unable to allocate
437 * required memory in ret_obj. Returns eslFAIL if an consistency check fails.
438 */
439 extern int p7_hit_Deserialize(const uint8_t *buf, uint32_t *n, P7_HIT *ret_obj){
440
441 uint8_t *ptr;
442 uint64_t network_64bit; // holds 64-bit values in network order
443 uint64_t host_64bit; //variable to hold 64-bit values after conversion to host order
444 uint32_t network_32bit; // holds 64-bit values in network order
445 uint32_t host_32bit; //variable to hold 64-bit values after conversion to host order
446 uint32_t obj_size; // How much space does the variable-length portion of the serialized object take up?
447 int status, string_length;
448 uint8_t presence_flags;
449 int i;
450 if (ret_obj == NULL || buf == NULL || n == NULL)
451 {
452 return eslEINVAL;
453 }
454
455 ptr = (uint8_t *) buf + *n;
456
457 //First field: Size of the serialized object. Copy out of buffer into scalar variable to deal with memory alignment, convert to
458 // host machine order
459 memcpy(&network_32bit, ptr, sizeof(uint32_t)); // Grab the bytes out of the buffer
460 obj_size = esl_ntoh32(network_32bit);
461 ptr += sizeof(uint32_t);
462
463 //Field 2: window_length
464 memcpy(&network_32bit, ptr, sizeof(uint32_t)); // Grab the bytes out of the buffer
465 ret_obj->window_length = esl_ntoh32(network_32bit);
466 ptr += sizeof(uint32_t);
467
468 //Field 3: sortkey
469 memcpy(&network_64bit, ptr, sizeof(double)); // Grab the bytes out of the buffer
470 host_64bit = esl_ntoh64(network_64bit);
471 ret_obj->sortkey = *((double *) &host_64bit);
472 ptr += sizeof(double);
473
474 //Field 4: score
475 memcpy(&network_32bit, ptr, sizeof(float)); // Grab the bytes out of the buffer
476 host_32bit = esl_ntoh32(network_32bit);
477 ret_obj->score = *((float *) &host_32bit);
478 ptr += sizeof(float);
479
480 //Field 5: pre_score
481 memcpy(&network_32bit, ptr, sizeof(float)); // Grab the bytes out of the buffer
482 host_32bit = esl_ntoh32(network_32bit);
483 ret_obj->pre_score = *((float *) &host_32bit);
484 ptr += sizeof(float);
485
486 //Field 6: sum_score
487 memcpy(&network_32bit, ptr, sizeof(float)); // Grab the bytes out of the buffer
488 host_32bit = esl_ntoh32(network_32bit);
489 ret_obj->sum_score = *((float *) &host_32bit);
490 ptr += sizeof(float);
491
492 //Field 7: lnP
493 memcpy(&network_64bit, ptr, sizeof(double)); // Grab the bytes out of the buffer
494 host_64bit = esl_ntoh64(network_64bit);
495 ret_obj->lnP = *((double *) &host_64bit);
496 ptr += sizeof(double);
497
498 //Field 8: pre_lnP
499 memcpy(&network_64bit, ptr, sizeof(double)); // Grab the bytes out of the buffer
500 host_64bit = esl_ntoh64(network_64bit);
501 ret_obj->pre_lnP = *((double *) &host_64bit);
502 ptr += sizeof(double);
503
504 //Field 9: sum_lnP
505 memcpy(&network_64bit, ptr, sizeof(double)); // Grab the bytes out of the buffer
506 host_64bit = esl_ntoh64(network_64bit);
507 ret_obj->sum_lnP = *((double *) &host_64bit);
508 ptr += sizeof(double);
509
510 //Field 10: nexpected
511 memcpy(&network_32bit, ptr, sizeof(float)); // Grab the bytes out of the buffer
512 host_32bit = esl_ntoh32(network_32bit);
513 ret_obj->nexpected = *((float *) &host_32bit);
514 ptr += sizeof(float);
515
516 //Field 11: nregions
517 memcpy(&network_32bit, ptr, sizeof(uint32_t)); // Grab the bytes out of the buffer
518 ret_obj->nregions = esl_ntoh32(network_32bit);
519 ptr += sizeof(uint32_t);
520
521 //Field 12: nclustered
522 memcpy(&network_32bit, ptr, sizeof(uint32_t)); // Grab the bytes out of the buffer
523 ret_obj->nclustered = esl_ntoh32(network_32bit);
524 ptr += sizeof(uint32_t);
525
526 //Field 13: noverlaps
527 memcpy(&network_32bit, ptr, sizeof(uint32_t)); // Grab the bytes out of the buffer
528 ret_obj->noverlaps = esl_ntoh32(network_32bit);
529 ptr += sizeof(uint32_t);
530
531 //Field 14: nenvelopes
532 memcpy(&network_32bit, ptr, sizeof(uint32_t)); // Grab the bytes out of the buffer
533 ret_obj->nenvelopes = esl_ntoh32(network_32bit);
534 ptr += sizeof(uint32_t);
535
536 //Field 15: ndom
537 memcpy(&network_32bit, ptr, sizeof(uint32_t)); // Grab the bytes out of the buffer
538 ret_obj->ndom = esl_ntoh32(network_32bit);
539 ptr += sizeof(uint32_t);
540
541 //Field 16: flags
542 memcpy(&network_32bit, ptr, sizeof(uint32_t)); // Grab the bytes out of the buffer
543 ret_obj->flags = esl_ntoh32(network_32bit);
544 ptr += sizeof(uint32_t);
545
546 //Field 17: nreported
547 memcpy(&network_32bit, ptr, sizeof(uint32_t)); // Grab the bytes out of the buffer
548 ret_obj->nreported = esl_ntoh32(network_32bit);
549 ptr += sizeof(uint32_t);
550
551 //Field 18: nincluded
552 memcpy(&network_32bit, ptr, sizeof(uint32_t)); // Grab the bytes out of the buffer
553 ret_obj->nincluded = esl_ntoh32(network_32bit);
554 ptr += sizeof(uint32_t);
555
556 //Field 19: best_domain
557 memcpy(&network_32bit, ptr, sizeof(uint32_t)); // Grab the bytes out of the buffer
558 ret_obj->best_domain = esl_ntoh32(network_32bit);
559 ptr += sizeof(uint32_t);
560
561 //Field 20: seqidx
562 memcpy(&network_64bit, ptr, sizeof(uint64_t)); // Grab the bytes out of the buffer
563 ret_obj->seqidx = esl_ntoh64(network_64bit);
564 ptr += sizeof(uint64_t);
565
566 //Field 21: subseq_start
567 memcpy(&network_64bit, ptr, sizeof(uint64_t)); // Grab the bytes out of the buffer
568 ret_obj->subseq_start = esl_ntoh64(network_64bit);
569 ptr += sizeof(uint64_t);
570
571 //Field 22: presence flags
572 memcpy(&presence_flags, ptr, 1); // Grab the bytes out of the buffer
573 ptr += 1;
574
575 //Field 23: name string
576 string_length = strlen((char *) ptr) +1;
577
578 if(ret_obj->name != NULL){
579 free(ret_obj->name);
580 }
581 ESL_ALLOC(ret_obj->name, string_length);
582
583 strcpy(ret_obj->name, (char *) ptr);
584 ptr += string_length;
585
586 //field 24: acc, if present
587 if(ret_obj->acc != NULL){
588 free(ret_obj->acc);
589 }
590
591 if(presence_flags & ACC_PRESENT){
592 string_length = strlen((char *) ptr) +1;
593
594 ESL_ALLOC(ret_obj->acc, string_length);
595
596 strcpy(ret_obj->acc, (char *) ptr);
597 ptr += string_length;
598 }
599 else{
600 ret_obj->acc = NULL;
601 }
602
603 //field 25: desc, if present
604 if(ret_obj->desc != NULL){
605 free(ret_obj->desc);
606 }
607
608 if(presence_flags & DESC_PRESENT){
609 string_length = strlen((char *) ptr) +1;
610
611 ESL_ALLOC(ret_obj->desc, string_length);
612
613 strcpy(ret_obj->desc, (char *) ptr);
614 ptr += string_length;
615 }
616 else{
617 ret_obj->desc = NULL;
618 }
619
620 // sanity check
621 if((ptr - (buf + *n)) !=obj_size){
622 ESL_EXCEPTION(eslFAIL, "Error: Size of serialized object did not match expected in p7_hit_Deserialize\n");
623 }
624
625 ESL_ALLOC(ret_obj->dcl, ret_obj->ndom * sizeof(P7_DOMAIN));
626
627 *n = ptr- buf; // reset n to point just past fixed-length fields
628
629 for(i = 0; i < ret_obj->ndom; i++){
630 ret_obj->dcl[i].scores_per_pos = NULL; // set internal pointers to known values so that domain_Deserialize does the right thing
631 ret_obj->dcl[i].ad = NULL;
632 int ret_code = p7_domain_Deserialize(buf, n, &(ret_obj->dcl[i]));
633 if (ret_code != eslOK){
634 return ret_code;
635 }
636 }
637
638 return eslOK;
639 ERROR:
640 return eslEMEM;
641 }
642
643 /*****************************************************************
644 * 2. Debugging Functions
645 *****************************************************************/
646
647 /* Function: p7_hit_TestSample
648 * Synopsis: Creates a P7_HIT object that contains random data.
649 *
650 * Purpose: Creates a P7_HIT object that contains random data. This data will be syntactically correct,
651 * but is not intended to be in any way a "reasonable" hit. For example, the number of P7_DOMAIN
652 * objects in the P7_HIT objects will match the value of the object's ndom field, but the vales of the
653 * object's score, pre_score, and sum_score fields may not be consistent with each other.
654 *
655 * Inputs: rng: the random-number generator to use in creating this object.
656 * ret_obj: pointer that is used to return the created object
657 *
658 * Returns: eslOK, and the created object in ret_obj
659 *
660 * Throws: Returns eslEMEM if unable to allocate or re-allocate memory. Returns eslEINVAL if ret_obj == NULL
661 */
662 extern int p7_hit_TestSample(ESL_RAND64 *rng, P7_HIT **ret_obj){
663 int status, i;
664 int string_length;
665 if(ret_obj == NULL){
666 return eslEINVAL;
667 }
668
669 ESL_RANDOMNESS *rng32; // hack until esl_rsq_Sample is updated to rand64
670 rng32 = esl_randomness_Create(0);
671
672 // avoid memory leaks if we're passed a pointer to an existing object
673 if(*ret_obj != NULL){
674 p7_hit_Destroy(*ret_obj);
675 }
676
677 ESL_ALLOC(*ret_obj, sizeof(P7_HIT));
678
679 P7_HIT *the_obj = *ret_obj; // get a convenience pointer
680
681
682 //always have a name string
683 string_length = esl_rand64_Roll(rng, 100) + 1;
684
685 // generate a random string of printable characters
686 ESL_ALLOC(the_obj->name, string_length +1); // +1 for end of string char
687 esl_rsq_Sample(rng32, eslRSQ_SAMPLE_PRINT, string_length, &(the_obj->name));
688
689
690 // 50% chance of acc string
691 if(esl_rand64_Roll(rng, 2) == 0){
692 string_length = esl_rand64_Roll(rng, 100) + 1;
693
694 // generate a random string of printable characters
695 ESL_ALLOC(the_obj->acc, string_length + 1);
696 esl_rsq_Sample(rng32, eslRSQ_SAMPLE_PRINT, string_length, &(the_obj->acc));
697 }
698 else{
699 the_obj->acc = NULL;
700 }
701
702 // 50% chance of desc string
703 if(esl_rand64_Roll(rng, 2) == 0){
704 string_length = esl_rand64_Roll(rng, 100) + 1;
705
706 // generate a random string of printable characters
707 ESL_ALLOC(the_obj->desc, string_length+1);
708 esl_rsq_Sample(rng32, eslRSQ_SAMPLE_PRINT, string_length, &(the_obj->desc));
709 }
710 else{
711 the_obj->desc = NULL;
712 }
713
714 the_obj->window_length = (int) esl_rand64(rng);
715 the_obj->sortkey = esl_rand64_double(rng);
716 the_obj->score = (float) esl_rand64_double(rng);
717 the_obj->pre_score = (float) esl_rand64_double(rng);
718 the_obj->sum_score = (float) esl_rand64_double(rng);
719 the_obj->lnP = esl_rand64_double(rng);
720 the_obj->pre_lnP = esl_rand64_double(rng);
721 the_obj->sum_lnP = esl_rand64_double(rng);
722 the_obj->nexpected = (float) esl_rand64_double(rng);
723 the_obj->nregions = (int) esl_rand64(rng);
724 the_obj->nclustered = (int) esl_rand64(rng);
725 the_obj->noverlaps = (int) esl_rand64(rng);
726 the_obj->nenvelopes = (int) esl_rand64(rng);
727 the_obj->ndom = (esl_rand64(rng) % 10) + 1; // limit this to small positive numbers to keep the size of the dcl array
728 // under control
729
730 the_obj->flags = (esl_rand64_Roll(rng, 31)); // This field has limited range, keep values within that range
731 the_obj->nreported = (int) esl_rand64(rng);
732 the_obj->nincluded = (int) esl_rand64(rng);
733 the_obj->best_domain = esl_rand64(rng) % the_obj->ndom; // keep this value within the set of domains in this hit
734
735 the_obj->seqidx = esl_rand64(rng);
736 the_obj->subseq_start = esl_rand64(rng);
737
738 ESL_ALLOC(the_obj->dcl, the_obj->ndom *sizeof(P7_DOMAIN));
739
740 P7_DOMAIN **handle, *ptr;
741 handle = &ptr;
742 for(i = 0; i < the_obj->ndom; i++){
743 ptr = &(the_obj->dcl[i]);
744 p7_domain_TestSample(rng, handle);
745 }
746 esl_randomness_Destroy(rng32);
747 return eslOK; // nothing went wrong
748
749 ERROR:
750 return eslEMEM;
751 }
752
753
754
755 /* Function: p7_hit_Compare
756 * Synopsis: Compares two P7_HIT objects for equality within the specified tolerances
757 *
758 * Purpose: Compares two P7_HIT objects. Integer fields are compared for equality. Floating-point fields are
759 * compared for equality within the specified relative and absolute tolerances
760 *
761 * Inputs: first: The first object to be compared
762 * second: The second object to be compared
763 * atol: The absolute tolerance to be used when comparing floating-point fields
764 * rtol: The relative tolerance to be used when comparing floating-point fields
765 *
766 * Returns: eslOK if the P7_HIT inputs match, eslFAIL otherwise
767 *
768 * Throws: Nothing
769 */
770 extern int p7_hit_Compare(P7_HIT *first, P7_HIT *second, double atol, double rtol){
771 int i;
772 if (strcmp(first->name, second->name) != 0)
773 {
774 return eslFAIL;
775 }
776
777 if(((first->acc == NULL) && (second->acc != NULL)) || ((first->acc != NULL) && (second->acc == NULL))){
778 return eslFAIL; // hits can't be the same if one of them has a string field that the other doesn't
779 }
780
781 if((first->acc != NULL) && (second->acc != NULL) && (strcmp(first->acc, second->acc) != 0)){
782 //both hits have acc strings but they don't match
783 return eslFAIL;
784 }
785 // The remaining option is first->acc == NULL and second->acc == NULL, which counts as a match in that field
786
787 if(((first->desc == NULL) && (second->desc != NULL)) || ((first->desc != NULL) && (second->desc == NULL))){
788 return eslFAIL; // hits can't be the same if one of them has a string field that the other doesn't
789 }
790
791 if((first->desc != NULL) && (second->desc != NULL) && (strcmp(first->desc, second->desc) != 0)){
792 //both hits have acc strings but they don't match
793 return eslFAIL;
794 }
795 // The remaining option is first->desc == NULL and second->desc == NULL, which counts as a match in that field
796
797 if(first->window_length != second->window_length){
798 return eslFAIL;
799 }
800
801 if(esl_DCompare(first->sortkey, second->sortkey, atol, rtol) != eslOK){
802 return eslFAIL;
803 }
804
805 if(esl_FCompare(first->score, second->score, (float) atol, (float) rtol) != eslOK){
806 return eslFAIL;
807 }
808
809 if(esl_FCompare(first->pre_score, second->pre_score, (float) atol, (float) rtol) != eslOK){
810 return eslFAIL;
811 }
812
813 if(esl_FCompare(first->sum_score, second->sum_score, (float) atol, (float) rtol) != eslOK){
814 return eslFAIL;
815 }
816
817 if(esl_DCompare(first->lnP, second->lnP, atol, rtol) != eslOK){
818 return eslFAIL;
819 }
820
821 if(esl_DCompare(first->pre_lnP, second->pre_lnP, atol, rtol) != eslOK){
822 return eslFAIL;
823 }
824
825 if(esl_DCompare(first->sum_lnP, second->sum_lnP, atol, rtol) != eslOK){
826 return eslFAIL;
827 }
828
829 if(first->nexpected != second->nexpected){
830 return eslFAIL;
831 }
832
833 if(first->nregions != second->nregions){
834 return eslFAIL;
835 }
836
837 if(first->nclustered != second->nclustered){
838 return eslFAIL;
839 }
840
841 if(first->noverlaps != second->noverlaps){
842 return eslFAIL;
843 }
844
845 if(first->nenvelopes != second->nenvelopes){
846 return eslFAIL;
847 }
848
849 if(first->ndom != second->ndom){
850 return eslFAIL;
851 }
852
853 if(first->flags != second->flags){
854 return eslFAIL;
855 }
856
857 if(first->nreported != second->nreported){
858 return eslFAIL;
859 }
860
861 if(first->nincluded != second->nincluded){
862 return eslFAIL;
863 }
864
865 if(first->best_domain != second->best_domain){
866 return eslFAIL;
867 }
868
869 if(first->seqidx != second->seqidx){
870 return eslFAIL;
871 }
872
873 if(first->subseq_start != second->subseq_start){
874 return eslFAIL;
875 }
876
877 for(i = 0; i < first->ndom; i++){
878 if(p7_domain_Compare(&(first->dcl[i]), &(second->dcl[i]), atol, rtol) != eslOK){
879 return eslFAIL;
880 }
881 }
882
883 // ignore offset field -- it is vestigal and will be going away
884 return eslOK; // If we get here without finding a miss-match, the hits contain the same values
885 }
886 /*****************************************************************
887 * 3. Unit tests
888 *****************************************************************/
889
890
891
892
893 #ifdef p7HIT_TESTDRIVE
894 // Test that the _Serialize() function generates the correct errors when passed invalid arguments
895 static void utest_Serialize_error_conditions(){
896 int status; // Easel error code variable
897 P7_HIT *foo;
898 uint8_t **buf;
899 uint32_t n;
900 uint32_t nalloc;
901 ESL_RAND64 *rng;
902
903 char msg[] = "utest_Serialize_error_conditions failed";
904 rng = esl_rand64_Create(0);
905 n = 0;
906 nalloc = 0;
907 foo = p7_hit_Create_empty();
908 // Test 1: _Serialize returns error if passed NULL buffer
909 buf = NULL;
910
911 if(p7_hit_Serialize(foo, buf, &n, &nalloc) != eslEINVAL){
912 esl_fatal(msg);
913 }
914 else{
915 //printf("null buffer check passed\n");
916 }
917
918 ESL_ALLOC(buf, sizeof(uint8_t *)); // set buf to valid value
919 *buf = NULL;
920
921 // Test 2: error on NULL n ptr
922 if(p7_hit_Serialize(foo, buf, NULL, &nalloc) != eslEINVAL){
923 esl_fatal(msg);
924 }
925 else{
926 //printf("invalid n check passed\n");
927 }
928
929 // Test 3: error on NULL object ptr
930 if(p7_hit_Serialize(NULL, buf, &n, &nalloc) != eslEINVAL){
931 esl_fatal(msg);
932 }
933 else{
934 //printf("invalid object check passed\n");
935 }
936
937 // Test 4: n != 0 and *buf == NULL
938 n = 3;
939 if(p7_hit_Serialize(foo, buf, &n, &nalloc) != eslEINVAL){
940 esl_fatal(msg);
941 }
942 else{
943 //printf("Non-zero n with NULL buffer check passed\n");
944 }
945
946 // Test 5: Nalloc != 0 and *buf == NULL
947 n = 0;
948 nalloc = 10;
949 if(p7_hit_Serialize(foo, buf, &n, &nalloc) != eslEINVAL){
950 esl_fatal(msg);
951 }
952 else{
953 //printf("Non-zero nalloc with NULL buffer check passed\n");
954 }
955
956 if(buf !=NULL && *buf != NULL){
957 free(*buf);
958 }
959 if(buf != NULL){
960 free(buf);
961 }
962
963 p7_hit_Destroy(foo);
964 esl_rand64_Destroy(rng);
965 return;
966
967 ERROR:
968 if(foo != NULL){
969 p7_hit_Destroy(foo);
970 }
971
972 if(buf !=NULL && *buf != NULL){
973 free(*buf);
974 }
975 if(buf != NULL){
976 free(buf);
977 }
978 esl_fatal(msg);
979 }
980
981
982 static void utest_Deserialize_error_conditions(){
983 P7_HIT *sampled = NULL; // sampled alidisplay that we'll serialze
984 P7_HIT *deserial = NULL; // alidisplay to hold the deserialized object
985 char msg[]="utest_Deserialize_error_conditions failed";
986 uint8_t *buf = NULL;
987 uint32_t n = 0, nalloc = 0;
988 ESL_RAND64 *rng = esl_rand64_Create(0);
989 deserial = p7_hit_Create_empty();
990 if(deserial == NULL){
991 esl_fatal(msg);
992 }
993
994 p7_hit_TestSample(rng, &sampled);
995 if(sampled == NULL){
996 esl_fatal(msg);
997 }
998
999 if(p7_hit_Serialize(sampled, &buf, &n, &nalloc) != eslOK){ // serialize an object to deserialize
1000 esl_fatal(msg);
1001 }
1002
1003 // Test 1: error on buf == NULL;
1004 if(p7_hit_Deserialize(NULL, &n, deserial) != eslEINVAL){
1005 esl_fatal(msg);
1006 }
1007 //printf("Test 1 passed\n");
1008
1009 // Test 2: error on n == NULL
1010 if(p7_hit_Deserialize(buf, NULL, deserial) != eslEINVAL){
1011 esl_fatal(msg);
1012 }
1013 //printf("Test 2 passed\n");
1014
1015 // Test 3: error on serialized object == NULL
1016 if(p7_hit_Deserialize(buf, &n, NULL) != eslEINVAL){
1017 esl_fatal(msg);
1018 }
1019 //printf("Test 3 passed\n");
1020 free(buf);
1021 p7_hit_Destroy(deserial);
1022 p7_hit_Destroy(sampled);
1023 esl_rand64_Destroy(rng);
1024 return;
1025 }
1026
1027 static void utest_Serialize(int ntrials){
1028 int i;
1029 uint8_t **buf;
1030 uint32_t n;
1031 uint32_t nalloc;
1032 P7_HIT **serial=NULL, *deserial=NULL;
1033 int status;
1034 char msg[] = "utest_Serialize failed";
1035
1036 ESL_ALLOC(buf, sizeof(uint8_t *));
1037 *buf = NULL;
1038 n = 0;
1039 nalloc = 0;
1040
1041 ESL_ALLOC(serial, ntrials * sizeof(P7_HIT *));
1042 for(i = 0; i< ntrials; i++){
1043 serial[i] = NULL;
1044 }
1045
1046 ESL_RAND64 *rng = esl_rand64_Create(0);
1047
1048 for(i = 0; i < ntrials; i++){
1049 if(p7_hit_TestSample(rng, &(serial[i])) != eslOK){
1050 esl_fatal(msg);
1051 }
1052 if(p7_hit_Serialize(serial[i], buf, &n, &nalloc) != eslOK){
1053 esl_fatal(msg);
1054 }
1055 }
1056
1057 n = 0; // reset to start of buffer
1058
1059 deserial = p7_hit_Create_empty();
1060 if(deserial == NULL){
1061 esl_fatal(msg);
1062 }
1063
1064 for(i = 0; i < ntrials; i++){
1065 if(p7_hit_Deserialize(*buf, &n, deserial) != eslOK){
1066 esl_fatal(msg);
1067 }
1068 if(p7_hit_Compare(serial[i], deserial, 1e-4, 1e-4) != eslOK){ // deserialized structure didn't match serialized
1069 esl_fatal(msg);
1070 }
1071 p7_hit_Destroy(deserial);
1072 deserial = p7_hit_Create_empty();
1073
1074 }
1075 // haven't failed yet, so we've succeeded. Clean up and exit
1076 free(*buf);
1077 free(buf);
1078 for(i = 0; i < ntrials; i++){
1079 p7_hit_Destroy(serial[i]);
1080 }
1081 free(serial);
1082 p7_hit_Destroy(deserial);
1083 esl_rand64_Destroy(rng);
1084 return;
1085
1086 ERROR:
1087 if(buf != NULL){
1088 if(*buf != NULL){
1089 free(*buf);
1090 }
1091 free(buf);
1092 }
1093
1094 if(serial != NULL){
1095 for(i = 0; i < ntrials; i++){
1096 if(serial[i] != NULL){
1097 p7_hit_Destroy(serial[i]);
1098 }
1099 }
1100 free(serial);
1101 }
1102
1103 if(deserial == NULL){
1104 p7_hit_Destroy(deserial);
1105 }
1106
1107 esl_fatal(msg);
1108
1109 }
1110 #endif
1111
1112 /*****************************************************************
1113 * 3. Test Driver
1114 *****************************************************************/
1115 #ifdef p7HIT_TESTDRIVE
1116
1117 int
1118 main(int argc, char **argv)
1119 {
1120
1121 utest_Serialize_error_conditions();
1122 utest_Deserialize_error_conditions();
1123 utest_Serialize(100);
1124 return eslOK; // If we get here, test passed
1125 }
1126
1127 #endif