Skip to content

Commit

Permalink
Merge pull request #14 from will-moore/fs_suffix
Browse files Browse the repository at this point in the history
Fs suffix
  • Loading branch information
joshmoore authored Jan 31, 2024
2 parents e874ca3 + c592226 commit 6594001
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
12 changes: 9 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,22 @@ specified Fileset ID:

::

$ omero mkngff sql --symlink_repo /OMERO/ManagedRepository --secret=secret 1234 /path/to/fileset.zarr > myNgff.sql
$ omero mkngff sql --symlink_repo /OMERO/ManagedRepository --secret=secret --bfoptions 1234 /path/to/fileset.zarr > myNgff.sql
$ psql -U omero -d idr -h $DBHOST -f myNgff.sql

To ONLY perform the symlink creation:
To ONLY perform the symlink creation (and optionally create fileset.zarr.bfoptions)

::

$ omero mkngff symlink /OMERO/ManagedRepository 1234 /path/to/fileset.zarr
$ omero mkngff symlink /OMERO/ManagedRepository 1234 /path/to/fileset.zarr --bfoptions


To ONLY create fileset.zarr.bfoptions

::

$ omero mkngff bfoptions /OMERO/ManagedRepository 1234 /path/to/fileset.zarr

Requirements
============

Expand Down
69 changes: 54 additions & 15 deletions src/omero_mkngff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from omero.cli import BaseControl, Parser
from omero.sys import ParametersI

SUFFIX = "mkngff"
HELP = """Plugin to swap OMERO filesets with NGFF
CLI plugin used to swap an existing OMERO fileset with
Expand All @@ -44,6 +43,9 @@
omero mkngff sql ${fileset} ${zarrdir} --zarr_name "nice.ome.zarr"
"""
FS_SUFFIX_HELP = ("New Fileset.templatePrefix will be old Fileset.templatePrefix + fs_suffix. "
"Default is to use _mkngff. "
"Use 'None' to specify an empty string (new templatePrefix is same as old).")

SETUP = """
Expand Down Expand Up @@ -138,6 +140,8 @@


class MkngffControl(BaseControl):
suffix = "_mkngff"

def _configure(self, parser: Parser) -> None:
parser.add_login_arguments()
sub = parser.add_subparsers()
Expand All @@ -155,10 +159,15 @@ def _configure(self, parser: Parser) -> None:
help=("Create symlinks from Fileset to symlink_target using"
"this ManagedRepo path, e.g. /data/OMERO/ManagedRepository")
)
sql.add_argument(
"--bfoptions", action="store_true",
help=("Create data.zarr.bfoptions file if --symlink_repo has been provided")
)
sql.add_argument(
"--clientpath",
help=("Base path to create clientpath/path/to/img.zarr/")
)
sql.add_argument("--fs_suffix", default="_mkngff", help=FS_SUFFIX_HELP)
sql.add_argument("fileset_id", type=int)
sql.add_argument("symlink_target")
sql.set_defaults(func=self.sql)
Expand All @@ -171,13 +180,25 @@ def _configure(self, parser: Parser) -> None:
"this ManagedRepo path, e.g. /data/OMERO/ManagedRepository"))
symlink.add_argument("fileset_id", type=int)
symlink.add_argument("symlink_target")
symlink.add_argument("--bfoptions", action="store_true", help="Create data.zarr.bfoptions file")
symlink.add_argument("--fs_suffix", default="_mkngff", help=FS_SUFFIX_HELP)
symlink.set_defaults(func=self.symlink)

bfoptions = sub.add_parser("bfoptions", help="Create data.zarr.bfoptions in Fileset")
bfoptions.add_argument("symlink_repo", help=(
"The ManagedRepo path, e.g. /data/OMERO/ManagedRepository"))
bfoptions.add_argument("fileset_id", type=int)
bfoptions.add_argument("symlink_target")
bfoptions.add_argument("--fs_suffix", default="_mkngff", help=FS_SUFFIX_HELP)
bfoptions.set_defaults(func=self.bfoptions)

def setup(self, args: Namespace) -> None:
self.ctx.out(SETUP)

def sql(self, args: Namespace) -> None:
prefix = self.get_prefix(args)
self.suffix = "" if args.fs_suffix == "None" else args.fs_suffix

self.ctx.err(f"Found prefix: {prefix} for fileset: {args.fileset_id}")

symlink_path = Path(args.symlink_target)
Expand All @@ -188,7 +209,7 @@ def sql(self, args: Namespace) -> None:

# If symlink dir exists, we assume that this fileset has been processed -> skip...
if args.symlink_repo:
symlink_dir = self.get_symlink_dir(args.symlink_repo, prefix, symlink_path)
symlink_dir = self.get_symlink_dir(args.symlink_repo, prefix)
if os.path.exists(symlink_dir):
self.ctx.err(f"Symlink dir exists at {symlink_dir} - skipping sql output")
return
Expand All @@ -207,7 +228,7 @@ def sql(self, args: Namespace) -> None:
row_path = str(row_path).replace(f"{symlink_path.parent}", "")
if str(row_path).startswith("/"):
row_path = str(row_path)[1:] # remove "/" from start
row_full_path = f"{prefix}_{SUFFIX}/{row_path}"
row_full_path = f"{prefix}{self.suffix}/{row_path}"
# pick the first .zattrs file we find, then update to ome.xml if we find it
if setid_target is None and row_name == ".zattrs" or row_name == "METADATA.ome.xml":
setid_target = [row_full_path, row_name]
Expand All @@ -228,21 +249,30 @@ def sql(self, args: Namespace) -> None:
self.ctx.out(
TEMPLATE.format(
OLD_FILESET=args.fileset_id,
PREFIX=f"{prefix}_{SUFFIX}/",
PREFIX=f"{prefix}{self.suffix}/",
ROWS=",\n".join(rows),
REPO=self.get_uuid(args),
UUID=args.secret,
)
)

# Finally create *_SUFFIX/ directory containing symlink to data
# Finally create *self.suffix/ directory containing symlink to data
if args.symlink_repo:
self.create_symlink(args.symlink_repo, prefix, symlink_path, args.symlink_target)
self.create_symlink(args.symlink_repo, prefix, args.symlink_target)
if args.bfoptions:
self.write_bfoptions(args.symlink_repo, prefix, args.symlink_target)

def bfoptions(self, args: Namespace) -> None:
self.suffix = "" if args.fs_suffix == "None" else args.fs_suffix
prefix = self.get_prefix(args)
self.write_bfoptions(args.symlink_repo, prefix, args.symlink_target)

def symlink(self, args: Namespace) -> None:
self.suffix = "" if args.fs_suffix == "None" else args.fs_suffix
prefix = self.get_prefix(args)
symlink_path = Path(args.symlink_target)
self.create_symlink(args.symlink_repo, prefix, symlink_path, args.symlink_target)
self.create_symlink(args.symlink_repo, prefix, args.symlink_target)
if args.bfoptions:
self.write_bfoptions(args.symlink_repo, prefix, args.symlink_target)

def get_prefix(self, args):

Expand All @@ -266,19 +296,28 @@ def get_prefix(self, args):

return prefix

def get_symlink_dir(self, symlink_repo, prefix, symlink_path):
def get_symlink_dir(self, symlink_repo, prefix):
prefix_dir = os.path.join(symlink_repo, prefix)
self.ctx.err(f"Checking for prefix_dir {prefix_dir}")
if not os.path.exists(prefix_dir):
self.ctx.die(402, f"Fileset dir does not exist: {prefix_dir}")
symlink_container = f"{symlink_path.parent}"
if symlink_container.startswith("/"):
symlink_container = symlink_container[1:] # remove "/" from start
symlink_dir = f"{prefix_dir}_{SUFFIX}"
symlink_dir = f"{prefix_dir}{self.suffix}"
return symlink_dir

def create_symlink(self, symlink_repo, prefix, symlink_path, symlink_target):
symlink_dir = self.get_symlink_dir(symlink_repo, prefix, symlink_path)
def write_bfoptions(self, managed_repo, fsprefix, symlink_target):
file_path = Path(symlink_target)
mkngff_dir = self.get_symlink_dir(managed_repo, fsprefix)
# os.makedirs(mkngff_dir, exist_ok=True)
zarr_path = os.path.join(mkngff_dir, file_path.name)
bfoptions_path = f"{zarr_path}.bfoptions"
self.ctx.err("write bfoptions to: %s" % bfoptions_path)
with open(bfoptions_path, "w") as f:
f.writelines(["omezarr.list_pixels=false",
"\nomezarr.quick_read=true"])

def create_symlink(self, symlink_repo, prefix, symlink_target):
symlink_path = Path(symlink_target)
symlink_dir = self.get_symlink_dir(symlink_repo, prefix)
self.ctx.err(f"Creating dir at {symlink_dir}")
os.makedirs(symlink_dir, exist_ok=True)

Expand Down

0 comments on commit 6594001

Please sign in to comment.