Skip to content

Commit 13514ca

Browse files
committed
feat: support binary data instead of a string
1 parent 976aec8 commit 13514ca

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

README.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,50 @@ go get github.com/tuxity/dnacoder
1414

1515
### Encoding a string to a DNA sequence
1616

17-
You can encode any string into a DNA sequence using `EncodeStringToDNA`.
17+
You can encode any string into a DNA sequence using `encode`.
1818

1919
```go
2020
package main
2121

2222
import (
23+
"io/ioutil"
24+
"log"
2325
"fmt"
2426
"github.com/tuxity/dnacoder"
2527
)
2628

2729
func main() {
28-
data := "Hello, DNA!"
29-
dnaSequence := dnacoder.EncodeStringToDNA(data)
30+
fileData, err := ioutil.ReadFile("example.txt") // Read the file as binary data
31+
if err != nil {
32+
log.Fatal(err)
33+
}
34+
dnaSequence := dnacoder.encode(fileData)
3035
fmt.Println("Encoded DNA Sequence:", dnaSequence)
3136
}
3237
```
3338

3439
### Decoding a DNA sequence back to a string
3540

36-
You can decode a DNA sequence back into the original string using `DecodeDNAToString`.
41+
You can decode a DNA sequence back into the original string using `decode`.
3742

3843
```go
3944
package main
4045

4146
import (
42-
"fmt"
47+
"io/ioutil"
48+
"log"
4349
"github.com/tuxity/dnacoder"
4450
)
4551

4652
func main() {
4753
dnaSequence := "GAGAGCACACGTATGATACATCTCTCTACGTCAGCTATATAGATCTCTGATCTGA"
48-
decodedString, err := dnacoder.DecodeDNAToString(dnaSequence)
54+
decodedData, err := dnacoder.decode(dnaSequence)
55+
if err != nil {
56+
log.Fatal(err)
57+
}
58+
err = ioutil.WriteFile("decoded_example.txt", decodedData, 0644)
4959
if err != nil {
50-
fmt.Println("Error decoding DNA sequence:", err)
51-
} else {
52-
fmt.Println("Decoded String:", decodedString)
60+
log.Fatal(err)
5361
}
5462
}
5563
```

dnacoder.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,20 @@ func decodeDNAToTernary(dnaSeq string) (string, error) {
6868
return ternaryStr.String(), nil
6969
}
7070

71-
func encode(data string) string {
71+
func encode(data []byte) string {
7272
ternaryStr := byteArrayToTernary([]byte(data))
7373
dnaSequence := encodeTernaryToDNA(ternaryStr)
7474

7575
return dnaSequence
7676
}
7777

78-
func decode(dnaSeq string) (string, error) {
78+
func decode(dnaSeq string) ([]byte, error) {
7979
ternaryStr, err := decodeDNAToTernary(dnaSeq)
8080
if err != nil {
81-
return "", err
81+
return nil, err
8282
}
8383

8484
byteArray := ternaryToByteArray(ternaryStr)
8585

86-
return string(byteArray), nil
86+
return byteArray, nil
8787
}

dnacoder_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,47 @@
11
package dnacoder
22

33
import (
4+
"bytes"
45
"testing"
56
)
67

78
func TestEncode(t *testing.T) {
8-
data := "Hello, DNA!"
9+
data := []byte{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x44, 0x4e, 0x41, 0x21} // "Hello, DNA!"
910
expectedDNA := "GAGAGCACACGTATGATACATCTCTCTACGTCAGCTATATAGATCTCTGATCTGA"
1011

1112
encodedDNA := encode(data)
12-
if encodedDNA != expectedDNA {
13+
if encodedDNA != expectedDNA {
1314
t.Errorf("Encode(%s) = %s; want %s", data, encodedDNA, expectedDNA)
1415
}
1516
}
1617

1718
func TestDecode(t *testing.T) {
1819
dnaSeq := "GAGAGCACACGTATGATACATCTCTCTACGTCAGCTATATAGATCTCTGATCTGA"
19-
expectedString := "Hello, DNA!"
20+
expectedData := []byte{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x44, 0x4e, 0x41, 0x21} // "Hello, DNA!"
2021

21-
decodedString, err := decode(dnaSeq)
22+
decodedData, err := decode(dnaSeq)
2223
if err != nil {
2324
t.Fatalf("Error decoding DNA sequence: %v", err)
2425
}
2526

26-
if decodedString != expectedString {
27-
t.Errorf("Decode(%s) = %s; want %s", dnaSeq, decodedString, expectedString)
27+
if !bytes.Equal(decodedData, expectedData) {
28+
t.Errorf("Decode(%s) = %s; want %s", dnaSeq, decodedData, expectedData)
2829
}
2930
}
3031

3132
func TestEncodeAndDecode(t *testing.T) {
32-
data := "Test String"
33+
data := []byte{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x44, 0x4e, 0x41, 0x21} // "Hello, DNA!"
3334

3435
// Encode the string to DNA
3536
encodedDNA := encode(data)
3637

3738
// Decode back to the original string
38-
decodedString, err := decode(encodedDNA)
39+
decodedData, err := decode(encodedDNA)
3940
if err != nil {
4041
t.Fatalf("Error decoding DNA sequence: %v", err)
4142
}
4243

43-
if decodedString != data {
44-
t.Errorf("Encode and Decode mismatch: got %s, want %s", decodedString, data)
44+
if !bytes.Equal(decodedData, data) {
45+
t.Errorf("Encode and Decode mismatch: got %s, want %s", decodedData, data)
4546
}
4647
}

0 commit comments

Comments
 (0)