Debian Codegraph @

hmmer/src/fm_general.c

raw symbols SCIP
1 /* Architecture-independent functions used in FM-index range compuations
2 * and list management.
3 *
4 * Contents:
5 * 1. List management
6 * 2. Interval / range computation
7 * 3. Functions related to the original sequence
8 * 4. FM data initialization, configuration, and reading from file
9 */
10 #include <p7_config.h>
11
12 #include "easel.h"
13 #include "esl_getopts.h"
14 #include "hmmer.h"
15
16 /* Function: fm_initSeeds()
17 *
18 * Synopsis: initialize the object used to store a list of seed diagonals
19 *
20 * Returns: eslEMEM in event of allocation failure, otherwise eslOK
21 */
22 int
23 fm_initSeeds (FM_DIAGLIST *list) {
24 int status;
25 list->size = 1000;
26 ESL_ALLOC(list->diags, list->size * sizeof(FM_DIAG));
27 list->count = 0;
28
29 return eslOK;
30
31 ERROR:
32 return eslEMEM;
33
34 }
35
36 /* Function: fm_newSeed()
37 *
38 * Synopsis: return a pointer to the next seed element on the list,
39 * increasing the size of the list, if necessary.
40 *
41 * Returns: NULL in event of allocation failure, otherwise pointer to
42 * the next seed diagonal
43 */
44 FM_DIAG *
45 fm_newSeed (FM_DIAGLIST *list) {
46 int status;
47
48 if (list->count == list->size) {
49 list->size *= 4;
50 ESL_REALLOC(list->diags, list->size * sizeof(FM_DIAG));
51 }
52 list->count++;
53 return list->diags + (list->count - 1);
54
55 ERROR:
56 return NULL;
57 }
58
59
60
61 /* Function: fm_initAmbiguityList()
62 *
63 * Synopsis: initialize the object used to store a list of ambiguity ranges
64 *
65 * Returns: eslEMEM in event of allocation failure, otherwise eslOK
66 */
67 int
68 fm_initAmbiguityList (FM_AMBIGLIST *list) {
69
70 int status;
71
72 list->size = 1000;
73
74 ESL_ALLOC(list->ranges, list->size * sizeof(FM_INTERVAL));
75 if (list->ranges == NULL )
76 esl_fatal("unable to allocate memory to store FM ambiguity data\n");
77
78 list->count = 0;
79
80 return eslOK;
81
82 ERROR:
83 return eslEMEM;
84
85 }
86
87 /* Function: fm_addAmbiguityRange()
88 *
89 * Synopsis: return a pointer to the next seed element on the list,
90 * increasing the size of the list, if necessary.
91 *
92 * Returns: NULL in event of allocation failure, otherwise pointer to
93 * the next seed diagonal
94 */
95 int
96 fm_addAmbiguityRange (FM_AMBIGLIST *list, uint32_t start, uint32_t stop) {
97 int status;
98
99 if (list->count == list->size) {
100 list->size *= 4;
101 ESL_REALLOC(list->ranges, list->size * sizeof(FM_INTERVAL));
102 }
103
104 list->ranges[list->count].lower = start;
105 list->ranges[list->count].upper = stop;
106
107 list->count++;
108
109 return eslOK;
110
111 ERROR:
112 return eslFAIL;
113 }
114
115
116
117
118
119 /*********************************************************************
120 *# 2. Interval / range computation
121 *********************************************************************/
122
123 /* Function: fm_updateIntervalForward()
124 *
125 * Synopsis: Implement Algorithm 4 (i371) of Simpson (Bioinformatics 2010)
126 *
127 * Purpose:
128 *
129 * Returns: eslOK
130 */
131 int
132 fm_updateIntervalForward( const FM_DATA *fm, const FM_CFG *cfg, char c, FM_INTERVAL *interval_bk, FM_INTERVAL *interval_f) {
133 uint32_t occLT_l, occLT_u, occ_l, occ_u;
134
135 fm_getOccCountLT (fm, cfg, interval_bk->lower - 1, c, &occ_l, &occLT_l);
136 fm_getOccCountLT (fm, cfg, interval_bk->upper, c, &occ_u, &occLT_u);
137
138 interval_f->lower += (occLT_u - occLT_l);
139 interval_f->upper = interval_f->lower + (occ_u - occ_l) - 1;
140
141 interval_bk->lower = abs((int)(fm->C[(int)c])) + occ_l;
142 interval_bk->upper = abs((int)(fm->C[(int)c])) + occ_u - 1;
143
144 return eslOK;
145 }
146
147 /* Function: getSARangeForward()
148 * Synopsis: For a given query sequence, find its interval in the FM-index, using forward search
149 * Purpose: Implement Algorithm 4 (i371) of Simpson (Bioinformatics 2010). It's the forward
150 * search on a bi-directional BWT, as described by Lam 2009.
151 * All the meat is in the method of counting characters - bwt_getOccCount, which
152 * depends on compilation choices.
153 *
154 * Note: it seems odd, but is correct, that the fm-index passed in to this function
155 * is the backward index corresponding to the forward index into which I want to
156 * do a forward search
157 */
158 int
159 fm_getSARangeForward( const FM_DATA *fm, FM_CFG *cfg, char *query, char *inv_alph, FM_INTERVAL *interval)
160 {
161
162 int i=0;
163 FM_INTERVAL interval_bk;
164
165 uint8_t c = inv_alph[(int)query[0]];
166 interval->lower = interval_bk.lower = abs((int)(fm->C[c]));
167 interval->upper = interval_bk.upper = abs((int)(fm->C[c+1]))-1;
168
169
170 while (interval_bk.lower>=0 && interval_bk.lower <= interval_bk.upper) {
171 c = query[++i];
172 if (c == '\0') // end of query - the current range defines the hits
173 break;
174
175 c = inv_alph[c];
176 fm_updateIntervalForward( fm, cfg, c, &interval_bk, interval);
177 cfg->occCallCnt+=2;
178 }
179
180 return eslOK;
181 }
182
183
184 int
185 fm_updateIntervalReverse( const FM_DATA *fm, const FM_CFG *cfg, char c, FM_INTERVAL *interval) {
186 int count1, count2;
187 //TODO: counting in these calls will often overlap
188 // - might get acceleration by merging to a single redundancy-avoiding call
189 count1 = fm_getOccCount (fm, cfg, interval->lower-1, c);
190 count2 = fm_getOccCount (fm, cfg, interval->upper, c);
191
192 interval->lower = abs((int)(fm->C[(int)c])) + count1;
193 interval->upper = abs((int)(fm->C[(int)c])) + count2 - 1;
194
195 return eslOK;
196 }
197
198
199
200 /* Function: getSARangeReverse()
201 * Synopsis: For a given query sequence, find its interval in the FM-index, using backward search
202 * Purpose: Implement Algorithm 3.6 (p17) of Firth paper (A Comparison of BWT Approaches
203 * to String Pattern Matching). This is what Simpson and Lam call "Reverse Search".
204 * All the meat is in the method of counting characters - bwt_getOccCount, which
205 * depends on compilation choices.
206 */
207 int
208 fm_getSARangeReverse( const FM_DATA *fm, FM_CFG *cfg, char *query, char *inv_alph, FM_INTERVAL *interval)
209 {
210
211
212 int i=0;
213
214 char c = inv_alph[(int)query[0]];
215 interval->lower = abs((int)(fm->C[(int)c]));
216 interval->upper = abs((int)(fm->C[(int)c+1]))-1;
217
218 while (interval->lower>=0 && interval->lower <= interval->upper) {
219 c = query[++i];
220 if (c == '\0') // end of query - the current range defines the hits
221 break;
222
223 c = inv_alph[(int)c];
224
225 fm_updateIntervalReverse(fm, cfg, c, interval);
226
227 cfg->occCallCnt+=2;
228 }
229
230 return eslOK;
231 }
232
233
234
235 /*********************************************************************
236 *# 3. Functions related to the original sequence
237 *********************************************************************/
238
239
240 /* Function: getChar()
241 * Synopsis: Find the character c residing at a given position in the BWT.
242 * Purpose: This method must account for possible string compression, either
243 * 4 characters in one byte for a 4-letter DNA/RNA alphabet, 2 chars
244 * per byte for a 15-letter alphabet of DNA/RNA with ambiguity codes,
245 * or one char per byte for amino acids.
246 */
247 uint8_t
248 fm_getChar(uint8_t alph_type, int j, const uint8_t *B )
249 {
250 uint8_t c = -1;
251
252 if (alph_type == fm_DNA /* || alph_type == fm_RNA */) {
253 /*
254 * B[j/4] is the byte of B in which j is found
255 *
256 * Let j' be the final two bits of j (j&0x2)
257 * The char bits are the two starting at position 2*j'.
258 * Without branching, grab them by shifting B[j/4] right 6-2*j' bits,
259 * then masking to keep the final two bits
260 */
261 c = (B[j/4] >> ( 0x6 - ((j&0x3)*2) ) & 0x3);
262 /*
263 } else if (alph_type == fm_DNA_full ) {
264 c = (B[j/2] >> (((j&0x1)^0x1)*4) ) & 0xf; //unpack the char: shift 4 bits right if it's odd, then mask off left bits in any case
265 */
266 } else { // amino
267 c = B[j];
268 }
269
270 return c;
271 }
272
273
274
275 /* Function: fm_findOverlappingAmbiguityBlock()
276 * Synopsis: Search in the meta->ambig_list array for the first
277 * ambiguity range starting after <start>. If that index
278 * comes before <end>, return it. Otherwise, return -1.
279 */
280 int32_t
281 fm_findOverlappingAmbiguityBlock (const FM_DATA *fm, const FM_METADATA *meta, uint32_t start, uint32_t end)
282 {
283
284 int lo = fm->ambig_offset;
285 int hi = lo + fm->ambig_cnt - 1;
286 int mid;
287 FM_INTERVAL *ranges = meta->ambig_list->ranges;
288
289 // (1) Search in the meta->ambig_list array for the last ambiguity range
290 // ending before <start>, using binary search, first handling edge cases:
291 if (hi <= lo) return hi; // either 0 or -1
292 if (ranges[lo].lower > end) return -1;
293 if (ranges[hi].upper < start) return -1;
294
295 while (lo < hi) {
296 mid = (lo + hi) / 2; // round down
297 if (ranges[mid].lower < start) lo = mid + 1; // too far left
298 else hi = mid; // might be too far right
299 }
300
301 //the range test above may have pushed the target one too far to the right
302 if (lo>0 && ranges[lo-1].upper >= start && ranges[lo-1].lower <= end) return lo-1;
303 else if ( ranges[lo].upper >= start && ranges[lo].lower <= end) return lo;
304 else return -1;
305
306 }
307
308
309 /* Function: fm_convertRange2DSQ()
310 * Synopsis: Convert the BWT range into a DSQ.
311 *
312 * Purpose: The input value of <first> is the 0-based position at which
313 * the requested range starts on either FM->T or revcomp(FM->T),
314 * depending on <complementarity>. Since only FM->T is stored,
315 * the necessary work is done to correct positions in the case
316 * that the positions are relative to the revcomp.
317 */
318 int
319 fm_convertRange2DSQ(const FM_DATA *fm, const FM_METADATA *meta, uint64_t first, int length, int complementarity, ESL_SQ *sq, int fix_ambiguities)
320 {
321 uint64_t i, j;
322
323 if (complementarity == p7_COMPLEMENT)
324 first = fm->N-(first+length)-1;
325
326 //All the "+1" dsq offsets below are because the dsq characters are 1-based.
327 esl_sq_GrowTo(sq, length);
328 sq->n = length;
329
330 if (meta->alph_type == fm_DNA ) {
331 /*
332 * B[j>>2] is the byte of B in which j is found (j/4)
333 *
334 * Let j' be the final two bits of j (j&0x2)
335 * The char bits are the two starting at position 2*j'.
336 * Without branching, grab them by shifting B[j/4] right 6-2*j' bits,
337 * then masking to keep the final two bits
338 */
339 for (i = first; i <= first+length-1; i++)
340 sq->dsq[i-first+1] = (fm->T[i/4] >> ( 0x6 - ((i&0x3)*2) ) & 0x3);
341 sq->dsq[length+1] = eslDSQ_SENTINEL;
342
343 if (fix_ambiguities) {
344 /* Account for the fact that in the DNA alphabet without ambiguity codes,
345 * makehmmerdb turns ambiguity codes into one of the nucleotides. Need
346 * to replace with an N.
347 */
348 int32_t pos = fm_findOverlappingAmbiguityBlock (fm, meta, first, first+length-1 );
349 if (pos != -1) {
350 while (pos <= fm->ambig_offset + fm->ambig_cnt -1 && meta->ambig_list->ranges[pos].lower <= first+length-1) {
351 int start = ESL_MAX(first, meta->ambig_list->ranges[pos].lower);
352 int end = ESL_MIN(first+length-1, meta->ambig_list->ranges[pos].upper);
353 for (j= start; j<=end; j++)
354 sq->dsq[j-first+1] = sq->abc->Kp-3; //'N'
355 pos++;
356 }
357 }
358 }
359
360 /*
361 } else if (meta->alph_type == fm_DNA_full) {
362 for (i = first; i<= first+length-1; i++) {
363 c = (fm->T[i/2] >> (((i&0x1)^0x1)*4) ) & 0xf; //unpack the char: shift 4 bits right if it's odd, then mask off left bits in any case
364 sq->dsq[i-first+1] = c + (c < 4 ? 0 : 1); //increment by one for ambiguity codes
365 }
366 sq->dsq[length+1] = eslDSQ_SENTINEL;
367 */
368 } else { // amino
369 for (i = first; i<= first+length-1; i++)
370 sq->dsq[i-first+1] = fm->T[i] + (fm->T[i] < 20 ? 0 : 1); //increment by one for ambiguity codes
371
372 sq->dsq[length+1] = eslDSQ_SENTINEL;
373 }
374
375 if (complementarity == p7_COMPLEMENT)
376 esl_sq_ReverseComplement(sq);
377
378 return eslOK;
379 }
380
381
382
383 /* Function: fm_computeSequenceOffset()
384 * Synopsis: Search in the meta->seq_data array for the sequence id corresponding to the
385 * requested position. The matching entry is the one with the largest index i
386 * such that seq_data[i].offset < pos
387 */
388 uint64_t
389 fm_computeSequenceOffset (const FM_DATA *fms, const FM_METADATA *meta, int block, uint64_t pos)
390 {
391
392 uint64_t lo = fms[block].seq_offset;
393 uint64_t hi = lo + fms[block].seq_cnt - 1;
394 uint64_t mid;
395
396 //binary search, first handling edge cases
397 if (lo==hi) return lo;
398 if (meta->seq_data[hi].fm_start <= pos) return hi;
399
400 while (1) {
401 mid = (lo + hi + 1) / 2; /* round up */
402 if (meta->seq_data[mid].fm_start <= pos) lo = mid; /* too far left */
403 else if (meta->seq_data[mid-1].fm_start > pos) hi = mid; /* too far right */
404 else return mid-1; /* found it */
405 }
406
407 }
408
409
410 /* Function: fm_getOriginalPosition()
411 * Synopsis: Find the id of the sequence in the original input corresponding
412 * to a given hit, and the position of that hit in the original
413 * Purpose: Given:
414 * fms - an array of FM-indexes
415 * meta - the fm metadata
416 * fm_id - index of the fm-index in which a hit is sought
417 * length - length of the hit in question
418 * direction - direction of the hit in question
419 * fm_pos - position in the fm-index
420 *
421 * Returns
422 * *segment_id - index of the sequence segment captured in the FM-index
423 * *seg_pos - position in the original sequence, as compressed in the FM binary data structure (zero based)
424 * int - eslERANGE if the range in question crosses the boundary between two target sequences. Otherwise eslOK.
425 */
426 int
427 fm_getOriginalPosition (const FM_DATA *fms, const FM_METADATA *meta, int fm_id, int length, int complementarity,
428 uint64_t fm_pos, uint32_t *segment_id, uint64_t *seg_pos
429 ) {
430
431 // if complementarity == p7_NOCOMPLEMENT, the end positions are in context of FM->T
432 // otherwise, they're in context of revcomp(FM->T).
433 if (complementarity == p7_COMPLEMENT) // need location in forward context:
434 fm_pos = fms->N - fm_pos - 1;
435
436 *segment_id = fm_computeSequenceOffset( fms, meta, fm_id, fm_pos);
437 *seg_pos = ( fm_pos - meta->seq_data[ *segment_id ].fm_start) + 1;
438
439 if (complementarity == p7_COMPLEMENT) // now reverse orientation
440 *seg_pos = meta->seq_data[ *segment_id ].length - *seg_pos + 1;
441
442
443 //verify that the hit doesn't extend beyond the bounds of the target sequence
444 if (*seg_pos + length - 1 > meta->seq_data[ *segment_id ].length )
445 return eslERANGE;
446
447 return eslOK;
448 }
449
450 /*********************************************************************
451 *# 4. FM data initialization, configuration, and reading from file
452 *********************************************************************/
453
454 int
455 fm_initConfigGeneric( FM_CFG *cfg, ESL_GETOPTS *go ) {
456
457 cfg->ssv_length = (go ? esl_opt_GetInteger(go, "--seed_ssv_length") : -1);
458 cfg->max_depth = (go ? esl_opt_GetInteger(go, "--seed_max_depth") : -1);
459 cfg->drop_max_len = (go ? esl_opt_GetInteger(go, "--seed_drop_max_len") : -1);
460 cfg->consec_pos_req = (go ? esl_opt_GetInteger(go, "--seed_req_pos") : -1);
461 cfg->consensus_match_req = (go ? esl_opt_GetInteger(go, "--seed_consens_match") : 10);
462 cfg->drop_lim = eslCONST_LOG2 * (go ? esl_opt_GetReal(go, "--seed_drop_lim") : -1.0); // convert from bits to nats
463 cfg->score_density_req = eslCONST_LOG2 * (go ? esl_opt_GetReal(go, "--seed_sc_density") : -1.0);// convert from bits to nats
464 cfg->scthreshFM = eslCONST_LOG2 * (go ? esl_opt_GetReal(go, "--seed_sc_thresh") : -1.0); // convert from bits to nats
465
466 return eslOK;
467 }
468
469 /* Function: fm_FM_free()
470 * Synopsis: release the memory required to store an individual FM-index
471 */
472 void
473 fm_FM_destroy ( FM_DATA *fm, int isMainFM)
474 {
475
476 free (fm->BWT_mem);
477 free (fm->C);
478 free (fm->occCnts_b);
479 free (fm->occCnts_sb);
480
481 if (isMainFM) {
482 free (fm->T);
483 free (fm->SA);
484 }
485 }
486
487 /* Function: fm_FM_read()
488 * Synopsis: Read the FM index off disk
489 * Purpose: Read the FM-index as written by fmbuild.
490 * First read the metadata header, then allocate space for the full index,
491 * then read it in.
492 */
493 int
494 fm_FM_read( FM_DATA *fm, FM_METADATA *meta, int getAll )
495 {
496 //shortcut variables
497 int64_t *C = NULL;
498
499 int i;
500 uint32_t *occCnts_sb = NULL;
501 int32_t compressed_bytes;
502 int num_freq_cnts_b;
503 int num_freq_cnts_sb;
504 int num_SA_samples;
505 int64_t prevC;
506 int cnt;
507 int chars_per_byte = 8/meta->charBits;
508 int status;
509
510
511 if(fread(&(fm->N), sizeof(uint64_t), 1, meta->fp) != 1 ||
512 fread(&(fm->term_loc), sizeof(uint32_t), 1, meta->fp) != 1 ||
513 fread(&(fm->seq_offset), sizeof(uint32_t), 1, meta->fp) != 1 ||
514 fread(&(fm->ambig_offset), sizeof(uint32_t), 1, meta->fp) != 1 ||
515 fread(&(fm->overlap), sizeof(uint32_t), 1, meta->fp) != 1 ||
516 fread(&(fm->seq_cnt), sizeof(uint32_t), 1, meta->fp) != 1 ||
517 fread(&(fm->ambig_cnt), sizeof(uint32_t), 1, meta->fp) != 1
518 )
519 {status=eslEFORMAT; goto ERROR;}
520
521 compressed_bytes = ((chars_per_byte-1+fm->N)/chars_per_byte);
522 num_freq_cnts_b = 1+ceil((double)fm->N/meta->freq_cnt_b);
523 num_freq_cnts_sb = 1+ceil((double)fm->N/meta->freq_cnt_sb);
524 num_SA_samples = 1+floor((double)fm->N/meta->freq_SA);
525
526 // allocate space, then read the data
527 if (getAll) ESL_ALLOC (fm->T, sizeof(uint8_t) * compressed_bytes );
528 ESL_ALLOC (fm->BWT_mem, sizeof(uint8_t) * (compressed_bytes + 31) ); // +31 for manual 16-byte alignment ( typically only need +15, but this allows offset in memory, plus offset in case of <16 bytes of characters at the end)
529 fm->BWT = (uint8_t *) (((unsigned long int)fm->BWT_mem + 15) & (~0xf)); // align vector memory on 16-byte boundaries
530 if (getAll) ESL_ALLOC (fm->SA, num_SA_samples * sizeof(uint32_t));
531 ESL_ALLOC (fm->C, (1+meta->alph_size) * sizeof(int64_t));
532 ESL_ALLOC (fm->occCnts_b, num_freq_cnts_b * (meta->alph_size ) * sizeof(uint16_t)); // every freq_cnt positions, store an array of ints
533 ESL_ALLOC (fm->occCnts_sb, num_freq_cnts_sb * (meta->alph_size ) * sizeof(uint32_t)); // every freq_cnt positions, store an array of ints
534
535
536 if(
537 (getAll && fread(fm->T, sizeof(uint8_t), compressed_bytes, meta->fp) != compressed_bytes) ||
538 (fread(fm->BWT, sizeof(uint8_t), compressed_bytes, meta->fp) != compressed_bytes) ||
539 (getAll && fread(fm->SA, sizeof(uint32_t), (size_t)num_SA_samples, meta->fp) != (size_t)num_SA_samples) ||
540 (fread(fm->occCnts_b, sizeof(uint16_t)*(meta->alph_size), (size_t)num_freq_cnts_b, meta->fp) != (size_t)num_freq_cnts_b) ||
541 (fread(fm->occCnts_sb, sizeof(uint32_t)*(meta->alph_size), (size_t)num_freq_cnts_sb, meta->fp) != (size_t)num_freq_cnts_sb)
542 )
543 {status=eslEFORMAT; goto ERROR;}
544
545 //shortcut variables
546 C = fm->C;
547 occCnts_sb = fm->occCnts_sb;
548
549 /*compute the first position of each letter in the alphabet in a sorted list
550 * (with an extra value to simplify lookup of the last position for the last letter).
551 * Negative values indicate that there are zero of that character in T, can be
552 * used to establish the end of the prior range*/
553 C[0] = 0;
554 for (i=0; i<meta->alph_size; i++) {
555 prevC = abs((int)(C[i]));
556
557 cnt = FM_OCC_CNT( sb, num_freq_cnts_sb-1, i);
558
559 if (cnt==0) {// none of this character
560 C[i+1] = prevC;
561 C[i] *= -1; // use negative to indicate that there's no character of this type, the number gives the end point of the previous
562 } else {
563 C[i+1] = prevC + cnt;
564 }
565 }
566 C[meta->alph_size] *= -1;
567 C[0] = 1;
568
569 return eslOK;
570
571 ERROR:
572 fm_FM_destroy(fm, getAll);
573 return status;
574 }
575
576
577 /* Function: readFMmeta()
578 * Synopsis: Read metadata from disk for the set of FM-indexes stored in a HMMER binary file
579 *
580 * Input: file pointer to binary file
581 * Output: return filled meta struct
582 */
583 int
584 fm_readFMmeta( FM_METADATA *meta)
585 {
586 int status;
587 int i;
588
589
590 fm_initAmbiguityList(meta->ambig_list);
591
592 if( fread(&(meta->fwd_only), sizeof(meta->fwd_only), 1, meta->fp) != 1 ||
593 fread(&(meta->alph_type), sizeof(meta->alph_type), 1, meta->fp) != 1 ||
594 fread(&(meta->alph_size), sizeof(meta->alph_size), 1, meta->fp) != 1 ||
595 fread(&(meta->charBits), sizeof(meta->charBits), 1, meta->fp) != 1 ||
596 fread(&(meta->freq_SA), sizeof(meta->freq_SA), 1, meta->fp) != 1 ||
597 fread(&(meta->freq_cnt_sb), sizeof(meta->freq_cnt_sb), 1, meta->fp) != 1 ||
598 fread(&(meta->freq_cnt_b), sizeof(meta->freq_cnt_b), 1, meta->fp) != 1 ||
599 fread(&(meta->block_count), sizeof(meta->block_count), 1, meta->fp) != 1 ||
600 fread(&(meta->seq_count), sizeof(meta->seq_count), 1, meta->fp) != 1 ||
601 fread(&(meta->ambig_list->count), sizeof(meta->ambig_list->count), 1, meta->fp) != 1 ||
602 fread(&(meta->char_count), sizeof(meta->char_count), 1, meta->fp) != 1
603 )
604 {status=eslEFORMAT; goto ERROR;}
605
606 /* sanity check - are these metadata for a real FM index?
607 * TODO: in an upcoming renovation of FM, capture FM validation & version as part of metadata header
608 */
609 if ( meta->alph_type != fm_DNA || /* this is the only legal value for nhmmer */
610 meta->fwd_only > 1 || /* must be 0 (false) or 1 (true) */
611 meta->charBits > 8 || /* should really be 2 ... but allowing for future growth */
612 meta->freq_SA > 10000 /* a suffix array sampling of this scale is insane */
613 )
614 {status=eslEFORMAT; return status;}
615
616
617 ESL_ALLOC (meta->seq_data, meta->seq_count * sizeof(FM_SEQDATA));
618
619
620 for (i=0; i<meta->seq_count; i++) {
621 if( fread(&(meta->seq_data[i].target_id), sizeof(meta->seq_data[i].target_id), 1, meta->fp) != 1 ||
622 fread(&(meta->seq_data[i].target_start), sizeof(meta->seq_data[i].target_start), 1, meta->fp) != 1 ||
623 fread(&(meta->seq_data[i].fm_start), sizeof(meta->seq_data[i].fm_start), 1, meta->fp) != 1 ||
624 fread(&(meta->seq_data[i].length), sizeof(meta->seq_data[i].length), 1, meta->fp) != 1 ||
625 fread(&(meta->seq_data[i].name_length), sizeof(meta->seq_data[i].name_length), 1, meta->fp) != 1 ||
626 fread(&(meta->seq_data[i].acc_length), sizeof(meta->seq_data[i].acc_length), 1, meta->fp) != 1 ||
627 fread(&(meta->seq_data[i].source_length),sizeof(meta->seq_data[i].source_length),1, meta->fp) != 1 ||
628 fread(&(meta->seq_data[i].desc_length), sizeof(meta->seq_data[i].desc_length), 1, meta->fp) != 1
629 )
630 {status=eslEFORMAT; goto ERROR;}
631
632 ESL_ALLOC (meta->seq_data[i].name, (1+meta->seq_data[i].name_length) * sizeof(char));
633 ESL_ALLOC (meta->seq_data[i].acc, (1+meta->seq_data[i].acc_length) * sizeof(char));
634 ESL_ALLOC (meta->seq_data[i].source,(1+meta->seq_data[i].source_length) * sizeof(char));
635 ESL_ALLOC (meta->seq_data[i].desc, (1+meta->seq_data[i].desc_length) * sizeof(char));
636
637 if(
638 fread(meta->seq_data[i].name, sizeof(char), meta->seq_data[i].name_length+1 , meta->fp) != meta->seq_data[i].name_length+1 ||
639 fread(meta->seq_data[i].acc, sizeof(char), meta->seq_data[i].acc_length+1 , meta->fp) != meta->seq_data[i].acc_length+1 ||
640 fread(meta->seq_data[i].source, sizeof(char), meta->seq_data[i].source_length+1 , meta->fp) != meta->seq_data[i].source_length+1 ||
641 fread(meta->seq_data[i].desc, sizeof(char), meta->seq_data[i].desc_length+1 , meta->fp) != meta->seq_data[i].desc_length+1
642 )
643 {status=eslEFORMAT; goto ERROR;}
644 }
645
646 if (meta->ambig_list->count > meta->ambig_list->size) {
647 ESL_REALLOC(meta->ambig_list->ranges, meta->ambig_list->count * sizeof(FM_INTERVAL));
648 meta->ambig_list->size = meta->ambig_list->count;
649 }
650
651 for (i=0; i<meta->ambig_list->count; i++) {
652 if( fread(&(meta->ambig_list->ranges[i].lower), sizeof(meta->ambig_list->ranges[i].lower), 1, meta->fp) != 1 ||
653 fread(&(meta->ambig_list->ranges[i].upper), sizeof(meta->ambig_list->ranges[i].upper), 1, meta->fp) != 1
654 )
655 {status=eslEAMBIGUOUS; goto ERROR;}
656 }
657
658 return eslOK;
659
660 ERROR:
661
662 if (meta->seq_data) {
663 for (i=0; i<meta->seq_count; i++)
664 free(meta->seq_data[i].name);
665 free(meta->seq_data);
666 }
667 free(meta);
668
669 return status;
670 }
671
672
673 /* Function: fm_configAlloc()
674 * Synopsis: Allocate a <FM_CFG> model object, and its FM_METADATA
675 */
676 int
677 fm_configAlloc(FM_CFG **cfg)
678 {
679 int status;
680
681 if (cfg == NULL) {status=eslERANGE; goto ERROR;}
682 *cfg = NULL;
683
684 ESL_ALLOC(*cfg, sizeof(FM_CFG) );
685 ESL_ALLOC((*cfg)->meta, sizeof(FM_METADATA));
686 ESL_ALLOC ((*cfg)->meta->ambig_list, sizeof(FM_AMBIGLIST));
687
688 return eslOK;
689
690 ERROR:
691 if (*cfg != NULL) {
692 if ((*cfg)->meta != NULL) free ((*cfg)->meta);
693 free (*cfg);
694 }
695 return status;
696 }
697
698
699
700 /* Function: fm_configDestroy()
701 * Synopsis: Destroy various memory items used for the FMindex implementation
702 *
703 * Purpose: Destroy the masks used by the FM index, the metadata for
704 * the FM index, and the config itself.
705 */
706 int
707 fm_configDestroy(FM_CFG *cfg ) {
708 if (cfg) {
709 #if defined (eslENABLE_SSE)
710 if (cfg->fm_chars_mem) free(cfg->fm_chars_mem);
711 if (cfg->fm_masks_mem) free(cfg->fm_masks_mem);
712 if (cfg->fm_reverse_masks_mem) free(cfg->fm_reverse_masks_mem);
713 #endif
714 fm_metaDestroy(cfg->meta);
715 free(cfg);
716 }
717 return eslOK;
718 }
719
720 /* Function: fm_metaDestroy()
721 * Synopsis: Destroy various metadata for the FMindex implementation
722 */
723 int
724 fm_metaDestroy(FM_METADATA *meta ) {
725 int i;
726 if (meta != NULL) {
727 for (i=0; i<meta->seq_count; i++) {
728 if(meta->seq_data[i].name) free(meta->seq_data[i].name);
729 if(meta->seq_data[i].acc) free(meta->seq_data[i].acc);
730 if(meta->seq_data[i].source) free(meta->seq_data[i].source);
731 if(meta->seq_data[i].desc) free(meta->seq_data[i].desc);
732
733 }
734 free(meta->seq_data);
735
736 if (meta->ambig_list) {
737 if (meta->ambig_list->ranges) free(meta->ambig_list->ranges);
738 free(meta->ambig_list);
739 }
740
741 fm_alphabetDestroy(meta);
742 free (meta);
743 }
744
745 return eslOK;
746 }
747
748
749