-
Notifications
You must be signed in to change notification settings - Fork 23
/
allen_mouse_atlas.py
104 lines (88 loc) · 3.05 KB
/
allen_mouse_atlas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from allensdk.api.queries.ontologies_api import OntologiesApi
from allensdk.api.queries.reference_space_api import ReferenceSpaceApi
from allensdk.core.reference_space_cache import ReferenceSpaceCache
from requests import exceptions
from pathlib import Path
from tqdm import tqdm
from atlas_gen.wrapup import wrapup_atlas_from_data
from brainatlas_api import descriptors
# Specify information about the atlas:
RES_UM = 25
VERSION = 2
ATLAS_NAME = f"allen_mouse"
SPECIES = "Mus musculus"
ATLAS_LINK = "http://www.brain-map.org.com"
CITATION = "Wang et al 2020, https://doi.org/10.1016/j.cell.2020.04.007"
ORIENTATION = "asl"
# Working path on disk:
working_dir = Path.home() / "brainglobe_workingdir"
working_dir.mkdir(exist_ok=True)
bg_root_dir = working_dir / "mouse"
bg_root_dir.mkdir(exist_ok=True)
# Temporary folder for nrrd files download:
download_dir_path = bg_root_dir / "downloading_path"
download_dir_path.mkdir(exist_ok=True)
# Download annotated and template volume:
#########################################
spacecache = ReferenceSpaceCache(
manifest=download_dir_path / "manifest.json",
# downloaded files are stored relative to here
resolution=RES_UM,
reference_space_key="annotation/ccf_2017"
# use the latest version of the CCF
)
# Download
annotated_volume, _ = spacecache.get_annotation_volume()
template_volume, _ = spacecache.get_template_volume()
print("Download completed...")
# Download structures tree and meshes:
######################################
oapi = OntologiesApi() # ontologies
struct_tree = spacecache.get_structure_tree() # structures tree
# Find id of set of regions with mesh:
select_set = "Structures whose surfaces are represented by a precomputed mesh"
mesh_set_ids = [
s["id"]
for s in oapi.get_structure_sets()
if s["description"] == select_set
]
structs_with_mesh = struct_tree.get_structures_by_set_id(mesh_set_ids)
# Directory for mesh saving:
meshes_dir = bg_root_dir / descriptors.MESHES_DIRNAME
space = ReferenceSpaceApi()
meshes_dict = dict()
for s in tqdm(structs_with_mesh):
name = s["id"]
filename = meshes_dir / f"{name}.obj"
try:
space.download_structure_mesh(
structure_id=s["id"],
ccf_version="annotation/ccf_2017",
file_name=filename,
)
meshes_dict[name] = filename
except (exceptions.HTTPError, ConnectionError):
print(s)
# Loop over structures, remove entries not used:
for struct in structs_with_mesh:
[struct.pop(k) for k in ["graph_id", "structure_set_ids", "graph_order"]]
# Wrap up, compress, and remove file:0
print(f"Finalising atlas")
wrapup_atlas_from_data(
atlas_name=ATLAS_NAME,
atlas_minor_version=VERSION,
citation=CITATION,
atlas_link=ATLAS_LINK,
species=SPECIES,
resolution=(RES_UM,) * 3,
orientation=ORIENTATION,
root_id=997,
reference_stack=template_volume,
annotation_stack=annotated_volume,
structures_list=structs_with_mesh,
meshes_dict=meshes_dict,
working_dir=bg_root_dir,
hemispheres_stack=None,
cleanup_files=False,
compress=True,
)