Cell Specimen Mapping

The cross-session alignment algorithm has been updated and re-run just prior to release 0.13.2 (June 15th, 2017). As a result all cell specimen IDs have changed. We have built a mapping table to help map from previous cell IDs to new cell IDs. The table is available as a WellKnownFile and the BrainObservatoryApi provides a method for downloading it and loading it as a DataFrame.

Download this file in .ipynb format here.

Downloading the Mapping Table

The BrainObservatoryApi provides a get_cell_specimen_id_mapping method for downloading the mapping table as a CSV file. The method requires a file name to save the table to, and will return the table as a DataFrame.

In [1]:
from allensdk.api.queries.brain_observatory_api import BrainObservatoryApi

api = BrainObservatoryApi()

table = api.get_cell_specimen_id_mapping("./cell_mapping_table.csv")

table.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 27124 entries, 0 to 27123
Data columns (total 4 columns):
old_cell_id              27124 non-null int64
session_A_new_cell_id    18492 non-null float64
session_B_new_cell_id    17674 non-null float64
session_C_new_cell_id    16868 non-null float64
dtypes: float64(3), int64(1)
memory usage: 847.7 KB

Using the Mapping Table

The cell specimen mapping table contains 4 columns, old_cell_id, session_A_new_cell_id, session_B_new_cell_id, session_C_new_cell_id. In some instances, multiple ROIs that were assigned to a single cell specimen may now be considered distinct cells after reprocessing, so an old cell specimen ID may map to different cell specimens in different sessions.

In [2]:
# Let's assume I did some previous analysis on a cell and
# want to get the new session B id for use with the SDK
old_cell_id = 517394938

session_B_id = int(table[table.old_cell_id == old_cell_id].session_B_new_cell_id)

# Looking at the output of the table I can see that this ID
# was actually split to two cells in reprocessing
table[table.old_cell_id == old_cell_id]
Out[2]:
old_cell_id session_A_new_cell_id session_B_new_cell_id session_C_new_cell_id
17 517394938 NaN 517396084.0 517396679.0
In [3]:
from allensdk.core.brain_observatory_cache import BrainObservatoryCache
import numpy as np
import copy
from matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap
%matplotlib inline

boc = BrainObservatoryCache(manifest_file='boc/manifest.json')

cell_ids = {}
cell_ids["three_session_B"] = int(table[table.old_cell_id == old_cell_id].session_B_new_cell_id)
cell_ids["three_session_C"] = int(table[table.old_cell_id == old_cell_id].session_C_new_cell_id)

datasets = {}
for session, cell_id in cell_ids.iteritems():
    # Find and download the session experiment
    exp = boc.get_ophys_experiments(cell_specimen_ids=[cell_id],
                                    session_types=[session])[0]
    datasets[session] = boc.get_ophys_experiment_data(exp['id'])

# set up a color map for overlay
overlay_map = ListedColormap(["g"])
overlay_map.set_bad(color='k', alpha=0)

# overlay the cell ROIs on the max projection
plt.figure(figsize=(11, 5))
for i, key in enumerate(sorted(datasets.keys())):
    plt.subplot(1, 2, i+1)
    dataset = datasets[key]
    cell_id = cell_ids[key]
    overlay_mask = dataset.get_roi_mask(cell_specimen_ids=[cell_id])[0].get_mask_plane().astype(float)
    overlay_mask[overlay_mask == 0] = np.nan
    plt.imshow(dataset.get_max_projection(), cmap='gray')
    plt.imshow(overlay_mask, cmap=overlay_map, alpha=0.3)
    plt.title(key)
    plt.axis('off')
plt.show()
In [4]:
# I found an interesting cell on the brain observatory website and
# want to see if it's a cell I previously did some analysis on
new_cell_id = 517396395

old_cell_id = int(table[(table.session_A_new_cell_id==new_cell_id) | 
                        (table.session_B_new_cell_id==new_cell_id) |
                        (table.session_C_new_cell_id==new_cell_id)].old_cell_id)

# I can see that this cell is found in all sessions as well.
table[(table.session_A_new_cell_id==new_cell_id) | 
      (table.session_B_new_cell_id==new_cell_id) |
      (table.session_C_new_cell_id==new_cell_id)]
Out[4]:
old_cell_id session_A_new_cell_id session_B_new_cell_id session_C_new_cell_id
19 517394947 517396395.0 517396395.0 517396395.0