Skip to content

Commit 22cb705

Browse files
r136: support string in revcomp()
1 parent f249b97 commit 22cb705

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ function decode(buf: ArrayBuffer|Bytes, enc?: string): string
8080
// Encode $str into an ArrayBuffer
8181
function encode(str: string, enc?: string): ArrayBuffer
8282

83+
// Reverse complement a DNA sequence in string
84+
function revcomp(seq: string): string
85+
86+
// Reverse complement a DNA sequence in place
87+
function revcomp(seq: ArrayBuffer|Bytes)
88+
8389
// Get version string
8490
function k8_version()
8591
```

k8.cc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2424
SOFTWARE.
2525
*/
26-
#define K8_VERSION "1.1-r135-dirty"
26+
#define K8_VERSION "1.1-r136-dirty"
2727

2828
#include <stdlib.h>
2929
#include <stdint.h>
@@ -553,6 +553,7 @@ static void k8_revcomp(const v8::FunctionCallbackInfo<v8::Value> &args)
553553
v8::HandleScope handle_scope(args.GetIsolate());
554554
uint8_t *seq = 0;
555555
int64_t len = 0;
556+
int32_t is_str = 0;
556557
if (args[0]->IsArrayBuffer()) {
557558
seq = (uint8_t*)args[0].As<v8::ArrayBuffer>()->GetBackingStore()->Data();
558559
len = args[0].As<v8::ArrayBuffer>()->GetBackingStore()->ByteLength();
@@ -562,6 +563,11 @@ static void k8_revcomp(const v8::FunctionCallbackInfo<v8::Value> &args)
562563
if (a && a->magic == K8_BYTES_MAGIC)
563564
seq = a->buf.s, len = a->buf.l;
564565
}
566+
} else if (args[0]->IsString()) {
567+
is_str = 1;
568+
len = args[0].As<v8::String>()->Length();
569+
seq = (uint8_t*)calloc(len + 1, 1);
570+
args[0].As<v8::String>()->WriteOneByte(args.GetIsolate(), seq);
565571
}
566572
if (seq == 0) return;
567573
for (int64_t i = 0; i < len>>1; ++i) {
@@ -570,6 +576,12 @@ static void k8_revcomp(const v8::FunctionCallbackInfo<v8::Value> &args)
570576
seq[i] = tmp < 128? k8_comp_tab[tmp] : tmp;
571577
}
572578
if (len & 1) seq[len>>1] = seq[len>>1] < 128? k8_comp_tab[seq[len>>1]] : seq[len>>1];
579+
if (is_str) {
580+
v8::Local<v8::String> str;
581+
if (v8::String::NewFromOneByte(args.GetIsolate(), seq, v8::NewStringType::kNormal, len).ToLocal(&str))
582+
args.GetReturnValue().Set(str);
583+
free(seq);
584+
}
573585
}
574586

575587
/***********************

0 commit comments

Comments
 (0)