{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial 5: Integrating RNA and Metabolomics\n", "In this tutorial, we demonstrate how to use SCIGMA to inegrate RNA and metabolomics. This tutorial can be run in under 10 minutes on a GPU-enabled computer." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from SCIGMA import *\n", "from data import load_data, preprocessing\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from utils import clustering\n", "import scanpy as sc\n", "import anndata\n", "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load Data\n", "To run this tutorial, make sure to download the metabolomics data from here: https://data.mendeley.com/datasets/w7nw4km7xd/1\n", "\n", "Modify the filepaths accordingly to match the downloaded data filepaths." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": true }, "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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", "
ProtocolPath.To.FileFile.NameData.TypeGlass.TypeArrayIDSample.IDMatrixLaserLaser.resolutionsampleConditionBrain.areaVisium.Protocol
0Sma_TO/sma/TO2/qc220221_MS__TO_mousebrain_Cy3_MV_gain5.jpgfluorescenceNotChargedTO2TO2_A1DHByL100.0mH1mHStriatumTissueOptimization
1Sma_TO_iCtrl/sma/TO2/qc220221_MS__TO_mousebrain_Cy3_MV_gain5.jpgfluorescenceNotChargedTO2TO2_A2DHBnLinfmH1mHStriatumTissueOptimization
2Sma_TO/sma/TO2/qc220221_MS__TO_mousebrain_Cy3_MV_gain5.jpgfluorescenceNotChargedTO2TO2_B1NorhyL100.0mH1mHStriatumTissueOptimization
3Sma_TO/sma/TO2/qc220221_MS__TO_mousebrain_Cy3_MV_gain5.jpgfluorescenceNotChargedTO2TO2_B2NorhyL100.0mH1mHStriatumTissueOptimization
4Sma_TO/sma/TO2/qc220221_MS__TO_mousebrain_Cy3_MV_gain5.jpgfluorescenceNotChargedTO2TO2_C19-AAyL100.0mH1mHStriatumTissueOptimization
5Sma_TO/sma/TO2/qc220221_MS__TO_mousebrain_Cy3_MV_gain5.jpgfluorescenceNotChargedTO2TO2_C29-AAyL100.0mH1mHStriatumTissueOptimization
6Sma_TO/sma/TO2/qc220221_MS__TO_mousebrain_Cy3_MV_gain5.jpgfluorescenceNotChargedTO2TO2_D1FMP-10yL100.0mH1mHStriatumTissueOptimization
7Sma_TO_iCtrl/sma/TO2/qc220221_MS__TO_mousebrain_Cy3_MV_gain5.jpgfluorescenceNotChargedTO2TO2_D2FMP-10nLinfmH1mHStriatumTissueOptimization
8Sma/sma/V11L12-038/V11L12-038_A1/output_data/V11L...V11L12-038_A1.Visium.RNARNANotChargedV11L12-038V11L12-038_A1DHByL100.0mPD3mPDStriatumStandard
9Sma/sma/V11L12-038/V11L12-038_B1/output_data/V11L...V11L12-038_B1.Visium.RNARNANotChargedV11L12-038V11L12-038_B1DHByL100.0mPD3mPDStriatumStandard
10Visium_iCtrl/sma/V11L12-038/V11L12-038_C1/output_data/V11L...V11L12-038_C1.Visium.RNARNANotChargedV11L12-038V11L12-038_C1nMnLinfmPD3mPDStriatumStandard
11Sma/sma/V11L12-038/V11L12-038_D1/output_data/V11L...V11L12-038_D1.Visium.RNARNANotChargedV11L12-038V11L12-038_D19-AAyL100.0mPD3mPDStriatumStandard
12Sma/sma/V11L12-109/V11L12-109_A1/output_data/V11L...V11L12-109_A1.Visium.RNARNANotChargedV11L12-109V11L12-109_A1FMP-10yL100.0mPD1mPDStriatumStandard
13Sma/sma/V11L12-109/V11L12-109_B1/output_data/V11L...V11L12-109_B1.Visium.RNARNANotChargedV11L12-109V11L12-109_B1FMP-10yL100.0mPD3mPDStriatumStandard
14Sma/sma/V11L12-109/V11L12-109_C1/output_data/V11L...V11L12-109_C1.Visium.RNARNANotChargedV11L12-109V11L12-109_C1FMP-10yL100.0mPD4mPDStriatumStandard
15Visium_iCtrl/sma/V11L12-109/V11L12-109_D1/output_data/V11L...V11L12-109_D1.Visium.RNARNANotChargedV11L12-109V11L12-109_D1FMP-10nLinfmPD3mPDStriatumStandard
16Sma/sma/V11T16-085/V11T16-085_A1/output_data/V11T...V11T16-085_A1.Visium.RNARNANotChargedV11T16-085V11T16-085_A1FMP-10yL100.0mPD1mPDSubNigraStandard
17Sma/sma/V11T16-085/V11T16-085_B1/output_data/V11T...V11T16-085_B1.Visium.RNARNANotChargedV11T16-085V11T16-085_B1FMP-10yL100.0mPD3mPDSubNigraStandard
18Sma/sma/V11T16-085/V11T16-085_C1/output_data/V11T...V11T16-085_C1.Visium.RNARNANotChargedV11T16-085V11T16-085_C1FMP-10yL100.0mPD4mPDSubNigraStandard
19Visium_iCtrl/sma/V11T16-085/V11T16-085_D1/output_data/V11T...V11T16-085_D1.Visium.RNARNANotChargedV11T16-085V11T16-085_D1nMnLinfmPD4mPDSubNigraStandard
20Sma/sma/V11T17-102/V11T17-102_A1/output_data/V11T...V11T17-102_A1.Visium.RNARNANotChargedV11T17-102V11T17-102_A1FMP-10yL100.0hPD1hPDStriatumRNArescue
21Sma/sma/V11T17-102/V11T17-102_B1/output_data/V11T...V11T17-102_B1.Visium.RNARNANotChargedV11T17-102V11T17-102_B1FMP-10yL100.0hPD1hPDStriatumRNArescue
22Sma/sma/V11T17-102/V11T17-102_C1/output_data/V11T...V11T17-102_C1.Visium.RNARNANotChargedV11T17-102V11T17-102_C1FMP-10yL100.0hPD1hPDStriatumRNArescue
23Sma/sma/V11T17-102/V11T17-102_D1/output_data/V11T...V11T17-102_D1.Visium.RNARNANotChargedV11T17-102V11T17-102_D1FMP-10yL100.0hPD1hPDStriatumRNArescue
24Visium/visium/V11T17-101/V11T17-101_A1/output_data/V...V11T17-101_A1.Visium.RNARNANotChargedV11T17-101V11T17-101_A1nMnLinfmPD1mPDSubNigraStandard
25Visium/visium/V11T17-101/V11T17-101_B1/output_data/V...V11T17-101_B1.Visium.RNARNANotChargedV11T17-101V11T17-101_B1nMnLinfmPD3mPDStriatumStandard
26Visium/visium/V11T17-101/V11T17-101_C1/output_data/V...V11T17-101_C1.Visium.RNARNANotChargedV11T17-101V11T17-101_C1nMnLinfmH1mHStriatumStandard
27Visium/visium/V11T17-101/V11T17-101_D1/output_data/V...V11T17-101_D1.Visium.RNARNANotChargedV11T17-101V11T17-101_D1nMnLinfhPD1hPDStriatumStandard
28Sma/sma/V11L12-038/V11L12-038_A1/output_data/V11L...V11L12-038_A1.Visium.DHB.220826_smamsi.csvlipidsNotChargedV11L12-038V11L12-038_A1DHByL100.0mPD3mPDStriatumStandard
29Sma/sma/V11L12-038/V11L12-038_B1/output_data/V11L...V11L12-038_B1.Visium.DHB.220826_smamsi.csvlipidsNotChargedV11L12-038V11L12-038_B1DHByL100.0mPD3mPDStriatumStandard
30Sma/sma/V11L12-038/V11L12-038_D1/output_data/V11L...V11L12-038_Mouse_D1.Visium.9aa.220826_smamsi.csvmetabolitesNotChargedV11L12-038V11L12-038_D19-AAyL100.0mPD3mPDStriatumStandard
31Sma/sma/V11L12-109/V11L12-109_A1/output_data/V11L...V11L12-109_A1.Visium.FMP.220826_smamsi.csvneurotransmittersNotChargedV11L12-109V11L12-109_A1FMP-10yL100.0mPD1mPDStriatumStandard
32Sma/sma/V11L12-109/V11L12-109_B1/output_data/V11L...V11L12-109_B1.Visium.FMP.220826_smamsi.csvneurotransmittersNotChargedV11L12-109V11L12-109_B1FMP-10yL100.0mPD3mPDStriatumStandard
33Sma/sma/V11L12-109/V11L12-109_C1/output_data/V11L...V11L12-109_C1.Visium.FMP.220826_smamsi.csvneurotransmittersNotChargedV11L12-109V11L12-109_C1FMP-10yL100.0mPD4mPDStriatumStandard
34Sma/sma/V11T16-085/V11T16-085_A1/output_data/V11T...V11T17-085_A1.Visium.FMP.220826_smamsi.csvneurotransmittersNotChargedV11T16-085V11T16-085_A1FMP-10yL100.0mPD1mPDSubNigraStandard
35Sma/sma/V11T16-085/V11T16-085_B1/output_data/V11T...V11T17-085_B1.Visium.FMP.220826_smamsi.csvneurotransmittersNotChargedV11T16-085V11T16-085_B1FMP-10yL100.0mPD3mPDSubNigraStandard
36Sma/sma/V11T16-085/V11T16-085_C1/output_data/V11T...V11T17-085_C1.Visium.FMP.220826_smamsi.csvneurotransmittersNotChargedV11T16-085V11T16-085_C1FMP-10yL100.0mPD4mPDSubNigraStandard
37Sma/sma/V11T17-102/V11T17-102_A1/output_data/V11T...V11T17-102_A1.Visium.FMP.220826_smamsi.csvneurotransmittersNotChargedV11T17-102V11T17-102_A1FMP-10yL100.0hPD1hPDStriatumRNArescue
38Sma/sma/V11T17-102/V11T17-102_B1/output_data/V11T...V11T17-102_B1.Visium.FMP.220826_smamsi.csvneurotransmittersNotChargedV11T17-102V11T17-102_B1FMP-10yL100.0hPD1hPDStriatumRNArescue
39Sma/sma/V11T17-102/V11T17-102_C1/output_data/V11T...V11T17-102_C1.Visium.FMP.220826_smamsi.csvneurotransmittersNotChargedV11T17-102V11T17-102_C1FMP-10yL100.0hPD1hPDStriatumRNArescue
40Sma/sma/V11T17-102/V11T17-102_D1/output_data/V11T...V11T17-102_D1.Visium.FMP.220826_smamsi.csvneurotransmittersNotChargedV11T17-102V11T17-102_D1FMP-10yL100.0hPD1hPDStriatumRNArescue
\n", "
" ], "text/plain": [ " Protocol Path.To.File \\\n", "0 Sma_TO /sma/TO2/qc \n", "1 Sma_TO_iCtrl /sma/TO2/qc \n", "2 Sma_TO /sma/TO2/qc \n", "3 Sma_TO /sma/TO2/qc \n", "4 Sma_TO /sma/TO2/qc \n", "5 Sma_TO /sma/TO2/qc \n", "6 Sma_TO /sma/TO2/qc \n", "7 Sma_TO_iCtrl /sma/TO2/qc \n", "8 Sma /sma/V11L12-038/V11L12-038_A1/output_data/V11L... \n", "9 Sma /sma/V11L12-038/V11L12-038_B1/output_data/V11L... \n", "10 Visium_iCtrl /sma/V11L12-038/V11L12-038_C1/output_data/V11L... \n", "11 Sma /sma/V11L12-038/V11L12-038_D1/output_data/V11L... \n", "12 Sma /sma/V11L12-109/V11L12-109_A1/output_data/V11L... \n", "13 Sma /sma/V11L12-109/V11L12-109_B1/output_data/V11L... \n", "14 Sma /sma/V11L12-109/V11L12-109_C1/output_data/V11L... \n", "15 Visium_iCtrl /sma/V11L12-109/V11L12-109_D1/output_data/V11L... \n", "16 Sma /sma/V11T16-085/V11T16-085_A1/output_data/V11T... \n", "17 Sma /sma/V11T16-085/V11T16-085_B1/output_data/V11T... \n", "18 Sma /sma/V11T16-085/V11T16-085_C1/output_data/V11T... \n", "19 Visium_iCtrl /sma/V11T16-085/V11T16-085_D1/output_data/V11T... \n", "20 Sma /sma/V11T17-102/V11T17-102_A1/output_data/V11T... \n", "21 Sma /sma/V11T17-102/V11T17-102_B1/output_data/V11T... \n", "22 Sma /sma/V11T17-102/V11T17-102_C1/output_data/V11T... \n", "23 Sma /sma/V11T17-102/V11T17-102_D1/output_data/V11T... \n", "24 Visium /visium/V11T17-101/V11T17-101_A1/output_data/V... \n", "25 Visium /visium/V11T17-101/V11T17-101_B1/output_data/V... \n", "26 Visium /visium/V11T17-101/V11T17-101_C1/output_data/V... \n", "27 Visium /visium/V11T17-101/V11T17-101_D1/output_data/V... \n", "28 Sma /sma/V11L12-038/V11L12-038_A1/output_data/V11L... \n", "29 Sma /sma/V11L12-038/V11L12-038_B1/output_data/V11L... \n", "30 Sma /sma/V11L12-038/V11L12-038_D1/output_data/V11L... \n", "31 Sma /sma/V11L12-109/V11L12-109_A1/output_data/V11L... \n", "32 Sma /sma/V11L12-109/V11L12-109_B1/output_data/V11L... \n", "33 Sma /sma/V11L12-109/V11L12-109_C1/output_data/V11L... \n", "34 Sma /sma/V11T16-085/V11T16-085_A1/output_data/V11T... \n", "35 Sma /sma/V11T16-085/V11T16-085_B1/output_data/V11T... \n", "36 Sma /sma/V11T16-085/V11T16-085_C1/output_data/V11T... \n", "37 Sma /sma/V11T17-102/V11T17-102_A1/output_data/V11T... \n", "38 Sma /sma/V11T17-102/V11T17-102_B1/output_data/V11T... \n", "39 Sma /sma/V11T17-102/V11T17-102_C1/output_data/V11T... \n", "40 Sma /sma/V11T17-102/V11T17-102_D1/output_data/V11T... \n", "\n", " File.Name Data.Type \\\n", "0 220221_MS__TO_mousebrain_Cy3_MV_gain5.jpg fluorescence \n", "1 220221_MS__TO_mousebrain_Cy3_MV_gain5.jpg fluorescence \n", "2 220221_MS__TO_mousebrain_Cy3_MV_gain5.jpg fluorescence \n", "3 220221_MS__TO_mousebrain_Cy3_MV_gain5.jpg fluorescence \n", "4 220221_MS__TO_mousebrain_Cy3_MV_gain5.jpg fluorescence \n", "5 220221_MS__TO_mousebrain_Cy3_MV_gain5.jpg fluorescence \n", "6 220221_MS__TO_mousebrain_Cy3_MV_gain5.jpg fluorescence \n", "7 220221_MS__TO_mousebrain_Cy3_MV_gain5.jpg fluorescence \n", "8 V11L12-038_A1.Visium.RNA RNA \n", "9 V11L12-038_B1.Visium.RNA RNA \n", "10 V11L12-038_C1.Visium.RNA RNA \n", "11 V11L12-038_D1.Visium.RNA RNA \n", "12 V11L12-109_A1.Visium.RNA RNA \n", "13 V11L12-109_B1.Visium.RNA RNA \n", "14 V11L12-109_C1.Visium.RNA RNA \n", "15 V11L12-109_D1.Visium.RNA RNA \n", "16 V11T16-085_A1.Visium.RNA RNA \n", "17 V11T16-085_B1.Visium.RNA RNA \n", "18 V11T16-085_C1.Visium.RNA RNA \n", "19 V11T16-085_D1.Visium.RNA RNA \n", "20 V11T17-102_A1.Visium.RNA RNA \n", "21 V11T17-102_B1.Visium.RNA RNA \n", "22 V11T17-102_C1.Visium.RNA RNA \n", "23 V11T17-102_D1.Visium.RNA RNA \n", "24 V11T17-101_A1.Visium.RNA RNA \n", "25 V11T17-101_B1.Visium.RNA RNA \n", "26 V11T17-101_C1.Visium.RNA RNA \n", "27 V11T17-101_D1.Visium.RNA RNA \n", "28 V11L12-038_A1.Visium.DHB.220826_smamsi.csv lipids \n", "29 V11L12-038_B1.Visium.DHB.220826_smamsi.csv lipids \n", "30 V11L12-038_Mouse_D1.Visium.9aa.220826_smamsi.csv metabolites \n", "31 V11L12-109_A1.Visium.FMP.220826_smamsi.csv neurotransmitters \n", "32 V11L12-109_B1.Visium.FMP.220826_smamsi.csv neurotransmitters \n", "33 V11L12-109_C1.Visium.FMP.220826_smamsi.csv neurotransmitters \n", "34 V11T17-085_A1.Visium.FMP.220826_smamsi.csv neurotransmitters \n", "35 V11T17-085_B1.Visium.FMP.220826_smamsi.csv neurotransmitters \n", "36 V11T17-085_C1.Visium.FMP.220826_smamsi.csv neurotransmitters \n", "37 V11T17-102_A1.Visium.FMP.220826_smamsi.csv neurotransmitters \n", "38 V11T17-102_B1.Visium.FMP.220826_smamsi.csv neurotransmitters \n", "39 V11T17-102_C1.Visium.FMP.220826_smamsi.csv neurotransmitters \n", "40 V11T17-102_D1.Visium.FMP.220826_smamsi.csv neurotransmitters \n", "\n", " Glass.Type ArrayID Sample.ID Matrix Laser Laser.resolution \\\n", "0 NotCharged TO2 TO2_A1 DHB yL 100.0 \n", "1 NotCharged TO2 TO2_A2 DHB nL inf \n", "2 NotCharged TO2 TO2_B1 Norh yL 100.0 \n", "3 NotCharged TO2 TO2_B2 Norh yL 100.0 \n", "4 NotCharged TO2 TO2_C1 9-AA yL 100.0 \n", "5 NotCharged TO2 TO2_C2 9-AA yL 100.0 \n", "6 NotCharged TO2 TO2_D1 FMP-10 yL 100.0 \n", "7 NotCharged TO2 TO2_D2 FMP-10 nL inf \n", "8 NotCharged V11L12-038 V11L12-038_A1 DHB yL 100.0 \n", "9 NotCharged V11L12-038 V11L12-038_B1 DHB yL 100.0 \n", "10 NotCharged V11L12-038 V11L12-038_C1 nM nL inf \n", "11 NotCharged V11L12-038 V11L12-038_D1 9-AA yL 100.0 \n", "12 NotCharged V11L12-109 V11L12-109_A1 FMP-10 yL 100.0 \n", "13 NotCharged V11L12-109 V11L12-109_B1 FMP-10 yL 100.0 \n", "14 NotCharged V11L12-109 V11L12-109_C1 FMP-10 yL 100.0 \n", "15 NotCharged V11L12-109 V11L12-109_D1 FMP-10 nL inf \n", "16 NotCharged V11T16-085 V11T16-085_A1 FMP-10 yL 100.0 \n", "17 NotCharged V11T16-085 V11T16-085_B1 FMP-10 yL 100.0 \n", "18 NotCharged V11T16-085 V11T16-085_C1 FMP-10 yL 100.0 \n", "19 NotCharged V11T16-085 V11T16-085_D1 nM nL inf \n", "20 NotCharged V11T17-102 V11T17-102_A1 FMP-10 yL 100.0 \n", "21 NotCharged V11T17-102 V11T17-102_B1 FMP-10 yL 100.0 \n", "22 NotCharged V11T17-102 V11T17-102_C1 FMP-10 yL 100.0 \n", "23 NotCharged V11T17-102 V11T17-102_D1 FMP-10 yL 100.0 \n", "24 NotCharged V11T17-101 V11T17-101_A1 nM nL inf \n", "25 NotCharged V11T17-101 V11T17-101_B1 nM nL inf \n", "26 NotCharged V11T17-101 V11T17-101_C1 nM nL inf \n", "27 NotCharged V11T17-101 V11T17-101_D1 nM nL inf \n", "28 NotCharged V11L12-038 V11L12-038_A1 DHB yL 100.0 \n", "29 NotCharged V11L12-038 V11L12-038_B1 DHB yL 100.0 \n", "30 NotCharged V11L12-038 V11L12-038_D1 9-AA yL 100.0 \n", "31 NotCharged V11L12-109 V11L12-109_A1 FMP-10 yL 100.0 \n", "32 NotCharged V11L12-109 V11L12-109_B1 FMP-10 yL 100.0 \n", "33 NotCharged V11L12-109 V11L12-109_C1 FMP-10 yL 100.0 \n", "34 NotCharged V11T16-085 V11T16-085_A1 FMP-10 yL 100.0 \n", "35 NotCharged V11T16-085 V11T16-085_B1 FMP-10 yL 100.0 \n", "36 NotCharged V11T16-085 V11T16-085_C1 FMP-10 yL 100.0 \n", "37 NotCharged V11T17-102 V11T17-102_A1 FMP-10 yL 100.0 \n", "38 NotCharged V11T17-102 V11T17-102_B1 FMP-10 yL 100.0 \n", "39 NotCharged V11T17-102 V11T17-102_C1 FMP-10 yL 100.0 \n", "40 NotCharged V11T17-102 V11T17-102_D1 FMP-10 yL 100.0 \n", "\n", " sample Condition Brain.area Visium.Protocol \n", "0 mH1 mH Striatum TissueOptimization \n", "1 mH1 mH Striatum TissueOptimization \n", "2 mH1 mH Striatum TissueOptimization \n", "3 mH1 mH Striatum TissueOptimization \n", "4 mH1 mH Striatum TissueOptimization \n", "5 mH1 mH Striatum TissueOptimization \n", "6 mH1 mH Striatum TissueOptimization \n", "7 mH1 mH Striatum TissueOptimization \n", "8 mPD3 mPD Striatum Standard \n", "9 mPD3 mPD Striatum Standard \n", "10 mPD3 mPD Striatum Standard \n", "11 mPD3 mPD Striatum Standard \n", "12 mPD1 mPD Striatum Standard \n", "13 mPD3 mPD Striatum Standard \n", "14 mPD4 mPD Striatum Standard \n", "15 mPD3 mPD Striatum Standard \n", "16 mPD1 mPD SubNigra Standard \n", "17 mPD3 mPD SubNigra Standard \n", "18 mPD4 mPD SubNigra Standard \n", "19 mPD4 mPD SubNigra Standard \n", "20 hPD1 hPD Striatum RNArescue \n", "21 hPD1 hPD Striatum RNArescue \n", "22 hPD1 hPD Striatum RNArescue \n", "23 hPD1 hPD Striatum RNArescue \n", "24 mPD1 mPD SubNigra Standard \n", "25 mPD3 mPD Striatum Standard \n", "26 mH1 mH Striatum Standard \n", "27 hPD1 hPD Striatum Standard \n", "28 mPD3 mPD Striatum Standard \n", "29 mPD3 mPD Striatum Standard \n", "30 mPD3 mPD Striatum Standard \n", "31 mPD1 mPD Striatum Standard \n", "32 mPD3 mPD Striatum Standard \n", "33 mPD4 mPD Striatum Standard \n", "34 mPD1 mPD SubNigra Standard \n", "35 mPD3 mPD SubNigra Standard \n", "36 mPD4 mPD SubNigra Standard \n", "37 hPD1 hPD Striatum RNArescue \n", "38 hPD1 hPD Striatum RNArescue \n", "39 hPD1 hPD Striatum RNArescue \n", "40 hPD1 hPD Striatum RNArescue " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "metadata_path = 'metablomics/metadata.csv'\n", "metadata = pd.read_csv(metadata_path)\n", "metadata" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "# V11L12-109: A_1, B_1, C_1\n", "# V11L12-038: A_1, B_1, D_1\n", "# V11T16-085: A_1, B_1, C_1\n", "# use the metadata information to find what paths to use \n", "rep = \"V11L12-109_C1\"\n", "folder = 'metablomics/sma/V11L12-109/'\n", "sample = f'{rep}/output_data/'\n", "msi_file = f'{rep}_MSI/V11L12-109_C1.Visium.FMP.220826_smamsi.csv' #FMP\n", "rna_file = f'{rep}_RNA/outs/filtered_feature_bc_matrix.h5'\n", "spatial_locations_file = f'{rep}_RNA/outs/spatial/tissue_positions_list.csv'" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/users/schang59/anaconda/glue/lib/python3.8/site-packages/anndata/_core/anndata.py:1830: UserWarning: Variable names are not unique. To make them unique, call `.var_names_make_unique`.\n", " utils.warn_names_duplicates(\"var\")\n" ] } ], "source": [ "msi_csv = pd.read_csv(folder+sample+msi_file) \n", "rna_spots = pd.read_csv(folder+sample+spatial_locations_file,header=None) \n", "rna_adata = sc.read_10x_h5(folder+sample+rna_file) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Preprocessing Data\n", "Format the data for SCIGMA" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "# get spots from both modalities that share coodrinates\n", "msi_filtered = msi_csv.merge(rna_spots[[0,2,3]], left_on=['x','y'], right_on=[2,3], how='inner').drop(columns=[2,3])" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "# get spots that were measured\n", "shared_spots = set(msi_filtered[0]).intersection(set(rna_adata.obs_names))\n", "mask = msi_filtered[0].isin(shared_spots)\n", "msi_filtered = msi_filtered[mask]" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "spatial = msi_filtered[['x', 'y']].to_numpy()" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/tmp/ipykernel_2076060/3057740423.py:1: FutureWarning: X.dtype being converted to np.float32 from float64. In the next version of anndata (0.9) conversion will not be automatic. Pass dtype explicitly to avoid this warning. Pass `AnnData(X, dtype=X.dtype, ...)` to get the future behavour.\n", " msi = anndata.AnnData(msi_filtered.drop(columns=['x','y',0]))\n", "/users/schang59/anaconda/glue/lib/python3.8/site-packages/anndata/_core/anndata.py:121: ImplicitModificationWarning: Transforming to str index.\n", " warnings.warn(\"Transforming to str index.\", ImplicitModificationWarning)\n" ] } ], "source": [ "msi = anndata.AnnData(msi_filtered.drop(columns=['x','y',0]))\n", "msi.obsm['spatial'] = spatial\n", "msi.obs_names = [str(i) for i in range(len(msi))]" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/users/schang59/anaconda/glue/lib/python3.8/site-packages/anndata/_core/anndata.py:1830: UserWarning: Variable names are not unique. To make them unique, call `.var_names_make_unique`.\n", " utils.warn_names_duplicates(\"var\")\n" ] } ], "source": [ "rna = rna_adata[msi_filtered[0]]\n", "rna.obsm['spatial'] = spatial\n", "rna.obs_names = [str(i) for i in range(len(rna))]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/users/schang59/anaconda/glue/lib/python3.8/site-packages/anndata/_core/anndata.py:1830: UserWarning: Variable names are not unique. To make them unique, call `.var_names_make_unique`.\n", " utils.warn_names_duplicates(\"var\")\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Preprocessing anndatas\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/users/schang59/anaconda/glue/lib/python3.8/site-packages/anndata/_core/anndata.py:1830: UserWarning: Variable names are not unique. To make them unique, call `.var_names_make_unique`.\n", " utils.warn_names_duplicates(\"var\")\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Constructing spatial graphs\n", "Constructing feature graphs\n", "Feature graph 1\n", "Feature graph 1 finished\n", "Feature graph 2\n", "Feature graph 2 finished\n" ] } ], "source": [ "# preprocess into dictionary\n", "data_dict = preprocessing(rna, msi, datatype='MSI',n_neighbors=6, feat_neighbors=6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Model Training" ] }, { "cell_type": "code", "execution_count": 397, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Num samples pruned: 0\n", "Creating adjacency matrices\n", "Model ready for training!\n" ] } ], "source": [ "seed = 2025\n", "model = SCIGMA(data_dict, dim_output=20, seed_num=seed, device=torch.device('cuda:0'), recon_weight1 = 1, recon_weight2 = 0.1, contrastive_weight1=1e-1, contrastive_weight2=1e-2, clr_weight=0.2, batch_size=len(data_dict['adata_omics1']), learning_rate=1e-3) #connectivities=connectivities)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "output = model.train(600)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "adata_combined = data_dict['adata_omics1'].copy()\n", "adata_combined.uns['emb_latent'] = output['emb_latent']\n", "adata_combined.uns['emb_recon'] = output['emb_recon']\n", "adata_combined.obsm['emb_combined'] = output['emb_latent_combined']\n", "adata_combined.obs['invtau'] = output['invtau']\n", "adata_combined.obs['tau'] = output['tau']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Spatial Domain Detection" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# we use mcluster as clustering tool by default.\n", "tool = 'mclust' # mclust, leiden, and louvain\n", "\n", "# clustering\n", "if tool == 'mclust':\n", " clustering(adata_combined, key='emb_combined', add_key='SCIGMA', n_clusters=12, method=tool, use_pca=False)\n", "elif tool in ['leiden', 'louvain']:\n", " clustering(adata_combined, key='emb_combined', add_key='SCIGMA', n_clusters=12, method=tool, start=0.1, end=2.0, increment=0.01)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# visualization\n", "fig, ax_list = plt.subplots(1, 1, figsize=(6, 6))\n", "sc.pl.embedding(adata_combined, basis='spatial', color='SCIGMA', ax=ax_list, title='SCIGMA', s=250, show=False, add_outline=False)\n", "plt.gca()\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "# visualization\n", "fig, ax_list = plt.subplots(1, 2, figsize=(12, 5))\n", "sc.pp.neighbors(adata_combined, use_rep='emb_combined', n_neighbors=10)\n", "sc.tl.umap(adata_combined)\n", "\n", "sc.pl.umap(adata_combined, color='SCIGMA', ax=ax_list[0], title='SCIGMA', s=60, show=False)\n", "sc.pl.embedding(adata_combined, basis='spatial', color='SCIGMA', ax=ax_list[1], title='ContrastiveSG', s=80, show=False)\n", "\n", "ax_list[0].get_legend().remove()\n", "plt.tight_layout(w_pad=0.3)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Uncertainty Visualization" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# uncertainty visualization\n", "sc.pl.embedding(adata_combined, \n", " basis='spatial', \n", " color=['invtau'], \n", " title=['Uncertainty'], s=50, show=False)\n", "plt.gca()\n", "plt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "glue", "language": "python", "name": "glue" }, "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.8.0" } }, "nbformat": 4, "nbformat_minor": 2 }