Skip to content

Instantly share code, notes, and snippets.

@mr-eyes
Last active June 30, 2019 22:03
Show Gist options
  • Save mr-eyes/5df5fe1d1a5198d1a32af43517dface6 to your computer and use it in GitHub Desktop.
Save mr-eyes/5df5fe1d1a5198d1a32af43517dface6 to your computer and use it in GitHub Desktop.
Convert image to DNA & Convert DNA to Image
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1 style=\"text-align:center\"><span style=\"color:brown\">Convert image to DNA & Convert DNA to Image</span></h1>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Encoding & Decoding functions"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def encode(kmer):\n",
" _map = {\"A\": 0, \"C\": 1, \"T\": 2, \"G\": 3}\n",
" strint = 0\n",
" for n in kmer:\n",
" curr = _map[n]\n",
" strint = strint | curr\n",
" strint = strint << 2\n",
"\n",
" return strint >> 2\n",
"\n",
"\n",
"def decode(kmer, kSize):\n",
" _map = {0: 'A', 1: 'C', 2: 'T', 3: 'G'}\n",
" kmer_str = \"\"\n",
" for i in range(kSize, 0, -1):\n",
" base = (kmer >> (i*2-2)) & 3\n",
" ch = _map[base]\n",
" kmer_str += ch\n",
"\n",
" return kmer_str"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Images R&W Functions"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def image_to_binary(image_path):\n",
" img = cv2.imread(image_path, 0)\n",
" ret, bw_img = cv2.threshold(img,127,255,cv2.THRESH_BINARY)\n",
" bw_img = np.array(bw_img)\n",
" bw_imAC= np.where(bw_img==255, 1, bw_img)\n",
" return bw_imAC"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Convert image binary matrix to DNA sequence"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def binary_matrix_to_DNA(image_binary_matrix):\n",
" seq = \"\"\n",
" row_length = len(image_binary_matrix[0])\n",
" for row in image_binary_matrix:\n",
" row = list(map(str, row))\n",
" row = int(\"\".join(row), 2)\n",
" dna = decode(row,row_length)\n",
" seq += dna\n",
" \n",
" return seq"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Convert image DNA sequence to image binary matrix"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def DNA_to_binary_matrix(DNA_Seq, row_length):\n",
" binary_matrix = []\n",
" format_str = f'0{row_length}b'\n",
" for i in range(0, len(DNA_Seq), row_length):\n",
" seq = DNA_Seq[i:i + row_length]\n",
" enc = encode(seq)\n",
" binary = list(map(int, format(enc, format_str)))\n",
" binary_matrix.append(binary)\n",
" \n",
" return binary_matrix"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load image and get its binary matrix"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### View: white_diagonal.png"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"white_diagonal.png\" style=\"height:100px\">"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 0 0 0]\n",
" [0 1 0 0]\n",
" [0 0 1 0]\n",
" [0 0 0 1]]\n"
]
}
],
"source": [
"bin_array = image_to_binary(\"white_diagonal.png\")\n",
"row_length = len(bin_array[0])\n",
"print(bin_array)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Convert the binary matrix to DNA sequence"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"AATAAACAAAATAAAC\n"
]
}
],
"source": [
"DNA = binary_matrix_to_DNA(bin_array)\n",
"print(DNA)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Convert the DNA back to binary matrix"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 0 0 0]\n",
" [0 1 0 0]\n",
" [0 0 1 0]\n",
" [0 0 0 1]]\n"
]
}
],
"source": [
"bin_mat = np.array(DNA_to_binary_matrix(DNA, row_length)).astype(np.uint8)\n",
"print(bin_mat)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Write it back to an image"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Convert to grey scale\n",
"grey = cv2.threshold(bin_mat, 0, 255, cv2.THRESH_OTSU)[1]\n",
"\n",
"# Write to disk\n",
"cv2.imwrite('decoded.png', grey)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### View: decoded.png"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"decoded.png\" style=\"height:100px\">"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## ~~ Assertion ~~"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2eb8aeffa2c9129086396be6b4a5acb9 decoded.png\r\n",
"2eb8aeffa2c9129086396be6b4a5acb9 white_diagonal.png\r\n"
]
}
],
"source": [
"!md5sum decoded.png white_diagonal.png"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment