saeyslab / nichenetr

NicheNet: predict active ligand-target links between interacting cells
452 stars 113 forks source link

Error in "evaluate_target_prediction(setting, ligand_target_matrix, ligands_position) : all genes have same response" #245

Open zcaiwei opened 5 months ago

zcaiwei commented 5 months ago

Hi, I'm trying to apply NicheNet to my simulated dataset, but I'm getting the following error, can you tell me what the cause of the error is? The following is my command

ligand_activities_simulated = predict_ligand_activities(geneset = geneset_oi_simulated, 
                                              background_expressed_genes = background_expressed_genes_simulated, 
                                              ligand_target_matrix = ligand_target_matrix_simulated_replaced, 
                                              potential_ligands = potential_test)

This is the error after running the command Error in evaluate_target_prediction(setting, ligand_target_matrix, ligands_position) : all genes have same response

Here is my complete code

library(nichenetr)  
library(tidyverse)  

setwd("~/RRoom/nichenet/data/") 

options(timeout = 600)

##----- creating lr_network
lr_network_simulated <- data.frame(
  from = c("L1","L2","L3","L3","L3","L3","L4","L7","L7","L7","L8","L10","L13","L13","L13"),
  to = c("R2","R7","R8","R2","R8","R8","R2","R8","R8","R8","R6","R2","R8","R8","R8")
)

##----- creating ligand_target_matrix
lr_sig_data <- data.frame(
  from = c("L1","L2","L3","L3","L3","L3","L4","L7","L7","L7","L8","L10","L13","L13","L13"),
  to = c("R2","R7","R8","R2","R8","R8","R2","R8","R8","R8","R6","R2","R8","R8","R8"),
  weight = c(0.9, 0.1, 0.1,0.1,0.1,0.1,0.1,0.8,0.1,0.1,0.2,0.3,0.8,0.1,0.2)
)

gr_data <- data.frame(
  from = c("R2","R7","R8","R2","R8","R8","R2","R8","R8","R8","R6","R2","R8","R8","R8"),
  to = c("G5","G10","G2","G5","G32","G39","G5","G2","G32","G39","G5","G5","G2","G32","G39"),
  weight = c(0.8, 0.1, 0.2,0.1,0.1,0.1,0.1,0.9,0.1,0.1,0.2,0.3,0.8,0.1,0.1)
)

weighted_networks <- list(lr_sig = lr_sig_data, gr = gr_data)

# The ligands column that requires calculating probability scores
ligands <- list("L1","L2","L3","L4","L7","L8","L10","L13")

ligand_target_matrix_simulated <- construct_ligand_target_matrix(
  weighted_networks = weighted_networks,
  ligands = ligands,
  ltf_cutoff = 0.20,
  algorithm = "PPR",
  damping_factor = 0.5,
  secondary_targets = FALSE,
  ligands_as_cols = TRUE,
  remove_direct_links = "no"
)

random_nums <- runif(sum(ligand_target_matrix_simulated == 0), min = 0, max = 0.3)
ligand_target_matrix_simulated_replaced <- ligand_target_matrix_simulated
ligand_target_matrix_simulated_replaced[ligand_target_matrix_simulated == 0] <- random_nums

##----- Step 1: Define expressed genes in sender and receiver cell populations
gene_cell_path <- "~/RRoom/nichenet/simulated_data/simulated_data_000/"
expression_simulated <- read.csv(paste0(gene_cell_path,"gene_cell_mat_std_00.csv"), header = TRUE, row.names = 1, sep = '\t')
expression_simulated <- as.data.frame(t(expression_simulated))
random_nums <- runif(sum(expression_simulated == 0), min = 0, max = 0.11)
expression_simulated_replaced <- expression_simulated
expression_simulated_replaced[expression_simulated == 0] <- random_nums
cell_labels <- read.csv(paste0(gene_cell_path,"CT1_CT2_label_str.txt"),header = TRUE, row.names = 1,sep = '\t')

CT1_ids = rownames(cell_labels[cell_labels$Label == "CT1",,drop = FALSE])
CT2_ids = rownames(cell_labels[cell_labels$Label == "CT2",,drop = FALSE])

expressed_genes_sender_simulated = expression_simulated_replaced[CT1_ids,] %>% apply(2,function(x){10*(2**x - 1)}) %>% apply(2,function(x){log2(mean(x) + 1)}) %>% .[. >= 2] %>% names()
expressed_genes_receiver_simulated = expression_simulated_replaced[CT2_ids,] %>% apply(2,function(x){10*(2**x - 1)}) %>% apply(2,function(x){log2(mean(x) + 1)}) %>% .[. >= 2] %>% names()

##------ Step 2: Define the gene set of interest and a background of genes
L_strings <- paste0("L", 0:14)
R_strings <- paste0("R", 0:9)
G_strings <- paste0("G", 0:39)
geneset_oi_simulated <- c(L_strings, R_strings, G_strings)
geneset_oi_simulated <- names(expression_simulated)
print(geneset_oi_simulated)

background_expressed_genes_simulated = expressed_genes_receiver_simulated %>% .[. %in% rownames(ligand_target_matrix_simulated)] 
print(background_expressed_genes_simulated)

##----- Step 3: Define a set of potential ligands
# If wanted, users can remove ligand-receptor interactions that were predicted based on protein-protein interactions and only keep ligand-receptor interactions that are described in curated databases. To do this: uncomment following line of code:
# lr_network = lr_network %>% filter(database != "ppi_prediction_go" & database != "ppi_prediction")

ligands_simulated = lr_network_simulated %>% pull(from) %>% unique()
expressed_ligands_simulated = intersect(ligands_simulated,expressed_genes_sender_simulated)

receptors_simulated = lr_network_simulated %>% pull(to) %>% unique()
expressed_receptors_simulated = intersect(receptors_simulated,expressed_genes_receiver_simulated)

lr_network_expressed_simulated = lr_network_simulated %>% filter(from %in% expressed_ligands_simulated & to %in% expressed_receptors_simulated) 
print(lr_network_expressed_simulated)

potential_ligands_simulated = lr_network_expressed_simulated %>% pull(from) %>% unique() 
print(potential_ligands_simulated) 

##----- Step 4: Perform NicheNet’s ligand activity analysis on the gene set of interest
potential_test <- c("L1","L7","L10")
ligand_activities_simulated = predict_ligand_activities(geneset = geneset_oi_simulated, 
                                              background_expressed_genes = background_expressed_genes_simulated, 
                                              ligand_target_matrix = ligand_target_matrix_simulated_replaced, 
                                              potential_ligands = potential_test)

This is a data frame named expression_simulated_replaced expression_simulated_replaced This is a data frame named ligand_target_matrix_simulated_replaced ligand_target_matrix_simulated_replaced

And this is background_expressed_genes_simulated "G5" "R2" "R8" "G2"