# set chunk defaults for suppressing console message and displaying code by default
knitr::opts_chunk$set(
  message = FALSE,
  # warning = FALSE,
  echo = TRUE,
  include = TRUE
)
# set working directory for chunks and show progress bar for knitr actions
knitr::opts_knit$set(
  root.dir = "~/GitHub/UNcleR/",
  progress = TRUE
)

library(profvis)

## data loading and manipulation
# library(feather)
library(tidyverse)
# library(magrittr) # for the Tee pipe
library(readxl)

## statistics
# library(rstatix)

## plotting
library(cowplot)
# library(ggrepel)
# library(ggridges)
# library(gganimate)
# library(lemon)
library(RColorBrewer)
library(xkcdcolors)
library(extrafont)

## programming
library(glue)
library(rlang)

## interactivity
# library(crosstalk)
# library(plotly)

# user packages
# library(fragr)
# library(UNcleR)
# library(RTtools)

# default style for ggplot
theme_set(
  theme_bw(base_family = "Roboto Condensed") +
    theme(
      axis.text = element_text(face = "bold"),
      panel.grid = element_blank()
    )
)


# useful variables
wellOrder <- purrr::map2_chr(rep(c(LETTERS[1:8]), 12), purrr::flatten_chr(purrr::map(c(1:12), rep, 8)), paste0)
uniOrder <- purrr::map2_chr(rep(c(LETTERS[1:16]), 3), purrr::flatten_chr(purrr::map(c(1:3), rep, 16)), paste0)

Parsing Bundled Export Spectra Files

Static Method Spectra

SLSpath <- "ln/local/T4 RNA Ligase/Exports/General Screen/210607-01-T4 RNA Ligase-Gen006L-SLS Bundle.xlsx"

SLSbundle_sheets <- readxl::excel_sheets(SLSpath) |> {
  \(x) x[match(x[x != "Sheet1"], uniOrder)]
}() |> rlang::set_names()
# SLSbundle_sheets

SLSbundle <- suppressMessages(purrr::map_dfr(SLSbundle_sheets,
  # see equivalent function below for DLS, sheet should be passed to avoid potential errors
  readxl::read_xlsx,
  path = SLSpath,
  skip = 3,
  col_types = "numeric",
  .name_repair = "universal",
  .id = "uni"
)) |>
tidyr::nest(specTm = c(2:3), specSLS266 = c(4:5), specSLS473 = c(6:7)) |>
dplyr::mutate(dplyr::across(
  tidyselect::contains("spec"),
  \(lcol) purrr::map(lcol, \(df) dplyr::rename_with(df, .cols = 1, .fn = ~"temp_C"))
)) |> 
  dplyr::mutate(dplyr::across(
  tidyselect::any_of(c("specSLS266", "specSLS473")),
  \(lcol) purrr::map(lcol, function(df) {
    dplyr::rename_with(df, .cols = c(1, 2), .fn = ~ c("temp_x", "intensity_y"))
  })
)) |> 
  dplyr::mutate(dplyr::across(
  tidyselect::any_of(c("specTm")),
  \(lcol) purrr::map(lcol, function(df) {
    dplyr::rename_with(df, .cols = c(1, 2), .fn = ~ c("temp_x", "BCM_y"))
  })
))
SLSbundle# |> {
  # \(df) df[["specTm"]][[1]]
# }()
import_staticBundle_test <- function(path) {
  sheets <- readxl::excel_sheets(path) |> {
    \(s) s[match(s[s != "Sheet1"], uniOrder)]
  }() |> rlang::set_names()

  bundle <- suppressMessages(purrr::map_dfr(sheets,
    readxl::read_xlsx,
    path = path,
    skip = 3,
    col_types = "numeric",
    .name_repair = "universal",
    .id = "uni"
  )) |>
  tidyr::nest(FLUORspec = c(2:3), SLSspec266 = c(4:5), SLSspec473 = c(6:7)) |>
  dplyr::mutate(dplyr::across(
    tidyselect::contains("spec"),
    \(lcol) purrr::map(lcol, \(df) dplyr::rename_with(df, .cols = 1, .fn = ~"temp_C"))
  ))

  return(bundle)
}
SLSbundle_files <- list.files("ln/local/T4 RNA Ligase/Exports/General Screen/",
  pattern = "SLS Bundle",
  full.names = TRUE
) |>  {
  \(path) rlang::set_names(path, nm = map_chr(path, stringr::str_extract, "(?<=//).*(?=.xlsx)"))
}()
# SLSbundle_files

SLSbundles <- purrr::map(SLSbundle_files, import_staticBundle_test) |> {
  \(l) rlang::set_names(l,
    nm = purrr::map_chr(
      SLSbundle_files,
      stringr::str_extract, "(?<=//).*(?=\\.xlsx)"
    )
  )
}()
SLSbundles

Dynamic Method Spectra

DLSpath <- "ln/local/Bst2Pol/Exports/General Screen/210615-01-Bst Pol-Gen006R-DLS Bundle.uni-2021-06-17T10-27-43.xlsx"

DLSbundle_sheets <- readxl::excel_sheets(DLSpath) |> {
  \(x) x[x != "Sheet1"]
}() |> rlang::set_names()
# DLSbundle_sheets

DLSbundle <- suppressMessages(purrr::map(
  DLSbundle_sheets,
  \(sheet) readxl::read_xlsx(
    path = DLSpath,
    sheet = sheet,
    skip = 2,
    col_types = "numeric",
    .name_repair = "universal"
  )
))
df_i <- DLSbundle[grepl("Intensity", names(DLSbundle))] |> {
  \(bundle) rlang::set_names(bundle,
    nm = stringr::str_extract(names(bundle), "(?<=\\-)[A-P]\\d+\\-\\d+$")
  )
}() |>
dplyr::bind_rows(.id = "uni") |>
tidyr::nest(specDLS_I = c(2:3))

df_m <- DLSbundle[grepl("Mass", names(DLSbundle))] |> {
  \(bundle) rlang::set_names(bundle,
    nm = stringr::str_extract(names(bundle), "(?<=\\-)[A-P]\\d+\\-\\d+$")
  )
}() |>
dplyr::bind_rows(.id = "uni") |>
tidyr::nest(specDLS_M = c(2:3))

df_c <- DLSbundle[grepl("Correlation", names(DLSbundle))] |> {
  \(bundle) rlang::set_names(bundle,
    nm = stringr::str_extract(names(bundle), "(?<=\\-)[A-P]\\d+\\-\\d+$")
  )
}() |>
dplyr::bind_rows(.id = "uni") |>
tidyr::nest(specDLS_C = c(2:3))

DLStable <- reduce(list(df_c, df_i, df_m), dplyr::left_join, by = "uni")
DLStable |>
tidyr::separate(uni, into = c("uni", "temp_C"), sep = "-", convert = TRUE) |>
  dplyr::group_by(uni) |> 
dplyr::filter(temp_C == min(temp_C)) |>
dplyr::select(-temp_C) |>
  dplyr::ungroup() |> 
dplyr::mutate(dplyr::across(
  tidyselect::any_of(c("specDLS_I", "specDLS_M")),
  \(lcol) purrr::map(lcol, function(df) {
    dplyr::rename_with(df, .cols = c(1, 2), .fn = ~ c("hydroDia_x", "amp_y"))
  })
)) |>
dplyr::mutate(dplyr::across(
  tidyselect::any_of(c("specDLS_C")),
  \(lcol) purrr::map(lcol, function(df) {
    dplyr::rename_with(df, .cols = c(1, 2), .fn = ~ c("time_x", "amp_y"))
  })
))# |> {
#   \(df) df[["specDLS_C"]][[1]]
# }()
import_dynamicBundle_test <- function(path) {
  sheets <- readxl::excel_sheets(path) |> {
    \(s) s[s != "Sheet1"]
  }() |> rlang::set_names()

  bundle <- suppressMessages(purrr::map(
    sheets,
    \(sheet) readxl::read_xlsx(
      path = path,
      sheet = sheet,
      skip = 2,
      col_types = "numeric",
      .name_repair = "universal"
    )
  ))

  table <- reduce( # iterate through the dfs two at a time..
    list(
      # correlation
      bundle[grepl("Correlation", names(bundle))] |> {
        \(b) rlang::set_names(b,
          nm = stringr::str_extract(names(b), "(?<=\\-)[A-P]\\d+\\-\\d+$")
        )
      }() |>
      dplyr::bind_rows(.id = "uni") |>
      tidyr::nest(specDLS_c = c(2:3)),
      # intensity
      bundle[grepl("Intensity", names(bundle))] |> {
        \(b) rlang::set_names(b,
          nm = stringr::str_extract(names(b), "(?<=\\-)[A-P]\\d+\\-\\d+$")
        )
      }() |>
      dplyr::bind_rows(.id = "uni") |>
      tidyr::nest(specDLS_i = c(2:3)),
      # mass
      bundle[grepl("Mass", names(bundle))] |> {
        \(b) rlang::set_names(b,
          nm = stringr::str_extract(names(b), "(?<=\\-)[A-P]\\d+\\-\\d+$")
        )
      }() |>
      dplyr::bind_rows(.id = "uni") |>
      tidyr::nest(specDLS_m = c(2:3))
    ),
    # ..using join function
    dplyr::left_join,
    by = "uni"
  ) |>
  tidyr::separate(uni, into = c("uni", "temp_C"), sep = "-", convert = TRUE) |>
    dplyr::group_by(uni) |> 
  dplyr::filter(temp_C == min(temp_C)) |>
  dplyr::select(-temp_C) |>
    dplyr::ungroup() |> 
  dplyr::mutate(dplyr::across(
    tidyselect::any_of(c("specDLS_I", "specDLS_M")),
    \(lcol) purrr::map(lcol, function(df) {
      dplyr::rename_with(df, .cols = c(1, 2), .fn = ~ c("hydroDia_x", "amp_y"))
    })
  )) |>
  dplyr::mutate(dplyr::across(
    tidyselect::any_of(c("specDLS_C")),
    \(lcol) purrr::map(lcol, function(df) {
      dplyr::rename_with(df, .cols = c(1, 2), .fn = ~ c("time_x", "amp_y"))
    })
  ))


  return(table)
}
test <- import_dynamicBundle_test("ln/local/Bst2Pol/Exports/General Screen/210615-01-Bst Pol-Gen006R-DLS Bundle.uni-2021-06-17T10-27-43.xlsx")
test
DLSbundle_files <- list.files("ln/local/T4 RNA Ligase/Exports/General Screen/",
  pattern = "DLS Bundle",
  full.names = TRUE
) |> {
  \(l) rlang::set_names(l, nm = map_chr(l, stringr::str_extract, "(?<=//).*(?=.xlsx)"))
}()

DLSbundles <- purrr::map(DLSbundle_files, import_dynamicBundle_test) |> {
  \(l) rlang::set_names(l,
    nm = purrr::map_chr(
      DLSbundle_files,
      stringr::str_extract, "(?<=//).*(?=\\.xlsx)"
    )
  )
}()
DLSbundles


eric-hunt/uncleR documentation built on Dec. 20, 2021, 5:28 a.m.