Skip to content

Commit

Permalink
bugfix: in-place revcomp() not working
Browse files Browse the repository at this point in the history
  • Loading branch information
lh3 committed Feb 20, 2018
1 parent 7dc7097 commit 29ed675
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
15 changes: 7 additions & 8 deletions python/cmappy.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,15 @@ static inline mm_reg1_t *mm_map_aux(const mm_idx_t *mi, const char *seq1, const
}
}

static inline uint8_t *mappy_revcomp(int len, uint8_t *seq)
static inline char *mappy_revcomp(int len, const uint8_t *seq)
{
int i;
for (i = 0; i < len>>1; ++i) {
uint8_t t = seq_comp_table[seq[i]];
seq[i] = seq_comp_table[seq[len - 1 - i]];
seq[len - 1 - i] = t;
}
if (len&1) seq[len>>1] = seq_comp_table[seq[len>>1]];
return seq;
char *rev;
rev = (char*)malloc(len + 1);
for (i = 0; i < len; ++i)
rev[len - i - 1] = seq_comp_table[seq[i]];
rev[len] = 0;
return rev;
}

#endif
2 changes: 1 addition & 1 deletion python/cmappy.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,6 @@ cdef extern from "cmappy.h":
void mm_fastx_close(kseq_t *ks)
int kseq_read(kseq_t *seq)

uint8_t *mappy_revcomp(int l, uint8_t *seq)
char *mappy_revcomp(int l, const uint8_t *seq)
int mm_verbose_level(int v)
void mm_reset_timer()
10 changes: 7 additions & 3 deletions python/mappy.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from libc.stdint cimport uint8_t, int8_t
from libc.stdlib cimport free
cimport cmappy
import sys

cmappy.mm_reset_timer()

Expand Down Expand Up @@ -165,9 +166,12 @@ def fastx_read(fn):
cmappy.mm_fastx_close(ks)

def revcomp(seq):
cdef uint8_t *s
s = cmappy.mappy_revcomp(len(seq), str.encode(seq))
return s if isinstance(s, str) else s.decode()
l = len(seq)
bseq = seq if isinstance(seq, bytes) else seq.encode()
cdef char *s = cmappy.mappy_revcomp(l, bseq)
r = s[:l] if isinstance(s, str) else s[:l].decode()
free(s)
return r

def verbose(v=None):
if v is None: v = -1
Expand Down

0 comments on commit 29ed675

Please sign in to comment.