-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalyse_cells.py
169 lines (141 loc) · 6.04 KB
/
analyse_cells.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import os
import re
import glob
from tqdm import tqdm
import imageio
from multiprocessing import Pool
from functools import partial
import numpy as np
import pandas as pd
import scipy.stats
import matplotlib.pyplot as plt
import seaborn as sns
import skimage.filters
import skimage.transform
import skimage.morphology
import skimage.measure
import skimage.segmentation
import sklearn.preprocessing
def measure_objects(labels, img, channel):
"""
Measure mean, std, mad, stdev, lower quartile and upper quartile
"""
intmean = np.zeros(labels.max(), dtype=np.float32)
intstd = np.zeros(labels.max(), dtype=np.float32)
intmedian = np.zeros(labels.max(), dtype=np.float32)
intmad = np.zeros(labels.max(), dtype=np.float32)
intlower = np.zeros(labels.max(), dtype=np.float32)
intupper = np.zeros(labels.max(), dtype=np.float32)
for i,label in enumerate(range(1,labels.max()+1)):
obj = img[labels == label]
lower,median,upper = np.quantile(obj, [0.25, 0.5, 0.75])
intmean[i] = obj.mean()
intstd[i] = obj.std()
intmedian[i] = median
intmad[i] = scipy.stats.median_absolute_deviation(obj)
intlower[i] = lower
intupper[i] = upper
df = pd.DataFrame(data={channel+'_mean': intmean,
channel+'_std': intstd,
channel+'_median': intmedian,
channel+'_mad': intmad,
channel+'_lower_quartile': intlower,
channel+'_upper_quartile': intupper})
return df
def measure_spot(spot, glass='', glassApath='', BOMI=2):
selem = skimage.morphology.disk(celldil)
if BOMI==2:
nucpath = os.path.join(glassApath,"dl_segm","{}~A-Spot{}-{}.tif".format(glass,spot,channels[0][1]))
else:
nucpath = os.path.join(glassApath,"dl_segm","{}_{}_roi{}.tif".format(glass,channels[0],spot))
nucimg = imageio.imread(nucpath)
nucimg = skimage.morphology.dilation(nucimg, selem)
if BOMI==2:
masknegpath = os.path.join(glassApath,"{}~A-Spot{}-{}.tif".format(glass,spot,negmask))
else:
masknegpath = os.path.join(glassApath,"{}_{}_roi{}_{}.tif".format(glass,channels[0],spot,negmask))
try:
masknegimg = imageio.imread(masknegpath)
except:
masknegimg = np.zeros(nucimg.shape, dtype=nucimg.dtype)
nucimg[masknegimg==1] = 0
nucimg = skimage.segmentation.relabel_sequential(nucimg)[0].astype(np.uint16)
# Save masked and relabeled nuclei image
imageio.imwrite(os.path.join(glassApath,"vis_segm","{}_{}_roi{}.tif".format(glass,negmask,spot)), nucimg)
# iterate over channels in spot
df_spot = None
for channel in channels:
try:
if BOMI==2:
channelpath = os.path.join(glassApath,"{}~A-Spot{}-{}.tif".format(glass,spot,schannel))
else:
channelpath = os.path.join(glassApath,"{}_{}_roi{}.tif".format(glass,channel,spot))
img = imageio.imread(channelpath)
except:
img = np.zeros(nucimg.shape, dtype=np.uint8)
# Calculate region props for img
df_meas = measure_objects(nucimg, img, channel_map[channel])
if df_spot is None:
df_spot = pd.DataFrame(data={'glass': [glass]*df_meas.shape[0],
'spot': [spot]*df_meas.shape[0]})
df_spot = pd.concat([df_spot, df_meas], axis=1)
return df_spot
def measure_glass(glass, BOMI, pool_procs):
if BOMI==2:
glassApath = os.path.join(panelpath,glass+'~A-Spots')
else:
glassApath = os.path.join(panelpath,glass)
spots = [spotre.match(os.path.basename(x)).group(1) for x in glob.glob(os.path.join(glassApath,'*.tif'))]
spots = sorted(list(dict.fromkeys(spots)))
measurefunc = partial(measure_spot, glass=glass, glassApath=glassApath, BOMI=BOMI)
pool = Pool(processes=pool_procs)
spot_dfs = pool.map(measurefunc, spots)
glassdf = pd.concat(spot_dfs, ignore_index=True)
return glassdf
# Feature extraction methods
def fmean(img):
return [("mean", np.mean(img))]
def fmedian(img):
return [("median", np.median(img))]
def fstd(img):
return [("stdev", np.std(img))]
def fmad(img):
return [("mad", scipy.stats.median_absolute_deviation(img, axis=None))]
def fhistogram(img):
ar = img.flatten()
valrange = (0,255)
hist,edges = np.histogram(ar, bins=10, range=valrange)
return [("bin_{:d}".format(i+1),hist[i]) for i in range(hist.shape[0])]
def main():
# BOMI2 settings
"""
panelpath = "BOMI2"
channels = ['1B', '1G', '1O', '1R', '1V', '1B_PanEpiMask', '1B_PanEpiMask_dist', '2B', '2R', '2V']
channel_map = {'1B': '1DAPI', '1G': 'PDGFRB', '1O': 'PDGFRA', '1R': 'FAP', '1V': 'SMA', '1B_PanEpiMask': 'PanEpiMask', '1B_PanEpiMask_dist': 'PanEpiMask_dist', '2B': '2DAPI', '2R': 'CD34', '2V': 'PanEpi'}
negmask = 'G_RedCellMask'
spotre = re.compile("[\w\-]*Spot(\d+)[\w\-\.]*")
BOMI = 2
"""
# BOMI1 settings
panelpath = "BOMI1"
channels = ['DAPI_ORG', 'AF488_ORG', 'AF555_ORG', 'AF750_ORG', 'AF647_ORG', 'PanEpiMask', 'PanEpiMask_dist']
channel_map = {'DAPI_ORG': 'DAPI', 'AF488_ORG': 'PDGFRB', 'AF555_ORG': 'PDGFRA', 'AF750_ORG': 'FAP', 'AF647_ORG': 'SMA', 'PanEpiMask': 'PanEpiMask', 'PanEpiMask_dist': 'PanEpiMask_dist'}
negmask = 'RedCellMask'
spotre = re.compile("[\w\-]*roi(\d+)[\w\-\.]*")
BOMI = 1
# General settings
pool_procs = 10
celldil = 6
# Feature settings
featfunc = [fmean, fmedian, fstd, fmad, fhistogram]
if BOMI==2:
glasses = [x.split('~')[0] for x in os.listdir(panelpath) if os.path.isdir(os.path.join(panelpath,x))]
else:
glasses = [x for x in os.listdir(panelpath) if os.path.isdir(os.path.join(panelpath,x)) and '1-ImageExport' in x]
glasses = sorted(list(dict.fromkeys(glasses)))
# iterate over glasses
for glass in glasses:
df = measure_glass(glass, BOMI, pool_procs)
df.to_csv(os.path.join(panelpath,glass+'_features.csv'), index=False)
if __name__=="__main__":
main()