I am trying to rename some genes in my expression matrix before plotting a heatmap using pheatmap in R. My original list of genes (genes_de_interesse) contains:
"ABCA11P", "TAP1", "TAP2", ...
I want to rename them as follows:
ABCA11P =ABCA11
TAP1 = ABCB2
TAP2 = ABCB3
So I applied the recode() function on the row names of the filtered expression matrix:
genes_de_interesse_renomeados <- recode(genes_de_interesse, "ABCA11P" = "ABCA11", "TAP1" = "ABCB2", "TAP2" = "ABCB3")
Then I filtered the expression matrix:
expr_matrix_filtered <- expr_matrix[rownames(expr_matrix) %in% genes_de_interesse_renomeados, ] %>% mutate(across(everything(), as.numeric))
And later selected columns for the heatmap using the renamed list:
heatmap_data <- final_data %>%
select(any_of(genes_de_interesse_renomeados)) %>% as.matrix()
Problem: If I use the original names (TAP1 and TAP2), the genes appear correctly in the heatmap. However, after renaming them to ABCB2 and ABCB3, they do not appear. Similarly, ABCA11 does not show up when replacing ABCA11P.
The renaming with recode() does not affect the subsetting step that uses genes_de_interesse. After filtering, the row names are still matched against the original names ("TAP1", "TAP2", "ABCA11P"), not the renamed ones ("ABCB2", "ABCB3", "ABCA11"). As a result, the heatmap never sees the renamed genes because they were not selected from the original matrix.
Question:
What is the correct way to rename genes in a matrix and make sure the renamed genes are included in the heatmap? Any help or example code would be greatly appreciated.
1 answer
The issue in your code arises because you apply recode() to the vector of genes of interest but do not update the row names of the expression matrix itself. As a result, the filtering step (rownames(expr_matrix) %in% genes_de_interesse_renomeados) finds no matches, since the row names remain the original values and do not correspond to the renamed ones.
To resolve this, first rename the row names of the entire expression matrix using recode(), which will only alter the specified genes and leave others unchanged. Then, proceed with filtering using the renamed gene list. This ensures that the renamed genes are present in the filtered matrix and will appear in the heatmap.
Here is an example of how to implement this:
# Define the mapping via recode on all row names
genes_de_interesse_renomeados <- recode(rownames(expr_matrix),
"ABCA11P" = "ABCA11",
"TAP1" = "ABCB2",
"TAP2" = "ABCB3")
# Assign the renamed row names back to the matrix
rownames(expr_matrix) <- genes_de_interesse_renomeados
# Now filter using the renamed list (assuming genes_de_interesse_renomeados is your updated vector)
expr_matrix_filtered <- expr_matrix[rownames(expr_matrix) %in% genes_de_interesse_renomeados, ] %>%
mutate(across(everything(), as.numeric))
# Proceed with your heatmap preparation
# If final_data has genes as columns (e.g., after transposition), the renamed genes will now match
heatmap_data <- final_data %>%
select(any_of(genes_de_interesse_renomeados)) %>%
as.matrix()
If your genes_de_interesse_renomeados vector includes only the genes to rename and not all genes of interest, adjust it to encompass all relevant genes after renaming.
Kevin
Log in to answer this question.
Please do this, which would be helpful or just subset those problematic rows containing those genes, so that other people can troubleshoot and help
Are you sure that command with recode is working?
Maybe I'm missing something, it seems your recode function is switching values in a vector and then you later try to subset based on the new values, but never change the rownames themselves.
Perhaps something like this may work:
Alternatively, you may use your recode command, but perhaps like this (although I haven't tried this way).