mappy to support BED

This commit is contained in:
Heng Li
2018-09-17 14:54:44 -04:00
parent a3e223f17b
commit b4b70db126
6 changed files with 63 additions and 10 deletions

View File

@@ -20,6 +20,11 @@ typedef struct {
uint32_t *cigar32;
} mm_hitpy_t;
typedef struct {
int32_t n, m;
mm_idx_bed_t *r;
} mm_bedpy_t;
static inline void mm_reg2hitpy(const mm_idx_t *mi, mm_reg1_t *r, mm_hitpy_t *h)
{
h->ctg = mi->seq[r->rid].name;
@@ -149,4 +154,33 @@ static mm_idx_t *mappy_idx_seq(int w, int k, int is_hpc, int bucket_bits, const
return mi;
}
static mm_bedpy_t *mappy_bed_new(void)
{
return (mm_bedpy_t*)calloc(1, sizeof(mm_bedpy_t));
}
static int mappy_bed_add(mm_bedpy_t *bed, mm_idx_t *mi, const char *name, uint32_t st, uint32_t en)
{
mm_idx_bed_t *b;
int id;
if (mi->h == 0) mm_idx_index_name(mi);
if (bed->n == bed->m) {
bed->m = bed->m? bed->m + (bed->m>>1) : 16;
bed->r = (mm_idx_bed_t*)realloc(bed->r, sizeof(mm_idx_bed_t) * bed->m);
}
id = mm_idx_name2id(mi, name);
if (id < 0 || st >= en) return -1;
if (en > mi->seq[id].len) en = mi->seq[id].len;
b = &bed->r[bed->n++];
b->x = (uint64_t)id << 32 | st;
b->end = en, b->idx = -1;
return 0;
}
static void mappy_bed_finalize(mm_bedpy_t *bed, mm_idx_t *mi)
{
mm_idx_bed_attach(mi, bed->n, bed->r); // bed->r is now owned by mi and will be deallocated with it
free(bed);
}
#endif

View File

@@ -117,6 +117,14 @@ cdef extern from "cmappy.h":
char *mappy_fetch_seq(const mm_idx_t *mi, const char *name, int st, int en, int *l)
mm_idx_t *mappy_idx_seq(int w, int k, int is_hpc, int bucket_bits, const char *seq, int l)
ctypedef struct mm_bedpy_t:
int32_t n, m
mm_idx_bed_t *r
mm_bedpy_t *mappy_bed_new()
int mappy_bed_add(mm_bedpy_t *bed, mm_idx_t *mi, const char *name, uint32_t st, uint32_t en)
void mappy_bed_finalize(mm_bedpy_t *bed, mm_idx_t *mi)
ctypedef struct kstring_t:
unsigned l, m
char *s

View File

@@ -112,7 +112,7 @@ cdef class Aligner:
cdef cmappy.mm_idxopt_t idx_opt
cdef cmappy.mm_mapopt_t map_opt
def __cinit__(self, fn_idx_in=None, preset=None, k=None, w=None, min_cnt=None, min_chain_score=None, min_dp_score=None, bw=None, best_n=None, n_threads=3, fn_idx_out=None, max_frag_len=None, extra_flags=None, seq=None, scoring=None):
def __cinit__(self, fn_idx_in=None, preset=None, k=None, w=None, min_cnt=None, min_chain_score=None, min_dp_score=None, bw=None, best_n=None, n_threads=3, fn_idx_out=None, max_frag_len=None, extra_flags=None, seq=None, scoring=None, bed=None):
cmappy.mm_set_opt(NULL, &self.idx_opt, &self.map_opt) # set the default options
if preset is not None:
cmappy.mm_set_opt(str.encode(preset), &self.idx_opt, &self.map_opt) # apply preset
@@ -137,6 +137,7 @@ cdef class Aligner:
self.map_opt.sc_ambi = scoring[6]
cdef cmappy.mm_idx_reader_t *r;
cdef cmappy.mm_bedpy_t *bed_agg;
if seq is None:
if fn_idx_out is None:
@@ -153,6 +154,14 @@ cdef class Aligner:
cmappy.mm_mapopt_update(&self.map_opt, self._idx)
self.map_opt.mid_occ = 1000 # don't filter high-occ seeds
if bed is not None:
bed_agg = cmappy.mappy_bed_new()
for b in bed:
if len(b) < 3: en = int(b[1]) + 1
else: en = int(b[2])
cmappy.mappy_bed_add(bed_agg, self._idx, str.encode(b[0]), int(b[1]), en)
cmappy.mappy_bed_finalize(bed_agg, self._idx)
def __dealloc__(self):
if self._idx is not NULL:
cmappy.mm_idx_destroy(self._idx)