{ "cells": [ { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "# Counte mRNA inside Tracking Cells" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "Collapsed": "false", "collapsed": true, "jupyter": { "outputs_hidden": true } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "┌ Info: Precompiling Images [916415d5-f1e6-5110-898d-aaa5f9f070e0]\n", "└ @ Base loading.jl:1260\n", "WARNING: ImageQualityIndexes.psnr is deprecated, use assess_psnr instead.\n", " likely near /home/hf/.julia/packages/Images/0SDGT/src/Images.jl:60\n", "┌ Info: Precompiling ImageSegmentation [80713f31-8817-5129-9cf8-209ff8fb23e1]\n", "└ @ Base loading.jl:1260\n", "┌ Info: Precompiling Plots [91a5bcdd-55d7-5caf-9e0b-520d859cae80]\n", "└ @ Base loading.jl:1260\n", "┌ Info: Precompiling Distributions [31c24e10-a181-5473-b8eb-7969acd0382f]\n", "└ @ Base loading.jl:1260\n", "┌ Info: Precompiling GaussianMixtures [cc18c42c-b769-54ff-9e2a-b28141a64aae]\n", "└ @ Base loading.jl:1260\n" ] } ], "source": [ "using FileIO, Images\n", "using ImageSegmentation\n", "using Plots\n", "using Random\n", "using Distributions\n", "using GaussianMixtures\n", "using Statistics" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "get_random_color (generic function with 1 method)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function get_random_color(seed)\n", " Random.seed!(seed)\n", " rand(RGB{N0f8})\n", "end" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "watershedborder (generic function with 1 method)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"\n", "Generate border form watershed result\n", "\"\"\"\n", "#function watershedborder(watershed_segments::SegmentedImage{Array{Int64,2},Float64} )\n", "function watershedborder(watershed_segments)\n", " marker_border = BitArray(undef, size(watershed_segments.image_indexmap));\n", " marker_border .=false\n", " #marker_border = zeros(Int8, size(watershed_segments.image_indexmap));\n", " for label in watershed_segments.segment_labels\n", " marker_border .|= ((watershed_segments.image_indexmap.==label) .⊻ erode(watershed_segments.image_indexmap .==label));\n", " end\n", " marker_border;\n", "end" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "data_dir = \"/datahub/rawdata/tandeng/mRNA_imaging/mRNA_confocal_hamamatsu-60X-TIRF\";\n", "img_15_1 = load(File(format\"TIFF\", \"$data_dir/20200315/HE7-11-1-80uw-const_1_s1.ome.btf.tiff\"));" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "img_16_2 = load(File(format\"TIFF\", \"$data_dir/20200316/HE7-11-1-80uw-PWM_1_s2.ome.tiff\"));" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "# 3. LoG, Distance Map" ] }, { "cell_type": "code", "execution_count": 112, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "split_cell_LoG" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"\n", "Use LoG without Distance Map fiter raw image to extract cell\n", "\"\"\"\n", "function split_cell_LoG(stack::Array{Gray{Normed{UInt16,16}},3}, time::Int)\n", " img_edge = zeros(N0f16, 1900, 1300, time);\n", " img_clear = zeros(N0f16, 1900, 1300, time);\n", " for i in 1:time\n", " imgx = mapwindow(median!, stack[:, :, 20*i+14], (5,5));\n", " imgx_log = imfilter(imgx, Kernel.LoG(40)) .< -1e-7 ;\n", " imgx_dist = distance_transform(feature_transform(imgx_log)) .^ 0.5;\n", " imgx_markers = label_components( imgx_dist .< 5);\n", " #imgx_segments = watershed( imgx_dist, imgx_markers); \n", " imgx_segments = watershed( imfilter(1 .- imgx, Kernel.gaussian(9)), imgx_markers);\n", " #imgout[:,:,i] = Gray.(imgx_segments.image_indexmap./maximum(imgx_segments.segment_labels));\n", " img_edge[:,:,i] = .~watershedborder(imgx_segments).*imgx;\n", " img_clear[:,:,i] = extract_nucleus( imgx, imgx_segments);\n", " end\n", "\t#map(i->get_random_color(i),labels_map(imgx_segments))]\n", " img_edge, img_clear;\n", "end" ] }, { "cell_type": "code", "execution_count": 151, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "extract_nucleus" ] }, "execution_count": 151, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"\n", "Extract nucleus from sperated cell\n", "\"\"\"\n", "function extract_nucleus( img, watershed_segments::SegmentedImage{Array{Int64,2},Float64} )\n", " img_clear = zeros(N0f16, size(watershed_segments.image_indexmap));\n", " img_blur = imfilter(img, Kernel.gaussian(9));\n", " for label in watershed_segments.segment_labels\n", " cell = img_blur .* (watershed_segments.image_indexmap .== label);\n", " # only select 70% brigter region or fixed area\n", " gmm = GMM(3, [pixel for pixel in real(cell) if pixel > 1e-5]);\n", " nucleus_th = sort(gmm.μ, dims=1)[end-1];\n", " img_clear .+= ( remove_small_area(cell .> nucleus_th)) .*img;\n", " end\n", " img_clear;\n", "end\n", "\n", "\"\"\"\n", "Just remove regions are small\n", "\"\"\"\n", "function remove_small_area(mask)\n", " mask_con = label_components(mask);\n", " mask_res = BitArray(undef, size(mask));\n", " mask_res .= false;cd\n", " for i in 1:maximum(mask_con)\n", " if sum(mask_con .== i) > 5e3\n", " mask_res .+= (mask_con.==i);\n", " end\n", " end\n", " mask_res;\n", "end" ] }, { "cell_type": "code", "execution_count": 298, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "split_cell_LoG" ] }, "execution_count": 298, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"\n", "Use LoG without Distance Map fiter raw image to extract cell\n", "\"\"\"\n", "function split_cell_LoG(stack::Array{Gray{Normed{UInt16,16}},3}, time::Int)\n", " img_edge = zeros(N0f16, 1900, 1300, time);\n", " img_clear = zeros(N0f16, 1900, 1300, time*20);\n", " for i in 1:time\n", " imgx = mapwindow(median!, stack[:, :, 20*i+14], (5,5));\n", " imgx_log = imfilter(imgx, Kernel.LoG(40)) .< -1e-7 ;\n", " imgx_dist = distance_transform(feature_transform(imgx_log)) .^ 0.5;\n", " imgx_markers = label_components( imgx_dist .< 5);\n", " #imgx_segments = watershed( imgx_dist, imgx_markers); \n", " imgx_segments = watershed( imfilter(1 .- imgx, Kernel.gaussian(9)), imgx_markers);\n", " #imgout[:,:,i] = Gray.(imgx_segments.image_indexmap./maximum(imgx_segments.segment_labels));\n", " img_edge[:,:,i] = .~watershedborder(imgx_segments).*imgx;\n", " img_clear_mask = extract_nucleus( imgx, imgx_segments) .> 0;\n", " for z in 1:20\n", " img_clear[:,:,(i-1)*20+z] = img_clear_mask .* stack[:,:,(i-1)*20+z]\n", " end\n", " end\n", "\t#map(i->get_random_color(i),labels_map(imgx_segments))]\n", " img_edge, img_clear;\n", "end" ] }, { "cell_type": "code", "execution_count": 192, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "using HDF5" ] }, { "cell_type": "code", "execution_count": 221, "metadata": { "Collapsed": "false" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 28.994724 seconds (18 allocations: 11.502 GiB, 0.09% gc time)\n" ] } ], "source": [ "@time h5write(\"h5testall.h5\", \"img\", rawview(real(reshape(img_15_1, 1900, 1300, 20, :))));\n", "#h5writeattr(\"bar.h5\", \"foo\", Dict(\"c\"=>\"value for metadata parameter c\",\"d\"=>\"metadata d\"))" ] }, { "cell_type": "code", "execution_count": 301, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "text/plain": [ "split_cell_LoG" ] }, "execution_count": 301, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"\n", "Use LoG without Distance Map fiter raw image to extract cell, (vesion 3)\n", "Export statistic data and find long-live cell\n", "\"\"\"\n", "function split_cell_LoG(stack::Array{Gray{Normed{UInt16,16}},3}, time::Int)\n", " img_edge = zeros(N0f16, 1900, 1300, time);\n", " img_clear = zeros(N0f16, 1900, 1300, time*20);\n", " mask_edge = zeros(Int16, 1900, 1300, time);\n", " mask_clear = zeros(Bool, 1900, 1300, time);\n", " \n", " for i in 1:time\n", " imgx = mapwindow(median!, stack[:, :, 20*i+14], (5,5));\n", " #imgx_log = imfilter(imgx.*(imgx.>(120/0xffff)), Kernel.LoG(40)) .< -1e-7 ;\n", " imgx_log = imfilter(imgx, Kernel.LoG(40)) .< -1e-7 ;\n", " imgx_dist = distance_transform(feature_transform(imgx_log)) .^ 0.5; #取阈值后再做?\n", " imgx_markers = label_components( imgx_dist .< 5); \n", " #imgx_segments = watershed( imgx_dist, imgx_markers); \n", " imgx_segments = watershed( imfilter(1 .- imgx, Kernel.gaussian(9)), imgx_markers); # 取阈值后再做?\n", " #imgout[:,:,i] = Gray.(imgx_segments.image_indexmap./maximum(imgx_segments.segment_labels));\n", " mask_edge[:,:,i] = imgx_segments.image_indexmap;\n", " img_edge[:,:,i] = .~watershedborder(imgx_segments).*imgx;\n", " img_clear_mask = extract_nucleus( imgx, imgx_segments) .> 0;\n", " mask_clear[:,:,i] = img_clear_mask;\n", " for z in 1:20\n", " img_clear[:,:,(i-1)*20+z] = img_clear_mask .* stack[:,:,(i-1)*20+z]\n", " end\n", " end\n", "\t#map(i->get_random_color(i),labels_map(imgx_segments))]\n", " img_edge, img_clear, mask_edge, mask_clear;\n", "end" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false", "scrolled": true }, "outputs": [], "source": [ "@time img_edge, img_clear, mask_edge, mask_clear = split_cell_LoG(img_16_2, 137);\n", "save(\"mask4stat.jld\",\"nucleus\", mask_clear, \"edge\", mask_edge);\n", "save(\"output/img_16_2_edge.tiff\", img_edge);\n", "save(\"output/img_16_2_clear.tiff\", img_clear[:,:,1:20:end]);\n", "#save(\"output/img_16_2_edge_z.tiff\", edge);\n", "#save(\"output/img_16_2_clear_z.tiff\", clear);" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false", "scrolled": true }, "outputs": [], "source": [ "@time edge, clear = split_cell_LoG(img_15_1, 20);\n", "save(\"output/img_15_1_edge.tiff\", edge);\n", "save(\"output/img_15_1_clear.tiff\", clear);" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false", "scrolled": true }, "outputs": [], "source": [ "@time edge, clear = split_cell_LoG(img_16_2, 1);\n", "save(\"output/img_16_2_edge_z.tiff\", edge);\n", "save(\"output/img_16_2_clear_z.tiff\", clear);" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false", "scrolled": true }, "outputs": [], "source": [ "@time out = split_cell_LoG(img_16_2, 137);\n", "save(\"output/img_16_2.tiff\", out);" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "Collapsed": "false" }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" }, { "name": "stderr", "output_type": "stream", "text": [ "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n", "GKS: character ignored due to unicode error\n" ] } ], "source": [ "edges, count = imhist(img_15_1[:,:,11]); \n", "edges2, count2 = imhist(mapwindow(median!, img_15_1[:,:,0], (5,5)));\n", "edges3, count3 = imhist(imfilter(img_15_1[:,:,10], Kernel.gaussian(3)));\n", "gr()\n", "plot(edges, count[2:end], label=\"raw\");\n", "plot!(edges2, count2[2:end], label=\"median\")\n", "plot!(edges3, count3[2:end], label=\"gaussian\")" ] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.4.0", "language": "julia", "name": "julia-1.4" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.4.0" } }, "nbformat": 4, "nbformat_minor": 4 }