First Steps with ISCHIA.jl

Setting up the Environment

Make sure that you have Julia version $\ge 1.6$ installed already. To add ISCHIA.jl as a package dependency, we need to install it from GitHub directly, as it hasn't been released on the Julia Registery yet.

This can be done in either of the two ways:

julia> using Pkg

julia> Pkg.add(url="https://github.com/aadimator/ISCHIA.jl")

or

julia> ] # ']' should be pressed

(@v1.9) pkg> add https://github.com/aadimator/ISCHIA.jl

This will clone the repository from the GitHub and install it in your package environment.

If you already have the GitHub repository cloned on your system and you want to install the package using that, you can do so with the following commands. Make sure that you are in the cloned project directory when running the followin commands.

julia> using Pkg

julia> Pkg.add(".") # Install current directory as a package

or

julia> ] # ']' should be pressed

(@v1.9) pkg> add .

Throughout the rest of the tutorial we will assume that you have installed the ISCHIA.jl package and have already typed using ISCHIA which loads the package:

julia> using ISCHIA

ISCHIA provides optimized implementations of several functions from the original ISCHIA repository, which was coded in R. Below, we'll try to replicate their Cooccurrence of ligand and receptors section, using their provided small dataset. Instructions on converting and importing their Seurat Object into AnnData object has been described in section.

Load the Libraries

First, let's load the required libraries. Install any library that isn't in your package environment already. Julia will automatically suggest the command to install missing packages.

using Muon
using RData
using ISCHIA
using DataFrames
using Combinatorics

Load the Data

sdata = readh5ad(joinpath(pkgdir(ISCHIA), "docs", "src", "assets", "Spatial.h5ad"))
lr_network = load(joinpath(pkgdir(ISCHIA), "docs", "src", "assets", "lr_network.rds"))
size(lr_network)
(12651, 4)

Filter LR Network

gene_names = sdata.var.name
sdata.var_names = gene_names

# Create LR_Pairs column
lr_network[!, :LR_Pairs] = string.(lr_network.from, "_", lr_network.to);
lr_network = lr_network[:, [:from, :to, :LR_Pairs]]

# Filter lr_network based on conditions
from_filter = in.(lr_network[:, :from], Ref(gene_names))
to_filter = in.(lr_network[:, :to], Ref(gene_names))
all_LR_network = lr_network[from_filter .& to_filter, :];

# Extract unique genes and common genes
all_LR_genes = unique(vcat(all_LR_network[:, :from], all_LR_network[:, :to]))
all_LR_genes_comm = intersect(all_LR_genes, collect(gene_names));

# Create LR.pairs and LR.pairs.AllCombos
LR_pairs = all_LR_network[:, :LR_Pairs]
all_combos = [join(combo, "_") for combo in combinations(all_LR_genes_comm, 2)];
length(all_combos)
1007490

Calculate LR Enrchiment

spatial_object = sdata
spatial_object.var_names = spatial_object.var.name
Condition = unique(spatial_object.obs[!, "orig.ident"])
LR_list = all_LR_genes_comm
LR_pairs = LR_pairs
exp_th = 1
corr_th = 0.2

cc4_enriched_lr = find_enriched_LR_pairs(spatial_object, ["CC4"], Condition, LR_list, LR_pairs, exp_th, corr_th);
cc7_enriched_lr = find_enriched_LR_pairs(spatial_object, ["CC7"], Condition, LR_list, LR_pairs, exp_th, corr_th);
Dict{String, Any} with 2 entries:
  "cooccurrence_table" => CooccurOutput(68101×12 DataFrame…
  "enriched_LRs"       => 487×6 DataFrame

Display Calculated LR Encrichment Scores

first(cc4_enriched_lr["enriched_LRs"], 5)
3×6 DataFrame
Rowfromtocorrelationligand_FCReceptor_FCpair
StringStringFloat64Float64Float64String
1C3CXCR40.791571.33463.3869C3_CXCR4
2CCL19CXCR40.7576514.27143.3869CCL19_CXCR4
3RPS19RPSA0.6479740.56950.4499RPS19_RPSA
first(cc7_enriched_lr["enriched_LRs"], 5)
5×6 DataFrame
Rowfromtocorrelationligand_FCReceptor_FCpair
StringStringFloat64Float64Float64String
1LGALS3ITGB40.8329630.76950.8609LGALS3_ITGB4
2CDHR5CDHR20.8301170.73610.9662CDHR5_CDHR2
3HLA-EITGA60.7808891.16730.7086HLA-E_ITGA6
4HLA-EPLXNB20.7790631.16730.7971HLA-E_PLXNB2
5COL1A1ITGA50.7642931.98661.872COL1A1_ITGA5

Find Differentially Occurring LR pairs

diff_df47 = find_differentially_cooccurring_LR_pairs(cc7_enriched_lr, cc4_enriched_lr, 0.05, 0.1)
first(diff_df47, 10)
10×5 DataFrame
Rowpairgroup1_pvalgroup2_pvalpval_differenceobserved_cooc
StringRealRealRealInt64
1HLA-A_HLA-B0.00.274730.27473235
2HLA-B_LGALS30.001220.903850.90263231
3HLA-B_HLA-C1.0e-50.50.49999230
4CD24_HLA-B0.000280.50.49972218
5HLA-B_HLA-E5.0e-50.153850.1538217
6HLA-A_HLA-C0.00.132870.13287210
7HLA-A_LGALS30.00.132870.13287210
8LGALS3_CD740.00.143070.14307207
9HLA-B_THBS10.00.384620.38462206
10HLA-C_CD740.00.143070.14307204