Skip to content

Commit 62a5f97

Browse files
committed
Add form for custom configs.
1 parent 823a159 commit 62a5f97

File tree

3 files changed

+107
-20
lines changed

3 files changed

+107
-20
lines changed

omero_vitessce/forms.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from django import forms
2+
3+
4+
class ConfigForm(forms.Form):
5+
6+
def __init__(self, file_names, file_urls,
7+
img_names, img_urls, *args, **kwargs):
8+
super(ConfigForm, self).__init__(*args, **kwargs)
9+
10+
self.text_choices = [i for i in zip(file_urls, file_names)]
11+
self.image_choices = [i for i in zip(img_urls, img_names)]
12+
13+
self.text_choices.insert(0, ('', '---'))
14+
15+
# No empty default, we alway want an image in the config
16+
self.fields["image"] = forms.ChoiceField(
17+
choices=self.image_choices, required=True)
18+
19+
# For other fields it is OK not to have an image
20+
self.image_choices.insert(0, ('', '---'))
21+
22+
self.fields["segmentation"] = forms.ChoiceField(
23+
choices=self.image_choices, required=False)
24+
self.fields["cell identities"] = forms.ChoiceField(
25+
choices=self.text_choices, required=False)
26+
self.fields["expression"] = forms.ChoiceField(
27+
choices=self.text_choices, required=False)

omero_vitessce/templates/omero_vitessce/vitessce_panel.html

+8-4
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@
88
{% if json_configs %}
99
<div class="right_tab_inner">
1010
<p>Please select an attached config file from the list below:</p>
11+
<ul>
1112
{% for file, url in json_configs.items%}
12-
<a href={{url}} target="_blank"> {{file}}</a>
13+
<li><a href={{url}} target="_blank"> {{file}}</a></li>
1314
{% endfor %}
15+
</ul>
1416
</div>
1517
{% else %}
1618
<div class="right_tab_inner">
1719
<p> There are no config files attached! </p>
1820
<p> Either attach a config file or click below to autogenerate one:</p>
19-
<button type="button">
20-
<a href="{% url 'generate_config' %}/{{obj_type}}/{{obj_id}}";>Generate Config File</a>
21-
</button>
2221

22+
<form action='/omero_vitessce/generate_config/{{obj_type}}/{{obj_id}}' method="post">
23+
{% csrf_token %}
24+
{{ form }}
25+
<input type = "submit" value = "Generate Config">
26+
</form>
2327
</div>
2428
{% endif %}
2529
</body>

omero_vitessce/views.py

+72-16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from omeroweb.webclient.decorators import login_required
88

99
from . import omero_vitessce_settings
10+
from .forms import ConfigForm
1011

1112
from vitessce import VitessceConfig, OmeZarrWrapper, MultiImageWrapper
1213
from vitessce import ViewType as Vt, FileType as Ft, CoordinationType as Ct
@@ -15,6 +16,32 @@
1516
SERVER = omero_vitessce_settings.SERVER_ADDRESS[1:-1]
1617

1718

19+
def get_files_images(obj_type, obj_id, conn):
20+
""" Gets all the files attached to an object,
21+
and images if the object is a dataset,
22+
and returns a list of file names and a list of urls
23+
for the files and eventually the images
24+
"""
25+
obj = conn.getObject(obj_type, obj_id)
26+
file_names = [
27+
i for i in obj.listAnnotations()
28+
if i.OMERO_TYPE().NAME ==
29+
"ome.model.annotations.FileAnnotation_name"]
30+
file_urls = [i.getId() for i in file_names]
31+
file_names = [i.getFileName() for i in file_names]
32+
file_urls = [SERVER + "/webclient/annotation/" + str(i) for i in file_urls]
33+
34+
if obj_type == "dataset":
35+
imgs = list(obj.listChildren())
36+
img_urls = [build_zarr_image_url(i.getId()) for i in imgs]
37+
img_names = [i.getName() for i in imgs]
38+
else:
39+
img_urls = [build_zarr_image_url(obj_id)]
40+
img_names = [obj.getName()]
41+
42+
return file_names, file_urls, img_names, img_urls
43+
44+
1845
def build_viewer_url(config_id):
1946
""" Generates urls like:
2047
http://localhost:4080/omero_vitessce/?config=http://localhost:4080/webclient/annotation/999
@@ -47,28 +74,48 @@ def get_attached_configs(obj_type, obj_id, conn):
4774
return config_files, config_urls
4875

4976

50-
def create_dataset_config(dataset_id, conn):
77+
def create_dataset_config(dataset_id, config_args):
5178
"""
5279
Generates a Vitessce config for an OMERO dataset and returns it.
5380
Assumes all images in the dataset are zarr files
5481
which can be served with omero-web-zarr.
5582
All images are added to the same view.
5683
"""
57-
dataset = conn.getObject("dataset", dataset_id)
58-
images = [i for i in dataset.listChildren()]
59-
6084
vc = VitessceConfig(schema_version="1.0.6")
6185
vc_dataset = vc.add_dataset()
62-
wrappers = []
63-
for img in images:
64-
wrapper = OmeZarrWrapper(
65-
img_url=build_zarr_image_url(img.getId()),
66-
name=img.getName())
67-
wrappers.append(wrapper)
68-
vc_dataset.add_object(MultiImageWrapper(image_wrappers=wrappers,
86+
87+
img_url = config_args.get("image")
88+
89+
images = [OmeZarrWrapper(img_url=img_url, name="Image")]
90+
91+
vc.add_view(Vt.SPATIAL, dataset=vc_dataset, x=0, y=0, w=8, h=10)
92+
vc.add_view(Vt.LAYER_CONTROLLER, dataset=vc_dataset, x=8, y=0, w=2, h=10)
93+
94+
if config_args.get("cell identities"):
95+
vc_dataset = vc_dataset.add_file(
96+
url=config_args.get("cell identities"),
97+
file_type=Ft.OBS_SETS_CSV,
98+
coordination_values={"obsType": "cell"},
99+
options={
100+
"obsIndex": "cell_id",
101+
"obsSets": [
102+
{"name": "Clustering", "column": "graphclust"}]})
103+
vc.add_view(Vt.OBS_SETS, dataset=vc_dataset, x=10, y=5, w=2, h=5)
104+
if config_args.get("expression"):
105+
vc_dataset = vc_dataset.add_file(
106+
url=config_args.get("expression"),
107+
file_type=Ft.OBS_FEATURE_MATRIX_CSV)
108+
vc.add_view(Vt.FEATURE_LIST, dataset=vc_dataset, x=10, y=0, w=2, h=5)
109+
if config_args.get("segmentation"):
110+
segmentation = OmeZarrWrapper(
111+
img_url=config_args.get("segmentation"),
112+
name="Segmentation",
113+
is_bitmask=True)
114+
images.append(segmentation)
115+
116+
vc_dataset.add_object(MultiImageWrapper(image_wrappers=images,
69117
use_physical_size_scaling=True))
70-
vc.add_view(Vt.SPATIAL, dataset=vc_dataset, x=0, y=0, w=10, h=10)
71-
vc.add_view(Vt.LAYER_CONTROLLER, dataset=vc_dataset, x=10, y=0, w=2, h=10)
118+
72119
vc.add_coordination_by_dict({
73120
Ct.SPATIAL_ZOOM: 2,
74121
Ct.SPATIAL_TARGET_X: 0,
@@ -77,7 +124,7 @@ def create_dataset_config(dataset_id, conn):
77124
return vc
78125

79126

80-
def create_image_config(image_id):
127+
def create_image_config(image_id, config_args):
81128
"""
82129
Generates a Vitessce config for an OMERO image and returns it.
83130
Assumes the images is an OME-NGFF v0.4 file
@@ -87,8 +134,10 @@ def create_image_config(image_id):
87134
vc_dataset = vc.add_dataset().add_file(
88135
url=build_zarr_image_url(image_id),
89136
file_type=Ft.IMAGE_OME_ZARR)
137+
90138
vc.add_view(Vt.SPATIAL, dataset=vc_dataset, x=0, y=0, w=10, h=10)
91139
vc.add_view(Vt.LAYER_CONTROLLER, dataset=vc_dataset, x=10, y=0, w=2, h=10)
140+
92141
vc.add_coordination_by_dict({
93142
Ct.SPATIAL_ZOOM: 2,
94143
Ct.SPATIAL_TARGET_X: 0,
@@ -133,6 +182,12 @@ def vitessce_panel(request, obj_type, obj_id, conn=None, **kwargs):
133182
context = {"json_configs": dict(zip(config_files, config_urls)),
134183
"obj_type": obj_type, "obj_id": obj_id}
135184

185+
if not config_files:
186+
files, urls, img_files, img_urls = get_files_images(
187+
obj_type, obj_id, conn)
188+
form = ConfigForm(files, urls, img_files, img_urls)
189+
context["form"] = form
190+
136191
return render(request, "omero_vitessce/vitessce_panel.html", context)
137192

138193

@@ -142,11 +197,12 @@ def generate_config(request, obj_type, obj_id, conn=None, **kwargs):
142197
write it to a temporarily file and attach it. Then open the
143198
viewer with the autogenerated config.
144199
"""
200+
145201
obj_id = int(obj_id)
146202
if obj_type == "image":
147-
vitessce_config = create_image_config(obj_id)
203+
vitessce_config = create_image_config(obj_id, request.POST)
148204
if obj_type == "dataset":
149-
vitessce_config = create_dataset_config(obj_id, conn)
205+
vitessce_config = create_dataset_config(obj_id, request.POST)
150206

151207
config_id = attach_config(vitessce_config, obj_type, obj_id, conn)
152208
viewer_url = build_viewer_url(config_id)

0 commit comments

Comments
 (0)