diff options
Diffstat (limited to 'content/posts/2022-07-05-what-would-dna-sound-if-synthesized.md')
| -rw-r--r-- | content/posts/2022-07-05-what-would-dna-sound-if-synthesized.md | 163 |
1 files changed, 86 insertions, 77 deletions
diff --git a/content/posts/2022-07-05-what-would-dna-sound-if-synthesized.md b/content/posts/2022-07-05-what-would-dna-sound-if-synthesized.md index 15bbf2b..e26088b 100644 --- a/content/posts/2022-07-05-what-would-dna-sound-if-synthesized.md +++ b/content/posts/2022-07-05-what-would-dna-sound-if-synthesized.md | |||
| @@ -7,37 +7,38 @@ draft: false | |||
| 7 | 7 | ||
| 8 | ## Introduction | 8 | ## Introduction |
| 9 | 9 | ||
| 10 | Lately, I have been thinking a lot about the nature of life, what are the | 10 | Lately, I have been thinking a lot about the nature of life, what are the |
| 11 | foundation blocks of life and things like that. It's remarkable how complex | 11 | foundation blocks of life and things like that. It's remarkable how complex and |
| 12 | and on the other hand simple the creation is when you look at it. The miracle | 12 | on the other hand simple the creation is when you look at it. The miracle of |
| 13 | of life keeps us grounded when our imagination goes wild. If the DNA are the | 13 | life keeps us grounded when our imagination goes wild. If the DNA are the blocks |
| 14 | blocks of life, you could consider them to be an API nature provided us to | 14 | of life, you could consider them to be an API nature provided us to better |
| 15 | better understand all of this chaos masquerading as order. | 15 | understand all of this chaos masquerading as order. |
| 16 | 16 | ||
| 17 | I have been reading a lot about superintelligence and our somehow misguided | 17 | I have been reading a lot about superintelligence and our somehow misguided path |
| 18 | path to create general artificial intelligence. What would the building blocks | 18 | to create general artificial intelligence. What would the building blocks or our |
| 19 | or our creation look like? Is the compression really the ultimate storage of | 19 | creation look like? Is the compression really the ultimate storage of |
| 20 | information? Will our creation also ponder this questions when creating new | 20 | information? Will our creation also ponder this questions when creating new |
| 21 | worlds for themselves, or will we just disappear into the vastness of | 21 | worlds for themselves, or will we just disappear into the vastness of |
| 22 | possibilities? It is a little offensive that we are playing God whilst being | 22 | possibilities? It is a little offensive that we are playing God whilst being |
| 23 | completely ignorant of our own reality. Who knows! Like many other | 23 | completely ignorant of our own reality. Who knows! Like many other |
| 24 | breakthroughs, this one will also come at a cost not known to us when it | 24 | breakthroughs, this one will also come at a cost not known to us when it finally |
| 25 | finally happens. | 25 | happens. |
| 26 | 26 | ||
| 27 | To keep things a bit lighter, I decided to convert some popular DNA sequences | 27 | To keep things a bit lighter, I decided to convert some popular DNA sequences |
| 28 | into an audio files for us to listen to. I am not the first one, nor I will be | 28 | into an audio files for us to listen to. I am not the first one, nor I will be |
| 29 | the last one to do this. But it is an interesting exercise in better | 29 | the last one to do this. But it is an interesting exercise in better |
| 30 | understanding the relationship between art and science. Maybe listening to DNA | 30 | understanding the relationship between art and science. Maybe listening to DNA |
| 31 | instead of parsing it will find a way into better understanding, or at least | 31 | instead of parsing it will find a way into better understanding, or at least |
| 32 | enjoying the creation and cryptic nature of life. | 32 | enjoying the creation and cryptic nature of life. |
| 33 | 33 | ||
| 34 | ## DNA encoding and primer example | 34 | ## DNA encoding and primer example |
| 35 | 35 | ||
| 36 | I have been exploring DNA in the past in my post from about 3 years ago in | 36 | I have been exploring DNA in the past in my post from about 3 years ago in |
| 37 | [Encoding binary data into DNA sequence](/encoding-binary-data-into-dna-sequence.html) | 37 | [Encoding binary data into DNA |
| 38 | where I have been converting all sorts of data into DNA sequences. | 38 | sequence](/encoding-binary-data-into-dna-sequence.html) where I have been |
| 39 | converting all sorts of data into DNA sequences. | ||
| 39 | 40 | ||
| 40 | This will be a similar exercise but instead of converting to DNA, I will be | 41 | This will be a similar exercise but instead of converting to DNA, I will be |
| 41 | generating tones from Nucleotides. | 42 | generating tones from Nucleotides. |
| 42 | 43 | ||
| 43 | | Nucleotides | Note | Frequency | | 44 | | Nucleotides | Note | Frequency | |
| @@ -49,12 +50,13 @@ generating tones from Nucleotides. | |||
| 49 | 50 | ||
| 50 | Since we do not have T in equal-tempered scale, I choose D to represent T note. | 51 | Since we do not have T in equal-tempered scale, I choose D to represent T note. |
| 51 | 52 | ||
| 52 | You can check [Frequencies for equal-tempered scale, A4 = 440 Hz](https://pages.mtu.edu/~suits/notefreqs.html). | 53 | You can check [Frequencies for equal-tempered scale, A4 = 440 |
| 53 | For this tuning, we also choose `Speed of Sound = 345 m/s = 1130 ft/s = 770 miles/hr`. | 54 | Hz](https://pages.mtu.edu/~suits/notefreqs.html). For this tuning, we also |
| 55 | choose `Speed of Sound = 345 m/s = 1130 ft/s = 770 miles/hr`. | ||
| 54 | 56 | ||
| 55 | Now that we have this out of the way, we can also brush up on the DNA | 57 | Now that we have this out of the way, we can also brush up on the DNA sequencing |
| 56 | sequencing a bit. This is a famous quote I also used for the encoding tests, | 58 | a bit. This is a famous quote I also used for the encoding tests, and it goes |
| 57 | and it goes like this. | 59 | like this. |
| 58 | 60 | ||
| 59 | > How wonderful that we have met with a paradox. Now we have some hope of | 61 | > How wonderful that we have met with a paradox. Now we have some hope of |
| 60 | > making progress. | 62 | > making progress. |
| @@ -72,16 +74,16 @@ AACCTCACCAAACGGGACAAGATCGCCGGCGGGCTAGTATACAAGAACGCTTGCCAGTAC | |||
| 72 | AACC | 74 | AACC |
| 73 | ``` | 75 | ``` |
| 74 | 76 | ||
| 75 | This is what we gonna work with to get things rolling forward, when creating | 77 | This is what we gonna work with to get things rolling forward, when creating |
| 76 | parser and waveform generator. | 78 | parser and waveform generator. |
| 77 | 79 | ||
| 78 | ## Parsing DNA data | 80 | ## Parsing DNA data |
| 79 | 81 | ||
| 80 | This step is rather simple one. All we need to do is parse input DNA sequence in | 82 | This step is rather simple one. All we need to do is parse input DNA sequence in |
| 81 | [FASTA format](https://en.wikipedia.org/wiki/FASTA_format) well known | 83 | [FASTA format](https://en.wikipedia.org/wiki/FASTA_format) well known in |
| 82 | in [Bioinformatics](https://en.wikipedia.org/wiki/Bioinformatics) to extract | 84 | [Bioinformatics](https://en.wikipedia.org/wiki/Bioinformatics) to extract single |
| 83 | single Nucleotides that will be converted into separate tones based on | 85 | Nucleotides that will be converted into separate tones based on equal-tempered |
| 84 | equal-tempered scale explained above. | 86 | scale explained above. |
| 85 | 87 | ||
| 86 | ```python | 88 | ```python |
| 87 | nucleotide_tone_map = { | 89 | nucleotide_tone_map = { |
| @@ -101,8 +103,9 @@ def generate_from_dna_sequence(sequence): | |||
| 101 | 103 | ||
| 102 | ## Generating sine wave | 104 | ## Generating sine wave |
| 103 | 105 | ||
| 104 | Because we are essentially creating a long stream of notes we will be appending | 106 | Because we are essentially creating a long stream of notes we will be appending |
| 105 | sine notes to a global array we will later use for creating a WAV file out of it. | 107 | sine notes to a global array we will later use for creating a WAV file out of |
| 108 | it. | ||
| 106 | 109 | ||
| 107 | ```python | 110 | ```python |
| 108 | import math | 111 | import math |
| @@ -118,7 +121,7 @@ def append_sinewave(freq=440.0, duration_milliseconds=500, volume=1.0): | |||
| 118 | return | 121 | return |
| 119 | ``` | 122 | ``` |
| 120 | 123 | ||
| 121 | The sine wave generated here is the standard beep. If you want something more | 124 | The sine wave generated here is the standard beep. If you want something more |
| 122 | aggressive, you could try a square or saw tooth waveform. | 125 | aggressive, you could try a square or saw tooth waveform. |
| 123 | 126 | ||
| 124 | ## Generating a WAV file from accumulated sine waves | 127 | ## Generating a WAV file from accumulated sine waves |
| @@ -144,13 +147,13 @@ def save_wav(file_name): | |||
| 144 | wav_file.close() | 147 | wav_file.close() |
| 145 | ``` | 148 | ``` |
| 146 | 149 | ||
| 147 | 44100 is the industry standard sample rate - CD quality. If you need to save | 150 | 44100 is the industry standard sample rate - CD quality. If you need to save on |
| 148 | on file size, you can adjust it downwards. The standard for low quality is, | 151 | file size, you can adjust it downwards. The standard for low quality is, 8000 or |
| 149 | 8000 or 8kHz. | 152 | 8kHz. |
| 150 | 153 | ||
| 151 | WAV files here are using short, 16 bit, signed integers for the sample size. | 154 | WAV files here are using short, 16 bit, signed integers for the sample size. |
| 152 | So, we multiply the floating-point data we have by 32767, the maximum value | 155 | So, we multiply the floating-point data we have by 32767, the maximum value for |
| 153 | for a short integer. | 156 | a short integer. |
| 154 | 157 | ||
| 155 | > It is theoretically possible to use the floating point -1.0 to 1.0 data | 158 | > It is theoretically possible to use the floating point -1.0 to 1.0 data |
| 156 | > directly in a WAV file, but not obvious how to do that using the wave module | 159 | > directly in a WAV file, but not obvious how to do that using the wave module |
| @@ -159,8 +162,9 @@ for a short integer. | |||
| 159 | ## Generating Spectograms | 162 | ## Generating Spectograms |
| 160 | 163 | ||
| 161 | I have tried two methods of doing this and both were just fine. I however opted | 164 | I have tried two methods of doing this and both were just fine. I however opted |
| 162 | out to use the [SoX - Sound eXchange, the Swiss Army knife of audio manipulation](https://linux.die.net/man/1/sox) | 165 | out to use the [SoX - Sound eXchange, the Swiss Army knife of audio |
| 163 | one because it didn't require anything else. | 166 | manipulation](https://linux.die.net/man/1/sox) one because it didn't require |
| 167 | anything else. | ||
| 164 | 168 | ||
| 165 | ```shell | 169 | ```shell |
| 166 | sox output.wav -n spectrogram -o spectrogram.png | 170 | sox output.wav -n spectrogram -o spectrogram.png |
| @@ -174,8 +178,9 @@ An example spectrogram of Ludwig van Beethoven Symphony No. 6 First movement. | |||
| 174 | 178 | ||
| 175 |  | 179 |  |
| 176 | 180 | ||
| 177 | The other option could also be in combination with [gnuplot](http://www.gnuplot.info/). | 181 | The other option could also be in combination with |
| 178 | This would require an intermediary step, however. | 182 | [gnuplot](http://www.gnuplot.info/). This would require an intermediary step, |
| 183 | however. | ||
| 179 | 184 | ||
| 180 | ```shell | 185 | ```shell |
| 181 | sox output.wav audio.dat | 186 | sox output.wav audio.dat |
| @@ -183,7 +188,8 @@ tail -n+3 audio.dat > audio_only.dat | |||
| 183 | gnuplot audio.gpi | 188 | gnuplot audio.gpi |
| 184 | ``` | 189 | ``` |
| 185 | 190 | ||
| 186 | And input file `audio.gpi` that would be passed to gnuplot looks something like this. | 191 | And input file `audio.gpi` that would be passed to gnuplot looks something like |
| 192 | this. | ||
| 187 | 193 | ||
| 188 | ``` | 194 | ``` |
| 189 | # set output format and size | 195 | # set output format and size |
| @@ -214,9 +220,9 @@ plot "audio_only.dat" with lines lt rgb 'red' | |||
| 214 | 220 | ||
| 215 | ## Pre-generated sequences | 221 | ## Pre-generated sequences |
| 216 | 222 | ||
| 217 | What I did was take interesting parts from an animal's genome and feed it to a | 223 | What I did was take interesting parts from an animal's genome and feed it to a |
| 218 | tone generator script. This then generated a WAV file and I converted those to | 224 | tone generator script. This then generated a WAV file and I converted those to |
| 219 | MP3, so they can be played in a browser. The last step was creating a | 225 | MP3, so they can be played in a browser. The last step was creating a |
| 220 | spectrogram based on a WAV file. | 226 | spectrogram based on a WAV file. |
| 221 | 227 | ||
| 222 | ### Niels Bohr quote | 228 | ### Niels Bohr quote |
| @@ -229,8 +235,9 @@ spectrogram based on a WAV file. | |||
| 229 | 235 | ||
| 230 | ### Mouse | 236 | ### Mouse |
| 231 | 237 | ||
| 232 | This is part of a mouse genome `Mus_musculus.GRCm39.dna.nonchromosomal`. | 238 | This is part of a mouse genome `Mus_musculus.GRCm39.dna.nonchromosomal`. You |
| 233 | You can get [genom data here](http://ftp.ensembl.org/pub/release-106/fasta/mus_musculus/dna/). | 239 | can get [genom data |
| 240 | here](http://ftp.ensembl.org/pub/release-106/fasta/mus_musculus/dna/). | ||
| 234 | 241 | ||
| 235 | <audio controls> | 242 | <audio controls> |
| 236 | <source src="/assets/dna-synthesized/mouse/out.mp3" type="audio/mpeg"> | 243 | <source src="/assets/dna-synthesized/mouse/out.mp3" type="audio/mpeg"> |
| @@ -240,8 +247,9 @@ You can get [genom data here](http://ftp.ensembl.org/pub/release-106/fasta/mus_m | |||
| 240 | 247 | ||
| 241 | ### Bison | 248 | ### Bison |
| 242 | 249 | ||
| 243 | This is part of a bison genome `Bison_bison_bison.Bison_UMD1.0.cdna`. | 250 | This is part of a bison genome `Bison_bison_bison.Bison_UMD1.0.cdna`. You can |
| 244 | You can get [genom data here](http://ftp.ensembl.org/pub/release-106/fasta/bison_bison_bison/cdna/). | 251 | get [genom data |
| 252 | here](http://ftp.ensembl.org/pub/release-106/fasta/bison_bison_bison/cdna/). | ||
| 245 | 253 | ||
| 246 | <audio controls> | 254 | <audio controls> |
| 247 | <source src="/assets/dna-synthesized/bison/out.mp3" type="audio/mpeg"> | 255 | <source src="/assets/dna-synthesized/bison/out.mp3" type="audio/mpeg"> |
| @@ -251,8 +259,9 @@ You can get [genom data here](http://ftp.ensembl.org/pub/release-106/fasta/bison | |||
| 251 | 259 | ||
| 252 | ### Taurus | 260 | ### Taurus |
| 253 | 261 | ||
| 254 | This is part of a taurus genome `Bos_taurus.ARS-UCD1.2.cdna`. | 262 | This is part of a taurus genome `Bos_taurus.ARS-UCD1.2.cdna`. You can get |
| 255 | You can get [genom data here](http://ftp.ensembl.org/pub/release-106/fasta/bos_taurus/cdna/). | 263 | [genom data |
| 264 | here](http://ftp.ensembl.org/pub/release-106/fasta/bos_taurus/cdna/). | ||
| 256 | 265 | ||
| 257 | <audio controls> | 266 | <audio controls> |
| 258 | <source src="/assets/dna-synthesized/taurus/out.mp3" type="audio/mpeg"> | 267 | <source src="/assets/dna-synthesized/taurus/out.mp3" type="audio/mpeg"> |
| @@ -262,14 +271,14 @@ You can get [genom data here](http://ftp.ensembl.org/pub/release-106/fasta/bos_t | |||
| 262 | 271 | ||
| 263 | ## Making a drummer out of a DNA sequence | 272 | ## Making a drummer out of a DNA sequence |
| 264 | 273 | ||
| 265 | To make things even more interesting, I decided to send this data via MIDI to | 274 | To make things even more interesting, I decided to send this data via MIDI to my |
| 266 | my [Elektron Model:Samples](https://www.elektron.se/en/model-samples). This is | 275 | [Elektron Model:Samples](https://www.elektron.se/en/model-samples). This is a |
| 267 | a really cool piece of equipment that supports MIDI in via USB and 3.5 mm | 276 | really cool piece of equipment that supports MIDI in via USB and 3.5 mm audio |
| 268 | audio jack. | 277 | jack. |
| 269 | 278 | ||
| 270 | Elektron is connected to my MacBook via USB cable and audio out is patched to | 279 | Elektron is connected to my MacBook via USB cable and audio out is patched to a |
| 271 | a Sony Bluetooth speaker I have that supports 3.5 mm audio in. Elektron | 280 | Sony Bluetooth speaker I have that supports 3.5 mm audio in. Elektron doesn't |
| 272 | doesn't have internal speakers. | 281 | have internal speakers. |
| 273 | 282 | ||
| 274 |  | 283 |  |
| 275 | 284 | ||
| @@ -277,9 +286,9 @@ doesn't have internal speakers. | |||
| 277 | 286 | ||
| 278 |  | 287 |  |
| 279 | 288 | ||
| 280 | For communicating with Elektron, I choose `pygame` Python module that has MIDI | 289 | For communicating with Elektron, I choose `pygame` Python module that has MIDI |
| 281 | built in. With this, it was rather simple to send notes to the device. All I | 290 | built in. With this, it was rather simple to send notes to the device. All I did |
| 282 | did was map MIDI notes to the actual Nucleotides. | 291 | was map MIDI notes to the actual Nucleotides. |
| 283 | 292 | ||
| 284 | Before all of this I also checked Audio MIDI Setup app under MacOS and checked | 293 | Before all of this I also checked Audio MIDI Setup app under MacOS and checked |
| 285 | MIDI Studio by pressing ⌘-2. | 294 | MIDI Studio by pressing ⌘-2. |
| @@ -329,20 +338,21 @@ pygame.midi.quit() | |||
| 329 | <video src="/assets/dna-synthesized/elektron/elektron.mp4" controls></video> | 338 | <video src="/assets/dna-synthesized/elektron/elektron.mp4" controls></video> |
| 330 | 339 | ||
| 331 | All of this could be made much more interesting if I choose different | 340 | All of this could be made much more interesting if I choose different |
| 332 | instruments for different Nucleotides, or doing more funky stuff with Elektron. | 341 | instruments for different Nucleotides, or doing more funky stuff with Elektron. |
| 333 | But for now, this should be enough. It is just a proof of concept. Something | 342 | But for now, this should be enough. It is just a proof of concept. Something to |
| 334 | to play around with. | 343 | play around with. |
| 335 | 344 | ||
| 336 | ## Going even further | 345 | ## Going even further |
| 337 | 346 | ||
| 338 | As you probably notice, the end results are quite similar to each other. This | 347 | As you probably notice, the end results are quite similar to each other. This is |
| 339 | is to be expected because we are operating only with 4 notes essentially. What | 348 | to be expected because we are operating only with 4 notes essentially. What |
| 340 | could make this more interesting is using something like [Supercollider](https://supercollider.github.io/) | 349 | could make this more interesting is using something like |
| 341 | to create more interesting sounds. By transposing notes or using effects based | 350 | [Supercollider](https://supercollider.github.io/) to create more interesting |
| 342 | on repeated data in a sequence. Possibilities are endless. | 351 | sounds. By transposing notes or using effects based on repeated data in a |
| 352 | sequence. Possibilities are endless. | ||
| 343 | 353 | ||
| 344 | It is really astonishing what can be achieved with a little bit of code and an | 354 | It is really astonishing what can be achieved with a little bit of code and an |
| 345 | idea. I could see this becoming an interesting background soundscape instrument | 355 | idea. I could see this becoming an interesting background soundscape instrument |
| 346 | if done properly. It could replace random note generator with something more | 356 | if done properly. It could replace random note generator with something more |
| 347 | intriguing, biological, natural. | 357 | intriguing, biological, natural. |
| 348 | 358 | ||
| @@ -351,4 +361,3 @@ music of nature. Even though it's quite the same, it's also quite different. | |||
| 351 | The subtle differences on repeat kind of creates music on its own. Makes you | 361 | The subtle differences on repeat kind of creates music on its own. Makes you |
| 352 | wonder. It kind of puts Occam’s Razor in its place. Nature for sure loves to | 362 | wonder. It kind of puts Occam’s Razor in its place. Nature for sure loves to |
| 353 | make things as energy efficient as possible. | 363 | make things as energy efficient as possible. |
| 354 | |||
