if(!requireNamespace("pacman", quietly = F))
  install.packages("pacman")
## Loading required namespace: pacman
library("pacman")

p_load("vroom",
       "dplyr",
       "ggplot2")
Datos_PCR <- vroom(file = "https://raw.githubusercontent.com/ManuelLaraMVZ/Metabolomica_2026_1/refs/heads/main/Cts")
## Rows: 20 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (4): Well, Grupo, Practica, Fluor
## dbl (1): Cq
## 
## ℹ 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.
Datos_PCR
## # A tibble: 20 × 5
##    Well       Grupo      Practica Fluor    Cq
##    <chr>      <chr>      <chr>    <chr> <dbl>
##  1 50         Curva      Absoluta SYBR   5.15
##  2 10         Curva      Absoluta SYBR  10.8 
##  3 5          Curva      Absoluta SYBR  13.9 
##  4 1          Curva      Absoluta SYBR  15.7 
##  5 0.5        Curva      Absoluta SYBR  16.5 
##  6 0.1        Curva      Absoluta SYBR  18.3 
##  7 Muestra    G1         Absoluta SYBR  11.5 
##  8 Muestra    G2         Absoluta SYBR  13.1 
##  9 A01        G1         Relativa SYBR  28.6 
## 10 A02        G1         Relativa SYBR  15.0 
## 11 A03        G1         Relativa SYBR  14.0 
## 12 A04        G1         Relativa SYBR  50   
## 13 A05        G1         Relativa SYBR  15.5 
## 14 A06        G1         Relativa SYBR  12.6 
## 15 A01        G2         Relativa SYBR  12.7 
## 16 A02        G2         Relativa SYBR  16.3 
## 17 A03        G2         Relativa SYBR  13.1 
## 18 Referencia Referencia Relativa SYBR  18.4 
## 19 Profesor   G1         Relativa SYBR  20.0 
## 20 Profesor   G2         Relativa SYBR  20.0
Filtrado <- Datos_PCR %>%
  filter(Practica == "Relativa") %>%
  filter(Grupo %in% c("G1", "G2")) %>%
  select(Well, Grupo, Cq)

Filtrado
## # A tibble: 11 × 3
##    Well     Grupo    Cq
##    <chr>    <chr> <dbl>
##  1 A01      G1     28.6
##  2 A02      G1     15.0
##  3 A03      G1     14.0
##  4 A04      G1     50  
##  5 A05      G1     15.5
##  6 A06      G1     12.6
##  7 A01      G2     12.7
##  8 A02      G2     16.3
##  9 A03      G2     13.1
## 10 Profesor G1     20.0
## 11 Profesor G2     20.0
Referencia <- Datos_PCR %>%
  filter(Practica == "Relativa") %>%
  filter(Grupo == "Referencia") %>%
  select(Well, Cq)

Referencia
## # A tibble: 1 × 2
##   Well          Cq
##   <chr>      <dbl>
## 1 Referencia  18.4
Ref_prom <- mean(Referencia$Cq, na.rm = TRUE)

Dos_DCt <- Filtrado %>%
  mutate(
    DCt = Cq - Ref_prom,
    DosDCt = 2^(-DCt)
  )

Dos_DCt
## # A tibble: 11 × 5
##    Well     Grupo    Cq   DCt   DosDCt
##    <chr>    <chr> <dbl> <dbl>    <dbl>
##  1 A01      G1     28.6 10.1  8.83e- 4
##  2 A02      G1     15.0 -3.38 1.04e+ 1
##  3 A03      G1     14.0 -4.37 2.07e+ 1
##  4 A04      G1     50   31.6  3.08e-10
##  5 A05      G1     15.5 -2.93 7.64e+ 0
##  6 A06      G1     12.6 -5.77 5.44e+ 1
##  7 A01      G2     12.7 -5.71 5.22e+ 1
##  8 A02      G2     16.3 -2.09 4.26e+ 0
##  9 A03      G2     13.1 -5.31 3.96e+ 1
## 10 Profesor G1     20.0  1.64 3.20e- 1
## 11 Profesor G2     20.0  1.64 3.20e- 1
Control <- Dos_DCt %>%
  filter(Grupo == "G1") %>%
  summarise(control = mean(DosDCt, na.rm = TRUE)) %>%
  pull(control)

Dos_DDCt <- Dos_DCt %>%
  mutate(
    DosDDCt = DosDCt / Control,
    L2 = log2(DosDDCt)
  )

Dos_DDCt
## # A tibble: 11 × 7
##    Well     Grupo    Cq   DCt   DosDCt  DosDDCt      L2
##    <chr>    <chr> <dbl> <dbl>    <dbl>    <dbl>   <dbl>
##  1 A01      G1     28.6 10.1  8.83e- 4 6.61e- 5 -13.9  
##  2 A02      G1     15.0 -3.38 1.04e+ 1 7.78e- 1  -0.363
##  3 A03      G1     14.0 -4.37 2.07e+ 1 1.55e+ 0   0.634
##  4 A04      G1     50   31.6  3.08e-10 2.31e-11 -35.3  
##  5 A05      G1     15.5 -2.93 7.64e+ 0 5.72e- 1  -0.806
##  6 A06      G1     12.6 -5.77 5.44e+ 1 4.07e+ 0   2.03 
##  7 A01      G2     12.7 -5.71 5.22e+ 1 3.91e+ 0   1.97 
##  8 A02      G2     16.3 -2.09 4.26e+ 0 3.19e- 1  -1.65 
##  9 A03      G2     13.1 -5.31 3.96e+ 1 2.97e+ 0   1.57 
## 10 Profesor G1     20.0  1.64 3.20e- 1 2.39e- 2  -5.38 
## 11 Profesor G2     20.0  1.64 3.20e- 1 2.39e- 2  -5.38
Grafica_comparativa <- ggplot(Dos_DDCt,
       aes(x = Well,
           y = DosDDCt,
           fill = Grupo)) +
  geom_col() +
  theme_classic() +
  labs(title = "Análisis relativo RT-qPCR",
       subtitle = "Equipo: LSD",
       caption = "Diseño: SZV",
       x = "Muestra",
       y = "Fold Change (2^-ΔΔCt)")

Grafica_comparativa

Dos_DDCt2 <- Dos_DCt %>%
  mutate(
    DosDDCt = DosDCt / Control,
    L2 = log2(DosDDCt)
  )

Grafica_comparativa2 <- ggplot(Dos_DDCt2,
       aes(x = Well,
           y = L2,
           fill = Grupo)) +
  geom_col() +
  theme_classic() +
  labs(title = "Análisis relativo RT-qPCR",
       subtitle = "Equipo: LSD",
       caption = "Diseño: SZV",
       x = "Muestra",
       y = "Log2(Fold Change)") +
  geom_hline(yintercept = 0,
             linetype = "solid",
             color = "black",
             linewidth = 0.2)

Grafica_comparativa2