Skip to content

omero/files.py

OMERO File utiltities

download_file_annotation(file_annot, outdir='.')

Downloads FileAnnotation from OMERO into a local directory.

Parameters:

Name Type Description Default
file_annot FileAnnotationWrapper

Remote Omero File Annotation object.

required
out_dir

Where to download this file.

required

Returns:

Type Description
str

String path to downloaded file.

Source code in lavlab/omero/files.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def download_file_annotation(file_annot: FileAnnotationWrapper, outdir=".") -> str:
    """
    Downloads FileAnnotation from OMERO into a local directory.

    Parameters
    ----------
    file_annot: omero.gateway.FileAnnotationWrapper
        Remote Omero File Annotation object.
    out_dir: str, Default: '.'
        Where to download this file.

    Returns
    -------
    str
        String path to downloaded file.
    """
    path = os.path.abspath(outdir) + os.sep + file_annot.getFile().getName()
    print(f"Downloading {path}...")
    with open(path, "wb") as f:
        for chunk in file_annot.getFileInChunks():
            f.write(chunk)
    print(f"{path} downloaded!")
    return path

get_script_by_name(conn, fn, absolute=False, check_user_scripts=False)

Searches for an omero script in the host with the given name.

Parameters:

Name Type Description Default
conn BlitzGateway

An Omero BlitzGateway with a session.

required
fn str

Name of remote Omero.Script

required
absolute

Absolute uses getScriptID(). This method does not accept wildcards and requires a path. Default will get all remote script names and compare the remote filename to fn.

False
checkUserScripts

Not implemented.

required

Returns:

Type Description
int

Omero.Script Id

Source code in lavlab/omero/files.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def get_script_by_name(
    conn: BlitzGateway, fn: str, absolute=False, check_user_scripts=False
) -> int:
    """
    Searches for an omero script in the host with the given name.

    Parameters
    ----------
    conn: omero.gateway.BlitzGateway
        An Omero BlitzGateway with a session.
    fn: str
        Name of remote Omero.Script
    absolute: bool, Default: False
        Absolute uses getScriptID(). This method does not accept wildcards and requires a path.
        Default will get all remote script names and compare the remote filename to fn.
    checkUserScripts: bool, Default: False
        Not implemented.

    Returns
    -------
    int
        Omero.Script Id
    """
    if check_user_scripts:
        LOGGER.warning(
            "getScriptByName not fully implemented! May cause unexpected results!"
        )
    script_svc = conn.getScriptService()
    try:
        if absolute is True:
            return script_svc.getScriptID(fn)
        for script in script_svc.getScripts():
            if script.getName().getValue() == fn:
                return script.getId().getValue()
        raise ValueError("Could not find script!")
    finally:
        script_svc.close()

upload_file_as_annotation(parent_obj, file_path, namespace, mime=None, overwrite=True)

Uploads a given filepath to omero as an annotation for parent_obj under namespace.

parent_obj: omero.gateway.ParentWrapper Object that should own the annotation. (typically an ImageWrapper) file_path: str Local path of file to upload as annotation. namespace: str Remote namespace to put the file annotation mime: str, optional Mimetype for filetype. If None this will be assumed based on file extension overwrite: bool, Default: True Overwrites existing file annotation in this namespace. return: omero.gateway.FileAnnotationWrapper Uploaded FileAnnotation object.

Source code in lavlab/omero/files.py
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def upload_file_as_annotation(
    parent_obj: ImageWrapper,
    file_path: str,
    namespace: str,
    mime: Optional[str] = None,
    overwrite=True,
) -> FileAnnotationWrapper:
    """
    Uploads a given filepath to omero as an annotation for parent_obj under namespace.

    parent_obj: omero.gateway.ParentWrapper
        Object that should own the annotation. (typically an ImageWrapper)
    file_path: str
        Local path of file to upload as annotation.
    namespace: str
        Remote namespace to put the file annotation
    mime: str, optional
        Mimetype for filetype. If None this will be assumed based on file extension
    overwrite: bool, Default: True
        Overwrites existing file annotation in this namespace.
    return: omero.gateway.FileAnnotationWrapper
        Uploaded FileAnnotation object.
    """
    conn = parent_obj._conn  # pylint: disable=W0212

    # if no mime provided try to parse from filename, if cannot, assume plaintext
    if mime is None:
        mime = lavlab.ctx.FILETYPE_ENUM.get_mimetype_from_path(file_path)

    # if overwrite is true and an annotation already exists in this namespace, delete it
    if overwrite is True:
        obj = parent_obj.getAnnotation(namespace)
        if obj is not None:
            conn.deleteObjects("Annotation", [obj.id], wait=True)

    # create, link, and return new annotation
    annot_obj = conn.createFileAnnfromLocalFile(file_path, mimetype=mime, ns=namespace)
    parent_obj.linkAnnotation(annot_obj)
    return annot_obj