Skip to content

Instantly share code, notes, and snippets.

@will-moore
Last active November 7, 2025 11:27
Show Gist options
  • Save will-moore/c39e8ed2f8385a2a122f753c1ddd98ea to your computer and use it in GitHub Desktop.
Save will-moore/c39e8ed2f8385a2a122f753c1ddd98ea to your computer and use it in GitHub Desktop.
# conda activate kong_idr
# pip install alpineer
# Successfully installed alpineer-0.1.13 charset-normalizer-2.1.1 contourpy-1.3.3 cycler-0.12.1 fonttools-4.60.1 imageio-2.37.0
# kiwisolver-1.4.9lazy-loader-0.4 matplotlib-3.10.7 natsort-8.4.0 networkx-3.5 numpy-1.26.4 packaging-25.0 pandas-2.3.3 pillow-12.0.0
# pyparsing-3.2.5 python-dateutil-2.9.0.post0 pytz-2025.2 scikit-image-0.25.2 scipy-1.16.2 six-1.17.0 tifffile-2025.10.16 tzdata-2025.2
# xarray-2025.10.1 xmltodict-0.13.0
# pip install zarr
# pip install ome-zarr
# pip install distinctipy
import os
import shutil
import distinctipy
import numpy as np
import pandas as pd
import skimage.io as io
import xarray as xr
import zarr
from ome_zarr.io import parse_url
from ome_zarr.writer import write_image, write_labels, add_metadata
from ome_zarr.format import FormatV04
from tifffile import tifffile
from alpineer.load_utils import load_imgs_from_tree
ch_dirs = os.listdir("Brainiaqc_R1C1_tumor")
print("loading tiffs...")
arr = load_imgs_from_tree(".", fovs=["Brainiaqc_R1C1_tumor"], channels=[f.replace(".tiff", "") for f in ch_dirs if ".tiff" in f]).squeeze()
path = "./Brainiaqc_R1C1_tumor_chan_range_updated_float.ome.zarr"
# cleanup previous run
if os.path.exists(path):
shutil.rmtree(path)
os.mkdir(path)
store = parse_url(path, mode="w", fmt=FormatV04()).store
root = zarr.group(store=store)
print("writing image...")
write_image(
image=arr.transpose("channels", "cols", "rows").data,
group=root,
axes="cyx",
chunks=(1, 512, 512)
)
immune_data = {
"Arginase1": {'min': 2.338113643668427e-12, 'max': 0.3191571831703186, 'quant': 0.003587209209799767},
"Au": {'min': 1.4304805517895147e-07, 'max': 18852.7421875, 'quant': 3044.3330078125},
"Calprotectin": {'min': 7.435472051432745e-13, 'max': 0.26487305760383606, 'quant': 0.002762245144695044},
"CD11b": {'min': 1.0285663813724977e-11, 'max': 0.23785588145256042, 'quant': 0.0040564489550888524},
"CD123": {'min': 4.2167762337452785e-12, 'max': 0.5123094320297241, 'quant': 0.016449574530124633},
"CD133": {'min': 1.766330002183833e-13, 'max': 0.13575468957424164, 'quant': 0.003509793896228075},
"CD14": {'min': 2.5928726180013406e-11, 'max': 0.1845734417438507, 'quant': 0.009801786169409743},
"CD141": {'min': 1.7610787828026275e-11, 'max': 0.46980878710746765, 'quant': 0.004652803624048806},
"CD163": {'min': 2.3722111081037056e-11, 'max': 0.44624149799346924, 'quant': 0.007033188035711638},
"CD20": {'min': 7.505864853263322e-12, 'max': 0.32041358947753906, 'quant': 0.011009564427658883},
"CD206": {'min': 2.8575371235906033e-12, 'max': 0.2938898503780365, 'quant': 0.007894903281703579},
"CD208": {'min': 2.270713131413693e-11, 'max': 0.16857700049877167, 'quant': 0.005436609545722585},
"CD209": {'min': 1.0560456155384035e-11, 'max': 0.3710136413574219, 'quant': 0.004955732412636249},
"CD3": {'min': 9.679421977479286e-13, 'max': 0.1332237720489502, 'quant': 0.0032684125192463387},
"CD31": {'min': 4.580334809350939e-12, 'max': 0.12210951745510101, 'quant': 0.003034361507743598},
"CD38": {'min': 5.332484670841908e-13, 'max': 0.3506328761577606, 'quant': 0.007530768429860474},
"CD4": {'min': 2.1886282716709893e-11, 'max': 0.1276802271604538, 'quant': 0.00243015925399959},
"CD40": {'min': 1.4410985425816758e-12, 'max': 0.49346333742141724, 'quant': 0.0023508940543979367},
"CD45": {'min': 1.2679710580110193e-11, 'max': 0.2636483609676361, 'quant': 0.013308528158813698},
"CD47": {'min': 2.3620085921882694e-12, 'max': 0.5872179269790649, 'quant': 0.007347360868006962},
"CD68": {'min': 2.323796753980756e-12, 'max': 0.26905497908592224, 'quant': 0.013857441022992141},
"CD8": {'min': 2.40863622449905e-12, 'max': 0.26305580139160156, 'quant': 0.005625095637515179},
"CD86": {'min': 7.789048811317201e-13, 'max': 0.0763201043009758, 'quant': 0.00230691067408769},
"chan_39": {'min': 4.9877101560014125e-08, 'max': 494442.34375, 'quant': 721.5427368164164},
"chan_48": {'min': 8.955317043479738e-11, 'max': 0.7204626798629761, 'quant': 0.008421191759407476},
"chan_70": {'min': 8.955317043479738e-11, 'max': 0.4184867739677429, 'quant': 0.020423896610736847},
"Chym_Tryp": {'min': 7.123065332015344e-11, 'max': 0.09745732694864273, 'quant': 0.0016915014339611},
"Fe": {'min': 1.110524033842708e-10, 'max': 0.9042514562606812, 'quant': 0.19002918541431413},
"FoxP3": {'min': 7.478279280548339e-12, 'max': 0.15783345699310303, 'quant': 0.002673671720549456},
"GFAP": {'min': 2.9318613509188296e-11, 'max': 0.460718035697937, 'quant': 0.037639737129211426},
"GLUT1": {'min': 1.1196220166265203e-11, 'max': 0.32715272903442383, 'quant': 0.012987279146909711},
"HLA1": {'min': 9.982281250512856e-14, 'max': 0.16302350163459778, 'quant': 0.01173638794571158},
"HLADR": {'min': 1.324970688720839e-11, 'max': 0.6149294972419739, 'quant': 0.015002240240573894},
"ICOS": {'min': 2.1065611061077405e-11, 'max': 0.546130895614624, 'quant': 0.002625846825540068},
"IDO1": {'min': 2.5711531428246337e-12, 'max': 0.2629132568836212, 'quant': 0.003263338848482813},
"iNOS": {'min': 4.732268721850674e-13, 'max': 0.6933630704879761, 'quant': 0.0031296652788296332},
"Ki67": {'min': 1.2598823893872346e-11, 'max': 0.17278432846069336, 'quant': 0.00506661106366664},
"LAG3": {'min': 4.728944557989834e-13, 'max': 0.26920679211616516, 'quant': 0.0026069177547469657},
"NeuN": {'min': 1.3806106431701881e-11, 'max': 0.8319262862205505, 'quant': 0.007427214081399087},
"Noodle": {'min': 8.955317043479738e-11, 'max': 11.73404598236084, 'quant': 0.14440538227558264},
"Nuclear": {'min': 3.909576221788891e-12, 'max': 0.8866555094718933, 'quant': 0.1298742838203908},
"Olig2": {'min': 5.071525664701593e-12, 'max': 0.06570776551961899, 'quant': 0.004879393209703268},
"PD1": {'min': 5.561413309040919e-12, 'max': 0.03402993083000183, 'quant': 0.0020620983419939844},
"PDL1": {'min': 4.480650659527408e-12, 'max': 0.49868181347846985, 'quant': 0.0027799271163530606},
"TIM3": {'min': 5.901105992744959e-12, 'max': 0.42394253611564636, 'quant': 0.004158808384090662},
"TMEM119": {'min': 3.189364137373696e-12, 'max': 0.8724086284637451, 'quant': 0.0060252790525555655},
"Tox": {'min': 3.1702838596513183e-13, 'max': 0.5510217547416687, 'quant': 0.0030463177012279626},
"TMEM119_nuc_exclude": {'min': 3.189364137373696e-12, 'max': 0.8724086284637451, 'quant': 0.004660611795261511},
"FoxP3_nuc_include": {'min': 2.9780511390242737e-11, 'max': 0.11063197255134583, 'quant': 0.002236247565597295}
}
tumor_data = {
"chan_160": {'min': 7.532374030061462e-12, 'max': 0.20723320543766022, 'quant': 0.0014132570708170547},
"YAP_TAZ": {'min': 1.1727997913102417e-11, 'max': 0.14891786873340607, 'quant': 0.0012123950943350793},
"GFAP": {'min': 8.69083667878745e-13, 'max': 0.2730637490749359, 'quant': 0.03801778517663479},
"CD14": {'min': 8.745850723322623e-13, 'max': 0.24074719846248627, 'quant': 0.008075324026867767},
"FOXP3_nuc_include": {'min': 5.512339282948142e-12, 'max': 0.10370194166898727, 'quant': 0.002033180650323622},
"CD133": {'min': 6.4856930589995976e-12, 'max': 0.5240638256072998, 'quant': 0.0029088571714237337},
"HLA1": {'min': 7.925669669173185e-12, 'max': 0.1908862143754959, 'quant': 0.016782670840621045},
"EphA2": {'min': 5.5060300936660145e-12, 'max': 0.23760239779949188, 'quant': 0.0038096670014783757},
"H3K27M": {'min': 1.2086251270637671e-11, 'max': 0.3337879776954651, 'quant': 0.019051869306713376},
"H3K27M_nuc_include": {'min': 1.2086251270637671e-11, 'max': 0.3337879776954651, 'quant': 0.02725092422217129},
"NG2": {'min': 9.924304433805986e-12, 'max': 0.09346918016672134, 'quant': 0.001943711244966827},
"TMEM119": {'min': 6.512428183530483e-12, 'max': 0.5371255278587341, 'quant': 0.004784781611524527},
"IL13RA2": {'min': 6.423195308968843e-12, 'max': 0.3641238808631897, 'quant': 0.0015066607599146633},
"GM2_GD2": {'min': 6.528171558016493e-13, 'max': 0.18504668772220612, 'quant': 0.006570419427007507},
"H3K27me3": {'min': 6.235470473292537e-12, 'max': 0.31764334440231323, 'quant': 0.024608188327401886},
"GPC2": {'min': 1.655707038486498e-12, 'max': 0.28105634450912476, 'quant': 0.002857971275225281},
"HLADR": {'min': 4.7911266967681776e-12, 'max': 0.3840442895889282, 'quant': 0.016539067588746556},
"Fe": {'min': 1.4062373487888635e-10, 'max': 0.569177508354187, 'quant': 0.15545435309410127},
"chan_161": {'min': 2.3539582284272387e-13, 'max': 0.21430979669094086, 'quant': 0.0016921615053433923},
"Ki67": {'min': 1.2916768349358065e-12, 'max': 0.2742050886154175, 'quant': 0.012545444816351059},
"EGFR": {'min': 2.4525639765599072e-12, 'max': 0.17864947021007538, 'quant': 0.010460484381765126},
"CD45": {'min': 9.681090130941872e-12, 'max': 0.1709788739681244, 'quant': 0.017042090781033137},
"chan_70": {'min': 8.955317043479738e-11, 'max': 0.395751416683197, 'quant': 0.007045812346041194},
"CD163": {'min': 2.4868470130390286e-11, 'max': 0.3394933342933655, 'quant': 0.004122159560210992},
"chan_71": {'min': 5.774770245325911e-11, 'max': 0.07514934241771698, 'quant': 0.0010776243463624237},
"FOXP3": {'min': 5.512339282948142e-12, 'max': 0.10842673480510712, 'quant': 0.002145006139762696},
"IDH1_R132H": {'min': 7.0860860582055984e-12, 'max': 0.5044521689414978, 'quant': 0.004422259135171778},
"VISTA": {'min': 5.451162091163475e-12, 'max': 0.0867706835269928, 'quant': 0.001922674936940884},
"ApoE": {'min': 8.815174203655532e-14, 'max': 0.3407961428165436, 'quant': 0.02593737876042726},
"NeuN": {'min': 1.4893721022105066e-12, 'max': 0.5741397738456726, 'quant': 0.0028763863700442035},
"chan_144": {'min': 8.983883081903343e-12, 'max': 0.24030126631259918, 'quant': 0.001416306011378765},
"CD8": {'min': 1.654870511458295e-11, 'max': 0.18795296549797058, 'quant': 0.006837657131254678},
"EGFRvIII": {'min': 4.075957019816778e-12, 'max': 0.16633155941963196, 'quant': 0.001423637827392664},
"CD4": {'min': 2.2644934538629258e-11, 'max': 0.10614129155874252, 'quant': 0.003167676422744986},
"CD47": {'min': 3.832223600952478e-12, 'max': 0.3683778941631317, 'quant': 0.0036098748608492433},
"chan_48": {'min': 8.955317043479738e-11, 'max': 0.44926315546035767, 'quant': 0.0027516720746643825},
"TMEM119_nuc_exclude": {'min': 6.512428183530483e-12, 'max': 0.5243969559669495, 'quant': 0.004682268896140169},
"Noodle": {'min': 8.955317043479738e-11, 'max': 4.283236980438232, 'quant': 0.05796530097723007},
"GD2": {'min': 7.646063469868292e-12, 'max': 0.391054630279541, 'quant': 0.0015781432739458978},
"chan_173": {'min': 2.5462932110031922e-11, 'max': 0.12496441602706909, 'quant': 0.0016201292723417304},
"Nuclear": {'min': 8.815817867380282e-12, 'max': 0.6591458916664124, 'quant': 0.11239310830831517},
"Olig2": {'min': 7.584031493090837e-12, 'max': 0.07252675294876099, 'quant': 0.005319919735193256},
"CD31": {'min': 1.0082936222066685e-11, 'max': 0.07169125974178314, 'quant': 0.0022199534345418177},
"CD70": {'min': 4.666464697294925e-12, 'max': 0.053205326199531555, 'quant': 0.0015241389884613384},
"chan_69": {'min': 6.305806137668624e-12, 'max': 0.2559332847595215, 'quant': 0.0021829746314324504},
"HER2": {'min': 2.9108336834643422e-12, 'max': 0.45123594999313354, 'quant': 0.002192450780421495},
"chan_39": {'min': 4.5182574126556574e-08, 'max': 19817.24609375, 'quant': 253.8186761474608},
"Au": {'min': 1.375818214910396e-07, 'max': 14305.9462890625, 'quant': 2416.407470703125},
"CD3": {'min': 6.942987417629665e-12, 'max': 0.3767843544483185, 'quant': 0.0039700312772765756},
"B7H3": {'min': 1.2842354133249523e-12, 'max': 0.23573313653469086, 'quant': 0.011938127130270038},
# "GPC2_nuc_exclude": {'min': 7.572108738640448e-12, 'max': 0.23957610130310059, 'quant': 0.002904701209627096},
# "VISTA_mask": {'min': 255.0, 'max': 255.0, 'quant': 255.0},
# "GFAP_mask": {'min': 255.0, 'max': 255.0, 'quant': 255.0}
}
tumor_colors = distinctipy.get_colors(len(tumor_data))
immune_colors = distinctipy.get_colors(len(immune_data))
def rgb_to_hex(rgb_tuple):
r = int(rgb_tuple[0] * 255)
g = int(rgb_tuple[1] * 255)
b = int(rgb_tuple[2] * 255)
return f'{r:02x}{g:02x}{b:02x}'
channels_metadata = []
for i, chan in enumerate(arr.channels.values):
print("Channel", chan)
channels_metadata.append(
{
"active": i < 5, # first 5 channels active
"label": chan,
"window": {"start": 0, "end": tumor_data[chan]["quant"], "min": 0, "max": tumor_data[chan]["max"]},
"color": rgb_to_hex(tumor_colors[i])
}
)
add_metadata(root, {"omero": {
"channels": channels_metadata
}})
# Labels
seg_labels = []
for file in ["Brainiaqc_R1C1_nuclear.tiff", "Brainiaqc_R1C1_whole_cell.tiff"]:
seg_labels.append(tifffile.imread(
f'./segmentation/{file}'
).squeeze())
seg_labels = np.stack(seg_labels)
seg_xr = xr.DataArray(
seg_labels,
dims=["seg_type", "x", "y"],
coords=[["nuclear", "whole_cell"], np.arange(2048), np.arange(2048)]
)
for cell_seg_type in ["nuclear", "whole_cell"]:
cell_seg_data = seg_xr.sel(seg_type=cell_seg_type)
label_data = cell_seg_data.transpose("y", "x")
label_data = label_data.data[...]
print("label", cell_seg_type, label_data.shape)
write_labels(
labels=label_data,
group=root,
name=f"{cell_seg_type}_segmentation",
axes="yx"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment