{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "55fdf449",
   "metadata": {},
   "source": [
    "# Structural Variants Detection by using long reads \n",
    "\n",
    "#### Structural variation with `Sniffles` (LR)__ \n",
    "\n",
    "Sniffles is a structural variation caller using third generation sequencing (PacBio or Oxford Nanopore). It detects all types of SVs (10bp+) using evidence from split-read alignments, high-mismatch regions, and coverage analysis.\n",
    "\n",
    "https://github.com/fritzsedlazeck/Sniffles\n",
    "\n",
    "In the following exercices, we will :\n",
    "* map the long reads of the `5417` and `A8` samples against the reference genome `GCA_002220235.1_ASM222023v1_genomic`\n",
    "* call SV and create a ?snf file for each sample\n",
    "* merge calling using the snf files into a single .vcf \n",
    "\n",
    "------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a09d637d-b5e4-4f2d-bba3-858703d906d9",
   "metadata": {},
   "source": [
    "#### __Preparing data before mapping__\n",
    "\n",
    "* Go into the `LONG_READS` directory\n",
    "* List recursively the LONG_READS directory \n",
    "* For each sample, merge all fastq files into a single fastq file `all_LR_A8.fastq.gz` and `all_LR_5417.fastq.gz` in the `LONG_READS` directory - `zcat`\n",
    "\n",
    "#### `5417`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de2573e3-c27a-4e00-91ae-ff194db7e8e9",
   "metadata": {},
   "outputs": [],
   "source": [
    " #\n",
    " cd ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16cf06d8-97fd-4331-87c6-48dc56f23b65",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Merging fastq\n",
    "zcat ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f276b07c-e9c9-4814-ba00-0be387396020",
   "metadata": {},
   "outputs": [],
   "source": [
    "gzip ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "091bbfd4-d151-4bd7-bcff-e56c6ed8d20b",
   "metadata": {},
   "outputs": [],
   "source": [
    "ls"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d116fd02-02d6-4430-8e20-02c531b07f0f",
   "metadata": {},
   "source": [
    "#### `A8`\n",
    "\n",
    "Try to create the fastq compressed in a single command"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a98eb800-ed10-4104-b59b-f34e65dc4b23",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "f705bb76-aca3-4179-85b7-2d4ec1df67db",
   "metadata": {},
   "source": [
    "#### __Mapping Long Reads against the reference genome__\n",
    "\n",
    "* Go into the`RESULTS` directory and create the directory `SV_CALLING`\n",
    "* Perform the mapping with `minimaps2` https://github.com/lh3/minimap2\n",
    "* Sort the bam file with `samtools sort` and index the bam file created\n",
    "\n",
    "```\n",
    "minimap2 -ax map-ont -t 8 --MD -R '@RG\\tID:SAMPLE_ID\\tSM:SAMPLE_ID'  REF_FILE FASTQ_FILE > SAM_FILE\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98bf89bf-e4e8-46ef-9d52-d96e5ebbcaff",
   "metadata": {},
   "source": [
    "#### Mapping for the `5417` sample"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44a9e60f-c749-4145-a568-6b0703b748e2",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "minimap2  ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b7d2e0e-aa2a-4624-be61-eaa213da0bc0",
   "metadata": {},
   "outputs": [],
   "source": [
    "samtools sort ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a2d55407-bb4b-45be-a05c-3cad455e48d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "samtools index ..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "779fc9ec-910d-46d8-a7b1-b36d28e2111c",
   "metadata": {},
   "source": [
    "#### Mapping for the `A8`sample\n",
    "\n",
    "You can do it with 2 commands only (mapping + bam index) !"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2f95542-faee-4c31-a8b0-db5940afebb2",
   "metadata": {},
   "outputs": [],
   "source": [
    "minimap2 -..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89e9a844-650d-470e-b843-3bc250d549b0",
   "metadata": {},
   "source": [
    "#### __SV calling for each sample with `sniffle`__\n",
    "\n",
    "```\n",
    "sniffles --input sample1.bam --snf sample1.snf\n",
    "```\n",
    "\n",
    "#### `5417`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c8acd8c-ea5e-4e57-8d09-9fb4367641a3",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "sniffles .."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa631a5f-32ec-46f4-a862-cae217a38cc0",
   "metadata": {},
   "outputs": [],
   "source": [
    "ls -lrt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72d8496e-8f51-4e89-beb6-f5f09e25140b",
   "metadata": {},
   "source": [
    "`A8`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4a383dd-4e77-40ad-970e-c0dcba5adbe9",
   "metadata": {},
   "outputs": [],
   "source": [
    "sniffles ..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c59e4b2-03c2-4374-ac1f-f2481b13125b",
   "metadata": {},
   "source": [
    "#### __Merging SV calling  with `sniffle`__\n",
    "\n",
    "```\n",
    "sniffles --input snf_files_list.tsv --vcf multisample.vcf\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c370c87-0547-4d94-9ede-930a27331937",
   "metadata": {},
   "outputs": [],
   "source": [
    "sniffles ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f68756d-92fe-4929-8200-66fc4e540c81",
   "metadata": {},
   "outputs": [],
   "source": [
    "ls"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9a8a616",
   "metadata": {
    "tags": []
   },
   "source": [
    "#### __Have a look on the VCF file__ "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95a0b720",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "ec9e8ab7-2b9a-407e-8b0e-b477f964a096",
   "metadata": {},
   "source": [
    "#### __Count the number of SVs__ `bftools stats`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18de1869-8010-419e-869e-3ba3c1c00485",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "bcftools stats multisample.vcf.gz | head -n30"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66694f69",
   "metadata": {},
   "source": [
    "# Crossing informations between SNP (illumina data) and SV - `bedtools intersect` or `intersectBed`\n",
    "    \n",
    "    https://bedtools.readthedocs.io/en/latest/content/tools/intersect.html\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24b25459",
   "metadata": {},
   "source": [
    "* Create a directory VARIANTS_COMPARISON \n",
    "* Count how many SNPs detectd with bcftools are within each SV detected by SNIFFLE\n",
    "* Extract all SNPs within each SVs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "add13c89",
   "metadata": {},
   "source": [
    "#### count SNP number within each SV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b645e71c",
   "metadata": {},
   "outputs": [],
   "source": [
    "bedtools intersect ..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e738f724",
   "metadata": {},
   "source": [
    "#### extract all SNPs within each SV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8538f10f",
   "metadata": {},
   "outputs": [],
   "source": [
    "bedtools intersect  ..."
   ]
  }
 ],
 "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": 5
}
