Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
15a63c8
Initial commit (squash original history)
joshmoore Aug 1, 2013
bb4705d
Update README.md
evenhuis Jan 12, 2018
77f3368
Update README.md
evenhuis Jan 12, 2018
62a7e06
Initial commit for add / remove scripts
Jan 11, 2018
ddf78e4
Update README.md
evenhuis Jan 12, 2018
0ae2018
Updated the readme
Jan 11, 2018
2e9450c
Update README.md
evenhuis Jan 12, 2018
b681350
First attempt at csv KV data
Jan 30, 2018
21538fd
Merge branch 'master' of https://github.com/evenhuis/omero-user-scripts
Jan 30, 2018
7e09f3f
Update README.md
evenhuis Jan 30, 2018
24e12c4
cleaned up key-val adding
Jan 31, 2018
3e9de54
Merge branch 'master' of https://github.com/evenhuis/omero-user-scripts
Jan 31, 2018
6c6bbdb
initial commit of Create_Metadata_from_csv.py
Feb 2, 2018
23d0d50
changed name
Feb 2, 2018
b51dce3
renamed add_key_val to reflect filename focus
Feb 5, 2018
30cd700
Added KV from description
evenhuis Mar 5, 2018
d91c75d
split description up
evenhuis Mar 9, 2018
b9593ae
Fixing exception syntax.
CFGrote May 20, 2020
26a755c
added command line download
evenhuis Mar 29, 2018
ee3f5c7
iteritems() -> items(), properly raise exception.
CFGrote May 20, 2020
027b1b8
Added the csv scripts
evenhuis Jun 3, 2018
bac0c6a
Unset hardcoded namespace
CFGrote May 20, 2020
2dc3722
Add local download script
evenhuis Jun 3, 2018
b109565
Fixing Remover.
CFGrote May 20, 2020
31c91ca
fixed up mistake in writing csv file:
evenhuis Jun 26, 2018
176f96d
fix namespace.
CFGrote May 20, 2020
0446d00
added 5.2 version of script
evenhuis Jul 5, 2018
d859db1
'iteritems->items', 'Exception, ex -> Exception as ex' in all scripts…
CFGrote May 20, 2020
ae3b418
Added example Parse_OMERO_Properties.py
evenhuis Jul 5, 2018
75cbc8a
Fixing print statements -> function.
CFGrote May 20, 2020
919038b
added install notes
evenhuis Jul 5, 2018
faefdbf
More fixes: print.
CFGrote May 20, 2020
854b287
Update README.md
evenhuis Jul 5, 2018
0b92c94
corrected path
evenhuis Jul 5, 2018
5e6e054
Fixing prints.
CFGrote May 20, 2020
e673912
fixed some styling, added env vars, commented readme
Jul 11, 2018
10ba31e
Merge branch 'master' of https://github.com/evenhuis/omero-user-scripts
evenhuis Jul 5, 2018
47334e7
Remove old scripts and rename.
CFGrote May 20, 2020
dbb44c0
removed unused file
Jul 11, 2018
31c0bca
remove space in code block
evenhuis Jul 5, 2018
1318b17
Putting the scripts into top level dir.
CFGrote May 20, 2020
005d458
updated readme
Jul 11, 2018
c49534e
removeed tabs
evenhuis Jul 5, 2018
bb3f13b
rename again.
CFGrote May 20, 2020
15fdf10
Merge pull request #1 from moisbo/master
evenhuis Jul 12, 2018
5dfac30
added more docs
evenhuis Sep 28, 2018
92778a1
Merge pull request #1 from mpievolbio-scicomp/fixes
CFGrote Apr 19, 2021
c3c8e04
Merge branch 'master' of https://github.com/evenhuis/omero-user-scripts
evenhuis Sep 28, 2018
5306092
Update README.md
CFGrote Apr 19, 2021
d275c13
Update README.md
evenhuis Sep 28, 2018
382fc52
Update README.md
CFGrote Apr 19, 2021
30ea358
Update README.md
evenhuis Sep 28, 2018
684ffa4
Added installation information.
Apr 19, 2021
552afaf
Update README.md
evenhuis Sep 28, 2018
e9353db
Update README.md
Dec 17, 2021
115e92f
Update README.md
evenhuis Sep 28, 2018
76383ee
Merge pull request #2 from mpievolbio-scicomp/master
evenhuis Dec 20, 2021
c98b35f
Update readme
CFGrote Dec 21, 2021
d881b1b
Remove user-scripts specific files
joshmoore Jan 6, 2022
322fa20
Rename according to https://github.com/ome/omero-scripts/pull/191
joshmoore Jan 6, 2022
3478a3e
Cleanup method names
CFGrote Jan 6, 2022
76bcf1c
Merge branch 'squashed-initial-history' into ometa-squashed
joshmoore Jan 11, 2022
b561081
Fix naming in KeyVal_from_csv.py
will-moore Jan 18, 2022
cffc994
Delete KeyVal_from_Description.py and KeyVal_from_Filename.py
will-moore Jan 18, 2022
1b5da61
Update contact info
will-moore Jan 18, 2022
43e1f30
Merge pull request #2 from will-moore/ometa-squashed
joshmoore Jan 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
244 changes: 244 additions & 0 deletions omero/annotation_scripts/KeyVal_from_csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
# coding=utf-8
"""
MIF/Add_Key_Val_from_csv.py

Adds key-value (kv) metadata to images in a dataset from a csv file
The first column contains the filenames
The first row of the file contains the keys
The rest is the values for each file/key

-----------------------------------------------------------------------------
Copyright (C) 2018
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
------------------------------------------------------------------------------
Created by Christian Evenhuis

"""

import omero
from omero.gateway import BlitzGateway
from omero.rtypes import rstring, rlong
import omero.scripts as scripts
from omero.model import PlateI, ScreenI, DatasetI
from omero.rtypes import *
from omero.cmd import Delete2

import sys
import csv
import copy

# this is for downloading a temp file
from omero.util.temp_files import create_path

from omero.util.populate_roi import DownloadingOriginalFileProvider
from omero.util.populate_metadata import ParsingContext

from collections import OrderedDict



# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def get_existing_map_annotations( obj ):
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
print("getting the existing kv's")
ord_dict = OrderedDict()
for ann in obj.listAnnotations():
if( isinstance(ann, omero.gateway.MapAnnotationWrapper) ):
kvs = ann.getValue()
for k,v in kvs:
if k not in ord_dict:
ord_dict[k]=set()
ord_dict[k].add(v)
return ord_dict

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def remove_map_annotations(conn, dtype, Id ):
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
image = conn.getObject(dtype,int(Id))
namespace = omero.constants.metadata.NSCLIENTMAPANNOTATION

filename = image.getName()

anns = list( image.listAnnotations())
mapann_ids = [ann.id for ann in anns
if isinstance(ann, omero.gateway.MapAnnotationWrapper) ]

try:
delete = Delete2(targetObjects={'MapAnnotation': mapann_ids})
handle = conn.c.sf.submit(delete)
conn.c.waitOnCmd(handle, loops=10, ms=500, failonerror=True,
failontimeout=False, closehandle=False)

except Exception as ex:
print("Failed to delete links: {}".format(ex.message))
return

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def get_original_file(conn, object_type, object_id, file_ann_id=None):
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
omero_object = conn.getObject("Dataset", int(object_id))
if omero_object is None:
sys.stderr.write("Error: Dataset does not exist.\n")
sys.exit(1)
file_ann = None

for ann in omero_object.listAnnotations():
if isinstance(ann, omero.gateway.FileAnnotationWrapper):
file_name = ann.getFile().getName()
# Pick file by Ann ID (or name if ID is None)
if (file_ann_id is None and file_name.endswith(".csv")) or (
ann.getId() == file_ann_id):
file_ann = ann
if file_ann is None:
sys.stderr.write("Error: File does not exist.\n")
sys.exit(1)

return file_ann.getFile()._obj


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def populate_metadata(client, conn, script_params):
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
dataType = script_params["Data_Type"]
ids = script_params["IDs"]

datasets = list(conn.getObjects(dataType, ids))
for ds in datasets:
ID = ds.getId()

# not sure what this is doing
file_ann_id = None
if "File_Annotation" in script_params:
file_ann_id = long(script_params["File_Annotation"])
print("set ann id")

original_file = get_original_file(
conn, dataType, ID, file_ann_id)
provider = DownloadingOriginalFileProvider(conn)

# read the csv
file_handle = provider.get_original_file_data(original_file)
data =list(csv.reader(file_handle,delimiter=','))
file_handle.close()

# create a dictionary for image_name:id
dict_name_id={}
for img in ds.listChildren():
img_name = img.getName()
if( img_name in dict_name_id ):
sys.stderr.write("File names not unique: {}".format(img_name))
sys.exit(1)
dict_name_id[img_name] = int(img.getId())

# keys are in the header row
header =data[0]
kv_data = header[1:] # first header is the fimename columns
rows = data[1:]

nimg_updated = 0
for row in rows: # loop over images
img_name = row[0]
if( img_name not in dict_name_id ):
print("Can't find filename : {}".format(img_name) )
else:
img_ID = dict_name_id[img_name] # look up the ID
img = conn.getObject('Image',img_ID) # get the img

existing_kv = get_existing_map_annotations( img )
updated_kv = copy.deepcopy(existing_kv)
print("Existing kv ")
for k,vset in existing_kv.items():
print(type(vset),len(vset))
for v in vset:
print(k,v)

for i in range(1,len(row)): # first entry is the filename
key = header[i].strip()
vals = row[i].strip().split(';')
if( len(vals) > 0 ):
for val in vals:
if len(val)>0 :
if key not in updated_kv: updated_kv[key] = set()
print("adding",key,val)
updated_kv[key].add(val)

if( existing_kv != updated_kv ):
nimg_updated = nimg_updated + 1
print("The key-values pairs are different")
remove_map_annotations( conn, 'Image', img.getId() )
map_ann = omero.gateway.MapAnnotationWrapper(conn)
namespace = omero.constants.metadata.NSCLIENTMAPANNOTATION
map_ann.setNs(namespace)
# convert the ordered dict to a list of lists
kv_list=[]
for k,vset in updated_kv.items():
for v in vset:
kv_list.append( [k,v] )
map_ann.setValue(kv_list)
map_ann.save()
img.linkAnnotation(map_ann)
else:
print("No change change in kv's")

return "Added {} kv pairs to {}/{} files ".format(len(header)-1,nimg_updated,len(dict_name_id))


def run_script():

data_types = [rstring('Dataset')]
client = scripts.client(
'Add_Key_Val_from_csv',
"""
This script processes a csv file, attached to a Dataset
""",
scripts.String(
"Data_Type", optional=False, grouping="1",
description="Choose source of images",
values=data_types, default="Dataset"),

scripts.List(
"IDs", optional=False, grouping="2",
description="Plate or Screen ID.").ofType(rlong(0)),

scripts.String(
"File_Annotation", grouping="3",
description="File ID containing metadata to populate."),

authors=["Christian Evenhuis"],
institutions=["MIF UTS"],
contact="https://forum.image.sc/tag/omero"
)

try:
# process the list of args above.
script_params = {}
for key in client.getInputKeys():
if client.getInput(key):
script_params[key] = client.getInput(key, unwrap=True)

# wrap client to use the Blitz Gateway
conn = BlitzGateway(client_obj=client)
print("script params")
for k,v in script_params.items():
print(k,v)
message = populate_metadata(client, conn, script_params)
client.setOutput("Message", rstring(message))
except:
pass

finally:
client.closeSession()


if __name__ == "__main__":
run_script()
Loading