aboutsummaryrefslogtreecommitdiff
path: root/content/posts/2019-01-03-encoding-binary-data-into-dna-sequence.md
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2023-05-26 00:40:40 +0200
committerMitja Felicijan <mitja.felicijan@gmail.com>2023-05-26 00:40:40 +0200
commit43b0708769eb61392050045b881f8e6ba39c5b66 (patch)
tree3939579a13b8325325d5ebb8e05324a41ed78a6d /content/posts/2019-01-03-encoding-binary-data-into-dna-sequence.md
parent49e7e7d555a6cd9810d81561fa3e98e3d64502be (diff)
downloadmitjafelicijan.com-43b0708769eb61392050045b881f8e6ba39c5b66.tar.gz
Massive update to posts, archetypes
Added a archetypes for creating notes and posts so it auto-populates fields. Fixed existing posts so they align with the rule of 80 columns now.
Diffstat (limited to 'content/posts/2019-01-03-encoding-binary-data-into-dna-sequence.md')
-rw-r--r--content/posts/2019-01-03-encoding-binary-data-into-dna-sequence.md133
1 files changed, 98 insertions, 35 deletions
diff --git a/content/posts/2019-01-03-encoding-binary-data-into-dna-sequence.md b/content/posts/2019-01-03-encoding-binary-data-into-dna-sequence.md
index 577e241..a57201f 100644
--- a/content/posts/2019-01-03-encoding-binary-data-into-dna-sequence.md
+++ b/content/posts/2019-01-03-encoding-binary-data-into-dna-sequence.md
@@ -1,47 +1,71 @@
1--- 1---
2title: Encoding binary data into DNA sequence 2title: Encoding binary data into DNA sequence
3url: encoding-binary-data-into-dna-sequence.html 3url: encoding-binary-data-into-dna-sequence.html
4date: 2019-01-03 4date: 2019-01-03T12:00:00+02:00
5draft: false 5draft: false
6--- 6---
7 7
8## Initial thoughts 8## Initial thoughts
9 9
10Imagine a world where you could go outside and take a leaf from a tree and put it through your personal DNA sequencer and get data like music, videos or computer programs from it. Well, this is all possible now. It was not done on a large scale because it is quite expensive to create DNA strands but it's possible. 10Imagine a world where you could go outside and take a leaf from a tree and put
11it through your personal DNA sequencer and get data like music, videos or
12computer programs from it. Well, this is all possible now. It was not done
13on a large scale because it is quite expensive to create DNA strands but it's
14possible.
11 15
12Encoding data into DNA sequence is relatively simple process once you understand the relationship between binary data and nucleotides and scientists have been making large leaps in this field in order to provide viable long-term storage solution for our data that would potentially survive our specie if case of global disaster. We could imprint all the world's knowledge into plants and ensure the survival of our knowledge. 16Encoding data into DNA sequence is relatively simple process once you understand
17the relationship between binary data and nucleotides and scientists have been
18making large leaps in this field in order to provide viable long-term storage
19solution for our data that would potentially survive our specie if case of
20global disaster. We could imprint all the world's knowledge into plants and
21ensure the survival of our knowledge.
13 22
14More optimistic usage for this technology would be easier storage of ever growing data we produce every day. Once machines for sequencing DNA become fast enough and cheaper this could mean the next evolution of storing data and abandoning classical hard and solid state drives in data warehouses. 23More optimistic usage for this technology would be easier storage of ever
24growing data we produce every day. Once machines for sequencing DNA become fast
25enough and cheaper this could mean the next evolution of storing data and
26abandoning classical hard and solid state drives in data warehouses.
15 27
16As we currently stand this is still not viable but it is quite an amazing and cool technology. 28As we currently stand this is still not viable but it is quite an amazing and
29cool technology.
17 30
18My interests in this field are purely in encoding processes and experimental testing mainly because I don't have the access to this expensive machines. My initial goal was to create a toolkit that can be used by everybody to encode their data into a proper DNA sequence. 31My interests in this field are purely in encoding processes and experimental
32testing mainly because I don't have the access to this expensive machines. My
33initial goal was to create a toolkit that can be used by everybody to encode
34their data into a proper DNA sequence.
19 35
20## Glossary 36## Glossary
21 37
22**deoxyribose** 38**deoxyribose**
23A five-carbon sugar molecule with a hydrogen atom rather than a hydroxyl group in the 2′ position; the sugar component of DNA nucleotides. 39A five-carbon sugar molecule with a hydrogen atom rather than a hydroxyl group
40in the 2′ position; the sugar component of DNA nucleotides.
24 41
25**double helix** 42**double helix**
26The molecular shape of DNA in which two strands of nucleotides wind around each other in a spiral shape. 43The molecular shape of DNA in which two strands of nucleotides wind around
44each other in a spiral shape.
27 45
28**nitrogenous base** 46**nitrogenous base**
29A nitrogen-containing molecule that acts as a base; often referring to one of the purine or pyrimidine components of nucleic acids. 47A nitrogen-containing molecule that acts as a base; often referring to one of
48the purine or pyrimidine components of nucleic acids.
30 49
31**phosphate group** 50**phosphate group**
32A molecular group consisting of a central phosphorus atom bound to four oxygen atoms. 51A molecular group consisting of a central phosphorus atom bound to four oxygen
52atoms.
33 53
34**RGB** 54**RGB**
35The RGB color model is an additive color model in which red, green and blue light are added together in various ways to reproduce a broad array of colors. 55The RGB color model is an additive color model in which red, green and blue
56light are added together in various ways to reproduce a broad array of colors.
36 57
37**GCC** 58**GCC**
38The GNU Compiler Collection is a compiler system produced by the GNU Project supporting various programming languages. 59The GNU Compiler Collection is a compiler system produced by the GNU Project
60supporting various programming languages.
39 61
40## Data encoding 62## Data encoding
41 63
42**TL;DR:** Encoding involves the use of a code to change original data into a form that can be used by an external process. 64**TL;DR:** Encoding involves the use of a code to change original data into a
65form that can be used by an external process.
43 66
44Encoding is the process of converting data into a format required for a number of information processing needs, including: 67Encoding is the process of converting data into a format required for a number
68of information processing needs, including:
45 69
46- Program compiling and execution 70- Program compiling and execution
47- Data transmission, storage and compression/decompression 71- Data transmission, storage and compression/decompression
@@ -49,7 +73,9 @@ Encoding is the process of converting data into a format required for a number o
49 73
50Encoding can have two meanings: 74Encoding can have two meanings:
51 75
52- In computer technology, encoding is the process of applying a specific code, such as letters, symbols and numbers, to data for conversion into an equivalent cipher. 76- In computer technology, encoding is the process of applying a specific code,
77 such as letters, symbols and numbers, to data for conversion into an
78 equivalent cipher.
53- In electronics, encoding refers to analog to digital conversion. 79- In electronics, encoding refers to analog to digital conversion.
54 80
55## Quick history of DNA 81## Quick history of DNA
@@ -73,13 +99,19 @@ Encoding can have two meanings:
73 99
74## What is DNA? 100## What is DNA?
75 101
76Deoxyribonucleic acid, a self-replicating material which is **present in nearly all living organisms** as the main constituent of chromosomes. It is the **carrier of genetic information**. 102Deoxyribonucleic acid, a self-replicating material which is
103**present in nearly all living organisms** as the main constituent of
104chromosomes. It is the **carrier of genetic information**.
77 105
78> The nitrogen in our DNA, the calcium in our teeth, the iron in our blood, the carbon in our apple pies were made in the interiors of collapsing stars. We are made of starstuff. 106> The nitrogen in our DNA, the calcium in our teeth, the iron in our blood,
79> 107> the carbon in our apple pies were made in the interiors of collapsing stars.
108> We are made of starstuff.
80> **-- Carl Sagan, Cosmos** 109> **-- Carl Sagan, Cosmos**
81 110
82The nucleotide in DNA consists of a sugar (deoxyribose), one of four bases (cytosine (C), thymine (T), adenine (A), guanine (G)), and a phosphate. Cytosine and thymine are pyrimidine bases, while adenine and guanine are purine bases. The sugar and the base together are called a nucleoside. 111The nucleotide in DNA consists of a sugar (deoxyribose), one of four bases
112(cytosine (C), thymine (T), adenine (A), guanine (G)), and a phosphate.
113Cytosine and thymine are pyrimidine bases, while adenine and guanine are purine
114bases. The sugar and the base together are called a nucleoside.
83 115
84![DNA](/assets/dna-sequence/dna-basics.jpg) 116![DNA](/assets/dna-sequence/dna-basics.jpg)
85 117
@@ -88,6 +120,7 @@ The nucleotide in DNA consists of a sugar (deoxyribose), one of four bases (cyto
88## Encode binary data into DNA sequence 120## Encode binary data into DNA sequence
89 121
90As an input file you can use any file you want: 122As an input file you can use any file you want:
123
91- ASCII files, 124- ASCII files,
92- Compiled programs, 125- Compiled programs,
93- Multimedia files (MP3, MP4, MVK, etc), 126- Multimedia files (MP3, MP4, MVK, etc),
@@ -95,15 +128,19 @@ As an input file you can use any file you want:
95- Database files, 128- Database files,
96- etc. 129- etc.
97 130
98Note: If you would copy all the bytes from RAM to file or pipe data to file you could encode also this data as long as you provide file pointer to the encoder. 131Note: If you would copy all the bytes from RAM to file or pipe data to file you
132could encode also this data as long as you provide file pointer to the encoder.
99 133
100### Basic Encoding 134### Basic Encoding
101 135
102As already mentioned, the Basic Encoding is based on a simple mapping. Since DNA is composed of 4 nucleotides (Adenine, Cytosine, Guanine, Thymine; usually referred using the first letter). Using this technique we can encode 136As already mentioned, the Basic Encoding is based on a simple mapping. Since
137DNA is composed of 4 nucleotides (Adenine, Cytosine, Guanine, Thymine; usually
138referred using the first letter). Using this technique we can encode
103 139
104$$ log_2(4) = log_2(2^2) = 2 bits $$ 140$$ log_2(4) = log_2(2^2) = 2 bits $$
105 141
106using a single nucleotide. In this way, we are able to use the 4 bases that compose the DNA strand to encode each byte of data. 142using a single nucleotide. In this way, we are able to use the 4 bases that
143compose the DNA strand to encode each byte of data.
107 144
108| Two bits | Nucleotides | 145| Two bits | Nucleotides |
109| -------- | ---------------- | 146| -------- | ---------------- |
@@ -112,7 +149,8 @@ using a single nucleotide. In this way, we are able to use the 4 bases that comp
112| 01 | **C** (Cytosine) | 149| 01 | **C** (Cytosine) |
113| 11 | **T** (Thymine) | 150| 11 | **T** (Thymine) |
114 151
115With this in mind we can simply encode any data by using two-bit to Nucleotides conversion. 152With this in mind we can simply encode any data by using two-bit to
153Nucleotides conversion.
116 154
117```python 155```python
118{ Algorithm 1: Naive byte array to DNA encode } 156{ Algorithm 1: Naive byte array to DNA encode }
@@ -135,15 +173,29 @@ begin
135end 173end
136``` 174```
137 175
138Another encoding would be **Goldman encoding**. Using this encoding helps with Nonsense mutation (amino acids replaced by a stop codon) that occurs and is the most problematic during translation because it leads to truncated amino acid sequences, which in turn results in truncated proteins. 176Another encoding would be **Goldman encoding**. Using this encoding helps with
177Nonsense mutation (amino acids replaced by a stop codon) that occurs and is
178the most problematic during translation because it leads to truncated amino
179acid sequences, which in turn results in truncated proteins.
139 180
140[Where to store big data? In DNA: Nick Goldman at TEDxPrague](https://www.youtube.com/watch?v=a4PiGWNsIEU) 181[Where to store big data? In DNA: Nick Goldman at TEDxPrague](https://www.youtube.com/watch?v=a4PiGWNsIEU)
141 182
142### FASTA file format 183### FASTA file format
143 184
144In bioinformatics, FASTA format is a text-based format for representing either nucleotide sequences or peptide sequences, in which nucleotides or amino acids are represented using single-letter codes. The format also allows for sequence names and comments to precede the sequences. The format originates from the FASTA software package, but has now become a standard in the field of bioinformatics. 185In bioinformatics, FASTA format is a text-based format for representing either
145 186nucleotide sequences or peptide sequences, in which nucleotides or amino acids
146The first line in a FASTA file started either with a ">" (greater-than) symbol or, less frequently, a ";" (semicolon) was taken as a comment. Subsequent lines starting with a semicolon would be ignored by software. Since the only comment used was the first, it quickly became used to hold a summary description of the sequence, often starting with a unique library accession number, and with time it has become commonplace to always use ">" for the first line and to not use ";" comments (which would otherwise be ignored). 187are represented using single-letter codes. The format also allows for sequence
188names and comments to precede the sequences. The format originates from the
189FASTA software package, but has now become a standard in the field of
190bioinformatics.
191
192The first line in a FASTA file started either with a ">" (greater-than) symbol
193or, less frequently, a ";" (semicolon) was taken as a comment. Subsequent
194lines starting with a semicolon would be ignored by software. Since the only
195comment used was the first, it quickly became used to hold a summary
196description of the sequence, often starting with a unique library accession
197number, and with time it has become commonplace to always use ">" for the first
198line and to not use ";" comments (which would otherwise be ignored).
147 199
148``` 200```
149;LCBO - Prolactin precursor - Bovine 201;LCBO - Prolactin precursor - Bovine
@@ -166,7 +218,8 @@ GLMPFLHTSKHRSMMLRPLSQALFWTLTMDLLTLTWIGSQPVEYPYTIIGQMASILYFSIILAFLPIAGX
166IENY 218IENY
167``` 219```
168 220
169FASTA format was extended by [FASTQ](https://en.wikipedia.org/wiki/FASTQ_format) format from the [Sanger Centre](https://www.sanger.ac.uk/) in Cambridge. 221FASTA format was extended by [FASTQ](https://en.wikipedia.org/wiki/FASTQ_format)
222format from the [Sanger Centre](https://www.sanger.ac.uk/) in Cambridge.
170 223
171### PNG encoded DNA sequence 224### PNG encoded DNA sequence
172 225
@@ -177,7 +230,8 @@ FASTA format was extended by [FASTQ](https://en.wikipedia.org/wiki/FASTQ_format)
177| C ➞ Cytosine | (255,0,0) | Red | 230| C ➞ Cytosine | (255,0,0) | Red |
178| T ➞ Thymine | (255,255,0) | Yellow | 231| T ➞ Thymine | (255,255,0) | Yellow |
179 232
180With this in mind we can create a simple algorithm to create PNG representation of a DNA sequence. 233With this in mind we can create a simple algorithm to create PNG representation
234of a DNA sequence.
181 235
182```python 236```python
183{ Algorithm 2: Naive DNA to PNG encode from FASTA file } 237{ Algorithm 2: Naive DNA to PNG encode from FASTA file }
@@ -198,9 +252,11 @@ end
198 252
199## Encoding text file in practice 253## Encoding text file in practice
200 254
201In this example we will take a simple text file as our input stream for encoding. This file will have a quote from Niels Bohr and saved as txt file. 255In this example we will take a simple text file as our input stream for
256encoding. This file will have a quote from Niels Bohr and saved as txt file.
202 257
203> How wonderful that we have met with a paradox. Now we have some hope of making progress. 258> How wonderful that we have met with a paradox. Now we have some hope of
259> making progress.
204> ― Niels Bohr 260> ― Niels Bohr
205 261
206First we encode text file into FASTA file. 262First we encode text file into FASTA file.
@@ -250,7 +306,8 @@ After encoding into PNG format this file looks like this.
250 306
251The larger the input stream is the larger the PNG file would be. 307The larger the input stream is the larger the PNG file would be.
252 308
253Compiled basic Hello World C program with [GCC](https://www.gnu.org/software/gcc/) would [look like](/assets/dna-sequence/sample.png). 309Compiled basic Hello World C program with [GCC](https://www.gnu.org/software/gcc/)
310would [look like](/assets/dna-sequence/sample.png).
254 311
255```c 312```c
256// gcc -O3 -o sample sample.c 313// gcc -O3 -o sample sample.c
@@ -265,10 +322,12 @@ main() {
265## Toolkit for encoding data 322## Toolkit for encoding data
266 323
267I have created a toolkit with two main programs: 324I have created a toolkit with two main programs:
325
268- dnae-encode (encodes file into FASTA file) 326- dnae-encode (encodes file into FASTA file)
269- dnae-png (encodes FASTA file into PNG) 327- dnae-png (encodes FASTA file into PNG)
270 328
271Toolkit with full source code is available on [github.com/mitjafelicijan/dna-encoding](https://github.com/mitjafelicijan/dna-encoding). 329Toolkit with full source code is available on
330[github.com/mitjafelicijan/dna-encoding](https://github.com/mitjafelicijan/dna-encoding).
272 331
273### dnae-encode 332### dnae-encode
274 333
@@ -311,11 +370,13 @@ First we generate some binary sample data with dd.
311dd if=<(openssl enc -aes-256-ctr -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt < /dev/zero) of=1KB.bin bs=1KB count=1 iflag=fullblock 370dd if=<(openssl enc -aes-256-ctr -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt < /dev/zero) of=1KB.bin bs=1KB count=1 iflag=fullblock
312``` 371```
313 372
314Our freshly generated 1KB file looks something like this (its full of garbage data as intended). 373Our freshly generated 1KB file looks something like this (its full of garbage
374data as intended).
315 375
316![Sample binary file 1KB](/assets/dna-sequence/sample-binary-file.png) 376![Sample binary file 1KB](/assets/dna-sequence/sample-binary-file.png)
317 377
318We create following binary files: 378We create following binary files:
379
319- 1KB.bin 380- 1KB.bin
320- 10KB.bin 381- 10KB.bin
321- 100KB.bin 382- 100KB.bin
@@ -323,7 +384,8 @@ We create following binary files:
323- 10MB.bin 384- 10MB.bin
324- 100MB.bin 385- 100MB.bin
325 386
326After this we create FASTA files for all the binary files by encoding them into DNA sequence. 387After this we create FASTA files for all the binary files by encoding them
388into DNA sequence.
327 389
328```bash 390```bash
329./dnae-encode -i 100MB.bin -o 100MB.fa 391./dnae-encode -i 100MB.bin -o 100MB.fa
@@ -348,3 +410,4 @@ gzip -9 < 10MB.fa > 10MB.fa.gz
348- https://opentextbc.ca/biology/chapter/9-1-the-structure-of-dna/ 410- https://opentextbc.ca/biology/chapter/9-1-the-structure-of-dna/
349- https://arxiv.org/abs/1801.04774 411- https://arxiv.org/abs/1801.04774
350- https://en.wikipedia.org/wiki/FASTA_format 412- https://en.wikipedia.org/wiki/FASTA_format
413