├── .bumpversion.cfg ├── .gitattributes ├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── tool-request.md └── workflows │ ├── build_and_test.yml │ ├── bump-version-on-push.yml │ └── yaml_linter.yml ├── .gitignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── MANIFEST.in ├── PACKAGING.md ├── README.md ├── citation.tex ├── docs ├── _config.yml ├── assets │ └── css │ │ └── style.scss ├── cookbook.md ├── doctools.py ├── index.md ├── index.md.template └── logo │ └── pdb-tools.png ├── pdbtools ├── __init__.py ├── pdb_b.py ├── pdb_chain.py ├── pdb_chainbows.py ├── pdb_chainxseg.py ├── pdb_chkensemble.py ├── pdb_delchain.py ├── pdb_delelem.py ├── pdb_delhetatm.py ├── pdb_delinsertion.py ├── pdb_delres.py ├── pdb_delresname.py ├── pdb_element.py ├── pdb_fetch.py ├── pdb_fixinsert.py ├── pdb_fromcif.py ├── pdb_gap.py ├── pdb_head.py ├── pdb_intersect.py ├── pdb_keepcoord.py ├── pdb_merge.py ├── pdb_mkensemble.py ├── pdb_occ.py ├── pdb_reatom.py ├── pdb_reres.py ├── pdb_rplchain.py ├── pdb_rplresname.py ├── pdb_seg.py ├── pdb_segxchain.py ├── pdb_selaltloc.py ├── pdb_selatom.py ├── pdb_selchain.py ├── pdb_selelem.py ├── pdb_selhetatm.py ├── pdb_selmodel.py ├── pdb_selres.py ├── pdb_selresname.py ├── pdb_selseg.py ├── pdb_shiftres.py ├── pdb_sort.py ├── pdb_splitchain.py ├── pdb_splitmodel.py ├── pdb_splitseg.py ├── pdb_tidy.py ├── pdb_tocif.py ├── pdb_tofasta.py ├── pdb_uniqname.py ├── pdb_validate.py └── pdb_wc.py ├── setup.cfg ├── setup.py └── tests ├── README.md ├── __init__.py ├── config.py ├── data ├── anisou.pdb ├── anisou_altloc.pdb ├── anisou_missing.pdb ├── dummy.pdb ├── dummy_altloc.pdb ├── dummy_altloc2.pdb ├── dummy_altloc3.pdb ├── dummy_az09.pdb ├── dummy_insertions.pdb ├── dummy_nohead.pdb ├── ensemble_OK.cif ├── ensemble_OK.pdb ├── ensemble_error_1.pdb ├── ensemble_error_2.pdb ├── ensemble_error_3.pdb ├── ensemble_error_4.pdb ├── ensemble_error_MODEL.pdb ├── ensemble_more_OK.pdb ├── hetatm.pdb ├── hetatm_bad.pdb ├── hetatm_ensemble.pdb └── vu7.pdb ├── test_pdb_b.py ├── test_pdb_chain.py ├── test_pdb_chainbows.py ├── test_pdb_chainxseg.py ├── test_pdb_chkensemble.py ├── test_pdb_delchain.py ├── test_pdb_delelem.py ├── test_pdb_delhetatm.py ├── test_pdb_delinsertion.py ├── test_pdb_delres.py ├── test_pdb_delresname.py ├── test_pdb_element.py ├── test_pdb_fixinsert.py ├── test_pdb_fromcif.py ├── test_pdb_gap.py ├── test_pdb_head.py ├── test_pdb_intersect.py ├── test_pdb_keepcoord.py ├── test_pdb_merge.py ├── test_pdb_mkensemble.py ├── test_pdb_occ.py ├── test_pdb_reatom.py ├── test_pdb_reres.py ├── test_pdb_rplchain.py ├── test_pdb_rplresname.py ├── test_pdb_seg.py ├── test_pdb_segxchain.py ├── test_pdb_selaltloc.py ├── test_pdb_selatom.py ├── test_pdb_selchain.py ├── test_pdb_selelem.py ├── test_pdb_selhetatm.py ├── test_pdb_selmodel.py ├── test_pdb_selres.py ├── test_pdb_selresname.py ├── test_pdb_selseg.py ├── test_pdb_shiftres.py ├── test_pdb_sort.py ├── test_pdb_splitchain.py ├── test_pdb_splitmodel.py ├── test_pdb_splitseg.py ├── test_pdb_tidy.py ├── test_pdb_tocif.py ├── test_pdb_tofasta.py ├── test_pdb_uniqname.py ├── test_pdb_validate.py ├── test_pdb_wc.py └── utils.py /.bumpversion.cfg: -------------------------------------------------------------------------------- 1 | [bumpversion] 2 | current_version = 2.5.1 3 | commit = True 4 | message = [SKIP] version bump {current_version} -> {new_version} 5 | tag = True 6 | 7 | [bumpversion:file:setup.py] 8 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Set LF for Python files 2 | *py text eol=lf 3 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. Ex. My PDB file is blank after I use this tool! 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior. Preferrably, a code snippet. 15 | ``` 16 | pdb_fetch.py 1xxx.pdb | pdb_oopsy.py > result 17 | cat result # result is empty! 18 | ``` 19 | 20 | **Expected behavior** 21 | The output PDB file should not be blank, but should have YY replaced! 22 | 23 | 24 | **Desktop (please complete the following information):** 25 | - OS: [e.g. Mac OS, Linux, Windows] 26 | - Python Version [e.g. 2.7, 3.6, 3.7] 27 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/tool-request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Tool request 3 | about: Suggest an idea for a new pdb-tool 4 | title: '' 5 | labels: Tool Request 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your tool request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always trying to do [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. Ex. I would like a tool that did [...] 15 | 16 | **Does any of the existing tools or a combination of them do what you want?** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Remember** 20 | Each `pdb-tools` should do one and one job only. If you have to describe what you want using an _and_, probably it is the job for at least _two_ tools. We want simple tools that can be combined, for flexibility. 21 | -------------------------------------------------------------------------------- /.github/workflows/build_and_test.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: build_and_test 3 | 4 | on: # yamllint disable-line rule:truthy 5 | pull_request: 6 | branches: 7 | - master 8 | push: 9 | branches: 10 | - master 11 | 12 | jobs: 13 | # Only lint in one platform to save compute time 14 | linter: 15 | runs-on: ${{ matrix.platform }} 16 | strategy: 17 | matrix: 18 | platform: [ubuntu-latest] 19 | python-version: ["3.10"] 20 | 21 | steps: 22 | - uses: actions/checkout@v2 23 | 24 | - name: Set up Python ${{ matrix.python-version }} 25 | uses: actions/setup-python@v2 26 | with: 27 | python-version: ${{ matrix.python-version }} 28 | 29 | - name: Install dependencies 30 | run: | 31 | python -m pip install --upgrade pip setuptools wheel 32 | shell: bash 33 | 34 | - name: Linter 35 | run: | 36 | python -m pip install flake8 37 | flake8 . 38 | 39 | build_and_test: 40 | name: Build & Test 41 | runs-on: ${{ matrix.platform }} 42 | needs: [linter] 43 | strategy: 44 | matrix: 45 | platform: [ubuntu-latest, macos-latest, windows-latest] 46 | python-version: [3.7, 3.8, 3.9, "3.10"] 47 | 48 | steps: 49 | - uses: actions/checkout@v2 50 | 51 | - name: Set up Python ${{ matrix.python-version }} 52 | uses: actions/setup-python@v2 53 | with: 54 | python-version: ${{ matrix.python-version }} 55 | 56 | - name: Install dependencies 57 | run: | 58 | python -m pip install --upgrade pip setuptools wheel 59 | python -m pip install --upgrade bump2version twine 60 | python -m pip install --upgrade coverage 61 | 62 | - name: Build 63 | run: | 64 | python --version 65 | python setup.py sdist bdist_wheel 66 | twine check dist/*.whl 67 | twine check dist/*.tar.gz 68 | bump2version --dry-run --verbose --allow-dirty patch 69 | bump2version --dry-run --verbose --allow-dirty minor 70 | bump2version --dry-run --verbose --allow-dirty major 71 | 72 | - name: Install pdb-tools 73 | run: | 74 | python -m pip install . 75 | 76 | - name: Test on Linux 77 | run: | 78 | script -q -e -c "coverage run -p setup.py test" 79 | shell: bash 80 | if: matrix.os == 'ubuntu-latest' 81 | 82 | - name: Test on MacOS / Windows 83 | run: | 84 | coverage run -p setup.py test 85 | shell: bash 86 | if: matrix.os != 'ubuntu-latest' 87 | env: 88 | SKIP_TTY_TESTS: true 89 | 90 | # Store coverage data for later 91 | # https://hynek.me/articles/ditch-codecov-python/ 92 | - name: Store coverage data 93 | uses: actions/upload-artifact@v3 94 | with: 95 | name: coverage-data 96 | path: .coverage.* 97 | if-no-files-found: ignore 98 | retention-days: 2 99 | 100 | # Combine and analyze coverage 101 | # https://hynek.me/articles/ditch-codecov-python/ 102 | coverage: 103 | name: Combine & Check coverage. 104 | runs-on: ubuntu-latest 105 | needs: [build_and_test] 106 | steps: 107 | - uses: actions/checkout@v2 108 | - uses: actions/setup-python@v2 109 | with: 110 | python-version: "3.10" 111 | 112 | - run: | 113 | python -m pip install --upgrade pip setuptools wheel 114 | python -m pip install --upgrade coverage 115 | 116 | - name: Download coverage data. 117 | uses: actions/download-artifact@v2 118 | with: 119 | name: coverage-data 120 | 121 | # We should update the threshold as we write more tests. 122 | # It should be the minimum we are comfortable with. 123 | - name: Combine coverage & fail if it's < threshold %. 124 | run: | 125 | python -m coverage combine 126 | python -m coverage html --skip-covered --skip-empty 127 | python -m coverage report --fail-under=80 128 | 129 | - name: Upload HTML report if check failed. 130 | uses: actions/upload-artifact@v2 131 | with: 132 | name: html-report 133 | path: htmlcov 134 | if: ${{ failure() }} 135 | -------------------------------------------------------------------------------- /.github/workflows/bump-version-on-push.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # yamllint disable rule:line-length 3 | name: Bump_and_Package 4 | 5 | on: # yamllint disable-line rule:truthy 6 | push: 7 | branches: 8 | - master 9 | 10 | jobs: 11 | bump-version: 12 | 13 | runs-on: ubuntu-latest 14 | if: "!startsWith(github.event.head_commit.message, '[SKIP]')" 15 | 16 | steps: 17 | 18 | - uses: actions/checkout@v1 19 | 20 | - name: Set up Python 21 | uses: actions/setup-python@v1 22 | with: 23 | python-version: '3.x' 24 | 25 | - name: Setup Git 26 | run: | 27 | git config user.name "JoaoRodrigues" 28 | git config user.email 'joaorodrigues@users.noreply.github.com' 29 | git remote set-url origin \https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY 30 | git checkout "${GITHUB_REF:11}" 31 | 32 | - name: Create skip flag 33 | run: | 34 | echo "SKIPBUMP=FALSE" >> $GITHUB_ENV 35 | 36 | - name: Install dependencies 37 | run: | 38 | python -m pip install --upgrade pip 39 | pip install bump2version setuptools wheel twine 40 | 41 | - name: Bump Minor Version 42 | run: | 43 | bump2version minor 44 | echo "SKIPBUMP=TRUE" >> $GITHUB_ENV 45 | if: "startsWith(github.event.head_commit.message, '[FEATURE]')" 46 | 47 | # Default action 48 | - name: Bump Patch Version 49 | run: | 50 | bump2version patch 51 | if: env.SKIPBUMP == 'FALSE' 52 | 53 | # No major version change should go through automatically. 54 | 55 | - name: Commit version change to master 56 | run: | 57 | git push --follow-tags 58 | 59 | - name: Build and publish 60 | env: 61 | TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} 62 | TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} 63 | run: | 64 | python setup.py sdist bdist_wheel 65 | twine upload dist/* 66 | -------------------------------------------------------------------------------- /.github/workflows/yaml_linter.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Yaml Lint 3 | on: [push, pull_request] # yamllint disable-line rule:truthy 4 | jobs: 5 | lintAllTheThings: 6 | runs-on: ubuntu-latest 7 | steps: 8 | - uses: actions/checkout@v2 9 | - name: yaml-lint 10 | uses: ibiqlik/action-yamllint@v3 11 | with: 12 | file_or_dir: .github/workflows/*.yml 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Python setuptools 2 | build/ 3 | dist/ 4 | *egg-info/ 5 | *__pycache__ 6 | *.pyc 7 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at j.p.g.l.m.rodrigues@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | graft pdbtools 2 | 3 | include LICENSE 4 | include README.md 5 | include citation.tex 6 | 7 | exclude .appveyor.yml 8 | exclude .bumpversion.cfg 9 | exclude .coveragerc 10 | exclude .travis.yml 11 | exclude CODE_OF_CONDUCT.md 12 | exclude CONTRIBUTING.md 13 | exclude MANIFEST.in 14 | exclude PACKING.md 15 | exclude tox.ini 16 | 17 | prune docs 18 | prune tests 19 | 20 | global-exclude *.py[cod] __pycache__/* *.so *.dylib 21 | global-exclude *.swp 22 | global-exclude tags 23 | -------------------------------------------------------------------------------- /PACKAGING.md: -------------------------------------------------------------------------------- 1 | # Packaging pdb-tools 2 | This guide describes (roughly) how to package `pdb-tools` into something `pip` 3 | can handle. 4 | 5 | ## Versioning 6 | `pdb-tools` uses semantic versioning: MAJOR.MINOR.PATCH (e.g. 2.0.0). It works 7 | more or less like this: 8 | 9 | * For every new release fixing bugs, increment the PATCH counter (e.g. 2.0.1). 10 | * For every release adding minor features to tools or new tools that do not 11 | impact any of the existing ones, increment MINOR (e.g. 2.1.0). 12 | * For major changes that will most likely impact the usage of existing tools, 13 | increment MAJOR (e.g. 3.0.0). 14 | 15 | In practice, when packaging the distribution we should update the version number 16 | in `setup.py` accordingly (otherwise `twine` will give an error). 17 | 18 | ## Packaging 19 | This is a rough guide to building a distributable package: 20 | 21 | ### (1) Ensure all tests pass: 22 | ```bash 23 | python setup.py test 24 | ``` 25 | 26 | 2. Ensure there are no warnings from flake8: 27 | ```bash 28 | flake8 --ignore=E501,E731 29 | ``` 30 | 31 | 3. Build the distributable package: 32 | ```bash 33 | python setup.py sdist bdist_wheel 34 | 35 | # Upload to PyPI: testing repo for now. 36 | #twine upload --repository-url https://test.pypi.org/legacy/ dist/* 37 | twine upload dist/* 38 | ``` 39 | -------------------------------------------------------------------------------- /citation.tex: -------------------------------------------------------------------------------- 1 | @article {Rodrigues483305, 2 | author = {Rodrigues, Jo{\~a}o P.G.L.M. and Teixeira, Jo{\~a}o M.C. and Trellet, Mika{\"e}l and Bonvin, Alexandre M.J.J.}, 3 | title = {pdb-tools: a swiss army knife for molecular structures}, 4 | elocation-id = {483305}, 5 | year = {2018}, 6 | doi = {10.1101/483305}, 7 | publisher = {Cold Spring Harbor Laboratory}, 8 | abstract = {The pdb-tools are a collection of Python scripts for working with molecular structure data in the PDB format. They allow users to edit, convert, and validate PDB files, from the command-line, in a simple but efficient manner. The pdb-tools are implemented in Python, without any external dependencies, and are freely available under the open-source Apache License at https://github.com/haddocking/pdb-tools/ and on PyPI (https://pypi.org/project/pdb-tools/).}, 9 | URL = {https://www.biorxiv.org/content/early/2018/12/04/483305}, 10 | eprint = {https://www.biorxiv.org/content/early/2018/12/04/483305.full.pdf}, 11 | journal = {bioRxiv} 12 | } -------------------------------------------------------------------------------- /docs/_config.yml: -------------------------------------------------------------------------------- 1 | title: 'pdb-tools' 2 | headline: '' 3 | 4 | logo: /logo/pdb-tools.png 5 | show_downloads: true 6 | description: 'A swiss army knife for editing PDB files.' 7 | theme: jekyll-theme-minimal 8 | github: 9 | zip_url: https://github.com/haddocking/pdb-tools/archive/2.0.0-rc1.zip 10 | tar_url: https://github.com/haddocking/pdb-tools/archive/2.0.0-rc1.tar.gz 11 | -------------------------------------------------------------------------------- /docs/assets/css/style.scss: -------------------------------------------------------------------------------- 1 | --- 2 | --- 3 | 4 | @import "{{ site.theme }}"; 5 | 6 | /* Pad image bottom */ 7 | .wrapper img { 8 | padding-bottom: 1%; 9 | } 10 | 11 | /* Remove bullets from ordered lists */ 12 | ul { 13 | list-style-type: none; 14 | padding: 0; 15 | margin: 0; 16 | } 17 | 18 | /* Make content wider */ 19 | .wrapper { 20 | width: 1200px; 21 | } 22 | 23 | section { 24 | width: 860px; 25 | } 26 | 27 | footer { 28 | width: 280px; 29 | } 30 | -------------------------------------------------------------------------------- /docs/cookbook.md: -------------------------------------------------------------------------------- 1 | --- 2 | layout: default 3 | --- 4 | 5 | * Removing hydrogens, renaming `HIP` to `HIS`, and renumbering atoms, for all PDBs from a folder to a new folder: 6 | 7 | ```bash 8 | mkdir folder_new 9 | for i in folder/*pdb; do pdb_delelem -H $i | pdb_rplresname -HIP:HIS | pdb_reatom -1 | pdb_tidy > folder_new/$(basename $i); done 10 | ``` 11 | -------------------------------------------------------------------------------- /docs/doctools.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Scrapes tool headers for documentation. 5 | """ 6 | 7 | import importlib 8 | from pathlib import Path 9 | 10 | with open('index.md', 'w') as handle: 11 | 12 | with open('index.md.template') as template: 13 | print(template.read(), file=handle) 14 | 15 | import pdbtools 16 | modfile = Path(pdbtools.__file__) 17 | for f in sorted(list(modfile.parent.iterdir())): 18 | # ignore __init__.py and others. 19 | if f.stem.startswith('_') or f.suffix != '.py': 20 | continue 21 | 22 | # Dynamically import tool to get __doc__ 23 | name = f.stem 24 | try: 25 | tool = importlib.import_module(f'pdbtools.{name}') 26 | except ModuleNotFoundError: 27 | print(f'Could not import module: {name}') 28 | continue 29 | 30 | # Parse documentation from docstrings 31 | # Preserve white-space as best as possible. 32 | # First non-empty line is always short description. 33 | # Last lines are always licensing disclaimer 34 | summary = None 35 | long_description = [] 36 | 37 | doctext = tool.__doc__.replace('<', '<').replace('>', '>') 38 | for line in doctext.split('\n'): 39 | if summary is None and not line.strip(): 40 | continue 41 | if line.startswith('This program is part of the'): 42 | break 43 | elif summary is None: 44 | summary = line 45 | else: 46 | long_description.append(line) 47 | 48 | long_description = '\n'.join(long_description) 49 | print('
', file=handle) 50 | print('
', file=handle) 51 | print(f"{name}

{summary}

", file=handle) 52 | print( 53 | f'{long_description}', 54 | file=handle 55 | ) 56 | print('
', file=handle) 57 | print('
', file=handle) 58 | -------------------------------------------------------------------------------- /docs/logo/pdb-tools.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haddocking/pdb-tools/e4368bcdbb4d862606f12f2456bf888db6ded4a0/docs/logo/pdb-tools.png -------------------------------------------------------------------------------- /pdbtools/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright 2018 João Pedro Rodrigues 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | """The pdb-tools library. 18 | 19 | A Swiss army knife for manipulating and editing PDB files. 20 | 21 | You can use pdb-tools as a library or as a series of convenient 22 | command-line applications. The complete documentation is available at: 23 | 24 | http://www.bonvinlab.org/pdb-tools/ 25 | 26 | Examples at the command-line 27 | ---------------------------- 28 | 29 | $ pdb_fetch 1brs > 1brs.pdb 30 | $ pdb_reres -1 1ctf.pdb > 1ctf_renumbered.pdb 31 | $ pdb_selchain -A,D 1brs.pdb | pdb_delhetatm | pdb_tidy > 1brs_AD_noHET.pdb 32 | 33 | 34 | Examples using pdb-tools as library 35 | ----------------------------------- 36 | 37 | You can import according to your needs: 38 | 39 | >>> import pdbtools 40 | >>> from pdbtools import * 41 | >>> from pdbtools import MODULE 42 | >>> from pdbtools import pdb_selchain 43 | 44 | Chain the different functionalities conveniently: 45 | 46 | >>> from pdbtools import pdb_selchain, pdb_selatom, pdb_keepcoord 47 | >>> with open('dummy.pdb') as fh: 48 | >>> chain_a = pdb_selchain.run(fh, ['A']) 49 | >>> only_N = pdb_selatom.run(chain_a, ['N']) 50 | >>> coords = pdb_keepcoord.run(only_N) 51 | >>> final = pdb_reres.run(coords, 5) 52 | >>> print(''.join(final)) 53 | 54 | The list of MODULEs is specified bellow. 55 | 56 | All packages have three functions: `check_input`, `main`, and `run`. 57 | The latter executes the logic of each package. `check_input` checks and 58 | prepares potential input parameters to feed `run`. Use `check_input` in 59 | case you are not sure the received input is correct. You can chain both 60 | functions: 61 | 62 | >>> MODULE.run(**MODULE.check_input(*args)) 63 | 64 | If you control the input parameters use `run` directly. In general, 65 | `run` functions are generators yielding the modified PDB data 66 | line-by-line. `main` is used solely in the context of the command-line 67 | interface. 68 | 69 | All MODULEs and `run` functions provide comprehensive documentation. 70 | 71 | >>> help(MODULE) 72 | >>> help(MODULE.run) 73 | """ 74 | 75 | 76 | __all__ = [ 77 | 'pdb_b', 78 | 'pdb_chainbows', 79 | 'pdb_chain', 80 | 'pdb_chainxseg', 81 | 'pdb_chkensemble', 82 | 'pdb_delchain', 83 | 'pdb_delelem', 84 | 'pdb_delhetatm', 85 | 'pdb_delinsertion', 86 | 'pdb_delresname', 87 | 'pdb_delres', 88 | 'pdb_element', 89 | 'pdb_fetch', 90 | 'pdb_fixinsert', 91 | 'pdb_fromcif', 92 | 'pdb_gap', 93 | 'pdb_head', 94 | 'pdb_intersect', 95 | 'pdb_keepcoord', 96 | 'pdb_merge', 97 | 'pdb_mkensemble', 98 | 'pdb_occ', 99 | 'pdb_reatom', 100 | 'pdb_reres', 101 | 'pdb_rplchain', 102 | 'pdb_rplresname', 103 | 'pdb_seg', 104 | 'pdb_segxchain', 105 | 'pdb_selaltloc', 106 | 'pdb_selatom', 107 | 'pdb_selchain', 108 | 'pdb_selelem', 109 | 'pdb_selhetatm', 110 | 'pdb_selresname', 111 | 'pdb_selres', 112 | 'pdb_selseg', 113 | 'pdb_shiftres', 114 | 'pdb_sort', 115 | 'pdb_splitchain', 116 | 'pdb_splitmodel', 117 | 'pdb_splitseg', 118 | 'pdb_tidy', 119 | 'pdb_tocif', 120 | 'pdb_tofasta', 121 | 'pdb_uniqname', 122 | 'pdb_validate', 123 | 'pdb_wc', 124 | ] 125 | -------------------------------------------------------------------------------- /pdbtools/pdb_chainbows.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright 2020 João Pedro Rodrigues 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | 18 | """ 19 | Renames chain identifiers sequentially, based on TER records. 20 | 21 | Since HETATM records are not separated by TER records and usually come together 22 | at the end of the PDB file, this script will attempt to reassign their chain 23 | identifiers based on the changes it made to ATOM lines. This might lead to bad 24 | output in certain corner cases. 25 | 26 | Usage: 27 | python pdb_chainbows.py 28 | 29 | Example: 30 | python pdb_chainbows.py 1CTF.pdb 31 | 32 | This program is part of the `pdb-tools` suite of utilities and should not be 33 | distributed isolatedly. The `pdb-tools` were created to quickly manipulate PDB 34 | files using the terminal, and can be used sequentially, with one tool streaming 35 | data to another. They are based on old FORTRAN77 code that was taking too much 36 | effort to maintain and compile. RIP. 37 | """ 38 | 39 | import os 40 | import string 41 | import sys 42 | 43 | __author__ = "Joao Rodrigues" 44 | __email__ = "j.p.g.l.m.rodrigues@gmail.com" 45 | 46 | 47 | def check_input(args): 48 | """Checks whether to read from stdin/file and validates user input/options. 49 | """ 50 | 51 | # Defaults 52 | fh = sys.stdin # file handle 53 | 54 | if not len(args): 55 | # Reading from pipe with default option 56 | if sys.stdin.isatty(): 57 | sys.stderr.write(__doc__) 58 | sys.exit(1) 59 | 60 | elif len(args) == 1: 61 | if not os.path.isfile(args[0]): 62 | emsg = 'ERROR!! File not found or not readable: \'{}\'\n' 63 | sys.stderr.write(emsg.format(args[0])) 64 | sys.stderr.write(__doc__) 65 | sys.exit(1) 66 | 67 | fh = open(args[0], 'r') 68 | 69 | else: # Whatever ... 70 | emsg = 'ERROR!! Script takes 1 argument, not \'{}\'\n' 71 | sys.stderr.write(emsg.format(len(args))) 72 | sys.stderr.write(__doc__) 73 | sys.exit(1) 74 | 75 | return fh 76 | 77 | 78 | def run(fhandle): 79 | """ 80 | Set chains sequentially based on existing TER records. 81 | 82 | Follow sequence [ABC...abc...012...]. 83 | 84 | This function is a generator. 85 | 86 | Parameters 87 | ---------- 88 | fhandle : an iterable giving the PDB file line-by-line 89 | 90 | Yields 91 | ------ 92 | str (line-by-line) 93 | The modified (or not) PDB line. 94 | """ 95 | chainlist = list( 96 | string.digits[::-1] + string.ascii_lowercase[::-1] + string.ascii_uppercase[::-1] 97 | ) # 987...zyx...cbaZYX...BCA. 98 | max_chains = len(chainlist) 99 | 100 | chain_map = {} # for HETATM. 101 | 102 | curchain = chainlist.pop() 103 | records = ('ATOM', 'TER', 'ANISOU') 104 | for line in fhandle: 105 | if line.startswith(records): 106 | chain_map[line[21]] = curchain 107 | line = line[:21] + curchain + line[22:] 108 | 109 | if line.startswith('TER'): 110 | try: 111 | curchain = chainlist.pop() 112 | except IndexError: 113 | emsg = 'ERROR!! Structure contains more than {} TER records.\n' 114 | sys.stderr.write(emsg.format(max_chains)) 115 | sys.stderr.write(__doc__) 116 | sys.exit(1) 117 | 118 | elif line.startswith('HETATM'): 119 | hetchain = chain_map[line[21]] 120 | line = line[:21] + hetchain + line[22:] 121 | 122 | yield line 123 | 124 | 125 | set_chain_sequence = run 126 | 127 | 128 | def main(): 129 | # Check Input 130 | pdbfh = check_input(sys.argv[1:]) 131 | 132 | # Do the job 133 | new_pdb = run(pdbfh) 134 | 135 | try: 136 | _buffer = [] 137 | _buffer_size = 5000 # write N lines at a time 138 | for lineno, line in enumerate(new_pdb): 139 | if not (lineno % _buffer_size): 140 | sys.stdout.write(''.join(_buffer)) 141 | _buffer = [] 142 | _buffer.append(line) 143 | 144 | sys.stdout.write(''.join(_buffer)) 145 | sys.stdout.flush() 146 | except IOError: 147 | # This is here to catch Broken Pipes 148 | # for example to use 'head' or 'tail' without 149 | # the error message showing up 150 | pass 151 | 152 | # last line of the script 153 | # We can close it even if it is sys.stdin 154 | pdbfh.close() 155 | sys.exit(0) 156 | 157 | 158 | if __name__ == '__main__': 159 | main() 160 | -------------------------------------------------------------------------------- /pdbtools/pdb_chainxseg.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright 2018 João Pedro Rodrigues 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | 18 | """ 19 | Swaps the segment identifier for the chain identifier. 20 | 21 | Usage: 22 | python pdb_chainxseg.py 23 | 24 | Example: 25 | python pdb_chainxseg.py 1CTF.pdb 26 | 27 | This program is part of the `pdb-tools` suite of utilities and should not be 28 | distributed isolatedly. The `pdb-tools` were created to quickly manipulate PDB 29 | files using the terminal, and can be used sequentially, with one tool streaming 30 | data to another. They are based on old FORTRAN77 code that was taking too much 31 | effort to maintain and compile. RIP. 32 | """ 33 | 34 | import os 35 | import sys 36 | 37 | __author__ = "Joao Rodrigues" 38 | __email__ = "j.p.g.l.m.rodrigues@gmail.com" 39 | 40 | 41 | def check_input(args): 42 | """Checks whether to read from stdin/file and validates user input/options. 43 | """ 44 | 45 | # Defaults 46 | fh = sys.stdin # file handle 47 | 48 | if not len(args): 49 | # Reading from pipe 50 | if sys.stdin.isatty(): 51 | sys.stderr.write(__doc__) 52 | sys.exit(1) 53 | 54 | elif len(args) == 1: 55 | # Reading from file 56 | if not os.path.isfile(args[0]): 57 | emsg = 'ERROR!! File not found or not readable: \'{}\'\n' 58 | sys.stderr.write(emsg.format(args[0])) 59 | sys.stderr.write(__doc__) 60 | sys.exit(1) 61 | 62 | fh = open(args[0], 'r') 63 | 64 | else: # Whatever ... 65 | emsg = 'ERROR!! Script takes 1 argument, not \'{}\'\n' 66 | sys.stderr.write(emsg.format(len(args))) 67 | sys.stderr.write(__doc__) 68 | sys.exit(1) 69 | 70 | return fh 71 | 72 | 73 | def pad_line(line): 74 | """Helper function to pad line to 80 characters in case it is shorter""" 75 | size_of_line = len(line) 76 | if size_of_line < 80: 77 | padding = 80 - size_of_line + 1 78 | line = line.strip('\n') + ' ' * padding + '\n' 79 | return line[:81] # 80 + newline character 80 | 81 | 82 | def run(fhandle): 83 | """ 84 | Replace the segment identifier with the contents of the chain identifier. 85 | 86 | Acts on ATOM/HETATM/ANISOU. 87 | 88 | This function is a generator. 89 | 90 | Parameters 91 | ---------- 92 | fhandle : a line-by-line iterator of the original PDB file. 93 | 94 | Yields 95 | ------ 96 | str (line-by-line) 97 | The modified (or not) PDB line. 98 | """ 99 | 100 | _pad_line = pad_line 101 | 102 | records = ('ATOM', 'HETATM', 'ANISOU') 103 | for line in fhandle: 104 | if line.startswith(records): 105 | line = _pad_line(line) 106 | yield line[:72] + line[21].ljust(4) + line[76:] 107 | else: 108 | yield line 109 | 110 | 111 | place_chain_on_seg = run 112 | 113 | 114 | def main(): 115 | # Check Input 116 | pdbfh = check_input(sys.argv[1:]) 117 | 118 | # Do the job 119 | new_pdb = run(pdbfh) 120 | 121 | try: 122 | _buffer = [] 123 | _buffer_size = 5000 # write N lines at a time 124 | for lineno, line in enumerate(new_pdb): 125 | if not (lineno % _buffer_size): 126 | sys.stdout.write(''.join(_buffer)) 127 | _buffer = [] 128 | _buffer.append(line) 129 | 130 | sys.stdout.write(''.join(_buffer)) 131 | sys.stdout.flush() 132 | except IOError: 133 | # This is here to catch Broken Pipes 134 | # for example to use 'head' or 'tail' without 135 | # the error message showing up 136 | pass 137 | 138 | # last line of the script 139 | # We can close it even if it is sys.stdin 140 | pdbfh.close() 141 | sys.exit(0) 142 | 143 | 144 | if __name__ == '__main__': 145 | main() 146 | -------------------------------------------------------------------------------- /pdbtools/pdb_delhetatm.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright 2018 João Pedro Rodrigues 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | 18 | """ 19 | Removes all HETATM records in the PDB file. 20 | 21 | Usage: 22 | python pdb_delhetatm.py 23 | 24 | Example: 25 | python pdb_delhetatm.py 1CTF.pdb 26 | 27 | This program is part of the `pdb-tools` suite of utilities and should not be 28 | distributed isolatedly. The `pdb-tools` were created to quickly manipulate PDB 29 | files using the terminal, and can be used sequentially, with one tool streaming 30 | data to another. They are based on old FORTRAN77 code that was taking too much 31 | effort to maintain and compile. RIP. 32 | """ 33 | 34 | import os 35 | import sys 36 | 37 | __author__ = "Joao Rodrigues" 38 | __email__ = "j.p.g.l.m.rodrigues@gmail.com" 39 | 40 | 41 | def check_input(args): 42 | """Checks whether to read from stdin/file and validates user input/options. 43 | """ 44 | 45 | # Defaults 46 | fh = sys.stdin # file handle 47 | 48 | if not len(args): 49 | # Reading from pipe with default option 50 | if sys.stdin.isatty(): 51 | sys.stderr.write(__doc__) 52 | sys.exit(1) 53 | 54 | elif len(args) == 1: 55 | if not os.path.isfile(args[0]): 56 | emsg = 'ERROR!! File not found or not readable: \'{}\'\n' 57 | sys.stderr.write(emsg.format(args[0])) 58 | sys.stderr.write(__doc__) 59 | sys.exit(1) 60 | 61 | fh = open(args[0], 'r') 62 | 63 | else: # Whatever ... 64 | emsg = 'ERROR!! Script takes 1 argument, not \'{}\'\n' 65 | sys.stderr.write(emsg.format(len(args))) 66 | sys.stderr.write(__doc__) 67 | sys.exit(1) 68 | 69 | return fh 70 | 71 | 72 | def run(fhandle): 73 | """ 74 | Remove all HETATM and associated records from the PDB file. 75 | 76 | This function is a generator. 77 | 78 | Parameters 79 | ---------- 80 | fhandle : a line-by-line iterator of the original PDB file. 81 | 82 | Yields 83 | ------ 84 | str (line-by-line) 85 | The modified (or not) PDB line. 86 | """ 87 | 88 | # CONECT 1179 746 1184 1195 1203 89 | char_ranges = (slice(6, 11), slice(11, 16), 90 | slice(16, 21), slice(21, 26), slice(26, 31)) 91 | 92 | het_serials = set() 93 | for line in fhandle: 94 | if line.startswith('HETATM'): 95 | het_serials.add(line[6:11]) 96 | continue 97 | elif line.startswith('ANISOU'): 98 | if line[6:11] in het_serials: 99 | continue 100 | elif line.startswith('CONECT'): 101 | if any(line[cr] in het_serials for cr in char_ranges): 102 | continue 103 | 104 | yield line 105 | 106 | 107 | remove_hetatm = run 108 | 109 | 110 | def main(): 111 | # Check Input 112 | pdbfh = check_input(sys.argv[1:]) 113 | 114 | # Do the job 115 | new_pdb = run(pdbfh) 116 | 117 | try: 118 | _buffer = [] 119 | _buffer_size = 5000 # write N lines at a time 120 | for lineno, line in enumerate(new_pdb): 121 | if not (lineno % _buffer_size): 122 | sys.stdout.write(''.join(_buffer)) 123 | _buffer = [] 124 | _buffer.append(line) 125 | 126 | sys.stdout.write(''.join(_buffer)) 127 | sys.stdout.flush() 128 | except IOError: 129 | # This is here to catch Broken Pipes 130 | # for example to use 'head' or 'tail' without 131 | # the error message showing up 132 | pass 133 | 134 | # last line of the script 135 | # We can close it even if it is sys.stdin 136 | pdbfh.close() 137 | sys.exit(0) 138 | 139 | 140 | if __name__ == '__main__': 141 | main() 142 | -------------------------------------------------------------------------------- /pdbtools/pdb_delinsertion.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright 2018 João Pedro Rodrigues 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | 18 | """ 19 | Deletes insertion codes in a PDB file. 20 | 21 | Deleting an insertion code shifts the residue numbering of downstream 22 | residues. Allows for picking specific residues to delete insertion codes for. 23 | 24 | Usage: 25 | python pdb_delinsertion.py [-