-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatch_SIMA_Metadata_CSV_Generator.py
644 lines (486 loc) · 25.4 KB
/
Batch_SIMA_Metadata_CSV_Generator.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
"""
E407.4 order:
1. draq7
2. dapi
3. phase
4. high contrast phase
5. confocal tritc
6. confocal dapi
"""
channel_presets = {
"Confocal DRAQ7": {
"ChannelName": "DRAQ7",
"Color": "#dd00ff",
"AcquisitionType": "Confocal",
"ChannelType": "Fluoresence"},
"Nonconfocal DAPI": {
"ChannelName": "DAPI",
"Color": "#0035ff",
"AcquisitionType": "NonConfocal",
"ChannelType": "Fluoresence"},
"Confocal DAPI": {
"ChannelName": "DAPI",
"Color": "#0035ff",
"AcquisitionType": "Confocal",
"ChannelType": "Fluoresence"},
"NonConfocal Bright Field": {
"ChannelName": "Bright Field",
"Color": "#888a8c",
"AcquisitionType": "NonConfocal",
"ChannelType": "Fluoresence"},
"NonConfocal Bright Field-High Contrast": {
"ChannelName": "Bright Field-High Contrast",
"Color": "#48494a",
"AcquisitionType": "NonConfocal",
"ChannelType": "Fluoresence"},
"Confocal Texas Red": {
"ChannelName": "Texas Red",
"Color": "#ed0707",
"AcquisitionType": "Confocal",
"ChannelType": "Fluoresence"},
"Confocal GFP": {
"ChannelName": "GFP",
"Color": "#07ed07",
"AcquisitionType": "Confocal",
"ChannelType": "Fluoresence"},
"Confocal CY5": {
"ChannelName": "CY5",
"Color": "#dd00ff",
"AcquisitionType": "Confocal",
"ChannelType": "Fluoresence"},
"Confocal TRITC": {
"ChannelName": "TRITC",
"Color": "#ed0707",
"AcquisitionType": "Confocal",
"ChannelType": "Fluoresence"},
}
import tifffile
import xml.etree.ElementTree as ET
import json
from PIL import Image
import os
import xmltodict
from datetime import datetime
import csv
def get_input_output():
print("\nPress Ctrl + C to exit anytime.")
tiff_filepaths = []
while True:
input_directory = input("\nINPUT - Enter an input folder with aligned kinetic stacks to split/generate a SImA Upload CSV: ")
if not os.path.isdir(input_directory):
print(f"ERROR: The folder '{input_directory}' does not exist.")
continue
else:
# Walk through the directory and get .tif files
for root, dirs, files in os.walk(input_directory):
for file in files:
if file.endswith(".tif") or file.endswith(".tiff"):
tiff_filepaths.append(os.path.join(root, file))
if not len(tiff_filepaths) > 0:
print(f"ERROR: The folder '{input_directory}' does not contain any .tif files, please try again.")
continue
else:
break
while True:
output_directory = input("\nOUTPUT - Enter an folder to output the CSV and split stacks: ")
if not os.path.isdir(output_directory):
print(f"ERROR: The folder '{output_directory}' does not exist, try again.")
continue
else:
break
return tiff_filepaths, output_directory
def well_id_to_row_col(well_id):
rows = "ABCDEFGHIJKLMNOP"
row_letter = well_id[0].upper()
column_number = int(well_id[1:])
row_number = rows.index(row_letter) + 1
return row_number, column_number
def convert_date_format(date_str):
date_obj = datetime.strptime(date_str, '%m/%d/%y')
formatted_date = date_obj.strftime('%Y-%m-%dT%H:%M:%SZ')
unix_time = int(date_obj.timestamp())
return formatted_date, unix_time
def clean_dict_keys(d):
"""Recursively remove '@' from keys in a dictionary."""
if isinstance(d, dict):
return {k.lstrip('@'): clean_dict_keys(v) for k, v in d.items()}
elif isinstance(d, list):
return [clean_dict_keys(i) for i in d]
else:
return d
def extract_ome_metadata_as_dict(tiff_path):
with tifffile.TiffFile(tiff_path) as tif:
#print(f"TIFF File: {tiff_path}")
#print(f"Number of pages (frames): {len(tif.pages)}\n")
# Check for OME metadata
if tif.ome_metadata:
#print("\nOME Metadata Found")
# Convert OME XML to dictionary
ome_dict = xmltodict.parse(tif.ome_metadata)
# Clean the dictionary keys to remove '@' from attribute names
cleaned_ome_dict = clean_dict_keys(ome_dict)
return cleaned_ome_dict
else:
print("No OME metadata found.")
return None
def get_metadata_info(image_metadata):
plateName = image_metadata["OME"]["StructuredAnnotations"]["XMLAnnotation"]["Value"]["BTIImageMetaData"]["ImageReference"]["Plate"]
channelName = image_metadata["OME"]["StructuredAnnotations"]["XMLAnnotation"]["Value"]["BTIImageMetaData"]["ImageAcquisition"]["Channel"]["Color"]
emissionWavelength = image_metadata["OME"]["StructuredAnnotations"]["XMLAnnotation"]["Value"]["BTIImageMetaData"]["ImageAcquisition"]["Channel"]["EmissionWavelength"]
excitationWavelength = image_metadata["OME"]["StructuredAnnotations"]["XMLAnnotation"]["Value"]["BTIImageMetaData"]["ImageAcquisition"]["Channel"]["ExcitationWavelength"]
num_channels = image_metadata["OME"]["Image"]["Pixels"]["SizeC"]
num_timepoints = image_metadata["OME"]["Image"]["Pixels"]["SizeT"]
imageWidth = image_metadata["OME"]["Image"]["Pixels"]["SizeX"]
imageHeight = image_metadata["OME"]["Image"]["Pixels"]["SizeY"]
objectiveMagnification = image_metadata["OME"]["StructuredAnnotations"]["XMLAnnotation"]["Value"]["BTIImageMetaData"]["ImageAcquisition"]["ObjectiveSize"]
objectiveNA = image_metadata["OME"]["StructuredAnnotations"]["XMLAnnotation"]["Value"]["BTIImageMetaData"]["ImageAcquisition"]["NumericalAperture"]
positionX = image_metadata["OME"]["StructuredAnnotations"]["XMLAnnotation"]["Value"]["BTIImageMetaData"]["ImageReference"]["HorizontalTotal"]
positionY = image_metadata["OME"]["StructuredAnnotations"]["XMLAnnotation"]["Value"]["BTIImageMetaData"]["ImageReference"]["HorizontalTotal"]
# Calculated/Derived
verticalTotal = image_metadata["OME"]["StructuredAnnotations"]["XMLAnnotation"]["Value"]["BTIImageMetaData"]["ImageReference"]["VerticalTotal"]
horizTotal = image_metadata["OME"]["StructuredAnnotations"]["XMLAnnotation"]["Value"]["BTIImageMetaData"]["ImageReference"]["HorizontalTotal"]
numFields = int(verticalTotal) * int(horizTotal)
exposureTimeMS = image_metadata["OME"]["StructuredAnnotations"]["XMLAnnotation"]["Value"]["BTIImageMetaData"]["ImageAcquisition"]["ShutterSpeedMS"]
exposureTimeS = int(exposureTimeMS) / 1000
measurementDate = image_metadata["OME"]["StructuredAnnotations"]["XMLAnnotation"]["Value"]["BTIImageMetaData"]["ImageReference"]["Date"]
measurementDate, absoluteTime = convert_date_format(measurementDate)
wellID = image_metadata["OME"]["StructuredAnnotations"]["XMLAnnotation"]["Value"]["BTIImageMetaData"]["ImageReference"]["Well"]
row, column = well_id_to_row_col(wellID)
objectiveSizeInt = int(objectiveMagnification)
if objectiveSizeInt == 4:
resolution = "1.6286"
elif objectiveSizeInt == 10:
resolution = "0.6500"
elif objectiveSizeInt == 20:
resolution = "0.3250"
elif objectiveSizeInt == 40:
resolution = "0.1612"
elif objectiveSizeInt == 60:
resolution = "0.1082"
resolutionX = resolution
resolutionY = resolution
field = "1"
plane = "1"
channel = ""
timeOffset = "0"
orientationMatrix = "[[1,0,0],[0,1,0],[0,0,1]]"
acquisitionType = ""
# Replaced later:
sourceFilename = ""
timepoint = ""
channelColor = "#0035FF"
channelType = "Fluoresence"
return plateName, measurementDate, row, column, field, timepoint, plane, channel, channelName, channelColor,channelType, resolutionX, resolutionY, exposureTimeS, emissionWavelength, excitationWavelength, positionX, positionY, timeOffset, absoluteTime, imageWidth, imageHeight, numFields, num_timepoints, objectiveMagnification, objectiveNA, acquisitionType, orientationMatrix, sourceFilename, wellID
def create_append_SIMA_CSV(filepath, data):
headers = [
"PlateName", "MeasurementDate", "Row", "Column", "Field", "Timepoint", "Plane", "Channel",
"ChannelName", "ChannelColor", "ChannelType", "ImageResolutionX@um", "ImageResolutionY@um",
"ExposureTime[s]", "MainEmissionWavelength@nm", "MainExcitationWavelength@nm",
"PositionX@um", "PositionY@um", "TimeOffset@s", "AbsoluteTime@s", "ImageWidth",
"ImageHeight", "NumberOfFields", "NumberOfTimepoints", "ObjectiveMagnification",
"ObjectiveNA", "AcquisitionType", "OrientationMatrix", "SourceFilename"
]
try:
with open(filepath, mode='a+', newline='') as file:
writer = csv.writer(file)
file.seek(0)
if file.read(1) == '':
writer.writerow(headers)
writer.writerows(data)
except Exception as e:
print(f"ERROR create_append_SIMA_CSV: {e}")
def colored_text(text, hex_color):
# Remove '#' if present in the hex string
hex_color = hex_color.lstrip('#')
# Convert the hex color to RGB
rgb = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
# Return the colored text string with ANSI escape codes
return f"\033[38;2;{rgb[0]};{rgb[1]};{rgb[2]}m{text}\033[0m"
def get_channel_settings_old(image_metadata, channel_presets):
num_channels = int(image_metadata["OME"]["Image"]["Pixels"]["SizeC"])
channel_names = list(channel_presets.keys())
print("\n")
for channelName_index, channel in enumerate(channel_names):
acquisitionType = channel_presets[channel_names[channelName_index]]["AcquisitionType"]
hex_color = channel_presets[channel_names[channelName_index]]["Color"]
print(colored_text(f"{channelName_index + 1}. {channel:<40} {acquisitionType:<15} {hex_color:<10}", hex_color))
print(f"\nEnter the number of your channel ID for the {num_channels} channels in each stack (e.g. Enter \"6\" for Confocal Texas Red)")
chosen_channel_ids = []
for i in range (0, num_channels):
while True:
channel_id = input(f"\nChannel {i+1} ID: ")
if not channel_id.isdigit():
print("WARNING: Your entry was not a number, please try again:\n")
continue
else:
channel_id = int(channel_id)
if not (((channel_id - 1) >= 0) and ((channel_id - 1) <= len(channel_names))):
print("WARNING: Your entry was not in range of the available options, please try again:\n")
continue
chosen_channel_name = channel_names[channel_id-1]
chosen_channel_ids.append((i, chosen_channel_name))
print(f"\tChannel {i+1} is {channel_names[channel_id - 1]}")
break
return chosen_channel_ids
def get_channel_settings(image_metadata, channel_presets):
num_channels = int(image_metadata["num_channels"])
channel_names = list(channel_presets.keys())
print("\n")
# Print out a list of options to choose from
for channelName_index, channel in enumerate(channel_names):
acquisitionType = channel_presets[channel_names[channelName_index]]["AcquisitionType"]
hex_color = channel_presets[channel_names[channelName_index]]["Color"]
print(colored_text(f"{channelName_index + 1}. {channel:<40} {acquisitionType:<15} {hex_color:<10}", hex_color))
print(f"\nEnter the number of your channel ID for the {num_channels} channels in each stack (e.g. Enter \"6\" for Confocal Texas Red)")
chosen_channel_ids = []
for i in range (0, num_channels):
while True:
channel_id = input(f"\nChannel {i+1} ID: ")
if not channel_id.isdigit():
print("WARNING: Your entry was not a number, please try again:\n")
continue
else:
channel_id = int(channel_id)
if not (((channel_id - 1) >= 0) and ((channel_id - 1) <= len(channel_names))):
print("WARNING: Your entry was not in range of the available options, please try again:\n")
continue
chosen_channel_name = channel_names[channel_id-1]
chosen_channel_ids.append((i, chosen_channel_name))
print(f"\tChannel {i+1} is {channel_names[channel_id - 1]}")
break
return chosen_channel_ids
def split_stack_channels_timepoints(tiff_filepath, image_metadata, channel_names_inorder, output_directory, create_well_folder):
field = image_metadata["field"]
plane = image_metadata["plane"]
orientationMatrix = image_metadata["orientationMatrix"]
# timepoint = None
# channel = None
# channelColor = image_metadata["channelColor"]
# sourceFilename = ""
plateName = image_metadata["plateName"]
measurementDate = image_metadata["measurementDate"]
row = image_metadata["row"]
column = image_metadata["column"]
channelName = image_metadata["channelName"]
channelType = image_metadata["channelType"]
resolutionX = image_metadata["resolutionX"]
resolutionY = image_metadata["resolutionY"]
exposureTimeS = image_metadata["exposureTimeS"]
emissionWavelength = image_metadata["emissionWavelength"]
excitationWavelength = image_metadata["excitationWavelength"]
positionX = image_metadata["positionX"]
positionY = image_metadata["positionY"]
timeOffset = image_metadata["timeOffset"]
absoluteTime = image_metadata["absoluteTime"]
imageWidth = image_metadata["imageWidth"]
imageHeight = image_metadata["imageHeight"]
numFields = image_metadata["numFields"]
num_timepoints = image_metadata["num_timepoints"]
objectiveMagnification = image_metadata["objectiveMagnification"]
objectiveNA = image_metadata["objectiveNA"]
acquisitionType = image_metadata["acquisitionType"]
well_ID = image_metadata["wellID"]
if create_well_folder:
output_directory = os.path.join(output_directory, well_ID)
if not os.path.isdir(output_directory):
os.mkdir(output_directory)
output_metadata = []
time_point_index = 0
channel_name_index = 0
with Image.open(tiff_filepath) as img:
channel_name_index_max = len(channel_names_inorder)
num_frames = img.n_frames
time_point_index_max = int(num_frames / channel_name_index_max)
if num_frames % channel_name_index_max != 0:
print(f"ERROR split_stack_channels_timepoints: The number of frames ({num_frames}) divided by number of channels ({channel_name_index_max}) was not zero-divisible for {tiff_filepath}.\n\tCannot split stack evenly without matching number of frames and number of channels")
exit()
for i in range(num_frames):
# Save the frame with correct channel and time point
channel_prefix = channel_names_inorder[channel_name_index][0]
field_num = 1
read_step = "RS"
output_filename = f"{well_ID}_{read_step}_{channel_name_index + 1}_{field_num}_{channel_prefix}_{time_point_index+1:03d}.tif"
output_filepath = os.path.join(output_directory, output_filename)
img.seek(i)
img.save(output_filepath)
print(f"\tSaved frame {i+1} as {output_filename}")
# Create the metadata tuples and output
sourceFilename = output_filename
acquisitionType = channel_names_inorder[channel_name_index][1]
timepoint = time_point_index + 1
channel = channel_name_index + 1
channelColor = channel_names_inorder[channel_name_index][2]
channelName = channel_names_inorder[channel_name_index][0]
metadata_tuple = (plateName, measurementDate, row, column, field, timepoint, plane, channel, channelName, channelColor, channelType, resolutionX, resolutionY, exposureTimeS, emissionWavelength, excitationWavelength, positionX, positionY, timeOffset, absoluteTime, imageWidth, imageHeight, numFields, num_timepoints, objectiveMagnification, objectiveNA, acquisitionType, orientationMatrix, sourceFilename)
output_metadata.append(metadata_tuple)
# Increment indices and reset if necessary
channel_name_index = (channel_name_index + 1) % channel_name_index_max
if channel_name_index == 0: # This means channel_name_index has reset
time_point_index = (time_point_index + 1) % time_point_index_max
return output_metadata
def extract_metadata_as_dict(tiff_path):
with tifffile.TiffFile(tiff_path) as tif:
# Check for OME metadata and parse if available
if tif.ome_metadata:
ome_dict = xmltodict.parse(tif.ome_metadata)
return get_clean_metadata_dict(clean_dict_keys(ome_dict))
# If OME metadata is not available, extract and parse metadata from page 0
if tif.pages:
page_0 = tif.pages[0]
page_0_metadata_xml = page_0.description # Assuming XML metadata is in the description field
if page_0_metadata_xml:
try:
page_0_dict = xmltodict.parse(page_0_metadata_xml)
return get_clean_metadata_dict(clean_dict_keys(page_0_dict))
except Exception as e:
if "IJMetadata" in page_0.tags:
ij_metadata = page_0.tags["IJMetadata"].value["Info"]
start_index = ij_metadata.find("<OME")
if start_index != -1:
ij_metadata = ij_metadata[start_index:]
ome_dict = xmltodict.parse(ij_metadata)
return get_clean_metadata_dict(clean_dict_keys(ome_dict))
else:
return None
return None
def get_value_from_metadata_dict(final_key, data):
"""Recursively search for the final key in a nested dictionary and return its value."""
if isinstance(data, dict):
for key, value in data.items():
if key == final_key:
return value
elif isinstance(value, dict):
result = get_value_from_metadata_dict(final_key, value)
if result is not None:
return result
elif isinstance(value, list):
for item in value:
result = get_value_from_metadata_dict(final_key, item)
if result is not None:
return result
return None
def get_clean_metadata_dict(original_metadata_dict):
# Calculated/Derived
verticalTotal = get_value_from_metadata_dict("VerticalTotal", original_metadata_dict)
if verticalTotal is None:
verticalTotal = get_value_from_metadata_dict("verticalTotal", original_metadata_dict)
horizTotal = get_value_from_metadata_dict("HorizontalTotal", original_metadata_dict)
if horizTotal is None:
horizTotal = get_value_from_metadata_dict("horizontalTotal", original_metadata_dict)
print(verticalTotal)
print(horizTotal)
numFields = int(verticalTotal) * int(horizTotal)
exposureTimeMS = get_value_from_metadata_dict("ShutterSpeedMS", original_metadata_dict)
exposureTimeS = int(exposureTimeMS) / 1000
measurementDate = get_value_from_metadata_dict("Date", original_metadata_dict)
measurementDate, absoluteTime = convert_date_format(measurementDate)
wellID = get_value_from_metadata_dict("Well", original_metadata_dict)
row, column = well_id_to_row_col(wellID)
objectiveMagnification = get_value_from_metadata_dict("ObjectiveSize", original_metadata_dict)
objectiveSizeInt = int(objectiveMagnification)
if objectiveSizeInt == 4:
resolution = "1.6286"
elif objectiveSizeInt == 10:
resolution = "0.6500"
elif objectiveSizeInt == 20:
resolution = "0.3250"
elif objectiveSizeInt == 40:
resolution = "0.1612"
elif objectiveSizeInt == 60:
resolution = "0.1082"
image_width = get_value_from_metadata_dict("SizeX", original_metadata_dict)
if image_width == None or image_width == "":
image_width = get_value_from_metadata_dict("PixelWidth", original_metadata_dict)
image_height = get_value_from_metadata_dict("SizeY", original_metadata_dict)
if image_height == None or image_height == "":
image_height = get_value_from_metadata_dict("PixelHeight", original_metadata_dict)
clean_metadata_dict = {
"plateName" : get_value_from_metadata_dict("Plate", original_metadata_dict),
"measurementDate" : measurementDate,
"absoluteTime" : absoluteTime,
"wellID" : wellID,
"row" : row,
"column" : column,
"verticalTotal" : verticalTotal,
"horizTotal" : horizTotal,
"numFields": numFields,
"exposureTimeS" : exposureTimeS,
"channelName" : get_value_from_metadata_dict("Color", original_metadata_dict),
"emissionWavelength" : get_value_from_metadata_dict("EmissionWavelength", original_metadata_dict),
"excitationWavelength" : get_value_from_metadata_dict("ExcitationWavelength", original_metadata_dict),
"num_channels" : get_value_from_metadata_dict("SizeC", original_metadata_dict),
"num_timepoints" : get_value_from_metadata_dict("SizeT", original_metadata_dict),
"imageWidth" : image_width,
"imageHeight" : image_height,
"resolutionX" : resolution,
"resolutionY" : resolution,
"objectiveNA" : get_value_from_metadata_dict("NumericalAperture", original_metadata_dict),
"objectiveMagnification" : objectiveSizeInt,
"field" : "1",
"plane" : "1",
"channel" : "",
"timeOffset" : "0",
"orientationMatrix" : "[[1,0,0],[0,1,0],[0,0,1]]",
"acquisitionType" : "",
"sourceFilename" : "",
"timepoint" : "",
"channelColor" : "#0035FF",
"channelType" : "Fluoresence",
"positionX" : "0",
"positionY" : "0",
}
return clean_metadata_dict
print("\n\n\nCAUTION: All the tifs in your input directory must have the same number of separate \nimage channels or else the split naming convention will not be accurate to the actual channel.\n")
# Get the inputs and outputs
tiff_filepath_list, output_directory = get_input_output()
print(f"\n\nSelected input contains {len(tiff_filepath_list)} tifs to be processed.\nSelected output: {output_directory}")
# Select a tiff to make sure everything checks out
test_tiff_filepath = tiff_filepath_list[0]
if not os.path.isfile(test_tiff_filepath):
print(f"\n\nERROR {test_tiff_filepath} is not a valid tiff file. Check to make sure it exists.")
exit()
if not test_tiff_filepath.endswith(".tif"):
print(f"\n\nERROR {test_tiff_filepath} is not a tiff file. Enter a .tif filepath and retry.\n")
exit()
image_metadata = extract_metadata_as_dict(test_tiff_filepath)
if image_metadata is None:
print(f"ERROR get_clean_metadata_dict: No OME Metadata was found for {test_tiff_filepath}. Exiting script...")
exit()
# Get the settings for each channel according to the presets and convert it into a list to pass onto split_stack_channels_timepoints
chosen_channel_ids = get_channel_settings(image_metadata, channel_presets)
num_channels = image_metadata["num_channels"]
channel_names_inorder = []
for channel in chosen_channel_ids:
index, channel_key = channel
# channel[1] is the channel key name, channel[1] is the respective index of the channel
preset = channel_presets[channel[1]]
name = preset["ChannelName"]
acquisitionType = preset["AcquisitionType"]
color = preset["Color"]
channel_names_inorder.append((name, acquisitionType, color))
confirmation = input("Confirm that the above settings are correct (\"n\" for no, any other key to continue):")
if confirmation.lower == "n":
print("Exiting script. Please restart the script manually.")
exit()
output_csv_fp = os.path.join(output_directory, "ImageIndex.ColumbusIDX.csv")
for tiff_filepath in tiff_filepath_list:
# Reset the data for the new image
split_metadata = ""
print(f"Parsing {tiff_filepath}...")
# Extract the metadata
print("\tExtracting metadata")
# original_metadata_dict = extract_metadata_as_dict(tiff_filepath)
# image_metadata = get_clean_metadata_dict(original_metadata_dict)
image_metadata = extract_metadata_as_dict(tiff_filepath)
if image_metadata is None:
print(f"ERROR get_clean_metadata_dict: No OME Metadata was found for {tiff_filepath}. Exiting script...")
exit()
# Parse the metadata to write to the CSV
print("\tParsing metadata and splitting channels to output...")
split_metadata = split_stack_channels_timepoints(tiff_filepath, image_metadata, channel_names_inorder, output_directory, create_well_folder=False)
# Throw the data into the CSV
print("\tAdding data to CSV...")
create_append_SIMA_CSV(output_csv_fp, split_metadata)
print("Finished process successfully")
# TODO
# Make sure split_stack_channels_timepoints can handle the new dict format for image_metadata