{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "#  __Let's doing the same mapping with each sample sequenced in illumina__\n",
    "We have 18 samples in the SHORT_READS directory. For each sample, we want to perform all the steps to obtain the last bam used to call SNPs.\n",
    "\n",
    "The trick here is to use a __`for` loop in bash__, in which all the unique commands to be carried out for a sample are grouped together. And if we have 3 samples, these commands will be executed 3 times, for example. We cans\n",
    "\n",
    "We will use a for loop : \n",
    "* first with two samples\n",
    "* secondly with the 18 samples : 1613 1868 4752 5417 685 716 A1 A8 B1 B218 B8 C218 C2 D119 E2 E318 G11 H44\n",
    "      \n",
    "    \n",
    "### Before starting, remove all the files generated in the results directory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# PUT COMMAND"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Let's try our first for loop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "for sample in 1613 1868 \n",
    "  do\n",
    "    echo \"$sample\"\n",
    "  done;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "for sample in 1613 1868 \n",
    "  do\n",
    "    echo \"---> I can start a series of instructions for the same sample :\"\n",
    "    echo -e \"\\t Here the sample **${sample}** \\n\"\n",
    "  done;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Gradually add each analysis step:\n",
    "* mapping with `bwa mem`\n",
    "* then running `samtools flagstat` \n",
    "* then filtering with `samtools view` and `samtools index`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "for sample in 1613 1868\n",
    "  do\n",
    "    echo -e \"###################### ${sample} \"\n",
    "    \n",
    "    echo -e \"#### Mapping \"\n",
    "    # PUT COMMAND WITH BWA MEM COMMAND\n",
    "    \n",
    "    echo -e \"### Stat \"\n",
    "    # PUT COMMAND WITH SAMTOOLS FLAGSTAT COMMAND\n",
    "    \n",
    "    echo -e \"### Filtering\"\n",
    "    # PUT COMMAND WITH SAMTOOLS VIEW TO FILTER BAM\n",
    "    \n",
    "    echo -e \"### Indexing bam\"\n",
    "    # PUT COMMAND WITH SAMTOOLS INDEX\n",
    "    \n",
    "\n",
    "    done;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### List the content of the `RESULTS` directory - `ls -lht`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# PUT COMMAND"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "-----------\n",
    "#### __BONUS__... only if you have the time to write more \"complex\" code...  \n",
    "We want to group all the files generated by the same software for each sample in a single :\n",
    "* the `1_bam_raw` directory that contains all bam files\n",
    "* the `2_bam_flagstat` directory that contains all files generated by `samtools flagstat`\n",
    "* the `3_bam_filtered` directory that contains all filtered bam files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#### ADDING FILE TREE\n",
    "res_dir=\"/home/jovyan/SV_DATA/RESULTS\"\n",
    "bam_raw_dir=\"${res_dir}/1_bam_raw\"\n",
    "bam_stat_dir=\"${res_dir}/2_bam_flagstat\"\n",
    "bam_filtered_dir=\"${res_dir}/3_bam_filtered\"\n",
    "\n",
    "data_dir=\"/home/jovyan/SV_DATA\"\n",
    "ref=\"${data_dir}/REF/GCA_002220235.1_ASM222023v1_genomic.fna\"\n",
    "\n",
    "cd $res_dir\n",
    "mkdir -p $bam_raw_dir\n",
    "mkdir -p $bam_stat_dir\n",
    "mkdir -p $bam_filtered_dir\n",
    "\n",
    "for sample in 1613 1868\n",
    "  do\n",
    "  \n",
    "    fq1=\"${data_dir}/SHORT_READS/${sample}_R1.fastq.gz\"\n",
    "    fq2=\"${data_dir}/SHORT_READS/${sample}_R2.fastq.gz\"\n",
    "    \n",
    "    echo -e \"#### Mapping \"\n",
    "    # PUT COMMAND WITH BWA MEM COMMAND\n",
    "    \n",
    "    echo -e \"### Stat \"\n",
    "    # PUT COMMAND WITH SAMTOOLS FLAGSTAT COMMAND\n",
    "    \n",
    "    echo -e \"### Filtering\"\n",
    "    # PUT COMMAND WITH SAMTOOLS VIEW TO FILTER BAM\n",
    "    \n",
    "    echo -e \"### Indexing bam\"\n",
    "    # PUT COMMAND WITH SAMTOOLS INDEX\n",
    "    \n",
    "    done;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### List recursively the content of the `RESULTS` directory - `ls -R`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ls -lRh /home/jovyan/SV_DATA/RESULTS "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Let's use the loop on all samples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "for sample in 1613 1868 4752 5417 685 716 A1 A8 B1 B218 B8 C218 C2 D119 E2 E318 G11 H44\n",
    "  do\n",
    "    # WRITE CODE   \n",
    "  done;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### List recursively the content of the `RESULTS` directory - `ls -R`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# PUT COMMAND"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "## SNPs calling using all bams just generated"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# PUT COMMAND"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Count the number of variants with `bcftools stat`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# PUT COMMAND"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Bash",
   "language": "bash",
   "name": "bash"
  },
  "language_info": {
   "codemirror_mode": "shell",
   "file_extension": ".sh",
   "mimetype": "text/x-sh",
   "name": "bash"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
