---
title: "paigeExport"
---

# Gerekli kütüphaneler

```r
library(magrittr)
library(purrr)
library(jsonlite)
```



```r
list.dirs(path = "Y:/", full.names = TRUE, recursive = TRUE)
```

# Slide özellikleri json

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

```r
# 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)
  )

```




```r
# 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)
  )

```





```r
# 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)
  )



```



```r
# 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)
  )

```

```r
df_paige2 <- df_paige2 %>% 
  dplyr::rename(Block = Name,
                Staining = Name.1) 

```

```r
df_paige <- dplyr::bind_rows(df_paige, df_paige2)
```



```r
# slide'lara verilecek yeni isimleri hazırla

df_paige <- df_paige %>% 
  dplyr::mutate(
    slideName = paste0(slideID, ".svs"
                       )
  )
```


```r
df_paige <- df_paige %>% 
  dplyr::select(
    RequestId,
    ExamId,
    Block,
    Staining,
    slideID,
    slideName
    ) %>% 
  dplyr::distinct()

```




```r
df_paige <- df_paige %>%
  dplyr::group_by(RequestId) %>%
  dplyr::mutate(caseNo = paste0("c", dplyr::cur_group_id()))
```


```r
df_paige <- df_paige %>%
  dplyr::group_by(caseNo) %>%
  dplyr::mutate(slide = 1:dplyr::n())
```

```r
df_paige <- df_paige %>%
  dplyr::mutate(newName = paste0(caseNo, "_s", slide,".svs")
  )
```



```r

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)
# 
# }

```

```r
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")


```








```r
# 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)



```


```r
# anonimleştirilen dosyaları etiketle

df_paige$isanonymized[df_paige$newName %in% anonymisedFiles] <- "yes"

```




```r
# 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)

```



```r
|# 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
  )

```






