Skip to content

Commit

Permalink
AIDAmri Version 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lscharwaechter committed Sep 28, 2020
1 parent 911c3ff commit a088e60
Show file tree
Hide file tree
Showing 90 changed files with 12,691 additions and 0 deletions.
Binary file modified .DS_Store
Binary file not shown.
61 changes: 61 additions & 0 deletions ARA/AllenBrainAPI-master/DownloadImageSeries.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
function DownloadImageSeries(outdir, expid, varargin)
% download Allen sample brain using the Allen API
%
% function DownloadImageSeries(outdir, expid, varargin)
%
%
% Inputs [required]
% outdir - where to put the JPEGs
% expid - [numerical scalar] experiment ID assigned by Allen
%
% Inputs [optional]
% 'downsample' sets how many times the image will be downsampled and scaled down,
% e.g. downsample=3 means the image will be 1/2^3 = 1/8 of original size.
% 'range' specifies the range of 16 bit RGB values that will be mapped onto 8 bit
%
%
% Example
% - Pull in a nicely downsampled version of experiment ID 479701339 into the current directory:
% >> DownloadImageSeries(pwd,479701339,'downsample',4)
%
%
% PZ


params = inputParser;
params.addParamValue('range', '0,2500,0,2500,0,4095', @ischar);
params.addParamValue('downsample', 2, @isnumeric);
params.parse(varargin{:});

% download XML data for experiment
exp_url = ['http://api.brain-map.org/api/v2/data/SectionDataSet/' ...
num2str(expid) '.xml?include=section_images'];
disp(['Accessing ',exp_url]);
doc = xmlread(exp_url);

% select the image IDs for all images
list = doc.getElementsByTagName('section-image');

nImages = list.getLength;

% cycle through images, zero-indexed
for ind = 0:nImages-1
thisImage = list.item(ind);
imageid = thisImage.getElementsByTagName('id');
% number of the brain section (they aren't in order for some reason)
sectionnum = thisImage.getElementsByTagName('section-number');

query = ['http://api.brain-map.org/api/v2/section_image_download/' ...
char(imageid.item(0).getFirstChild.getData) ...
'?range=' params.Results.range...
'&downsample=' num2str(params.Results.downsample)];
disp([ num2str(ind+1) ' of ' num2str(nImages) ': ' query]);

try
urlwrite(query,...
[ outdir filesep ...
char(sectionnum.item(0).getFirstChild.getData) '.jpg']);
catch
disp(['Error getting image from ',query]);
end
end
30 changes: 30 additions & 0 deletions ARA/AllenBrainAPI-master/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# README #

Tools for accessing the Allen Atlas API from MATLAB.
e.g. downloading images, projection data, searching for particular experiments, etc.
Functionality isn't complete right now and is a bit rough in places, but everything works and the functions should provide a good template for showing you how to add functionality.
For more details see the [Allen API documentation](http://help.brain-map.org/display/mouseconnectivity/API).
You will need to install [JSONlab](http://ch.mathworks.com/matlabcentral/fileexchange/33381-jsonlab--a-toolbox-to-encode-decode-json-files-in-matlab-octave) for some functions to work.



### Examples ###

Get structure list that underlies the Allen Reference Atlas:
```
>> S=getAllenStructureList;
```


Keep only the areas that are children of `Cerebellum`:
```
>> S=getAllenStructureList('childrenOf','Cerebellum');
```

### Also see ###
There are other useful functions too. See:

* DownloadImageSeries.m
* findAllenExperiments.m
* getInjectionIDfromExperiment.m
* getProjectionDataFromExperiment.m
71 changes: 71 additions & 0 deletions ARA/AllenBrainAPI-master/acronym2structureID.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
function [IDs,ARA_LIST]=acronym2structureID(acronyms,ARA_LIST,quiet)
% Convert a list of ARA (Allen Reference Atlas) acronyms to a vector of structure IDs
%
% function [IDs,ARA_LIST]=acronym2structureID(acronyms,ARA_LIST,quiet)
%
% Purpose
% Each Allen Reference Atlas (ARA) brain area is associated with a unique
% number (structure ID), a long name and an acronym. This function converts
% the acronym to an area structure ID number.
%
%
% Inputs
% acronyms - a cell array (if a list) of brain area acronyms or a single string.
% ARA_LIST - [optional] the first output of getAllenStructureList
% quiet - [optional, false by default] if true, do not print any warning messages
%
%
% Outputs
% IDs - a vector of brain area structure IDs (if more than one acronym was provided)
% ARA_LIST - the CSV data from getAllenStructureList in the form of a cell array
%
%
% Examples
%
% >> acronym2structureID({'VISp','VISal'})
%
% ans =
%
% 1x2 int32 row vector
%
% 385 402
%
%
%
% Rob Campbell
%
% See also:
% getAllenStructureList, structureID2name


if isstr(acronyms)
acronyms={acronyms};
end

if nargin<2 || isempty(ARA_LIST)
ARA_LIST = getAllenStructureList;
end

if nargin<3
quiet = false;
end

%loop through and find all the acronyms
for ii=1:length(acronyms)

f=strmatch(lower(acronyms{ii}),lower(ARA_LIST.acronym),'exact');
if isempty(f)
if ~quiet
fprintf('%s finds no acronym %s in the atlas\n',mfilename, acronyms{ii})
end
continue
end
if length(f)>1
if ~quiet
error('found more than one ID index')
end
end
IDs(ii) = ARA_LIST.id(f);
end


77 changes: 77 additions & 0 deletions ARA/AllenBrainAPI-master/examples/thalamus.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
function thalamus
% make a projection density plot
%
% This is the MATLAB version of the R example found at
% http://api.brain-map.org/examples/doc/thalamus/thalamus.R.html
%
% We have some more robust MATLAB functions that encapsulate
% some operations that were performed in-line in the R example.
% These standalone functions are used where appropriate here.
%
%
% Rob Campbell - Basel 2015


structures = getAllenStructureList; %read structure data as a table

data_sets = url2table('http://api.brain-map.org/api/v2/data/query.csv?criteria=model::SectionDataSet,rma::criteria,[id$in100141219,112423392,127084296,127866392,139426984,146858006,112424813],specimen%28injections%29,rma::options[tabular$eq%27distinct%20data_sets.id%20as%20section_data_set_id,injections.primary_injection_structure_id%27]');

%Here we use arrayfun instead of the R sapply function
data_sets.graph_order = arrayfun(@(x) structures.graph_order(structures.id == x), data_sets.primary_injection_structure_id);
data_sets.acronym = arrayfun(@(x) structures.acronym(structures.id == x), data_sets.primary_injection_structure_id);

data_sets = sortrows(data_sets,'graph_order');


unionizes = url2table('http://api.brain-map.org/api/v2/data/query.csv?criteria=model::ProjectionStructureUnionize,rma::criteria,section_data_set[id$in100141219,112423392,127084296,127866392,139426984,146858006,112424813],structure[acronym$in''VPL'',''VPM'',''PO'',''VAL'',''PF'',''VM'',''CM'',''RH'',''MD'',''PVT'',''RE'',''AM'',''AV'',''AD'',''LD'',''LP'',''LGv'',''LGd'',''MG''],rma::include,structure,rma::options[num_rows$eqall]');



%This is much more long-winded than the R way:
%m = xtabs(projection_volume ~ section_data_set_id + structure_id, unionizes)
setID = unique(unionizes.section_data_set_id);
strID = unique(unionizes.structure_id);
for ii=1:length(setID)
for jj=1:length(strID)
f=find(unionizes.section_data_set_id == setID(ii) & unionizes.structure_id == strID(jj));
m(ii,jj) = sum(unionizes.projection_volume(f));
end
end


%We now get the row and column names rather than use the ID numbers
row_acronyms = arrayfun(@(x) data_sets.acronym(data_sets.section_data_set_id == x), setID);
col_acronyms = arrayfun(@(x) structures.acronym(structures.id == x), strID);



%Sort the rows and columns by sorted graph_order
[~,row_order] = sort(arrayfun(@(x) data_sets.graph_order(data_sets.section_data_set_id == x), setID));
[~,col_order] = sort(arrayfun(@(x) structures.graph_order(structures.id == x), strID));

om = m(row_order,col_order);
row_acronyms = row_acronyms(row_order);
col_acronyms = col_acronyms(col_order);


%Make the plot
clf
imagesc(om)
set(gca,'YTick',1:size(om,1), 'YTickLabel',row_acronyms, 'XTick',1:size(om,2), 'XTickLabel',col_acronyms)
colormap hot
axis equal tight

xlabel('target structure')
ylabel('primary injection structure')
title('Cortico-thalamic projection')




%----------------------------------------------
function thisTable=url2table(url)
%Read a URL into a table
thalamusTMP=fullfile(tempdir,'thalamus.csv');
urlwrite(url,thalamusTMP);
thisTable=readtable(thalamusTMP);
delete(thalamusTMP)
102 changes: 102 additions & 0 deletions ARA/AllenBrainAPI-master/findAllenExperiments.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
function varargout = findAllenExperiments(varargin)
% find all Allen experiments defined parameters
%
% function [IDs,json]=findAllenExperiments('param1','val1','param2','val2',...)
%
%
% Inputs
% 'injection' - search for experiments with injections in this location. not searched for by default.
% this is case senstive. So 'VISp' searches for V1 but 'visp' produces an error.
% 'line' - search for experiments on this transgenic line. not searched for by default. use '0' for wild-type,
% 'primary' - true/false. true by default, search for injections where 'injection' was the primary
% injection structure. if false it it will search for cases where 'injection' was not the primary
% structure.
%
%
% Outputs
% IDs - a vector of numbers corresponding to experiment IDs
% json - cell array of structures containing all data pulled out of the JSON returned by the Allen API.
%
%
% Examples
% a) Return all experiments on wild type animals:
% findAllenExperiments('line','0');
%
% b) Return all experiments with injections in V1
% findAllenExperiments('injection','VISp');
% or:
% findAllenExperiments('injection','385');
%
% c) Return all experiments with injections in cortex
% findAllenExperiments('injection','Isocortex');
%
% Rob Campbell - Basel 2015
%
% requires JSONlab from the FEX
%
% See Also:
% getInjectionIDfromExperiment


if ~exist('loadjson')
disp('Please install JSONlab from the FEX')
return
end

%Handle input arguments
params = inputParser;
params.CaseSensitive = false;
params.addParamValue('injection','',@ischar)
params.addParamValue('line','',@ischar)
params.addParamValue('primary',true,@islogical)

params.parse(varargin{:});


%Build the URL
%this is the base URL
url = 'http://api.brain-map.org/api/v2/data/query.json?criteria=service::mouse_connectivity_injection_structure';

%now we extend it according to what is being searched
if ~isempty(params.Results.injection)
url = [url,'[injection_structures$eq',params.Results.injection,']'];
end

if ~isempty(params.Results.line)
url = [url,'[transgenic_lines$eq',params.Results.line,']'];
end

if params.Results.primary
primary='true';
else
primary='false';
end
url = [url,'[primary_structure_only$eq',primary,']'];



%Get data from Allen
page=urlread(url);
result=loadjson(page);
if ~result.success
fprintf('Query failed!\n%s\nAt URL: %s\n\n',result.msg,url);
end


%Return data
IDs = ones(1,length(result.msg));
for ii=1:length(IDs)
IDs(ii) = result.msg{ii}.id;
end
fprintf('Found %d experiments\n',length(IDs))



if nargout>0
varargout{1}=IDs;
end
if nargout>1
varargout{2}=result.msg;
end


Loading

0 comments on commit a088e60

Please sign in to comment.