rm(list = ls())
###############################input data 
dir_path <- "D:\\2023\\2023_1_virus\\1_original_2_virus_for_SM-20230817\\3_sars-cov2-rdrp-followup-3\\"
dir_path_name <- dir(dir_path,pattern = "*.txt",full.names = T)
dir_path_name
## [1] "D:\\2023\\2023_1_virus\\1_original_2_virus_for_SM-20230817\\3_sars-cov2-rdrp-followup-3\\sars-cov2-rdrp-followup-3.txt"
data_1 <- read.delim2(grep("sars-cov2-rdrp-followup-3.txt",dir_path_name,value = T),header = T,stringsAsFactors = F)
dim(data_1) #[1] 128 117
## [1] 128 117
data_1$Max.Resp <- as.numeric(data_1$Max.Resp)
#str(data_1)
data_3 <- data_1$Sample.ID[data_1$Max.Resp < -50]
data_3 <- gsub("-.*", "", data_3)
#data_3
#########################################################
#INPUT DIAGRAM
dir_path_1 <- dir_path 
dir.create(paste0(dir_path_1, "positive\\"))
## Warning in dir.create(paste0(dir_path_1, "positive\\")):
## 'D:\2023\2023_1_virus\1_original_2_virus_for_SM-20230817\3_sars-cov2-rdrp-followup-3\positive'
## already exists
library(magick)
## Linking to ImageMagick 6.9.12.3
## Enabled features: cairo, freetype, fftw, ghostscript, heic, lcms, pango, raw, rsvg, webp
## Disabled features: fontconfig, x11
#?dir
dir_path_name_1 <- dir(dir_path_1, pattern = "*.tif",full.names = T, recursive = T)
#dir_path_name_1
for (i in 1:length(unique(data_3))) {
  tryCatch({
    #i =3
    print(i)
    image_1 <- image_read(grep(unique(data_3)[i], dir_path_name_1, value = T))
    print(image_1)    
    image_2 <- image_trim(image_1)
    # Resize
    #image_3 <- image_scale(image_2, "700") # width: 300px
    #image_4 <- image_resize(image_2, "1000x1000")
    #print(image_3)  
    image_write(image_2, path = paste0(dir_path_1, "positive\\",unique(data_3)[i], ".tiff"),
                format="tiff",density = 1,quality = 100)
    
  }, error = function(e) {
    cat("ERROR :",conditionMessage(e), "\n")
    cat("ERROR :", conditionMessage(e),"---",i,"---",gsub("\\:","-",Sys.time()),file = "error.txt", append = TRUE, "\n")
    #cat(message('** ERR at ', Sys.time(), " **"),file = "test_1.txt", append = TRUE)
    #print(e)
  },
  warning = function(w){
    cat("Warning :", conditionMessage(w),"---",i,"---",gsub("\\:","-",Sys.time()),file = "warning.txt", append = TRUE, "\n") 
    # print(w)
  })
}
## [1] 1
##   format width height colorspace matte filesize density
## 1   TIFF  1347    957       sRGB FALSE   202184     1x1
## 2   TIFF  1347    957       sRGB FALSE   202184     1x1
## 3   TIFF  1347    957       sRGB FALSE   202184     1x1
## 4   TIFF  1417   1003       sRGB FALSE    51792 300x300
## [1] 2
##   format width height colorspace matte filesize density
## 1   TIFF  1347    957       sRGB FALSE   184934     1x1
## 2   TIFF  1347    957       sRGB FALSE   184934     1x1
## 3   TIFF  1347    957       sRGB FALSE   184934     1x1
## 4   TIFF  1417   1003       sRGB FALSE    48436 300x300
## [1] 3
##   format width height colorspace matte filesize density
## 1   TIFF  1347    957       sRGB FALSE   187466     1x1
## 2   TIFF  1347    957       sRGB FALSE   187466     1x1
## 3   TIFF  1347    957       sRGB FALSE   187466     1x1
## 4   TIFF  1417   1003       sRGB FALSE    47504 300x300
## [1] 4
##   format width height colorspace matte filesize density
## 1   TIFF  1347    957       sRGB FALSE   209156     1x1
## 2   TIFF  1347    957       sRGB FALSE   209156     1x1
## 3   TIFF  1347    957       sRGB FALSE   209156     1x1
## 4   TIFF  1417   1003       sRGB FALSE    53738 300x300
## [1] 5
##   format width height colorspace matte filesize density
## 1   TIFF  1347    957       sRGB FALSE   201254     1x1
## 2   TIFF  1347    957       sRGB FALSE   201254     1x1
## 3   TIFF  1347    957       sRGB FALSE   201254     1x1
## 4   TIFF  1417   1003       sRGB FALSE    51138 300x300
## [1] 6
##   format width height colorspace matte filesize density
## 1   TIFF  1347    957       sRGB FALSE   178016     1x1
## 2   TIFF  1347    957       sRGB FALSE   178016     1x1
## 3   TIFF  1347    957       sRGB FALSE   178016     1x1
## 4   TIFF  1417   1003       sRGB FALSE    45068 300x300
## [1] 7
##   format width height colorspace matte filesize density
## 1   TIFF  1347    957       sRGB FALSE   185972     1x1
## 2   TIFF  1347    957       sRGB FALSE   185972     1x1
## 3   TIFF  1347    957       sRGB FALSE   185972     1x1
## 4   TIFF  1417   1003       sRGB FALSE    48770 300x300
## [1] 8
##   format width height colorspace matte filesize density
## 1   TIFF  1347    957       sRGB FALSE   193016     1x1
## 2   TIFF  1347    957       sRGB FALSE   193016     1x1
## 3   TIFF  1347    957       sRGB FALSE   193016     1x1
## 4   TIFF  1417   1003       sRGB FALSE    49138 300x300
## [1] 9
##   format width height colorspace matte filesize density
## 1   TIFF  1347    957       sRGB FALSE   195320     1x1
## 2   TIFF  1347    957       sRGB FALSE   195320     1x1
## 3   TIFF  1347    957       sRGB FALSE   195320     1x1
## 4   TIFF  1417   1003       sRGB FALSE    50788 300x300
## [1] 10
##   format width height colorspace matte filesize density
## 1   TIFF  1347    957       sRGB FALSE   211730     1x1
## 2   TIFF  1347    957       sRGB FALSE   211730     1x1
## 3   TIFF  1347    957       sRGB FALSE   211730     1x1
## 4   TIFF  1417   1003       sRGB FALSE    53018 300x300
## [1] 11
##   format width height colorspace matte filesize density
## 1   TIFF  1347    957       sRGB FALSE   180110     1x1
## 2   TIFF  1347    957       sRGB FALSE   180110     1x1
## 3   TIFF  1347    957       sRGB FALSE   180110     1x1
## 4   TIFF  1417   1003       sRGB FALSE    46700 300x300
## [1] 12
##   format width height colorspace matte filesize density
## 1   TIFF  1347    957       sRGB FALSE   182540     1x1
## 2   TIFF  1347    957       sRGB FALSE   182540     1x1
## 3   TIFF  1347    957       sRGB FALSE   182540     1x1
## 4   TIFF  1417   1003       sRGB FALSE    46298 300x300
## [1] 13
##   format width height colorspace matte filesize density
## 1   TIFF  1347    957       sRGB FALSE    71176     1x1
## [1] 14
##   format width height colorspace matte filesize density
## 1   TIFF  1347    957       sRGB FALSE   197882     1x1
## 2   TIFF  1347    957       sRGB FALSE   197882     1x1
## 3   TIFF  1347    957       sRGB FALSE   197882     1x1
## 4   TIFF  1417   1003       sRGB FALSE    51388 300x300
## [1] 15
##   format width height colorspace matte filesize density
## 1   TIFF  1347    957       sRGB FALSE   202694     1x1
## 2   TIFF  1347    957       sRGB FALSE   202694     1x1
## 3   TIFF  1347    957       sRGB FALSE   202694     1x1
## 4   TIFF  1417   1003       sRGB FALSE    51490 300x300
## [1] 16
##   format width height colorspace matte filesize density
## 1   TIFF  1347    957       sRGB FALSE   188648     1x1
## 2   TIFF  1347    957       sRGB FALSE   188648     1x1
## 3   TIFF  1347    957       sRGB FALSE   188648     1x1
## 4   TIFF  1417   1003       sRGB FALSE    48172 300x300