Last active
June 5, 2018 15:23
-
-
Save will-moore/8b6575c279ea3fdc3aff4c84e803056e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import omero | |
from omero.gateway import BlitzGateway, TagAnnotationWrapper, MapAnnotationWrapper | |
conn = BlitzGateway("username", "password", host='localhost', port=4064) | |
conn.connect() | |
DATASET = 25403 | |
NAMESPACE = "map.annotation.from.csv" | |
dataset = conn.getObject('Dataset', DATASET) | |
# make a dict of name: image | |
images = {} | |
for image in dataset.listChildren(): | |
images[image.getName()] = image | |
# open the file in universal-newline mode | |
with open('/Users/wmoore/Documents/biology-data/Zegami-Oxford/zegami_epic.txt', 'rU') as tsvfile: | |
# from https://medium.com/@adds68/parsing-tsv-file-with-csv-in-python-662d6347b0cd | |
reader = csv.DictReader(tsvfile, dialect='excel-tab') | |
# print dir(reader) | |
print reader.fieldnames | |
for row in reader: | |
name = row['image'] | |
image = images.get(name) | |
if image is None: | |
continue | |
print "\n", name | |
# Create a Map annotation and link to image: | |
key_value_data = [[field, row[field]] for field in reader.fieldnames] | |
map_ann = MapAnnotationWrapper(conn) | |
map_ann.setNs(NAMESPACE) | |
map_ann.setValue(key_value_data) | |
map_ann.save() | |
image.linkAnnotation(map_ann) | |
# Set Description to link to the figure | |
figure_url = row['OMERO.Figure link'] | |
image.setDescription(figure_url) | |
image.save() | |
# Find or create a Tag from "Gene" and link to Image | |
gene = row['Gene'] | |
tags = list(conn.getObjects("TagAnnotation", attributes={'textValue': gene})) | |
if len(tags) > 0: | |
try: | |
image.linkAnnotation(tags[0]) | |
except omero.ValidationException: | |
# Tag already on Image | |
pass | |
else: | |
tag = TagAnnotationWrapper(conn) | |
tag.setValue(gene) | |
tag.save() | |
image.linkAnnotation(tag) | |
print "Done" | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment