# Necessary libraries
suppressPackageStartupMessages(library(LFQbench))

# Parse the variables out of the command line, if called from command line
command_args <- commandArgs(trailingOnly = FALSE)
script_args <- commandArgs(trailingOnly = TRUE)
# print(command_args)
file.arg.name <- "--file="

if (length(script_args) > 0 && length(grep(file.arg.name, command_args)) > 0) {
  script_file <- sub(file.arg.name, "", command_args[grep(file.arg.name, command_args)])
  script_dir <- dirname(script_file)
  if (script_dir != "") {
    script_dir <- paste(script_dir, "/", sep = "")
  }
  
  working_dir <- script_args[1]
  data_dir <- script_args[2]
  print(working_dir)
} else {
  # otherwise, use this hardcoded working directory
  working_dir <- "C:/DIA_Course/Data/DIA_data/QE/qep_19w_ppm10_rt10"
  data_dir <- "QE"
}

# Human, Yeast, E. coli composition
sampleComposition = data.frame( species = c("HUMAN", "YEAST", "ECOLI"), 
                                A = c( 65, 15, 20 ), 
                                B = c( 65, 30, 05 ) )


if (data_dir == "QE") {
  
  # Data sets being processed - can specify more in this structure in the future
  dataSets = data.frame( "HYE124_TTOF6600_64var" = c( 
    "collinsb_X1803_171", "collinsb_X1803_173", "collinsb_X1803_175" # A
    ,
    "collinsb_X1803_172", "collinsb_X1803_174", "collinsb_X1803_176" # B 
  ),
  row.names = c( "A1", "A2", "A3", "B1", "B2", "B3" ) )

} else {  # TTOF

  # Data sets being processed - can specify more in this structure in the future
  dataSets = data.frame( "HYE124_TTOF6600_64var" = c( 
    "collinsb_I180316_001_SW-A", "collinsb_I180316_003_SW-A", "collinsb_I180316_005_SW-A" # A 
    ,
    "collinsb_I180316_002_SW-B", "collinsb_I180316_004_SW-B", "collinsb_I180316_006_SW-B" # B
  ),
  row.names = c( "A1", "A2", "A3", "B1", "B2", "B3" ) )
}

# Species tags from the FASTA sequence names                       
speciesTags = list( HUMAN = "_HUMAN", YEAST = "_YEAS", ECOLI = "_ECOLI" )

# Init configurations
LFQbench.initConfiguration( SampleComposition = sampleComposition )
FSWE.initConfiguration( injectionNames = dataSets, speciesTags = speciesTags )
LFQbench.setDataRootFolder( rootFolder = working_dir, createSubfolders = T )

# Generate reports and plots
FSWE.generateReports("SWATHbenchmark.csv",
              softwareSource = "Skyline",
              keep_original_names = T,
              singleHits = F,
              plotHistogram = T,
              plotHistNAs = T,
              reportSequences = F)
nye123.res = LFQbench.batchProcessRootFolder()
