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.
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).