MERFISH whole brain spatial transcriptomics (part 2b)#

We can continue to explore our examples looking at the expression of canonical neurotransmitter transporter genes and gene Tac2 over the whole brain.

You need to be connected to the internet to run this notebook and that you have downloaded the example data via the getting started notebook.

import os
import pandas as pd
import numpy as np
import anndata
import time
import json
import matplotlib.pyplot as plt
import requests

%matplotlib inline

The prerequisite for running this notebook is that the data have been downloaded to local directory maintaining the organization from the manifest.json. Change the download_base variable to where you have downloaded the data in your system.

version = '20231215'
download_base = '../../abc_download_root'

use_local_cache = False
manifest_path = 'releases/%s/manifest.json' % version

if not use_local_cache :
    url = 'https://allen-brain-cell-atlas.s3-us-west-2.amazonaws.com/' + manifest_path
    manifest = json.loads(requests.get(url).text)
else :
    file = os.path.join(download_base,manifest_path)
    with open(file,'rb') as f:
        manifest = json.load(f)
    
metadata = manifest['file_listing']['MERFISH-C57BL6J-638850']['metadata']
view_directory = os.path.join( download_base, 
                               manifest['directory_listing']['MERFISH-C57BL6J-638850']['directories']['metadata']['relative_path'], 
                              'views')
cache_views = False
if cache_views :
    os.makedirs( view_directory, exist_ok=True )

Read in the expanded cell metadata table we created in part 1.

rpath = metadata['cell_metadata_with_cluster_annotation']['files']['csv']['relative_path']
file = os.path.join( download_base, rpath )
cell = pd.read_csv(file,dtype={"cell_label":str,"neurotransmitter":str})
cell.set_index('cell_label',inplace=True)
cell.columns
Index(['brain_section_label', 'cluster_alias', 'average_correlation_score',
       'feature_matrix_label', 'donor_label', 'donor_genotype', 'donor_sex',
       'x', 'y', 'z', 'neurotransmitter', 'class', 'subclass', 'supertype',
       'cluster', 'neurotransmitter_color', 'class_color', 'subclass_color',
       'supertype_color', 'cluster_color'],
      dtype='object')

Read in the gene expression dataframe we created in part 2a.

rpath = metadata['example_genes_all_cells_expression']['files']['csv']['relative_path']
file = os.path.join( download_base, rpath )
exp = pd.read_csv(file,dtype={"cell_label":str})
exp.set_index('cell_label',inplace=True)

We define a helper functions aggregate_by_metadata to compute the average expression for a given catergory.

def aggregate_by_metadata( df, gnames, value, sort=False ) :
    grouped = df.groupby(value)[gnames].mean()
    if sort :
        grouped = grouped.sort_values(by=gnames[0],ascending=False)
    return grouped

Expression of canonical neurotransmitter transporter genes#

During analysis, clusters were assigned neurotransmitter identities based on the expression of of canonical neurotransmitter transporter genes. In this example, we create a dataframe comprising of expression of the 9 solute carrier family genes for all the cells in the dataset. We then group the cells by the assigned neurotransmitter class and compute the mean expression for each group and visualized as a colorized table.

The results are similar that in part 1. Using data from the whole brain, gene Slc17a7 is now most enriched in glutamatergic assigned cells. Gene Slc17a6 is most enriched in noradrenergic, then cholinergic types. Genes Slc6a5, Slc6a3 and Slc6a4 shows high specificity to glycinergic, dopaminergic, serotonergic respectively.

def plot_heatmap( df, fig_width = 8, fig_height = 4, cmap=plt.cm.magma_r, vmin=0, vmax=5) :

    arr = df.to_numpy()

    fig, ax = plt.subplots()
    fig.set_size_inches(fig_width,fig_height)

    res = ax.imshow(arr, cmap = cmap, aspect='auto', vmin=vmin, vmax=vmax)
    xlabs = df.columns.values
    ylabs = df.index.values

    ax.set_xticks(range(len(xlabs)))
    ax.set_xticklabels( xlabs )

    ax.set_yticks(range(len(ylabs)))
    res = ax.set_yticklabels( ylabs )
ntgenes = ['Slc17a7','Slc17a6','Slc17a8','Slc32a1','Slc6a5','Slc6a3','Slc6a4']
filtered = exp[ntgenes]
joined = cell.join( filtered )
agg = aggregate_by_metadata( joined, ntgenes, 'neurotransmitter' )
agg = agg[ntgenes]
plot_heatmap(agg,8,3,vmax=4)
../_images/e118b4f4bce1f681339245d5081e3f74da012d10aa2343d848b18f0d2c913d63.png

Grouping expression by dissection region of interest shows that each of these genes have distinct spatial patterns. The MERFISH data allows us to visualize these patterns in anatomical context.

agg = aggregate_by_metadata( joined, ntgenes, 'brain_section_label' )
agg = agg.loc[list(reversed(list(agg.index)))]
plot_heatmap(agg,8,11,vmax=3)
../_images/4b22bc0f45f33d428f294f9f9790263b02b1cf3a3926f595c8b733c389b2b4f5.png

We define a small helper function plot sections to visualize the cells for a specified set of brain sections either by colorized metadata or gene expression.

def plot_sections( df, feature, blist, cmap=None, fig_width = 20, fig_height = 5) :
    
    fig, ax = plt.subplots(1,len(blist))
    fig.set_size_inches(fig_width, fig_height)
    
    for idx,bsl in enumerate(blist) :
        
        filtered = df[df['brain_section_label'] == bsl]
        xx = filtered['x']
        yy = filtered['y']
        vv = filtered[feature]
        
        if cmap is not None :
            ax[idx].scatter(xx,yy,s=1.0,c=vv,marker='.',cmap=cmap)
        else :
            ax[idx].scatter(xx,yy,s=1.0,color=vv,marker=".")
            
        ax[idx].axis('equal')
        ax[idx].set_xlim(0,11)
        ax[idx].set_ylim(11,0)
        ax[idx].set_xticks([])
        ax[idx].set_yticks([])
        
        ax[idx].set_title("%s" % (bsl) )
        
    plt.subplots_adjust(wspace=0.01, hspace=0.01)
    return fig, ax
    

We will use the aggregate by brain section table above to pick a four sections of interest and plot cells in those sections by neurotransmitter type and by each of the transporter genes.

blist = ['C57BL6J-638850.51','C57BL6J-638850.31','C57BL6J-638850.19','C57BL6J-638850.01']
fig, ax = plot_sections( joined, 'neurotransmitter_color', blist, cmap=None)
../_images/c80593654cad3bbdec3e3058fd45500cb83d7ed034b04dc6009f51a9fd987266.png
fig, ax = plot_sections( joined, 'Slc17a7', blist, cmap=plt.cm.magma_r)
../_images/f29f2e9fdb7a243c158f86ac3c7de6cdd7f4fa5df1e2681b67e87b368f984996.png
fig, ax = plot_sections( joined, 'Slc17a6', blist, cmap=plt.cm.magma_r)
../_images/2d3e66df1a592933c796ed10204b428b935979e6916f4992274e04faabfc67a0.png
fig, ax = plot_sections( joined, 'Slc17a8', blist, cmap=plt.cm.magma_r)
../_images/455b177f0a8c4b11686a9954f2093755399c452e3b848e0b798dbef1530b0188.png
fig, ax = plot_sections( joined, 'Slc32a1', blist, cmap=plt.cm.magma_r)
../_images/1bd168a996e045ec5e715c830de551cdea623b46391b2a7111e91b540e4a715a.png
fig, ax = plot_sections( joined, 'Slc6a5', blist, cmap=plt.cm.magma_r)
../_images/bac780f0d65edb6fbaf0241e11a349182e0461d009fbb0eec1e809bdc3fd7393.png
fig, ax = plot_sections( joined, 'Slc6a3', blist, cmap=plt.cm.magma_r)
../_images/1b3868338b222197fa689517cb61a4d1212c94a396d9fd5c7b4ed58ea0bcee4a.png
fig, ax = plot_sections( joined, 'Slc6a4', blist, cmap=plt.cm.magma_r)
../_images/b779b3a039fc3bc3227edf3a189dac28f23669464c1a6cde8bf91c8eaf6ddec9.png

Expression of Tachykinin 2 (Tac2) in the whole brain#

In mice, the tachykinin 2 (Tac2) gene encodes neuropeptide called neurokinin B (NkB). Tac2 is produced by neurons in specific regions of the brain know to be invovled in emotion and social behavior. Based on ISH data from the Allen Mouse Brain Atlas, Tac 2 is sparsely expressed in the mouse isocortex and densely enriched is specific subcortical regions such the medial habenula (MH), the amygdala and hypothalamus.

In this example, we create a dataframe comprising expression values of Tac2 for all cells across the whole brain. As with the single brain section example, grouping expression by neurotransmitter show that Tac2 gene is enriched in cholinergic cell types. With the rest of brain included, we can observe that Tac2 is also enriched in Glut-GABA cell types as well.

exgenes = ['Tac2']
filtered = exp[exgenes]
joined = cell.join( filtered )
agg = aggregate_by_metadata( joined, exgenes, 'neurotransmitter', True )
plot_heatmap(agg,1,3)
../_images/799372d89fcb4773e1511ffa1ec26ea6fad342beb7d5c1a47f24d2ca0039e382.png

Grouping by class, shows that Tac2 is enriched in class “16 MH-LH Glut” with cells restricted to the medial (MH) and lateral (LH) habenula and a mixture of glutamatergic and cholinergic type and “06 CTX-CGE GABA” GABAergic cells originating from the caudal ganglionic eminence (CGE).

agg = aggregate_by_metadata( joined, exgenes, 'class', True ).head(8)
class_list = agg.index[0:2]
plot_heatmap(agg,1,3)
../_images/8138e0940acf077d66ef8c09f811ec93c10b7e8fa22bf49f4e605338aee2d6b5.png

At the next level, grouping by subclass reveals enrichment is highly anatomically localized cell types such as the medial habenula (MH), bed nuclei of the stria terminalis (BST), spinal nucleus of the trigeminal (SPVC), main olfactory blub (MOB), central amygdalar nucleus (CEA) and arcuate hypothalamic nucleus (ARH).

agg = aggregate_by_metadata( joined, exgenes, 'subclass', True ).head(15)
subclass_list = agg.index[0:10]
plot_heatmap(agg,1,3)
../_images/e00ceb4bd121567efeddbded3afe88f81533da2f75fc08c2afa9c3175f4a6c23.png

The MERFISH data allows us to visualize these spatial pattern in anatomical context. We can aggregate Tac2 expression by brain section so that we can find 4 sections where the enriched expression is located. We then visualize cells in those section by Tac2 expression, neurotransmitter identity, cell type classes and subclasses.

agg = aggregate_by_metadata( joined, exgenes, 'brain_section_label', False )
agg = agg.loc[list(reversed(list(agg.index)))]
plot_heatmap(agg,1,11,vmax=0.4)
../_images/a41d0fc9de451549ecd9d33ecf5ded349f58e0dea9f5f22492503b19804d71dc.png
blist = ['C57BL6J-638850.69','C57BL6J-638850.46','C57BL6J-638850.38','C57BL6J-638850.01']
fig, ax = plot_sections( joined, 'Tac2', blist, cmap=plt.cm.magma_r)
../_images/6cb9be2365a2f218ad4601110cc73941179adde9b2b5d8ecc13c71328fa8966a.png
fig, ax = plot_sections( joined, 'neurotransmitter_color', blist, cmap=None)
../_images/1320b54e71bee22aa01cc54dbe87acc255d72f2362d69e9e99549dfb84c4a3d5.png
fig, ax = plot_sections( joined, 'class_color', blist, cmap=None)
../_images/c84954c03a051b9e7a6b230f922d08f656d78c3fdc7b596b5916218753fab7fa.png
fig, ax = plot_sections( joined, 'subclass_color', blist, cmap=None)
../_images/22eecaca6127409da0f21653184a51844bd711d8297b4f7c2b7f17ffab77c292.png

We can use the Tac2 aggregate by subclass table above and pick out the top 10 most enriched subclasses and plot only them on the same set of brain sections and observed that this set of subclasses is able recapitulate the expression pattern of Tac2.

pred = [x in subclass_list for x in joined['subclass']]
filtered = joined[pred]
fig, ax = plot_sections( filtered, 'subclass_color', blist, cmap=None)
../_images/8785604f79a5ab8ab757719c2033bd44ca6197faf2c6a83c5de6ecf5b33e1648.png
subclass_list
Index(['088 BST Tac2 Gaba', '145 MH Tac2 Glut',
       '083 CEA-BST Rai14 Pdyn Crh Gaba', '258 SPVC Nmu Glut',
       '043 OB-mi Frmd7 Gaba', '047 Sncg Gaba', '082 CEA-BST Ebf1 Pdyn Gaba',
       '046 Vip Gaba', '103 PVHd-DMH Lhx6 Gaba', '126 ARH-PVp Tbx3 Glut'],
      dtype='object', name='subclass')