{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bfaf40b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#! /usr/bin/Rscript\n",
    "\n",
    "#' @title Read mzML files and extract precursor m/z(s)\n",
    "#'\n",
    "#' @description\n",
    "#'\n",
    "#' Spectra package reads files in .mzML format, filtering any empty\n",
    "#' spectrum and ommitting NA values from precurosr m/z list. \n",
    "#' \n",
    "\n",
    "\n",
    "#' @param input_dir is full directory where all MZML input files\n",
    "#'\n",
    "#' @param db is either one of the spectral libraries which can be \n",
    "#'        gnps, hmdb, mbank or all\n",
    "\n",
    "\n",
    "#' @return\n",
    "#' \n",
    "#' This function returns the spectra object and a list of \n",
    "#' precursor m/z(s) present in the .mzML file\n",
    "#' \n",
    "#' \n",
    "#' @author Mahnoor Zulfiqar\n",
    "#' \n",
    "#' @examples\n",
    "#' \n",
    "#' spec_Processing(\"/usr/project/MS2specfile1.mzML\", \"/usr/project/MS2specfile1\")\n",
    "\n",
    "\n",
    "# ---------- Preparations ----------\n",
    "# Load libraries\n",
    "library(\"Spectra\")\n",
    "\n",
    "# ---------- Arguments and user variables ----------\n",
    "args <- commandArgs(trailingOnly=TRUE)\n",
    "#print(args)\n",
    "\n",
    "x <- as.character(args[1])\n",
    "result_dir <- as.character(args[2])\n",
    "\n",
    "# ---------- spec_Processing ----------\n",
    "\n",
    "#' All spectra in mzML files preprocessing, return two outputs, pre-processed MS2 spectra and all precursor masses\n",
    "# x is one mzML file\n",
    "#' All spectra in mzML files preprocessing, return two outputs, pre-processed MS2 spectra and all precursor masses\n",
    "# x is one mzML file\n",
    "spec_Processing <- function(input_dir, x, result_dir){\n",
    "    \n",
    "    input_dir <- dirname(arg[1])\n",
    "    \n",
    "    x <- paste(input_dir, str_remove(x, \".\"), sep = \"\")\n",
    "    \n",
    "    result_dir <- paste(input_dir, str_remove(result_dir, \".\"), sep = \"\")\n",
    "    \n",
    "    if (file.exists(x) && substring(x, nchar(x)) == \"L\"){\n",
    "        if (dir.exists(result_dir)){\n",
    "            # read the spectra\n",
    "            sps_all <- Spectra(x, backend = MsBackendMzR())\n",
    "            #' Change backend to a MsBackendDataFrame: load data into memory\n",
    "            #sps_all <- setBackend(sps_all, MsBackendDataFrame())\n",
    "            #' Filter Empty Spectra\n",
    "            sps_all <- filterEmptySpectra(sps_all)\n",
    "            #' Extract Precursor m/z(s) in each file\n",
    "            pre_mz <- unique(precursorMz(sps_all))\n",
    "            #' Remove any NAs\n",
    "            pre_mz <- na.omit(pre_mz)\n",
    "            export(sps_all, backend = MsBackendMzR(), file = paste(result_dir, \"/processedSpectra.mzML\", sep = \"\"))\n",
    "            write.table(pre_mz, file = paste(result_dir, \"/premz_list.txt\", sep = \"\"), sep = \"/t\", row.names = FALSE, col.names = FALSE)\n",
    "            spsall_pmz <- list(sps_all, pre_mz)\n",
    "            return(spsall_pmz)\n",
    "        }\n",
    "        else{\n",
    "            stop(\"Seems like it is not the result directory of the input .mzML file which is provided as x. Please use the function ms2_rfilename to generate a result directory or create one yourself with the same name as the .mzML input file.\")\n",
    "        }\n",
    "    }\n",
    "    else{\n",
    "        stop(\"Are you sure x is an mzML input file?\")\n",
    "    }\n",
    "    \n",
    "}\n"
   ]
  }
 ],
 "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
}
