thalamus.R

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.

This script downloads projection volume values for seven cortical injections in a set of thalamic structures and builds a heatmap of those values.

First download the list of adult mouse structures from the API. The adult mouse structure graph has an id of 1.

structures = read.csv("http://api.brain-map.org/api/v2/data/Structure/query.csv?criteria=[graph_id$eq1]&num_rows=all")

Make a second API query that downloads the structure id of the primary injection structure of each experiment. This uses a tabular query to select a few columns of interest for the results. The first criteria chooses seven experiments by their ids. The specimen and injection models are included in the criteria so that the subsequent tabular query can access their columns. Each experiment can have multiple injections but only one primary injection structure, so the ‘distinct’ clause limits the results to one data set and primary injection structure per row.

data_set_url = "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]"
data_sets = read.csv(data_set_url)

Append a few more columns to the data_sets data frame using information from the structure list. The sapply method is a map-like routine that converts an array of values with the results of a function called on each array element. In these lines, I find the structure matching the data set’s primary injection structure and return a specific column from that structure.

data_sets$graph_order = sapply(data_sets$primary_injection_structure_id, function(x) structures[structures$id == x,]$graph_order)
data_sets$color = sapply(data_sets$primary_injection_structure_id, function(x) paste("#",structures[structures$id == x,]$color_hex_triplet,sep=""))
data_sets$acronym = sapply(data_sets$primary_injection_structure_id, function(x) structures[structures$id == x,]$acronym)

Reorder the data sets by the graph order of the primary injeciton structure.

data_sets = data_sets[order(data_sets$graph_order),]

Download the unionized projection volume values for a set of experiments restricted to a set of structures (by acronym).

data_url="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]"
unionizes = read.csv(data_url)

This method converts the unionize rows into a matrix where the rows have unique section data set ids, the columns have unique structure ids, and cells store projection_volume values.

m = xtabs(projection_volume ~ section_data_set_id + structure_id, unionizes)

Get a list of row and column labels. The row and column names of the matrix are data set ids and target structure ids (respectively). Use these to index into the structure array and retrieve acronyms.

row_acronyms = sapply(rownames(m), function(x) data_sets[data_sets$section_data_set_id == x,]$acronym)
col_acronyms = sapply(colnames(m), function(x) structures[structures$id == x,]$acronym)

Get the graph order for the row and column ids and use them to reorder the matrix.

row_order = rev(order(sapply(rownames(m), function(x) data_sets[data_sets$section_data_set_id == x,]$graph_order)))
col_order = order(sapply(colnames(m), function(x) structures[structures$id == x,]$graph_order))
om = m[row_order,col_order]

Generate the heatmap.

hot = colorRampPalette(c("black","red","yellow","white"))(256)
heatmap(om, Rowv = NA, Colv = NA, labRow=row_acronyms[row_order], labCol=col_acronyms[col_order], main="Cortico-thalamic Projection", xlab="Target Structure", ylab="Primary Injection Structure", col=hot, scale="none")