Chunk: Ctrl+Alt+I / Cmd+Opt+I

Cargar los paquetes, utilizando pacman

if (!require("pacman"))
  install.packages("pacman")
## Loading required package: pacman
library("pacman")

p_load("pheatmap", #rear los Heatmaps
       "RColorBrewer", 
       "ggplot2", 
       "dplyr", 
       "vroom",
       "FactoMineR", 
       "factoextra", 
       "tibble")

Llamar la base de datos

Datos_PCR <- vroom("https://raw.githubusercontent.com/ManuelLaraMVZ/Heatmaps/refs/heads/main/Ejemplo%206x4.csv")
## Rows: 8 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Gene, Condition
## dbl (6): Control_1, Control_2, Control_3, Tratamiento_1, Tratamiento_2, Trat...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(Datos_PCR)
## # A tibble: 6 × 8
##   Gene  Condition Control_1 Control_2 Control_3 Tratamiento_1 Tratamiento_2
##   <chr> <chr>         <dbl>     <dbl>     <dbl>         <dbl>         <dbl>
## 1 Gen_1 Target         31.0      29.3      29.3          26.7          25.8
## 2 Gen_2 Target         30.1      29.1      29.5          28.8          26.3
## 3 Gen_3 Target         30.3      29.0      29.9          27.6          26.6
## 4 Gen_4 Target         25.4      26.5      25.5          32.1          28.7
## 5 Gen_5 Target         29.2      28.5      26.8          29.3          30.3
## 6 Gen_6 Target         27.9      27.9      27.8          30.6          29.9
## # ℹ 1 more variable: Tratamiento_3 <dbl>

Sacar los genes de referencia, para poderlos utilizar como comparativos

Ref_gen_prom <- Datos_PCR %>% 
  filter(Condition == "Reference") %>% 
  select(-1, -2) %>% 
  summarise(across(everything(), mean, na.rm = T))
## Warning: There was 1 warning in `summarise()`.
## ℹ In argument: `across(everything(), mean, na.rm = T)`.
## Caused by warning:
## ! The `...` argument of `across()` is deprecated as of dplyr 1.1.0.
## Supply arguments directly to `.fns` through an anonymous function instead.
## 
##   # Previously
##   across(a:b, mean, na.rm = TRUE)
## 
##   # Now
##   across(a:b, \(x) mean(x, na.rm = TRUE))
head(Ref_gen_prom)
## # A tibble: 1 × 6
##   Control_1 Control_2 Control_3 Tratamiento_1 Tratamiento_2 Tratamiento_3
##       <dbl>     <dbl>     <dbl>         <dbl>         <dbl>         <dbl>
## 1      25.9      24.5      25.2          24.8          25.3          25.6

Obtener el valor de Delta Ct

DCt <- Datos_PCR %>% 
  filter(Condition == "Target") %>% 
  select(-2) %>% 
  mutate(across(-1, ~ -(. -Ref_gen_prom[[cur_column()]][[1]]),
                .names = "DCt_{.col}")) %>% 
  select(Gene, starts_with("DCt_"))

head(DCt)
## # A tibble: 6 × 7
##   Gene  DCt_Control_1 DCt_Control_2 DCt_Control_3 DCt_Tratamiento_1
##   <chr>         <dbl>         <dbl>         <dbl>             <dbl>
## 1 Gen_1        -5.06          -4.81        -4.07              -1.93
## 2 Gen_2        -4.18          -4.57        -4.27              -4.06
## 3 Gen_3        -4.39          -4.51        -4.64              -2.78
## 4 Gen_4         0.543         -1.99        -0.295             -7.32
## 5 Gen_5        -3.25          -4.05        -1.52              -4.51
## 6 Gen_6        -1.95          -3.39        -2.58              -5.77
## # ℹ 2 more variables: DCt_Tratamiento_2 <dbl>, DCt_Tratamiento_3 <dbl>

Escalar los datos

miRNA_escalado <- DCt %>% 
  column_to_rownames(var = "Gene") %>% 
  scale(center = T, 
        scale = T) %>% 
  as.data.frame()

miRNA_escalado <- miRNA_escalado * 2

miRNA_escalado
##       DCt_Control_1 DCt_Control_2 DCt_Control_3 DCt_Tratamiento_1
## Gen_1     -1.955055    -1.7446042     -1.354102         2.5112385
## Gen_2     -1.093947    -1.2935813     -1.581784         0.3403755
## Gen_3     -1.301984    -1.1783013     -2.009708         1.6466580
## Gen_4      3.481844     3.5965362      2.997433        -2.9804692
## Gen_5     -0.191901    -0.3094469      1.583660        -0.1141984
## Gen_6      1.061043     0.9293975      0.364501        -1.4036044
##       DCt_Tratamiento_2 DCt_Tratamiento_3
## Gen_1          2.227612          2.106222
## Gen_2          1.642266          2.034587
## Gen_3          1.354608          1.052326
## Gen_4         -0.791582         -2.670643
## Gen_5         -2.380496         -1.300499
## Gen_6         -2.052409         -1.221992

Definir los colores del Heatmap

paleta_colores <- colorRampPalette(c("#150f44", "white", "#fbe104"))(100)

Construir el Heatmap

Heatmap <- pheatmap(miRNA_escalado, 
                    color = paleta_colores,
                    cluster_rows = T,
                    cluster_cols = T,
                    show_rownames = T, #TRUE para mostrar los nombres de los genes, FALSE para quitarlos
                    show_colnames = T,
                    fontsize_row = 14,
                    fontsize_col = 14,
                    border_color = "black",
                    main = "Heatmap de expresión de miRNAs",
                    fontface_row = "bold")

Heatmap

Análisis de PCA

Calcular el PCA

PCA_resultados <- prcomp(t(miRNA_escalado),
                         center = TRUE, 
                         scale. = T)

summary(PCA_resultados)
## Importance of components:
##                          PC1    PC2     PC3     PC4     PC5       PC6
## Standard deviation     2.277 0.7830 0.32267 0.31401 0.01889 9.655e-18
## Proportion of Variance 0.864 0.1022 0.01735 0.01643 0.00006 0.000e+00
## Cumulative Proportion  0.864 0.9661 0.98351 0.99994 1.00000 1.000e+00

Screenplot: Varianza explicada por componente

fviz_eig(PCA_resultados,
         addlabels = T,
         barfill = "#7e085e", 
         barcolor = "#340527")

Gráfica de PCA (biplot)

PCA_df <- as.data.frame(PCA_resultados$x)
PCA_df$Sample <- rownames(PCA_df)

Graficarlo

PCA_plot <- ggplot(PCA_df,
                   aes(x = PC1,
                       y = PC2)) +
  geom_point(size = 4, aes(color = Sample)) +
  # geom_text(aes(label = Sample), vjust = -0.1, size = 3) +  # Descomentarlo si quieres etiquetas
  geom_hline(yintercept = 0, linetype = "solid", color = "black", linewidth = 1.5) +
  geom_vline(xintercept = 0, linetype = "solid", color = "black", linewidth = 1.5) +
  scale_x_continuous(breaks = seq(floor(min(PCA_df$PC1)), ceiling(max(PCA_df$PC1)), by = 0.5)) + # Escalado en X
  scale_y_continuous(breaks = seq(floor(min(PCA_df$PC2)), ceiling(max(PCA_df$PC2)), by = 0.5)) + # Escalado en Y
  labs(title = "PCA de expresión de miRNAs",  
       x = "PC1", 
       y = "PC2") +
  theme_minimal()

PCA_plot

Analizar el comportamiento de los genes

PCA_resultados_genes <- prcomp(miRNA_escalado,
                               center = T,
                               scale. = T)
summary(PCA_resultados_genes)
## Importance of components:
##                           PC1     PC2     PC3     PC4     PC5       PC6
## Standard deviation     2.2857 0.74666 0.39132 0.24689 0.06404 2.288e-17
## Proportion of Variance 0.8707 0.09292 0.02552 0.01016 0.00068 0.000e+00
## Cumulative Proportion  0.8707 0.96364 0.98916 0.99932 1.00000 1.000e+00
fviz_eig(PCA_resultados_genes, addlabels = T, 
         barfill = "#7e085e", 
         barcolor = "#340527")

PCA_df_genes <- as.data.frame(PCA_resultados_genes$x)
PCA_df_genes$Gene <- row.names(PCA_df_genes)

Gráfica

PCA_plot_genes <- ggplot(PCA_df_genes,
                         aes(x = PC1,
                             y = PC2,
                             label = Gene))+
  geom_point(size = 4, aes(color = Gene), show.legend = F)+
  geom_text(vjust = -0.05, size = 3)+
  geom_hline(yintercept = 0, linetype = "solid", color = "black", linewidth = 1.5) +
  geom_vline(xintercept = 0, linetype = "solid", color = "black", linewidth = 1.5) +
  labs(title = "PCA de expresión de miRNAs",  
       x = "PC1", 
       y = "PC2") +
  theme_minimal()
  
PCA_plot_genes