Skip to content

Commit

Permalink
r136: support string in revcomp()
Browse files Browse the repository at this point in the history
  • Loading branch information
attractivechaos committed May 27, 2024
1 parent f249b97 commit 22cb705
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ function decode(buf: ArrayBuffer|Bytes, enc?: string): string
// Encode $str into an ArrayBuffer
function encode(str: string, enc?: string): ArrayBuffer

// Reverse complement a DNA sequence in string
function revcomp(seq: string): string

// Reverse complement a DNA sequence in place
function revcomp(seq: ArrayBuffer|Bytes)

// Get version string
function k8_version()
```
Expand Down
14 changes: 13 additions & 1 deletion k8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#define K8_VERSION "1.1-r135-dirty"
#define K8_VERSION "1.1-r136-dirty"

#include <stdlib.h>
#include <stdint.h>
Expand Down Expand Up @@ -553,6 +553,7 @@ static void k8_revcomp(const v8::FunctionCallbackInfo<v8::Value> &args)
v8::HandleScope handle_scope(args.GetIsolate());
uint8_t *seq = 0;
int64_t len = 0;
int32_t is_str = 0;
if (args[0]->IsArrayBuffer()) {
seq = (uint8_t*)args[0].As<v8::ArrayBuffer>()->GetBackingStore()->Data();
len = args[0].As<v8::ArrayBuffer>()->GetBackingStore()->ByteLength();
Expand All @@ -562,6 +563,11 @@ static void k8_revcomp(const v8::FunctionCallbackInfo<v8::Value> &args)
if (a && a->magic == K8_BYTES_MAGIC)
seq = a->buf.s, len = a->buf.l;
}
} else if (args[0]->IsString()) {
is_str = 1;
len = args[0].As<v8::String>()->Length();
seq = (uint8_t*)calloc(len + 1, 1);
args[0].As<v8::String>()->WriteOneByte(args.GetIsolate(), seq);
}
if (seq == 0) return;
for (int64_t i = 0; i < len>>1; ++i) {
Expand All @@ -570,6 +576,12 @@ static void k8_revcomp(const v8::FunctionCallbackInfo<v8::Value> &args)
seq[i] = tmp < 128? k8_comp_tab[tmp] : tmp;
}
if (len & 1) seq[len>>1] = seq[len>>1] < 128? k8_comp_tab[seq[len>>1]] : seq[len>>1];
if (is_str) {
v8::Local<v8::String> str;
if (v8::String::NewFromOneByte(args.GetIsolate(), seq, v8::NewStringType::kNormal, len).ToLocal(&str))
args.GetReturnValue().Set(str);
free(seq);
}
}

/***********************
Expand Down

0 comments on commit 22cb705

Please sign in to comment.