/*************************************************************************************
                           The MIT License

   BWA-MEM2  (Sequence alignment using Burrows-Wheeler Transform),
   Copyright (C) 2019  Intel Corporation, Heng Li.

   Permission is hereby granted, free of charge, to any person obtaining
   a copy of this software and associated documentation files (the
   "Software"), to deal in the Software without restriction, including
   without limitation the rights to use, copy, modify, merge, publish,
   distribute, sublicense, and/or sell copies of the Software, and to
   permit persons to whom the Software is furnished to do so, subject to
   the following conditions:

   The above copyright notice and this permission notice shall be
   included in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   SOFTWARE.

Authors: Vasimuddin Md <vasimuddin.md@intel.com>; Sanchit Misra <sanchit.misra@intel.com>.
*****************************************************************************************/

#ifndef __INDEX_ELE_HPP
#define __INDEX_ELE_HPP

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "utils.h"
#include "bntseq.h"
#include "macro.h"

#define BWA_IDX_BWT 0x1
#define BWA_IDX_BNS 0x2
#define BWA_IDX_PAC 0x4
#define BWA_IDX_ALL 0x7

typedef struct {
	bntseq_t *bns; // information on the reference sequences
	uint8_t  *pac; // the actual 2-bit encoded reference sequences with 'N' converted to a random base
	int    is_shm;
	int64_t l_mem;
	uint8_t  *mem;
} bwaidx_fm_t;


class indexEle {
	
public:
	bwaidx_fm_t *idx;
	
	indexEle();
	~indexEle();
	void bwa_idx_load_ele(const char *hint, int which);
	char *bwa_idx_infer_prefix(const char *hint);

	/* Attach BNS + PAC from a packed bwa-mem3 index segment produced by
	 * bwa_shm_pack_from_disk. `base`/`len` describe the segment (typically
	 * from bwa_shm_attach). The bntseq_t struct and anns[] array are
	 * heap-copied (mapper mutates them); ambs[], name/anno strings, and
	 * pac are aliased into the segment. Sets idx->is_shm=1 and idx->mem,
	 * idx->l_mem so the existing destructor gate skips the wrong frees.
	 * The segment's lifetime belongs to the loader process; shm pages are
	 * never freed by this object.
	 */
	void bwa_idx_load_ele_from_shm(uint8_t *base, size_t len);
};
#endif
