Last active
April 21, 2016 05:42
-
-
Save sbesson/6ac1de6599315685aca8fc0215b9a500 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
| #!/usr/bin/env python | |
| # Copyright (C) 2016 Open Microscopy Environment & University of Dundee. | |
| # All rights reserved. | |
| # | |
| # 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. | |
| # author: [email protected] | |
| from omero.gateway import BlitzGateway | |
| from omero.cmd import FindChildren | |
| from omero.callbacks import CmdCallbackI | |
| from omero.model import FolderI, RoiI, FolderRoiLinkI | |
| from omero.rtypes import rstring, unwrap | |
| from omero.sys import ParametersI | |
| from random import choice | |
| import logging | |
| # create logger | |
| logger = logging.getLogger('folderize') | |
| logger.setLevel(logging.INFO) | |
| # create console handler and set level to debug | |
| ch = logging.StreamHandler() | |
| ch.setLevel(logging.INFO) | |
| # create formatter | |
| formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') | |
| # add formatter to ch | |
| ch.setFormatter(formatter) | |
| # add ch to logger | |
| logger.addHandler(ch) | |
| # set up connection | |
| conn = BlitzGateway('demo', 'ome', | |
| host='orca-3.openmicroscopy.org', port=24064, secure=True) | |
| status = conn.connect() | |
| if status: | |
| logger.info("Connection established") | |
| else: | |
| logger.warn("Connection failed") | |
| exit() | |
| updateService = conn.getUpdateService() | |
| # find ROI proxies | |
| roi_query = "select r.id from Roi r" | |
| p = conn.getQueryService().projection(roi_query, ParametersI()) | |
| roi_ids = [unwrap(row)[0] for row in p] | |
| rois = [RoiI(roi_id, False) for roi_id in roi_ids] | |
| logger.info("Found %g ROIs" % len(rois)) | |
| # create folder proxies | |
| folders = list() | |
| for i in range(12): | |
| folder = FolderI() | |
| folder.name = rstring('folder #%d' % (i+1)) | |
| folders.append(folder) | |
| folders = [FolderI(folder_.id.val, False) | |
| for folder_ in updateService.saveAndReturnArray(folders)] | |
| logger.info("Created %s folders" % len(folders)) | |
| # put ROIs in folders | |
| links = list() | |
| for roi in rois: | |
| link = FolderRoiLinkI() | |
| link.parent = choice(folders) | |
| link.child = roi | |
| links.append(link) | |
| # For large amount of ROIs it is necessary to divide into batches else the | |
| # update call throws and Ice.MemorySizeLimit exception | |
| batches = 50000 | |
| sublinks = [links[i:i + batches] for i in range(0, len(links), batches)] | |
| logger.info("Saving %s links in batches of %s" % (len(links), batches)) | |
| for i in range(len(sublinks)): | |
| updateService.saveAndReturnArray(sublinks[i]) | |
| logger.info("Linked %s ROIs to folders - %s/%s" % ( | |
| len(sublinks[i]), i, len(sublinks))) | |
| conn._closeSession() | |
| # to see the resulting ROI counts, adjust the plate ID in this query, | |
| ''' | |
| bin/omero hql "SELECT parent.name, COUNT(id) FROM FolderRoiLink | |
| WHERE child.image IN | |
| (SELECT image FROM WellSample WHERE well.plate.id = 244) | |
| GROUP BY parent.name" | |
| ''' |
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
| #!/usr/bin/env python | |
| # Copyright (C) 2016 Open Microscopy Environment & University of Dundee. | |
| # All rights reserved. | |
| # | |
| # 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. | |
| from omero.gateway import BlitzGateway | |
| from omero.model import FolderI, RoiI, FolderRoiLinkI | |
| from omero.rtypes import rstring, unwrap | |
| from omero.sys import ParametersI | |
| import logging | |
| # create logger | |
| logger = logging.getLogger('folderize_genesymbol') | |
| logger.setLevel(logging.INFO) | |
| # create console handler and set level to debug | |
| ch = logging.StreamHandler() | |
| ch.setLevel(logging.INFO) | |
| # create formatter | |
| formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') | |
| # add formatter to ch | |
| ch.setFormatter(formatter) | |
| # add ch to logger | |
| logger.addHandler(ch) | |
| # set up connection | |
| conn = BlitzGateway('demo', 'demo', | |
| host='orca-3.openmicroscopy.org', port=24064, secure=True) | |
| status = conn.connect() | |
| if status: | |
| logger.info("Connection established") | |
| else: | |
| logger.warn("Connection failed") | |
| exit() | |
| updateService = conn.getUpdateService() | |
| # Find ROI proxies | |
| logger.info("Fetching ROIs associated to images with Gene Symbols") | |
| roi_query = ("select distinct mv.value, r.id from Roi r " | |
| "join r.image i " | |
| "join i.annotationLinks al " | |
| "join al.child ma " | |
| "join ma.mapValue mv " | |
| "where mv.name = 'Gene Symbol'") | |
| p = conn.getQueryService().projection(roi_query, ParametersI()) | |
| genes = [unwrap(row)[0] for row in p] | |
| rois = [unwrap(row)[1] for row in p] | |
| unique_genes = set(genes) | |
| logger.info("Found %g ROIs associated to %g gene symbols" % ( | |
| len(rois), len(set(genes)))) | |
| # Create folders per gene | |
| # Initializes top folder | |
| topfolder = FolderI() | |
| topfolder.name = rstring('Genes') | |
| objects = [] | |
| for gene in unique_genes: | |
| folder = FolderI() | |
| folder.name = rstring(gene) | |
| folder.setParentFolder(topfolder) | |
| objects.append(folder) | |
| logger.info("Creating folders for each unique gene") | |
| folders = updateService.saveAndReturnArray(objects) | |
| logger.info("Created %s folders" % len(folders)) | |
| # Create dictionary of gene folders for linking | |
| genefolders = {} | |
| for folder in folders: | |
| genefolders[folder.name.val] = FolderI(folder.id.val, False) | |
| # Create array of Folder-ROI lonks | |
| links = [] | |
| for i in range(len(rois)): | |
| link = FolderRoiLinkI() | |
| link.parent = genefolders[genes[i]] | |
| link.child = RoiI(rois[i], False) | |
| links.append(link) | |
| # For large amount of ROIs it is necessary to divide into batches else the | |
| # update call throws and Ice.MemorySizeLimit exception | |
| batches = 50000 | |
| sublinks = [links[i:i + batches] for i in range(0, len(links), batches)] | |
| logger.info("Saving %s links in batches of %s" % (len(links), batches)) | |
| for i in range(len(sublinks)): | |
| updateService.saveAndReturnArray(sublinks[i]) | |
| logger.info("Linked %s ROIs to folders - %s/%s" % ( | |
| len(sublinks[i]), i, len(sublinks))) | |
| conn._closeSession() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment