paigeExport

Gerekli kütüphaneler

library(magrittr)
library(purrr)
library(jsonlite)
list.dirs(path = "Y:/", full.names = TRUE, recursive = TRUE)

Slide özellikleri json

Her slide ile birlikte aynı isimde bir .info.json dosyası export ediliyor.

# json files listesini getir 
json_files_paige <- list.files(
  path = "Y:/paige/",
  all.files = TRUE,
  full.names = TRUE,
  recursive = TRUE,
  pattern = ".info.json"
)

# slide isimlerini ekle

paige <- as.data.frame(json_files_paige) %>% 
  dplyr::rename(fullName = json_files_paige) %>% 
  dplyr::mutate(
    fileName = gsub(pattern = "Y:/paige/", replacement = "", x = fullName)
  ) %>% 
  dplyr::mutate(
    fileName = gsub(pattern = ".svs.info.json", replacement = "", x = fileName)
  )
# her json dosyasını oku, içindekileri ayır ve dosya ismiyle eşleştir

df_paige <-  
  purrr::map2_df(
    .x = paige$fullName,
    .y = paige$fileName,
    .f = ~ jsonlite::fromJSON(.x,
                              flatten = TRUE) %>% 
      as.data.frame() %>% 
      dplyr::mutate(slideID = .y)
  )
# json files listesini getir 
json_files_paige2 <- list.files(
  path = "Y:/paige/",
  all.files = TRUE,
  full.names = TRUE,
  recursive = TRUE,
  pattern = ".svs.import"
)

# slide isimlerini ekle

paige2 <- as.data.frame(json_files_paige2) %>% 
  dplyr::rename(fullName = json_files_paige2) %>% 
  dplyr::mutate(
    fileName = gsub(pattern = "Y:/paige/", replacement = "", x = fullName)
  ) %>% 
  dplyr::mutate(
    fileName = gsub(pattern = ".svs.import", replacement = "", x = fileName)
  )

# her json dosyasını oku, içindekileri ayır ve dosya ismiyle eşleştir

df_paige2 <-  
  purrr::map2_df(
    .x = paige2$fullName,
    .y = paige2$fileName,
    .f = ~ jsonlite::fromJSON(.x,
                              flatten = TRUE) %>% 
      as.data.frame() %>% 
      dplyr::mutate(slideID = .y)
  )
df_paige2 <- df_paige2 %>% 
  dplyr::rename(Block = Name,
                Staining = Name.1) 
df_paige <- dplyr::bind_rows(df_paige, df_paige2)
# slide'lara verilecek yeni isimleri hazırla

df_paige <- df_paige %>% 
  dplyr::mutate(
    slideName = paste0(slideID, ".svs"
                       )
  )
df_paige <- df_paige %>% 
  dplyr::select(
    RequestId,
    ExamId,
    Block,
    Staining,
    slideID,
    slideName
    ) %>% 
  dplyr::distinct()
df_paige <- df_paige %>%
  dplyr::group_by(RequestId) %>%
  dplyr::mutate(caseNo = paste0("c", dplyr::cur_group_id()))
df_paige <- df_paige %>%
  dplyr::group_by(caseNo) %>%
  dplyr::mutate(slide = 1:dplyr::n())
df_paige <- df_paige %>%
  dplyr::mutate(newName = paste0(caseNo, "_s", slide,".svs")
  )

newPath <- "Y:/paige_anonym/"

# dir.create(path = newPath)


# for (i in 1:dim(df_paige)[1]) {
# 
#   oldFilePath <- paste0("Y:/paige/", df_paige[i, "slideName"])
# 
#   newFilePath <- paste0(newPath, df_paige[i, "newName"])
# 
#   file.copy(from = oldFilePath, to = newFilePath)
# 
# }
openxlsx::write.xlsx(
  x = df_paige,
  file = paste0("./anonym/", "df_paige.xlsx"),
  asTable = TRUE,
  overwrite = TRUE,
  keepNA = TRUE
)

saveRDS(df_paige, file = "./anonym/df_paige.RDS")
# anonimleştirilen dosyaları tespit et

paige_anonym <- list.files(
  path = "Y:/paige_anonym/",
  all.files = TRUE,
  full.names = FALSE,
  recursive = TRUE,
  pattern = "_anonym.svs"
)

anonymisedFiles <- gsub(pattern = newPath, replacement = "", x = paige_anonym)
anonymisedFiles <- gsub(pattern = "_anonym", replacement = "", x = anonymisedFiles)

# anonimleştirilen dosyaları etiketle

df_paige$isanonymized[df_paige$newName %in% anonymisedFiles] <- "yes"
# Yüklemeye hazır dosyaları ayrı klasöre taşı

newPath <- "Y:/paige_readyToUpload/"

# dir.create(path = newPath)

paige_anonym <- list.files(
  path = "Y:/paige_anonym/",
  all.files = TRUE,
  full.names = TRUE,
  recursive = TRUE,
  pattern = "_anonym.svs"
)

fs::file_move(path = paige_anonym,
              new_path = newPath)
|# eval=FALSE,include=FALSE,echo=FALSE

df_all <- 

    purrr::map_dfr(.x = list.files(path = "./anonym/", pattern = ".RDS", full.names = TRUE),
                   .f = readRDS,
                   .id = "id")

mean(is.na(df_all$isanonymized))

openxlsx::write.xlsx(
    x = df_all,
    file = paste0("./anonym/", "df_all.xlsx"),
    asTable = TRUE,
    overwrite = TRUE,
    keepNA = TRUE
  )