In [1]:
import json
from allensdk.api.api import Api
from allensdk.api.queries.rma_api import RmaApi
from allensdk.api.queries.mouse_connectivity_api import MouseConnectivityApi
import pandas as pd
import itertools as it

rma = RmaApi()
mca = MouseConnectivityApi()

def read_data(parsed_json):
    return parsed_json['msg']

def pretty(result):
    print(json.dumps(result, indent=2))
    
def tabular_dataframe(parsed_tabular_json):
    df = pd.DataFrame.from_records(parsed_tabular_json,
                                   columns=parsed_tabular_json[0].keys())
    return df
In [2]:
section_data_set_id = 168095756
In [3]:
# rma version from detail page
results = mca.get_manual_injection_summary(section_data_set_id)

print('Manual Injection Summary')
print('Experiment: %d' % (results[0]['id']))
injections = results[0]['specimen']['injections']
print('Primary Structure: %s' % (injections[0]['structure']['name']))

print('Coords. (AP, ML, DV, L):')
for injection in injections:
    print ('\t%s(%f, %f, %f, %f)' % (injection['registration_point'],
                                     injection['coordinates_ap'],
                                     injection['coordinates_ml'],
                                     injection['coordinates_dv'],
                                     injection['angle']))
print('Transgenic Line: %s' % (results[0]['specimen']['donor']['transgenic_mouse']['transgenic_lines'][0]['name']))
print(results[0]['specimen']['donor']['transgenic_mouse']['transgenic_lines'][0]['description'])
Manual Injection Summary
Experiment: 168095756
Primary Structure: Agranular insular area, posterior part
Coords. (AP, ML, DV, L):
	Bregma(-0.340000, 3.750000, 3.250000, 0.000000)
	Bregma(-0.340000, 4.000000, 3.000000, 0.000000)
Transgenic Line: Syt6-Cre_KI148
Sparse, scattered expression in brain areas including the medulla, pons, and midbrain. Enriched in specific areas within thalamus, layer 6a cortex, and olfactory areas.
In [4]:
projection_results = mca.get_structure_unionizes([section_data_set_id], is_injection=False)
projection_df = tabular_dataframe(projection_results)
projection_df
Out[4]:
projection_energy sum_pixels sum_projection_pixels projection_intensity is_injection max_voxel_density hemisphere_id sum_projection_pixel_intensity structure_id volume section_data_set_id projection_density sum_pixel_intensity projection_volume max_voxel_y max_voxel_x max_voxel_z id normalized_projection_volume
0 2.306380e-04 9.702630e+08 472.7330 473.3740 False 0.044554 3 2.237790e+05 533 1.188570 168095756 4.872220e-07 2.051620e+11 5.790980e-07 260 8280 4130 467146288 0.000750
1 1.717320e-02 2.039400e+08 26210.1000 133.6240 False 0.212651 2 3.502310e+06 527 0.249827 168095756 1.285190e-04 3.540600e+10 3.210740e-05 2440 6750 10340 467144044 0.041564
2 4.622120e-03 1.954190e+10 482089.0000 187.3620 False 0.436230 1 9.032500e+07 477 23.938800 168095756 2.466950e-05 4.590190e+12 5.905590e-04 6980 6020 4070 467143013 0.764488
3 0.000000e+00 1.293840e+08 0.0000 0.0000 False 0.000000 1 0.000000e+00 810 0.158495 168095756 0.000000e+00 2.939290e+10 0.000000e+00 0 0 0 467143207 0.000000
4 0.000000e+00 1.617120e+07 0.0000 0.0000 False 0.000000 3 0.000000e+00 1077 0.019810 168095756 0.000000e+00 4.534980e+09 0.000000e+00 0 0 0 467147307 0.000000
5 1.674610e-03 9.754200e+06 68.9027 237.0660 False 0.024353 3 1.633450e+04 197 0.011949 168095756 7.063900e-06 1.124940e+09 8.440580e-08 5250 8170 5570 467145633 0.000109
6 0.000000e+00 1.080530e+08 0.0000 0.0000 False 0.000000 1 0.000000e+00 620 0.132365 168095756 0.000000e+00 2.723140e+10 0.000000e+00 0 0 0 467143091 0.000000
7 1.474660e-02 9.252310e+09 893985.0000 152.6200 False 0.410572 3 1.364400e+08 803 11.334100 168095756 9.662300e-05 1.752440e+12 1.095130e-03 7060 5150 4770 467146732 1.417670
8 1.710270e-01 3.410420e+08 95246.1000 612.3870 False 0.354223 3 5.832750e+07 312782636 0.417777 168095756 2.792790e-04 6.564230e+10 1.166760e-04 2400 10140 9640 467147588 0.151040
9 1.180080e-03 3.304350e+07 408.9390 95.3537 False 0.021354 1 3.899390e+04 530 0.040478 168095756 1.237580e-05 3.254440e+09 5.009510e-07 2060 7350 5680 467143039 0.000648
10 0.000000e+00 6.631020e+07 0.0000 0.0000 False 0.000000 2 0.000000e+00 105 0.081230 168095756 0.000000e+00 6.866070e+09 0.000000e+00 0 0 0 467143523 0.000000
11 1.376270e-04 4.258370e+08 765.5390 76.5563 False 0.031846 2 5.860680e+04 443 0.521650 168095756 1.797730e-06 1.335750e+10 9.377850e-07 2060 7320 5780 467143946 0.001214
12 0.000000e+00 1.123110e+09 0.0000 0.0000 False 0.000000 3 0.000000e+00 1021 1.375810 168095756 0.000000e+00 2.331880e+11 0.000000e+00 0 0 0 467147201 0.000000
13 0.000000e+00 7.747470e+07 0.0000 0.0000 False 0.000000 1 0.000000e+00 1026 0.094907 168095756 0.000000e+00 9.654830e+09 0.000000e+00 0 0 0 467143359 0.000000
14 1.500910e-03 3.474640e+09 12704.7000 410.4870 False 0.118254 3 5.215120e+06 637 4.256430 168095756 3.656420e-06 7.254650e+11 1.556330e-05 4700 7000 7520 467146476 0.020147
15 1.315880e-03 2.448930e+08 5933.7600 54.3077 False 0.045441 2 3.222490e+05 697 0.299994 168095756 2.423000e-05 9.208320e+09 7.268850e-06 6920 10900 5850 467144287 0.009410
16 6.636330e-07 1.585780e+09 7.3174 143.8180 False 0.004545 3 1.052370e+03 201 1.942580 168095756 4.614390e-09 4.118330e+11 8.963810e-09 1730 6040 9620 467145636 0.000012
17 2.703910e-03 5.656090e+08 5272.1700 290.0810 False 0.068384 1 1.529350e+06 968 0.692871 168095756 9.321230e-06 2.237660e+11 6.458410e-06 4420 11750 4600 467143316 0.008361
18 3.300640e-03 2.239590e+08 3209.2800 230.3340 False 0.044294 2 7.392070e+05 651 0.274349 168095756 1.432980e-05 5.177170e+10 3.931370e-06 4860 12390 6490 467144217 0.005089
19 1.446990e-02 2.316780e+07 2419.8600 138.5350 False 0.129681 3 3.352360e+05 19 0.028381 168095756 1.044490e-04 4.287460e+09 2.964320e-06 2860 4870 5710 467145304 0.003837
20 0.000000e+00 1.092520e+08 0.0000 0.0000 False 0.000000 1 0.000000e+00 634 0.133834 168095756 0.000000e+00 1.682360e+10 0.000000e+00 0 0 0 467143102 0.000000
21 2.302750e-01 3.747360e+08 313955.0000 274.8550 False 0.375780 2 8.629210e+07 153 0.459051 168095756 8.378050e-04 2.696670e+10 3.845950e-04 5200 11750 8050 467143557 0.497865
22 1.553710e-01 1.284190e+08 206661.0000 96.5478 False 0.451492 1 1.995260e+07 841 0.157314 168095756 1.609270e-03 2.570010e+09 2.531590e-04 7070 10140 5630 467143225 0.327719
23 4.105660e-03 3.608120e+08 7978.1800 185.6780 False 0.105637 1 1.481370e+06 749 0.441994 168095756 2.211180e-05 5.173540e+10 9.773270e-06 5680 8360 5140 467143170 0.012652
24 0.000000e+00 7.922700e+06 0.0000 0.0000 False 0.000000 2 0.000000e+00 318 0.009705 168095756 0.000000e+00 1.819940e+09 0.000000e+00 0 0 0 467143766 0.000000
25 0.000000e+00 1.299960e+07 0.0000 0.0000 False 0.000000 3 0.000000e+00 312782570 0.015925 168095756 0.000000e+00 2.191420e+09 0.000000e+00 0 0 0 467147539 0.000000
26 6.190850e-04 7.069100e+08 2903.8900 150.7070 False 0.088194 2 4.376380e+05 403 0.865965 168095756 4.107860e-06 1.452120e+11 3.557270e-06 5610 7450 7950 467143900 0.004605
27 0.000000e+00 5.067000e+05 0.0000 0.0000 False 0.000000 1 0.000000e+00 680 0.000621 168095756 0.000000e+00 5.614310e+07 0.000000e+00 0 0 0 467143131 0.000000
28 2.385520e-02 3.417520e+09 364667.0000 223.5610 False 0.481188 3 8.152540e+07 754 4.186460 168095756 1.067050e-04 6.736840e+11 4.467170e-04 7270 5420 8000 467146652 0.578282
29 1.129990e-03 2.515060e+09 17607.8000 161.4060 False 0.112369 2 2.842000e+06 4 3.080950 168095756 7.000960e-06 6.355190e+11 2.156960e-05 2360 10710 7580 467143462 0.027922
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2169 2.869420e-01 2.775890e+08 86146.6000 924.6080 False 0.384302 2 7.965180e+07 694 0.340046 168095756 3.103390e-04 7.527670e+10 1.055300e-04 4580 3490 8000 467144280 0.136610
2170 0.000000e+00 2.338200e+08 0.0000 0.0000 False 0.000000 3 0.000000e+00 816 0.286430 168095756 0.000000e+00 6.783150e+10 0.000000e+00 0 0 0 467146754 0.000000
2171 5.831780e-04 6.943490e+08 5051.7200 80.1568 False 0.126657 2 4.049290e+05 589 0.850578 168095756 7.275470e-06 1.396510e+11 6.188350e-06 4240 3960 5700 467144117 0.008011
2172 1.104760e+00 1.439070e+09 2268140.0000 700.9370 False 0.467217 3 1.589820e+09 119 1.762860 168095756 1.576110e-03 3.395030e+11 2.778470e-03 5190 4270 9490 467145467 3.596780
2173 0.000000e+00 4.876480e+08 0.0000 0.0000 False 0.000000 1 0.000000e+00 319 0.597369 168095756 0.000000e+00 1.028350e+11 0.000000e+00 0 0 0 467142910 0.000000
2174 2.892590e-04 2.412760e+08 476.1760 146.5670 False 0.022123 2 6.979150e+04 250 0.295564 168095756 1.973570e-06 3.295150e+10 5.833160e-07 3140 5670 6470 467143675 0.000755
2175 0.000000e+00 3.874500e+07 0.0000 0.0000 False 0.000000 2 0.000000e+00 312782550 0.047463 168095756 0.000000e+00 6.618170e+09 0.000000e+00 0 0 0 467145228 0.000000
2176 0.000000e+00 1.082980e+09 0.0000 0.0000 False 0.000000 2 0.000000e+00 138 1.326650 168095756 0.000000e+00 2.138610e+11 0.000000e+00 0 0 0 467143546 0.000000
2177 0.000000e+00 4.401900e+06 0.0000 0.0000 False 0.000000 2 0.000000e+00 372 0.005392 168095756 0.000000e+00 6.469710e+08 0.000000e+00 0 0 0 467143855 0.000000
2178 1.583330e-02 1.765520e+08 15585.8000 179.3560 False 0.267278 2 2.795400e+06 619 0.216276 168095756 8.827860e-05 3.602800e+10 1.909260e-05 7120 5760 7510 467144167 0.024716
2179 1.154110e+00 4.682520e+08 727549.0000 742.7860 False 0.605537 3 5.404130e+08 163 0.573609 168095756 1.553760e-03 8.718470e+10 8.912480e-04 5260 5740 9830 467145572 1.153730
2180 1.325710e-04 3.845160e+07 80.0228 63.7014 False 0.012991 3 5.097560e+03 238 0.047103 168095756 2.081130e-06 1.434590e+09 9.802790e-08 5350 10220 5760 467145710 0.000127
2181 0.000000e+00 9.920160e+07 0.0000 0.0000 False 0.000000 2 0.000000e+00 906 0.121522 168095756 0.000000e+00 1.784200e+10 0.000000e+00 0 0 0 467144704 0.000000
2182 0.000000e+00 4.647600e+07 0.0000 0.0000 False 0.000000 2 0.000000e+00 910 0.056933 168095756 0.000000e+00 8.506240e+09 0.000000e+00 0 0 0 467144717 0.000000
2183 1.916020e-01 1.099130e+11 23671300.0000 889.6650 False 0.945823 3 2.105950e+10 315 134.643000 168095756 2.153640e-04 2.597050e+13 2.899730e-02 4600 5720 9490 467145854 37.537500
2184 9.070440e-04 1.281640e+09 6085.1200 191.0410 False 0.165236 1 1.162510e+06 541 1.570010 168095756 4.747910e-06 2.446410e+11 7.454270e-06 3070 9070 740 467143045 0.009650
2185 2.247380e-03 2.800810e+08 10952.8000 57.4692 False 0.050729 1 6.294490e+05 1000 0.343099 168095756 3.910580e-05 1.501010e+10 1.341720e-05 6830 12760 4060 467143339 0.017369
2186 0.000000e+00 1.836900e+06 0.0000 0.0000 False 0.000000 2 0.000000e+00 456 0.002250 168095756 0.000000e+00 3.480810e+08 0.000000e+00 0 0 0 467143967 0.000000
2187 1.765510e-01 7.494710e+08 489374.0000 270.3860 False 0.375780 3 1.323200e+08 153 0.918102 168095756 6.529580e-04 6.220530e+10 5.994830e-04 5200 11750 8050 467145548 0.776040
2188 0.000000e+00 3.268260e+07 0.0000 0.0000 False 0.000000 3 0.000000e+00 312782640 0.040036 168095756 0.000000e+00 8.488150e+09 0.000000e+00 0 0 0 467147591 0.000000
2189 2.852410e-01 1.494670e+08 181952.0000 234.3150 False 0.436456 2 4.263410e+07 558 0.183097 168095756 1.217340e-03 2.438990e+10 2.228910e-04 1840 5350 9440 467144076 0.288536
2190 3.876340e-02 4.267160e+09 273796.0000 604.1350 False 0.400506 2 1.654100e+08 993 5.227270 168095756 6.416350e-05 8.950760e+11 3.354000e-04 1900 2360 6660 467144918 0.434180
2191 1.002250e-01 3.999060e+07 21137.5000 189.6170 False 0.186188 3 4.008040e+06 557 0.048988 168095756 5.285620e-04 6.471600e+09 2.589350e-05 6210 7850 4450 467146315 0.033520
2192 0.000000e+00 5.333210e+08 0.0000 0.0000 False 0.000000 1 0.000000e+00 1020 0.653318 168095756 0.000000e+00 1.094550e+11 0.000000e+00 0 0 0 467143354 0.000000
2193 0.000000e+00 9.112500e+06 0.0000 0.0000 False 0.000000 2 0.000000e+00 653 0.011163 168095756 0.000000e+00 8.580830e+08 0.000000e+00 0 0 0 467144219 0.000000
2194 4.436300e-03 6.598530e+07 1100.4800 266.0010 False 0.052134 1 2.927300e+05 312782632 0.080832 168095756 1.667770e-05 1.065170e+10 1.348090e-06 2510 9470 1160 467143454 0.001745
2195 0.000000e+00 3.306320e+08 0.0000 0.0000 False 0.000000 1 0.000000e+00 945 0.405024 168095756 0.000000e+00 7.957860e+10 0.000000e+00 0 0 0 467143299 0.000000
2196 0.000000e+00 1.033130e+08 0.0000 0.0000 False 0.000000 2 0.000000e+00 234 0.126558 168095756 0.000000e+00 2.303430e+10 0.000000e+00 0 0 0 467143653 0.000000
2197 0.000000e+00 2.044590e+08 0.0000 0.0000 False 0.000000 2 0.000000e+00 182305697 0.250463 168095756 0.000000e+00 4.964210e+10 0.000000e+00 0 0 0 467145216 0.000000
2198 1.658070e-01 1.327680e+07 10887.5000 202.1940 False 0.186188 1 2.201390e+06 1 0.016264 168095756 8.200410e-04 1.907110e+09 1.333720e-05 6210 7850 4450 467142721 0.017265

2199 rows × 19 columns

In [6]:
from allensdk.api.queries.ontologies_api import OntologiesApi
oa = OntologiesApi()

atlases = tabular_dataframe(oa.get_atlases_table(brief=True))
mouse_p56_coronal_id = atlases[atlases.name == 'Mouse, P56, Coronal']['structure_graph'].iloc[0]['id'] # 1

# calculate a list of only the ancestors that are in the limited structure set
structure_set_df = tabular_dataframe(oa.get_structures(mouse_p56_coronal_id))

oa.unpack_structure_set_ancestors(structure_set_df)

structure_set_df[structure_set_df.acronym == 'PIR']
Out[6]:
st_level graph_id name weight acronym parent_structure_id graph_order sphinx_id hemisphere_id safe_name ... failed depth neuro_name_structure_id_path neuro_name_structure_id structure_name_facet failed_facet id ontology_id atlas_id structure_set_ancestor
377 NaN 1 Piriform area 8690 PIR 698 377 378 3 Piriform area ... False 6 None None 3867654445 734881840 961 1 261 [997, 8, 567, 688, 695, 698, 961]

1 rows × 22 columns

In [7]:
cutoff = 0.0050
data_field = 'projection_volume'

# projection joined to structures
table = pd.concat((
    pd.merge(projection_df[projection_df.hemisphere_id == h]
                          [projection_df.projection_volume > cutoff].loc[:,['hemisphere_id',
                                                                            'structure_id',
                                                                            data_field]],
             structure_set_df[['id','acronym','graph_order']],
             how='inner',
             left_on='structure_id', right_on='id')
    for h in [1, 2])).sort('graph_order').\
        reset_index('structure_id', 'hemisphere_id')
    
table[['acronym', 'hemisphere_id', 'projection_volume']]

table
/home/timf/anaconda/lib/python2.7/site-packages/pandas/core/frame.py:1825: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
  "DataFrame index.", UserWarning)
Out[7]:
hemisphere_id structure_id projection_volume id acronym graph_order
0 1 997 0.020088 997 root 0
1 2 997 0.062792 997 root 0
2 1 8 0.015301 8 grey 1
3 2 8 0.058538 8 grey 1
4 2 567 0.046989 567 CH 2
5 2 688 0.045993 688 CTX 3
6 2 695 0.043597 695 CTXpl 4
7 2 315 0.028033 315 Isocortex 5
8 2 95 0.015947 95 AI 260
9 2 111 0.009313 111 AIp 267
10 2 922 0.005093 922 PERI 328
11 2 698 0.011002 698 OLF 340
12 2 961 0.008747 961 PIR 377
13 1 343 0.010513 343 BS 588
14 2 343 0.011057 343 BS 588
In [8]:
display_table = table.loc[:,['acronym', 'hemisphere_id', 'projection_volume', 'graph_order']].pivot(index='graph_order',
                                                                                                    columns='hemisphere_id').iloc[:,1:]

display_table.columns = ['structure','L', 'R']
display_table
Out[8]:
structure L R
graph_order
0 root 0.020088 0.062792
1 grey 0.015301 0.058538
2 CH NaN 0.046989
3 CTX NaN 0.045993
4 CTXpl NaN 0.043597
5 Isocortex NaN 0.028033
260 AI NaN 0.015947
267 AIp NaN 0.009313
328 PERI NaN 0.005093
340 OLF NaN 0.011002
377 PIR NaN 0.008747
588 BS 0.010513 0.011057
In [ ]: