Skip to content

Instantly share code, notes, and snippets.

@Swarchal
Created July 28, 2021 13:41
Show Gist options
  • Save Swarchal/d014e95159dffa868885f89a07e0d6dc to your computer and use it in GitHub Desktop.
Save Swarchal/d014e95159dffa868885f89a07e0d6dc to your computer and use it in GitHub Desktop.
import os
import tempfile
from collections import defaultdict
import string
import htsomeropy
import pandas as pd
from tqdm import tqdm
import cellprofiler_core.preferences as cpprefs
cpprefs.set_headless()
import cellprofiler
import cellprofiler.modules as cpm
import cellprofiler_core.pipeline as cpp
from cellprofiler_core.modules.injectimage import InjectImage
HERE = os.path.dirname(os.path.abspath(__file__))
PIPELINE_PATH = os.path.join(HERE, "pipeline.cppipe")
N_WELLS = 20
# tempfile
new_output_directory = os.path.normcase(tempfile.mkdtemp())
cpprefs.set_default_output_directory(new_output_directory)
def load_pipeline(pipeline_path=PIPELINE_PATH):
pipeline = cpp.Pipeline()
pipeline.load(pipeline_path)
return pipeline
def prep_pipeline(pipeline):
"""remove first 4 modules which aren't needed"""
for i in range(4):
pipeline.remove_module(1)
return pipeline
try:
omero = htsomeropy.gateway.Omero()
omero.interactive_login()
print(" ** connected to omero ** ")
plate = omero.plate(plate_id=52)
orig_pipeline = load_pipeline(PIPELINE_PATH)
pipeline = prep_pipeline(orig_pipeline)
wells = plate.wells
wells = wells[:N_WELLS]
well_count = len(wells)
files = defaultdict(list)
for well in tqdm(wells):
for field_n, field in enumerate(tqdm(well.fields, leave=False), 1):
pipeline_copy = pipeline.copy()
for img, channel_name in field.channels:
inject_image_module = InjectImage(channel_name, img)
inject_image_module.set_module_num(1)
pipeline_copy.add_module(inject_image_module)
#print(f"analysing plate:{plate.name}, well:{well.name} field:{field_n}")
m = pipeline_copy.run()
# collect results
for obj in ["nuclei", "cells"]:
path = os.path.join(new_output_directory, f"MyExpt_{obj}.csv")
f = pd.read_csv(path, index_col=None, header=0)
f["Metadata_well"] = well.name
f["Metadata_site"] = field_n
f["CellCount"] = f.shape[0]
files[obj].append(f)
for obj in ["nuclei", "cells"]:
df = pd.concat(files[obj], ignore_index=True)
print(df)
df.to_csv(f"output_{obj}.csv", index=False)
finally:
omero.disconnect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment