from allensdk.api.cache import Cache, cacheable
from allensdk.api.queries.grid_data_api import GridDataApi
from allensdk.api.queries.mouse_connectivity_api import MouseConnectivityApi
from .grid_data_api_prerelease import GridDataApiPrerelease
from ...core import lims_utilities as lu
_STRUCTURE_TREE_ROOT_ID = 997
_STRUCTURE_TREE_ROOT_NAME = "root"
_STRUCTURE_TREE_ROOT_ACRONYM = "root"
_EXPERIMENT_QUERY = '''
with specimens_concat_workflows as (
select sp.id,
string_agg(w.name, '|') as workflows
from specimens as sp
join specimens_workflows as spw on spw.specimen_id = sp.id
join workflows as w on w.id = spw.workflow_id
group by sp.id
),
injections_concat_structures as (
select inj.id as injection_id,
string_agg(st.name,'|' order by st.graph_order)
as injection_structures_name,
string_agg(st.acronym,'|' order by st.graph_order)
as injection_structures_acronym,
string_agg(cast(st.id as varchar),'|' order by st.graph_order)
as injection_structures_id
from injections as inj
join injections_structures as ist on ist.injection_id = inj.id
join flat_structures_v st on st.id = ist.structure_id
group by inj.id
)
select distinct
iser.id,
iser.workflow_state,
sp.name as specimen_name,
gdr.name as gender,
a.name as age,
p.name as project_code,
spcw.workflows,
g.name as transgenic_line, --some image series have 2 lines
pst.id as structure_id,
pst.name as structure_name,
pst.acronym as structure_acronym,
ics.injection_structures_name,
ics.injection_structures_acronym,
ics.injection_structures_id
from image_series as iser
join projects as p on p.id = iser.project_id
join specimens as sp on sp.id = iser.specimen_id
join donors as d on d.id = sp.donor_id
join injections inj on inj.specimen_id = sp.id
left join flat_structures_v pst on pst.id = inj.primary_injection_structure_id
left join ages as a on a.id = d.age_id
left join genders as gdr on gdr.id = d.gender_id
left join donors_genotypes as dg on dg.donor_id = sp.donor_id
left join genotypes as g on dg.genotype_id = g.id
-- concat joins --
left join specimens_concat_workflows as spcw on spcw.id = iser.specimen_id
left join injections_concat_structures as ics on ics.injection_id = inj.id
-- only image series we can pull and ensure mice (should all be mice already) --
where iser.storage_directory is not null and d.organism_id = 2
'''
def _experiment_dict(row):
# use empty strings instead of null
null_fill = lambda s: s if s is not None else ""
exp = dict()
exp['id'] = row[b'id']
exp['age'] = null_fill(row[b'age'])
exp['gender'] = null_fill(row[b'gender'])
exp['project_code'] = null_fill(row[b'project_code'])
exp['specimen_name'] = null_fill(row[b'specimen_name'])
exp['transgenic_line'] = null_fill(row[b'transgenic_line'])
exp['workflow_state'] = null_fill(row[b'workflow_state'])
# list : [''] or ['workflow1', 'workflow2', ... ]
exp['workflows'] = null_fill(row[b'workflows'])
exp['workflows'] = exp['workflows'].split('|')
if row[b'structure_id'] is not None:
exp['structure_id'] = row[b'structure_id']
exp['structure_name'] = row[b'structure_name']
exp['structure_abbrev'] = row[b'structure_acronym']
else:
# use root structure for compatibility with structure tree
exp['structure_id'] = _STRUCTURE_TREE_ROOT_ID
exp['structure_name'] = _STRUCTURE_TREE_ROOT_NAME
exp['structure_abbrev'] = _STRUCTURE_TREE_ROOT_ACRONYM
if row[b'injection_structures_id'] is not None:
ids = row[b'injection_structures_id'].split('|')
names = row[b'injection_structures_name'].split('|')
acronyms = row[b'injection_structures_acronym'].split('|')
else:
# have at least prim. inj. struct. in structures
ids = (exp['structure_id'], )
names = (exp['structure_name'], )
acronyms = (exp['structure_abbrev'], )
keys = 'id', 'name', 'abbreviation'
values = zip(ids, names, acronyms)
structures = map(lambda s: dict(zip(keys, s)), values)
exp['injection_structures'] = list(structures)
return exp
[docs]class MouseConnectivityApiPrerelease(MouseConnectivityApi):
'''Client for retrieving prereleased mouse connectivity data from lims.
Parameters
----------
base_uri : string, optional
Does not affect pulling from lims.
file_name : string, optional
File name to save/read storage_directories dict. Passed to
GridDataApiPrerelease constructor.
'''
def __init__(self,
storage_directories_file_name,
cache_storage_directories=True,
base_uri=None):
super(MouseConnectivityApiPrerelease, self).__init__(base_uri=base_uri)
self.grid_data_api = GridDataApiPrerelease.from_file_name(
storage_directories_file_name, cache=cache_storage_directories)
[docs] @cacheable()
def get_experiments(self):
query_result = lu.query(_EXPERIMENT_QUERY)
experiments = []
for row in query_result:
if str(row[b'id']) in self.grid_data_api.storage_directories:
exp_dict = _experiment_dict(row)
experiments.append(exp_dict)
return experiments
#@cacheable()
[docs] def get_structure_unionizes(self):
raise NotImplementedError()
[docs] @cacheable(strategy='create',
pathfinder=Cache.pathfinder(file_name_position=1,
path_keyword='path'))
def download_injection_density(self, path, experiment_id, resolution):
file_name = "%s_%s.nrrd" % (GridDataApi.INJECTION_DENSITY, resolution)
self.grid_data_api.download_projection_grid_data(
path, experiment_id, file_name)
[docs] @cacheable(strategy='create',
pathfinder=Cache.pathfinder(file_name_position=1,
path_keyword='path'))
def download_projection_density(self, path, experiment_id, resolution):
file_name = "%s_%s.nrrd" % (GridDataApi.PROJECTION_DENSITY, resolution)
self.grid_data_api.download_projection_grid_data(
path, experiment_id, file_name)
[docs] @cacheable(strategy='create',
pathfinder=Cache.pathfinder(file_name_position=1,
path_keyword='path'))
def download_injection_fraction(self, path, experiment_id, resolution):
file_name = "%s_%s.nrrd" % (GridDataApi.INJECTION_FRACTION, resolution)
self.grid_data_api.download_projection_grid_data(
path, experiment_id, file_name)
[docs] @cacheable(strategy='create',
pathfinder=Cache.pathfinder(file_name_position=1,
path_keyword='path'))
def download_data_mask(self, path, experiment_id, resolution):
file_name = "%s_%s.nrrd" % (GridDataApi.DATA_MASK, resolution)
self.grid_data_api.download_projection_grid_data(
path, experiment_id, file_name)