diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2023-05-26 00:40:40 +0200 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2023-05-26 00:40:40 +0200 |
| commit | 43b0708769eb61392050045b881f8e6ba39c5b66 (patch) | |
| tree | 3939579a13b8325325d5ebb8e05324a41ed78a6d /content/posts/2019-01-03-encoding-binary-data-into-dna-sequence.md | |
| parent | 49e7e7d555a6cd9810d81561fa3e98e3d64502be (diff) | |
| download | mitjafelicijan.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.md | 133 |
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 | --- |
| 2 | title: Encoding binary data into DNA sequence | 2 | title: Encoding binary data into DNA sequence |
| 3 | url: encoding-binary-data-into-dna-sequence.html | 3 | url: encoding-binary-data-into-dna-sequence.html |
| 4 | date: 2019-01-03 | 4 | date: 2019-01-03T12:00:00+02:00 |
| 5 | draft: false | 5 | draft: false |
| 6 | --- | 6 | --- |
| 7 | 7 | ||
| 8 | ## Initial thoughts | 8 | ## Initial thoughts |
| 9 | 9 | ||
| 10 | Imagine 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. | 10 | Imagine a world where you could go outside and take a leaf from a tree and put |
| 11 | it through your personal DNA sequencer and get data like music, videos or | ||
| 12 | computer programs from it. Well, this is all possible now. It was not done | ||
| 13 | on a large scale because it is quite expensive to create DNA strands but it's | ||
| 14 | possible. | ||
| 11 | 15 | ||
| 12 | Encoding 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. | 16 | Encoding data into DNA sequence is relatively simple process once you understand |
| 17 | the relationship between binary data and nucleotides and scientists have been | ||
| 18 | making large leaps in this field in order to provide viable long-term storage | ||
| 19 | solution for our data that would potentially survive our specie if case of | ||
| 20 | global disaster. We could imprint all the world's knowledge into plants and | ||
| 21 | ensure the survival of our knowledge. | ||
| 13 | 22 | ||
| 14 | More 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. | 23 | More optimistic usage for this technology would be easier storage of ever |
| 24 | growing data we produce every day. Once machines for sequencing DNA become fast | ||
| 25 | enough and cheaper this could mean the next evolution of storing data and | ||
| 26 | abandoning classical hard and solid state drives in data warehouses. | ||
| 15 | 27 | ||
| 16 | As we currently stand this is still not viable but it is quite an amazing and cool technology. | 28 | As we currently stand this is still not viable but it is quite an amazing and |
| 29 | cool technology. | ||
| 17 | 30 | ||
| 18 | My 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. | 31 | My interests in this field are purely in encoding processes and experimental |
| 32 | testing mainly because I don't have the access to this expensive machines. My | ||
| 33 | initial goal was to create a toolkit that can be used by everybody to encode | ||
| 34 | their data into a proper DNA sequence. | ||
| 19 | 35 | ||
| 20 | ## Glossary | 36 | ## Glossary |
| 21 | 37 | ||
| 22 | **deoxyribose** | 38 | **deoxyribose** |
| 23 | A five-carbon sugar molecule with a hydrogen atom rather than a hydroxyl group in the 2′ position; the sugar component of DNA nucleotides. | 39 | A five-carbon sugar molecule with a hydrogen atom rather than a hydroxyl group |
| 40 | in the 2′ position; the sugar component of DNA nucleotides. | ||
| 24 | 41 | ||
| 25 | **double helix** | 42 | **double helix** |
| 26 | The molecular shape of DNA in which two strands of nucleotides wind around each other in a spiral shape. | 43 | The molecular shape of DNA in which two strands of nucleotides wind around |
| 44 | each other in a spiral shape. | ||
| 27 | 45 | ||
| 28 | **nitrogenous base** | 46 | **nitrogenous base** |
| 29 | A nitrogen-containing molecule that acts as a base; often referring to one of the purine or pyrimidine components of nucleic acids. | 47 | A nitrogen-containing molecule that acts as a base; often referring to one of |
| 48 | the purine or pyrimidine components of nucleic acids. | ||
| 30 | 49 | ||
| 31 | **phosphate group** | 50 | **phosphate group** |
| 32 | A molecular group consisting of a central phosphorus atom bound to four oxygen atoms. | 51 | A molecular group consisting of a central phosphorus atom bound to four oxygen |
| 52 | atoms. | ||
| 33 | 53 | ||
| 34 | **RGB** | 54 | **RGB** |
| 35 | The 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. | 55 | The RGB color model is an additive color model in which red, green and blue |
| 56 | light are added together in various ways to reproduce a broad array of colors. | ||
| 36 | 57 | ||
| 37 | **GCC** | 58 | **GCC** |
| 38 | The GNU Compiler Collection is a compiler system produced by the GNU Project supporting various programming languages. | 59 | The GNU Compiler Collection is a compiler system produced by the GNU Project |
| 60 | supporting 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 |
| 65 | form that can be used by an external process. | ||
| 43 | 66 | ||
| 44 | Encoding is the process of converting data into a format required for a number of information processing needs, including: | 67 | Encoding is the process of converting data into a format required for a number |
| 68 | of 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 | ||
| 50 | Encoding can have two meanings: | 74 | Encoding 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 | ||
| 76 | Deoxyribonucleic 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**. | 102 | Deoxyribonucleic acid, a self-replicating material which is |
| 103 | **present in nearly all living organisms** as the main constituent of | ||
| 104 | chromosomes. 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 | ||
| 82 | The 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. | 111 | The nucleotide in DNA consists of a sugar (deoxyribose), one of four bases |
| 112 | (cytosine (C), thymine (T), adenine (A), guanine (G)), and a phosphate. | ||
| 113 | Cytosine and thymine are pyrimidine bases, while adenine and guanine are purine | ||
| 114 | bases. The sugar and the base together are called a nucleoside. | ||
| 83 | 115 | ||
| 84 |  | 116 |  |
| 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 | ||
| 90 | As an input file you can use any file you want: | 122 | As 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 | ||
| 98 | Note: 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. | 131 | Note: If you would copy all the bytes from RAM to file or pipe data to file you |
| 132 | could encode also this data as long as you provide file pointer to the encoder. | ||
| 99 | 133 | ||
| 100 | ### Basic Encoding | 134 | ### Basic Encoding |
| 101 | 135 | ||
| 102 | As 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 | 136 | As already mentioned, the Basic Encoding is based on a simple mapping. Since |
| 137 | DNA is composed of 4 nucleotides (Adenine, Cytosine, Guanine, Thymine; usually | ||
| 138 | referred 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 | ||
| 106 | using 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. | 142 | using a single nucleotide. In this way, we are able to use the 4 bases that |
| 143 | compose 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 | ||
| 115 | With this in mind we can simply encode any data by using two-bit to Nucleotides conversion. | 152 | With this in mind we can simply encode any data by using two-bit to |
| 153 | Nucleotides 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 | |||
| 135 | end | 173 | end |
| 136 | ``` | 174 | ``` |
| 137 | 175 | ||
| 138 | Another 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. | 176 | Another encoding would be **Goldman encoding**. Using this encoding helps with |
| 177 | Nonsense mutation (amino acids replaced by a stop codon) that occurs and is | ||
| 178 | the most problematic during translation because it leads to truncated amino | ||
| 179 | acid 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 | ||
| 144 | In 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. | 185 | In bioinformatics, FASTA format is a text-based format for representing either |
| 145 | 186 | nucleotide sequences or peptide sequences, in which nucleotides or amino acids | |
| 146 | The 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). | 187 | are represented using single-letter codes. The format also allows for sequence |
| 188 | names and comments to precede the sequences. The format originates from the | ||
| 189 | FASTA software package, but has now become a standard in the field of | ||
| 190 | bioinformatics. | ||
| 191 | |||
| 192 | The first line in a FASTA file started either with a ">" (greater-than) symbol | ||
| 193 | or, less frequently, a ";" (semicolon) was taken as a comment. Subsequent | ||
| 194 | lines starting with a semicolon would be ignored by software. Since the only | ||
| 195 | comment used was the first, it quickly became used to hold a summary | ||
| 196 | description of the sequence, often starting with a unique library accession | ||
| 197 | number, and with time it has become commonplace to always use ">" for the first | ||
| 198 | line 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 | |||
| 166 | IENY | 218 | IENY |
| 167 | ``` | 219 | ``` |
| 168 | 220 | ||
| 169 | FASTA format was extended by [FASTQ](https://en.wikipedia.org/wiki/FASTQ_format) format from the [Sanger Centre](https://www.sanger.ac.uk/) in Cambridge. | 221 | FASTA format was extended by [FASTQ](https://en.wikipedia.org/wiki/FASTQ_format) |
| 222 | format 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 | ||
| 180 | With this in mind we can create a simple algorithm to create PNG representation of a DNA sequence. | 233 | With this in mind we can create a simple algorithm to create PNG representation |
| 234 | of 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 | ||
| 201 | In 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. | 255 | In this example we will take a simple text file as our input stream for |
| 256 | encoding. 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 | ||
| 206 | First we encode text file into FASTA file. | 262 | First we encode text file into FASTA file. |
| @@ -250,7 +306,8 @@ After encoding into PNG format this file looks like this. | |||
| 250 | 306 | ||
| 251 | The larger the input stream is the larger the PNG file would be. | 307 | The larger the input stream is the larger the PNG file would be. |
| 252 | 308 | ||
| 253 | Compiled basic Hello World C program with [GCC](https://www.gnu.org/software/gcc/) would [look like](/assets/dna-sequence/sample.png). | 309 | Compiled basic Hello World C program with [GCC](https://www.gnu.org/software/gcc/) |
| 310 | would [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 | ||
| 267 | I have created a toolkit with two main programs: | 324 | I 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 | ||
| 271 | Toolkit with full source code is available on [github.com/mitjafelicijan/dna-encoding](https://github.com/mitjafelicijan/dna-encoding). | 329 | Toolkit 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. | |||
| 311 | dd 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 | 370 | dd 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 | ||
| 314 | Our freshly generated 1KB file looks something like this (its full of garbage data as intended). | 373 | Our freshly generated 1KB file looks something like this (its full of garbage |
| 374 | data as intended). | ||
| 315 | 375 | ||
| 316 |  | 376 |  |
| 317 | 377 | ||
| 318 | We create following binary files: | 378 | We 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 | ||
| 326 | After this we create FASTA files for all the binary files by encoding them into DNA sequence. | 387 | After this we create FASTA files for all the binary files by encoding them |
| 388 | into 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 | |||
