Skip to content

Commit

Permalink
Refactor get_iss_data.py to use examples/support
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Tung authored and ttung committed Jun 1, 2018
1 parent f62c515 commit 105559c
Showing 1 changed file with 74 additions and 100 deletions.
174 changes: 74 additions & 100 deletions examples/get_iss_data.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import argparse
from typing import IO, Tuple

import io
import os
import json
import zipfile

import requests
from skimage.io import imread, imsave
from slicedimage import ImageFormat, Tile, TileSet, Writer
from slicedimage import ImageFormat

from starfish.constants import Coordinates, Indices
from starfish.constants import Indices
from starfish.util.argparse import FsExistsType
from examples.support import FetchedImage, ImageFetcher, write_experiment_json


SHAPE = (980, 1330)


def download(input_dir, url):
Expand All @@ -19,76 +24,36 @@ def download(input_dir, url):
z.extractall(input_dir)


def build_hybridization_stack(input_dir):
default_shape = imread(os.path.join(input_dir, str(1), "c{}.TIF".format(1))).shape
class ISSImage(FetchedImage):
def __init__(self, file_path):
self.file_path = file_path

hybridization_images = TileSet(
[Coordinates.X, Coordinates.Y, Indices.HYB, Indices.CH],
{Indices.HYB: 4, Indices.CH: 4},
default_shape,
ImageFormat.TIFF,
)
@property
def shape(self) -> Tuple[int, ...]:
return SHAPE

@property
def format(self) -> ImageFormat:
return ImageFormat.TIFF

def image_data_handle(self) -> IO:
return open(self.file_path, "rb")


class HybridizationImageFetcher(ImageFetcher):
def __init__(self, input_dir):
self.input_dir = input_dir

def get_image(self, fov: int, hyb: int, ch: int, z: int) -> FetchedImage:
return ISSImage(os.path.join(self.input_dir, str(hyb + 1), "c{}.TIF".format(ch + 2)))

for hyb in range(4):
for ch in range(4):
tile = Tile(
{
Coordinates.X: (0.0, 0.0001),
Coordinates.Y: (0.0, 0.0001),
},
{
Indices.HYB: hyb,
Indices.CH: ch,
},
)
path = os.path.join(input_dir, str(hyb + 1), "c{}.TIF".format(ch + 2))
tile.set_source_fh_contextmanager(
lambda _path=path: open(_path, "rb"),
ImageFormat.TIFF,
)
hybridization_images.add_tile(tile)

return hybridization_images


def build_fov(input_dir, hybridization_stack_name, codebook_json_filename, output_dir):
prefix = "fov_0"

nuclei = imread(input_dir + "DO/c1.TIF")
dots = imread(input_dir + "DO/c2.TIF")

experiment = {
'version': "0.0.0",
'hybridization_images': hybridization_stack_name,
'codebook': codebook_json_filename,
'auxiliary_images': {},
}

nuclei_fname = "{}_{}.tiff".format(prefix, "nuclei")
imsave(output_dir + nuclei_fname, nuclei)
experiment['auxiliary_images']['nuclei'] = {
'file': nuclei_fname,
'tile_shape': nuclei.shape,
'tile_format': "TIFF",
'coordinates': {
'x': (0.0, 0.0001),
'y': (0.0, 0.0001),
},
}

dots_fname = "{}_{}.tiff".format(prefix, "dots")
imsave(output_dir + dots_fname, dots)
experiment['auxiliary_images']['dots'] = {
'file': dots_fname,
'tile_shape': dots.shape,
'tile_format': "TIFF",
'coordinates': {
'x': (0.0, 0.0001),
'y': (0.0, 0.0001),
},
}

return experiment
class AuxImageFetcher(ImageFetcher):
def __init__(self, path):
self.path = path

def get_image(self, fov: int, hyb: int, ch: int, z: int) -> FetchedImage:
return ISSImage(self.path)


def write_json(res, output_path):
Expand All @@ -99,24 +64,6 @@ def write_json(res, output_path):
json.dump(res, outfile, indent=4)


def tile_opener(tileset_path, tile, ext):
tile_basename = os.path.splitext(tileset_path)[0]
return open(
"{}-H{}-C{}.{}".format(
tile_basename,
tile.indices[Indices.HYB],
tile.indices[Indices.CH],
"tiff", # this is not `ext` because ordinarily, output is saved as .npy. since we're copying the data, it
# should stay .tiff
),
"wb")


def tile_writer(tile, fh):
tile.copy(fh)
return ImageFormat.TIFF


def format_data(input_dir, output_dir, d):
if not input_dir.endswith("/"):
input_dir += "/"
Expand All @@ -133,14 +80,45 @@ def format_data(input_dir, output_dir, d):
input_dir += "ExampleInSituSequencing/"
print("Using data in : {}".format(input_dir))

image_stack = build_hybridization_stack(input_dir)
image_stack_name = "hybridization.json"
Writer.write_to_path(
image_stack,
os.path.join(output_dir, image_stack_name),
pretty=True,
tile_opener=tile_opener,
tile_writer=tile_writer)
def add_codebook(experiment_json_doc):
experiment_json_doc['codebook'] = "codebook.json"

# TODO: (ttung) remove the following unholy hacks. this is because we want to point at a tileset rather than
# a collection.
experiment_json_doc['hybridization_images'] = "hybridization-fov_000.json"
experiment_json_doc['auxiliary_images']['nuclei'] = "nuclei-fov_000.json"
experiment_json_doc['auxiliary_images']['dots'] = "dots-fov_000.json"
return experiment_json_doc

# the magic numbers here are just for the ISS example data set.
write_experiment_json(
output_dir,
1,
{
Indices.HYB: 4,
Indices.CH: 4,
Indices.Z: 1,
},
{
'nuclei': {
Indices.HYB: 1,
Indices.CH: 1,
Indices.Z: 1,
},
'dots': {
Indices.HYB: 1,
Indices.CH: 1,
Indices.Z: 1,
}
},
hyb_image_fetcher=HybridizationImageFetcher(input_dir),
aux_image_fetcher={
'nuclei': AuxImageFetcher(os.path.join(input_dir, "DO", "c1.TIF")),
'dots': AuxImageFetcher(os.path.join(input_dir, "DO", "c2.TIF")),
},
postprocess_func=add_codebook,
default_shape=SHAPE,
)

codebook = [
{'barcode': "AAGC", 'gene': "ACTB_human"},
Expand All @@ -149,10 +127,6 @@ def format_data(input_dir, output_dir, d):
codebook_json_filename = "codebook.json"
write_json(codebook, os.path.join(output_dir, codebook_json_filename))

starfish_input = build_fov(input_dir, image_stack_name, codebook_json_filename, output_dir)
starfish_input_name = "experiment.json"
write_json(starfish_input, os.path.join(output_dir, starfish_input_name))


if __name__ == "__main__":
parser = argparse.ArgumentParser()
Expand Down

0 comments on commit 105559c

Please sign in to comment.