options(stringsAsFactors=F)
args <- commandArgs(trailingOnly = T)
expr_befor_norm <- args[1]
expr_after_norm <- args[2]
geof_file <- args[3]

suppressMessages(library(DESeq2))
exp<-read.table(file=expr_befor_norm, header = TRUE, row.names = 1, check.names = F)
exp<-as.matrix(exp)
geof <- estimateSizeFactorsForMatrix(exp)
normalize <- function(matrix, f){
  nc <- ncol(matrix)
  if (nc != length(f)) warning('error')
  else {
    for(i in 1:nc) {
      matrix[, i] <- matrix[, i] / f[i]
    }
  }
  return(matrix)
}
# get normalzied expression matrix
geo_exp <- normalize(exp, geof)
write.table(x = geo_exp, file = expr_after_norm, sep = "\t", row.names = T, col.names = T, quote = F)
write.table(x = geof, file = geof_file, sep = "\t", row.names = T, col.names = F, quote = F)
