Skip to content

Instantly share code, notes, and snippets.

@emilroz
Last active August 29, 2015 14:10
Show Gist options
  • Save emilroz/d61d4ce8e4412b8b4c2e to your computer and use it in GitHub Desktop.
Save emilroz/d61d4ce8e4412b8b4c2e to your computer and use it in GitHub Desktop.
Script shows how to change file names after import. Custom filter are used to select images of interest.
'''
Copyright (C) 2014 Glencoe Software, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''
import omero
from omero.gateway import BlitzGateway
from omero.gateway import omero_type
from omero.rtypes import rlong
import argparse
import re
def rename(image, iupdate, args):
regex = re.compile(r'\[Series (\d+)\]')
imageName = image.name.getValue()
m = regex.search(imageName)
newName = ''
if m:
seriesNumber = int(m.group(1))
if seriesNumber == 1:
newName = regex.sub('', imageName)
elif seriesNumber == 2:
newName = regex.sub('[label image]', imageName)
elif seriesNumber == 3:
newName = regex.sub('[macro image]', imageName)
if args.dryrun is False:
image.setName(omero_type(newName))
iupdate.saveObject(image)
print imageName, '->', newName
def parse_arguments(parser):
parser.add_argument('-s', help='server')
parser.add_argument('-p', default='4064', help='port', type=int)
parser.add_argument('-u', help='user')
parser.add_argument('-w', help='password')
parser.add_argument('--project', default='0',
help='Project ID to interogate', type=int)
parser.add_argument('--dryrun', help='dryrun', action="store_true")
return parser.parse_args()
parser = argparse.ArgumentParser()
args = parse_arguments(parser)
if args.dryrun:
print "DryRun this time"
conn = BlitzGateway(args.u, args.w,
host=args.s, port=args.p)
conn.connect()
user = conn.getUser()
group = conn.getGroupFromContext()
updateService = conn.getUpdateService()
queryService = conn.getQueryService()
params = omero.sys.ParametersI()
params.addId(user.getId())
params.add("gid", rlong(group.getId()))
query = ''
if args.project == 0:
query = "select i from Image as i where i.details.owner.id = :id " \
" and i.details.group.id = :gid" \
" and lower(i.name) like '%svs%series%'"
else:
print "Running complex query"
params.add("pid", rlong(args.project))
query = "select i from Image as i" \
" join fetch i.datasetLinks as dLinks" \
" join fetch dLinks.parent as dataset" \
" join fetch dataset.projectLinks as pLinks" \
" join fetch pLinks.parent as p where p.id = :pid" \
" and i.details.owner.id = :id" \
" and i.details.group.id = :gid" \
" and lower(i.name) like '%svs%series%'"
queryResult = queryService.findAllByQuery(query, params)
for image in queryResult:
fileset = conn.getObject("Fileset", image.getFileset().getId())
if len(fileset.copyImages()) > 2:
rename(image, updateService, args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment