|
| 1 | +import spatialdata as sd |
| 2 | +import anndata as ad |
| 3 | +from spatialdata_io import xenium |
| 4 | +import shutil |
| 5 | +import os |
| 6 | +import zipfile |
| 7 | +import tempfile |
| 8 | + |
| 9 | +## VIASH START |
| 10 | +par = { |
| 11 | + "input": "https://s3-us-west-2.amazonaws.com/10x.files/samples/atera/dev/WTA_Preview_FFPE_Breast_Cancer/WTA_Preview_FFPE_Breast_Cancer_xe_outs.zip", |
| 12 | + "segmentation_id": [ |
| 13 | + "cell", |
| 14 | + "nucleus", |
| 15 | + ], |
| 16 | + "dataset_id": "value", |
| 17 | + "dataset_name": "value", |
| 18 | + "dataset_url": "value", |
| 19 | + "dataset_reference": "value", |
| 20 | + "dataset_summary": "value", |
| 21 | + "dataset_description": "value", |
| 22 | + "dataset_organism": "value", |
| 23 | + "output": "temp/datasets/10x_atera/breast/breast.zarr" |
| 24 | +} |
| 25 | +meta = { |
| 26 | + "cpus": 1, |
| 27 | +} |
| 28 | + |
| 29 | +## VIASH END |
| 30 | + |
| 31 | +# Download the data if it's a download url, extract the data if it's a zip file |
| 32 | +par_input = par["input"] |
| 33 | +with tempfile.TemporaryDirectory() as tmpdirname: |
| 34 | + if par_input.startswith("http"): |
| 35 | + print(f"Downloading data to {tmpdirname}", flush=True) |
| 36 | + file_name = par_input.split("/")[-1] |
| 37 | + # download the data |
| 38 | + os.system(f"wget {par['input']} -O {tmpdirname}/{file_name}") |
| 39 | + par_input = tmpdirname + "/" + file_name |
| 40 | + |
| 41 | + if zipfile.is_zipfile(par_input): |
| 42 | + print(f"Extracting input zip to {tmpdirname}", flush=True) |
| 43 | + with zipfile.ZipFile(par_input, "r") as zip_ref: |
| 44 | + zip_ref.extractall(tmpdirname) |
| 45 | + par_input = tmpdirname |
| 46 | + |
| 47 | + # read the data |
| 48 | + sdata = xenium( |
| 49 | + path=par_input, |
| 50 | + n_jobs=meta["cpus"] or 1, |
| 51 | + cells_boundaries=True, |
| 52 | + nucleus_boundaries=True, |
| 53 | + morphology_focus=True, |
| 54 | + cells_as_circles=False, |
| 55 | + ) |
| 56 | + |
| 57 | + # remove morphology_focus |
| 58 | + _ = sdata.images.pop("morphology_focus") |
| 59 | + |
| 60 | + print("Add uns to table", flush=True) |
| 61 | + new_uns = { |
| 62 | + "dataset_id": par["dataset_id"], |
| 63 | + "dataset_name": par["dataset_name"], |
| 64 | + "dataset_url": par["dataset_url"], |
| 65 | + "dataset_reference": par["dataset_reference"], |
| 66 | + "dataset_summary": par["dataset_summary"], |
| 67 | + "dataset_description": par["dataset_description"], |
| 68 | + "dataset_organism": par["dataset_organism"], |
| 69 | + "segmentation_id": par["segmentation_id"], |
| 70 | + } |
| 71 | + for key, value in new_uns.items(): |
| 72 | + sdata.tables["table"].uns[key] = value |
| 73 | + |
| 74 | + print(f"Output: {sdata}", flush=True) |
| 75 | + |
| 76 | + print(f"Writing to '{par['output']}'", flush=True) |
| 77 | + if os.path.exists(par["output"]): |
| 78 | + shutil.rmtree(par["output"]) |
| 79 | + |
| 80 | + print(f"Output: {sdata}") |
| 81 | + |
| 82 | + sdata.write(par["output"]) |
0 commit comments