This class is a bit of a mess.

1) `fetchPatients` was moved here, where clearly doesn't belong here and it's anyway a basically redundant method (and yes, I know I wrote it). The same can be accomplished by changing `QtQueryResultInstance` and add there:

```
static hasMany = [patients: PatientDimension]
static mapping = {
    ...
    patients       joinTable: [name:   'qt_patient_set_collection',
                               key:    'result_instance_id',
                               column: 'patient_num']
     ...
}
```

2)  `patientMap` is a sorted map that you seem take just to get its keys afterwards. I don't get it.

3) The whole `retrieveData(Study study, List<Patient> patients, List<OntologyTerm> ontologyTerms) ` method is confusing. It takes a list of ontology terms (not a set), but the order doesn't seem particularly relevant since these ontology terms are then expand and you get an unknown number of terms between the original ones. It also doesn't guard against repeated ontology terms, something that `retrieveData(QueryResult patientSet,                                                                     List<ClinicalVariable> variables)` does because it was a source of bugs.

One the whole, it seems clear this interface needs to be redesigned because the original design only accounted for filtering patients by patient result. It seems to me the most straightforward way to fix this would be to change this class to: 1) receive lists of patients in addition to (or instead of) query results and 2) change the `QueriesResource` interface and add a method that returns a list of patients instead of a `QueryResult`.