{ "cells": [ { "cell_type": "markdown", "id": "73521bac", "metadata": {}, "source": [ "# 3. Generating a 4D-Communication Tensor from computed communication scores\n", "\n", "After inferring communication scores for combinations of ligand-receptor and sender-receiver cell pairs, we can use that information to identify context-dependent CCC patterns across multiple samples simultaneously by generating a 4D-Communication Tensor. LIANA handily outputs these score as a dataframe that is easy to use for building our tensor.\n", "\n", "In this tutorial we will show you how to use the dataframe saved from LIANA to generate a 4D-Communication Tensor that could be later used with Tensor-cell2cell." ] }, { "cell_type": "markdown", "id": "739c7ec9", "metadata": {}, "source": [ "## Initial Setup\n", "\n", "**Before running this notebook** \n", "\n", "GPUs can substantially speed-up the downstream decomposition. If you are planning to use a NVIDIA GPU, make sure to have a proper NVIDIA GPU driver (https://www.nvidia.com/Download/index.aspx) as well as the CUDA toolkit (https://developer.nvidia.com/cuda-toolkit) installed.\n", "\n", "Then, make sure to create an environment with PyTorch version >= v1.8.1 following these instructions to enable CUDA.\n", "\n", "https://pytorch.org/get-started/locally/\n", "\n", "### Enabling GPU use\n", "\n", "If you are using a NVIDIA GPU, after installing PyTorch with CUDA enabled, specify `gpu_use = True`. Otherwise, `gpu_use = False`. In R, this must be specified during tensor building. " ] }, { "cell_type": "code", "execution_count": 1, "id": "88f2b541", "metadata": { "vscode": { "languageId": "r" } }, "outputs": [], "source": [ "library(reticulate, quietly = TRUE)\n", "\n", "gpu_use = TRUE\n", "if (gpu_use){\n", " device <- 'cuda:0'\n", " tensorly <- reticulate::import('tensorly')\n", " tensorly$set_backend('pytorch')\n", "}else{\n", " device <- NULL\n", "}" ] }, { "cell_type": "markdown", "id": "9cefd77f", "metadata": {}, "source": [ "**load the necessary libraries**" ] }, { "cell_type": "code", "execution_count": 2, "id": "50007cc4", "metadata": { "vscode": { "languageId": "r" } }, "outputs": [], "source": [ "library(liana, quietly = TRUE)" ] }, { "cell_type": "code", "execution_count": 3, "id": "9b58b2c9", "metadata": { "vscode": { "languageId": "r" } }, "outputs": [], "source": [ "data_folder = '../../data/liana-outputs/'\n", "output_folder = '../../data/tc2c-outputs/'\n", "\n", "env_name = 'ccc_protocols' # conda environment created with ../../env_setup/setup_env.sh" ] }, { "cell_type": "markdown", "id": "c7e02ce4", "metadata": {}, "source": [ "## Load Data\n", "\n", "Open the list containing LIANA results for each sample/context. These results contain the communication scores of the combinations of ligand-receptor pairs and sender-receiver pairs." ] }, { "cell_type": "code", "execution_count": 4, "id": "58d5fd23", "metadata": { "vscode": { "languageId": "r" } }, "outputs": [], "source": [ "context_df_dict <- readRDS(file.path(data_folder, 'LIANA_by_sample_R.rds'))" ] }, { "cell_type": "markdown", "id": "81e96bd1", "metadata": {}, "source": [ "## Create 4D-Communication Tensor\n", "\n", "### Specify the order of the samples/contexts\n", "\n", "Here, we will specify an order of the samples/contexts given the condition they belong to (HC or *Control*, M or *Moderate COVID-19*, S or *Severe COVID-19*)." ] }, { "cell_type": "code", "execution_count": 5, "id": "d89ce3fa", "metadata": { "vscode": { "languageId": "r" } }, "outputs": [], "source": [ "sorted_samples = sort(names(context_df_dict))" ] }, { "cell_type": "code", "execution_count": 6, "id": "dcff0d18", "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "\n", "
source | target | ligand.complex | receptor.complex | magnitude_rank | specificity_rank | magnitude_mean_rank | natmi.prod_weight | sca.LRscore | cellphonedb.lr.mean | specificity_mean_rank | natmi.edge_specificity | connectome.weight_sc | logfc.logfc_comb | cellphonedb.pvalue |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
<chr> | <chr> | <chr> | <chr> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> |
T | NK | B2M | CD3D | 3.983438e-11 | 0.0134542495 | 1.000000 | 8.059861 | 0.9610254 | 3.410586 | 487.75 | 0.08327606 | 1.3008562 | 0.8719799 | 0 |
Macrophages | NK | B2M | CD3D | 3.186750e-10 | 0.0135072450 | 2.000000 | 8.059599 | 0.9610248 | 3.410500 | 487.00 | 0.08327335 | 1.3005565 | 0.8832177 | 0 |
NK | NK | B2M | CD3D | 4.979297e-09 | 0.0181475450 | 3.666667 | 7.614378 | 0.9599465 | 3.264099 | 571.25 | 0.07867324 | 0.7909129 | 0.6978778 | 0 |
T | NK | B2M | KLRD1 | 1.366319e-08 | 0.0003588748 | 5.666667 | 6.865250 | 0.9579074 | 3.297900 | 211.75 | 0.17129308 | 6.9609202 | 0.8624079 | 0 |
B | NK | B2M | CD3D | 2.039520e-08 | 0.0192306877 | 5.333333 | 7.518543 | 0.9597023 | 3.232586 | 595.00 | 0.07768305 | 0.6812107 | 0.6910179 | 0 |
Macrophages | NK | B2M | KLRD1 | 2.039520e-08 | 0.0003588748 | 6.666667 | 6.865027 | 0.9579067 | 3.297814 | 210.75 | 0.17128751 | 6.9606205 | 0.8736456 | 0 |