Package omero :: Package plugins :: Module cecog
[hide private]
[frames] | no frames]

Source Code for Module omero.plugins.cecog

  1  #!/usr/bin/env python 
  2  """ 
  3     Methods for working with cecog 
  4   
  5     Copyright 2010 University of Dundee, Inc. All rights reserved. 
  6     Use is subject to license terms supplied in LICENSE.txt 
  7   
  8  """ 
  9   
 10  import os 
 11  import re 
 12  import sys 
 13  import exceptions 
 14   
 15  from omero.cli import BaseControl, CLI, OMERODIR 
 16  from omero_ext.argparse import FileType 
 17   
 18  import omero 
 19  import omero.constants 
 20   
 21  from omero.rtypes import * 
 22   
 23   
24 -class CecogControl(BaseControl):
25 """CeCog integration plugin. 26 27 Provides actions for prepairing data and otherwise 28 integrating with Cecog. See the Run_Cecog_4.1.py 29 script. 30 """ 31 32 # [MetaMorph_PlateScanPackage] 33 #regex_subdirectories = re.compile('(?=[^_]).*?(?P<D>\d+).*?') 34 #regex_position = re.compile('P(?P<P>.+?)_') 35 #continuous_frames = 1 36 regex_token = re.compile(r'(?P<Token>.+)\.') 37 regex_time = re.compile(r'T(?P<T>\d+)') 38 regex_channel = re.compile(r'_C(?P<C>.+?)(_|$)') 39 regex_zslice = re.compile(r'_Z(?P<Z>\d+)') 40
41 - def _configure(self, parser):
42 sub = parser.sub() 43 44 class Action(object): 45 46 def __init__(this, name): 47 func = getattr(self, name) 48 this.parser = sub.add_parser(name, help=func.__doc__, description=func.__doc__) 49 this.parser.set_defaults(func=func)
50 51 def add_argument(this, *args, **kwargs): 52 this.parser.add_argument(*args, **kwargs) 53 return this
54 55 merge = Action("merge") 56 merge.add_argument("path", help="Path to image files") 57 58 rois = Action("rois") 59 rois.add_argument("-f", "--file", required=True, help="Details file to be parsed") 60 rois.add_argument("-i", "--image", required=True, help="Image id which should have ids attached") 61 62 ## 63 ## Public methods 64 ##
65 - def merge(self, args):
66 """Uses PIL to read multiple planes from a local folder. 67 68 Planes are combined and uploaded to OMERO as new images with additional T, C, Z dimensions. 69 70 It should be run as a local script (not via scripting service) in order that it has 71 access to the local users file system. Therefore need EMAN2 or PIL installed locally. 72 73 Example usage: 74 will$ bin/omero cecog merge /Applications/CecogPackage/Data/Demo_data/0037/ 75 76 Since this dir does not contain folders, this will upload images in '0037' into a Dataset called Demo_data 77 in a Project called 'Data'. 78 79 will$ bin/omero cecog merge /Applications/CecogPackage/Data/Demo_data/ 80 81 Since this dir does contain folders, this will look for images in all subdirectories of 'Demo_data' and 82 upload images into a Dataset called Demo_data in a Project called 'Data'. 83 84 Images will be combined in Z, C and T according to the MetaMorph_PlateScanPackage naming convention. 85 E.g. tubulin_P0037_T00005_Cgfp_Z1_S1.tiff is Point 37, Timepoint 5, Channel gfp, Z 1. S? 86 see /Applications/CecogPackage/CecogAnalyzer.app/Contents/Resources/resources/naming_schemes.conf 87 """ 88 """ 89 Processes the command args, makes project and dataset then calls uploadDirAsImages() to process and 90 upload the images to OMERO. 91 """ 92 from omero.rtypes import unwrap 93 from omero.util.script_utils import uploadDirAsImages 94 95 path = args.path 96 client = self.ctx.conn(args) 97 queryService = client.sf.getQueryService() 98 updateService = client.sf.getUpdateService() 99 pixelsService = client.sf.getPixelsService() 100 101 # if we don't have any folders in the 'dir' E.g. CecogPackage/Data/Demo_data/0037/ 102 # then 'Demo_data' becomes a dataset 103 subDirs = [] 104 for f in os.listdir(path): 105 fullpath = path + f 106 # process folders in root dir: 107 if os.path.isdir(fullpath): 108 subDirs.append(fullpath) 109 110 # get the dataset name and project name from path 111 if len(subDirs) == 0: 112 p = path[:-1] # will remove the last folder 113 p = os.path.dirname(p) 114 else: 115 if os.path.basename(path) == "": 116 p = path[:-1] # remove slash 117 118 datasetName = os.path.basename(p) # e.g. Demo_data 119 p = p[:-1] 120 p = os.path.dirname(p) 121 projectName = os.path.basename(p) # e.g. Data 122 self.ctx.err("Putting images in Project: %s Dataset: %s" % (projectName, datasetName)) 123 124 # create dataset 125 dataset = omero.model.DatasetI() 126 dataset.name = rstring(datasetName) 127 dataset = updateService.saveAndReturnObject(dataset) 128 # create project 129 project = omero.model.ProjectI() 130 project.name = rstring(projectName) 131 project = updateService.saveAndReturnObject(project) 132 # put dataset in project 133 link = omero.model.ProjectDatasetLinkI() 134 link.parent = omero.model.ProjectI(project.id.val, False) 135 link.child = omero.model.DatasetI(dataset.id.val, False) 136 updateService.saveAndReturnObject(link) 137 138 if len(subDirs) > 0: 139 for subDir in subDirs: 140 self.ctx.err("Processing images in %s" % subDir) 141 rv = uploadDirAsImages(client.sf, queryService, updateService, pixelsService, subDir, dataset) 142 self.ctx.out("%s" % unwrap(rv)) 143 144 # if there are no sub-directories, just put all the images in the dir 145 else: 146 self.ctx.err("Processing images in %s" % path) 147 rv = uploadDirAsImages(client.sf, queryService, updateService, pixelsService, path, dataset) 148 self.ctx.out("%s" % unwrap(rv))
149
150 - def rois(self, args):
151 """Parses an object_details text file, as generated by CeCog Analyzer and saves the data as ROIs on an Image in OMERO. 152 153 Text file is of the form: 154 155 frame objID classLabel className centerX centerY mean sd 156 1 10 6 lateana 1119 41 76.8253796095 54.9305640673 157 158 159 Example usage: 160 bin/omero cecog rois -f Data/Demo_output/analyzed/0037/statistics/P0037__object_details.txt -i 502 161 """ 162 """ 163 Processes the command args, parses the object_details.txt file and creates ROIs on the image specified in OMERO 164 """ 165 from omero.util.script_utils import uploadCecogObjectDetails 166 filePath = args.file 167 imageId = args.image 168 if not os.path.exists(filePath): 169 self.ctx.die(654, "Could find the object_details file at %s" % filePath) 170 171 client = self.ctx.conn(args) 172 updateService = client.sf.getUpdateService() 173 ids = uploadCecogObjectDetails(updateService, imageId, filePath) 174 self.ctx.out("Rois created: %s" % len(ids))
175 176 177 try: 178 register("cecog", CecogControl, CecogControl.__doc__) 179 except NameError: 180 if __name__ == "__main__": 181 cli = CLI() 182 cli.register("cecog", CecogControl, CecogControl.__doc__) 183 cli.invoke(sys.argv[1:]) 184