Skip to content

Commit

Permalink
option to choose between or/and to join tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom-TBT committed Nov 8, 2023
1 parent 58a2de9 commit 98b1b91
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
4 changes: 3 additions & 1 deletion omero_tagsearch/forms.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from django.forms import Form, MultipleChoiceField, BooleanField
from django.forms import Form, MultipleChoiceField, BooleanField, ChoiceField


class TagSearchForm(Form):
selectedTags = MultipleChoiceField()
excludedTags = MultipleChoiceField()
operation = ChoiceField()
results_preview = BooleanField()

def __init__(self, tags, conn=None, *args, **kwargs):
Expand All @@ -12,4 +13,5 @@ def __init__(self, tags, conn=None, *args, **kwargs):
# Process Tags into choices (lists of tuples)
self.fields["selectedTags"].choices = tags
self.fields["excludedTags"].choices = tags
self.fields["operation"].choices = (("AND", "AND"), ("OR", "OR"))
self.conn = conn
7 changes: 6 additions & 1 deletion omero_tagsearch/templates/omero_tagsearch/tagnav.html
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@
}
});

$('#id_operation').change(function() {
$("#tagSearchForm").submit();
});


$("#filtersearch label").inFieldLabels();

Expand Down Expand Up @@ -215,6 +219,7 @@ <h2>{% trans "Tag Search" %}</h2>
{{ tagnav_form.non_field_errors }}
<div class="fieldWrapper">
<div class="tagSearchDivider">
<label for="id_operation">Joining method:</label>{{ tagnav_form.operation }}<br/>
{{ tagnav_form.selectedTags.errors }}
<label for="id_selectedTags">Selected Tags:</label>
{{ tagnav_form.selectedTags }}
Expand All @@ -223,7 +228,7 @@ <h2>{% trans "Tag Search" %}</h2>
Each time a tag is added, the list of potential tags to add is reduced to those that overlap with the tags already specified.
A count of results is shown in the search panel and at any time it is possible to show the Projects, Datasets and Images that match by turning on the <b>Preview<b></small>">
<img src="{% static 'webgateway/img/help16.png' %}" />
</span><br/>
</span><br/><br/>
{{ tagnav_form.excludedTags.errors }}
<label for="id_excludedTags">Excluded Tags:</label>
{{ tagnav_form.excludedTags }}
Expand Down
63 changes: 28 additions & 35 deletions omero_tagsearch/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ def tag_image_search(request, conn=None, **kwargs):

selected_tags = [int(x) for x in request.POST.getlist("selectedTags")]
excluded_tags = [int(x) for x in request.POST.getlist("excludedTags")]
operation = request.POST.get("operation")
results_preview = bool(request.POST.get("results_preview"))

# validate experimenter is in the active group
Expand All @@ -255,32 +256,24 @@ def tag_image_search(request, conn=None, **kwargs):
service_opts = conn.SERVICE_OPTS.copy()
service_opts.setOmeroGroup(active_group)

def getObjectsWithAllAnnotations(obj_type, include_ids, exclude_ids):
def getObjectsWithAnnotations(obj_type, include_ids, exclude_ids):
# Get the images that match
if len(exclude_ids) == 0:
hql = (
"select link.parent.id from %sAnnotationLink link "
"where link.child.id in (:incl_ids) "
"group by link.parent.id "
"having count (distinct link.child) = %s" % (obj_type, len(include_ids))
)
params = Parameters()
params.map = {}
params.map["incl_ids"] = rlist([rlong(o) for o in set(include_ids)])
else:
hql = (
"select link.parent.id from %sAnnotationLink link "
"where link.child.id in (:incl_ids) and link.parent.id not in "
"(select link.parent.id from %sAnnotationLink link "
"where link.child.id in (:excl_ids))"
"group by link.parent.id "
"having count (distinct link.child) = %s" % (obj_type, obj_type, len(include_ids))
)
params = Parameters()
params.map = {}
params.map["incl_ids"] = rlist([rlong(o) for o in set(include_ids)])
params = Parameters()
params.map = {}
params.map["incl_ids"] = rlist([rlong(o) for o in set(include_ids)])

hql = ("select link.parent.id from %sAnnotationLink link "
"where link.child.id in (:incl_ids) " % (obj_type))
if len(exclude_ids) > 0:
hql += (" and link.parent.id not in "
"(select link.parent.id from %sAnnotationLink link "
"where link.child.id in (:excl_ids)) " % (obj_type))
params.map["excl_ids"] = rlist([rlong(o) for o in set(exclude_ids)])

hql += "group by link.parent.id"
if operation == "AND":
hql += f" having count (distinct link.child) = {len(include_ids)}"

qs = conn.getQueryService()
return [x[0].getValue() for x in qs.projection(hql, params, service_opts)]

Expand All @@ -298,27 +291,27 @@ def getObjectsWithAllAnnotations(obj_type, include_ids, exclude_ids):
image_count = 0

if selected_tags:
image_ids = getObjectsWithAllAnnotations("Image", selected_tags, excluded_tags)
image_ids = getObjectsWithAnnotations("Image", selected_tags, excluded_tags)
context["image_count"] = len(image_ids)
image_count = len(image_ids)

dataset_ids = getObjectsWithAllAnnotations("Dataset", selected_tags, excluded_tags)
dataset_ids = getObjectsWithAnnotations("Dataset", selected_tags, excluded_tags)
context["dataset_count"] = len(dataset_ids)
dataset_count = len(dataset_ids)

project_ids = getObjectsWithAllAnnotations("Project", selected_tags, excluded_tags)
project_ids = getObjectsWithAnnotations("Project", selected_tags, excluded_tags)
context["project_count"] = len(project_ids)
project_count = len(project_ids)

screen_ids = getObjectsWithAllAnnotations("Screen", selected_tags, excluded_tags)
screen_ids = getObjectsWithAnnotations("Screen", selected_tags, excluded_tags)
context["screen_count"] = len(screen_ids)
screen_count = len(screen_ids)

plate_ids = getObjectsWithAllAnnotations("Plate", selected_tags, excluded_tags)
plate_ids = getObjectsWithAnnotations("Plate", selected_tags, excluded_tags)
context["plate_count"] = len(plate_ids)
plate_count = len(plate_ids)

acquisition_ids = getObjectsWithAllAnnotations(
acquisition_ids = getObjectsWithAnnotations(
"PlateAcquisition", selected_tags, excluded_tags
)
context["acquisition_count"] = len(acquisition_ids)
Expand Down Expand Up @@ -372,14 +365,14 @@ def getObjectsWithAllAnnotations(obj_type, include_ids, exclude_ids):

def getAnnotationsForObjects(obj_type, oids):
# Get the images that match
hql = (
"select distinct link.child.id from %sAnnotationLink link "
"where link.parent.id in (:oids)" % obj_type
)

params = Parameters()
params.map = {}
params.map["oids"] = rlist([rlong(o) for o in oids])
hql = (
"select distinct link.child.id from %sAnnotationLink link " % obj_type
)
if operation == "AND":
hql += "where link.parent.id in (:oids)"
params.map["oids"] = rlist([rlong(o) for o in oids])

qs = conn.getQueryService()
return [
Expand Down

0 comments on commit 98b1b91

Please sign in to comment.