download_data.js |
|
---|---|
Copyright 2013 Allen Institute for Brain Science Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. |
|
Download the projection volume signal for several cortical data sets within a specific set of thalamic structures. |
var api_path = "http://api.brain-map.org/api/v2/";
var data_set_ids = [ 100141219, 112423392, 127084296, 127866392, 139426984, 146858006, 112424813 ];
var thalamus_structure_acronyms = [ 'VPL', 'VPM', 'PO', 'VAL', 'PF', 'VM', 'CM', 'RH', 'MD', 'PVT',
'RE', 'AM', 'AV', 'AD', 'LD', 'LP', 'LGv', 'LGd', 'MG' ]; |
Make the API query to download all of the structures in the mouse ontology. |
function download_structures(on_success) {
var url = api_path + "data/Structure/query.json?" +
"criteria=[graph_id$eq1]" +
"&num_rows=all";
$.ajax(url, {
success: function(response) {
var structures = response.msg.sort(function(a,b) { return a.graph_order - b.graph_order; });
on_success(structures);
},
error: function(response) {
api_error(response.statusText, url);
}
});
} |
Download all of the unionize values for a set of structures and data sets. |
function download_unionizes(structures, on_success) {
var structure_ids = structures.map(function(s) { return s.id; });
var url = api_path + "data/ProjectionStructureUnionize/query.json?" +
"criteria=[structure_id$in" + structure_ids.join(',') + "]" +
"[section_data_set_id$in" + data_set_ids.join(',') + "]" +
"&only=projection_volume,section_data_set_id,structure_id,hemisphere_id" +
"&num_rows=all";
$.ajax(url, {
success: function(response) {
on_success(response.msg);
},
error: function(response) {
api_error(response.statusText, url);
}
});
} |
Download the meta data for the data sets hard-coded at the top of this file. |
function download_data_sets(on_success) {
var url = api_path + "data/SectionDataSet/query.json?" +
"criteria=[id$in" + data_set_ids.join(',') + "]" +
"&include=specimen(injections)";
$.ajax(url, {
success: function(response) {
var data_sets = response.msg; |
For each data set, identify its primary injection structure. A data set can have more that one injection. The primary injection structure id is listed in every injection. |
for (var i = 0; i < data_sets.length; i++) {
var ds = data_sets[i];
var injections = ds.specimen.injections;
ds.primary_injection_structure_id = injections[0].primary_injection_structure_id;
}
on_success(data_sets);
},
error: function(response) {
api_error(response.statusText, url);
}
});
} |
Download all of the meta data to make the heatmap. |
function download_data(on_success) {
download_data_sets(function(data_sets) {
download_structures(function(structures) {
var thalamus_structures = structures.filter(function(s) {
return thalamus_structure_acronyms.indexOf(s.acronym) >= 0;
});
download_unionizes(thalamus_structures, function(unionizes) {
on_success(process_data(data_sets, structures, thalamus_structures, unionizes));
});
});
});
} |
Preprocess the data to make it amenable for the heatmap view. |
function process_data(data_sets, structures, thalamus_structures, unionizes) { |
Make a hash from structure_id to structure object. |
var structure_hash = {};
for (var i = 0; i < structures.length; i++) {
var s = structures[i];
structure_hash[s.id] = s;
} |
Sort data sets by primary injection structure graph order |
data_sets = data_sets.sort(function(a,b) {
return structure_hash[a.primary_injection_structure_id].graph_order -
structure_hash[b.primary_injection_structure_id].graph_order;
}); |
Index the data sets by structure, and get their display order. |
var data_set_structures = {};
var data_set_order = {};
for (var i = 0; i < data_sets.length; i++) {
var ds = data_sets[i];
data_set_structures[ds.id] = structure_hash[ds.primary_injection_structure_id];
data_set_order[ds.id] = i;
}
|
Get the display order of the target structures. |
var thalamus_order = {}
for (var i = 0; i < thalamus_structures.length; i++) {
thalamus_order[thalamus_structures[i].id] = i;
} |
Split the unionized volume values up by hemisphere. |
var left_data = [];
var right_data = [];
var both_data = [];
var LEFT = 1, RIGHT = 2, BOTH = 3;
for (var i = 0; i < unionizes.length; i++) {
var u = unionizes[i];
var data = null;
if (u.hemisphere_id == LEFT) {
data = left_data;
} else if (u.hemisphere_id == RIGHT) {
data = right_data;
} else {
data = both_data;
}
data.push({
source_structure: data_set_structures[u.section_data_set_id],
source_index: data_set_order[u.section_data_set_id],
target_structure: structure_hash[u.structure_id],
target_index: thalamus_order[u.structure_id],
value: u.projection_volume
});
}
return {
source_structures: data_sets.map(function(ds) { return structure_hash[ds.primary_injection_structure_id]; }),
target_structures: thalamus_structures,
left_data: left_data,
right_data: right_data,
both_data: both_data
};
} |
If something goes wrong, alert the user. |
function api_error(response, url) {
var error_html =
"<p>There was an error with the following query:</p>" +
"<p>" + url + "</p>" +
"<p>Error message:</p>" +
"<p>" + response + "</p>";
var dialog = $( "#errorDialog" );
var existing_errors = dialog.html();
$( "#errorDialog" )
.html(existing_errors + error_html)
.dialog({
width: 500,
height: 200,
modal: true
});
} |