Allen Brain Atlas API/SDK tutorial
This is a neuro 'informatics' tutorial we ran at the 2015 graduate research orientation.
See more info at brain-map website and AllenBrainAtlas on github.
1. Test Allen Brain Institute mouse connectivity web interface
http://connectivity.brain-map.org/
Search for Secondary Visual source injections sites:

- Look for several injections across VISl or VISal
- Make note of the experiment id no. for each here:
- id1 = 263780729
- id2 =100141796
- id3 =167794131
- id4 =
- ...
- Make note of the experiment id no. for each here:

2. Render and explore datasets in 3D
Open each of the selected datasets in Brain Explorer (click the 3D button from the web interface).

- Render the injections in different colors in Brain Explorer desktop software
- Create snapshots (shift-cmd-4), drag onto the
mv2imgFolder.app
function on the Desktop, and paste into this markdown text file:

3. Use allensdk to download and plot multiple experiments
First open the command line terminal (cmd-spacebar, search for terminal).
Type in the following:
cd ~/Desktop
mkdir 20150915_visual_connect
cd 20150915_visual_connect
ipython --pylab
Download selected experiment data
#enter selected projection numbers here, e.g. 167794131, 297231636, 287495026
id1 = 167794131
id2 = 297231636
id3 = 272736450
# import allensdk python api
from allensdk.api.queries.mouse_connectivity_api import MouseConnectivityApi
mca = MouseConnectivityApi()
# get metadata for all experiments
experiments = mca.experiment_source_search(injection_structures=['VIS','PTLp','RSP'])
# find selected experiments and format filenames, **Use the %paste magic function if pasting into ipython
for i in range(len(experiments)):
if (experiments[i]['id'] == id1):
fn1 = str(experiments[i]['injection-structures'][0]['abbreviation']) + '_' + str(experiments[i]['id']) + '_' + str(experiments[i]['transgenic-line'])
if (experiments[i]['id'] == id2):
fn2 = str(experiments[i]['injection-structures'][0]['abbreviation']) + '_' + str(experiments[i]['id']) + '_' + str(experiments[i]['transgenic-line'])
if (experiments[i]['id'] == id3):
fn3 = str(experiments[i]['injection-structures'][0]['abbreviation']) + '_' + str(experiments[i]['id']) + '_' + str(experiments[i]['transgenic-line'])
# download selected experiment projection density files at 25 µm resolution
mca.download_projection_density(fn1 + '.nrrd', id1, resolution=25)
mca.download_projection_density(fn2 + '.nrrd', id2, resolution=25)
mca.download_projection_density(fn3 + '.nrrd', id3, resolution=25)
Plot projection density images
import datetime, nrrd
A1, metadata1 = nrrd.read(fn1 + '.nrrd')
A1.shape
imshow(A1[300,:,:])
mxProj1 = amax(A1,0) #second input is the dimension you want to project over
imshow(mxProj1)
Make multicolor projection images
filenames = [fn1, fn2, fn3]
A1, metadata1 = nrrd.read(filenames[0] + '.nrrd')
A2, metadata2 = nrrd.read(filenames[1] + '.nrrd')
A3, metadata2 = nrrd.read(filenames[2] + '.nrrd')
def rgbMaxProj(A1,A2,A3,dim):
mxProj1 = amax(A1,dim)
mxProj2 = amax(A2,dim)
mxProj3 = amax(A3,dim)
rgbA = zeros((mxProj1.shape[0],mxProj1.shape[1],3))
rgbA[...,0] += mxProj1
rgbA[...,1] += mxProj2
rgbA[...,2] += mxProj3
return rgbA
figure(dpi=300)
subplot(131)
rgbA = rgbMaxProj(A1,A2,A3,0)
ax1=imshow(rgbA,interpolation=None)
subplot(132)
rgbA = rgbMaxProj(A1,A2,A3,1)
ax2=imshow(rgbA,interpolation=None)
subplot(133)
rgbA = rgbMaxProj(A1,A2,A3,2)
ax3=imshow(rgbA,interpolation=None)
title('r:' + filenames[0] + ', g:' + filenames[1] + ', b:' + filenames[2])
dateStr = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
rcParams['pdf.fonttype'] = 42
savefig(dateStr + '.png')
savefig(dateStr + '.pdf',dpi=300)

Download and automatically save multiple plots for a bunch of experiments
Look at VISam, PTLp, RSP projections
from allensdk.api.queries.mouse_connectivity_api import MouseConnectivityApi
mca = MouseConnectivityApi()
# get metadata for all experiments
experiments = mca.experiment_source_search(injection_structures=['PTLp','VISam','RSP'])
# download the projection density volume for one of the experiments. This will also download and save the file in the local folder as well as storing it as a variable.
#pd = mca.download_projection_density('example.nrrd', experiments[0]['id'], resolution=25)
for i in range(len(experiments)):
filename = str(experiments[i]['injection-structures'][0]['abbreviation']) + '_' + str(experiments[i]['id']) + '_' + str(experiments[i]['transgenic-line'])
print filename
mca.download_projection_density(filename + '.nrrd', experiments[i]['id'], resolution=25)
Save the printed filenames as a text file 2015-09-15_experiments.txt
with one filename per line.
fn = "2015-09-15_experiments.txt"
with open(fn) as f:
data = f.read().splitlines()
def print3chan(fn1,fn2,fn3):
A1, metadata1 = nrrd.read(fn1 + '.nrrd')
A2, metadata2 = nrrd.read(fn2 + '.nrrd')
A3, metadata2 = nrrd.read(fn3 + '.nrrd')
#plot images as multiplot
fig=figure(1,dpi=300)
subplot(131)
rgbA = rgbMaxProj(A1,A2,A3,0)
ax1=imshow(rgbA,interpolation=None)
subplot(132)
rgbA = rgbMaxProj(A1,A2,A3,1)
ax2=imshow(rgbA,interpolation=None)
title('r:' + fn1 + '\n g:' + fn2 + '\n b:' + fn3)
subplot(133)
rgbA = rgbMaxProj(A1,A2,A3,2)
ax3=imshow(rgbA,interpolation=None)
#save fig
dateStr = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
rcParams['pdf.fonttype'] = 42
savefig(dateStr + '.png')
savefig(dateStr + '.pdf',dpi=300)
close(fig)
def rgbMaxProj(A1,A2,A3,dim):
mxProj1 = amax(A1,dim)
mxProj2 = amax(A2,dim)
mxProj3 = amax(A3,dim)
rgbA = zeros((mxProj1.shape[0],mxProj1.shape[1],3))
rgbA[...,0] += mxProj1
rgbA[...,1] += mxProj2
rgbA[...,2] += mxProj3
return rgbA
for i in arange(len(data),step=3):
j = i + 1
k = i + 2
if (abs(i - arange(len(data))[-1]) < 2):
k = 0
if (abs(i - arange(len(data))[-1]) < 1):
j = 0
k = 1
print3chan(data[i],data[j],data[k])