Skip to content

Instantly share code, notes, and snippets.

@miyanari
Created September 10, 2013 19:44
Show Gist options
  • Save miyanari/6514549 to your computer and use it in GitHub Desktop.
Save miyanari/6514549 to your computer and use it in GitHub Desktop.
Output file (x, y, z, vol, dist) is saved as csv file in a specified directory. Cropped image stack for each nucleus ( from def ModiCord ) is saved as like DNA-FISH_Demo_cropNuc[number].tiff in a specified directory.
from fiji.threshold import Auto_Local_Threshold as ALT
from ij.gui import Roi
from imagescience.image import Image
from imagescience.feature import Laplacian
from process3d import Erode_, Dilate_, MinMaxMedian
from process3d import DistanceTransform3D as D3D
from Utilities import Counter3D
from ij.plugin import Thresholder, GaussianBlur3D, ZProjector, Duplicator
from ij.process import ImageProcessor
import os, csv
#####################
#Nucleus segmentation with laminCh &
#3D ROI extraction from original image &
#Export to imagelist (implist)
#####################
def CSplit_0(imp):
imps = ChannelSplitter.split(imp)
nucimp = imps[0]
return nucimp
def Zpro(nucimp):
zpnuc = ZProjector(nucimp)
zpnuc.setMethod(ZProjector.MAX_METHOD)
zpnuc.doProjection()
zpnucimp = zpnuc.getProjection()
return zpnucimp
def Gblur(zpnucimp):
gbnuc = GaussianBlur()
ip = zpnucimp.getProcessor()
gbnuc.blurGaussian(ip, 3, 3, 0.01)
def AutoLoThre(zpnucimp):
ALT().exec(zpnucimp, "Niblack", 30, 0, 0, True)
def AnaPar(zpnucimp):
rt = ResultsTable()
measopt = ParticleAnalyzer.AREA + ParticleAnalyzer.SHAPE_DESCRIPTORS + ParticleAnalyzer.CENTROID + ParticleAnalyzer.RECT
paopt = ParticleAnalyzer.EXCLUDE_EDGE_PARTICLES
paopt += ParticleAnalyzer.INCLUDE_HOLES
paopt += ParticleAnalyzer.SHOW_MASKS
paopt += ParticleAnalyzer.CLEAR_WORKSHEET
apnuc = ParticleAnalyzer(paopt, measopt, rt, 3000, 40000000, 0.6, 1.0)
apnuc.setHideOutputImage(True)
apnuc.analyze(zpnucimp)
maskimp = apnuc.getOutputImage()
return maskimp, rt
def ModiCord(rt, imp):
offset = 10
implist = []
for i in range(rt.getCounter()):
bx = rt.getValue("BX", i) - offset
by = rt.getValue("BY", i) - offset
bw = rt.getValue("Width", i) + offset*2
bh = rt.getValue("Height", i) + offset*2
imp.setRoi(int(bx), int(by), int(bw), int(bh))
dp = Duplicator()
dpimp = dp.run(imp)
root = "/Users/miyanari/Desktop/EMBL_with Kota/FISH_data/OutPut/cropped_3D_images" #specify directory
filename = str(imp.getShortTitle()) + "_cropNuc" + str(i) + ".tif" #specify file name
fullpath = os.path.join(root, filename)
fs = FileSaver(dpimp)
fs.saveAsTiffStack(fullpath)
#fs.save()
implist.append(dpimp)
#dpimp.show() #cropped image
print "Number of segmented nucleus is", len(implist)
return implist
def Med3D(nucimp):
medimp = MinMaxMedian.convolve(nucimp, MinMaxMedian.MEDIAN)
return medimp
def GauBlur3D(imp):
gb3d = GaussianBlur3D()
sigma =2.0
gb3d.blur(imp, sigma, sigma, sigma)
def autoThre(lapimp):
midslice = int(lapimp.getStackSize() / 2)
lapimp.setSlice(midslice)
ip = lapimp.getStack().getProcessor(midslice)
ip.setAutoThreshold(AutoThresholder.Method.Default, False)
IJ.setThreshold(lapimp, ip.getMinThreshold(), ip.getMaxThreshold())
IJ.run(lapimp, "Convert to Mask", "method=Default background=Light black")
return lapimp
def nucleus_LaminFunction (imp):
lami_imp = CSplit_0(imp)
#lami_imp.show()
zplami_imp=Zpro(lami_imp)
Gblur(zplami_imp)
AutoLoThre(zplami_imp)
maskimp, rt = AnaPar(zplami_imp)
#rt.show("test")
#maskimp.show()
implist = ModiCord(rt, imp) # an image list of extracted nuclei
#print implist
#implist[0].show()
return lami_imp, zplami_imp, maskimp, rt, implist
#####################
#DAPI binaryse from 3D cropped.img of DAPI channel
#####################
def CSplit_2(imp):
imps = ChannelSplitter.split(imp)
nucimp = imps[2]
return nucimp
def autoThre_DAPI(lapimp):
midslice = int(lapimp.getStackSize() / 2)
lapimp.setSlice(midslice)
ip = lapimp.getStack().getProcessor(midslice)
ip.setAutoThreshold(AutoThresholder.Method.Default, False)
IJ.setThreshold(lapimp, ip.getMinThreshold(), ip.getMaxThreshold())
IJ.run(lapimp, "Convert to Mask", "method=Mean background=Light black")
return lapimp
def Erode3D(lapimp):
er = Erode_()
ip = lapimp.getProcessor()
erimp = er.erode(lapimp, 255, True)
return erimp
def Dilate(erimp):
dil = Dilate_()
dilimp = dil.dilate(erimp, 255, True)
return dilimp
def obCount3d(dilimp):
ctnuc = Counter3D(dilimp, 128, 5000, 5000000, False, False)
ctnucob3d = ctnuc.getObject(0)
surfimp = ctnuc.getObjMap()
#ctnuc.showStatistics(True)
return surfimp
def obCount3d_noEdge(dilimp):
ctnuc = Counter3D(dilimp, 128, 50000, 5000000, True, False)
ctnucob3d = ctnuc.getObject(0)
surfimp = ctnuc.getObjMap()
return surfimp
def multi255(imp):
dstk = imp.getStack()
for i in range(dstk.getSize()):
ip = dstk.getProcessor(i +1)
ip.multiply(255.0)
def DistanceTransform3D(imp):
#binimp = IJ.getImage()
d3d = D3D()
distimp = d3d.getTransformed(imp, 0)
#distimp.show()
return distimp
def DAPI_function(imp):
nucimp =CSplit_2(imp)
GauBlur3D(nucimp)
medimp = Med3D(nucimp)
autoThre_DAPI(medimp)
#medimp.show()
erimp = Erode3D(medimp)
IJ.run(erimp, "Invert", "stack")
surfimp = obCount3d(erimp)
#surfimp.show()
multi255(surfimp)
IJ.run(surfimp, "Fill Holes", "stack")
IJ.run(surfimp, "Watershed", "stack")
#snucimp = obCount3d_noEdge(surfimp)
#snucimp.show()
distimp = DistanceTransform3D(surfimp)
#distimp.show()
return distimp
#####################
#FISH 3D spots functions
#####################
def CSplit_3(imp):
imps = ChannelSplitter.split(imp)
nucimp = imps[3]
return nucimp
def maxZprojection(stackimp):
zp = ZProjector(stackimp)
zp.setMethod(ZProjector.MAX_METHOD)
zp.doProjection()
zpimp = zp.getProjection()
return zpimp
def autoThreshold_3Dspots(imp):
zpimp = maxZprojection(imp)
ip = zpimp.getProcessor()
ip.setAutoThreshold(AutoThresholder.Method.Yen, False)
IJ.setThreshold(imp, ip.getMinThreshold(), ip.getMaxThreshold())
#for i in range(imp.getStackSize()):
# lutmode = ImageProcessor.NO_LUT_UPDATE
# imp.getStack().getProcessor(i+1).setThreshold(ip.getMinThreshold(), ip.getMaxThreshold(), lutmode)
IJ.run(imp, "Convert to Mask", "method=Default background=Light black")
return imp
def obCount3d_FISH(dilimp):
ctnuc = Counter3D(dilimp, 200, 10, 5000000, True, False)
ctnucob3d = ctnuc.getObject(0)
obimp = ctnuc.getObjMap()
#ctnuc.showStatistics(True)
objlist = ctnuc.getObjectsList()
return obimp, objlist
def divBy255(lapimp):
dstk = lapimp.getStack()
for i in range(dstk.getSize()):
ip = dstk.getProcessor(i +1)
ip.multiply(1/255.0)
def FISH_spots_function(imp):
nucimp =CSplit_3(imp)
medimp = Med3D(nucimp)
autoThreshold_3Dspots(medimp)
IJ.run(medimp, "Invert", "stack")
cimp, objlist = obCount3d_FISH(medimp)
#cimp.show()
centlist = []
for obj in objlist:
cent = obj.centroid
centlist.append([round(cent[0]), round(cent[1]), round(cent[2]), obj.size])
#print len(centlist)
return cimp, centlist
#####################
#Chr_paint function
#####################
def CSplit_1(imp):
imps = ChannelSplitter.split(imp)
nucimp = imps[1]
return nucimp
def autoThreshold_3DChr(imp):
zpimp = maxZprojection(imp)
ip = zpimp.getProcessor()
ip.setAutoThreshold(AutoThresholder.Method.Yen, False)
IJ.setThreshold(imp, ip.getMinThreshold(), ip.getMaxThreshold())
#for i in range(imp.getStackSize()):
# lutmode = ImageProcessor.NO_LUT_UPDATE
# imp.getStack().getProcessor(i+1).setThreshold(ip.getMinThreshold(), ip.getMaxThreshold(), lutmode)
IJ.run(imp, "Convert to Mask", "method=Default background=Light black")
return imp
def obCount3d_Chr(dilimp, minSize):
ctnuc = Counter3D(dilimp, 200, minSize, 5000000, True, False)
ctnucob3d = ctnuc.getObject(0)
surfimp = ctnuc.getObjMap()
#ctnuc.showStatistics(True)
IJ.setThreshold(surfimp, 1.0, 255.0)
IJ.run(surfimp, "Convert to Mask", "method=Default background=Light black")
return surfimp
def Chr_function(imp):
nucimp =CSplit_1(imp)
#nucimp.show()
medimp = Med3D(nucimp)
#medimp.show()
autoThreshold_3DChr(medimp)
IJ.run(medimp, "Invert", "stack")
cimp = obCount3d_Chr(medimp, 1000) #set value
cimp.show()
#####################
#Lamin function
#####################
def CSplit_0(imp):
imps = ChannelSplitter.split(imp)
nucimp = imps[0]
return nucimp
def LGauBlur3D(imp, sigma):
gb3d = GaussianBlur3D()
gb3d.blur(imp, sigma, sigma, sigma)
def FJ_Lap(medimp, val): #value for Laplacian
img = Image.wrap(medimp)
lap =Laplacian()
lapimg = lap.run(img, val)
lapimp = lapimg.imageplus()
return lapimp
def autoThre_Lamin(lapimp):
midslice = int(lapimp.getStackSize() / 2)
lapimp.setSlice(midslice)
ip = lapimp.getStack().getProcessor(midslice)
#ip.setAutoThreshold(AutoThresholder.Method.Default, False)
ip.setAutoThreshold(AutoThresholder.Method.Triangle, False)
#ip.setAutoThreshold(AutoThresholder.Method.Mean, False)
IJ.setThreshold(lapimp, ip.getMinThreshold(), ip.getMaxThreshold())
IJ.run(lapimp, "Convert to Mask", "method=Default background=Light black")
return lapimp
def obCount3d_Lamin(dilimp, minSize):
ctnuc = Counter3D(dilimp, 128, minSize, 500000000, True, False)
ctnucob3d = ctnuc.getObject(0)
obimp = ctnuc.getObjMap()
#ctnuc.showStatistics(True)
IJ.setThreshold(obimp, 1.0, 255.0)
IJ.run(obimp, "Convert to Mask", "method=Default background=Light black")
return obimp
def Lamin_function(imp):
nucimp =CSplit_0(imp)
#nucimp.show()
LGauBlur3D(nucimp, 1.5) #set value
medimp = Med3D(nucimp)
#medimp.show()
lapimp = FJ_Lap(medimp, 0.1) #set value
#lapimp.show()
autoThre_Lamin(lapimp)
erimp = Erode3D(lapimp)
#erimp.show()
dilimp = Dilate(erimp)
#dilimp.show()
obimp = obCount3d_Lamin(dilimp, 5000) #set value
obimp.show()
#######################
#main
#######################
def mainworkflow(imp):
lami_imp, zplami_imp, maskimp, rt, implist = nucleus_LaminFunction(imp) # selection of segmented nucleus with lamin data
#lami_imp.show() # a splitted lamin image
#zplami_imp.show() # zprojection
#maskimp.show() # after autothreshold
#rt.show("result_table_lamin_at") # result table of autothreshold
#print implist # a list of extracted nucleus
disimplist =[]
root = "/Users/miyanari/Desktop/EMBL_with Kota/CSV" #specify the folder
filename = "testdata.csv" #specify file name
fullpath = os.path.join(root, filename)
#print fullpath
f = open(fullpath, 'wb') # 'wb'
writer = csv.writer(f)
row = ["nuc_image", "x", "y", "z", "volume", "distance"] #column name
writer.writerow(row)
for aimp in implist:
cimp, centlist = FISH_spots_function(aimp)
#cimp.show() # a binarized image of DNA-FISH spots
#print centlist # list of centroids for each FISH spot, [x, y, z, vol]
distimp = DAPI_function(aimp)
#distimp.show() #a distance map of DAPI
disimplist.append(distimp)
#print centlist
for cent in centlist:
dist = distimp.getStack().getProcessor(int(round(cent[2]))).getf(int(round(cent[0])), int(round(cent[1])))
cent.append(dist)
row = [aimp, cent[0], cent[1], cent[2], cent[3], cent[4]]
writer.writerow(row)
#print centlist
f.close()
'''
'''
#Lamin_function(aimp)
#Chr_function(aimp)
#aimp.show()
# lamin - fish calculation
#output
# print len(disimplist)
# return disimplist
'''
'''
#####################
#start
#####################
imp = IJ.openImage("/Users/miyanari/Desktop/EMBL_with Kota/FISH_data/DNA-FISH_Demo.tif")
ROI = mainworkflow(imp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment