{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f11c09bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "library(parallel)\n",
    "library(doParallel)\n",
    "library(future)\n",
    "library(iterators)\n",
    "library(listenv)\n",
    "\n",
    "options(future.globals.maxSize = 8 * 1024^3) # increase dataset size limit taken by future to 8GB\n",
    "\n",
    "# detects number of cores\n",
    "n.cores <- parallel::detectCores()\n",
    "\n",
    "\n",
    "plan(list(\n",
    "  tweak(multisession, workers = ((n.cores + 5) %/% 3) %/% 2),\n",
    "  tweak(multisession, workers = ((n.cores + 5) %/% 3) %/% 2),\n",
    "  tweak(multisession, workers = 3)\n",
    "))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c76c1c2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "start.time <- Sys.time()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4eba95f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ---------- Script ----------\n",
    "# input directory\n",
    "input_dir <- paste(getwd(), \"/data\", sep = \"\")\n",
    "input_dir"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7beca586",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load the functions file\n",
    "source(paste(getwd(), \"/Workflow_R_Functions.r\", sep = \"\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef1c131c",
   "metadata": {},
   "outputs": [],
   "source": [
    "input_table <- data.frame(ms2_rfilename(input_dir))\n",
    "input_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19332e98",
   "metadata": {},
   "outputs": [],
   "source": [
    "input_table_idxs <- listenv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f0ef442",
   "metadata": {},
   "outputs": [],
   "source": [
    "for (i in 1:nrow(input_table)){\n",
    "\n",
    "input_table_idxs[[i]] <- future({\n",
    "    ##Preprocess and Read the mzMLfiles\n",
    "        spec_pr <- spec_Processing(input_dir,\n",
    "                                   input_table[i, \"mzml_files\"],\n",
    "                                   input_table[i, \"ResultFileNames\"])\n",
    "\n",
    "        #perform dereplication with all dbs\n",
    "        df_derep <- spec_dereplication_file(mzml_file = input_table[i, \"mzml_files\"],\n",
    "                                            pre_tbl = paste(input_dir, str_remove(paste(input_table[i, \"ResultFileNames\"], \"/premz_list.txt\", sep = \"\"), \".\"), sep =\"\"),\n",
    "                                            proc_mzml = paste(input_dir, str_remove(paste(input_table[i, \"ResultFileNames\"], \"/processedSpectra.mzML\", sep = \"\"), \".\"), sep =\"\"),\n",
    "                                            db = \"all\",\n",
    "                                            result_dir = input_table[i, \"ResultFileNames\"],\n",
    "                                            file_id = input_table[i, \"File_id\"],\n",
    "                                            input_dir,\n",
    "                                            no_of_candidates = 50,\n",
    "                                            ppmx = 15)\n",
    "        Extract MS2 peak lists\n",
    "        spec_pr2 <- ms2_peaks(pre_tbl = paste(input_dir, str_remove(paste(input_table[i, \"ResultFileNames\"], \"/premz_list.txt\", sep = \"\"), \".\"), sep =\"\"),\n",
    "                              proc_mzml = paste(input_dir, str_remove(paste(input_table[i, \"ResultFileNames\"], \"/processedSpectra.mzML\", sep = \"\"), \".\"), sep =\"\"),\n",
    "                              input_dir,\n",
    "                              result_dir = input_table[i, \"ResultFileNames\"],\n",
    "                             file_id = input_table[i, \"File_id\"])\n",
    "\n",
    "        # camera results for isotopes\n",
    "#         cam_res <- cam_func(input_dir,\n",
    "#                             f = input_table[i, \"mzml_files\"], \n",
    "#                             ms2features = paste(input_dir, str_remove(paste(input_table[i, \"ResultFileNames\"], \"/insilico/MS2DATA.csv\", sep = \"\"), \".\"), sep = \"\"))\n",
    "\n",
    "        # Extract MS1 peaks or isotopic peaks\n",
    "        ms1p <- ms1_peaks(x = paste(input_dir, str_remove(paste(input_table[i, \"ResultFileNames\"],'/insilico/MS2DATA.csv', sep = \"\"), \".\"), sep =\"\"),\n",
    "                          y = NA, \n",
    "                          input_table[i, \"ResultFileNames\"],\n",
    "                          input_dir,\n",
    "                          QCfile = FALSE)\n",
    "\n",
    "\n",
    "        #prepare sirius parameter files\n",
    "        sirius_param_files <- sirius_param(x = paste(input_dir, str_remove(paste(input_table[i, \"ResultFileNames\"],'/insilico/MS1DATA.csv', sep = \"\"), \".\"), sep =\"\"),\n",
    "                                           result_dir = input_table[i, 'ResultFileNames'],\n",
    "                                           input_dir,\n",
    "                                           SL = FALSE)\n",
    "        # Run sirius\n",
    "        run_sirius(files = paste(input_dir, str_remove(paste(input_table[i, \"ResultFileNames\"],'/insilico/MS1DATA_SiriusP.tsv', sep = \"\"), \".\"), sep =\"\"),\n",
    "                   ppm_max = 5,\n",
    "                   ppm_max_ms2 = 15,\n",
    "                   QC = FALSE,\n",
    "                   SL = FALSE,\n",
    "                   SL_path = NA,\n",
    "                   candidates = 30,\n",
    "                  profile = \"orbitrap\",\n",
    "                  db = \"ALL\")\n",
    "   }) #end input_table_idxs future\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecab5b43",
   "metadata": {},
   "outputs": [],
   "source": [
    "input_table_idxs <- as.list(input_table_idxs)\n",
    "v_input_table_idxs <- future::value(input_table_idxs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb2ca800",
   "metadata": {},
   "outputs": [],
   "source": [
    "end.time <- Sys.time()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "553a6aba",
   "metadata": {},
   "outputs": [],
   "source": [
    "time.taken <- end.time - start.time\n",
    "print(time.taken)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "4.2.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
