-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep_3_quantification.py
160 lines (131 loc) · 5.58 KB
/
step_3_quantification.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
from tqdm import tqdm
import skimage
import numpy as np
import yaml
import os
import pandas as pd
import easygui
def calculate_all_double_positives(ch1, ch2, df):
double_pos = 0
for i in range(0, len(df['label'])):
if df.loc[i, 'positives_ch' + str(ch1)] == 1:
if df.loc[i, 'positives_ch' + str(ch2)] == 1:
double_pos = double_pos + 1
return double_pos
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
choices = ["25", "50", "75"]
final_filenames = []
final_total_cells = []
final_ch2 = []
final_ch3 = []
final_ch4 = []
final_ch2_ch3 = []
final_ch3_ch4 = []
final_ch2_ch4 = []
df_final = pd.DataFrame()
#output_folder = 'step_2_output_new'
output_folder = easygui.diropenbox('Select folder output from step 2')
image_folder = output_folder + os.path.sep + 'restitched'
positive_image_folder = output_folder + os.path.sep + 'positive_cell_images'
quantification_folder = output_folder + os.path.sep + 'quantification'
intensity_image_folder = output_folder + os.path.sep + 'intensity_images'
masks_folder = output_folder + os.path.sep + 'cellpose'
report_folder = output_folder + os.path.sep + 'report'
os.makedirs(report_folder, exist_ok=True)
os.makedirs(positive_image_folder, exist_ok=True)
files = os.listdir(image_folder)
for i, file in enumerate(files):
CONFIG_NAME = 'config.yaml'
with open(CONFIG_NAME, "r") as f:
config = yaml.safe_load(f)
channels_to_quantify = config['channels']
weighting_d25 = config['weighting_d25']
weighting_d50 = config['weighting_d50']
weighting_d75 = config['weighting_d75']
channel_names = config['channel_names']
channel_names.insert(0, 'total')
print(channel_names)
df = pd.read_csv(quantification_folder + os.path.sep + file + '.csv')
df_thresh = pd.read_csv(quantification_folder + os.path.sep + file + '_thresh.csv')
thresholds = []
positives = []
total_positive_list = []
total_cells = len(df['label'])
total_positive_list.append(total_cells)
percentages = []
percentages.append(1)
thresholds.append(-1)
print(file)
df_summary = pd.DataFrame()
for position, channel in enumerate(channels_to_quantify):
positive_list = []
positive_count = 0
if 'Day_25' in file or 'DIV25' in file:
weighting = weighting_d25
if 'Day_50' in file or 'DIV50' in file:
weighting = weighting_d50
if 'Day_75' in file or 'DIV75' in file:
weighting = weighting_d75
df_positives = pd.DataFrame()
thresh = df_thresh['bg_mean_ch_' + str(channel)] + (weighting[position] * df_thresh['bg_sd_ch_' + str(channel)])
thresh = thresh[0]
print('using threshold: ' + str(thresh))
thresholds.append(int(thresh))
for intensity in df['intensity_ch_' + str(channel)]:
if intensity <= thresh:
positive_list.append(0)
if intensity > thresh:
positive_list.append(1)
positive_count = positive_count + 1
df['positives_ch' + str(channel)] = positive_list
total_positive_list.append(positive_count)
percentages.append(round((positive_count / total_cells), 2))
if channel == 2:
final_ch2.append(positive_count)
if channel == 3:
final_ch3.append(positive_count)
if channel == 4:
final_ch4.append(positive_count)
#make the channel images
intensity_image = skimage.io.imread (intensity_image_folder + os.path.sep + file + '_ch' + str(channel) + '.tif')
intensity_image[intensity_image<thresh] = 0
intensity_image[intensity_image>thresh] = channel-1
skimage.io.imsave(positive_image_folder + os.path.sep + file + '_positive_ch' + str(channel) + '.tif', intensity_image, check_contrast=False)
#calculate all double positives
channel_names.append(channel_names[0+1] + '+' + channel_names[1+1])
double_pos = calculate_all_double_positives(channels_to_quantify[0], channels_to_quantify[1], df)
total_positive_list.append(double_pos)
percentages.append(round((double_pos / total_cells), 2))
thresholds.append(-1)
final_ch2_ch3.append(double_pos)
channel_names.append(channel_names[0+1] + '+' + channel_names[2+1])
double_pos = calculate_all_double_positives(channels_to_quantify[0], channels_to_quantify[2], df)
total_positive_list.append(double_pos)
percentages.append(round((double_pos / total_cells), 2))
thresholds.append(-1)
final_ch2_ch4.append(double_pos)
channel_names.append(channel_names[1+1] + '+' + channel_names[2+1])
double_pos = calculate_all_double_positives(channels_to_quantify[1], channels_to_quantify[2], df)
total_positive_list.append(double_pos)
percentages.append(round((double_pos / total_cells), 2))
thresholds.append(-1)
final_ch3_ch4.append(double_pos)
print(channel_names)
df_summary['channels'] = channel_names
df_summary['threshold'] = thresholds
df_summary['count'] = total_positive_list
df_summary['percent'] = percentages
final_filenames.append(file)
final_total_cells.append(total_cells)
df_summary.to_csv(report_folder + os.path.sep + file + '_summary.csv', index=False)
df_final['filenames'] = final_filenames
df_final['total_cells'] = final_total_cells
df_final['ch2'] = final_ch2
df_final['ch3'] = final_ch3
df_final['ch4'] = final_ch4
df_final['ch2_ch3'] = final_ch2_ch3
df_final['ch2_ch4'] = final_ch2_ch4
df_final['ch3_ch4'] = final_ch3_ch4
df_final.to_csv(output_folder + os.path.sep + 'final_summary.csv', index=False)