Skip to content

Commit 15fdf10

Browse files
evenhuisjoshmoore
authored andcommitted
Merge pull request #1 from moisbo/master
Run scripts with environment variables
2 parents c49534e + 005d458 commit 15fdf10

File tree

6 files changed

+182
-67
lines changed

6 files changed

+182
-67
lines changed

omero/annotation_scripts/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,10 @@ nosetests.xml
3333
.mr.developer.cfg
3434
.project
3535
.pydevproject
36+
37+
38+
downloads/OMERO.py-5.4.1-ice36-b75.zip
39+
downloads/OMERO.py-5.4.1-ice36-b75
40+
cli_scripts/downloads
41+
.idea
42+
.DS_Store

omero/annotation_scripts/README.md

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,106 @@ The scripts were developed using the following resources:
1919
# Installing OMERO CLI for 5.4.1
2020

2121
## 1. Create a virtual env for python 2.7:
22-
conda create -n OMERO_CLI python=2.7 anaconda
22+
23+
```
24+
conda create -n OMERO_CLI python=2.7 anaconda
25+
```
26+
2327
## 2. Download the package from:
2428
Get the "OMERO python" package download
2529

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

2832
or directly from this [link](http://downloads.openmicroscopy.org/omero/5.4.1/artifacts/OMERO.py-5.4.1-ice36-b75.zip).
2933
## 3. Install the ICE library
30-
pip install zeroc-ice==3.6.4
34+
35+
```
36+
pip install zeroc-ice==3.6.4
37+
```
3138

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

35-
cd /Users/evenhuis/anaconda3/envs/OMERO_5.4_CLI
36-
mkdir -p ./etc/conda/activate.d
37-
mkdir -p ./etc/conda/deactivate.d
38-
touch ./etc/conda/activate.d/env_vars.sh
39-
touch ./etc/conda/deactivate.d/env_vars.sh
40-
43+
```
44+
cd /Users/evenhuis/anaconda3/envs/OMERO_5.4_CLI
45+
mkdir -p ./etc/conda/activate.d
46+
mkdir -p ./etc/conda/deactivate.d
47+
touch ./etc/conda/activate.d/env_vars.sh
48+
touch ./etc/conda/deactivate.d/env_vars.sh
49+
```
50+
4151
Add the following to the activate.d/env_vars.sh:
4252

53+
4354
```
4455
#!/bin/sh
56+
57+
export OMERO_PREFIX=~/Dropbox/MIF/OMERO/downloads_5.4.1/OMERO.py-5.4.1-ice36-b75
58+
4559
export ORIGPATH=$PATH
4660
export ORIGPYTHONPATH=$PYTHONPATH
47-
export OMERO_PREFIX=~/Dropbox/MIF/OMERO/downloads_5.4.1/OMERO.py-5.4.1-ice36-b75
4861
export PATH=$PATH:$OMERO_PREFIX/lib/python:$OMERO_PREFIX/bin
4962
export PYTHONPATH=$PYTHONPATH:$OMERO_PREFIX/lib/python:$OMERO_PREFIX/bin
5063
```
64+
5165
This appends the OMERO library to the search path. And the following in deactivate.d/env_vars.sh restores the path variabeles
5266

53-
export PATH=$ORIGPATH
54-
export PYTHONPATH=$ORIGPYTHONPATH
67+
```
68+
export PATH=$ORIGPATH
69+
export PYTHONPATH=$ORIGPYTHONPATH
5570
56-
unset ORIGPATH
57-
unset ORIGPYTHONPATH
58-
71+
unset ORIGPATH
72+
unset ORIGPYTHONPATH
73+
```
5974

75+
5. Examples:
6076

77+
## Run downloads
6178

79+
1. Example
80+
```bash
81+
OMERO_USERNAME='USERNAME' OMERO_USER_PASSWORD='PASSWORD' OMERO_APP_URL='omero-app.server.edu' python download_files.py -h
82+
```
83+
Result:
84+
```bash
85+
usage: download_files.py [-h] [-p PROJECT [PROJECT ...]]
86+
[-d DATASET [DATASET ...]] [-g [GROUP]] [-o] [-t]
87+
88+
Download datasets and projects from OMERO
89+
90+
optional arguments:
91+
-h, --help show this help message and exit
92+
-p PROJECT [PROJECT ...], --project PROJECT [PROJECT ...]
93+
IDs of projects to download
94+
-d DATASET [DATASET ...], --dataset DATASET [DATASET ...]
95+
IDs of datasets to download
96+
-g [GROUP], --group [GROUP]
97+
name of group
98+
-o, --orig download originals
99+
-t, --tif download OME-TIFs
100+
101+
```
102+
103+
2. Example
104+
```bash
105+
OMERO_USERNAME='USERNAME' OMERO_USER_PASSWORD='PASSWORD' OMERO_APP_URL='omero-app.server.edu' python download_files.py -d 2071 -p 759 -o
106+
```
107+
```bash
108+
109+
omero-app.research.uts.edu.au
110+
Current user:
111+
ID: 2
112+
Username: 135553
113+
Full Name: Moises Sacal Bonequi
114+
['2071']
115+
moises-dataset/
116+
/gravatar.jpeg
117+
['759']
118+
moises-dataset/
119+
/gravatar.jpeg
120+
121+
```
62122
63123
Legal
64124
-----
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python2
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Created on Mon Nov 20 14:16:53 2017
5+
6+
@author: evenhuis
7+
8+
FOR TRAINING PURPOSES ONLY!
9+
10+
Change this file to Parse_OMERO_Properties.py and enter your ID/username
11+
"""
12+
13+
import omero
14+
import os
15+
import sys
16+
17+
try:
18+
omero_app_url = os.environ["OMERO_APP_URL"]
19+
omero_username = os.environ["OMERO_USERNAME"]
20+
omero_user_password = os.environ["OMERO_USER_PASSWORD"]
21+
except KeyError:
22+
print "Please set the environment variable OMERO_USERNAME, OMERO_USER_PASSWORD and OMERO_APP_URL"
23+
sys.exit(1)
24+
25+
client = omero.client(omero_app_url)
26+
27+
omeroProperties = client.getProperties().getPropertiesForPrefix('omero')
28+
29+
# Configuration
30+
# =================================================================
31+
# These values will be imported by all the other training scripts.
32+
HOST = omeroProperties.get('omero.host', omero_app_url)
33+
PORT = omeroProperties.get('omero.port', 4064)
34+
USERNAME = omeroProperties.get('omero.user', omero_username)
35+
PASSWORD = omeroProperties.get('omero.pass', omero_user_password)
36+
OMERO_WEB_HOST = omeroProperties.get('omero.webhost')
37+
SERVER_NAME = omeroProperties.get(omero_app_url)
38+
# projectId = omeroProperties.get('omero.projectid')
39+
# datasetId = omeroProperties.get('omero.datasetid')
40+
# imageId = omeroProperties.get('omero.imageid')

omero/annotation_scripts/cli_scripts/download_files.py

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,123 +5,131 @@
55
66
@author: evenhuis
77
"""
8-
#from Parse_OMERO_Properties import datasetId, imageId, plateId
8+
# from Parse_OMERO_Properties import datasetId, imageId, plateId
99

1010
import sys
1111
import argparse
1212
import os
1313

14+
1415
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
15-
def download_dataset( conn, Id, path, orig=False, tif=False ):
16-
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
17-
''' download a dataset from OMERO
18-
INPUT : conn : the connection needs to be open
19-
Id : ID of the dataset
20-
path : location of local filesystem
21-
fmt : "o" is orginal , "t" is tiff
22-
'''
23-
16+
def download_dataset(conn, Id, path, orig=False, tif=False):
17+
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
18+
"""
19+
download a dataset from OMERO
20+
INPUT : conn, the connection needs to be open
21+
Id : ID of the dataset
22+
path : location of local filesystem
23+
fmt : "o" is original , "t" is tiff
24+
"""
25+
2426
# get the data set
25-
dataset = conn.getObject('Dataset',Id)
26-
if( dataset==None ):
27+
dataset = conn.getObject('Dataset', Id)
28+
if (dataset == None):
2729
print("Dataset ID {} not found in group".format(Id))
2830
sys.exit(1)
2931
print("here")
3032

3133
# get the images
32-
imgs = list(dataset.listChildren())
33-
34+
imgs = list(dataset.listChildren())
35+
3436
# this is the directory to place the data in
3537
ds_name = dataset.getName()
36-
print("{}/".format(ds_name) )
37-
reldir = os.path.join( path, ds_name)
38-
if( not os.path.isdir(reldir) ):
38+
print("{}/".format(ds_name))
39+
reldir = os.path.join(path, ds_name)
40+
if (not os.path.isdir(reldir)):
3941
os.makedirs(reldir)
40-
42+
4143
for img in imgs:
42-
print(" "*len(ds_name)+"/{}".format(img.getName()))
43-
44-
if( orig ):
44+
print(" " * len(ds_name) + "/{}".format(img.getName()))
45+
46+
if (orig):
4547
for orig in img.getImportedImageFiles():
4648
name = orig.getName()
47-
file_path = os.path.join( reldir, name)
49+
file_path = os.path.join(reldir, name)
4850

49-
if( not os.path.exists( file_path) ):
51+
if (not os.path.exists(file_path)):
5052
with open(str(file_path), 'w') as f:
5153
for chunk in orig.getFileInChunks():
5254
f.write(chunk)
5355

54-
if( tif ):
55-
name = os.path.basename(img.getName())+".ome.tif"
56+
if (tif):
57+
name = os.path.basename(img.getName()) + ".ome.tif"
5658
file_path = os.path.join(reldir, name)
5759
file_size, block_gen = img.exportOmeTiff(bufsize=65536)
5860
with open(str(file_path), "wb") as f:
5961
for piece in block_gen:
6062
f.write(piece)
61-
62-
return
63+
64+
return
65+
6366

6467
"""
6568
start-code
6669
"""
6770

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

7678
args = parser.parse_args()
7779

7880
# Create a connection
7981
# ===================
82+
83+
84+
from omero.gateway import BlitzGateway
85+
from Parse_OMERO_Properties import USERNAME, PASSWORD, HOST, PORT
86+
87+
print(HOST)
88+
89+
conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT)
90+
8091
try:
81-
from omero.gateway import BlitzGateway
82-
from Parse_OMERO_Properties import USERNAME, PASSWORD, HOST, PORT
83-
print(HOST)
84-
conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT)
8592
conn.connect()
86-
93+
8794
user = conn.getUser()
8895
print "Current user:"
8996
print " ID:", user.getId()
9097
print " Username:", user.getName()
9198
print " Full Name:", user.getFullName()
9299

93-
if( args.group is not None ):
100+
if args.group is not None:
94101
print("change group")
95102
new_group = args.group
96-
groups = [ g.getName() for g in conn.listGroups() ]
103+
groups = [g.getName() for g in conn.listGroups()]
97104
print(groups)
98-
if( new_group not in groups ):
105+
if new_group not in groups:
99106
print("{} not found in groups:".format(new_group))
100107
for gn in groups:
101108
print(" {}".format(gn))
102109
sys.exit(1)
103110
else:
104111
conn.setGroupNameForSession(new_group)
105-
106-
path = os.getcwd()
107-
print( args.dataset )
112+
113+
path = os.path.join(os.getcwd(), 'downloads')
114+
print(args.dataset)
108115
for d_id in args.dataset:
109-
download_dataset( conn, d_id, path, orig=args.orig, tif=args.tif )
116+
download_dataset(conn, d_id, path, orig=args.orig, tif=args.tif)
110117

111118
print(args.project)
112119
for p_id in args.project:
113-
project = conn.getObject('Project',p_id)
114-
path_p = os.path.join(path,project.getName())
115-
if( project==None ):
120+
project = conn.getObject('Project', p_id)
121+
path_p = os.path.join(path, project.getName())
122+
if project is None:
116123
print("project ID {} not found in group {}".format(p_id, orig=args.orig, tif=args.tif))
117124
sys.exit(1)
118125

119126
for ds in list(project.listChildren()):
120-
download_dataset( conn, ds.getId(), path_p, orig=args.orig, tif=args.tif )
127+
download_dataset(conn, ds.getId(), path_p, orig=args.orig, tif=args.tif)
128+
129+
except Exception:
130+
print "There was a problem trying to connect"
131+
sys.exit(1)
121132

122-
123-
finally:
133+
finally:
124134
# When you are done, close the session to free up server resources.
125135
conn.close()
126-
127-

omero/annotation_scripts/cli_scripts/downloads/.gitkeep

Whitespace-only changes.

omero/annotation_scripts/downloads/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)