Skip to content

Commit

Permalink
Merge pull request ome#1 from moisbo/master
Browse files Browse the repository at this point in the history
Run scripts with environment variables
  • Loading branch information
evenhuis committed Jul 12, 2018
2 parents 12749f9 + da48503 commit d6eedae
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 67 deletions.
7 changes: 7 additions & 0 deletions omero/annotation_scripts/.gitignore
Expand Up @@ -33,3 +33,10 @@ nosetests.xml
.mr.developer.cfg
.project
.pydevproject


downloads/OMERO.py-5.4.1-ice36-b75.zip
downloads/OMERO.py-5.4.1-ice36-b75
cli_scripts/downloads
.idea
.DS_Store
88 changes: 74 additions & 14 deletions omero/annotation_scripts/README.md
Expand Up @@ -19,46 +19,106 @@ The scripts were developed using the following resources:
# Installing OMERO CLI for 5.4.1

## 1. Create a virtual env for python 2.7:
conda create -n OMERO_CLI python=2.7 anaconda

```
conda create -n OMERO_CLI python=2.7 anaconda
```

## 2. Download the package from:
Get the "OMERO python" package download

* [https://www.openmicroscopy.org/omero/downloads/](https://www.openmicroscopy.org/omero/downloads/)

or directly from this [link](http://downloads.openmicroscopy.org/omero/5.4.1/artifacts/OMERO.py-5.4.1-ice36-b75.zip).
## 3. Install the ICE library
pip install zeroc-ice==3.6.4

```
pip install zeroc-ice==3.6.4
```

## 4. Add conda paths

Add the path to the library. Instructions from [conda webpage](https://conda.io/docs/user-guide/tasks/manage-environments.html#saving-environment-variables)

cd /Users/evenhuis/anaconda3/envs/OMERO_5.4_CLI
mkdir -p ./etc/conda/activate.d
mkdir -p ./etc/conda/deactivate.d
touch ./etc/conda/activate.d/env_vars.sh
touch ./etc/conda/deactivate.d/env_vars.sh

```
cd /Users/evenhuis/anaconda3/envs/OMERO_5.4_CLI
mkdir -p ./etc/conda/activate.d
mkdir -p ./etc/conda/deactivate.d
touch ./etc/conda/activate.d/env_vars.sh
touch ./etc/conda/deactivate.d/env_vars.sh
```

Add the following to the activate.d/env_vars.sh:


```
#!/bin/sh
export OMERO_PREFIX=~/Dropbox/MIF/OMERO/downloads_5.4.1/OMERO.py-5.4.1-ice36-b75
export ORIGPATH=$PATH
export ORIGPYTHONPATH=$PYTHONPATH
export OMERO_PREFIX=~/Dropbox/MIF/OMERO/downloads_5.4.1/OMERO.py-5.4.1-ice36-b75
export PATH=$PATH:$OMERO_PREFIX/lib/python:$OMERO_PREFIX/bin
export PYTHONPATH=$PYTHONPATH:$OMERO_PREFIX/lib/python:$OMERO_PREFIX/bin
```

This appends the OMERO library to the search path. And the following in deactivate.d/env_vars.sh restores the path variabeles

export PATH=$ORIGPATH
export PYTHONPATH=$ORIGPYTHONPATH
```
export PATH=$ORIGPATH
export PYTHONPATH=$ORIGPYTHONPATH
unset ORIGPATH
unset ORIGPYTHONPATH
unset ORIGPATH
unset ORIGPYTHONPATH
```

5. Examples:

## Run downloads

1. Example
```bash
OMERO_USERNAME='USERNAME' OMERO_USER_PASSWORD='PASSWORD' OMERO_APP_URL='omero-app.server.edu' python download_files.py -h
```
Result:
```bash
usage: download_files.py [-h] [-p PROJECT [PROJECT ...]]
[-d DATASET [DATASET ...]] [-g [GROUP]] [-o] [-t]

Download datasets and projects from OMERO

optional arguments:
-h, --help show this help message and exit
-p PROJECT [PROJECT ...], --project PROJECT [PROJECT ...]
IDs of projects to download
-d DATASET [DATASET ...], --dataset DATASET [DATASET ...]
IDs of datasets to download
-g [GROUP], --group [GROUP]
name of group
-o, --orig download originals
-t, --tif download OME-TIFs

```
2. Example
```bash
OMERO_USERNAME='USERNAME' OMERO_USER_PASSWORD='PASSWORD' OMERO_APP_URL='omero-app.server.edu' python download_files.py -d 2071 -p 759 -o
```
```bash

omero-app.research.uts.edu.au
Current user:
ID: 2
Username: 135553
Full Name: Moises Sacal Bonequi
['2071']
moises-dataset/
/gravatar.jpeg
['759']
moises-dataset/
/gravatar.jpeg

```
Legal
-----
Expand Down
40 changes: 40 additions & 0 deletions omero/annotation_scripts/cli_scripts/Parse_OMERO_Properties.py
@@ -0,0 +1,40 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 20 14:16:53 2017
@author: evenhuis
FOR TRAINING PURPOSES ONLY!
Change this file to Parse_OMERO_Properties.py and enter your ID/username
"""

import omero
import os
import sys

try:
omero_app_url = os.environ["OMERO_APP_URL"]
omero_username = os.environ["OMERO_USERNAME"]
omero_user_password = os.environ["OMERO_USER_PASSWORD"]
except KeyError:
print "Please set the environment variable OMERO_USERNAME, OMERO_USER_PASSWORD and OMERO_APP_URL"
sys.exit(1)

client = omero.client(omero_app_url)

omeroProperties = client.getProperties().getPropertiesForPrefix('omero')

# Configuration
# =================================================================
# These values will be imported by all the other training scripts.
HOST = omeroProperties.get('omero.host', omero_app_url)
PORT = omeroProperties.get('omero.port', 4064)
USERNAME = omeroProperties.get('omero.user', omero_username)
PASSWORD = omeroProperties.get('omero.pass', omero_user_password)
OMERO_WEB_HOST = omeroProperties.get('omero.webhost')
SERVER_NAME = omeroProperties.get(omero_app_url)
# projectId = omeroProperties.get('omero.projectid')
# datasetId = omeroProperties.get('omero.datasetid')
# imageId = omeroProperties.get('omero.imageid')
114 changes: 61 additions & 53 deletions omero/annotation_scripts/cli_scripts/download_files.py
Expand Up @@ -5,123 +5,131 @@
@author: evenhuis
"""
#from Parse_OMERO_Properties import datasetId, imageId, plateId
# from Parse_OMERO_Properties import datasetId, imageId, plateId

import sys
import argparse
import os


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def download_dataset( conn, Id, path, orig=False, tif=False ):
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
''' download a dataset from OMERO
INPUT : conn : the connection needs to be open
Id : ID of the dataset
path : location of local filesystem
fmt : "o" is orginal , "t" is tiff
'''

def download_dataset(conn, Id, path, orig=False, tif=False):
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"""
download a dataset from OMERO
INPUT : conn, the connection needs to be open
Id : ID of the dataset
path : location of local filesystem
fmt : "o" is original , "t" is tiff
"""

# get the data set
dataset = conn.getObject('Dataset',Id)
if( dataset==None ):
dataset = conn.getObject('Dataset', Id)
if (dataset == None):
print("Dataset ID {} not found in group".format(Id))
sys.exit(1)
print("here")

# get the images
imgs = list(dataset.listChildren())
imgs = list(dataset.listChildren())

# this is the directory to place the data in
ds_name = dataset.getName()
print("{}/".format(ds_name) )
reldir = os.path.join( path, ds_name)
if( not os.path.isdir(reldir) ):
print("{}/".format(ds_name))
reldir = os.path.join(path, ds_name)
if (not os.path.isdir(reldir)):
os.makedirs(reldir)

for img in imgs:
print(" "*len(ds_name)+"/{}".format(img.getName()))
if( orig ):
print(" " * len(ds_name) + "/{}".format(img.getName()))

if (orig):
for orig in img.getImportedImageFiles():
name = orig.getName()
file_path = os.path.join( reldir, name)
file_path = os.path.join(reldir, name)

if( not os.path.exists( file_path) ):
if (not os.path.exists(file_path)):
with open(str(file_path), 'w') as f:
for chunk in orig.getFileInChunks():
f.write(chunk)

if( tif ):
name = os.path.basename(img.getName())+".ome.tif"
if (tif):
name = os.path.basename(img.getName()) + ".ome.tif"
file_path = os.path.join(reldir, name)
file_size, block_gen = img.exportOmeTiff(bufsize=65536)
with open(str(file_path), "wb") as f:
for piece in block_gen:
f.write(piece)

return

return


"""
start-code
"""


parser = argparse.ArgumentParser(description='Download datasets and projects from OMERO')
parser.add_argument('-p','--project', nargs="+", default=[],help="IDs of projects to download")
parser.add_argument('-d','--dataset', nargs="+", default=[],help="IDs of datasets to download")
parser.add_argument('-g','--group' , nargs="?", help="name of group")
parser.add_argument('-o','--orig' , action="store_true", default=False, help="download originals")
parser.add_argument('-t','--tif' , action="store_true", default=False, help="download OME-TIFs" )
parser.add_argument('-p', '--project', nargs="+", default=[], help="IDs of projects to download")
parser.add_argument('-d', '--dataset', nargs="+", default=[], help="IDs of datasets to download")
parser.add_argument('-g', '--group', nargs="?", help="name of group")
parser.add_argument('-o', '--orig', action="store_true", default=False, help="download originals")
parser.add_argument('-t', '--tif', action="store_true", default=False, help="download OME-TIFs")

args = parser.parse_args()

# Create a connection
# ===================


from omero.gateway import BlitzGateway
from Parse_OMERO_Properties import USERNAME, PASSWORD, HOST, PORT

print(HOST)

conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT)

try:
from omero.gateway import BlitzGateway
from Parse_OMERO_Properties import USERNAME, PASSWORD, HOST, PORT
print(HOST)
conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT)
conn.connect()

user = conn.getUser()
print "Current user:"
print " ID:", user.getId()
print " Username:", user.getName()
print " Full Name:", user.getFullName()

if( args.group is not None ):
if args.group is not None:
print("change group")
new_group = args.group
groups = [ g.getName() for g in conn.listGroups() ]
groups = [g.getName() for g in conn.listGroups()]
print(groups)
if( new_group not in groups ):
if new_group not in groups:
print("{} not found in groups:".format(new_group))
for gn in groups:
print(" {}".format(gn))
sys.exit(1)
else:
conn.setGroupNameForSession(new_group)
path = os.getcwd()
print( args.dataset )

path = os.path.join(os.getcwd(), 'downloads')
print(args.dataset)
for d_id in args.dataset:
download_dataset( conn, d_id, path, orig=args.orig, tif=args.tif )
download_dataset(conn, d_id, path, orig=args.orig, tif=args.tif)

print(args.project)
for p_id in args.project:
project = conn.getObject('Project',p_id)
path_p = os.path.join(path,project.getName())
if( project==None ):
project = conn.getObject('Project', p_id)
path_p = os.path.join(path, project.getName())
if project is None:
print("project ID {} not found in group {}".format(p_id, orig=args.orig, tif=args.tif))
sys.exit(1)

for ds in list(project.listChildren()):
download_dataset( conn, ds.getId(), path_p, orig=args.orig, tif=args.tif )
download_dataset(conn, ds.getId(), path_p, orig=args.orig, tif=args.tif)

except Exception:
print "There was a problem trying to connect"
sys.exit(1)


finally:
finally:
# When you are done, close the session to free up server resources.
conn.close()


Empty file.
Empty file.

0 comments on commit d6eedae

Please sign in to comment.