# ONE REST queries

While the basic one.search command allows us to filter experimental sessions by date, lab, subject etc., you may quickly find that you want to apply more complex queries to restrict your search output. This can be achieved using the one.alyx.rest command. This has access to a range of tables stored in the database and gives us more flexibility when forming our queries.

Note.

REST queries can only be made while ONE is in online mode.

## one.search vs one.alyx.rest

We will get started by importing ONE

[1]:

from one.api import ONE
one = ONE(base_url='https://openalyx.internationalbrainlab.org')


We can list the possible tables that we have access to, by typing the following command

[2]:

one.alyx.list_endpoints()

Out[2]:

['brain-regions',
'cache',
'cache.zip',
'channels',
'data-formats',
'data-repository',
'data-repository-type',
'dataset-types',
'datasets',
'files',
'insertions',
'labs',
'locations',
'notes',
'projects',
'register-file',
'revisions',
'sessions',
'subjects',
'surgeries',
'sync-file-status',
'tags',
'trajectories',
'users',
'water-requirement',
'water-restricted-subjects',
'water-restriction',
'water-type',
'weighings']


Let’s first look at sessions table. The information about what filters can be applied to this table are available under FILTERS here. Let’s apply a filter based on performance_qte and task_protocol. We can do this using the following expression,

[3]:

sess_info = one.alyx.rest('sessions', 'list', performance_gte=70, task_protocol='ephys')
sess_info[0]

Out[3]:

{'subject': 'SWC_043',
'start_time': '2020-09-21T19:02:16.707541',
'number': 1,
'lab': 'hoferlab',
'project': 'ibl_neuropixel_brainwide_01',
'url': 'https://openalyx.internationalbrainlab.org/sessions/4ecb5d24-f5cc-402c-be28-9d0f7cb14b3a',


Notice how this command doesn’t just return the session eID but a dictionary containing information about each session. We can extract the set of eIDs using the to_eid method:

[4]:

one.to_eid(sess_info)

Out[4]:

['4ecb5d24-f5cc-402c-be28-9d0f7cb14b3a',
'c7bd79c9-c47e-4ea5-aea3-74dda991b48e',


You might have noticed that this same query could have been achieved using the one.search method with the remote query flag,

[5]:

eids = one.search(performance_gte=70, task_protocol='ephys', query_type='remote')
eids

Out[5]:

<one.util.LazyId at 0x1f77a7de070>


## Accessing other Alyx tables

With the one.alyx.rest command we are not only limited to the sessions table, but can formulate queries based on other tables to find session eIDs of interest. Consider the case where we want to find all sessions that have probe insertions that target a specific ML and AP coordinate. For this we can formulate our query based on the trajectories table. Let’s see if there are any probe insertions at the coordinates ML(x) = -2225, and AP(y) = -1894 from bregma.

[6]:

trajs = one.alyx.rest('trajectories', 'list', x=-2224.97344914846, y=-1893.87527870804)
trajs[0]

Out[6]:

{'id': '4c9f8b8c-16c5-45fe-8f5c-3c490ab410c0',
'probe_insertion': 'b749446c-18e3-4987-820a-50649ab0f826',
'x': -2224.97344914846,
'y': -1893.87527870804,
'z': -118.0,
'depth': 4056.23152523877,
'theta': 10.0389727803095,
'phi': 147.057669457309,
'roll': 0.0,
'provenance': 'Ephys aligned histology track',
'session': {'subject': 'KS023',
'start_time': '2019-12-10T16:44:12.945000',
'number': 1,
'lab': 'cortexlab',
'probe_name': 'probe01',
'coordinate_system': 'IBL-Allen',
'datetime': '2020-10-22T17:12:18.051432',
'json': {'2020-06-12T00:39:15_nate': [[-1.0019317591838977,
0.00034058267716535555,
0.0016527874015748033,
0.0024480629921259846,
0.002829795275590551,
0.003028614173228347,
0.0034103464566929134,
1.0047060407069595],
[-1.0406470176854647,
0.00048822751056065705,
0.0018277480314961014,
0.0025676767397318587,
0.003116094488188976,
0.0033228661417322833,
0.003631191804541761,
1.043766234935011]],
'2020-06-12T13:59:02_guido': [[-1.0019317591838977,
0.0015536774969915758,
0.0019066305655836338,
0.0024734945848375457,
0.002831795427196149,
0.0030564019253910947,
0.0035163104693140793,
0.0036018748495788206,
1.0047060407069595],
[-1.0292080172001559,
0.0018317617328520441,
0.0020189338146811057,
0.0025751022864019236,
0.0031152274368231032,
0.003329138387484956,
0.003743852207068633,
0.0038439555428302105,
1.0323437818176728]],
'2020-06-19T10:52:36_noam.roth': [[-1.0019317591838977,
6.359947558177599e-05,
0.0016512625368731563,
0.001826051130776794,
0.002867499836119305,
0.003049571288102261,
0.0034938256309406746,
1.0047060407069595],
[-1.0564995398078083,
0.00013747124460739002,
0.0018187682726975574,
0.001971708292363159,
0.0031151170108161255,
0.0033263198951163546,
0.003713596584213875,
1.0595667342952302]],
'2020-06-26T16:40:14_Karolina_Socha': [[-1.0019317591838977,
0.0016060922358435495,
0.001800663164039696,
0.002870803269118505,
0.0030264600116754227,
0.0034480303561004078,
1.0047060407069595],
[-1.001722057342154,
0.001839577349678878,
0.002021176882661998,
0.0031147574223975086,
0.003318214266999465,
0.00363611558669002,
1.0049673959658172]]}}


We can find the session eID associated with this trajectory by looking at the id of the session field in the returned dictionary

[7]:

eid = trajs[0]['session']['id']
eid

Out[7]:

'aad23144-0e52-4eac-80c5-c4ee2decb198'


Note.

It is not just sessions that have unique IDs associated with them! Every object stored in Alyx has a unique UUID, whether it is a trajectory, a subject, a user or a dataset. For example in the above example we can access the unique ID of the trajectory by typing traj_id = trajs[0]['id']

## Searching with one.alyx.rest

The one.alyx.rest command is also provides an alternative method to one.list for searching the database for possible keywords that you can use to restrict your queries. For example, if we want to find the names of female subjects in the Witten lab that are alive, we can use the subjects table to write,

[8]:

subj_info = one.alyx.rest('subjects', 'list', lab='cortexlab', sex='F', alive=False)
subj_nickname = [subj['nickname'] for subj in subj_info]
subj_nickname

Out[8]:

['KS023']