labbook part 9 Extra analysis, cell interactions

PHOTO EMBED

Thu Jan 23 2025 19:53:49 GMT+0000 (Coordinated Universal Time)

Saved by @eho135

# Extra analysis (1,2,3,4) ####

# since no new packages were able to be downloaded, i do custom things

# Custom Ligand-Receptor Interaction Networks
# use custom lists of ligands and receptors to build your interaction network manually

# Define ligand-receptor pairs (from a database or literature)
ligand_receptor_pairs <- data.frame(
  Ligand = c("CD80", "CD86","CD70", "CCL2", "CXCL9"),
  Receptor = c("CD28", "CD28", "CD27", "CCR2", "CXCL9")
)

# Create a data frame to represent ligand-receptor interactions for each cell
ligand_receptor_matrix <- data.frame(cell = colnames(so), stringsAsFactors = FALSE)

# Loop over each ligand-receptor pair and check if they are present in the gene list
for (i in 1:nrow(ligand_receptor_pairs)) {
  ligand <- ligand_receptor_pairs$Ligand[i]
  receptor <- ligand_receptor_pairs$Receptor[i]
  
  # Check if the ligand and receptor genes exist in the Seurat object (so)
  ligand_receptor_matrix[[paste0("Ligand_", ligand)]] <- ifelse(ligand %in% rownames(so), 1, 0)
  ligand_receptor_matrix[[paste0("Receptor_", receptor)]] <- ifelse(receptor %in% rownames(so), 1, 0)
}

# Assign this new matrix to the Seurat object metadata
so@meta.data <- cbind(so@meta.data, ligand_receptor_matrix[, -1])  # Remove 'cell' column as it's redundant

# Verify if the new columns have been added
head(so@meta.data)

# Calculate the expression of ligands and receptors for each cell type (or cluster) in your Seurat object.
ligand_expression <- FetchData(so_donorint, vars = ligand_receptor_pairs$Ligand)
receptor_expression <- FetchData(seurat_object, vars = ligand_receptor_pairs$Receptor)

# Calculate potential interactions based on co-expression or proximity of ligand-receptor pairs.
# Example: correlation of ligand and receptor expression
interaction_scores <- cor(ligand_expression, receptor_expression)

# Visualize the interactions as a network.
library(igraph)

network_graph <- graph_from_data_frame(ligand_receptor_pairs)

# Open a graphics device (PNG in this case)
png("plots5/ligandreceptorinteractions.png", 
    width = 1000, height = 800, res = 300, bg = "white")

plot(network_graph,
     vertex.label = V(network_graph)$name, 
     vertex.label.cex = 0.3,         # Adjust label size
     vertex.label.color = "black",   # Label color
     vertex.label.dist = 4)          # Distance from node

# Close the device to save the plot
dev.off()
content_copyCOPY