{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6b2f79a7-279a-40a7-af36-556f2eba2b40",
   "metadata": {},
   "source": [
    "# Project I: Pathogenicity"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e3ac9cd-55ab-40cc-8f7a-28fc8975bef2",
   "metadata": {},
   "source": [
    "## Approach 1 / NCBI datasets: Download and rename genomes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "174c31fe-fb95-41bc-ac8d-f4d2995a7627",
   "metadata": {},
   "outputs": [],
   "source": [
    "wget -c 'https://ftp.ncbi.nlm.nih.gov/pub/datasets/command-line/v2/linux-amd64/datasets'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c920dee9-d8d4-45be-bf20-e861e230d33a",
   "metadata": {},
   "outputs": [],
   "source": [
    "chmod u+x datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76bde3ac-3ac1-4c92-82d9-f95238f7da8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "./datasets"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ab1313c-91c4-4600-b9e6-7e064d69253a",
   "metadata": {},
   "source": [
    "### Load proteomes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5eabda65-0d95-4eb9-bd1a-446d57632f3d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "for i in GCF_000005845.2 GCF_000008865.2; do ./datasets download genome accession \\\n",
    "$i --include protein --filename $i.zip; done"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da90e753-702f-4b54-ba46-731e4b4eaf95",
   "metadata": {},
   "outputs": [],
   "source": [
    "unzip -o GCF_000005845.2.zip # -o -> overwrite"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2ba74b1-cb4c-4d84-b9bd-3ad6d1f0b5ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "ls -l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6360600-e1fd-44ea-b6ac-e3ff53013b8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "ls ./ncbi_dataset/data/GCF_000005845.2/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4bbc643-9112-4fd3-829f-ccb7e141b0e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "cp ./ncbi_dataset/data/GCF_000005845.2/protein.faa ec-k12.fasta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b507eed-e36c-4468-9284-58225b1db96b",
   "metadata": {},
   "outputs": [],
   "source": [
    "unzip -o GCF_000008865.2.zip\n",
    "cp ./ncbi_dataset/data/GCF_000008865.2/protein.faa ec-h7.fasta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "62dce8d9-d0d3-4c1c-af50-c02b59ba92ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "grep -c \">\" ec*.fasta"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62d99d17-0023-4172-91ff-6b681e4d0062",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## Approach 2 / wget: Download and rename genomes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f8c832a-edc6-4516-ae2d-005752f9ceb8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# E.coli O157:H7\n",
    "wget https://ftp.ncbi.nlm.nih.gov/genomes/all/GCF/000/008/865/GCF_000008865.2_ASM886v2/GCF_000008865.2_ASM886v2_protein.faa.gz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5e9d9e9-bdba-43bd-9130-0b105d3b6204",
   "metadata": {},
   "outputs": [],
   "source": [
    "gunzip ./GCF_000008865.2_ASM886v2_protein.faa.gz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96f29a98-193b-4281-a1f7-1afb32d37287",
   "metadata": {},
   "outputs": [],
   "source": [
    "mv ./GCF_000008865.2_ASM886v2_protein.faa ecoli_h7.faa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1d3d978-af4e-4efd-9d6a-3f8e60163e25",
   "metadata": {},
   "outputs": [],
   "source": [
    "# E.coli K12\n",
    "wget https://ftp.ncbi.nlm.nih.gov/genomes/all/GCF/000/005/845/GCF_000005845.2_ASM584v2/GCF_000005845.2_ASM584v2_protein.faa.gz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0294a9ee-62f6-4bf6-b75e-1933fe98cdce",
   "metadata": {},
   "outputs": [],
   "source": [
    "gunzip ./GCF_000005845.2_ASM584v2_protein.faa.gz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b53e3d38-e111-435d-99bb-301ce56d73b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "mv ./GCF_000005845.2_ASM584v2_protein.faa ecoli_k12.faa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ec7fff6-f4ae-431a-ac9c-4bcf14ed7d0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "head -2 *.faa"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f40da420-15cb-4735-8040-067a2908daab",
   "metadata": {},
   "source": [
    "## Create BLAST database"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70c7f574-fc4b-46bd-9704-80f7033c49d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "makeblastdb -in ec-k12.fasta -dbtype prot -title \"Escherichia coli K12\" -out ecolik12 -parse_seqids"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29c3ec4f-736f-47e1-be51-bf700d98458b",
   "metadata": {},
   "source": [
    "## BLAST"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a85abe2e-33c9-4f58-a7b0-eecb22cd88c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "time blastp -db ecolik12 -query ec-h7.fasta -out h7vsk12.txt -evalue .00001"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4520c86-ebba-49c2-8881-51fb0faa0103",
   "metadata": {},
   "outputs": [],
   "source": [
    "ls -lh ec-* h7*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8fe3b6b7-374e-4bc4-934a-eaea9ce95a14",
   "metadata": {},
   "outputs": [],
   "source": [
    "wc -l h7vsk12.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34cad6e5-3da7-4cd4-8940-968bbad1ec13",
   "metadata": {},
   "source": [
    "## Processing BLAST results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b4a8d588-3a3e-4cdf-b5aa-fb4720127976",
   "metadata": {},
   "outputs": [],
   "source": [
    "awk '/Query=/ || /No hits/{print $0}' h7vsk12.txt | \n",
    "awk '{line[NR]=$0; if($0~/No hits/){print line[NR-1]}}' | \n",
    "egrep -v \"([Uu]nknown| [Pp]utative|[Hh]ypothetical|[Uu]ncharacterized)\" | head -20"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39ec1deb-0063-432a-9fc2-7b54944c342f",
   "metadata": {},
   "source": [
    "# Project II: Redox regulation"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65e17bfb-9a5c-4352-87d5-2183ce981ff9",
   "metadata": {},
   "source": [
    "## Download structures"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d55ac3da-3f5b-440c-b014-7022ed0d619c",
   "metadata": {},
   "outputs": [],
   "source": [
    "wget 'https://files.rcsb.org/download/1FRF.pdb'\n",
    "wget 'https://files.rcsb.org/download/1FRV.pdb'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d929d995-952f-40f6-8b7b-d85a66ee99ee",
   "metadata": {},
   "source": [
    "## Visual Inspection\n",
    "\n",
    "Run [Jmol](https://jmol.awkologist.de) and use the following commands:\n",
    "```\n",
    "load =1FRF\n",
    "select all\n",
    "spacefill 50\n",
    "select cys and sulfur\n",
    "color yellow\n",
    "spacefill 250\n",
    "monitor 3914 5273\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53fb5e3d-92cb-44c3-bcaa-849c12ea73cc",
   "metadata": {},
   "source": [
    "## Computational Inspection\n",
    "### I) Manually\n",
    "Requires the following script:\n",
    "```\n",
    "# save as distance.awk\n",
    "# searches close cysteine sulfur atoms in a structure\n",
    "# requires a structure file (*.pdb)\n",
    "# usage: awk -f distance.awk structure.pdb\n",
    "\n",
    "BEGIN{print \"Cysteines in the Structure...\"; ORS=\"\"}\n",
    "\n",
    "$1==\"ATOM\" && $4==\"CYS\" && $3==\"SG\" {\n",
    "print $4$6\", \"\n",
    "cys_x[$6]=$7; cys_y[$6]=$8; cys_z[$6]=$9\n",
    "}\n",
    "\n",
    "END{ ORS=\"\\n\"\n",
    "for (key1 in cys_x) {\n",
    "  for (key2 in cys_x) { \n",
    "      dx=cys_x[key1]-cys_x[key2]\n",
    "      dy=cys_y[key1]-cys_y[key2]\n",
    "      dz=cys_z[key1]-cys_z[key2]\n",
    "      distance=sqrt(dx^2+dy^2+dz^2)\n",
    "      if (distance < 3 && distance != 0 && key1<key2) {\n",
    "        i++\n",
    "        candidate[i]=key1\"-\"key2\": \"distance\n",
    "      }\n",
    "  }\n",
    "}\n",
    "print \"\\nCandidates ...\"\n",
    "for (keys in candidate) {print candidate[keys]}\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33372dfe-55c2-4251-a96b-c446e43c6aa8",
   "metadata": {},
   "outputs": [],
   "source": [
    "awk -f distance.awk 1FRF.pdb\n",
    "awk -f distance.awk 1FRV.pdb"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f21cf84-7038-4fd0-8084-69d3eda9d9a3",
   "metadata": {},
   "source": [
    "### II) Comfortably\n",
    "\n",
    "Requires the following script:\n",
    "```\n",
    "# save as distance-batch.awk / searches close cysteines in a structure\n",
    "# requires a structure files (*.pdb)\n",
    "# usage: for i in DIR/*.pdb; do awk -f distance-batch.awk $i; done\n",
    "\n",
    "$1==\"ATOM\" && $4==\"CYS\" && $3==\"SG\" {cys_x[$6]=$7; cys_y[$6]=$8; cys_z[$6]=$9}\n",
    "\n",
    "END{i=0\n",
    "for (key1 in cys_x) {\n",
    "  for (key2 in cys_x) {\n",
    "      dx=cys_x[key1]-cys_x[key2]; dy=cys_y[key1]-cys_y[key2]; dz=cys_z[key1]-cys_z[key2]\n",
    "      distance=sqrt(dx^2+dy^2+dz^2)\n",
    "      if (distance < 3 && distance != 0 && key1 < key2) {\n",
    "        i++; candidate[i]=key1\"-\"key2\": \"distance\n",
    "        hit[key1]=key1; hit[key2]=key2\n",
    "      }\n",
    "  }\n",
    "}\n",
    "if(i!=0){\n",
    "  print FILENAME \" is a candidate:\"\n",
    "  for (keys in candidate) {print candidate[keys]}\n",
    "  ORS=\"\"; file = FILENAME\".script\"\n",
    "  print \"load =\"FILENAME\"\\n\" > file\n",
    "  print \"hide water; spacefill off\\n\" >> file\n",
    "  print \"select cys.sg; spacefill 200\\nselect \" >> file\n",
    "  for(keys in hit){cystein=cystein\"cys\"keys\".sg,\"}\n",
    "  print substr(cystein,1,length(cystein)-1) >> file\n",
    "  print \"\\nspacefill 400\\n\" >> file\n",
    "  print \"select cys.sg\\n\" >> file\n",
    "  print \"color surfacedistance\\n\" >> file\n",
    "  print \"\\n\" >> file\n",
    "  ORS=\"\\n\"\n",
    "  }\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50b755fe-ab8a-4667-a897-f203ad4f9fea",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in *.pdb; do awk -f distance-batch.awk $i; done"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9effeb5c-5f81-4386-9dbb-821d541b4c0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "cat *.pdb.script"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "538650e9-e49e-48a0-97e2-084e86b60b86",
   "metadata": {},
   "source": [
    "For 1FRV use additionally:\n",
    "```\n",
    "restrict :A or :B;\n",
    "center selected;\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5acccf40-421d-4108-bf81-775e0e600cf1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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
}
