aboutsummaryrefslogtreecommitdiff
path: root/content/posts/2019-01-03-encoding-binary-data-into-dna-sequence.md
diff options
context:
space:
mode:
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.md143
1 files changed, 70 insertions, 73 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 a57201f..f0343ae 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
@@ -7,64 +7,59 @@ draft: false
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 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 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 12computer programs from it. Well, this is all possible now. It was not done on a
13on a large scale because it is quite expensive to create DNA strands but it's 13large scale because it is quite expensive to create DNA strands but it's
14possible. 14possible.
15 15
16Encoding data into DNA sequence is relatively simple process once you understand 16Encoding data into DNA sequence is relatively simple process once you understand
17the relationship between binary data and nucleotides and scientists have been 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 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 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 20global disaster. We could imprint all the world's knowledge into plants and
21ensure the survival of our knowledge. 21ensure the survival of our knowledge.
22 22
23More optimistic usage for this technology would be easier storage of ever 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 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 25enough and cheaper this could mean the next evolution of storing data and
26abandoning classical hard and solid state drives in data warehouses. 26abandoning classical hard and solid state drives in data warehouses.
27 27
28As we currently stand this is still not viable but it is quite an amazing and 28As we currently stand this is still not viable but it is quite an amazing and
29cool technology. 29cool technology.
30 30
31My interests in this field are purely in encoding processes and experimental 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 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 33initial goal was to create a toolkit that can be used by everybody to encode
34their data into a proper DNA sequence. 34their data into a proper DNA sequence.
35 35
36## Glossary 36## Glossary
37 37
38**deoxyribose** 38**deoxyribose** A five-carbon sugar molecule with a hydrogen atom rather than a
39A five-carbon sugar molecule with a hydrogen atom rather than a hydroxyl group 39hydroxyl group in the 2′ position; the sugar component of DNA nucleotides.
40in the 2′ position; the sugar component of DNA nucleotides.
41 40
42**double helix** 41**double helix** The molecular shape of DNA in which two strands of nucleotides
43The molecular shape of DNA in which two strands of nucleotides wind around 42wind around each other in a spiral shape.
44each other in a spiral shape.
45 43
46**nitrogenous base** 44**nitrogenous base** A nitrogen-containing molecule that acts as a base; often
47A nitrogen-containing molecule that acts as a base; often referring to one of 45referring to one of the purine or pyrimidine components of nucleic acids.
48the purine or pyrimidine components of nucleic acids.
49 46
50**phosphate group** 47**phosphate group** A molecular group consisting of a central phosphorus atom
51A molecular group consisting of a central phosphorus atom bound to four oxygen 48bound to four oxygen atoms.
52atoms.
53 49
54**RGB** 50**RGB** The RGB color model is an additive color model in which red, green and
55The RGB color model is an additive color model in which red, green and blue 51blue light are added together in various ways to reproduce a broad array of
56light are added together in various ways to reproduce a broad array of colors. 52colors.
57 53
58**GCC** 54**GCC** The GNU Compiler Collection is a compiler system produced by the GNU
59The GNU Compiler Collection is a compiler system produced by the GNU Project 55Project supporting various programming languages.
60supporting various programming languages.
61 56
62## Data encoding 57## Data encoding
63 58
64**TL;DR:** Encoding involves the use of a code to change original data into a 59**TL;DR:** Encoding involves the use of a code to change original data into a
65form that can be used by an external process. 60form that can be used by an external process.
66 61
67Encoding is the process of converting data into a format required for a number 62Encoding is the process of converting data into a format required for a number
68of information processing needs, including: 63of information processing needs, including:
69 64
70- Program compiling and execution 65- Program compiling and execution
@@ -74,7 +69,7 @@ of information processing needs, including:
74Encoding can have two meanings: 69Encoding can have two meanings:
75 70
76- In computer technology, encoding is the process of applying a specific code, 71- 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 72 such as letters, symbols and numbers, to data for conversion into an
78 equivalent cipher. 73 equivalent cipher.
79- In electronics, encoding refers to analog to digital conversion. 74- In electronics, encoding refers to analog to digital conversion.
80 75
@@ -99,23 +94,25 @@ Encoding can have two meanings:
99 94
100## What is DNA? 95## What is DNA?
101 96
102Deoxyribonucleic acid, a self-replicating material which is 97Deoxyribonucleic acid, a self-replicating material which is **present in nearly
103**present in nearly all living organisms** as the main constituent of 98all living organisms** as the main constituent of chromosomes. It is the
104chromosomes. It is the **carrier of genetic information**. 99**carrier of genetic information**.
105 100
106> The nitrogen in our DNA, the calcium in our teeth, the iron in our blood, 101> The nitrogen in our DNA, the calcium in our teeth, the iron in our blood,
107> the carbon in our apple pies were made in the interiors of collapsing stars. 102> the carbon in our apple pies were made in the interiors of collapsing stars.
108> We are made of starstuff. 103> We are made of starstuff.
109> **-- Carl Sagan, Cosmos** 104> **-- Carl Sagan, Cosmos**
110 105
111The nucleotide in DNA consists of a sugar (deoxyribose), one of four bases 106The nucleotide in DNA consists of a sugar (deoxyribose), one of four bases
112(cytosine (C), thymine (T), adenine (A), guanine (G)), and a phosphate. 107(cytosine (C), thymine (T), adenine (A), guanine (G)), and a phosphate.
113Cytosine and thymine are pyrimidine bases, while adenine and guanine are purine 108Cytosine and thymine are pyrimidine bases, while adenine and guanine are purine
114bases. The sugar and the base together are called a nucleoside. 109bases. The sugar and the base together are called a nucleoside.
115 110
116![DNA](/assets/dna-sequence/dna-basics.jpg) 111![DNA](/assets/dna-sequence/dna-basics.jpg)
117 112
118*DNA (a) forms a double stranded helix, and (b) adenine pairs with thymine and cytosine pairs with guanine. (credit a: modification of work by Jerome Walker, Dennis Myts)* 113*DNA (a) forms a double stranded helix, and (b) adenine pairs with thymine and
114cytosine pairs with guanine. (credit a: modification of work by Jerome Walker,
115Dennis Myts)*
119 116
120## Encode binary data into DNA sequence 117## Encode binary data into DNA sequence
121 118
@@ -128,18 +125,18 @@ As an input file you can use any file you want:
128- Database files, 125- Database files,
129- etc. 126- etc.
130 127
131Note: If you would copy all the bytes from RAM to file or pipe data to file you 128Note: 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. 129could encode also this data as long as you provide file pointer to the encoder.
133 130
134### Basic Encoding 131### Basic Encoding
135 132
136As already mentioned, the Basic Encoding is based on a simple mapping. Since 133As already mentioned, the Basic Encoding is based on a simple mapping. Since DNA
137DNA is composed of 4 nucleotides (Adenine, Cytosine, Guanine, Thymine; usually 134is composed of 4 nucleotides (Adenine, Cytosine, Guanine, Thymine; usually
138referred using the first letter). Using this technique we can encode 135referred using the first letter). Using this technique we can encode
139 136
140$$ log_2(4) = log_2(2^2) = 2 bits $$ 137$$ log_2(4) = log_2(2^2) = 2 bits $$
141 138
142using a single nucleotide. In this way, we are able to use the 4 bases that 139using 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. 140compose the DNA strand to encode each byte of data.
144 141
145| Two bits | Nucleotides | 142| Two bits | Nucleotides |
@@ -149,8 +146,8 @@ compose the DNA strand to encode each byte of data.
149| 01 | **C** (Cytosine) | 146| 01 | **C** (Cytosine) |
150| 11 | **T** (Thymine) | 147| 11 | **T** (Thymine) |
151 148
152With this in mind we can simply encode any data by using two-bit to 149With this in mind we can simply encode any data by using two-bit to Nucleotides
153Nucleotides conversion. 150conversion.
154 151
155```python 152```python
156{ Algorithm 1: Naive byte array to DNA encode } 153{ Algorithm 1: Naive byte array to DNA encode }
@@ -173,29 +170,29 @@ begin
173end 170end
174``` 171```
175 172
176Another encoding would be **Goldman encoding**. Using this encoding helps with 173Another encoding would be **Goldman encoding**. Using this encoding helps with
177Nonsense mutation (amino acids replaced by a stop codon) that occurs and is 174Nonsense mutation (amino acids replaced by a stop codon) that occurs and is the
178the most problematic during translation because it leads to truncated amino 175most problematic during translation because it leads to truncated amino acid
179acid sequences, which in turn results in truncated proteins. 176sequences, which in turn results in truncated proteins.
180 177
181[Where to store big data? In DNA: Nick Goldman at TEDxPrague](https://www.youtube.com/watch?v=a4PiGWNsIEU) 178[Where to store big data? In DNA: Nick Goldman at TEDxPrague](https://www.youtube.com/watch?v=a4PiGWNsIEU)
182 179
183### FASTA file format 180### FASTA file format
184 181
185In bioinformatics, FASTA format is a text-based format for representing either 182In bioinformatics, FASTA format is a text-based format for representing either
186nucleotide sequences or peptide sequences, in which nucleotides or amino acids 183nucleotide sequences or peptide sequences, in which nucleotides or amino acids
187are represented using single-letter codes. The format also allows for sequence 184are represented using single-letter codes. The format also allows for sequence
188names and comments to precede the sequences. The format originates from the 185names and comments to precede the sequences. The format originates from the
189FASTA software package, but has now become a standard in the field of 186FASTA software package, but has now become a standard in the field of
190bioinformatics. 187bioinformatics.
191 188
192The first line in a FASTA file started either with a ">" (greater-than) symbol 189The first line in a FASTA file started either with a ">" (greater-than) symbol
193or, less frequently, a ";" (semicolon) was taken as a comment. Subsequent 190or, less frequently, a ";" (semicolon) was taken as a comment. Subsequent lines
194lines starting with a semicolon would be ignored by software. Since the only 191starting with a semicolon would be ignored by software. Since the only comment
195comment used was the first, it quickly became used to hold a summary 192used was the first, it quickly became used to hold a summary description of the
196description of the sequence, often starting with a unique library accession 193sequence, often starting with a unique library accession number, and with time
197number, and with time it has become commonplace to always use ">" for the first 194it has become commonplace to always use ">" for the first line and to not use
198line and to not use ";" comments (which would otherwise be ignored). 195";" comments (which would otherwise be ignored).
199 196
200``` 197```
201;LCBO - Prolactin precursor - Bovine 198;LCBO - Prolactin precursor - Bovine
@@ -218,7 +215,7 @@ GLMPFLHTSKHRSMMLRPLSQALFWTLTMDLLTLTWIGSQPVEYPYTIIGQMASILYFSIILAFLPIAGX
218IENY 215IENY
219``` 216```
220 217
221FASTA format was extended by [FASTQ](https://en.wikipedia.org/wiki/FASTQ_format) 218FASTA format was extended by [FASTQ](https://en.wikipedia.org/wiki/FASTQ_format)
222format from the [Sanger Centre](https://www.sanger.ac.uk/) in Cambridge. 219format from the [Sanger Centre](https://www.sanger.ac.uk/) in Cambridge.
223 220
224### PNG encoded DNA sequence 221### PNG encoded DNA sequence
@@ -230,7 +227,7 @@ format from the [Sanger Centre](https://www.sanger.ac.uk/) in Cambridge.
230| C ➞ Cytosine | (255,0,0) | Red | 227| C ➞ Cytosine | (255,0,0) | Red |
231| T ➞ Thymine | (255,255,0) | Yellow | 228| T ➞ Thymine | (255,255,0) | Yellow |
232 229
233With this in mind we can create a simple algorithm to create PNG representation 230With this in mind we can create a simple algorithm to create PNG representation
234of a DNA sequence. 231of a DNA sequence.
235 232
236```python 233```python
@@ -252,7 +249,7 @@ end
252 249
253## Encoding text file in practice 250## Encoding text file in practice
254 251
255In this example we will take a simple text file as our input stream for 252In 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. 253encoding. This file will have a quote from Niels Bohr and saved as txt file.
257 254
258> How wonderful that we have met with a paradox. Now we have some hope of 255> How wonderful that we have met with a paradox. Now we have some hope of
@@ -306,8 +303,9 @@ After encoding into PNG format this file looks like this.
306 303
307The larger the input stream is the larger the PNG file would be. 304The larger the input stream is the larger the PNG file would be.
308 305
309Compiled basic Hello World C program with [GCC](https://www.gnu.org/software/gcc/) 306Compiled basic Hello World C program with
310would [look like](/assets/dna-sequence/sample.png). 307[GCC](https://www.gnu.org/software/gcc/) would [look
308like](/assets/dna-sequence/sample.png).
311 309
312```c 310```c
313// gcc -O3 -o sample sample.c 311// gcc -O3 -o sample sample.c
@@ -370,7 +368,7 @@ First we generate some binary sample data with dd.
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 368dd 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
371``` 369```
372 370
373Our freshly generated 1KB file looks something like this (its full of garbage 371Our freshly generated 1KB file looks something like this (its full of garbage
374data as intended). 372data as intended).
375 373
376![Sample binary file 1KB](/assets/dna-sequence/sample-binary-file.png) 374![Sample binary file 1KB](/assets/dna-sequence/sample-binary-file.png)
@@ -410,4 +408,3 @@ gzip -9 < 10MB.fa > 10MB.fa.gz
410- https://opentextbc.ca/biology/chapter/9-1-the-structure-of-dna/ 408- https://opentextbc.ca/biology/chapter/9-1-the-structure-of-dna/
411- https://arxiv.org/abs/1801.04774 409- https://arxiv.org/abs/1801.04774
412- https://en.wikipedia.org/wiki/FASTA_format 410- https://en.wikipedia.org/wiki/FASTA_format
413