├── .github └── workflows │ └── ci.yaml ├── .gitignore ├── LICENSE ├── README.md ├── _config.yml ├── binder ├── environment.yml └── postBuild ├── course-summary.docx ├── css ├── .gitkeep └── dock.css ├── docking.html ├── images ├── .gitkeep ├── hiv-protease.png ├── indinavir.png ├── main-setup │ ├── anaconda-terminal.png │ ├── jupyter-cells.png │ ├── jupyter-close.png │ ├── jupyter-interrupt.png │ ├── jupyter-main-window.png │ ├── jupyter-restart.png │ ├── macos-terminal.png │ ├── oxcompbio-zipdownload.png │ └── ubuntu-terminal.png └── nelfinavir.png ├── setup.md ├── tests ├── environment.yml └── pytest.ini └── tutorials ├── Docking ├── CONTRIBUTORS.MD ├── README.md ├── docking-2020.tar.gz ├── docking-2022.html ├── docking-2022.pdf └── images │ ├── hiv-protease.png │ ├── indinavir.png │ └── nelfinavir.png ├── Homology-Modelling ├── DATA │ ├── 01_TM │ │ ├── 1bxw.pdb │ │ ├── 2k0l.pdb │ │ ├── OmpATM.pdb │ │ ├── OmpA_TM_pymol.fasta │ │ ├── OmpA_alignment_TM.clw │ │ ├── OmpA_alignment_TM.pir │ │ ├── ompa.fasta │ │ ├── ompa_Cter.fasta │ │ ├── ompa_E_PDB.fasta │ │ ├── ompa_E_PDB_TM.fasta │ │ ├── ompa_E_PDB_TM_noM.fasta │ │ ├── ompa_K_PDB.fasta │ │ ├── ompa_K_PDB_TM.fasta │ │ ├── ompa_TM.fasta │ │ ├── pairwise_alignment_TM.ali │ │ ├── run_modeller.py │ │ ├── seq_YPTM.B99990001.pdb │ │ ├── seq_YPTM.D00000001 │ │ ├── seq_YPTM.V99990001 │ │ ├── seq_YPTM.fasta │ │ ├── seq_YPTM.ini │ │ ├── seq_YPTM.rsr │ │ └── seq_YPTM.sch │ ├── 02_Cter │ │ ├── OmpACT.pdb │ │ ├── OmpA_Cter.pir │ │ ├── delafter │ │ │ ├── Full_OmpA_YP.pdb │ │ │ ├── OmpACT.pdb │ │ │ ├── OmpA_FullModel.pdb │ │ │ ├── ompa_Cter.fasta │ │ │ ├── pairwise_alignment_CT.ali │ │ │ ├── prev_OmpA_FullModel.pdb │ │ │ ├── run_modeller_score.py │ │ │ └── seq_YPCT_G.fasta │ │ ├── pairwise_alignment_CT.ali │ │ ├── run_modeller_score.log │ │ ├── run_modeller_score.py │ │ ├── seq_YPCT_G.B99990001.pdb │ │ ├── seq_YPCT_G.B99990002.pdb │ │ ├── seq_YPCT_G.B99990003.pdb │ │ ├── seq_YPCT_G.B99990004.pdb │ │ ├── seq_YPCT_G.B99990005.pdb │ │ ├── seq_YPCT_G.B99990006.pdb │ │ ├── seq_YPCT_G.B99990007.pdb │ │ ├── seq_YPCT_G.B99990008.pdb │ │ ├── seq_YPCT_G.B99990009.pdb │ │ ├── seq_YPCT_G.B99990010.pdb │ │ ├── seq_YPCT_G.D00000001 │ │ ├── seq_YPCT_G.D00000002 │ │ ├── seq_YPCT_G.D00000003 │ │ ├── seq_YPCT_G.D00000004 │ │ ├── seq_YPCT_G.D00000005 │ │ ├── seq_YPCT_G.D00000006 │ │ ├── seq_YPCT_G.D00000007 │ │ ├── seq_YPCT_G.D00000008 │ │ ├── seq_YPCT_G.D00000009 │ │ ├── seq_YPCT_G.D00000010 │ │ ├── seq_YPCT_G.V99990001 │ │ ├── seq_YPCT_G.V99990002 │ │ ├── seq_YPCT_G.V99990003 │ │ ├── seq_YPCT_G.V99990004 │ │ ├── seq_YPCT_G.V99990005 │ │ ├── seq_YPCT_G.V99990006 │ │ ├── seq_YPCT_G.V99990007 │ │ ├── seq_YPCT_G.V99990008 │ │ ├── seq_YPCT_G.V99990009 │ │ ├── seq_YPCT_G.V99990010 │ │ ├── seq_YPCT_G.ini │ │ ├── seq_YPCT_G.rsr │ │ ├── seq_YPCT_G.sch │ │ └── seq_YPCT_models.pdb │ ├── 03_loop_refine │ │ ├── Full_OmpA_YP.BL00010001.pdb │ │ ├── Full_OmpA_YP.BL00020001.pdb │ │ ├── Full_OmpA_YP.BL00030001.pdb │ │ ├── Full_OmpA_YP.BL00040001.pdb │ │ ├── Full_OmpA_YP.BL00050001.pdb │ │ ├── Full_OmpA_YP.BL00060001.pdb │ │ ├── Full_OmpA_YP.BL00070001.pdb │ │ ├── Full_OmpA_YP.BL00080001.pdb │ │ ├── Full_OmpA_YP.BL00090001.pdb │ │ ├── Full_OmpA_YP.BL00100001.pdb │ │ ├── Full_OmpA_YP.DL00010001 │ │ ├── Full_OmpA_YP.DL00020001 │ │ ├── Full_OmpA_YP.DL00030001 │ │ ├── Full_OmpA_YP.DL00040001 │ │ ├── Full_OmpA_YP.DL00050001 │ │ ├── Full_OmpA_YP.DL00060001 │ │ ├── Full_OmpA_YP.DL00070001 │ │ ├── Full_OmpA_YP.DL00080001 │ │ ├── Full_OmpA_YP.DL00090001 │ │ ├── Full_OmpA_YP.DL00100001 │ │ ├── Full_OmpA_YP.IL00000001.pdb │ │ ├── Full_OmpA_YP.lrsr │ │ ├── Full_OmpA_YP.pdb │ │ ├── refine_loops.py │ │ └── score_dope_obj.dat │ ├── 04_full_protein │ │ └── Full_OmpA_YP.pdb │ ├── 05_quality_check │ │ ├── charts │ │ │ ├── prep-chirals.txt │ │ │ ├── prep-multi.html │ │ │ ├── prep-rama.pdf │ │ │ └── prep-ramaZ.txt │ │ ├── molprobity.zip │ │ └── prep.pdb │ └── 06_final_compared │ │ ├── AF-Q8ZG77-F1-model_v4.pdb │ │ ├── Full_OmpA_YP.pdb │ │ └── Full_OmpA_YP_linkmodels.pdb ├── README.md └── homology-modelling-tutorial.ipynb └── Python ├── 00_Introduction ├── Introduction.ipynb └── no_solutions │ └── Introduction.ipynb ├── 01_BasicTypes ├── BasicTypes.ipynb └── no_solutions │ └── BasicTypes.ipynb ├── 02_Variables ├── Variables_and_Comparisons.ipynb └── no_solutions │ └── Variables_and_Comparisons.ipynb ├── 03_Loops ├── Loops.ipynb └── no_solutions │ └── Loops.ipynb ├── 04_Conditionals ├── Conditionals.ipynb └── no_solutions │ └── Conditionals.ipynb ├── 05_Lists ├── Lists.ipynb └── no_solutions │ └── Lists.ipynb ├── 06_FileHandling ├── FileHandling.ipynb ├── datafiles │ ├── file1.txt │ ├── file2.txt │ └── file3.txt └── no_solutions │ ├── FileHandling.ipynb │ └── datafiles │ ├── file1.txt │ ├── file2.txt │ └── file3.txt ├── 07_Functions ├── Functions.ipynb └── no_solutions │ └── Functions.ipynb ├── 08_Documentation ├── Documentation.ipynb └── no_solutions │ └── Documentation.ipynb ├── 09_Libraries ├── Libraries.ipynb └── no_solutions │ └── Libraries.ipynb ├── 10_Numpy ├── Numpy.ipynb └── no_solutions │ └── Numpy.ipynb ├── 11_OOP ├── Molecule.png ├── OOP.ipynb ├── SimpleMolecule.png ├── acetylene.png └── no_solutions │ ├── Molecule.png │ ├── OOP.ipynb │ ├── SimpleMolecule.png │ └── acetylene.png ├── 12_ProteinAnalysis ├── ProteinAnalysis.ipynb ├── datafiles │ └── 1HSG.pdb └── no_solutions │ ├── ProteinAnalysis.ipynb │ └── datafiles │ └── 1HSG.pdb ├── 13_Simulation_and_Analysis ├── Simulation_and_Analysis.ipynb ├── datafiles │ └── 1hsg.pdb └── no_solutions │ ├── Simulation_and_Analysis.ipynb │ └── datafiles │ └── 1hsg.pdb ├── CONTRIBUTORS.md ├── README.md ├── cleanup.sh ├── images ├── jupyter-run-all.png ├── verify-location.png └── verify-outputs.png ├── setup-conda.sh └── setup.md /.github/workflows/ci.yaml: -------------------------------------------------------------------------------- 1 | name: "CI" 2 | on: 3 | pull_request: 4 | branches: 5 | - main 6 | push: 7 | branches: 8 | - main 9 | schedule: 10 | # At 07:00 UTC on Monday and Thursday. 11 | - cron: "0 7 * * 1,4" 12 | 13 | concurrency: 14 | # Cancel out any previous workflows within the same PR 15 | group: "${{ github.ref }}-${{ github.head_ref }}-${{ github.workflow }}" 16 | cancel-in-progress: True 17 | 18 | defaults: 19 | run: 20 | shell: bash -l {0} 21 | 22 | jobs: 23 | tests: 24 | runs-on: ${{ matrix.OS }}-latest 25 | name: "tests" 26 | strategy: 27 | fail-fast: False 28 | matrix: 29 | os: ['ubuntu', 'macos', 'windows'] 30 | python-version: ['3.7', '3.8', '3.9'] 31 | 32 | steps: 33 | - uses: actions/checkout@v3 34 | 35 | - uses: conda-incubator/setup-miniconda@v2 36 | with: 37 | auto-update-conda: True 38 | use-mamba: True 39 | python-version: ${{ matrix.python-version }} 40 | miniforge-variant: Mambaforge 41 | environment-file: tests/environment.yml 42 | activate-environment: OxPython 43 | 44 | - name: "environment information" 45 | run: | 46 | mamba info -a 47 | mamba list 48 | - name: "run notebooks tests" 49 | run: | 50 | pytest -v --nbval-lax 51 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # python 2 | __pycache__/ 3 | .mypy_cache 4 | *.pyc 5 | .coverage 6 | 7 | # jupyter 8 | .ipynb_checkpoints 9 | 10 | # editors 11 | .vscode 12 | .idea 13 | *~ 14 | .swp 15 | 16 | # os 17 | .DS_Store 18 | 19 | # generated files 20 | *.npz 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2017-2020, SBCB Oxford 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Oxford Computational Biochemistry course 2 | 3 | This repository contains the teaching materials for the Oxford Computational Biochemistry course. 4 | This two and half day course is aimed at graduate students who have had no prior experience in the area of Computational Biochemistry. It assumes no prior knowledge and only tries to cover basic concepts. 5 | 6 | ## Getting started 7 | 8 | In order to participate in these tutorials you will need specific software installations. 9 | 10 | To get started, please see the [overall setup instructions](setup.md). 11 | 12 | ## Tutorials 13 | 14 | ### 1) [Python](tutorials/Python) [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/bigginlab/OxCompBio/master?filepath=tutorials%2FPython) 15 | 16 | A set of jupyter notebooks which aim to teach the basics of python programming assuming no prior knowledge. 17 | First introducing core concepts such as variables, loops, conditionals and lists, we eventually demonstrate how molecular structures can be analysed using python libraries such as [MDAnalysis](https://www.mdanalysis.org/) and [NGLView](http://nglviewer.org/nglview/latest/). 18 | 19 | #### Summary of notebooks 20 | 21 | | Notebook | Colab link | 22 | |-------------------------------------------------|------------| 23 | | 0. Introduction | [![notebook 1](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/00_Introduction/no_solutions/Introduction.ipynb) 24 | | 1. Basic Types | [![notebook 2](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/01_BasicTypes/no_solutions/BasicTypes.ipynb) 25 | | 2. Variables and Comparisons | [![notebook 3](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/02_Variables/no_solutions/Variables_and_Comparisons.ipynb) 26 | | 3. Loops | [![notebook 4](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/03_Loops/no_solutions/Loops.ipynb) 27 | | 4. Conditionals | [![notebook 5](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/04_Conditionals/no_solutions/Conditionals.ipynb) 28 | | 5. Lists | [![notebook 6](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/05_Lists/no_solutions/Lists.ipynb) 29 | | 6. File Handling | [![notebook 7](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/06_FileHandling/no_solutions/FileHandling.ipynb) 30 | | 7. Functions | [![notebook 8](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/07_Functions/no_solutions/Functions.ipynb) 31 | | 8. Documentation | [![notebook 9](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/08_Documentation/no_solutions/Documentation.ipynb) 32 | | 9. Libraries | [![notebook 10](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/09_Libraries/no_solutions/Libraries.ipynb) 33 | | 10. NumPy | [![notebook 11](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/10_Numpy/no_solutions/Numpy.ipynb) 34 | | 11. Object Orientated Programming | [![notebook 12](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/11_OOP/no_solutions/OOP.ipynb) 35 | | 12. Protein Analysis | [![notebook 13](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/12_ProteinAnalysis/no_solutions/ProteinAnalysis.ipynb) 36 | | 13. Simulation and Analysis | [![notebook 14](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/13_Simulation_and_Analysis/no_solutions/Simulation_and_Analysis.ipynb) 37 | 38 | ### 2) [Homology Modelling](tutorials/Homology-Modelling) 39 | 40 | Contains a practical tutorial and lecture slides that aim to: 41 | 42 | • Introduce the process of homology modelling. 43 | 44 | • Summarise the methods for predicting the structure from sequence. 45 | 46 | • Describe the individual steps involved in creating and optimising a protein homology model. 47 | 48 | • Outline the methods available to evaluate the quality of homology models. 49 | 50 | ### 3) Molecular Dynamics 51 | 52 | The Molecular Dynamics workshop contents can be obtained from the following [dropbox link](https://www.dropbox.com/sh/i150dwitodfitqp/AACs7T8TuJwdKn6lBAqeLVC8a?dl=0) 53 | 54 | 55 | ### 4) Molecular Docking 56 | 57 | A tutorial which aims to teach the basics of molecular docking. 58 | 59 | The tutorial contains: 60 | - Lecture slides introducing the concept of molecular docking. 61 | - A tutorial which looks at using [Autodock Vina](http://vina.scripps.edu/) to dock small ligands to HIV-1 protease. 62 | 63 | ## Acknowledgements 64 | 65 | The Oxford Computational Biochemistry course has been written by several authors over several years. 66 | Please see individual tutorials for contributor logs. 67 | 68 | Course leader: Professor Syma Khalid 69 | 70 | ## License 71 | 72 | The code is released under the [BSD-3](https://opensource.org/licenses/BSD-3-Clause) license (see `LICENSE` file) while other content is released under the [CC BY-NC 4.0](https://creativecommons.org/licenses/by-nc/4.0/) license (see below). 73 | 74 | Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. 75 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /binder/environment.yml: -------------------------------------------------------------------------------- 1 | name: OxPython 2 | channels: 3 | - jaimergp/label/unsupported-cudatoolkit-shim 4 | - conda-forge 5 | dependencies: 6 | # Base 7 | - python=3.9 8 | - pip 9 | - ipython 10 | 11 | # Jupyter 12 | - notebook 13 | - jupyter_contrib_nbextensions 14 | 15 | # Maths 16 | - numpy 17 | 18 | # Viz 19 | - matplotlib 20 | 21 | # MD 22 | - MDAnalysis>=2.0.0 23 | - nglview 24 | - openmm 25 | - parmed 26 | 27 | # testing 28 | - pytest 29 | - nbval 30 | - ipywidgets<8 31 | -------------------------------------------------------------------------------- /binder/postBuild: -------------------------------------------------------------------------------- 1 | jupyter contrib nbextension install --user 2 | jupyter nbextension enable splitcell/splitcell 3 | jupyter nbextension enable rubberband/main 4 | jupyter nbextension enable exercise2/main 5 | jupyter nbextension enable autosavetime/main 6 | jupyter nbextension enable collapsible_headings/main 7 | jupyter nbextension enable codefolding/main 8 | jupyter nbextension enable limit_output/main 9 | jupyter nbextension enable toc2/main 10 | -------------------------------------------------------------------------------- /course-summary.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/course-summary.docx -------------------------------------------------------------------------------- /css/.gitkeep: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /css/dock.css: -------------------------------------------------------------------------------- 1 | body { 2 | color: black; 3 | background-color: white; 4 | margin-left: 5%; 5 | margin-right: 5%; 6 | font-family: Helvetica, Arial, Verdana, Helvetica, sans-serif ; 7 | text-align: justify; 8 | } 9 | p, ul, ol, dl, address { 10 | color: black; 11 | background-color: white; 12 | } 13 | 14 | ol.roman { 15 | marign-left: 0; 16 | padding-left: 15; 17 | } 18 | h1 { 19 | color: #0303af; 20 | background-color: white; 21 | border-bottom: solid thin; 22 | } 23 | h2 { 24 | color: black; 25 | background-color: white; 26 | } 27 | h3 { 28 | color: black; 29 | background-color: white; 30 | } 31 | 32 | /* dl are used quite frequently as menus: increase distance between 33 | items */ 34 | 35 | dl dd { margin-bottom: 1em; } 36 | 37 | /* dl with bold 'menu items' */ 38 | dl.strong dt { font-weight: bold; } 39 | 40 | 41 | /* table */ 42 | th { vertical-align: baseline } 43 | td { vertical-align: top } 44 | 45 | 46 | table.default { 47 | background: #EEEEEE; 48 | border: none; 49 | padding: 0px; 50 | border: thin solid #DDDDDD 51 | } 52 | 53 | table.default th { 54 | background: #DDDDDD; 55 | color: #2a668c; 56 | padding-left: 5px; 57 | padding-right: 5px; 58 | } 59 | 60 | table.default td { 61 | vertical-align: top; 62 | padding-left: 5px; 63 | padding-right: 5px; 64 | } 65 | 66 | table.default caption { 67 | background: #DDDDDD; 68 | color: #2a668c; 69 | } 70 | 71 | table.ruled { 72 | background: #EEEEEE; 73 | border: none; 74 | padding: 0px; 75 | border: thin solid #DDDDDD 76 | } 77 | 78 | table.ruled th { 79 | background: #DDDDDD; 80 | color: #2a668c; 81 | } 82 | 83 | table.ruled td { 84 | vertical-align: top; 85 | padding: 1px; 86 | margin: 0px; 87 | border: thin solid #DDDDDD 88 | } 89 | 90 | table.ruled td table { 91 | border-style: none; 92 | } 93 | 94 | table.ruled td table td { 95 | border-style: none; 96 | } 97 | 98 | 99 | table.ruled caption { 100 | background: #DDDDDD; 101 | color: #2a668c; 102 | } 103 | 104 | .searchbox { 105 | padding: 1%; 106 | width: 89%; 107 | margin-left: 5%; 108 | margin-right: 5%; 109 | } 110 | 111 | 112 | 113 | a:link { 114 | color: blue; 115 | background-color: transparent; 116 | } 117 | a:visited { 118 | color: purple; 119 | background-color: transparent; 120 | } 121 | a:active { 122 | color: red; 123 | background-color: white; 124 | } 125 | 126 | div#menu { 127 | background: #d1dbff; /* light blue */ 128 | color: black; 129 | text-align: center; 130 | padding-top: 5px; 131 | padding-bottom:5px; 132 | margin-top: 2em; 133 | margin-bottom: 1em; 134 | clear: both; 135 | } 136 | 137 | address { 138 | font-size: 85%; 139 | } 140 | 141 | #question { font-weight: bold; color: brown; 142 | } 143 | 144 | 145 | 146 | /* no border on imglinks */ 147 | a:link img, a:visited img { border-style: none } 148 | @media screen { /* hide from IE3 */ 149 | a[href]:hover { background: #ffa } 150 | } 151 | -------------------------------------------------------------------------------- /docking.html: -------------------------------------------------------------------------------- 1 | tutorials/Docking/docking-2020.html -------------------------------------------------------------------------------- /images/.gitkeep: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /images/hiv-protease.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/images/hiv-protease.png -------------------------------------------------------------------------------- /images/indinavir.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/images/indinavir.png -------------------------------------------------------------------------------- /images/main-setup/anaconda-terminal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/images/main-setup/anaconda-terminal.png -------------------------------------------------------------------------------- /images/main-setup/jupyter-cells.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/images/main-setup/jupyter-cells.png -------------------------------------------------------------------------------- /images/main-setup/jupyter-close.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/images/main-setup/jupyter-close.png -------------------------------------------------------------------------------- /images/main-setup/jupyter-interrupt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/images/main-setup/jupyter-interrupt.png -------------------------------------------------------------------------------- /images/main-setup/jupyter-main-window.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/images/main-setup/jupyter-main-window.png -------------------------------------------------------------------------------- /images/main-setup/jupyter-restart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/images/main-setup/jupyter-restart.png -------------------------------------------------------------------------------- /images/main-setup/macos-terminal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/images/main-setup/macos-terminal.png -------------------------------------------------------------------------------- /images/main-setup/oxcompbio-zipdownload.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/images/main-setup/oxcompbio-zipdownload.png -------------------------------------------------------------------------------- /images/main-setup/ubuntu-terminal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/images/main-setup/ubuntu-terminal.png -------------------------------------------------------------------------------- /images/nelfinavir.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/images/nelfinavir.png -------------------------------------------------------------------------------- /setup.md: -------------------------------------------------------------------------------- 1 | # Main setup instructions for the Oxford Computational Biochemistry course 2 | 3 | #### Navigation 4 | 5 | - [Return to the main page](README.md) 6 | - [Python tutorial setup instructions](./tutorials/Python/setup.md) 7 | - [MD tutorial setup instructions](./tutorials/MD/setup.md) 8 | - [Homology modelling tutorial setup instructions](./tutorials/Homology-Modelling/setup.md) 9 | - [Docking tutorial setup instructions](./tutorials/Docking/README.md) 10 | 11 | 12 | ## Contents 13 | 14 | The Oxford Computational Biochemistry course contains 4 self-contained 15 | tutorials which each have different software installation requirements. These 16 | instructions can be found in the respective tutorial's folder. Here we 17 | provide some basic instructions on the base setup requirements common to these 18 | tutorials and how to obtain a local copy of the materials. 19 | 20 | In order to ensure that you have everything ready in advance of the workshop, 21 | we ask that you **follow these setup instructions before attending each tutorial**. 22 | Q&A sessions will be setup on the two days preceeding the course to help you 23 | with any issues you might encounter during the setup process. 24 | 25 | 26 | ## 1. [Anaconda] 27 | 28 | All tutorials in this course rely heavily on the [Anaconda] package manager. 29 | This cross platform tool allows you to install other software packages in an 30 | easy and controllable manner. We require all attendees to install this prior 31 | to starting any of the tutorials. Please see the follow the [Anaconda install instructions] 32 | to get started. 33 | 34 | The [Anaconda] package manager, is named [conda]. Throughout the tutorials you 35 | will see several invocations of [conda], including to create [environments] and 36 | install software. If you are unfamiliar with this tool, it is heavily 37 | recommened that you go through the [getting started guide]. 38 | 39 | 40 | ## 2. Using the terminal 41 | 42 | Throughout the tutorials we will be interacting with various software through 43 | the use of a terminal interface. Terminals are a powerful means by which to 44 | navigate through files and quickly execute various programs. 45 | 46 | On linux/macOS this is generally done though a bash/zsh shell. 47 | 48 | - In linux this can be accessed by searching for terminal in the startup menu. 49 | See below for an example in the ubuntu gnome shell. 50 | 51 | 52 | ![Ubuntu shell](./images/main-setup/ubuntu-terminal.png) 53 | 54 | 55 | - On macOS this is done by searching for terminal in the Launchpad (see the 56 | [following for more details][apple terminal support]). 57 | 58 | 59 | ![macos shell](./images/main-setup/macos-terminal.png) 60 | 61 | 62 | On Windows (unless using WSL, the Windows Subsystem for Linux, in which case please follow the linux instructions), 63 | this will be done via the Anaconda prompt. The Anaconda prompt can be opened by 64 | searching for it in the startup menu after having installed Anaconda (see the 65 | step above). 66 | 67 | 68 | ![Anaconda Prompt](./images/main-setup/anaconda-terminal.png) 69 | 70 | 71 | There are substantial differences in the way in which the terminal works between 72 | Windows and linx/macOS. We will give a quick overview of the the basics below, 73 | including some links for extra resources you can look at to get more familiar 74 | with using the shell. We recommend that you have a look through these before 75 | attempting the tutorials. 76 | 77 | Note: in the MD tutorial you will eventually be required to access a remote 78 | HPC machine to run a Molecular Dynamics simulations. This will require use 79 | of the linux terminal. If you are using Windows you will therefore also need 80 | to look at the instructions for the linux terminal before attempting this tutorial. 81 | 82 | ### Basic linux/macOS terminal instructions 83 | 84 | This section only provides a very basic overview of using the bash/zsh shell 85 | in linux/macOS. For more information please see the following tutorials: 86 | 87 | - [Ubuntu's terminal tutorial](https://ubuntu.com/tutorials/command-line-for-beginners#1-overview) 88 | - [MacOS terminal beginners guide](https://www.youtube.com/watch?v=aKRYQsKR46I) (youtube video) 89 | 90 | **_On first opening the terminal_** 91 | 92 | Generally upon opening the terminal you will be placed in your $HOME directory. 93 | This is the base directory where all the documents for your login account are 94 | placed. If you ever need to return to this directory enter the following command: 95 | 96 | ``` 97 | cd $HOME 98 | ``` 99 | or even just 100 | 101 | ``` 102 | cd 103 | ``` 104 | 105 | 106 | **_Seeing and navigating the contents of folders_** 107 | 108 | One of the main uses of the terminal is to allow users to quickly see and navigate 109 | through the various files and folders on your computer. 110 | 111 | You can see what files are in the current folder you are accessing using: 112 | 113 | ``` 114 | ls -lrt 115 | ``` 116 | 117 | You can change from one folder to another (in this case named 'newfolder') by doing: 118 | 119 | ``` 120 | cd newfolder 121 | ``` 122 | 123 | To go back out of the folder, you can do: 124 | 125 | ``` 126 | cd .. 127 | ``` 128 | 129 | If you want to know where you are amongst all your folders you can do: 130 | 131 | ``` 132 | pwd 133 | ``` 134 | 135 | This will give you the full path to the current folder. 136 | 137 | If you want to delete (remove) a file do: 138 | 139 | ``` 140 | rm file 141 | ``` 142 | 143 | If you want to rename a file do: 144 | 145 | ``` 146 | mv file newfilename 147 | ``` 148 | 149 | If you want to view the contents of a file do: 150 | 151 | ``` 152 | more file 153 | ``` 154 | 155 | 156 | ### Basic Windows Anaconda terminal instructions 157 | 158 | This section only provides a very basic overview of using the Anaconda terminal. 159 | For more information please see the following tutorials: 160 | 161 | - [Windows command line tutorial](https://youtu.be/MBBWVgE0ewk) (youtube video) 162 | Note: this uses the standard windows command line, but the instructions should carry over 163 | to the Anaconda terminal. 164 | Disclaimer: we have not watched all 9 videos in the tutorial series and do not take 165 | responsibility for any of the contents. 166 | 167 | **_On first opening the terminal_** 168 | 169 | Generally upon opening the terminal you will be placed in your %HOMEPATH% directory. 170 | This is the base directory where all the main folders (My Documents, Downloads, etc...) 171 | for your login account are placed, which is usually `C:\Users\username`. If you ever 172 | need to return to this folder enter the following command: 173 | 174 | ``` 175 | cd %HOMEPATH% 176 | ``` 177 | 178 | **_Seeing and navigating the contents of folders_** 179 | 180 | One of the main uses of the terminal is to allow users to quickly see and navigate 181 | through the various files and folders on your computer. 182 | 183 | You can see what files are in the current folder you are accessing using: 184 | 185 | ``` 186 | dir 187 | ``` 188 | 189 | You can change from one folder to another (in this case named 'newfolder') by doing: 190 | 191 | ``` 192 | cd newfolder 193 | ``` 194 | 195 | To go back out of the folder, you can do: 196 | 197 | ``` 198 | cd .. 199 | ``` 200 | 201 | If you want to know where you are amongst all your folders you can do: 202 | 203 | ``` 204 | cd 205 | ``` 206 | 207 | This will give you the full path to the current folder. 208 | 209 | If you want to delete (remove) a file do: 210 | 211 | ``` 212 | del file 213 | ``` 214 | 215 | If you want to rename a file do: 216 | 217 | ``` 218 | rename file newfilename 219 | ``` 220 | 221 | If you want to view the contents of a file do: 222 | 223 | ``` 224 | more file 225 | ``` 226 | 227 | ### Using editors 228 | 229 | At several points during the tutorials you might be asked to edit a file. 230 | 231 | There are several editors available and generally we recommend you use 232 | the one that you are most accustomed with (if possible). 233 | 234 | Here are our recommendations for editors that can be invoked directly from 235 | the command line: 236 | 237 | #### linux/macOS 238 | 239 | - [gedit](https://help.gnome.org/users/gedit/stable/) (recommended for linux, usually pre-installed) 240 | - [nano](https://wiki.gentoo.org/wiki/Nano/Basics_Guide) (recommended for linux & macOS, pre-installed) 241 | - [TextEdit](https://support.apple.com/en-gb/guide/textedit/welcome/mac) (for macOS, pre-installed. From the terminal prompt you can issue `open -a TextEdit`) 242 | - [vim](https://www.vim.org/download.php) 243 | 244 | #### Windows 245 | 246 | - notepad (recommended) 247 | 248 | Built into Windows, just type `notepad filename` in your terminal. This will open a 249 | window where you can edit the file, save and then close the window to get back to 250 | your terminal. 251 | 252 | - [notepad++](https://notepad-plus-plus.org/) 253 | - [vim](https://www.vim.org/download.php) 254 | 255 | 256 | ## 3. Downloading a local copy of the course materials 257 | 258 | In order to carry out the exercies presented in the tutorials you will need to 259 | download a local copy of the course materials. Our recommended way is to do the following: 260 | 261 | Go to the tutorial github page: [https://github.com/bigginlab/OxCompBio](https://github.com/bigginlab/OxCompBio) 262 | 263 | On the top left-hand side of the page you will see a green button titled "Code". 264 | 265 | Click on it and then click on "Download ZIP". 266 | 267 | 268 | ![download zip](./images/main-setup/oxcompbio-zipdownload.png) 269 | 270 | 271 | This will download a copy of the course materials to your computer. Then simply 272 | move the zip file to some convenient folder (ideally easily accessible through your 273 | terminal), and extract it (macOS: double click, windows: right-click and extract all). 274 | 275 | This will create a folder named `OxCompBio-master`. 276 | 277 | Note: there are other more complex way to obtain these materials, such as through 278 | the use of `git`, however in the interest of easy of use, we shall not discuss them 279 | here. 280 | 281 | 282 | ## 4. [Jupyter notebooks] 283 | 284 | The tutorials rely on the use of [Jupyter notebooks] to provide an interactive 285 | experience, particularly when dealing with python code. The install 286 | instructions for each tutorial will install a copy of the jupyter notebook 287 | interface where required. We recommend that you start by setting up the 288 | [python tutorial] and then look at some of the following resources on how to 289 | use notebooks: 290 | 291 | - [The jupyter notebook beginners guide](https://jupyter-notebook-beginner-guide.readthedocs.io/en/latest/execute.html) 292 | - [Codeacademy's jupyter notebook tutorial](https://www.codecademy.com/articles/how-to-use-jupyter-notebooks) 293 | - [Quick introduction to jupyter notebooks](https://www.youtube.com/watch?v=jZ952vChhuI) (youtube video) 294 | 295 | 296 | Briefly, one usually starts a jupyter notebook session by opening a terminal, 297 | navigating to the folder where `ipynb` (notebook) files are present, and 298 | typing the following: 299 | 300 | ``` 301 | jupyter notebook 302 | ``` 303 | 304 | This should automatically open an interactive session in your browser. 305 | 306 | 307 | ![jupyter main session](./images/main-setup/jupyter-main-window.png) 308 | 309 | 310 | You can then click on the `.ipynb` (notebook) file to start it. 311 | 312 | Jupyter notebooks are composed of cells, some of which contain normal text 313 | (with a white background), and code blocks (shaded in grey): 314 | 315 | 316 | ![jupyter cells](./images/main-setup/jupyter-cells.png) 317 | 318 | 319 | Code blocks are meant to be interactive, you can type in them and press 320 | `shift+Enter` to run the contents of the cells. 321 | 322 | You can exit a notebook by clicking "File"->"Close and Halt": 323 | 324 | 325 | ![jupyter close](./images/main-setup/jupyter-close.png) 326 | 327 | 328 | Sometimes when too many things are running, notebooks can start to 329 | behave erratically, when this happens clicking on the "interrupt" button 330 | may help: 331 | 332 | 333 | ![jupyter-interrupt](./images/main-setup/jupyter-interrupt.png) 334 | 335 | 336 | Alternatively, using one of the "Kernel"->"restart" options can be very useful. 337 | Particularly, the "Restart & Clear Output" can be useful in cleaning up the notebook's 338 | outputs and starting again: 339 | 340 | 341 | ![jupyter-restart](./images/main-setup/jupyter-restart.png) 342 | 343 | 344 | **Note**: In a Jupyter notebook you can run cells in any order you like. This might create 345 | problems in the long run, therefore we suggest to run the cells sequentially (from top to bottom). 346 | If you start to get strange results within a Jupyter notebook, you can use "Kernel"->"Restart & Run all" 347 | to run clean everything up and re-run the notebook from scratch. 348 | 349 | ## 5. What to do next 350 | 351 | The next step is to do the individual setup steps for each tutorial. 352 | 353 | These can be found at the following links: 354 | 355 | - [Python](./tutorials/Python/setup.md) 356 | - [Homology-Modelling](./tutorials/Homology-Modelling/setup.md) 357 | - [MD](./tutorials/MD/setup.md) 358 | - [Docking](./tutorials/Docking/README.md) 359 | 360 | [Anaconda]: https://www.anaconda.com/products/individual 361 | [Anaconda install instructions]: https://www.anaconda.com/products/individual 362 | [conda]: https://docs.conda.io/en/latest/ 363 | [environments]: https://docs.conda.io/projects/conda/en/latest/user-guide/getting-started.html#managing-envs 364 | [getting started guide]: https://docs.conda.io/projects/conda/en/latest/user-guide/getting-started.html 365 | [Jupyter notebooks]: https://jupyter.org/ 366 | [python tutorial]: ./tutorials/Python/setup.md 367 | [apple terminal support]: https://support.apple.com/en-gb/guide/terminal/apd5265185d-f365-44cb-8b09-71a064a42125/mac#:~:text=Open%20Terminal&text=Click%20the%20Launchpad%20icon%20in,%2C%20then%20double%2Dclick%20Terminal. 368 | -------------------------------------------------------------------------------- /tests/environment.yml: -------------------------------------------------------------------------------- 1 | name: OxPython 2 | channels: 3 | - jaimergp/label/unsupported-cudatoolkit-shim 4 | - conda-forge 5 | dependencies: 6 | # Base 7 | - pip 8 | - ipython 9 | 10 | # Jupyter 11 | - notebook 12 | - jupyter_contrib_nbextensions 13 | 14 | # Maths 15 | - numpy 16 | 17 | # Viz 18 | - matplotlib 19 | 20 | # MD 21 | - MDAnalysis>=2.0.0 22 | - nglview 23 | - openmm 24 | - parmed 25 | 26 | # testing 27 | - pytest 28 | - nbval 29 | - ipywidgets<8 30 | -------------------------------------------------------------------------------- /tests/pytest.ini: -------------------------------------------------------------------------------- 1 | [pytest] 2 | testpaths = 3 | ../tutorials/Python/00_Introduction/ 4 | ../tutorials/Python/01_BasicTypes/ 5 | ../tutorials/Python/02_Variables/ 6 | ../tutorials/Python/03_Loops/ 7 | ../tutorials/Python/04_Conditionals/ 8 | ../tutorials/Python/05_Lists/ 9 | ../tutorials/Python/06_FileHandling/ 10 | ../tutorials/Python/07_Functions/ 11 | ../tutorials/Python/08_Documentation/ 12 | ../tutorials/Python/09_Libraries/ 13 | ../tutorials/Python/10_Numpy/ 14 | ../tutorials/Python/11_OOP/ 15 | ../tutorials/Python/12_ProteinAnalysis/ 16 | -------------------------------------------------------------------------------- /tutorials/Docking/CONTRIBUTORS.MD: -------------------------------------------------------------------------------- 1 | # List of contributors to the Oxford Computational Biochemistry Docking Course 2 | 3 | Authors are added at each year of contribution and sorted by last name alphabetical order. 4 | 5 | ## 2022 6 | - Philip Biggin 7 | - Afroditi-Maria Zaki 8 | 9 | ## 2020 10 | 11 | - Rocco Meli 12 | - Philip Biggin 13 | 14 | ## 2019 15 | 16 | - Irfan Alibay 17 | 18 | 19 | ## 2018 20 | 21 | - Joe Bluck 22 | - Philip Biggin 23 | 24 | ## 2017 25 | 26 | - Philip Biggin 27 | - Marc Daemgen 28 | - Naushad Velgy 29 | 30 | ## 2016 31 | - Philip Biggin 32 | - Matteo Aldeghi 33 | 34 | ## 2015 35 | - Philip Biggin 36 | - Matteo Aldeghi 37 | 38 | ## 2014 39 | - Philip Biggin 40 | - Julia Busch 41 | - Laura Domicevica 42 | 43 | ## 2013 44 | - Philip Biggin 45 | - Matteo Aldeghi 46 | - Laura Domicevica 47 | 48 | ## 2012 49 | - Greg Ross 50 | - Philip Biggin 51 | 52 | ## 2011 53 | - Greg Ross 54 | - Philip Biggin 55 | 56 | ## 2010 57 | - Ranjit Vijayan 58 | - Philip Biggin 59 | 60 | ## 2009 61 | - Ranjit Vijayan 62 | - Philip Biggin 63 | 64 | ## 2008 65 | - Ranjit Vijayan 66 | - Philip Biggin 67 | -------------------------------------------------------------------------------- /tutorials/Docking/README.md: -------------------------------------------------------------------------------- 1 | # Oxford Computational Biochemistry Docking Tutorial 2 | 3 | ## Contents 4 | 5 | This tutorial teaches the basics of molecular docking. 6 | 7 | Here we dock small molecules indinavir and nelfinavir into HIV-1 protease. 8 | 9 | ## Getting started 10 | 11 | Please see [https://bigginlab.github.io/OxCompBio/docking.html](https://bigginlab.github.io/OxCompBio/docking.html) for the tutorial instructions, including installation instructions for the software below. 12 | 13 | If you are doing the OxCompBio course, everything has already been setup for you and the required software should work. 14 | If you want to do this tutorial on your own, you need to complete the following sections: 15 | 16 | Appendix III: Installing the packages locally 17 | 18 | 19 | You can navigate to the other setup instructions through the following: 20 | 21 | - [Return to the main setup instructions](../../setup.md) 22 | - [Python tutorial setup instructions](../Python/setup.md) 23 | - [MD tutorial setup instructions](../MD/setup.md) 24 | - [Homology Modelling setup instructions](../Homology-Modelling/setup.md) 25 | 26 | 27 | ## Requirements 28 | 29 | The following software installations are required to do this workshop: 30 | 31 | For the molecular docking calculations: 32 | - [Autodock Vina](http://vina.scripps.edu/) 33 | 34 | For analysis and visualisation: 35 | - [Pymol](https://pymol.org/2/) 36 | 37 | Also for analysis and visualisation: 38 | - [VMD](https://www.ks.uiuc.edu/Research/vmd/) 39 | 40 | Alternative for system preparation (optional): 41 | - [AutodockTools](http://autodock.scripps.edu/resources/adt) 42 | 43 | For electrostatics visualization 44 | - [APBS](https://server.poissonboltzmann.org/) 45 | -------------------------------------------------------------------------------- /tutorials/Docking/docking-2020.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/tutorials/Docking/docking-2020.tar.gz -------------------------------------------------------------------------------- /tutorials/Docking/docking-2022.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/tutorials/Docking/docking-2022.pdf -------------------------------------------------------------------------------- /tutorials/Docking/images/hiv-protease.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/tutorials/Docking/images/hiv-protease.png -------------------------------------------------------------------------------- /tutorials/Docking/images/indinavir.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/tutorials/Docking/images/indinavir.png -------------------------------------------------------------------------------- /tutorials/Docking/images/nelfinavir.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/tutorials/Docking/images/nelfinavir.png -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/01_TM/OmpA_TM_pymol.fasta: -------------------------------------------------------------------------------- 1 | >obj01 2 | MAPKDNTWYTGAKLGWSQYHDTGLINNNGPTHENKLGAGAFGGYQVNPYVGFEMGYDWLGRMPYKGSVEN 3 | GAYKAQGVQLTAKLGYPITDDLDIYTRLGGMVWRADTYSNVYGKNHDTGVSPVFAGGVEYAITPEIATRL 4 | EYQWTNNIGDAHTIGTRPDNGMLSLGVSYRFG 5 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/01_TM/OmpA_alignment_TM.clw: -------------------------------------------------------------------------------- 1 | CLUSTAL multiple sequence alignment by MUSCLE (3.8) 2 | 3 | 4 | sp|Q8ZG77|OMPA_YERPE -APKDNTWYTGGKLGWSQYQDTGSI----INNDGPTHKDQLGAGAFFGYQANQYLGFEMG 5 | 2K0L_1|Chain -APKDNTWYAGGKLGWSQYHDTGFYGNGFQNNNGPTRNDQLGAGAFGGYQVNPYLGFEMG 6 | 1BXW_1|Chain MAPKDNTWYTGAKLGWSQYHDTGLI-----NNNGPTHENKLGAGAFGGYQVNPYVGFEMG 7 | ********:*.*******:*** **:***.:::****** ***.* *:***** 8 | 9 | sp|Q8ZG77|OMPA_YERPE YDWLGRMPYKGDINNGAFKAQGVQLAAKLSYPVAQDLDVYTRLGGLVWRADAKGSFDGGL 10 | 2K0L_1|Chain YDWLGRMAYKGSVDNGAFKAQGVQLTAKLGYPITDDLDIYTRLGGMVWRADSKGNYASTG 11 | 1BXW_1|Chain YDWLGRMPYKGSVENGAYKAQGVQLTAKLGYPITDDLDIYTRLGGMVWRADTYSNVYG-- 12 | *******.***.::***:*******:***.**:::***:******:*****: .. . 13 | 14 | sp|Q8ZG77|OMPA_YERPE DRASGHDTGVSPLVALGAEYAWTKNWATRMEYQWVNNIGDRETVGARPDNGLLSVGVSYR 15 | 2K0L_1|Chain VSRSEHDTGVSPVFAGGVEWAVTRDIATRLEYQWVNNIGDAGTVGTRPDNGMLSLGVSYR 16 | 1BXW_1|Chain ---KNHDTGVSPVFAGGVEYAITPEIATRLEYQWTNNIGDAHTIGTRPDNGMLSLGVSYR 17 | . *******:.* *.*:* * : ***:****.***** *:*:*****:**:***** 18 | 19 | sp|Q8ZG77|OMPA_YERPE FG 20 | 2K0L_1|Chain FG 21 | 1BXW_1|Chain FG 22 | ** 23 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/01_TM/OmpA_alignment_TM.pir: -------------------------------------------------------------------------------- 1 | >P1;sp|Q8ZG77|OMPA_YERPE/1-177 2 | sp|Q8ZG77|OMPA_YERPE 177 residues 3 | -APKDNTWYTGGKLGWSQYQDTGSI----INNDGPTHKDQLGAGAFFGYQANQYLGFEMGYDWLGRMPYKGD 4 | INNGAFKAQGVQLAAKLSYPVAQDLDVYTRLGGLVWRADAKGSFDGGLDRASGHDTGVSPLVALGAEYAWTK 5 | NWATRMEYQWVNNIGDRETVGARPDNGLLSVGVSYRFG* 6 | >P1;2K0L_1|Chain/1-181 7 | 2K0L_1|Chain 181 residues 8 | -APKDNTWYAGGKLGWSQYHDTGFYGNGFQNNNGPTRNDQLGAGAFGGYQVNPYLGFEMGYDWLGRMAYKGS 9 | VDNGAFKAQGVQLTAKLGYPITDDLDIYTRLGGMVWRADSKGNYASTGVSRSEHDTGVSPVFAGGVEWAVTR 10 | DIATRLEYQWVNNIGDAGTVGTRPDNGMLSLGVSYRFG* 11 | >P1;1BXW_1|Chain/1-172 12 | 1BXW_1|Chain 172 residues 13 | MAPKDNTWYTGAKLGWSQYHDTGLI-----NNNGPTHENKLGAGAFGGYQVNPYVGFEMGYDWLGRMPYKGS 14 | VENGAYKAQGVQLTAKLGYPITDDLDIYTRLGGMVWRADTYSNVYG-----KNHDTGVSPVFAGGVEYAITP 15 | EIATRLEYQWTNNIGDAHTIGTRPDNGMLSLGVSYRFG* 16 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/01_TM/ompa.fasta: -------------------------------------------------------------------------------- 1 | >sp|Q8ZG77|OMPA_YERPE Outer membrane protein A OS=Yersinia pestis OX=632 GN=ompA PE=1 SV=1 2 | MKKTAIALAVALVGFATVAQAAPKDNTWYTGGKLGWSQYQDTGSIINNDGPTHKDQLGAG 3 | AFFGYQANQYLGFEMGYDWLGRMPYKGDINNGAFKAQGVQLAAKLSYPVAQDLDVYTRLG 4 | GLVWRADAKGSFDGGLDRASGHDTGVSPLVALGAEYAWTKNWATRMEYQWVNNIGDRETV 5 | GARPDNGLLSVGVSYRFGQEDAAAPIVAPTPAPAPIVDTKRFTLKSDVLFGFNKANLKPE 6 | GQQALDQLYAQLSSIDPKDGSVVVLGFADRIGQPAPNLALSQRRADSVRDYLVSKGIPAD 7 | KITARGEGQANPVTGNTCDNVKPRAALIECLAPDRRVEIEVKGYKEVVTQPQA 8 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/01_TM/ompa_Cter.fasta: -------------------------------------------------------------------------------- 1 | >sp|Q8ZG77|OMPA_YERPE OmpA_Cter OS=Yersinia pestis OX=632 GN=ompA PE=1 SV=1 2 | QEDAAAPIVAPTPAPAPIVDTKRFTLKSDVLFGFNKANLKPEGQQALDQLYA 3 | QLSSIDPKDGSVVVLGFADRIGQPAPNLALSQRRADSVRDYLVSKGIPADKIT 4 | ARGEGQANPVTGNTCDNVKPRAALIECLAPDRRVEIEVKGYKEVVTQPQA 5 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/01_TM/ompa_E_PDB.fasta: -------------------------------------------------------------------------------- 1 | >1BXW_1|Chain A|PROTEIN (OUTER MEMBRANE PROTEIN A)|Escherichia coli BL21(DE3) (469008) 2 | MAPKDNTWYTGAKLGWSQYHDTGLINNNGPTHENKLGAGAFGGYQVNPYVGFEMGYDWLGRMPYKGSVENGAYKAQGVQLTAKLGYPITDDLDIYTRLGGMVWRADTYSNVYGKNHDTGVSPVFAGGVEYAITPEIATRLEYQWTNNIGDAHTIGTRPDNGMLSLGVSYRFG 3 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/01_TM/ompa_E_PDB_TM.fasta: -------------------------------------------------------------------------------- 1 | >1BXW_1|Chain A|PROTEIN (OUTER MEMBRANE PROTEIN A)|Escherichia coli BL21(DE3) (469008) 2 | MAPKDNTWYTGAKLGWSQYHDTGLINNNGPTHENKLGAGAFGGYQVNPYVGFEMGYDWLGRMPYKGSVENGAYKAQGVQLTAKLGYPITDDLDIYTRLGGMVWRADTYSNVYGKNHDTGVSPVFAGGVEYAITPEIATRLEYQWTNNIGDAHTIGTRPDNGMLSLGVSYRFG 3 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/01_TM/ompa_E_PDB_TM_noM.fasta: -------------------------------------------------------------------------------- 1 | >1BXW_1|Chain A|PROTEIN (OUTER MEMBRANE PROTEIN A)|Escherichia coli BL21(DE3) (469008) 2 | APKDNTWYTGAKLGWSQYHDTGLINNNGPTHENKLGAGAFGGYQVNPYVGFEMGYDWLGRMPYKGSVENGAYKAQGVQLTAKLGYPITDDLDIYTRLGGMVWRADTYSNVYGKNHDTGVSPVFAGGVEYAITPEIATRLEYQWTNNIGDAHTIGTRPDNGMLSLGVSYRFG 3 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/01_TM/ompa_K_PDB.fasta: -------------------------------------------------------------------------------- 1 | >2K0L_1|Chain A|Outer membrane protein A|Klebsiella pneumoniae (573) 2 | ARIMKAIFVLNAAPKDNTWYAGGKLGWSQYHDTGFYGNGFQNNNGPTRNDQLGAGAFGGYQVNPYLGFEMGYDWLGRMAYKGSVDNGAFKAQGVQLTAKLGYPITDDLDIYTRLGGMVWRADSKGNYASTGVSRSEHDTGVSPVFAGGVEWAVTRDIATRLEYQWVNNIGDAGTVGTRPDNGMLSLGVSYRFGQEDAAPVVAPAPAPAPEHHHHHH 3 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/01_TM/ompa_K_PDB_TM.fasta: -------------------------------------------------------------------------------- 1 | >2K0L_1|Chain A|Outer membrane protein A|Klebsiella pneumoniae (573) 2 | APKDNTWYAGGKLGWSQYHDTGFYGNGFQNNNGPTRNDQLGAGAFGGYQVNPYLGFEMGYDWLGRMAYKGSVDNGAFKAQGVQLTAKLGYPITDDLDIYTRLGGMVWRADSKGNYASTGVSRSEHDTGVSPVFAGGVEWAVTRDIATRLEYQWVNNIGDAGTVGTRPDNGMLSLGVSYRFG 3 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/01_TM/ompa_TM.fasta: -------------------------------------------------------------------------------- 1 | >sp|Q8ZG77|OMPA_YERPE OmpA_TM OS=Yersinia pestis OX=632 GN=ompA PE=1 SV=1 2 | APKDNTWYTGGKLGWSQYQDTGSIINNDGPTHKDQLGAGAFFGYQANQYLGFEMGYDWLG 3 | RMPYKGDINNGAFKAQGVQLAAKLSYPVAQDLDVYTRLGGLVWRADAKGSFDGGLDRASG 4 | HDTGVSPLVALGAEYAWTKNWATRMEYQWVNNIGDRETVGARPDNGLLSVGVSYRFG 5 | 6 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/01_TM/pairwise_alignment_TM.ali: -------------------------------------------------------------------------------- 1 | >P1;OmpATM 2 | structureX:OmpATM:0:A:172:A::: 0.00: 0.00 3 | MAPKDNTWYTGAKLGWSQYHDTGLI-----NNNGPTHENKLGAGAFGGYQVNPYVGFEMGYDWLGRMPYKGS 4 | VENGAYKAQGVQLTAKLGYPITDDLDIYTRLGGMVWRADTYSNVYG-----KNHDTGVSPVFAGGVEYAITP 5 | EIATRLEYQWTNNIGDAHTIGTRPDNGMLSLGVSYRFG* 6 | >P1;seq_YPTM 7 | sequence:seq_YPTM::::::: 0.00: 0.00 8 | -APKDNTWYTGGKLGWSQYQDTGSI----INNDGPTHKDQLGAGAFFGYQANQYLGFEMGYDWLGRMPYKGD 9 | INNGAFKAQGVQLAAKLSYPVAQDLDVYTRLGGLVWRADAKGSFDGGLDRASGHDTGVSPLVALGAEYAWTK 10 | NWATRMEYQWVNNIGDRETVGARPDNGLLSVGVSYRFG* 11 | 12 | 13 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/01_TM/run_modeller.py: -------------------------------------------------------------------------------- 1 | from modeller import * 2 | from modeller.automodel import * # Load the automodel class 3 | 4 | log.verbose() 5 | env = environ() 6 | 7 | # directories for input atom files 8 | env.io.atom_files_directory = ['.', '.'] 9 | 10 | a = automodel(env, 11 | alnfile = 'pairwise_alignment_TM.ali', # alignment filename 12 | knowns = 'OmpATM', # codes of the templates 13 | sequence = 'seq_YPTM') # code of the target - we can call this what we like 14 | 15 | a.starting_model= 1 # index of the first model 16 | a.ending_model = 1 # index of the last model as this is 1 and the start is 1 - we will just generated 1 model 17 | # (determines how many models to calculate) 18 | a.make() # do comparative modeling 19 | 20 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/01_TM/seq_YPTM.fasta: -------------------------------------------------------------------------------- 1 | >sp|Q8ZG77|OMPA_YERPE OmpA_TM OS=Yersinia pestis OX=632 GN=ompA PE=1 SV=1 2 | APKDNTWYTGGKLGWSQYQDTGSIINNDGPTHKDQLGAGAFFGYQANQYLGFEMGYDWLG 3 | RMPYKGDINNGAFKAQGVQLAAKLSYPVAQDLDVYTRLGGLVWRADAKGSFDGGLDRASG 4 | HDTGVSPLVALGAEYAWTKNWATRMEYQWVNNIGDRETVGARPDNGLLSVGVSYRFG 5 | 6 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/01_TM/seq_YPTM.sch: -------------------------------------------------------------------------------- 1 | # MTH NRNG 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 2 | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 3 | 1 1 2 0.010 0.010 0.010 0.010 0.000 0.000 0.000 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.000 0.000 0.000 0.010 4 | 2 1 4 0.100 0.100 0.100 0.100 0.000 0.000 0.000 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.000 0.000 0.000 0.100 5 | 3 1 6 0.500 0.500 0.500 0.500 0.000 0.000 0.000 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.000 0.000 0.000 0.500 6 | 4 1 10 1.000 1.000 1.000 1.000 0.000 0.000 0.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 0.000 0.000 0.000 1.000 7 | 5 1 20 1.000 1.000 1.000 1.000 0.000 0.000 0.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 0.000 0.000 0.000 1.000 8 | 6 1 30 1.000 1.000 1.000 1.000 0.000 0.000 0.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 0.000 0.000 0.000 1.000 9 | 7 1 50 1.000 1.000 1.000 1.000 0.010 0.010 0.010 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 0.010 0.010 0.010 1.000 10 | 8 1 80 1.000 1.000 1.000 1.000 0.100 0.100 0.100 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 0.100 0.100 0.100 1.000 11 | 9 1 120 1.000 1.000 1.000 1.000 0.500 0.500 0.500 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 0.500 0.500 0.500 1.000 12 | 10 1 200 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 13 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/02_Cter/OmpA_Cter.pir: -------------------------------------------------------------------------------- 1 | >P1;sp|Q8ZG77|OMPA_YERPE/1-155 2 | sp|Q8ZG77|OMPA_YERPE 155 residues 3 | QEDAAAPIVAPTPAPAPIVDTKRFTLKSDVLFGFNKANLKPEGQQALDQLYAQLSSIDPKDGSVVVLGFADR 4 | IGQPAPNLALSQRRADSVRDYLVSKGIPADKITARGEGQANPVTGNTCDNVKPRAALIECLAPDRRVEIEVK 5 | GYKEVVTQPQA* 6 | >P1;pdb|5NHX|A/1-126 7 | pdb|5NHX|A 126 residues 8 | -------------------ATKHFTLKSDVLFNFNKATLKPEGQQALDQLYTQLSNMDPKDGSAVVLGYTDR 9 | IGSEAYNQQLSEKRAQSVVDYLVAKGIPAGKISARGMGESNPVTGNTCDNVKARAALIDCLAPDRRVEIEVK 10 | G----------* 11 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/02_Cter/delafter/ompa_Cter.fasta: -------------------------------------------------------------------------------- 1 | >sp|Q8ZG77|OMPA_YERPE OmpA_Cter OS=Yersinia pestis OX=632 GN=ompA PE=1 SV=1 2 | QEDAAAPIVAPTPAPAPIVDTKRFTLKSDVLFGFNKANLKPEGQQALDQLYA 3 | QLSSIDPKDGSVVVLGFADRIGQPAPNLALSQRRADSVRDYLVSKGIPADKIT 4 | ARGEGQANPVTGNTCDNVKPRAALIECLAPDRRVEIEVKGYKEVVTQPQA 5 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/02_Cter/delafter/pairwise_alignment_CT.ali: -------------------------------------------------------------------------------- 1 | >P1;OmpACT 2 | structureX:OmpACT:4:A:129:A::: 0.00: 0.00 3 | --------------------ATKHFTLKSDVLFNFNKATLKPEGQQALDQLYTQLSNMDPKDGSAVVLGYTD 4 | RIGSEAYNQQLSEKRAQSVVDYLVAKGIPAGKISARGMGESNPVTGNTCDNVKARAALIDCLAPDRRVEIEV 5 | KG----------* 6 | >P1;seq_YPCT_G 7 | sequence:seq_YPCT_G::::::: 0.00: 0.00 8 | GQEDAAAPIVAPTPAPAPIVDTKRFTLKSDVLFGFNKANLKPEGQQALDQLYAQLSSIDPKDGSVVVLGFAD 9 | RIGQPAPNLALSQRRADSVRDYLVSKGIPADKITARGEGQANPVTGNTCDNVKPRAALIECLAPDRRVEIEV 10 | KGYKEVVTQPQA* 11 | 12 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/02_Cter/delafter/run_modeller_score.py: -------------------------------------------------------------------------------- 1 | from modeller import * 2 | from modeller.automodel import * # Load the automodel class 3 | 4 | log.verbose() 5 | env = environ() 6 | 7 | # directories for input atom files 8 | env.io.atom_files_directory = ['.', '.'] 9 | 10 | a = automodel(env, 11 | alnfile = 'pairwise_alignment_CT.ali', # alignment filename 12 | knowns = 'OmpACT', # codes of the templates 13 | sequence = 'seq_YPCT_G', # code of the target - we can call this what we like 14 | assess_methods=(assess.DOPE, 15 | assess.GA341)) 16 | 17 | 18 | a.starting_model= 1 # index of the first model 19 | a.ending_model = 10 # index of the last model as this is 1 and the start is 1 - we will just generated 1 model 20 | # (determines how many models to calculate) 21 | a.make() # do comparative modeling 22 | 23 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/02_Cter/delafter/seq_YPCT_G.fasta: -------------------------------------------------------------------------------- 1 | >sp|Q8ZG77|OMPA_YERPE OmpA_Cter OS=Yersinia pestis OX=632 GN=ompA PE=1 SV=1 2 | GQEDAAAPIVAPTPAPAPIVDTKRFTLKSDVLFGFNKANLKPEGQQALDQLYAQ 3 | LSSIDPKDGSVVVLGFADRIGQPAPNLALSQRRADSVRDYLVSKGIPADKITAR 4 | GEGQANPVTGNTCDNVKPRAALIECLAPDRRVEIEVKGYKEVVTQPQA 5 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/02_Cter/pairwise_alignment_CT.ali: -------------------------------------------------------------------------------- 1 | >P1;OmpACT 2 | structureX:OmpACT:4:A:129:A::: 0.00: 0.00 3 | --------------------ATKHFTLKSDVLFNFNKATLKPEGQQALDQLYTQLSNMDPKDGSAVVLGYTD 4 | RIGSEAYNQQLSEKRAQSVVDYLVAKGIPAGKISARGMGESNPVTGNTCDNVKARAALIDCLAPDRRVEIEV 5 | KG----------* 6 | >P1;seq_YPCT_G 7 | sequence:seq_YPCT_G::::::: 0.00: 0.00 8 | GQEDAAAPIVAPTPAPAPIVDTKRFTLKSDVLFGFNKANLKPEGQQALDQLYAQLSSIDPKDGSVVVLGFAD 9 | RIGQPAPNLALSQRRADSVRDYLVSKGIPADKITARGEGQANPVTGNTCDNVKPRAALIECLAPDRRVEIEV 10 | KGYKEVVTQPQA* 11 | 12 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/02_Cter/run_modeller_score.py: -------------------------------------------------------------------------------- 1 | from modeller import * 2 | from modeller.automodel import * # Load the automodel class 3 | 4 | log.verbose() 5 | env = environ() 6 | 7 | # directories for input atom files 8 | env.io.atom_files_directory = ['.', '.'] 9 | 10 | a = automodel(env, 11 | alnfile = 'pairwise_alignment_CT.ali', # alignment filename 12 | knowns = 'OmpACT', # codes of the templates 13 | sequence = 'seq_YPCT_G', # code of the target - we can call this what we like 14 | assess_methods=(assess.DOPE, 15 | assess.GA341)) 16 | 17 | 18 | a.starting_model= 1 # index of the first model 19 | a.ending_model = 10 # index of the last model as this is 1 and the start is 1 - we will just generated 1 model 20 | # (determines how many models to calculate) 21 | a.make() # do comparative modeling 22 | 23 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/02_Cter/seq_YPCT_G.sch: -------------------------------------------------------------------------------- 1 | # MTH NRNG 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 2 | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 3 | 1 1 2 0.010 0.010 0.010 0.010 0.000 0.000 0.000 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.010 0.000 0.000 0.000 0.010 4 | 2 1 4 0.100 0.100 0.100 0.100 0.000 0.000 0.000 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.100 0.000 0.000 0.000 0.100 5 | 3 1 6 0.500 0.500 0.500 0.500 0.000 0.000 0.000 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.500 0.000 0.000 0.000 0.500 6 | 4 1 10 1.000 1.000 1.000 1.000 0.000 0.000 0.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 0.000 0.000 0.000 1.000 7 | 5 1 20 1.000 1.000 1.000 1.000 0.000 0.000 0.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 0.000 0.000 0.000 1.000 8 | 6 1 30 1.000 1.000 1.000 1.000 0.000 0.000 0.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 0.000 0.000 0.000 1.000 9 | 7 1 50 1.000 1.000 1.000 1.000 0.010 0.010 0.010 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 0.010 0.010 0.010 1.000 10 | 8 1 80 1.000 1.000 1.000 1.000 0.100 0.100 0.100 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 0.100 0.100 0.100 1.000 11 | 9 1 120 1.000 1.000 1.000 1.000 0.500 0.500 0.500 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 0.500 0.500 0.500 1.000 12 | 10 1 200 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 13 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/03_loop_refine/refine_loops.py: -------------------------------------------------------------------------------- 1 | # Loop refinement of an existing model 2 | from modeller import * 3 | from modeller.automodel import * 4 | 5 | log.verbose() 6 | env = environ() 7 | 8 | # directories for input atom files 9 | env.io.atom_files_directory = ['.'] # needed if you put your coordinate files somewhere 10 | 11 | # Create a new class based on 'loopmodel' so that we can redefine 12 | # select_loop_atoms (necessary) 13 | class MyLoop(loopmodel): 14 | # This routine picks the residues to be refined by loop modeling 15 | def select_loop_atoms(self): 16 | # 10 residue insertion 17 | return selection(self.residue_range('177:A', '195:A')) # You will have CHANGE THIS FOR YOUR MODEL - 18 | # these refer to the muscle_nAChR.pdb file - 19 | # so change for your template that you call below... 20 | 21 | m = MyLoop(env, 22 | inimodel='Full_OmpA_YP.pdb', # initial model of the target - CHANGE TO YOUR TEMPLATE PDB 23 | sequence='Full_OmpA_YP') # code of the target 24 | 25 | m.loop.starting_model= 1 # index of the first loop model 26 | m.loop.ending_model = 10 # index of the last loop model 27 | m.loop.md_level = refine.very_fast # loop refinement method; this yields 28 | # models quickly but of low quality; 29 | # use refine.slow for better models 30 | 31 | m.make() 32 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/03_loop_refine/score_dope_obj.dat: -------------------------------------------------------------------------------- 1 | -13441.14258 695.7407 2 | -13535.98438 688.8433 3 | -13488.87891 659.8948 4 | -13542.64941 752.0714 5 | -13073.04785 768.9686 6 | -13442.33203 730.2045 7 | -13406.02734 727.0439 8 | -13394.29980 735.2073 9 | -13446.65234 766.6654 10 | -13333.38379 675.8484 11 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/05_quality_check/charts/prep-chirals.txt: -------------------------------------------------------------------------------- 1 | SUMMARY: 0 total outliers at 373 tetrahedral centers (0.00%) 2 | SUMMARY: 0 handedness outliers at 323 chiral centers (0.00%) 3 | SUMMARY: 0 tetrahedral geometry outliers 4 | SUMMARY: 0 pseudochiral naming errors 5 | 6 | 7 | 8 | Handedness swaps 9 | ---------------------------------------------------------------------- 10 | None 11 | ---------------------------------------------------------------------- 12 | 13 | 14 | Tetrahedral geometry outliers 15 | ---------------------------------------------------------------------- 16 | None 17 | ---------------------------------------------------------------------- 18 | 19 | 20 | Probable atom naming errors around pseudochiral centers 21 | e.g. CG1 and CG2 around Valine CB 22 | ---------------------------------------------------------------------- 23 | None 24 | ---------------------------------------------------------------------- 25 | -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/05_quality_check/charts/prep-rama.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/tutorials/Homology-Modelling/DATA/05_quality_check/charts/prep-rama.pdf -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/DATA/05_quality_check/molprobity.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/tutorials/Homology-Modelling/DATA/05_quality_check/molprobity.zip -------------------------------------------------------------------------------- /tutorials/Homology-Modelling/README.md: -------------------------------------------------------------------------------- 1 | # Oxford Computational Biochemistry Homology Modelling Tutorial 2 | 3 | ## Content 4 | 5 | Contained here is a tutorial to get your started with the basics of Homology Modelling. 6 | 7 | The tutorial is self contained within the `homology-modelling-tutorial.ipynb` jupyter notebook. 8 | 9 | 10 | ## Getting started 11 | 12 | If you are doing the Oxford Biochemistry course, just start exploring the jupyter notebook. 13 | 14 | Otherwise, note that you will need to have jupyter notebook already installed as well as pymol, jalview and modeller. 15 | -------------------------------------------------------------------------------- /tutorials/Python/00_Introduction/no_solutions/Introduction.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 0. Introduction" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## Course overview\n", 15 | "\n", 16 | "This tutorial aims to teach the basics of Python programming with a focus on its application in computational biochemistry. Assuming no previous knowledge of Python, we start by explaining the basic building blocks (e.g. variables, conditionals, and lists) and build upon these to show examples of how Python can be used to solve problems related to computational biochemistry (e.g. the analysis of protein structures and Molecular Dynamics simulations).\n", 17 | "\n", 18 | "Learning how to fully use a programming language, especially for the first time, is a task which can easily take several months to achieve. Indeed, the Python programming language is rather vast and fully covering it would never be achievable within the timeframes allowed by this course. Instead, this workshop aims to offer attendees a jumping point into learning more about Python and its many applications.\n", 19 | "\n", 20 | "We recommend the following useful resources for Python:\n", 21 | "- [Python Documentation](https://docs.python.org/3.11/) (tutorials, library reference, ...)\n", 22 | "- [CodeAcademy Python tutorial](https://www.codecademy.com/learn/learn-python)\n", 23 | "\n" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "## Python programming - what and why?\n", 31 | "\n", 32 | "The Python programming language is a general-purpose interpreted object-orientated programming language which has existed for over 30 years. Originally developed by Guido van Rossum, the language has matured over the years into a incredibly large ecosystem with millions of developers. Python is not under the care of the Python Software Foundation and usually has yearly releases of the Python programming language, comprising of new features and bug fixes, with the latest release being python 3.11. More information on Python and its development process can be found here: https://www.python.org/. As this workshop concentrates solely on the use of the Python programming language, one could rightly ask why Python and not one of the hundreds of other programming languages that currently exist? There are several reasons as to why this choice was made:\n", 33 | "\n", 34 | "1. Python has over recent years become a commonly used tool by scientists looking to rapidly create, explore and analyse data. Many existing scientific tools either exist purely in Python or offer Python interfaces (for example [alphafold](https://github.com/deepmind/alphafold) or the biomolecular viewer [ChimeraX](https://www.cgl.ucsf.edu/chimerax/). It has therefore become important for many scientists to have a working understanding of how to use basic Python commands.\n", 35 | "\n", 36 | "\n", 37 | "2. Python is very flexible and easy to play with. In part due to its nature as an `interpreted language` (unlike languages like C++ and Fortran which are _compiled languages_ that require to be passed through a compiler before a computer can understand written code), it is easy to make modifications to code and directly see how it may affect outcomes. This does come at the cost of performance, with Python being slower than compiled languages, however there are many ways in which savvy programmers can go around this limitation.\n", 38 | "\n", 39 | "\n", 40 | "3. Python is portable, free to use and open-source. Unlike programming language such as Matlab it does not require a paid license to operate. It also works equally well on any operating system, be it; Linux, Windows or MacOS.\n", 41 | "\n", 42 | "\n", 43 | "4. Python comes with an incredibly large ecosystem with many software packages to extend its usage. We unfortunately won’t be touching upon this in this tutorial, but there are many ways to extend Python, with tools such as NumPy for scientific computing (https://numpy.org/), scikit-learn for machine learning (https://scikit-learn.org/stable/), and matplotlib for drawing figures (https://matplotlib.org/). These massively expand the base utility of the Python programming language, allowing for complex tools and packages to be written.\n", 44 | "\n", 45 | "That being said, this workshop does not aim to advocate for one programming language over any other. Indeed, each programming language has its own advantage and disadvantages based on the intended use case. With most programming languages having very similar core components and concepts, we hope that this primer to Python programming will serve useful for whichever programming language you many end up using in the future.\n", 46 | "\n", 47 | "Other commonly used interpreted languages such as Matlab and R. Matlab's interpreter is not free and open source, so you won't be able to run your code unless you keep paying for a licence. R is a fantastic language for statistical computing, but it is very domain-specific." 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "## License and Acknowledgements\n", 55 | "\n", 56 | "These workshop materials are licensed under a [CC BY SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/legalcode) license. The workshop materials are the product of several authors who have contributed to this course over the years. Please see [CONTRIBUTORS.md](https://github.com/IAlibay/oxcompbio-python/blob/main/CONTRIBUTORS.md) for a full list of acknowledgements. As the tutorial was not originally tracked on github, we extend our apologies to any authors which may have been missed from this list." 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "## Course structure\n", 64 | "\n", 65 | "The tutorial is thought across a series of jupyter notebooks:\n", 66 | "\n", 67 | "0. Introduction\n", 68 | "1. Basic Types\n", 69 | "2. Variables\n", 70 | "3. Conditionals\n", 71 | "4. Loops\n", 72 | "5. Lists\n", 73 | "6. File Handling\n", 74 | "7. Functions\n", 75 | "8. Documentation\n", 76 | "9. Libraries\n", 77 | "10. Numpy\n", 78 | "11. Object Orientated Programming (OOP)\n", 79 | "12. Protein Analysis\n", 80 | "13. Running and Analysing a short MD simulation\n", 81 | "\n", 82 | "These notebooks are contained within their own labelled/named directories. Within these directories you will also sometimes find `datafiles` sub-directories that contain all the relevant data to run the notebook.\n", 83 | "\n", 84 | "We recommend that each section be followed in order, however those with prior experience with Python may wish to jump to the later sections." 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "## Launching the notebooks\n", 92 | "\n", 93 | "Please see the `setup.md` \"Starting the tutorial\" instructions.\n", 94 | "\n", 95 | "Briefly, once you have cloned a copy of these materials and setup your `conda` environment, using a terminal (Anaconda prompt in Windows) do the following:\n", 96 | "\n", 97 | "1. Activate the conda environment: `conda activate OxPython`\n", 98 | "2. Start the jupyter notebook server: `jupyter notebook`\n", 99 | "3. From there navigate to the desired directory and click on the `.ipynb` notebook you wish to open.\n", 100 | "4. To close the notebooks, click on \"File\" -> \"Close and Halt\".\n", 101 | "5. You may then also want to quit the jupyter notebook server altogether.\n" 102 | ] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "metadata": {}, 107 | "source": [ 108 | "## Using the notebooks\n", 109 | "\n", 110 | "In order to better demonstrate how the Python programming language works, we employ the use of Jupyter notebooks in this tutorial. [Jupyter notebooks](https://jupyter.org/) are a useful way to demonstrate Python code in a clear and organised manner, whilst avoiding the need to write out files. In practice, one may need to write out code to a file and then run them directly through the command line, how to do this is not directly covered by this tutorial, but do ask a demonstrator for more information!\n", 111 | "\n", 112 | "Each notebook is composed of a series of `cells` which can either contain text (such as this one) or code (which will usually have the words `In []:` next to them). You can type in code `cells` by clicking on them and typing whatever changes you want to make.\n", 113 | "\n", 114 | "Code cells can be used to run any valid Python code. To do this, simply click on the cell and press the `Run` button in the toolbar above, or type `Shift+Enter`.\n", 115 | "\n", 116 | "> ⚠️ **Note:**\n", 117 | ">All code executed is retained in the `memory` of the notebook. That means that if you `import` a module or declare a variable, these will be seen and can interact with code executed in `cells` further down the notebook. There are many places in this tutorial where modules are imported early on in the notebook and used in subsequent code `cells`. We therefore recommend that tutorial users do not jump to later parts of the notebook without doing the earlier ones.\n", 118 | "\n", 119 | "\n", 120 | "Notebooks can be cleared to their original state by navigating to `Kernel > Restart & Clear Output`. A further prompt will ask you if you wish to clear all the outputs, pressing on this will return the notebook to a state where no code `cells` have been run.\n", 121 | "\n", 122 | "> ⚠️ **Note:**\n", 123 | ">When finished, it is important that you shut down the notebook properly. This can be done by clicking on `File > Close and Halt`.\n", 124 | "\n", 125 | "More information on using Jupyter notebooks (this is recommended pre-reading ahead of starting this workshop), can be found here: https://www.codecademy.com/article/how-to-use-jupyter-notebooks" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "## Running the notebooks from home\n", 133 | "\n", 134 | "Instructions on starting the workshop notebooks in the Biochemistry computer lab will be provided on the day. However, should you wish to run the the notebooks online from the convenience of your own computer, you can do so using either Google Colab or binder. Links can be found in the workshop [README](https://github.com/IAlibay/oxcompbio-python/blob/main/README.md)." 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": {}, 140 | "source": [ 141 | "## 0.1 A first command in python:\n", 142 | "\n", 143 | "Let's start things off with executing our first Python command. In the cell below we will use `print` to write out \"Hello world\". Try it out by clicking on the cell and pressing `Run` in the toolbar above." 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": null, 149 | "metadata": {}, 150 | "outputs": [], 151 | "source": [ 152 | "# A first Python command\n", 153 | "print('Hello world')" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "There is a lot happening here which we will cover in later parts of the workshop. `print` is a function which is offered by default in Python and specifically exists to print out text.\n", 161 | "\n", 162 | "The `print` function takes an input value (some text, in quotation marks) and prints the text as output (without the quotation marks). One should note that Python is case-sensitive - ie. we can't use `Print` or `PRINT`.\n", 163 | "\n", 164 | "Whilst `print` is a very simple function, it is one of the most useful. It allows us to write out any information relevant to our Python code. This can be anything from usage instruction to the output of some calculation.\n", 165 | "\n", 166 | "**Note:** As of Python 3, the inputs to the 'print' function must be placed within parantheses. If you use code developed for Python 2.7 or lower, you may notice that this requirement is not followed and therefore will cause issues if run using Python 3+. There are other key differences between Python 3 and previous versions, some of which will be covered in this tutorial. For a more detailed look into them, the following resource may be of use: [Porting Python 2 Code to Python 3](https://docs.python.org/3.6/howto/pyporting.html). As Python 2.7 reached end of life as of January 2020, we strongly recommend that no new code be written in Python 2.7. You may however notice that older / unmaintaned code may not have moved away from Python 2.7 yet." 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": {}, 172 | "source": [ 173 | "### A note on python comments\n", 174 | "\n", 175 | "You will have noticed in the above code block the presence of the words `# A first Python command` and how it does not affect the notebook output. In Python, in-line code comments are indicated using the `#` symbol. That is to say that any text that comes after this `#` is considered to be a \"comment\". Comments are ignored by the _Python interpreter_ and exist solely for the convenience of annotating code. It is considered good practice to comment your code in order to make it more readable for yourself and future users. Other means of commenting Python code, such as docstrings, also exist and will be briefly covered in section 8 of this tutorial.\n", 176 | "\n", 177 | "> Definition: The _Python interpreter_ is what takes any written Python code and turns it into commands that your computer can understand and execute." 178 | ] 179 | }, 180 | { 181 | "cell_type": "markdown", 182 | "metadata": {}, 183 | "source": [ 184 | "### Exercise 0.1.1\n", 185 | "\n", 186 | "In the cell below, use print to output the phrase \"This is the second command\"." 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": null, 192 | "metadata": {}, 193 | "outputs": [], 194 | "source": [ 195 | "# Exercise 0.1.1" 196 | ] 197 | }, 198 | { 199 | "cell_type": "markdown", 200 | "metadata": {}, 201 | "source": [ 202 | "## 0.2 Python scripts" 203 | ] 204 | }, 205 | { 206 | "cell_type": "markdown", 207 | "metadata": {}, 208 | "source": [ 209 | "Whilst jupyter notebooks can be useful for demonstrating Python code and prototyping new ideas, the main way in which to run Python code is to execute a script. To do this, you need to write the Python instructions within a file and execute it via the Python interpretor.\n", 210 | "\n", 211 | "For example if you were to write the following in a file (let's assume we call it hello.py):\n", 212 | "\n", 213 | "```\n", 214 | "print(\"Hello world\")\n", 215 | "```\n", 216 | "\n", 217 | "And then type `python hello.py` in a terminal, this should print out `Hello world`.\n", 218 | "\n", 219 | "As detailed above, this tutorial we will focuses on executing code within jupyter notebooks. However, it is worth remembering that using written scripts/code is often a much more reproducible and manageable way to use Python." 220 | ] 221 | }, 222 | { 223 | "cell_type": "markdown", 224 | "metadata": {}, 225 | "source": [ 226 | "## Review\n", 227 | "\n", 228 | "In this tutorial section we have:\n", 229 | "- Detailed the contents of the tutorial.\n", 230 | "- Shown how to execute and use a jupyter notebook session.\n", 231 | "- Used a function, `print`.\n", 232 | "- Discussed how to write Python scripts.\n", 233 | "- Discussed some of the advantages and disadvantages of Python" 234 | ] 235 | } 236 | ], 237 | "metadata": { 238 | "kernelspec": { 239 | "display_name": "Python 3 (ipykernel)", 240 | "language": "python", 241 | "name": "python3" 242 | }, 243 | "language_info": { 244 | "codemirror_mode": { 245 | "name": "ipython", 246 | "version": 3 247 | }, 248 | "file_extension": ".py", 249 | "mimetype": "text/x-python", 250 | "name": "python", 251 | "nbconvert_exporter": "python", 252 | "pygments_lexer": "ipython3", 253 | "version": "3.9.15" 254 | } 255 | }, 256 | "nbformat": 4, 257 | "nbformat_minor": 1 258 | } 259 | -------------------------------------------------------------------------------- /tutorials/Python/03_Loops/Loops.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 3 Loops" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Sometimes when programming you might need to do repetitive tasks. For example, doing a given operation with an increasingly larger number.\n", 15 | "\n", 16 | "Python offers loops as a way to make your life easier, and your code much smaller." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "## 3.1 While loops" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "Assume `number = 1` and you would like to add 1 until you reach 10. At each increment of `number` you want to print the value of the variable `number` to the screen. You could do it explicitly in the following manner:" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "number = 1\n", 40 | "print(\"number is equal to %d now.\" % number)\n", 41 | "number +=1\n", 42 | "print(\"number is equal to %d now.\" % number)\n", 43 | "number +=1\n", 44 | "print(\"number is equal to %d now.\" % number)\n", 45 | "number +=1\n", 46 | "print(\"number is equal to %d now.\" % number)\n", 47 | "number +=1\n", 48 | "print(\"number is equal to %d now.\" % number)\n", 49 | "number +=1\n", 50 | "print(\"number is equal to %d now.\" % number)\n", 51 | "number +=1\n", 52 | "print(\"number is equal to %d now.\" % number)\n", 53 | "number +=1\n", 54 | "print(\"number is equal to %d now.\" % number)\n", 55 | "number +=1\n", 56 | "print(\"number is equal to %d now.\" % number)\n", 57 | "number +=1\n", 58 | "print(\"number is equal to %d now.\" % number)\n", 59 | "number +=1" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "This will easily become tedious as the number of increments increases. Instead you can save yourself some time and just program this as a loop:" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | "number = 1\n", 76 | "while number <= 10:\n", 77 | " print(\"number is equal to %d now.\" % number)\n", 78 | " number += 1\n", 79 | " \n", 80 | "print(\"End of loop\")" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "The `while` loop is a useful construct which allows you to repeat a task while a certain statement is `True`.\n", 88 | "\n", 89 | "Here, this statement is `number <= 10`.\n", 90 | "\n", 91 | "The colon (`:`) after the statement marks the beginning of the task(s) that are to be performed in the loop.\n", 92 | "\n", 93 | "Once the statement becomes `False`, the loop will stop being executed and the code will move on to its next section (in this case we print \"End of loop\").\n", 94 | "\n", 95 | "\n", 96 | "#### Indents are important!\n", 97 | "\n", 98 | "As you can see from the code above, the task(s) to be done in the loop are indented. Indentation is how Python determines if a block of code belongs to a given method statement.\n", 99 | "\n", 100 | "For example, in the above code the presence of an indentation for:\n", 101 | "\n", 102 | "```python\n", 103 | "print(\"number is equal to %d now.\" % number)\n", 104 | "number += 1\n", 105 | "```\n", 106 | "\n", 107 | "Means that they belong to the `while` statement.\n", 108 | "\n", 109 | "However, since the:\n", 110 | "\n", 111 | "```python\n", 112 | "print(\"End of loop\")\n", 113 | "```\n", 114 | "does not have indentation, it does not get executed as part of the `while` statement.\n", 115 | "\n", 116 | "If the code was instead changed to indent the second `print` statement, you would see that it would be executed at every iteration of the `while` loop.\n" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": null, 122 | "metadata": {}, 123 | "outputs": [], 124 | "source": [ 125 | "number = 1\n", 126 | "while number <= 10:\n", 127 | " print(\"number is equal to %d now.\" % number)\n", 128 | " number += 1\n", 129 | " \n", 130 | " print(\"End of loop\")" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "### Aside" 138 | ] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "metadata": {}, 143 | "source": [ 144 | "`'%d' % number` is an example of string formatting, in this case we are 'inserting' the value of variables into a string (use `%d` for a number and `%s` for a string). If you want to insert multiple variables, you can use e.g.: `'%d %s' % (num, str)`. This is 'old-style' formatting; you can also use the 'new-style' formatting: `'{} {}'.format(var1, var2)`. The new-style formatting lets us index our entries, which can be convenient." 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": null, 150 | "metadata": {}, 151 | "outputs": [], 152 | "source": [ 153 | "print('This is %d example of a formatted %s' % (1, 'string'))\n", 154 | "print ('This is the {}nd example of a formatted {}.'.format(2, 'string'))\n", 155 | "\n", 156 | "# indexing with new-style\n", 157 | "print('This is the {A} thing, here is the {B}, the {C}, and now the {A} again'.format(A='first', C='third', B='second'))" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "Recent versions of Python (from 3.6 onwards) have introduced a cleaner formatting method named f-strings (format-string,`f\"\"` or `f''`). These are an extremely convenient for string formatting:" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": null, 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [ 173 | "x = 1\n", 174 | "print(f\"'x' is of type {type(x)} and contains the value {x}\")" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "Thus, by prepending our string with the letter `f`, we can use variables and valid Python expressions within `{}`. We strongly recommend the use of f-strings over other types of formatting since it greatly improves readability. In the rest of the course we will generally use this new formatting style." 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "metadata": { 187 | "collapsed": true 188 | }, 189 | "source": [ 190 | "### Exercise 1\n", 191 | "\n", 192 | "See what happens in the above example if you remove the colon or don't use indentation." 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": null, 198 | "metadata": { 199 | "solution2": "hidden", 200 | "solution2_first": true 201 | }, 202 | "outputs": [], 203 | "source": [ 204 | "# Exercise 1" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": null, 210 | "metadata": { 211 | "solution2": "hidden" 212 | }, 213 | "outputs": [], 214 | "source": [ 215 | "# NBVAL_RAISES_EXCEPTION\n", 216 | "## Note: ignore the above comment, this exists to allow us to test the notebook\n", 217 | "\n", 218 | "# Exercise 1\n", 219 | "while number <= 10\n", 220 | "print(\"number is equal to %d now.\" % number)\n", 221 | "number += 1\n", 222 | "print(\"End of loop\")" 223 | ] 224 | }, 225 | { 226 | "cell_type": "markdown", 227 | "metadata": { 228 | "collapsed": true 229 | }, 230 | "source": [ 231 | "**⚠️ Be careful about infinite loops!** A problem with `while` loops can occur when the condition under which they run is always `True`, making them run forever. Without the line `number += 1` in the above example, the loop would run forever and always print \"number is equal to 1 now.\" to the screen. Therefore, it is important to make sure that `while` loops terminate at some stage. The [`break`](https://docs.python.org/3/tutorial/controlflow.html?highlight=break#break-and-continue-statements-and-else-clauses-on-loops) statement exists to ensure this." 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "metadata": { 237 | "collapsed": true 238 | }, 239 | "source": [ 240 | "`While` statments are particularly useful when you are interating over a task an unknown number of times. An example of this would be if you are iteratively solving an equation for a given input until you reach a convergence limit.\n", 241 | "\n", 242 | "However, if you already know how many times you want to execute a set of tasks, `for` loops are better suited." 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": { 248 | "collapsed": true 249 | }, 250 | "source": [ 251 | "## 3.2 For loops" 252 | ] 253 | }, 254 | { 255 | "cell_type": "markdown", 256 | "metadata": {}, 257 | "source": [ 258 | "For loops take an *iterator statment* and loops over it until the iteration is exhausted.\n", 259 | "\n", 260 | "To demonstrate this, you can equally write the above example as a `for` loop:" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": null, 266 | "metadata": {}, 267 | "outputs": [], 268 | "source": [ 269 | "for number in range (1,11):\n", 270 | " print(f\"number is equal to {number} now.\")" 271 | ] 272 | }, 273 | { 274 | "cell_type": "markdown", 275 | "metadata": {}, 276 | "source": [ 277 | "Here `number in range (1,11)` is the *iterator statement* which generates values of `number` which progressively increase from 1 to 10 as the for loop is executed.\n", 278 | "\n", 279 | "**Note:** the range goes to 11, but the loop stops at 10. This is something where Python is not very intuitive. If you define ranges, Python will not include the last value of your range. Since number is an integer here, the last integer before 11 is 10." 280 | ] 281 | }, 282 | { 283 | "cell_type": "markdown", 284 | "metadata": {}, 285 | "source": [ 286 | "### Exercise 2\n", 287 | "\n", 288 | "Use either a `while` loop or a `for` loop to print out the first 10 entries in the 6 times table, i.e.:\n", 289 | "- 1x6=6\n", 290 | "- 2x6=12\n", 291 | "- ...\n", 292 | "- 10x6=60" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": null, 298 | "metadata": { 299 | "solution2": "hidden", 300 | "solution2_first": true 301 | }, 302 | "outputs": [], 303 | "source": [ 304 | "# Exercise 2" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": null, 310 | "metadata": { 311 | "solution2": "hidden" 312 | }, 313 | "outputs": [], 314 | "source": [ 315 | "# Exercise 2\n", 316 | "\n", 317 | "# Using a while loop\n", 318 | "number = 1\n", 319 | "while number <= 10:\n", 320 | " print(6*number)\n", 321 | " number += 1\n", 322 | "\n", 323 | "# Using a for loop\n", 324 | "number = 1\n", 325 | "for number in range(1,11):\n", 326 | " print(6*number)" 327 | ] 328 | }, 329 | { 330 | "cell_type": "markdown", 331 | "metadata": {}, 332 | "source": [ 333 | "You can also use loops within loops - as many as you like. Just keep good track of the indentations!" 334 | ] 335 | }, 336 | { 337 | "cell_type": "markdown", 338 | "metadata": {}, 339 | "source": [ 340 | "### Exercise 3\n", 341 | "\n", 342 | "Use nested loops to calculate the matrix product of the two matrices:\n", 343 | "\n", 344 | " \\begin{bmatrix}\n", 345 | " 1 & 2 \\\\\n", 346 | " 3 & 4\n", 347 | " \\end{bmatrix}\n", 348 | " \n", 349 | " and\n", 350 | " \n", 351 | " \\begin{bmatrix}\n", 352 | " -3 & 1 \\\\\n", 353 | " 4 & -2\n", 354 | " \\end{bmatrix}\n", 355 | "\n", 356 | "**Note:** You can define each matrix as a numpy array (see section 10). You might want to work through section 10 first and then come back and try this exercise." 357 | ] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": null, 362 | "metadata": { 363 | "solution2": "hidden", 364 | "solution2_first": true 365 | }, 366 | "outputs": [], 367 | "source": [ 368 | "# Exercise 3" 369 | ] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "execution_count": null, 374 | "metadata": { 375 | "solution2": "hidden" 376 | }, 377 | "outputs": [], 378 | "source": [ 379 | "# Exercise 3\n", 380 | "import numpy as np\n", 381 | "\n", 382 | "# set the two matricies:\n", 383 | "A = np.array([[1,2], [3,4]])\n", 384 | "print(A)\n", 385 | "B = np.array([[-3,1],[4,-2]])\n", 386 | "print(B)\n", 387 | "# make an array of the same size (i.e. 2 x 2), currently filled with zeroes, to store the product:\n", 388 | "product = np.zeros((2,2))\n", 389 | "\n", 390 | "# loop through the row numbers of the product:\n", 391 | "for i in range(2): \n", 392 | " # loop through the column numbers of the product:\n", 393 | " for j in range(2):\n", 394 | " # we get the (i, j) position in the product from the ith row of A and the jth row of B:\n", 395 | " product[i, j] = sum(A[i,:] * B[:,j])\n", 396 | " \n", 397 | "print('The product is:\\n', product)\n", 398 | "\n", 399 | "# There are other (easier) ways of going about this, e.g. the numpy library provides a function for this:\n", 400 | "# But this exercise demonstrates how we can use nested loops!\n", 401 | "np.dot(A, B)" 402 | ] 403 | }, 404 | { 405 | "cell_type": "markdown", 406 | "metadata": {}, 407 | "source": [ 408 | "## Range" 409 | ] 410 | }, 411 | { 412 | "cell_type": "markdown", 413 | "metadata": {}, 414 | "source": [ 415 | "As shown above, if you want to loop over a range of numbers, you can loop over `range`. `range` can be called with the arguments `(start, stop)` in the manner `range(start,stop)`. Here values will be assigned starting with the value of `start` and ending with the value of `stop - 1`. If `start` is omitted, `start=0` is assumed:" 416 | ] 417 | }, 418 | { 419 | "cell_type": "code", 420 | "execution_count": null, 421 | "metadata": {}, 422 | "outputs": [], 423 | "source": [ 424 | "for i in range(5):\n", 425 | " print(i)" 426 | ] 427 | }, 428 | { 429 | "cell_type": "code", 430 | "execution_count": null, 431 | "metadata": {}, 432 | "outputs": [], 433 | "source": [ 434 | "for i in range(-5, 5):\n", 435 | " print(i)" 436 | ] 437 | }, 438 | { 439 | "cell_type": "markdown", 440 | "metadata": {}, 441 | "source": [ 442 | "The `range` function accepts a third argument that allows to modify the step size (default `1`). The full signature of `range` is therefore `range(start, stop, step)`." 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": null, 448 | "metadata": {}, 449 | "outputs": [], 450 | "source": [ 451 | "for i in range(-5, 5, 2):\n", 452 | " print(i)" 453 | ] 454 | }, 455 | { 456 | "cell_type": "markdown", 457 | "metadata": {}, 458 | "source": [ 459 | "## Review:\n", 460 | "\n", 461 | "In this section, we have covered:\n", 462 | "- Using loops to do repetitive tasks.\n", 463 | "- The `while` loop construct.\n", 464 | "- The `for` loop construct.\n", 465 | "- Using `range` to generate numbers over a given range.\n", 466 | "- Formatting strings." 467 | ] 468 | } 469 | ], 470 | "metadata": { 471 | "kernelspec": { 472 | "display_name": "Python 3 (ipykernel)", 473 | "language": "python", 474 | "name": "python3" 475 | }, 476 | "language_info": { 477 | "codemirror_mode": { 478 | "name": "ipython", 479 | "version": 3 480 | }, 481 | "file_extension": ".py", 482 | "mimetype": "text/x-python", 483 | "name": "python", 484 | "nbconvert_exporter": "python", 485 | "pygments_lexer": "ipython3", 486 | "version": "3.9.15" 487 | } 488 | }, 489 | "nbformat": 4, 490 | "nbformat_minor": 1 491 | } 492 | -------------------------------------------------------------------------------- /tutorials/Python/03_Loops/no_solutions/Loops.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 3 Loops" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Sometimes when programming you might need to do repetitive tasks. For example, doing a given operation with an increasingly larger number.\n", 15 | "\n", 16 | "Python offers loops as a way to make your life easier, and your code much smaller." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "## 3.1 While loops" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "Assume `number = 1` and you would like to add 1 until you reach 10. At each increment of `number` you want to print the value of the variable `number` to the screen. You could do it explicitly in the following manner:" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "number = 1\n", 40 | "print(\"number is equal to %d now.\" % number)\n", 41 | "number +=1\n", 42 | "print(\"number is equal to %d now.\" % number)\n", 43 | "number +=1\n", 44 | "print(\"number is equal to %d now.\" % number)\n", 45 | "number +=1\n", 46 | "print(\"number is equal to %d now.\" % number)\n", 47 | "number +=1\n", 48 | "print(\"number is equal to %d now.\" % number)\n", 49 | "number +=1\n", 50 | "print(\"number is equal to %d now.\" % number)\n", 51 | "number +=1\n", 52 | "print(\"number is equal to %d now.\" % number)\n", 53 | "number +=1\n", 54 | "print(\"number is equal to %d now.\" % number)\n", 55 | "number +=1\n", 56 | "print(\"number is equal to %d now.\" % number)\n", 57 | "number +=1\n", 58 | "print(\"number is equal to %d now.\" % number)\n", 59 | "number +=1" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "This will easily become tedious as the number of increments increases. Instead you can save yourself some time and just program this as a loop:" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | "number = 1\n", 76 | "while number <= 10:\n", 77 | " print(\"number is equal to %d now.\" % number)\n", 78 | " number += 1\n", 79 | " \n", 80 | "print(\"End of loop\")" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "The `while` loop is a useful construct which allows you to repeat a task while a certain statement is `True`.\n", 88 | "\n", 89 | "Here, this statement is `number <= 10`.\n", 90 | "\n", 91 | "The colon (`:`) after the statement marks the beginning of the task(s) that are to be performed in the loop.\n", 92 | "\n", 93 | "Once the statement becomes `False`, the loop will stop being executed and the code will move on to its next section (in this case we print \"End of loop\").\n", 94 | "\n", 95 | "\n", 96 | "#### Indents are important!\n", 97 | "\n", 98 | "As you can see from the code above, the task(s) to be done in the loop are indented. Indentation is how Python determines if a block of code belongs to a given method statement.\n", 99 | "\n", 100 | "For example, in the above code the presence of an indentation for:\n", 101 | "\n", 102 | "```python\n", 103 | "print(\"number is equal to %d now.\" % number)\n", 104 | "number += 1\n", 105 | "```\n", 106 | "\n", 107 | "Means that they belong to the `while` statement.\n", 108 | "\n", 109 | "However, since the:\n", 110 | "\n", 111 | "```python\n", 112 | "print(\"End of loop\")\n", 113 | "```\n", 114 | "does not have indentation, it does not get executed as part of the `while` statement.\n", 115 | "\n", 116 | "If the code was instead changed to indent the second `print` statement, you would see that it would be executed at every iteration of the `while` loop.\n" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": null, 122 | "metadata": {}, 123 | "outputs": [], 124 | "source": [ 125 | "number = 1\n", 126 | "while number <= 10:\n", 127 | " print(\"number is equal to %d now.\" % number)\n", 128 | " number += 1\n", 129 | " \n", 130 | " print(\"End of loop\")" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "### Aside" 138 | ] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "metadata": {}, 143 | "source": [ 144 | "`'%d' % number` is an example of string formatting, in this case we are 'inserting' the value of variables into a string (use `%d` for a number and `%s` for a string). If you want to insert multiple variables, you can use e.g.: `'%d %s' % (num, str)`. This is 'old-style' formatting; you can also use the 'new-style' formatting: `'{} {}'.format(var1, var2)`. The new-style formatting lets us index our entries, which can be convenient." 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": null, 150 | "metadata": {}, 151 | "outputs": [], 152 | "source": [ 153 | "print('This is %d example of a formatted %s' % (1, 'string'))\n", 154 | "print ('This is the {}nd example of a formatted {}.'.format(2, 'string'))\n", 155 | "\n", 156 | "# indexing with new-style\n", 157 | "print('This is the {A} thing, here is the {B}, the {C}, and now the {A} again'.format(A='first', C='third', B='second'))" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "Recent versions of Python (from 3.6 onwards) have introduced a cleaner formatting method named f-strings (format-string,`f\"\"` or `f''`). These are an extremely convenient for string formatting:" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": null, 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [ 173 | "x = 1\n", 174 | "print(f\"'x' is of type {type(x)} and contains the value {x}\")" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "Thus, by prepending our string with the letter `f`, we can use variables and valid Python expressions within `{}`. We strongly recommend the use of f-strings over other types of formatting since it greatly improves readability. In the rest of the course we will generally use this new formatting style." 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "metadata": { 187 | "collapsed": true 188 | }, 189 | "source": [ 190 | "### Exercise 1\n", 191 | "\n", 192 | "See what happens in the above example if you remove the colon or don't use indentation." 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": null, 198 | "metadata": {}, 199 | "outputs": [], 200 | "source": [ 201 | "# Exercise 1" 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": { 207 | "collapsed": true 208 | }, 209 | "source": [ 210 | "**⚠️ Be careful about infinite loops!** A problem with `while` loops can occur when the condition under which they run is always `True`, making them run forever. Without the line `number += 1` in the above example, the loop would run forever and always print \"number is equal to 1 now.\" to the screen. Therefore, it is important to make sure that `while` loops terminate at some stage. The [`break`](https://docs.python.org/3/tutorial/controlflow.html?highlight=break#break-and-continue-statements-and-else-clauses-on-loops) statement exists to ensure this." 211 | ] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "metadata": { 216 | "collapsed": true 217 | }, 218 | "source": [ 219 | "`While` statments are particularly useful when you are interating over a task an unknown number of times. An example of this would be if you are iteratively solving an equation for a given input until you reach a convergence limit.\n", 220 | "\n", 221 | "However, if you already know how many times you want to execute a set of tasks, `for` loops are better suited." 222 | ] 223 | }, 224 | { 225 | "cell_type": "markdown", 226 | "metadata": { 227 | "collapsed": true 228 | }, 229 | "source": [ 230 | "## 3.2 For loops" 231 | ] 232 | }, 233 | { 234 | "cell_type": "markdown", 235 | "metadata": {}, 236 | "source": [ 237 | "For loops take an *iterator statment* and loops over it until the iteration is exhausted.\n", 238 | "\n", 239 | "To demonstrate this, you can equally write the above example as a `for` loop:" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": null, 245 | "metadata": {}, 246 | "outputs": [], 247 | "source": [ 248 | "for number in range (1,11):\n", 249 | " print(f\"number is equal to {number} now.\")" 250 | ] 251 | }, 252 | { 253 | "cell_type": "markdown", 254 | "metadata": {}, 255 | "source": [ 256 | "Here `number in range (1,11)` is the *iterator statement* which generates values of `number` which progressively increase from 1 to 10 as the for loop is executed.\n", 257 | "\n", 258 | "**Note:** the range goes to 11, but the loop stops at 10. This is something where Python is not very intuitive. If you define ranges, Python will not include the last value of your range. Since number is an integer here, the last integer before 11 is 10." 259 | ] 260 | }, 261 | { 262 | "cell_type": "markdown", 263 | "metadata": {}, 264 | "source": [ 265 | "### Exercise 2\n", 266 | "\n", 267 | "Use either a `while` loop or a `for` loop to print out the first 10 entries in the 6 times table, i.e.:\n", 268 | "- 1x6=6\n", 269 | "- 2x6=12\n", 270 | "- ...\n", 271 | "- 10x6=60" 272 | ] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "execution_count": null, 277 | "metadata": {}, 278 | "outputs": [], 279 | "source": [ 280 | "# Exercise 2" 281 | ] 282 | }, 283 | { 284 | "cell_type": "markdown", 285 | "metadata": {}, 286 | "source": [ 287 | "You can also use loops within loops - as many as you like. Just keep good track of the indentations!" 288 | ] 289 | }, 290 | { 291 | "cell_type": "markdown", 292 | "metadata": {}, 293 | "source": [ 294 | "### Exercise 3\n", 295 | "\n", 296 | "Use nested loops to calculate the matrix product of the two matrices:\n", 297 | "\n", 298 | " \\begin{bmatrix}\n", 299 | " 1 & 2 \\\\\n", 300 | " 3 & 4\n", 301 | " \\end{bmatrix}\n", 302 | " \n", 303 | " and\n", 304 | " \n", 305 | " \\begin{bmatrix}\n", 306 | " -3 & 1 \\\\\n", 307 | " 4 & -2\n", 308 | " \\end{bmatrix}\n", 309 | "\n", 310 | "**Note:** You can define each matrix as a numpy array (see section 10). You might want to work through section 10 first and then come back and try this exercise." 311 | ] 312 | }, 313 | { 314 | "cell_type": "code", 315 | "execution_count": null, 316 | "metadata": {}, 317 | "outputs": [], 318 | "source": [ 319 | "# Exercise 3" 320 | ] 321 | }, 322 | { 323 | "cell_type": "markdown", 324 | "metadata": {}, 325 | "source": [ 326 | "## Range" 327 | ] 328 | }, 329 | { 330 | "cell_type": "markdown", 331 | "metadata": {}, 332 | "source": [ 333 | "As shown above, if you want to loop over a range of numbers, you can loop over `range`. `range` can be called with the arguments `(start, stop)` in the manner `range(start,stop)`. Here values will be assigned starting with the value of `start` and ending with the value of `stop - 1`. If `start` is omitted, `start=0` is assumed:" 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": null, 339 | "metadata": {}, 340 | "outputs": [], 341 | "source": [ 342 | "for i in range(5):\n", 343 | " print(i)" 344 | ] 345 | }, 346 | { 347 | "cell_type": "code", 348 | "execution_count": null, 349 | "metadata": {}, 350 | "outputs": [], 351 | "source": [ 352 | "for i in range(-5, 5):\n", 353 | " print(i)" 354 | ] 355 | }, 356 | { 357 | "cell_type": "markdown", 358 | "metadata": {}, 359 | "source": [ 360 | "The `range` function accepts a third argument that allows to modify the step size (default `1`). The full signature of `range` is therefore `range(start, stop, step)`." 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": null, 366 | "metadata": {}, 367 | "outputs": [], 368 | "source": [ 369 | "for i in range(-5, 5, 2):\n", 370 | " print(i)" 371 | ] 372 | }, 373 | { 374 | "cell_type": "markdown", 375 | "metadata": {}, 376 | "source": [ 377 | "## Review:\n", 378 | "\n", 379 | "In this section, we have covered:\n", 380 | "- Using loops to do repetitive tasks.\n", 381 | "- The `while` loop construct.\n", 382 | "- The `for` loop construct.\n", 383 | "- Using `range` to generate numbers over a given range.\n", 384 | "- Formatting strings." 385 | ] 386 | } 387 | ], 388 | "metadata": { 389 | "kernelspec": { 390 | "display_name": "Python 3 (ipykernel)", 391 | "language": "python", 392 | "name": "python3" 393 | }, 394 | "language_info": { 395 | "codemirror_mode": { 396 | "name": "ipython", 397 | "version": 3 398 | }, 399 | "file_extension": ".py", 400 | "mimetype": "text/x-python", 401 | "name": "python", 402 | "nbconvert_exporter": "python", 403 | "pygments_lexer": "ipython3", 404 | "version": "3.9.15" 405 | } 406 | }, 407 | "nbformat": 4, 408 | "nbformat_minor": 1 409 | } 410 | -------------------------------------------------------------------------------- /tutorials/Python/04_Conditionals/Conditionals.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 4 Decisions: if.. elif.. else" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": { 13 | "collapsed": true 14 | }, 15 | "source": [ 16 | "Decision making plays a key role in programming and Python provides several ways to do that." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "## 4.1 The If statement" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "The `if` statement is used to check if a condition is fulfilled and then a task (or set of tasks) is executed. The following example shows how it is used to check if a number is even:" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "number = 2\n", 40 | "if number % 2 == 0:\n", 41 | " print(\"number is even.\")" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "The operator `%` is called modulus and gives the remainder of an integer division (see section 1). The *conditional expression* follows directly after the word `if` and ends with a colon `:` (just as in the syntax for loops, see section 3). The task(s) to be executed in case the condition is `True`, need to be indented (again as in the syntax for loops). Note that if the condition is not `True`, the task is simply not executed." 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "number = 3\n", 58 | "if number % 2 == 0:\n", 59 | " print(\"number is even.\")" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "## 4.2 The if.. else statement" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "An `else` statement can be combined with an `if` statement. An else statement contains the block of code that is executed if the conditional expression in the `if` statement resolves as `False`." 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": null, 79 | "metadata": {}, 80 | "outputs": [], 81 | "source": [ 82 | "number = 3\n", 83 | "if number % 2 == 0:\n", 84 | " print(\"number is even.\")\n", 85 | "else:\n", 86 | " print(\"number is odd.\")" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "Just as loops can be nested within loops, `if` statements can be nested within `if` statements." 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "Now that you know the concept of loops and decision making, by combining them you can do a range of different tasks!" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "### Exercise 1\n", 108 | "\n", 109 | "While `n < 10`, print whether each number is odd or even." 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": null, 115 | "metadata": { 116 | "solution2": "hidden", 117 | "solution2_first": true 118 | }, 119 | "outputs": [], 120 | "source": [ 121 | "# Exercise 1" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "metadata": { 128 | "solution2": "hidden" 129 | }, 130 | "outputs": [], 131 | "source": [ 132 | "# Exercise 1\n", 133 | "for n in range(1,11):\n", 134 | " if n % 2 == 0:\n", 135 | " print('%d is even' % n)\n", 136 | " else:\n", 137 | " print('%d is odd' % n)" 138 | ] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "metadata": {}, 143 | "source": [ 144 | "### Exercise 2\n", 145 | "\n", 146 | "While `n < 10`, find the number of values that are even." 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": null, 152 | "metadata": { 153 | "solution2": "hidden", 154 | "solution2_first": true 155 | }, 156 | "outputs": [], 157 | "source": [ 158 | "# Exercise 2" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": null, 164 | "metadata": { 165 | "solution2": "hidden" 166 | }, 167 | "outputs": [], 168 | "source": [ 169 | "# Exercise 2\n", 170 | "counter = 0\n", 171 | "for n in range(1,11):\n", 172 | " if n % 2 == 0:\n", 173 | " counter += 1\n", 174 | "print(f\"Number of even values: {counter}\")" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "If there is more than one condition you want to check, you can add `elif` statements to check each condition. You can have as many `elif`s between an `if` and `else` as you want." 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": null, 187 | "metadata": {}, 188 | "outputs": [], 189 | "source": [ 190 | "for n in range(-2, 2):\n", 191 | " if n < 0:\n", 192 | " print(f'{n} is negative')\n", 193 | " elif n > 0:\n", 194 | " print(f'{n} is positive')\n", 195 | " else:\n", 196 | " print(f'{n} is zero')" 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": {}, 202 | "source": [ 203 | "It is worth noting that in an `if`.. `elif`.. `else` block, the first statement that returns as `True` is executed." 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "## Review:\n", 211 | "\n", 212 | "In this section, we have covered:\n", 213 | "- How to do decision making\n", 214 | "- The `if` statement.\n", 215 | "- The `if .. else` statment.\n", 216 | "- The `if .. elif .. else` statement." 217 | ] 218 | } 219 | ], 220 | "metadata": { 221 | "kernelspec": { 222 | "display_name": "Python 3 (ipykernel)", 223 | "language": "python", 224 | "name": "python3" 225 | }, 226 | "language_info": { 227 | "codemirror_mode": { 228 | "name": "ipython", 229 | "version": 3 230 | }, 231 | "file_extension": ".py", 232 | "mimetype": "text/x-python", 233 | "name": "python", 234 | "nbconvert_exporter": "python", 235 | "pygments_lexer": "ipython3", 236 | "version": "3.9.15" 237 | } 238 | }, 239 | "nbformat": 4, 240 | "nbformat_minor": 1 241 | } 242 | -------------------------------------------------------------------------------- /tutorials/Python/04_Conditionals/no_solutions/Conditionals.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 4 Decisions: if.. elif.. else" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": { 13 | "collapsed": true 14 | }, 15 | "source": [ 16 | "Decision making plays a key role in programming and Python provides several ways to do that." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "## 4.1 The If statement" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "The `if` statement is used to check if a condition is fulfilled and then a task (or set of tasks) is executed. The following example shows how it is used to check if a number is even:" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "number = 2\n", 40 | "if number % 2 == 0:\n", 41 | " print(\"number is even.\")" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "The operator `%` is called modulus and gives the remainder of an integer division (see section 1). The *conditional expression* follows directly after the word `if` and ends with a colon `:` (just as in the syntax for loops, see section 3). The task(s) to be executed in case the condition is `True`, need to be indented (again as in the syntax for loops). Note that if the condition is not `True`, the task is simply not executed." 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "number = 3\n", 58 | "if number % 2 == 0:\n", 59 | " print(\"number is even.\")" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "## 4.2 The if.. else statement" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "An `else` statement can be combined with an `if` statement. An else statement contains the block of code that is executed if the conditional expression in the `if` statement resolves as `False`." 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": null, 79 | "metadata": {}, 80 | "outputs": [], 81 | "source": [ 82 | "number = 3\n", 83 | "if number % 2 == 0:\n", 84 | " print(\"number is even.\")\n", 85 | "else:\n", 86 | " print(\"number is odd.\")" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "Just as loops can be nested within loops, `if` statements can be nested within `if` statements." 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "Now that you know the concept of loops and decision making, by combining them you can do a range of different tasks!" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "### Exercise 1\n", 108 | "\n", 109 | "While `n < 10`, print whether each number is odd or even." 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": null, 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [ 118 | "# Exercise 1" 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "metadata": {}, 124 | "source": [ 125 | "### Exercise 2\n", 126 | "\n", 127 | "While `n < 10`, find the number of values that are even." 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": null, 133 | "metadata": {}, 134 | "outputs": [], 135 | "source": [ 136 | "# Exercise 2" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "If there is more than one condition you want to check, you can add `elif` statements to check each condition. You can have as many `elif`s between an `if` and `else` as you want." 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": null, 149 | "metadata": {}, 150 | "outputs": [], 151 | "source": [ 152 | "for n in range(-2, 2):\n", 153 | " if n < 0:\n", 154 | " print(f'{n} is negative')\n", 155 | " elif n > 0:\n", 156 | " print(f'{n} is positive')\n", 157 | " else:\n", 158 | " print(f'{n} is zero')" 159 | ] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "metadata": {}, 164 | "source": [ 165 | "It is worth noting that in an `if`.. `elif`.. `else` block, the first statement that returns as `True` is executed." 166 | ] 167 | }, 168 | { 169 | "cell_type": "markdown", 170 | "metadata": {}, 171 | "source": [ 172 | "## Review:\n", 173 | "\n", 174 | "In this section, we have covered:\n", 175 | "- How to do decision making\n", 176 | "- The `if` statement.\n", 177 | "- The `if .. else` statment.\n", 178 | "- The `if .. elif .. else` statement." 179 | ] 180 | } 181 | ], 182 | "metadata": { 183 | "kernelspec": { 184 | "display_name": "Python 3 (ipykernel)", 185 | "language": "python", 186 | "name": "python3" 187 | }, 188 | "language_info": { 189 | "codemirror_mode": { 190 | "name": "ipython", 191 | "version": 3 192 | }, 193 | "file_extension": ".py", 194 | "mimetype": "text/x-python", 195 | "name": "python", 196 | "nbconvert_exporter": "python", 197 | "pygments_lexer": "ipython3", 198 | "version": "3.9.15" 199 | } 200 | }, 201 | "nbformat": 4, 202 | "nbformat_minor": 1 203 | } 204 | -------------------------------------------------------------------------------- /tutorials/Python/06_FileHandling/datafiles/file1.txt: -------------------------------------------------------------------------------- 1 | This is a file containing a list of peptide sequences: 2 | ASDIPREDSDFLLLKK 3 | HFVNNNWERT 4 | PLSDMASI 5 | KWHGLLGSF 6 | QERTKMSFFWER 7 | -------------------------------------------------------------------------------- /tutorials/Python/06_FileHandling/datafiles/file2.txt: -------------------------------------------------------------------------------- 1 | In this new file 2 | this is the second line 3 | and this is the third line 4 | this line will be appended 5 | -------------------------------------------------------------------------------- /tutorials/Python/06_FileHandling/datafiles/file3.txt: -------------------------------------------------------------------------------- 1 | test -------------------------------------------------------------------------------- /tutorials/Python/06_FileHandling/no_solutions/FileHandling.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 6. Reading and writing files" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "One important aspect of using Python is being able to read and write files. To do this you use the `open` command:\n", 15 | "\n", 16 | "```\n", 17 | "open(filename-in-quotes, mode)\n", 18 | "```\n", 19 | "\n", 20 | "In place of `mode` you can specify:\n", 21 | "- `'r'`, to open a file you want to read\n", 22 | "- `'w'`, to open a file that you want to write (starting a new file).\n", 23 | "- `'a'`, to open a file that you want to append to (adding to an existing file).\n", 24 | "\n", 25 | "So, to open a file with filename `file1.txt` in \"read\" mode:\n", 26 | "\n", 27 | "```\n", 28 | "open('file1.txt', 'r')\n", 29 | "```\n", 30 | "\n", 31 | "To open a file, `file2.txt`, in \"write\" mode:\n", 32 | "\n", 33 | "```\n", 34 | "open('file2.txt', 'w')\n", 35 | "```\n", 36 | "\n", 37 | "**CAREFUL: this will create a new file called `file2.txt`. If you had an existing file named `file2.txt` it will be overwritten.**\n", 38 | "\n", 39 | "To open a file, `file3.txt`, in 'append' mode:\n", 40 | "\n", 41 | "```\n", 42 | "open('file3.txt', 'a')\n", 43 | "```\n", 44 | "\n", 45 | "If you don't specify the mode, the default is `'r'`.\n", 46 | "\n", 47 | "## 6.1 Reading files\n", 48 | "\n", 49 | "When we open a file in 'read' mode, we will also want to be able to access what has been read.\n", 50 | "\n", 51 | "To start with, we will create an opened file object and set it as a new variable, `f`. (You could change `f` to whatever you want!):" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": null, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "f = open('datafiles/file1.txt', 'r')" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "The file object, `f`, is an *iterator*, that is we can iterate through the file object (using a `for` loop) to obtain each new line in the file. We can then perform some operation on each line of the file.\n", 68 | "\n", 69 | "For example, if we just want to print each line in the file:" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": null, 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [ 78 | "for line in f:\n", 79 | " print(line)" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "Open the file, `file1.txt`, in a text editor and check that the file indeed contains the lines we printed above.\n", 87 | "\n", 88 | "Once we have iterated once through the file object, *f*, it is empty, so if we do the same thing again, nothing will happen:" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [ 97 | "for line in f:\n", 98 | " print(line)" 99 | ] 100 | }, 101 | { 102 | "cell_type": "markdown", 103 | "metadata": {}, 104 | "source": [ 105 | "If we want to read the lines again, we need to close the file object, using `f.close()`, and reopen the file:\n" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": null, 111 | "metadata": {}, 112 | "outputs": [], 113 | "source": [ 114 | "f.close()\n", 115 | "f = open(\"datafiles/file1.txt\", \"r\")" 116 | ] 117 | }, 118 | { 119 | "cell_type": "markdown", 120 | "metadata": {}, 121 | "source": [ 122 | "Now we can read the lines in the file again:" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": null, 128 | "metadata": {}, 129 | "outputs": [], 130 | "source": [ 131 | "for line in f:\n", 132 | " print(line)" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "Let's close the file object again, so we don't have it hanging around!" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": null, 145 | "metadata": {}, 146 | "outputs": [], 147 | "source": [ 148 | "f.close()" 149 | ] 150 | }, 151 | { 152 | "cell_type": "markdown", 153 | "metadata": {}, 154 | "source": [ 155 | "If you only wanted to read through the contents of the file once, this would be fine, but it is quite a nuisance if you want to refer back to the lines in the file later on.\n", 156 | "\n", 157 | "Instead, we can convert the file object to a list, which then gives us a list of the lines in the file, that we can go back to:" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": null, 163 | "metadata": {}, 164 | "outputs": [], 165 | "source": [ 166 | "# First open the file object\n", 167 | "file_object = open(\"datafiles/file1.txt\", \"r\")\n", 168 | "# Then we convert it to a list\n", 169 | "file_list = list(file_object)\n", 170 | "# Let's print the contents of the list\n", 171 | "print(file_list)\n", 172 | "# Finally we have to make sure that the file object is closed\n", 173 | "file_object.close()" 174 | ] 175 | }, 176 | { 177 | "cell_type": "markdown", 178 | "metadata": {}, 179 | "source": [ 180 | "Note that:\n", 181 | "1. This is a list of string objects, and each string ends in a new line character.\n", 182 | "2. We can still use file_list to print out the lines of the file in a 'for' loop." 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": null, 188 | "metadata": {}, 189 | "outputs": [], 190 | "source": [ 191 | "for line in file_list:\n", 192 | " print(line)" 193 | ] 194 | }, 195 | { 196 | "cell_type": "markdown", 197 | "metadata": {}, 198 | "source": [ 199 | "You can also use the `readlines()` *method* function:" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": null, 205 | "metadata": {}, 206 | "outputs": [], 207 | "source": [ 208 | "# First open the file object\n", 209 | "file_object = open(\"datafiles/file1.txt\",\"r\")\n", 210 | "# Then we use the readlines method to get all the lines\n", 211 | "file_list2 = file_object.readlines()\n", 212 | "# We close the file object for safety\n", 213 | "file_object.close()\n", 214 | "# We print the extracted list\n", 215 | "print(file_list2)" 216 | ] 217 | }, 218 | { 219 | "cell_type": "markdown", 220 | "metadata": {}, 221 | "source": [ 222 | "Let's put that into practise with an exercise:" 223 | ] 224 | }, 225 | { 226 | "cell_type": "markdown", 227 | "metadata": {}, 228 | "source": [ 229 | "### Exercise 1\n", 230 | "\n", 231 | "\n", 232 | "a) Print out just the first line in the file" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": null, 238 | "metadata": {}, 239 | "outputs": [], 240 | "source": [ 241 | "# Exercise 1 a)" 242 | ] 243 | }, 244 | { 245 | "cell_type": "markdown", 246 | "metadata": {}, 247 | "source": [ 248 | "b) Print out all except the first line in the file" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": null, 254 | "metadata": {}, 255 | "outputs": [], 256 | "source": [ 257 | "# Exercise 1 b)" 258 | ] 259 | }, 260 | { 261 | "cell_type": "markdown", 262 | "metadata": {}, 263 | "source": [ 264 | "c) Change the third peptide sequence from 'PLSDMASI' to 'PLSEMASI'" 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": null, 270 | "metadata": {}, 271 | "outputs": [], 272 | "source": [ 273 | "# Exercise 1 c)" 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": {}, 279 | "source": [ 280 | "d) Add a new peptide sequence (YYVHNKSERFT) to the end of `file_list`" 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": null, 286 | "metadata": {}, 287 | "outputs": [], 288 | "source": [ 289 | "# Exercise 1 d)" 290 | ] 291 | }, 292 | { 293 | "cell_type": "markdown", 294 | "metadata": {}, 295 | "source": [ 296 | "e) Now print all the peptide sequences again (without the first line)" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": null, 302 | "metadata": {}, 303 | "outputs": [], 304 | "source": [ 305 | "# Exercise 1 e)" 306 | ] 307 | }, 308 | { 309 | "cell_type": "markdown", 310 | "metadata": {}, 311 | "source": [ 312 | "## 6.2 Writing files\n", 313 | "\n", 314 | "To write the updated list of peptides to a new file we would need to open a new file, using \"write\" mode, and then use the `write` *method* fuction.\n", 315 | "\n", 316 | "The following example opens a new file, `file2.txt` (as mentioned above, we need to be careful that this file doesn't already exist, otherwise it will be overwritten), then writes three lines to the new file. Notice that the argument to the `write` function is a single string. We make use of the new line character `\\n` as part of the string to make separate lines." 317 | ] 318 | }, 319 | { 320 | "cell_type": "code", 321 | "execution_count": null, 322 | "metadata": {}, 323 | "outputs": [], 324 | "source": [ 325 | "file_object_2 = open('file2.txt', 'w')\n", 326 | "file_object_2.write('In this new file\\nthis is the second line\\n')" 327 | ] 328 | }, 329 | { 330 | "cell_type": "markdown", 331 | "metadata": {}, 332 | "source": [ 333 | "Note: the write function returns the number of characters written to the file." 334 | ] 335 | }, 336 | { 337 | "cell_type": "markdown", 338 | "metadata": {}, 339 | "source": [ 340 | "If I want to write more to the file I can do so by using the `write` function again:" 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": null, 346 | "metadata": {}, 347 | "outputs": [], 348 | "source": [ 349 | "file_object_2.write('and this is the third line\\n')" 350 | ] 351 | }, 352 | { 353 | "cell_type": "markdown", 354 | "metadata": {}, 355 | "source": [ 356 | "Once finished writing to the file, we should close the file, using `close` as before." 357 | ] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": null, 362 | "metadata": {}, 363 | "outputs": [], 364 | "source": [ 365 | "file_object_2.close()" 366 | ] 367 | }, 368 | { 369 | "cell_type": "markdown", 370 | "metadata": {}, 371 | "source": [ 372 | "We can now no longer write to `file_object_2`:" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": null, 378 | "metadata": {}, 379 | "outputs": [], 380 | "source": [ 381 | "# NBVAL_RAISES_EXCEPTION\n", 382 | "## Note: ignore the above comment, this exists to allow us to test the notebook\n", 383 | "\n", 384 | "file_object_2.write('another line\\n')" 385 | ] 386 | }, 387 | { 388 | "cell_type": "markdown", 389 | "metadata": {}, 390 | "source": [ 391 | "Open `file2.txt` in a text editor to check that the lines we wrote to the file are there.\n", 392 | "\n", 393 | "If we now want to append a fourth line to `file2.txt`, we can open the file again, this time in \"append' mode.\n", 394 | "\n", 395 | "The following opens `file2.txt` in append mode, writes a third line to that file, then closes the file object." 396 | ] 397 | }, 398 | { 399 | "cell_type": "code", 400 | "execution_count": null, 401 | "metadata": {}, 402 | "outputs": [], 403 | "source": [ 404 | "file_object_3 = open('file2.txt', 'a')\n", 405 | "file_object_3.write('this line will be appended\\n')\n", 406 | "file_object_3.close()" 407 | ] 408 | }, 409 | { 410 | "cell_type": "markdown", 411 | "metadata": {}, 412 | "source": [ 413 | "You can check `file2.txt` again (by opening in a text editor)\n", 414 | "\n", 415 | "### Exercise 2\n", 416 | "\n", 417 | "Going back to our file of peptide sequences (`file1.txt`): write the updated list of peptide sequence lines to a new file called `peptides.txt`. Check your answer by reading from `peptides.txt`.\n" 418 | ] 419 | }, 420 | { 421 | "cell_type": "code", 422 | "execution_count": null, 423 | "metadata": {}, 424 | "outputs": [], 425 | "source": [ 426 | "# Exercise 2" 427 | ] 428 | }, 429 | { 430 | "cell_type": "markdown", 431 | "metadata": {}, 432 | "source": [ 433 | "## 6.3 The context manager\n", 434 | "\n", 435 | "If you don't want to have to remember to close the file when you're done with it, you can work with your file inside a `with` statement. Your file will be automatically closed at the end of the `with` statement. The technical term you may see for this type of usage is using a file as a 'context manager'." 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": null, 441 | "metadata": {}, 442 | "outputs": [], 443 | "source": [ 444 | "with open('file3.txt', 'w') as out_file:\n", 445 | " # within this statement, the file is open\n", 446 | " out_file.write('test')" 447 | ] 448 | }, 449 | { 450 | "cell_type": "markdown", 451 | "metadata": {}, 452 | "source": [ 453 | "If we try to write outside the context manager, python will give us an error as the file is no longer opened:" 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "execution_count": null, 459 | "metadata": {}, 460 | "outputs": [], 461 | "source": [ 462 | "# NBVAL_RAISES_EXCEPTION\n", 463 | "## Note: ignore the above comment, this exists to allow us to test the notebook\n", 464 | "\n", 465 | "with open('file3.txt', 'w') as out_file:\n", 466 | " # within this statement, the file is open\n", 467 | " out_file.write('test')\n", 468 | "\n", 469 | "# now the file is closed\n", 470 | "out_file.write('test')" 471 | ] 472 | }, 473 | { 474 | "cell_type": "markdown", 475 | "metadata": {}, 476 | "source": [ 477 | "## Review\n", 478 | "\n", 479 | "In this section we have learned the following:\n", 480 | "- How to open files using `open` ('*filename*', '*mode*').\n", 481 | "- The 'r' is read mode; 'w' is write mode and 'a' is append mode.\n", 482 | "- To perform operation on the lines in a file that has been opened in read mode, convert the file object to a list, using list(*file_object*).\n", 483 | "- If opening a file in write mode, the new file will overwrite another file of the same name.\n", 484 | "- To write lines to a file opened in append or write mode, use *file_object*.write('*text-to-write-to-file*').\n", 485 | "- Once finished with a file object, close it using *file_object*.close().\n", 486 | "- Using a context manager to handle file opening and closing within a `with` statement.\n" 487 | ] 488 | } 489 | ], 490 | "metadata": { 491 | "kernelspec": { 492 | "display_name": "Python 3 (ipykernel)", 493 | "language": "python", 494 | "name": "python3" 495 | }, 496 | "language_info": { 497 | "codemirror_mode": { 498 | "name": "ipython", 499 | "version": 3 500 | }, 501 | "file_extension": ".py", 502 | "mimetype": "text/x-python", 503 | "name": "python", 504 | "nbconvert_exporter": "python", 505 | "pygments_lexer": "ipython3", 506 | "version": "3.9.15" 507 | } 508 | }, 509 | "nbformat": 4, 510 | "nbformat_minor": 1 511 | } 512 | -------------------------------------------------------------------------------- /tutorials/Python/06_FileHandling/no_solutions/datafiles/file1.txt: -------------------------------------------------------------------------------- 1 | This is a file containing a list of peptide sequences: 2 | ASDIPREDSDFLLLKK 3 | HFVNNNWERT 4 | PLSDMASI 5 | KWHGLLGSF 6 | QERTKMSFFWER 7 | -------------------------------------------------------------------------------- /tutorials/Python/06_FileHandling/no_solutions/datafiles/file2.txt: -------------------------------------------------------------------------------- 1 | In this new file 2 | this is the second line 3 | and this is the third line 4 | this line will be appended 5 | -------------------------------------------------------------------------------- /tutorials/Python/06_FileHandling/no_solutions/datafiles/file3.txt: -------------------------------------------------------------------------------- 1 | test -------------------------------------------------------------------------------- /tutorials/Python/07_Functions/Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 7. Writing functions" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "\"A function is a block of organised, reusable code that can be used to perform a single, related action.\"\n", 15 | "\n", 16 | "We have already seen examples of predefined python functions, most notably `print()`, but Python gives you the ability to define your own, custom functions." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "## 7.1 Function basics\n", 24 | "\n", 25 | "The way you define a custom function is as follows:" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "def function_name(function_variables):\n", 35 | "\n", 36 | " # Tasks for the function to perform\n", 37 | " \n", 38 | " return # Whatever we want our function to produce as a result" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "A very simple example, divide any given number by 3." 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "def divider(x):\n", 55 | " return x / 3" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "Now that we defined the function, it's time to use it, or call it!\n", 63 | "\n", 64 | "What would the following examples return?" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": null, 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [ 73 | "print(divider(9))" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": null, 79 | "metadata": {}, 80 | "outputs": [], 81 | "source": [ 82 | "print(divider(0))" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "A variable defined inside a function will only be available for use inside that function. If you want to use it outside a function, you need to have it returned by your function. \n", 90 | "\n", 91 | "Let's look a few examples:" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": null, 97 | "metadata": {}, 98 | "outputs": [], 99 | "source": [ 100 | "def summ(a, b):\n", 101 | " tot_all1 = a + b\n", 102 | " print(\"The sum from inside the function is: \", tot_all1)" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "metadata": {}, 109 | "outputs": [], 110 | "source": [ 111 | "summ(2,2)" 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "metadata": {}, 117 | "source": [ 118 | "If you try to ask for total outside the function you get the following:" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": null, 124 | "metadata": {}, 125 | "outputs": [], 126 | "source": [ 127 | "# NBVAL_RAISES_EXCEPTION\n", 128 | "## Note: ignore the above comment, this exists to allow us to test the notebook\n", 129 | "\n", 130 | "print(\"The total outside the function is: \", tot_all1)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "But if you define your function like that:" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": null, 143 | "metadata": {}, 144 | "outputs": [], 145 | "source": [ 146 | "def summ2(a,b):\n", 147 | " tot_all2 = a + b\n", 148 | " print(\"The sum from inside the function is: \", tot_all2)\n", 149 | " return tot_all2" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": null, 155 | "metadata": {}, 156 | "outputs": [], 157 | "source": [ 158 | "tot_all2 = summ2(2,2)\n", 159 | "\n", 160 | "print(\"The total outside the function is: \", tot_all2)" 161 | ] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "metadata": {}, 166 | "source": [ 167 | "A slightly more complicated example. Can you identify what these functions do?" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": null, 173 | "metadata": {}, 174 | "outputs": [], 175 | "source": [ 176 | "def is_multiple(x,y):\n", 177 | " return (x % y == 0)\n", 178 | "\n", 179 | "def is_prime(n):\n", 180 | " isPrime = True \n", 181 | " for div in range(2,int(n**0.5)+1):\n", 182 | " if is_multiple(n,div):\n", 183 | " isPrime = False \n", 184 | " return isPrime" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": null, 190 | "metadata": {}, 191 | "outputs": [], 192 | "source": [ 193 | "a = is_multiple(70, 2)\n", 194 | "b = is_multiple(5, 17)\n", 195 | "c = is_multiple(72, 6)\n", 196 | "\n", 197 | "d = is_prime(90)\n", 198 | "f = is_prime(53)\n", 199 | "\n", 200 | "print(a, b, c, d, f)" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "## 7.2 Using Arguments Names" 208 | ] 209 | }, 210 | { 211 | "cell_type": "markdown", 212 | "metadata": {}, 213 | "source": [ 214 | "Sometimes the list of arguments of a function is really long and understanding which argument corresponds to which variable can be tricky. To avoid this problem we can use the arguments names expliclty. Using the above defined `is_multiple` function:" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": null, 220 | "metadata": {}, 221 | "outputs": [], 222 | "source": [ 223 | "is_multiple(x=70, y=2)" 224 | ] 225 | }, 226 | { 227 | "cell_type": "markdown", 228 | "metadata": {}, 229 | "source": [ 230 | "The nice thing about using keyword arguments is that we can completely forget about their order:" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": null, 236 | "metadata": {}, 237 | "outputs": [], 238 | "source": [ 239 | "is_multiple(y=2, x=70)" 240 | ] 241 | }, 242 | { 243 | "cell_type": "markdown", 244 | "metadata": {}, 245 | "source": [ 246 | "**NOTE**: The call `is_multiple(y=2, x=70)` corresponds to `is_multiple(70, 2)` *not* `is_multiple(2, 70)`." 247 | ] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "metadata": {}, 252 | "source": [ 253 | "## 7.3 Default Arguments" 254 | ] 255 | }, 256 | { 257 | "cell_type": "markdown", 258 | "metadata": {}, 259 | "source": [ 260 | "When defining a function, you can specify default values for some arguments:" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": null, 266 | "metadata": {}, 267 | "outputs": [], 268 | "source": [ 269 | "def person(name, age, university=\"Oxford\"):\n", 270 | " print(f\"Name: {name}\")\n", 271 | " print(f\"Age: {age}\")\n", 272 | " print(f\"University: {university}\")" 273 | ] 274 | }, 275 | { 276 | "cell_type": "markdown", 277 | "metadata": {}, 278 | "source": [ 279 | "When using a function which has \"default arguments\" declared, it is possible to omit passing any value for those \"default arguments\". When this happens, the pre-declared \"default\" values will be used:" 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "execution_count": null, 285 | "metadata": {}, 286 | "outputs": [], 287 | "source": [ 288 | "person(\"Alice\", 99)" 289 | ] 290 | }, 291 | { 292 | "cell_type": "markdown", 293 | "metadata": {}, 294 | "source": [ 295 | "However, you can explicitly specify that argument if it needs to be changed:" 296 | ] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": null, 301 | "metadata": {}, 302 | "outputs": [], 303 | "source": [ 304 | "person(\"Alice\", 99, \"Unknown\")" 305 | ] 306 | }, 307 | { 308 | "cell_type": "markdown", 309 | "metadata": {}, 310 | "source": [ 311 | "**Note** Default arguments should be declared at the end of the argument list. The following definition fails with a `SyntaxError`:" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": null, 317 | "metadata": {}, 318 | "outputs": [], 319 | "source": [ 320 | "# NBVAL_RAISES_EXCEPTION\n", 321 | "## Note: ignore the above comment, this exists to allow us to test the notebook\n", 322 | "\n", 323 | "def test(x, y=1, z):\n", 324 | " pass" 325 | ] 326 | }, 327 | { 328 | "cell_type": "markdown", 329 | "metadata": {}, 330 | "source": [ 331 | "## 7.4 Anonymous Functions (Advanced)" 332 | ] 333 | }, 334 | { 335 | "cell_type": "markdown", 336 | "metadata": {}, 337 | "source": [ 338 | "In some cases you need to define a function only once and pass it to another function. For example, if we want to apply a function `func` to a value `x` we could write the following helper function:" 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": null, 344 | "metadata": {}, 345 | "outputs": [], 346 | "source": [ 347 | "def apply(x, func):\n", 348 | " return func(x)" 349 | ] 350 | }, 351 | { 352 | "cell_type": "markdown", 353 | "metadata": {}, 354 | "source": [ 355 | "Therefore if we want to apply our `divider` function from above we can do the following:" 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "execution_count": null, 361 | "metadata": {}, 362 | "outputs": [], 363 | "source": [ 364 | "def divider(x):\n", 365 | " return x / 3\n", 366 | "\n", 367 | "result = apply(9, divider)\n", 368 | "\n", 369 | "print(result)" 370 | ] 371 | }, 372 | { 373 | "cell_type": "markdown", 374 | "metadata": {}, 375 | "source": [ 376 | "However, defining a function if we are using it only once can be time consuming especially if it consists of only one line. To avoid a full function definition we can use anonymous functions (also called `lambda` functions):" 377 | ] 378 | }, 379 | { 380 | "cell_type": "code", 381 | "execution_count": null, 382 | "metadata": {}, 383 | "outputs": [], 384 | "source": [ 385 | "result = apply(9, lambda x: x / 3)\n", 386 | "\n", 387 | "print(result)" 388 | ] 389 | }, 390 | { 391 | "cell_type": "markdown", 392 | "metadata": {}, 393 | "source": [ 394 | "We see that the `lambda` function behaves exactly like the `divider` function but is much more concise: the keyword `lambda` substitutes `def divider`, the parenthesis around the argument `x` are dropped and there is no `return` keyword (which is implicit)." 395 | ] 396 | }, 397 | { 398 | "cell_type": "markdown", 399 | "metadata": {}, 400 | "source": [ 401 | "## Review\n", 402 | "\n", 403 | "In this section we have learned the following:\n", 404 | "- How to write and execute our own custom functions.\n", 405 | "- Using defined argument names in functions.\n", 406 | "- Using default arguments in functions.\n", 407 | "- Some basics of `anonymous` functions." 408 | ] 409 | } 410 | ], 411 | "metadata": { 412 | "kernelspec": { 413 | "display_name": "Python 3 (ipykernel)", 414 | "language": "python", 415 | "name": "python3" 416 | }, 417 | "language_info": { 418 | "codemirror_mode": { 419 | "name": "ipython", 420 | "version": 3 421 | }, 422 | "file_extension": ".py", 423 | "mimetype": "text/x-python", 424 | "name": "python", 425 | "nbconvert_exporter": "python", 426 | "pygments_lexer": "ipython3", 427 | "version": "3.9.15" 428 | } 429 | }, 430 | "nbformat": 4, 431 | "nbformat_minor": 1 432 | } 433 | -------------------------------------------------------------------------------- /tutorials/Python/07_Functions/no_solutions/Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 7. Writing functions" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "\"A function is a block of organised, reusable code that can be used to perform a single, related action.\"\n", 15 | "\n", 16 | "We have already seen examples of predefined python functions, most notably `print()`, but Python gives you the ability to define your own, custom functions." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "## 7.1 Function basics\n", 24 | "\n", 25 | "The way you define a custom function is as follows:" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "def function_name(function_variables):\n", 35 | "\n", 36 | " # Tasks for the function to perform\n", 37 | " \n", 38 | " return # Whatever we want our function to produce as a result" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "A very simple example, divide any given number by 3." 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "def divider(x):\n", 55 | " return x / 3" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "Now that we defined the function, it's time to use it, or call it!\n", 63 | "\n", 64 | "What would the following examples return?" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": null, 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [ 73 | "print(divider(9))" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": null, 79 | "metadata": {}, 80 | "outputs": [], 81 | "source": [ 82 | "print(divider(0))" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "A variable defined inside a function will only be available for use inside that function. If you want to use it outside a function, you need to have it returned by your function. \n", 90 | "\n", 91 | "Let's look a few examples:" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": null, 97 | "metadata": {}, 98 | "outputs": [], 99 | "source": [ 100 | "def summ(a, b):\n", 101 | " tot_all1 = a + b\n", 102 | " print(\"The sum from inside the function is: \", tot_all1)" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "metadata": {}, 109 | "outputs": [], 110 | "source": [ 111 | "summ(2,2)" 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "metadata": {}, 117 | "source": [ 118 | "If you try to ask for total outside the function you get the following:" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": null, 124 | "metadata": {}, 125 | "outputs": [], 126 | "source": [ 127 | "# NBVAL_RAISES_EXCEPTION\n", 128 | "## Note: ignore the above comment, this exists to allow us to test the notebook\n", 129 | "\n", 130 | "print(\"The total outside the function is: \", tot_all1)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "But if you define your function like that:" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": null, 143 | "metadata": {}, 144 | "outputs": [], 145 | "source": [ 146 | "def summ2(a,b):\n", 147 | " tot_all2 = a + b\n", 148 | " print(\"The sum from inside the function is: \", tot_all2)\n", 149 | " return tot_all2" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": null, 155 | "metadata": {}, 156 | "outputs": [], 157 | "source": [ 158 | "tot_all2 = summ2(2,2)\n", 159 | "\n", 160 | "print(\"The total outside the function is: \", tot_all2)" 161 | ] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "metadata": {}, 166 | "source": [ 167 | "A slightly more complicated example. Can you identify what these functions do?" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": null, 173 | "metadata": {}, 174 | "outputs": [], 175 | "source": [ 176 | "def is_multiple(x,y):\n", 177 | " return (x % y == 0)\n", 178 | "\n", 179 | "def is_prime(n):\n", 180 | " isPrime = True \n", 181 | " for div in range(2,int(n**0.5)+1):\n", 182 | " if is_multiple(n,div):\n", 183 | " isPrime = False \n", 184 | " return isPrime" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": null, 190 | "metadata": {}, 191 | "outputs": [], 192 | "source": [ 193 | "a = is_multiple(70, 2)\n", 194 | "b = is_multiple(5, 17)\n", 195 | "c = is_multiple(72, 6)\n", 196 | "\n", 197 | "d = is_prime(90)\n", 198 | "f = is_prime(53)\n", 199 | "\n", 200 | "print(a, b, c, d, f)" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "## 7.2 Using Arguments Names" 208 | ] 209 | }, 210 | { 211 | "cell_type": "markdown", 212 | "metadata": {}, 213 | "source": [ 214 | "Sometimes the list of arguments of a function is really long and understanding which argument corresponds to which variable can be tricky. To avoid this problem we can use the arguments names expliclty. Using the above defined `is_multiple` function:" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": null, 220 | "metadata": {}, 221 | "outputs": [], 222 | "source": [ 223 | "is_multiple(x=70, y=2)" 224 | ] 225 | }, 226 | { 227 | "cell_type": "markdown", 228 | "metadata": {}, 229 | "source": [ 230 | "The nice thing about using keyword arguments is that we can completely forget about their order:" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": null, 236 | "metadata": {}, 237 | "outputs": [], 238 | "source": [ 239 | "is_multiple(y=2, x=70)" 240 | ] 241 | }, 242 | { 243 | "cell_type": "markdown", 244 | "metadata": {}, 245 | "source": [ 246 | "**NOTE**: The call `is_multiple(y=2, x=70)` corresponds to `is_multiple(70, 2)` *not* `is_multiple(2, 70)`." 247 | ] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "metadata": {}, 252 | "source": [ 253 | "## 7.3 Default Arguments" 254 | ] 255 | }, 256 | { 257 | "cell_type": "markdown", 258 | "metadata": {}, 259 | "source": [ 260 | "When defining a function, you can specify default values for some arguments:" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": null, 266 | "metadata": {}, 267 | "outputs": [], 268 | "source": [ 269 | "def person(name, age, university=\"Oxford\"):\n", 270 | " print(f\"Name: {name}\")\n", 271 | " print(f\"Age: {age}\")\n", 272 | " print(f\"University: {university}\")" 273 | ] 274 | }, 275 | { 276 | "cell_type": "markdown", 277 | "metadata": {}, 278 | "source": [ 279 | "When using a function which has \"default arguments\" declared, it is possible to omit passing any value for those \"default arguments\". When this happens, the pre-declared \"default\" values will be used:" 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "execution_count": null, 285 | "metadata": {}, 286 | "outputs": [], 287 | "source": [ 288 | "person(\"Alice\", 99)" 289 | ] 290 | }, 291 | { 292 | "cell_type": "markdown", 293 | "metadata": {}, 294 | "source": [ 295 | "However, you can explicitly specify that argument if it needs to be changed:" 296 | ] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": null, 301 | "metadata": {}, 302 | "outputs": [], 303 | "source": [ 304 | "person(\"Alice\", 99, \"Unknown\")" 305 | ] 306 | }, 307 | { 308 | "cell_type": "markdown", 309 | "metadata": {}, 310 | "source": [ 311 | "**Note** Default arguments should be declared at the end of the argument list. The following definition fails with a `SyntaxError`:" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": null, 317 | "metadata": {}, 318 | "outputs": [], 319 | "source": [ 320 | "# NBVAL_RAISES_EXCEPTION\n", 321 | "## Note: ignore the above comment, this exists to allow us to test the notebook\n", 322 | "\n", 323 | "def test(x, y=1, z):\n", 324 | " pass" 325 | ] 326 | }, 327 | { 328 | "cell_type": "markdown", 329 | "metadata": {}, 330 | "source": [ 331 | "## 7.4 Anonymous Functions (Advanced)" 332 | ] 333 | }, 334 | { 335 | "cell_type": "markdown", 336 | "metadata": {}, 337 | "source": [ 338 | "In some cases you need to define a function only once and pass it to another function. For example, if we want to apply a function `func` to a value `x` we could write the following helper function:" 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": null, 344 | "metadata": {}, 345 | "outputs": [], 346 | "source": [ 347 | "def apply(x, func):\n", 348 | " return func(x)" 349 | ] 350 | }, 351 | { 352 | "cell_type": "markdown", 353 | "metadata": {}, 354 | "source": [ 355 | "Therefore if we want to apply our `divider` function from above we can do the following:" 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "execution_count": null, 361 | "metadata": {}, 362 | "outputs": [], 363 | "source": [ 364 | "def divider(x):\n", 365 | " return x / 3\n", 366 | "\n", 367 | "result = apply(9, divider)\n", 368 | "\n", 369 | "print(result)" 370 | ] 371 | }, 372 | { 373 | "cell_type": "markdown", 374 | "metadata": {}, 375 | "source": [ 376 | "However, defining a function if we are using it only once can be time consuming especially if it consists of only one line. To avoid a full function definition we can use anonymous functions (also called `lambda` functions):" 377 | ] 378 | }, 379 | { 380 | "cell_type": "code", 381 | "execution_count": null, 382 | "metadata": {}, 383 | "outputs": [], 384 | "source": [ 385 | "result = apply(9, lambda x: x / 3)\n", 386 | "\n", 387 | "print(result)" 388 | ] 389 | }, 390 | { 391 | "cell_type": "markdown", 392 | "metadata": {}, 393 | "source": [ 394 | "We see that the `lambda` function behaves exactly like the `divider` function but is much more concise: the keyword `lambda` substitutes `def divider`, the parenthesis around the argument `x` are dropped and there is no `return` keyword (which is implicit)." 395 | ] 396 | }, 397 | { 398 | "cell_type": "markdown", 399 | "metadata": {}, 400 | "source": [ 401 | "## Review\n", 402 | "\n", 403 | "In this section we have learned the following:\n", 404 | "- How to write and execute our own custom functions.\n", 405 | "- Using defined argument names in functions.\n", 406 | "- Using default arguments in functions.\n", 407 | "- Some basics of `anonymous` functions." 408 | ] 409 | } 410 | ], 411 | "metadata": { 412 | "kernelspec": { 413 | "display_name": "Python 3 (ipykernel)", 414 | "language": "python", 415 | "name": "python3" 416 | }, 417 | "language_info": { 418 | "codemirror_mode": { 419 | "name": "ipython", 420 | "version": 3 421 | }, 422 | "file_extension": ".py", 423 | "mimetype": "text/x-python", 424 | "name": "python", 425 | "nbconvert_exporter": "python", 426 | "pygments_lexer": "ipython3", 427 | "version": "3.9.15" 428 | } 429 | }, 430 | "nbformat": 4, 431 | "nbformat_minor": 1 432 | } 433 | -------------------------------------------------------------------------------- /tutorials/Python/08_Documentation/Documentation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 8 Help and documenting" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Python can be overwhelming, especially when you are reading someone else's scripts.\n", 15 | "\n", 16 | "There are two ways to make this simpler:\n", 17 | " \n", 18 | "1. Comments on scripts\n", 19 | "2. Use the help module" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "metadata": {}, 25 | "source": [ 26 | "## 8.1 Line Comments" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "metadata": {}, 32 | "source": [ 33 | "As previously mentioned, comments are simple to introduce within code. Using `#` in a line makes the rest of the line a comment. The interpreter will simply skip anything after the `#` when processing your code. This means you can annotate all the little details of your code to help others follow it better (and yourself, too!)." 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "# Define list x\n", 43 | "x = [1, 2, 3]\n", 44 | "\n", 45 | "# Print list x\n", 46 | "print(x)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "Unfortunately, too many comments can also be confusing; a good way to avoid many comments is to give a sensible name to your variables and functions. The following code is not very clear and therefore it has to be commented:" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [ 62 | "m = 1 # Mass\n", 63 | "a = 3 # Acceleration\n", 64 | "\n", 65 | "# Compute force f\n", 66 | "f = m * a" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "By choosing better names for our variables we can completely avoid comments without losing clarity:" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": null, 79 | "metadata": {}, 80 | "outputs": [], 81 | "source": [ 82 | "mass = 1\n", 83 | "acceleration = 3\n", 84 | "\n", 85 | "force = mass * acceleration" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "## 8.2 Multi-Line Comments" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "A multi-line string can be used as a comment:" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": null, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [ 108 | "\"\"\"\n", 109 | "Very long\n", 110 | "and multi-line\n", 111 | "comment\n", 112 | "\"\"\"\n", 113 | "\n", 114 | "print(\"The previous comment is ignored.\")" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "## 8.3 Docstrings" 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": {}, 127 | "source": [ 128 | "A multi-line comment used to document a function is called *docstring*:" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": null, 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [ 137 | "def force(mass, acceleration):\n", 138 | " \"\"\"\n", 139 | " This function computes the force using Newton's first law.\n", 140 | " \"\"\"\n", 141 | " return mass * acceleration" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | "Docstrings are extremely helpful to document functions, especially if other people need to use them. Luckily, all good Python packages use docstrings to document their functions!\n", 149 | "\n", 150 | "If you write any function, you are heavily encouraged to write docstrings for them.\n", 151 | "\n", 152 | "For a good example of how to write docstring, we refer you to the numpy docstring guide: https://numpydoc.readthedocs.io/en/latest/format.html" 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "metadata": {}, 158 | "source": [ 159 | "## 8.4 Help" 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "metadata": {}, 165 | "source": [ 166 | "The help module can give a lot of information on how to use certain functions. You can pass a function the `help` to get some information:" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": null, 172 | "metadata": {}, 173 | "outputs": [], 174 | "source": [ 175 | "help(list)" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "With the `help` function in hand, you can quickly look at the docstring for different functions. It's extremely clear why docstring are so important:" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": null, 188 | "metadata": {}, 189 | "outputs": [], 190 | "source": [ 191 | "help(force)" 192 | ] 193 | }, 194 | { 195 | "cell_type": "markdown", 196 | "metadata": {}, 197 | "source": [ 198 | "If our function was not documented with a docstring, this would be the result:" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": null, 204 | "metadata": {}, 205 | "outputs": [], 206 | "source": [ 207 | "def f(m, a):\n", 208 | " return m * a\n", 209 | "\n", 210 | "help(f)" 211 | ] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "metadata": {}, 216 | "source": [ 217 | "Not very informative, is it? In Jupyter Notebooks (or the [IPython](https://ipython.org/) console) you can also use `?` to obtain additional information interactively:" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": null, 223 | "metadata": {}, 224 | "outputs": [], 225 | "source": [ 226 | "force?" 227 | ] 228 | } 229 | ], 230 | "metadata": { 231 | "kernelspec": { 232 | "display_name": "Python 3 (ipykernel)", 233 | "language": "python", 234 | "name": "python3" 235 | }, 236 | "language_info": { 237 | "codemirror_mode": { 238 | "name": "ipython", 239 | "version": 3 240 | }, 241 | "file_extension": ".py", 242 | "mimetype": "text/x-python", 243 | "name": "python", 244 | "nbconvert_exporter": "python", 245 | "pygments_lexer": "ipython3", 246 | "version": "3.9.15" 247 | } 248 | }, 249 | "nbformat": 4, 250 | "nbformat_minor": 1 251 | } 252 | -------------------------------------------------------------------------------- /tutorials/Python/08_Documentation/no_solutions/Documentation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 8 Help and documenting" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Python can be overwhelming, especially when you are reading someone else's scripts.\n", 15 | "\n", 16 | "There are two ways to make this simpler:\n", 17 | " \n", 18 | "1. Comments on scripts\n", 19 | "2. Use the help module" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "metadata": {}, 25 | "source": [ 26 | "## 8.1 Line Comments" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "metadata": {}, 32 | "source": [ 33 | "As previously mentioned, comments are simple to introduce within code. Using `#` in a line makes the rest of the line a comment. The interpreter will simply skip anything after the `#` when processing your code. This means you can annotate all the little details of your code to help others follow it better (and yourself, too!)." 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "# Define list x\n", 43 | "x = [1, 2, 3]\n", 44 | "\n", 45 | "# Print list x\n", 46 | "print(x)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "Unfortunately, too many comments can also be confusing; a good way to avoid many comments is to give a sensible name to your variables and functions. The following code is not very clear and therefore it has to be commented:" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [ 62 | "m = 1 # Mass\n", 63 | "a = 3 # Acceleration\n", 64 | "\n", 65 | "# Compute force f\n", 66 | "f = m * a" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "By choosing better names for our variables we can completely avoid comments without losing clarity:" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": null, 79 | "metadata": {}, 80 | "outputs": [], 81 | "source": [ 82 | "mass = 1\n", 83 | "acceleration = 3\n", 84 | "\n", 85 | "force = mass * acceleration" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "## 8.2 Multi-Line Comments" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "A multi-line string can be used as a comment:" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": null, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [ 108 | "\"\"\"\n", 109 | "Very long\n", 110 | "and multi-line\n", 111 | "comment\n", 112 | "\"\"\"\n", 113 | "\n", 114 | "print(\"The previous comment is ignored.\")" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "## 8.3 Docstrings" 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": {}, 127 | "source": [ 128 | "A multi-line comment used to document a function is called *docstring*:" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": null, 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [ 137 | "def force(mass, acceleration):\n", 138 | " \"\"\"\n", 139 | " This function computes the force using Newton's first law.\n", 140 | " \"\"\"\n", 141 | " return mass * acceleration" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | "Docstrings are extremely helpful to document functions, especially if other people need to use them. Luckily, all good Python packages use docstrings to document their functions!\n", 149 | "\n", 150 | "If you write any function, you are heavily encouraged to write docstrings for them.\n", 151 | "\n", 152 | "For a good example of how to write docstring, we refer you to the numpy docstring guide: https://numpydoc.readthedocs.io/en/latest/format.html" 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "metadata": {}, 158 | "source": [ 159 | "## 8.4 Help" 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "metadata": {}, 165 | "source": [ 166 | "The help module can give a lot of information on how to use certain functions. You can pass a function the `help` to get some information:" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": null, 172 | "metadata": {}, 173 | "outputs": [], 174 | "source": [ 175 | "help(list)" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "With the `help` function in hand, you can quickly look at the docstring for different functions. It's extremely clear why docstring are so important:" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": null, 188 | "metadata": {}, 189 | "outputs": [], 190 | "source": [ 191 | "help(force)" 192 | ] 193 | }, 194 | { 195 | "cell_type": "markdown", 196 | "metadata": {}, 197 | "source": [ 198 | "If our function was not documented with a docstring, this would be the result:" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": null, 204 | "metadata": {}, 205 | "outputs": [], 206 | "source": [ 207 | "def f(m, a):\n", 208 | " return m * a\n", 209 | "\n", 210 | "help(f)" 211 | ] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "metadata": {}, 216 | "source": [ 217 | "Not very informative, is it? In Jupyter Notebooks (or the [IPython](https://ipython.org/) console) you can also use `?` to obtain additional information interactively:" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": null, 223 | "metadata": {}, 224 | "outputs": [], 225 | "source": [ 226 | "force?" 227 | ] 228 | } 229 | ], 230 | "metadata": { 231 | "kernelspec": { 232 | "display_name": "Python 3 (ipykernel)", 233 | "language": "python", 234 | "name": "python3" 235 | }, 236 | "language_info": { 237 | "codemirror_mode": { 238 | "name": "ipython", 239 | "version": 3 240 | }, 241 | "file_extension": ".py", 242 | "mimetype": "text/x-python", 243 | "name": "python", 244 | "nbconvert_exporter": "python", 245 | "pygments_lexer": "ipython3", 246 | "version": "3.9.15" 247 | } 248 | }, 249 | "nbformat": 4, 250 | "nbformat_minor": 1 251 | } 252 | -------------------------------------------------------------------------------- /tutorials/Python/09_Libraries/Libraries.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 9. Python libraries" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## 9.1 Using libraries" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "While creating your own functions can be incredibly useful, others may have done so already (and these might actually be faster).\n", 22 | "\n", 23 | "Often, these will come packaged as part of a wider python library." 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "A simple example of this is converting degree angles to radians.\n", 31 | "\n", 32 | "The basic formalism for this is:\n", 33 | "\n", 34 | "radians = degrees * pi / 180.0" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "### Exercise 1\n", 42 | "\n", 43 | "Write a function that converts degrees into radians" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "metadata": { 50 | "solution2": "hidden", 51 | "solution2_first": true 52 | }, 53 | "outputs": [], 54 | "source": [ 55 | "# Exercise 1" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": null, 61 | "metadata": { 62 | "solution2": "hidden" 63 | }, 64 | "outputs": [], 65 | "source": [ 66 | "# Exercise 1\n", 67 | "def deg2rad(angle):\n", 68 | " pi = 3.141593\n", 69 | " return angle * (pi / 180)\n", 70 | "\n", 71 | "print(deg2rad(65))" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "However, the `math` python library already offers a function to do this, allowing you to perform the same operation without having to write all the above lines." 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": {}, 85 | "outputs": [], 86 | "source": [ 87 | "import math\n", 88 | "print(math.radians(65))" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "## 9.2 Importing libraries" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "In the example above, we used the `math` library through the use of the `import` statement.\n", 103 | "\n", 104 | "In order to use any library you first must `import` it. For example to use the `string` library we do:" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "import string" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "Sometimes you might want to import a library but call it something else than its standard name. For example, you might consider that the `random` library name it too long for you, and you want to instead call it as `rand`. To do this we import the library as an *alias* using the `import libraryname as newname` construct: " 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [ 129 | "import random as rand" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "We can now use `rand` just like we would have used `random` if we had plainly imported the module." 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "## 9.3 Using library functions and objects" 144 | ] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "metadata": {}, 149 | "source": [ 150 | "Python libraries often contain multiple functions and objects that can be accessed and used.\n", 151 | "To access a given function you only need to call it using the construct `libraryname.functioname()`.\n", 152 | "\n", 153 | "For example to use the `pi` variable in math we would just do:" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": null, 159 | "metadata": {}, 160 | "outputs": [], 161 | "source": [ 162 | "import math\n", 163 | "\n", 164 | "print(math.pi)" 165 | ] 166 | }, 167 | { 168 | "cell_type": "markdown", 169 | "metadata": {}, 170 | "source": [ 171 | "Libraries usually contain lots of modules that themselves contain several functions.\n", 172 | "For example the commonly used `argarse` library (which is used to process arguments passed to python scripts), has an `ArgumentParser` object which is used for most argument parsing work.\n", 173 | "\n", 174 | "We can use it in the following way:" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": null, 180 | "metadata": {}, 181 | "outputs": [], 182 | "source": [ 183 | "import argparse\n", 184 | "\n", 185 | "parser = argparse.ArgumentParser" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "However, if we wanted to solely use the ArgumentParser object we could also selectively import the object using the `from` construct:" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": null, 198 | "metadata": {}, 199 | "outputs": [], 200 | "source": [ 201 | "from argparse import ArgumentParser\n", 202 | "\n", 203 | "parser = ArgumentParser" 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "The exact means by which a given library's functions/objects/modules are used will differ from library to library. Whenever using a new library, we recommend that you spend time reading the library's Application Program Interface (API) documentation.\n", 211 | "\n", 212 | "For example, you can see the documentation for argparse here: https://docs.python.org/3/library/argparse.html" 213 | ] 214 | }, 215 | { 216 | "cell_type": "markdown", 217 | "metadata": {}, 218 | "source": [ 219 | "## 9.4 A whole world of libraries" 220 | ] 221 | }, 222 | { 223 | "cell_type": "markdown", 224 | "metadata": {}, 225 | "source": [ 226 | "One of the main advantages of python is that there are many different libraries which exist that can help you achieve any goal.\n", 227 | "\n", 228 | "The standard python distribution offers a limited about of libraries, termed the \"standard library\", details about them can be found here: https://docs.python.org/3/library/\n", 229 | "\n", 230 | "These include the above mentioned `math`, `random`, and `argparse` libraries.\n", 231 | "\n", 232 | "Beyond the \"standard library\", there are also many amazing community driver packages, here is a non-exhaustive list of them:\n", 233 | "\n", 234 | "- NumPy (http://www.numpy.org/) and SciPy (https://www.scipy.org/) are excellent libraries \n", 235 | " that do a lot more that simple mathematical operations (like the Euclidean distance calculator above).\n", 236 | " These also interface very well with plotting libraries.\n", 237 | "\n", 238 | "- Matplotlib (https://matplotlib.org/) is the most commonly used plotting library available. It is simple, \n", 239 | " and can do a lot. Other plotting libraries, such as GGPlot (http://ggplot.yhathq.com/), SeaBorn (https://seaborn.pydata.org/), and Bokeh (https://bokeh.pydata.org/en/latest/).\n", 240 | "\n", 241 | "- Pandas (https://pandas.pydata.org/) is a great library to manipulate data structures. \n", 242 | " Its ease of use makes it ideal to work with large data sets.\n", 243 | "\n", 244 | "- MDAnalysis (https://www.mdanalysis.org/), MDTraj (http://mdtraj.org/1.9.0/), and \n", 245 | " PyTraj (https://github.com/Amber-MD/pytraj), are some of the libraries used to process\n", 246 | " Molecular Dynamics trajcetories and other files. These interface very well with \n", 247 | " NumPy and Pandas and Matplotlib.\n", 248 | "\n", 249 | "- SciKit-Learn (http://scikit-learn.org/stable/) for machine learning in Python.\n", 250 | "\n", 251 | "- MPI4Py (http://mpi4py.readthedocs.io/en/stable/) allows Python scripts to be parallelised \n", 252 | " (run over multiple processors).\n", 253 | "\n", 254 | "- Cython (http://cython.org/) allows you to write parts of your code in C/C++, making it very fast.\n", 255 | "\n", 256 | "Each library will have its means of directly installing them, however keeping track of various installs and avoiding clashes between libraries can be quite tedious. We instead recommend that a package manager like `conda` be used instead.\n", 257 | "\n", 258 | "See here for more details about conda: https://www.anaconda.com/distribution/" 259 | ] 260 | } 261 | ], 262 | "metadata": { 263 | "kernelspec": { 264 | "display_name": "Python 3 (ipykernel)", 265 | "language": "python", 266 | "name": "python3" 267 | }, 268 | "language_info": { 269 | "codemirror_mode": { 270 | "name": "ipython", 271 | "version": 3 272 | }, 273 | "file_extension": ".py", 274 | "mimetype": "text/x-python", 275 | "name": "python", 276 | "nbconvert_exporter": "python", 277 | "pygments_lexer": "ipython3", 278 | "version": "3.9.15" 279 | } 280 | }, 281 | "nbformat": 4, 282 | "nbformat_minor": 1 283 | } 284 | -------------------------------------------------------------------------------- /tutorials/Python/09_Libraries/no_solutions/Libraries.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 9. Python libraries" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## 9.1 Using libraries" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "While creating your own functions can be incredibly useful, others may have done so already (and these might actually be faster).\n", 22 | "\n", 23 | "Often, these will come packaged as part of a wider python library." 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "A simple example of this is converting degree angles to radians.\n", 31 | "\n", 32 | "The basic formalism for this is:\n", 33 | "\n", 34 | "radians = degrees * pi / 180.0" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "### Exercise 1\n", 42 | "\n", 43 | "Write a function that converts degrees into radians" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "# Exercise 1" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "However, the `math` python library already offers a function to do this, allowing you to perform the same operation without having to write all the above lines." 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "import math\n", 69 | "print(math.radians(65))" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "## 9.2 Importing libraries" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "In the example above, we used the `math` library through the use of the `import` statement.\n", 84 | "\n", 85 | "In order to use any library you first must `import` it. For example to use the `string` library we do:" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "import string" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "Sometimes you might want to import a library but call it something else than its standard name. For example, you might consider that the `random` library name it too long for you, and you want to instead call it as `rand`. To do this we import the library as an *alias* using the `import libraryname as newname` construct: " 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "metadata": {}, 108 | "outputs": [], 109 | "source": [ 110 | "import random as rand" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "We can now use `rand` just like we would have used `random` if we had plainly imported the module." 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": {}, 123 | "source": [ 124 | "## 9.3 Using library functions and objects" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "Python libraries often contain multiple functions and objects that can be accessed and used.\n", 132 | "To access a given function you only need to call it using the construct `libraryname.functioname()`.\n", 133 | "\n", 134 | "For example to use the `pi` variable in math we would just do:" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": null, 140 | "metadata": {}, 141 | "outputs": [], 142 | "source": [ 143 | "import math\n", 144 | "\n", 145 | "print(math.pi)" 146 | ] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "metadata": {}, 151 | "source": [ 152 | "Libraries usually contain lots of modules that themselves contain several functions.\n", 153 | "For example the commonly used `argarse` library (which is used to process arguments passed to python scripts), has an `ArgumentParser` object which is used for most argument parsing work.\n", 154 | "\n", 155 | "We can use it in the following way:" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": null, 161 | "metadata": {}, 162 | "outputs": [], 163 | "source": [ 164 | "import argparse\n", 165 | "\n", 166 | "parser = argparse.ArgumentParser" 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": {}, 172 | "source": [ 173 | "However, if we wanted to solely use the ArgumentParser object we could also selectively import the object using the `from` construct:" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": null, 179 | "metadata": {}, 180 | "outputs": [], 181 | "source": [ 182 | "from argparse import ArgumentParser\n", 183 | "\n", 184 | "parser = ArgumentParser" 185 | ] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": {}, 190 | "source": [ 191 | "The exact means by which a given library's functions/objects/modules are used will differ from library to library. Whenever using a new library, we recommend that you spend time reading the library's Application Program Interface (API) documentation.\n", 192 | "\n", 193 | "For example, you can see the documentation for argparse here: https://docs.python.org/3/library/argparse.html" 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": {}, 199 | "source": [ 200 | "## 9.4 A whole world of libraries" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "One of the main advantages of python is that there are many different libraries which exist that can help you achieve any goal.\n", 208 | "\n", 209 | "The standard python distribution offers a limited about of libraries, termed the \"standard library\", details about them can be found here: https://docs.python.org/3/library/\n", 210 | "\n", 211 | "These include the above mentioned `math`, `random`, and `argparse` libraries.\n", 212 | "\n", 213 | "Beyond the \"standard library\", there are also many amazing community driver packages, here is a non-exhaustive list of them:\n", 214 | "\n", 215 | "- NumPy (http://www.numpy.org/) and SciPy (https://www.scipy.org/) are excellent libraries \n", 216 | " that do a lot more that simple mathematical operations (like the Euclidean distance calculator above).\n", 217 | " These also interface very well with plotting libraries.\n", 218 | "\n", 219 | "- Matplotlib (https://matplotlib.org/) is the most commonly used plotting library available. It is simple, \n", 220 | " and can do a lot. Other plotting libraries, such as GGPlot (http://ggplot.yhathq.com/), SeaBorn (https://seaborn.pydata.org/), and Bokeh (https://bokeh.pydata.org/en/latest/).\n", 221 | "\n", 222 | "- Pandas (https://pandas.pydata.org/) is a great library to manipulate data structures. \n", 223 | " Its ease of use makes it ideal to work with large data sets.\n", 224 | "\n", 225 | "- MDAnalysis (https://www.mdanalysis.org/), MDTraj (http://mdtraj.org/1.9.0/), and \n", 226 | " PyTraj (https://github.com/Amber-MD/pytraj), are some of the libraries used to process\n", 227 | " Molecular Dynamics trajcetories and other files. These interface very well with \n", 228 | " NumPy and Pandas and Matplotlib.\n", 229 | "\n", 230 | "- SciKit-Learn (http://scikit-learn.org/stable/) for machine learning in Python.\n", 231 | "\n", 232 | "- MPI4Py (http://mpi4py.readthedocs.io/en/stable/) allows Python scripts to be parallelised \n", 233 | " (run over multiple processors).\n", 234 | "\n", 235 | "- Cython (http://cython.org/) allows you to write parts of your code in C/C++, making it very fast.\n", 236 | "\n", 237 | "Each library will have its means of directly installing them, however keeping track of various installs and avoiding clashes between libraries can be quite tedious. We instead recommend that a package manager like `conda` be used instead.\n", 238 | "\n", 239 | "See here for more details about conda: https://www.anaconda.com/distribution/" 240 | ] 241 | } 242 | ], 243 | "metadata": { 244 | "kernelspec": { 245 | "display_name": "Python 3 (ipykernel)", 246 | "language": "python", 247 | "name": "python3" 248 | }, 249 | "language_info": { 250 | "codemirror_mode": { 251 | "name": "ipython", 252 | "version": 3 253 | }, 254 | "file_extension": ".py", 255 | "mimetype": "text/x-python", 256 | "name": "python", 257 | "nbconvert_exporter": "python", 258 | "pygments_lexer": "ipython3", 259 | "version": "3.9.15" 260 | } 261 | }, 262 | "nbformat": 4, 263 | "nbformat_minor": 1 264 | } 265 | -------------------------------------------------------------------------------- /tutorials/Python/10_Numpy/Numpy.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 10. Numpy" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## 10.1 A short introduction to NumPy\n", 15 | "\n", 16 | "NumPy stands for \"Numerical Python\" and, along with some other libraries (SciPy, Pandas, etc.), is the core library used for scientific computing. It contains a big number of tools and functions that can be used to solve an array of common and not so common problems, some examples of which you'll see below. However, most importantly, NumPy contains the all powerful NumPy arrays!\n", 17 | "\n", 18 | "For an example of how NumPy can be used in scientific computing, see the following code for calculating the Euclidean distance between two points:" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "import numpy\n", 28 | "print(numpy.linalg.norm(numpy.array([1,2,3]) - numpy.array([1,2,3])))\n", 29 | "print(numpy.linalg.norm(numpy.array([1,2,3]) - numpy.array([3,2,1])))" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "Many scientific computing libraries, e.g. MDAnalysis (see section 11), rely on NumPy to hold and operate on the data passed to them.\n", 37 | "\n", 38 | "In this section, we will very briefly introduce the NumPy library, this will make work in Section 11 easier to understand." 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "## 10.2 The NumPy array" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "A numpy array is a bit like an improved version of a Python list,. It \"is a high-performance multidimensional array object that is a powerful data structure for efficient computation of arrays and matrices. To work with these arrays, there’s a huge amount of high-level mathematical functions which operate on these matrices and arrays.\" " 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "In other words, an array is a good structure to store data from 1D, 2D, 3D, or higher dimensional matrices." 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "This means that arrays can have rows and columns. In a 2D array, rows can also be called as the \"axis 0\" while columns are the \"axis 1\". The number of axis will go up according to the dimentions of the arrays so a 3D array would also have an \"axis 2\". These axes are useful when it comes to manipulating the data in your arrays. \n", 67 | "\n", 68 | "We will see some hands on examples of arrays below." 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "First, let us build a 2D array.\n", 76 | "\n", 77 | "NumPy has an `numpy.empty` object which will allow you to build any sized \"empty\" NumPy array. However, when doing this the array will be returned with array elements set to any random value. Instead, we often want to initialise our array to a set value to begin with.\n", 78 | "\n", 79 | "There are two main methods for doing this in NumPy, `ones` and `zeros`. These create NumPy arrays filled with 0 and 1 respectively.\n", 80 | "\n", 81 | "Here we will build an array filled with the value 1, using `numpy.ones`. We are passing the argument (5,3) which dictates the shape of the array. The first value details the size of axis 0 (5), and the second value the size of axis 1 (3)." 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": null, 87 | "metadata": {}, 88 | "outputs": [], 89 | "source": [ 90 | "# An example of a 2D array\n", 91 | "my_2d_array = numpy.ones((5, 3))\n", 92 | "\n", 93 | "print(my_2d_array)" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "NumPy arrays have many built-in methods, for example, the `shape` method will return the length of the array along each axis." 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": null, 106 | "metadata": {}, 107 | "outputs": [], 108 | "source": [ 109 | "# Print out the shape of `my_array`\n", 110 | "print(my_2d_array.shape)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "NumPy arrays also have special data types (named dtypes) which describe the values held within the array.\n", 118 | "\n", 119 | "Examples include; `numpy.int64`, `numpy.float64`, and `numpy.complex64`.\n", 120 | "\n", 121 | "The type can be set when building the array using the `dtype` argument to methods such as; `numpy.ones`, `numpy.zeros`, and `numpy.empty`.\n", 122 | "\n", 123 | "Each array will have a `dtype` attribute which will tell you what the array assumes the underlying data to be. In this case, we see that the `dtype` of `my_2d_array` is `numpy.float64` which is the default data type of `numpy.ones`:" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": null, 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [ 132 | "# Print out the data type of `my_array`\n", 133 | "# Similar to the type() function\n", 134 | "print(my_2d_array.dtype)" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": {}, 140 | "source": [ 141 | "Similarly, we could use the same idea to build a 3D array of shape (2,5,3), but this time filled with integers `dtype=numpy.int64`:" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": null, 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [ 150 | "# This is an example of a 3D array\n", 151 | "my_3d_array = numpy.ones((2, 5, 3), dtype=numpy.int64)\n", 152 | "\n", 153 | "print(my_3d_array)\n", 154 | "\n", 155 | "# Print out the shape of `my_array`\n", 156 | "print(my_3d_array.shape)\n", 157 | "\n", 158 | "# Print out the data type of `my_array`\n", 159 | "print(my_3d_array.dtype)" 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "metadata": {}, 165 | "source": [ 166 | "**Note:** The number in the `numpy.int64` `dtype` declaration stands for the size of the data type in bits. In this case, we are asking for a 64 bit integer, which means that it can hold any integer numbers that range from `-9223372036854775808` to `9223372036854775808`." 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": {}, 172 | "source": [ 173 | "## 10.3 Manipulating NumPy arrays\n", 174 | "\n", 175 | "You can also add, subtract, multiply or divide your arrays." 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": null, 181 | "metadata": {}, 182 | "outputs": [], 183 | "source": [ 184 | "my_new_array = my_2d_array + 1\n", 185 | "\n", 186 | "print(my_new_array)" 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": null, 192 | "metadata": {}, 193 | "outputs": [], 194 | "source": [ 195 | "my_new_array = my_2d_array + my_3d_array\n", 196 | "\n", 197 | "print(my_new_array)" 198 | ] 199 | }, 200 | { 201 | "cell_type": "markdown", 202 | "metadata": {}, 203 | "source": [ 204 | "In order to perform arithmetic, and other, operations on two, or more arrays, there are certain criteria that need to be fulfilled.\n", 205 | "\n", 206 | "Firstly, their dimensions need to be compatible. This is the case when they are equal.\n", 207 | "\n", 208 | "Secondly, two dimensions are compatible when one of them is 1.\n", 209 | "\n", 210 | "Thirdly, the arrays need to be compatible in all directions.\n", 211 | "\n", 212 | "Although standard use of `-`, `+`, `/` and `*` operators work, NumPy also provides a series of mathematical functions to achieve several types of array manipulations.\n", 213 | "\n", 214 | "For example one could calculate the dot product of two arrays using:" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": null, 220 | "metadata": {}, 221 | "outputs": [], 222 | "source": [ 223 | "# Here we use the numpy.array constructor to build arrays with pre-specified values.\n", 224 | "array_1 = numpy.array([[1, 0], [0, 1]])\n", 225 | "array_2 = numpy.array([[4, 1], [2, 2]])\n", 226 | "\n", 227 | "numpy.dot(array_1, array_2)" 228 | ] 229 | }, 230 | { 231 | "cell_type": "markdown", 232 | "metadata": {}, 233 | "source": [ 234 | "NumPy also includes several functions to analyse/manipulate the data in NumPy arrays.\n", 235 | "\n", 236 | "For example, we can histogram a 1D array in the following manner:" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": null, 242 | "metadata": {}, 243 | "outputs": [], 244 | "source": [ 245 | "array_1D = numpy.array([1,2,1])\n", 246 | "\n", 247 | "# We histogram the above 1 dimensional array with bins edges 0, 1, 2, and 3\n", 248 | "# This means that we expect the histogram to return:\n", 249 | "# * 0 in the 0->1 bin\n", 250 | "# * 2 in the 1->2 bin\n", 251 | "# * 1 in the 2->3 bin\n", 252 | "\n", 253 | "histogram, bins = numpy.histogram(array_1D, bins=[0, 1, 2, 3])\n", 254 | "\n", 255 | "print(histogram)" 256 | ] 257 | }, 258 | { 259 | "cell_type": "markdown", 260 | "metadata": {}, 261 | "source": [ 262 | "We could be spending hours talking about arrays and all the functionality of NumPy so this is where we will leave this part. If you are interested in learning more about NumPy, we recommend this very good tutorial from datacamp:\n", 263 | "\n", 264 | "https://www.datacamp.com/community/tutorials/python-numpy-tutorial\n" 265 | ] 266 | }, 267 | { 268 | "cell_type": "markdown", 269 | "metadata": {}, 270 | "source": [ 271 | "## Review\n", 272 | "\n", 273 | "In this section we covered the following:\n", 274 | "- The basic concept of the NumPy library.\n", 275 | "- How to create basic NumPy arrays.\n", 276 | "- The basics of NumPy array manipulation.\n", 277 | "- How NumPy functions can be used to analyse NumPy arrays." 278 | ] 279 | } 280 | ], 281 | "metadata": { 282 | "kernelspec": { 283 | "display_name": "Python 3 (ipykernel)", 284 | "language": "python", 285 | "name": "python3" 286 | }, 287 | "language_info": { 288 | "codemirror_mode": { 289 | "name": "ipython", 290 | "version": 3 291 | }, 292 | "file_extension": ".py", 293 | "mimetype": "text/x-python", 294 | "name": "python", 295 | "nbconvert_exporter": "python", 296 | "pygments_lexer": "ipython3", 297 | "version": "3.9.15" 298 | } 299 | }, 300 | "nbformat": 4, 301 | "nbformat_minor": 1 302 | } 303 | -------------------------------------------------------------------------------- /tutorials/Python/10_Numpy/no_solutions/Numpy.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 10. Numpy" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## 10.1 A short introduction to NumPy\n", 15 | "\n", 16 | "NumPy stands for \"Numerical Python\" and, along with some other libraries (SciPy, Pandas, etc.), is the core library used for scientific computing. It contains a big number of tools and functions that can be used to solve an array of common and not so common problems, some examples of which you'll see below. However, most importantly, NumPy contains the all powerful NumPy arrays!\n", 17 | "\n", 18 | "For an example of how NumPy can be used in scientific computing, see the following code for calculating the Euclidean distance between two points:" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "import numpy\n", 28 | "print(numpy.linalg.norm(numpy.array([1,2,3]) - numpy.array([1,2,3])))\n", 29 | "print(numpy.linalg.norm(numpy.array([1,2,3]) - numpy.array([3,2,1])))" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "Many scientific computing libraries, e.g. MDAnalysis (see section 11), rely on NumPy to hold and operate on the data passed to them.\n", 37 | "\n", 38 | "In this section, we will very briefly introduce the NumPy library, this will make work in Section 11 easier to understand." 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "## 10.2 The NumPy array" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "A numpy array is a bit like an improved version of a Python list,. It \"is a high-performance multidimensional array object that is a powerful data structure for efficient computation of arrays and matrices. To work with these arrays, there’s a huge amount of high-level mathematical functions which operate on these matrices and arrays.\" " 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "In other words, an array is a good structure to store data from 1D, 2D, 3D, or higher dimensional matrices." 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "This means that arrays can have rows and columns. In a 2D array, rows can also be called as the \"axis 0\" while columns are the \"axis 1\". The number of axis will go up according to the dimentions of the arrays so a 3D array would also have an \"axis 2\". These axes are useful when it comes to manipulating the data in your arrays. \n", 67 | "\n", 68 | "We will see some hands on examples of arrays below." 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "First, let us build a 2D array.\n", 76 | "\n", 77 | "NumPy has an `numpy.empty` object which will allow you to build any sized \"empty\" NumPy array. However, when doing this the array will be returned with array elements set to any random value. Instead, we often want to initialise our array to a set value to begin with.\n", 78 | "\n", 79 | "There are two main methods for doing this in NumPy, `ones` and `zeros`. These create NumPy arrays filled with 0 and 1 respectively.\n", 80 | "\n", 81 | "Here we will build an array filled with the value 1, using `numpy.ones`. We are passing the argument (5,3) which dictates the shape of the array. The first value details the size of axis 0 (5), and the second value the size of axis 1 (3)." 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": null, 87 | "metadata": {}, 88 | "outputs": [], 89 | "source": [ 90 | "# An example of a 2D array\n", 91 | "my_2d_array = numpy.ones((5, 3))\n", 92 | "\n", 93 | "print(my_2d_array)" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "NumPy arrays have many built-in methods, for example, the `shape` method will return the length of the array along each axis." 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": null, 106 | "metadata": {}, 107 | "outputs": [], 108 | "source": [ 109 | "# Print out the shape of `my_array`\n", 110 | "print(my_2d_array.shape)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "NumPy arrays also have special data types (named dtypes) which describe the values held within the array.\n", 118 | "\n", 119 | "Examples include; `numpy.int64`, `numpy.float64`, and `numpy.complex64`.\n", 120 | "\n", 121 | "The type can be set when building the array using the `dtype` argument to methods such as; `numpy.ones`, `numpy.zeros`, and `numpy.empty`.\n", 122 | "\n", 123 | "Each array will have a `dtype` attribute which will tell you what the array assumes the underlying data to be. In this case, we see that the `dtype` of `my_2d_array` is `numpy.float64` which is the default data type of `numpy.ones`:" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": null, 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [ 132 | "# Print out the data type of `my_array`\n", 133 | "# Similar to the type() function\n", 134 | "print(my_2d_array.dtype)" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": {}, 140 | "source": [ 141 | "Similarly, we could use the same idea to build a 3D array of shape (2,5,3), but this time filled with integers `dtype=numpy.int64`:" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": null, 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [ 150 | "# This is an example of a 3D array\n", 151 | "my_3d_array = numpy.ones((2, 5, 3), dtype=numpy.int64)\n", 152 | "\n", 153 | "print(my_3d_array)\n", 154 | "\n", 155 | "# Print out the shape of `my_array`\n", 156 | "print(my_3d_array.shape)\n", 157 | "\n", 158 | "# Print out the data type of `my_array`\n", 159 | "print(my_3d_array.dtype)" 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "metadata": {}, 165 | "source": [ 166 | "**Note:** The number in the `numpy.int64` `dtype` declaration stands for the size of the data type in bits. In this case, we are asking for a 64 bit integer, which means that it can hold any integer numbers that range from `-9223372036854775808` to `9223372036854775808`." 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": {}, 172 | "source": [ 173 | "## 10.3 Manipulating NumPy arrays\n", 174 | "\n", 175 | "You can also add, subtract, multiply or divide your arrays." 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": null, 181 | "metadata": {}, 182 | "outputs": [], 183 | "source": [ 184 | "my_new_array = my_2d_array + 1\n", 185 | "\n", 186 | "print(my_new_array)" 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": null, 192 | "metadata": {}, 193 | "outputs": [], 194 | "source": [ 195 | "my_new_array = my_2d_array + my_3d_array\n", 196 | "\n", 197 | "print(my_new_array)" 198 | ] 199 | }, 200 | { 201 | "cell_type": "markdown", 202 | "metadata": {}, 203 | "source": [ 204 | "In order to perform arithmetic, and other, operations on two, or more arrays, there are certain criteria that need to be fulfilled.\n", 205 | "\n", 206 | "Firstly, their dimensions need to be compatible. This is the case when they are equal.\n", 207 | "\n", 208 | "Secondly, two dimensions are compatible when one of them is 1.\n", 209 | "\n", 210 | "Thirdly, the arrays need to be compatible in all directions.\n", 211 | "\n", 212 | "Although standard use of `-`, `+`, `/` and `*` operators work, NumPy also provides a series of mathematical functions to achieve several types of array manipulations.\n", 213 | "\n", 214 | "For example one could calculate the dot product of two arrays using:" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": null, 220 | "metadata": {}, 221 | "outputs": [], 222 | "source": [ 223 | "# Here we use the numpy.array constructor to build arrays with pre-specified values.\n", 224 | "array_1 = numpy.array([[1, 0], [0, 1]])\n", 225 | "array_2 = numpy.array([[4, 1], [2, 2]])\n", 226 | "\n", 227 | "numpy.dot(array_1, array_2)" 228 | ] 229 | }, 230 | { 231 | "cell_type": "markdown", 232 | "metadata": {}, 233 | "source": [ 234 | "NumPy also includes several functions to analyse/manipulate the data in NumPy arrays.\n", 235 | "\n", 236 | "For example, we can histogram a 1D array in the following manner:" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": null, 242 | "metadata": {}, 243 | "outputs": [], 244 | "source": [ 245 | "array_1D = numpy.array([1,2,1])\n", 246 | "\n", 247 | "# We histogram the above 1 dimensional array with bins edges 0, 1, 2, and 3\n", 248 | "# This means that we expect the histogram to return:\n", 249 | "# * 0 in the 0->1 bin\n", 250 | "# * 2 in the 1->2 bin\n", 251 | "# * 1 in the 2->3 bin\n", 252 | "\n", 253 | "histogram, bins = numpy.histogram(array_1D, bins=[0, 1, 2, 3])\n", 254 | "\n", 255 | "print(histogram)" 256 | ] 257 | }, 258 | { 259 | "cell_type": "markdown", 260 | "metadata": {}, 261 | "source": [ 262 | "We could be spending hours talking about arrays and all the functionality of NumPy so this is where we will leave this part. If you are interested in learning more about NumPy, we recommend this very good tutorial from datacamp:\n", 263 | "\n", 264 | "https://www.datacamp.com/community/tutorials/python-numpy-tutorial\n" 265 | ] 266 | }, 267 | { 268 | "cell_type": "markdown", 269 | "metadata": {}, 270 | "source": [ 271 | "## Review\n", 272 | "\n", 273 | "In this section we covered the following:\n", 274 | "- The basic concept of the NumPy library.\n", 275 | "- How to create basic NumPy arrays.\n", 276 | "- The basics of NumPy array manipulation.\n", 277 | "- How NumPy functions can be used to analyse NumPy arrays." 278 | ] 279 | } 280 | ], 281 | "metadata": { 282 | "kernelspec": { 283 | "display_name": "Python 3 (ipykernel)", 284 | "language": "python", 285 | "name": "python3" 286 | }, 287 | "language_info": { 288 | "codemirror_mode": { 289 | "name": "ipython", 290 | "version": 3 291 | }, 292 | "file_extension": ".py", 293 | "mimetype": "text/x-python", 294 | "name": "python", 295 | "nbconvert_exporter": "python", 296 | "pygments_lexer": "ipython3", 297 | "version": "3.9.15" 298 | } 299 | }, 300 | "nbformat": 4, 301 | "nbformat_minor": 1 302 | } 303 | -------------------------------------------------------------------------------- /tutorials/Python/11_OOP/Molecule.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/tutorials/Python/11_OOP/Molecule.png -------------------------------------------------------------------------------- /tutorials/Python/11_OOP/SimpleMolecule.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/tutorials/Python/11_OOP/SimpleMolecule.png -------------------------------------------------------------------------------- /tutorials/Python/11_OOP/acetylene.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/tutorials/Python/11_OOP/acetylene.png -------------------------------------------------------------------------------- /tutorials/Python/11_OOP/no_solutions/Molecule.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/tutorials/Python/11_OOP/no_solutions/Molecule.png -------------------------------------------------------------------------------- /tutorials/Python/11_OOP/no_solutions/SimpleMolecule.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/tutorials/Python/11_OOP/no_solutions/SimpleMolecule.png -------------------------------------------------------------------------------- /tutorials/Python/11_OOP/no_solutions/acetylene.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/tutorials/Python/11_OOP/no_solutions/acetylene.png -------------------------------------------------------------------------------- /tutorials/Python/CONTRIBUTORS.md: -------------------------------------------------------------------------------- 1 | # List of contributors to the Oxford Computational Biochemistry python course 2 | 3 | Authors are added at each year of contribution and sorted by last name alphabetical order. 4 | 5 | ## 2022 6 | - Irfan Alibay 7 | 8 | ## 2020 9 | - Irfan Alibay 10 | - Rocco Meli 11 | 12 | ## 2019 13 | 14 | - Irfan Alibay 15 | - Rocco Meli 16 | 17 | ## 2018 18 | 19 | - Irfan Alibay 20 | - Marc Daemgen 21 | - William Glass 22 | 23 | ## 2017 24 | 25 | * Marc Daemgen 26 | * Anna Duncan 27 | * Nicholas Michelarakis 28 | * Fiona Naughton 29 | * Naushad Velgy 30 | 31 | -------------------------------------------------------------------------------- /tutorials/Python/README.md: -------------------------------------------------------------------------------- 1 | # Oxford Computational Biochemistry Python workshop 2 | 3 | This repository contains the teaching materials for the Python workshop from the Oxford Computational Biochemistry course. 4 | 5 | 6 | ## Getting started 7 | 8 | In order to participate in this workshop you can either [setup a local Python environment](setup.md), or run the notebooks directly through Binder or Google colab (see badges below). 9 | 10 | 11 | ## The Python workshop 12 | 13 | 14 | Click here to launch the workshop via binder: [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/IAlibay/oxcompbio-python/main?urlpath=/tree/) 15 | 16 | This consists of a set of jupyter notebooks which aim to teach the basics of python programming assuming no prior knowledge. 17 | First introducing core concepts such as variables, loops, conditionals and lists, we eventually demonstrate how molecular structures can be analysed using python libraries such as [MDAnalysis](https://www.mdanalysis.org/) and [NGLView](http://nglviewer.org/nglview/latest/). 18 | 19 | ### Summary of notebooks 20 | 21 | | Notebook | Colab link | 22 | |-------------------------------------------------|------------| 23 | | 0. Introduction | [![notebook 1](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/00_Introduction/no_solutions/Introduction.ipynb) 24 | | 1. Basic Types | [![notebook 2](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/01_BasicTypes/no_solutions/BasicTypes.ipynb) 25 | | 2. Variables and Comparisons | [![notebook 3](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/02_Variables/no_solutions/Variables_and_Comparisons.ipynb) 26 | | 3. Loops | [![notebook 4](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/03_Loops/no_solutions/Loops.ipynb) 27 | | 4. Conditionals | [![notebook 5](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/04_Conditionals/no_solutions/Conditionals.ipynb) 28 | | 5. Lists | [![notebook 6](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/05_Lists/no_solutions/Lists.ipynb) 29 | | 6. File Handling | [![notebook 7](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/06_FileHandling/no_solutions/FileHandling.ipynb) 30 | | 7. Functions | [![notebook 8](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/07_Functions/no_solutions/Functions.ipynb) 31 | | 8. Documentation | [![notebook 9](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/08_Documentation/no_solutions/Documentation.ipynb) 32 | | 9. Libraries | [![notebook 10](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/09_Libraries/no_solutions/Libraries.ipynb) 33 | | 10. NumPy | [![notebook 11](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/10_Numpy/no_solutions/Numpy.ipynb) 34 | | 11. Object Orientated Programming | [![notebook 12](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/11_OOP/no_solutions/OOP.ipynb) 35 | | 12. Protein Analysis | [![notebook 13](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/12_ProteinAnalysis/no_solutions/ProteinAnalysis.ipynb) 36 | | 13. Simulation and Analysis | [![notebook 14](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IAlibay/oxcompbio-python/blob/main/tutorial/13_Simulation_and_Analysis/no_solutions/Simulation_and_Analysis.ipynb) 37 | 38 | 39 | ## Acknowledgements 40 | 41 | The Oxford Computational Biochemistry Python workshop has been written by several authors over several years. 42 | Please see the [contributors list](CONTRIBUTORS.md) for more details. 43 | 44 | ## License 45 | 46 | The notebooks are licensed under a [CC BY-NC 4.0 SA](https://creativecommons.org/licenses/by-nc/4.0/) license (see below). 47 | -------------------------------------------------------------------------------- /tutorials/Python/cleanup.sh: -------------------------------------------------------------------------------- 1 | python -m nbconvert --ClearOutputPreprocessor.enabled=True --inplace *.ipynb **/*.ipynb -------------------------------------------------------------------------------- /tutorials/Python/images/jupyter-run-all.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/tutorials/Python/images/jupyter-run-all.png -------------------------------------------------------------------------------- /tutorials/Python/images/verify-location.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/tutorials/Python/images/verify-location.png -------------------------------------------------------------------------------- /tutorials/Python/images/verify-outputs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bigginlab/OxCompBio/e760380caa14595a10f90cd33f7b2bdb55e611b4/tutorials/Python/images/verify-outputs.png -------------------------------------------------------------------------------- /tutorials/Python/setup-conda.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Create conda environment 4 | conda create -y -n OxPython -c conda-forge mamba python=3.9 5 | 6 | source activate OxPython 7 | 8 | mamba install -y -c jaimergp/label/unsupported-cudatoolkit-shim -c conda-forge notebook jupyter_contrib_nbextensions numpy matplotlib "MDAnalysis>=2.0.0" nglview openmm parmed "ipywidgets<8" 9 | 10 | jupyter-nbextension enable nglview --py --sys-prefix 11 | 12 | # Enable Exercise 2 extension 13 | # https://jupyter-contrib-nbextensions.readthedocs.io/en/latest/nbextensions/exercise2/readme.html 14 | jupyter contrib nbextension install --user 15 | jupyter nbextension enable exercise2/main 16 | 17 | echo "setup done" 18 | -------------------------------------------------------------------------------- /tutorials/Python/setup.md: -------------------------------------------------------------------------------- 1 | # Setup instructions for the Oxford Computational Biochemistry Python workshop 2 | 3 | ## Contents 4 | 5 | This workshop is delivered through a series of [Jupyter notebooks](https://jupyter-notebook.readthedocs.io/en/stable/) which allow for interactive Python programming. 6 | 7 | To run these the following instructions should be followed: 8 | 9 | ### 1. Download the workshop materials 10 | 11 | In a terminal, close the workshop repository: 12 | 13 | ```bash 14 | git clone https://github.com/IAlibay/oxcompbio-python.git 15 | ``` 16 | 17 | ### 2. Create the conda environment 18 | 19 | First navigate within your shell to inside the repository 20 | 21 | ```bash 22 | cd oxcompbio-python 23 | ``` 24 | 25 | Then create the conda environment 26 | 27 | ```bash 28 | conda env create --file=environment.yml 29 | ``` 30 | 31 | Once it's finished, activate the environment 32 | 33 | ```bash 34 | conda activate OxPython 35 | ``` 36 | 37 | ### 3. Activate Jupyter extensions 38 | 39 | The workshop leverages the extended utility of several Jupyter nbextensions. 40 | 41 | To install these, the followed should be run once (after having activated the conda environment): 42 | 43 | ```bash 44 | jupyter contrib nbextension install --user 45 | jupyter nbextension enable splitcell/splitcell 46 | jupyter nbextension enable rubberband/main 47 | jupyter nbextension enable exercise2/main 48 | jupyter nbextension enable autosavetime/main 49 | jupyter nbextension enable collapsible_headings/main 50 | jupyter nbextension enable codefolding/main 51 | jupyter nbextension enable limit_output/main 52 | jupyter nbextension enable toc2/main 53 | ``` 54 | 55 | ### 4. Start a jupyter notebook seession 56 | 57 | Finally start a jupyter notebook session by typing the following in your shell. 58 | 59 | ```bash 60 | jupyter notebook 61 | ``` 62 | 63 | --------------------------------------------------------------------------------