|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "74e99565-d102-4935-a0b2-470bc42ed6d7", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "\n", |
| 9 | + "# Snekmer Motif Demo\n", |
| 10 | + "\n", |
| 11 | + "<b>Motif</b> is a pipeline for identification of functionally and structurally relevant amino acid motifs using feature selection.\n", |
| 12 | + "\n", |
| 13 | + "Motif uses the output of Model and a set of user-supplied protein families to train support vector machines for in- or out-of-family classification and find which kmers are the most informative.\n", |
| 14 | + "\n", |
| 15 | + "In this notebook, we will demonstrate how to use Snekmer Motif to find the kmers most indicative of membership in 3 small families.\n", |
| 16 | + "\n", |
| 17 | + "\n", |
| 18 | + "\n", |
| 19 | + "## Getting started with MOTIF\n", |
| 20 | + "\n", |
| 21 | + "### Setup\n", |
| 22 | + "\n", |
| 23 | + "First, install Snekmer using the instructions in the [user installation guide](https://github.com/PNNL-CompBio/Snekmer/).\n", |
| 24 | + "\n", |
| 25 | + "Before running Snekmer, verify that files have been placed in an **_input_** directory placed at the same level as the **_config.yaml_** file. The assumed file directory structure is illustrated below.\n", |
| 26 | + "\n", |
| 27 | + " ├── input\n", |
| 28 | + " │ ├── A.fasta\n", |
| 29 | + " │ ├── B.fasta\n", |
| 30 | + " │ ├── C.fasta\n", |
| 31 | + " │ ├── D.fasta\n", |
| 32 | + " │ ├── etc.\n", |
| 33 | + " │ ├── background\n", |
| 34 | + " │ │ ├── E.fasta\n", |
| 35 | + " │ │ ├── F.fasta\n", |
| 36 | + " │ │ ├── G.fasta\n", |
| 37 | + " │ │ ├── H.fasta\n", |
| 38 | + " │ │ └── etc.\n", |
| 39 | + " └── config.yaml\n", |
| 40 | + "\n", |
| 41 | + " (Note: Snekmer automatically creates the **_output_** directory when creating output files, so there is no need to create this folder in advance. Additionally, inclusion of background sequences is optional, but is illustrated above for interested users.)\n", |
| 42 | + "\n", |
| 43 | + " To ensure that snekmer is available in the Jupyter notebook do the following:\n", |
| 44 | + " conda activate snekmer\n", |
| 45 | + " conda install -c anaconda ipykernel\n", |
| 46 | + " python -m ipykernel install --user --name=snekmer\n", |
| 47 | + " jupyter notebook\n", |
| 48 | + "\n", |
| 49 | + "\n", |
| 50 | + "\n", |
| 51 | + "\n", |
| 52 | + " " |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "markdown", |
| 57 | + "id": "f4ea48af-6abe-469c-a427-949a25d10dfc", |
| 58 | + "metadata": {}, |
| 59 | + "source": [ |
| 60 | + "### Notes on Using Snekmer\n", |
| 61 | + "\n", |
| 62 | + "Snekmer assumes that the user will primarily process input files using the command line. For more detailed instructions, refer to the [README](https://github.com/PNNL-CompBio/Snekmer).\n", |
| 63 | + "\n", |
| 64 | + "The basic process for running Snekmer Motif is as follows:\n", |
| 65 | + "\n", |
| 66 | + "1. Verify that your file directory structure is correct and that the top-level directory contains a **_config.yaml_** file.\n", |
| 67 | + " - A **_config.yaml_** template has been included in the Snekmer codebase at **_resources/config.yaml_**.\n", |
| 68 | + "2. Modify the **_config.yaml_** with the desired parameters.\n", |
| 69 | + "3. Use the command line to navigate to the directory containing both the **_config.yaml_** file and **_input_** directory.\n", |
| 70 | + "4. If you are using background families you are not interested in identifying functionally relevant kmers from, run 'snekmer model', then move the FASTA files containing those families to the 'background' directory. You may skip this step if you are performing feature selection on all input families.\n", |
| 71 | + "5. Run 'snekmer motif'." |
| 72 | + ] |
| 73 | + }, |
| 74 | + { |
| 75 | + "cell_type": "markdown", |
| 76 | + "id": "a03d541d-f1e2-4e42-8f24-462d5bad5ba3", |
| 77 | + "metadata": {}, |
| 78 | + "source": [ |
| 79 | + "# Running Snekmer Motif\n", |
| 80 | + "\n", |
| 81 | + "First, install Snekmer using the instructions in the [user installation guide](https://snekmer.readthedocs.io/en/latest/getting_started/install.html).\n", |
| 82 | + "\n", |
| 83 | + "To ensure that the tutorial runs correctly, activate the conda environment containing your Snekmer installation and run the notebook from the environment.\n", |
| 84 | + "\n", |
| 85 | + "If you haven't yet run the [Snekmer tutorial](https://snekmer.readthedocs.io/en/latest/tutorial/index.html), you'll need to do so now. This runs Motif (and the original three Snekmer modes) on the demo example files and produces all output files. The tutorial uses the included default configuration parameters to guide the analysis, but the user can modify these parameters if a different configuration set is desired. The tutorial command line instructions are copied below:\n", |
| 86 | + "\n", |
| 87 | + "conda activate snekmer\n", |
| 88 | + " cd resources/tutorial/demo_example\n", |
| 89 | + " ./run_demo.sh\n", |
| 90 | + "\n", |
| 91 | + "Finally, we will initialize some parameters and parse filenames for this demo notebook." |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "code", |
| 96 | + "execution_count": null, |
| 97 | + "id": "7228680b-722a-4cdb-ba2b-181bae3e2a72", |
| 98 | + "metadata": {}, |
| 99 | + "outputs": [], |
| 100 | + "source": [ |
| 101 | + "# imports\n", |
| 102 | + "import glob\n", |
| 103 | + "import os\n", |
| 104 | + "import yaml\n", |
| 105 | + "from itertools import product\n", |
| 106 | + "import numpy as np\n", |
| 107 | + "import pandas as pd\n", |
| 108 | + "import matplotlib.pyplot as plt\n", |
| 109 | + "import seaborn as sns" |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "code", |
| 114 | + "execution_count": null, |
| 115 | + "id": "cbf3925a-9ade-4b17-b7bb-12f11333f140", |
| 116 | + "metadata": {}, |
| 117 | + "outputs": [], |
| 118 | + "source": [ |
| 119 | + "# load config file\n", |
| 120 | + "with open(os.path.join(\"..\", \"..\", \"resources\", \"config.yaml\"), \"r\") as configfile:\n", |
| 121 | + " config = yaml.safe_load(configfile)\n", |
| 122 | + "\n", |
| 123 | + "print(config)" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "code", |
| 128 | + "execution_count": null, |
| 129 | + "id": "a9fecdc8-7644-40ca-bd28-b381afe92a88", |
| 130 | + "metadata": {}, |
| 131 | + "outputs": [], |
| 132 | + "source": [ |
| 133 | + "filenames = sorted(\n", |
| 134 | + " [\n", |
| 135 | + " fa.rstrip(\".gz\")\n", |
| 136 | + " for fa, ext in product(\n", |
| 137 | + " glob.glob(os.path.join(\"demo_example\", \"input\", \"*\")),\n", |
| 138 | + " config[\"input_file_exts\"],\n", |
| 139 | + " )\n", |
| 140 | + " if fa.rstrip(\".gz\").endswith(f\".{ext}\")\n", |
| 141 | + " ]\n", |
| 142 | + ")\n", |
| 143 | + "\n", |
| 144 | + "families = sorted([os.path.splitext(os.path.basename(f))[0] for f in filenames])\n", |
| 145 | + "\n", |
| 146 | + "print(families)" |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "markdown", |
| 151 | + "id": "161dd2e2-b44c-4deb-af13-3aed4cc1899c", |
| 152 | + "metadata": {}, |
| 153 | + "source": [ |
| 154 | + "## Snekmer Motif output\n", |
| 155 | + "\n", |
| 156 | + "Snekmer Motif ranks kmers by p-value and support vector machine (SVM) weight after recursive feature elimination. The p-value is calculated as the number of rescoring iterations in which a kmer's SVM weight exceeded its weight on the correctly labeled input data, divided by the total number of rescoring iterations. Output files containing this data are stored in the **motif**/**p_values** directory. The other subdirectories within the **motif** directory are used for intermediate files that may be used to resume long workflows if execution is interrupted for any reason.\n", |
| 157 | + "\n", |
| 158 | + "The primary output file has the file name {input file name}.csv and has 5 columns:\n", |
| 159 | + "\n", |
| 160 | + "* **kmer**: The recoded amino acid sequence evaluated as a feature.\n", |
| 161 | + "* **real score**: The normalized weight learned for the kmer by an SVM trained for one-vs-all classification of the input family against a background of all other input families and any provided background sequences, after performing recursive feature elimination.\n", |
| 162 | + "* **false positive**: The number of rescoring iterations where an SVM learned a higher weight for a given kmer than that learned on the input data.\n", |
| 163 | + "* **n**: The number of rescoring iterations performed. This should be the same for each kmer and match the value of **n** contained in config.yaml.\n", |
| 164 | + "* **p**: The p-value calculated for each kmer.\n", |
| 165 | + "\n", |
| 166 | + "The Snekmer tutorial generates 3 output files, one for each family. We'll load and parse one of these:" |
| 167 | + ] |
| 168 | + }, |
| 169 | + { |
| 170 | + "cell_type": "code", |
| 171 | + "execution_count": null, |
| 172 | + "id": "aba5c403-f3ca-4978-81f4-efcced6b6810", |
| 173 | + "metadata": {}, |
| 174 | + "outputs": [], |
| 175 | + "source": [ |
| 176 | + "# read motif results\n", |
| 177 | + "results = pd.read_csv(os.path.join(\"demo_example\", \"output\", \"motif\", \"p_values\", \"nxrA.csv\"))\n", |
| 178 | + "results = results.sort_values(by=\"motif\").reset_index(drop=True)\n", |
| 179 | + "results[\"motif\"] = results[\"motif\"].astype(str)\n", |
| 180 | + "results" |
| 181 | + ] |
| 182 | + }, |
| 183 | + { |
| 184 | + "cell_type": "markdown", |
| 185 | + "id": "c82b8336-3981-475d-9028-376875a0e47b", |
| 186 | + "metadata": {}, |
| 187 | + "source": [ |
| 188 | + "As seen above, kmers are ordered first by p-value and then by their score on real data. " |
| 189 | + ] |
| 190 | + } |
| 191 | + ], |
| 192 | + "metadata": { |
| 193 | + "kernelspec": { |
| 194 | + "display_name": "Python 3 (ipykernel)", |
| 195 | + "language": "python", |
| 196 | + "name": "python3" |
| 197 | + }, |
| 198 | + "language_info": { |
| 199 | + "codemirror_mode": { |
| 200 | + "name": "ipython", |
| 201 | + "version": 3 |
| 202 | + }, |
| 203 | + "file_extension": ".py", |
| 204 | + "mimetype": "text/x-python", |
| 205 | + "name": "python", |
| 206 | + "nbconvert_exporter": "python", |
| 207 | + "pygments_lexer": "ipython3", |
| 208 | + "version": "3.12.1" |
| 209 | + } |
| 210 | + }, |
| 211 | + "nbformat": 4, |
| 212 | + "nbformat_minor": 5 |
| 213 | +} |
0 commit comments