{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba536ea0",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "#!/Users/mahnoorzulfiqar/opt/anaconda3/envs/mawRpy/bin/Rscript\n",
    "\n",
    "#' @title Result Directories\n",
    "#'\n",
    "#' @description\n",
    "#'\n",
    "#' It lists all files with .mzML extension.\n",
    "#' These files are without the full path, so the input_dir is added to \n",
    "#' the path to these files. For each .mzML file, the extension is removed \n",
    "#' and stored as a name for the results directory for each input .mzML \n",
    "#' file For each input .mzML file an id is generated like file_1\n",
    "#' A table is generated at the end\n",
    "\n",
    "\n",
    "#' @param input_dir is full directory where all MZML input files\n",
    "#'\n",
    "\n",
    "\n",
    "\n",
    "#' @return\n",
    "#' \n",
    "#' A table of mzml_files, ResultFileNames, File_id columns. \n",
    "#' Result files for each input .mzML is generated.\n",
    "#' \n",
    "\n",
    "#' @author Mahnoor Zulfiqar\n",
    "#' \n",
    "#' @examples\n",
    "#' \n",
    "#' ms2_rfilename(input_dir = \"/usr/project/\")\n",
    "\n",
    "\n",
    "# ---------- Preparations ----------\n",
    "\n",
    "# Load Libraries\n",
    "library(stringr)\n",
    "\n",
    "\n",
    "# ---------- Arguments and user variables ----------\n",
    "args <- commandArgs(trailingOnly=TRUE)\n",
    "\n",
    "\n",
    "input_dir <- as.character(args[1])\n",
    "\n",
    "\n",
    "## Specifying a function for creating result directories for each input mzml\n",
    "# input for the function:\n",
    "# input directory\n",
    "ms2_rfilename<- function(input_dir){\n",
    "    \n",
    "    input_dir <- getwd()\n",
    "    \n",
    "    if (dir.exists(input_dir)){\n",
    "        #list_ms2_files <- intersect(list.files(input_dir, pattern = \"_PRM_\"), list.files(input_dir, pattern = \".mzML\"))\n",
    "        list_ms2_files <- list.files(input_dir, pattern = \".mzML\")\n",
    "        mzml_file <- paste(input_dir, \"/\", list_ms2_files, sep = \"\")\n",
    "\n",
    "        #store the result file names to return to this function as output\n",
    "        mzml_files <- c()\n",
    "        ResultFileNames <- c()\n",
    "        File_id <- c()\n",
    "        nx <- 0\n",
    "        # x is mzML files\n",
    "        for (i in 1:length(mzml_file)){\n",
    "            nx <- nx+1\n",
    "            # remove .mzML to extract just the names\n",
    "            mzml_filex <- str_replace(mzml_file[i], input_dir, \".\")\n",
    "            name_mzmls <- str_remove(as.character(mzml_filex), \".mzML\")\n",
    "            name_mzmlsd <- str_remove(mzml_file[i], \".mzML\")\n",
    "            if (!file.exists(name_mzmlsd)){\n",
    "                dir.create(name_mzmlsd) ##create folder\n",
    "            }\n",
    "            ResultFileNames<- c(ResultFileNames, name_mzmls)\n",
    "            mzml_files <- c(mzml_files, mzml_filex)\n",
    "            File_id <- c(File_id, paste(\"file_\",nx, sep = \"\"))\n",
    "        }\n",
    "        input_table <- cbind(mzml_files, ResultFileNames, File_id)\n",
    "\n",
    "        write.csv(input_table, paste(input_dir, \"/input_table.csv\", sep = \"\"))\n",
    "        return(data.frame(input_table))\n",
    "    }\n",
    "    else{\n",
    "        stop(\"Your input_dir is incorrect. Please provide the directory where all your input files are stored. An example would be: '/Users/my_name/input_dir'. Good Luck\")\n",
    "    }\n",
    "}\n",
    "\n",
    "ms2_rfilename(input_dir)"
   ]
  }
 ],
 "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
}
