{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Retrieving Calcium Imaging Data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This section will serve as an introdution to retrieving two-photon fluorescence imaging data from the Allen Brain Observatory. By the end of this section you will know how to obtain a two-photon imaging dataset for a particular cell type in a specific visual area.\n", "\n", "## Setup\n", "We need a variety of standard Python packages in addition to two different `allensdk` toolboxes. If you have not yet installed the `allensdk`, run the cell below. Otherwise, you can skip to the cell that imports our toolboxes." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "allensdk already installed.\n" ] } ], "source": [ "# This will ensure that the AllenSDK is installed.\n", "# If not, it will install it for you.\n", "try:\n", " import allensdk\n", " if allensdk.__version__ == '2.11.2':\n", " print('allensdk already installed.')\n", " else:\n", " print('incompatible version of allensdk installed')\n", "except ImportError as e:\n", " !pip install allensdk" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/ashley/anaconda3/lib/python3.7/site-packages/dask/config.py:168: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.\n", " data = yaml.load(f.read()) or {}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Packages installed.\n" ] } ], "source": [ "# Import our toolboxes\n", "import allensdk.brain_observatory.stimulus_info as stim_info\n", "from allensdk.core.brain_observatory_cache import BrainObservatoryCache\n", "import pandas as pd \n", "import matplotlib.pyplot as plt\n", "print('Packages installed.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get a list of all possible transgenic mouse lines and brain areas, and choose which to work with." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Below, we'll create an instance of the [`BrainObservatoryCache`](https://alleninstitute.github.io/AllenSDK/allensdk.core.brain_observatory_cache.html). This object will allow us to retrieve and analyze the Allen Brain Observatory data." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "# We will create an instance of the Brain Observatory Cache as an object, \"boc.\"\n", "boc = BrainObservatoryCache(manifest_file='manifest.json')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we'll use `get_all_cre_lines()` on our `boc` object to return all of the possible Cre lines and brain areas are that we can analyze. We can also use the `get_all_targeted_structures()` method to return all brain areas in our dataset. \n", "\n", "You can find more info about the Cre-lines on the Allen Brain Atlas Transgenic Mouse Lines page. Refer back to the Brain Observatory landing page to learn more about the different visual areas. Make sure to visit the BrainObservatoryCache doucumentation for additional help with the methods used in this notebook. " ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "all cre lines: ['Cux2-CreERT2', 'Emx1-IRES-Cre', 'Fezf2-CreER', 'Nr5a1-Cre', 'Ntsr1-Cre_GN220', 'Pvalb-IRES-Cre', 'Rbp4-Cre_KL100', 'Rorb-IRES2-Cre', 'Scnn1a-Tg3-Cre', 'Slc17a7-IRES2-Cre', 'Sst-IRES-Cre', 'Tlx3-Cre_PL56', 'Vip-IRES-Cre'] \n", "\n", "all brain regions: ['VISal', 'VISam', 'VISl', 'VISp', 'VISpm', 'VISrl']\n" ] } ], "source": [ "# We'll assign the list of cre lines to a variable, 'cre-lines'.\n", "cre_lines = boc.get_all_cre_lines()\n", "print(f\"all cre lines: {cre_lines} \\n\")\n", "\n", "# We'll assign the list of possible structures to a variable, 'brain_areas'.\n", "brain_areas = boc.get_all_targeted_structures()\n", "print(\"all brain regions: \" + str(brain_areas))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Extract an experiment session" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With access to the cre lines and brain regions from the dataset, we can construct a dataframe of experiement containers from our desired cre lines and brain regions. Each experiment container has a group of experiments that share the same targeted brain area, cre line, and depth. However, the stimuli conditions may differ across experiments for a given contianer. \n", "\n", "We can use the `get_experiment_containers()` method of our `boc` object to return a list of dictionaries with data on the available experiment contianers. The method takes in the arguments `cre_lines` and `targeted_structures` which both require lists as inputs. \n", "\n", "*Note*: Not every cre line and brain region combination will have data. If no data is availabe for a certain combindation, an empty dataframe will be created. You can use the `empty` attribute on your new datafram to check if it is empty. " ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
imaging_depthtargeted_structurecre_linereporter_linedonor_namespecimen_nametagsfailed
id
637671552375VISpTlx3-Cre_PL56Ai148(TIT2L-GC6f-ICL-tTA2)340430Tlx3-Cre_PL56;Ai148-340430[]False
617381603375VISpTlx3-Cre_PL56Ai148(TIT2L-GC6f-ICL-tTA2)336824Tlx3-Cre_PL56;Ai148(CAM)-336824[]False
627823571375VISpTlx3-Cre_PL56Ai148(TIT2L-GC6f-ICL-tTA2)337458Tlx3-Cre_PL56;Ai148-337458[]False
648377366375VISpTlx3-Cre_PL56Ai148(TIT2L-GC6f-ICL-tTA2)352889Tlx3-Cre_PL56;Ai148-352889[]False
657016265375VISpTlx3-Cre_PL56Ai148(TIT2L-GC6f-ICL-tTA2)357093Tlx3-Cre_PL56;Ai148-357093[]False
637669268375VISpTlx3-Cre_PL56Ai148(TIT2L-GC6f-ICL-tTA2)340427Tlx3-Cre_PL56;Ai148-340427[]False
\n", "
" ], "text/plain": [ " imaging_depth targeted_structure cre_line \\\n", "id \n", "637671552 375 VISp Tlx3-Cre_PL56 \n", "617381603 375 VISp Tlx3-Cre_PL56 \n", "627823571 375 VISp Tlx3-Cre_PL56 \n", "648377366 375 VISp Tlx3-Cre_PL56 \n", "657016265 375 VISp Tlx3-Cre_PL56 \n", "637669268 375 VISp Tlx3-Cre_PL56 \n", "\n", " reporter_line donor_name \\\n", "id \n", "637671552 Ai148(TIT2L-GC6f-ICL-tTA2) 340430 \n", "617381603 Ai148(TIT2L-GC6f-ICL-tTA2) 336824 \n", "627823571 Ai148(TIT2L-GC6f-ICL-tTA2) 337458 \n", "648377366 Ai148(TIT2L-GC6f-ICL-tTA2) 352889 \n", "657016265 Ai148(TIT2L-GC6f-ICL-tTA2) 357093 \n", "637669268 Ai148(TIT2L-GC6f-ICL-tTA2) 340427 \n", "\n", " specimen_name tags failed \n", "id \n", "637671552 Tlx3-Cre_PL56;Ai148-340430 [] False \n", "617381603 Tlx3-Cre_PL56;Ai148(CAM)-336824 [] False \n", "627823571 Tlx3-Cre_PL56;Ai148-337458 [] False \n", "648377366 Tlx3-Cre_PL56;Ai148-352889 [] False \n", "657016265 Tlx3-Cre_PL56;Ai148-357093 [] False \n", "637669268 Tlx3-Cre_PL56;Ai148-340427 [] False " ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Assign visual area and cre line of interest for analysis \n", "visual_area = 'VISp'\n", "cre_line = 'Tlx3-Cre_PL56'\n", "\n", "# Get experiment contianers for visual area and cre line of interest \n", "exp_cont = boc.get_experiment_containers(targeted_structures = [visual_area], \n", " cre_lines = [cre_line])\n", "\n", "# Create an experiment dataframe \n", "exp_cont_df = pd.DataFrame(exp_cont)\n", "\n", "# Check if dataframe is empty \n", "if exp_cont_df.empty:\n", " print('Data frame is empty, choose another cre line & visual area combination.')\n", "\n", "# Assign the ID column as the index and return dataframe\n", "exp_cont_df = exp_cont_df.set_index('id')\n", "exp_cont_df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's look into one of these experiment containers, most of which have three different sessions for different types of visual stimuli. We can call `get_ophys_experiments()` on our `boc` object to return the experiments within a container. This method takes in the arguments `experiment_container_ids` and `stimuli` which both require lists. We can select an id from our dataframe and choose a stimuli. In our case, we'll filter for experiments with the `natural_scenes` stimuli. " ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{'id': 644660705, 'imaging_depth': 375, 'targeted_structure': 'VISp', 'cre_line': 'Tlx3-Cre_PL56', 'reporter_line': 'Ai148(TIT2L-GC6f-ICL-tTA2)', 'acquisition_age_days': 133, 'experiment_container_id': 627823571, 'session_type': 'three_session_B', 'donor_name': '337458', 'specimen_name': 'Tlx3-Cre_PL56;Ai148-337458', 'fail_eye_tracking': False}]\n" ] } ], "source": [ "# Assign experiment container id and stimuli \n", "exp_container_id = 627823571\n", "stim = ['natural_scenes']\n", "\n", "# Call experiment container for our id and stimuli of interest\n", "experiments = boc.get_ophys_experiments(experiment_container_ids = [exp_container_id],\n", " stimuli = stim)\n", "\n", "# Print out our container \n", "print(experiments)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Note**: You can execute `get_all_stimuli()` on the `boc` object if you are unsure of what stimuli are available. Not all experiments or experiment contiainers will have every stimuli in their datasets. " ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Stimuli found in Brain Observatory:\n" ] }, { "data": { "text/plain": [ "['drifting_gratings',\n", " 'locally_sparse_noise',\n", " 'locally_sparse_noise_4deg',\n", " 'locally_sparse_noise_8deg',\n", " 'natural_movie_one',\n", " 'natural_movie_three',\n", " 'natural_movie_two',\n", " 'natural_scenes',\n", " 'spontaneous',\n", " 'static_gratings']" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print('Stimuli found in Brain Observatory:')\n", "boc.get_all_stimuli()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let's download the experiment data using the `get_ophys_experiment_data` method. If you look closley above, you can see that `experiments` is a list of dictionaries for each experiment in our assigned container. We will need to input the session id of our experiment into this method. \n", "\n", "**Note**: The cell below downloads some data, and make take a minute or so to run. It is important that you do not interrupt the download of the data or else the file will become corrupted and you will have to delete it and try again. " ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "# Note: This id is different from our experiment container id \n", "session_id = experiments[0]['id']\n", "data = boc.get_ophys_experiment_data(session_id)\n", "\n", "# Take a look at the kind of object data is \n", "print(data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The returned output is data accessor object that can be used to perform analyses that we will discuss in the next section. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's how you retrieve an experiment from the Allen Brain Observatory! If you want to retrieve multiple experiments across multiple brain areas or cell types, you can use the `get_experiment_containers` method without specifying a targeted structure or brain area, and then subset your dataframe. There are many different types of analyses that can be done with this data. For example, we can creat a maximum projection image of the data to see how our cells respond to certain stimuli. We can also find out if our cells, if any, are direction selective. We will go over how to perform these analyses, and more, in the next two sections." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }