def trials_json(self, restrict='reason', filter_interventions=None, filter_phases=None): """ Returns an array of trial JSON for the matching trials, optionally filtered by intervention type and/or drug phases. """ if not self.done: raise Exception("Trial results are not yet available") sqlite = SQLite.get(self.sqlite_db) if sqlite is None: raise Exception("No SQLite handle, please set up properly") # look up trials. Currently cheaply filtering by string comparison qry = "SELECT nct FROM trials WHERE run_id = ? AND reason IS NULL" if 'reason' == restrict: qry += ' AND reason IS NULL' tpls = [self.run_id] if filter_interventions is not None: ored = [] for inter in filter_interventions: ored.append('types LIKE "%%%s%%"' % inter) # ored.append('instr(types, ?)') # tpls.append(inter) if len(ored) > 0: qry = qry + ' AND (' + ' OR '.join(ored) + ')' if filter_phases is not None: ored = [] for phase in filter_phases: ored.append('phases LIKE "%%%s%%"' % phase) # ored.append('instr(phases, ?)') # tpls.append(phase) if len(ored) > 0: qry = qry + ' AND (' + ' OR '.join(ored) + ')' trials = [] fields = ['keyword', 'phase', 'overall_contact'] lat = float(self.reference_location[0]) if self.reference_location else 0 lng = float(self.reference_location[1]) if self.reference_location else 0 # retrieve ncts qry += ' ORDER BY distance ASC' for row in sqlite.execute(qry, tuple(tpls)): trial = Trial(row[0]) trial.load() trial_dict = trial.json(fields) # add trial locations if lat and lng: closest = [] for loc in trial.locations_closest_to(lat, lng, open_only=True): closest.append(loc[0].json()) trial_dict['location'] = closest trials.append(trial_dict) # grab trial data in batch from db - PROBLEM: distance order is not preserved # for trial in Trial.retrieve(ncts): # trials.append(trial.json(fields)) return trials
def trials_json(self, restrict='reason', filter_interventions=None, filter_phases=None): """ Returns an array of trial JSON for the matching trials, optionally filtered by intervention type and/or drug phases. """ if not self.done: raise Exception("Trial results are not yet available") sqlite = SQLite.get(self.sqlite_db) if sqlite is None: raise Exception("No SQLite handle, please set up properly") # look up trials. Currently cheaply filtering by string comparison qry = "SELECT nct FROM trials WHERE run_id = ? AND reason IS NULL" if 'reason' == restrict: qry += ' AND reason IS NULL' tpls = [self.run_id] if filter_interventions is not None: ored = [] for inter in filter_interventions: ored.append('types LIKE "%%%s%%"' % inter) # ored.append('instr(types, ?)') # tpls.append(inter) if len(ored) > 0: qry = qry + ' AND (' + ' OR '.join(ored) + ')' if filter_phases is not None: ored = [] for phase in filter_phases: ored.append('phases LIKE "%%%s%%"' % phase) # ored.append('instr(phases, ?)') # tpls.append(phase) if len(ored) > 0: qry = qry + ' AND (' + ' OR '.join(ored) + ')' trials = [] fields = ['keyword', 'phase', 'overall_contact'] lat = float( self.reference_location[0]) if self.reference_location else 0 lng = float( self.reference_location[1]) if self.reference_location else 0 # retrieve ncts qry += ' ORDER BY distance ASC' for row in sqlite.execute(qry, tuple(tpls)): trial = Trial(row[0]) trial.load() trial_dict = trial.json(fields) # add trial locations if lat and lng: closest = [] for loc in trial.locations_closest_to(lat, lng, open_only=True): closest.append(loc[0].json()) trial_dict['location'] = closest trials.append(trial_dict) # grab trial data in batch from db - PROBLEM: distance order is not preserved # for trial in Trial.retrieve(ncts): # trials.append(trial.json(fields)) return trials