Skip to content

Bio.PDB.PDBList update - #943

Merged
peterjc merged 11 commits into
biopython:masterfrom
BiPUJ:master
Mar 1, 2017
Merged

peterjc merged 11 commits into
biopython:masterfrom
BiPUJ:master

Conversation

@dadoskawina

Copy link
Copy Markdown
Contributor

Changes to module:

  1. Possibility to download structures in PDBx/mmCif, PDB, PDBML/XML and
    mmtf formats (both from function and command line)
  2. Possibility to download large structures in PDB-like formatted bundles
  3. New function: download specified set of structures
  4. Code is now python 3.x compatible
  5. Unit tests added
  6. Some minor updates

Note. The default download format changed from (deprecated) PDB to
(recommended by RCSB) PDBx/mmCIf.

1. Possibility to download structures in PDBx/mmCif, PDB, PDBML/XML and
mmtf formats
2. Possibility to download large structures in PDB-like formatted
bundles
3. New function: download specified set of structures
4. Code python 3.x comatible
5. Unit tests added.

Note. The default download format changed from (deprecated) PDB to
(recommended by RCSB) PDBx/mmCIf.
Update Bio.PDB.PDBList module
@peterjc

peterjc commented Sep 27, 2016

Copy link
Copy Markdown
Member

The PEP8 issues reported by TravisCI should be easy to fix:

Tests/test_PDBList.py:66:30: E251 unexpected spaces around keyword / parameter equals
Tests/test_PDBList.py:66:32: E251 unexpected spaces around keyword / parameter equals
Tests/test_PDBList.py:94:65: E225 missing whitespace around operator

(This just runs the command line tool pep8 with selected checks enabled)

@peterjc

peterjc commented Sep 27, 2016

Copy link
Copy Markdown
Member

Does this look like an intermittent network failure on TravisCI?

======================================================================
ERROR: test_retrieve_pdb_file (test_PDBList.TestPDBListGetStructure)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/travis/build/biopython/biopython/Tests/test_PDBList.py", line 87, in test_retrieve_pdb_file
    pdblist.retrieve_pdb_file(structure, file_format, overwrite, obsolete, pdir)
  File "/home/travis/build/biopython/biopython/build/lib.linux-x86_64-2.7/Bio/PDB/PDBList.py", line 267, in retrieve_pdb_file
    out.writelines(gz)
  File "/opt/python/2.7.9/lib/python2.7/gzip.py", line 462, in readline
    c = self.read(readsize)
  File "/opt/python/2.7.9/lib/python2.7/gzip.py", line 268, in read
    self._read(readsize)
  File "/opt/python/2.7.9/lib/python2.7/gzip.py", line 303, in _read
    self._read_gzip_header()
  File "/opt/python/2.7.9/lib/python2.7/gzip.py", line 197, in _read_gzip_header
    raise IOError, 'Not a gzipped file'
IOError: Not a gzipped file
----------------------------------------------------------------------

As an aside, can you add this to Tests/test_PDBList.py so it respects the run_tests.py --offline ... option:

import requires_internet
requires_internet.check()

@peterjc

peterjc commented Sep 27, 2016

Copy link
Copy Markdown
Member

Can this also handle the RSSB's new binary Macromolecular Transmission Format (MMTF, see http://mmtf.rcsb.org)? CC @abradle

@peterjc

peterjc commented Sep 27, 2016

Copy link
Copy Markdown
Member

Are you happy to dual license your contribution(s) under both the Biopython Licence Agreement and the 3-Clause BSD licence? See #898

@dadoskawina

Copy link
Copy Markdown
Contributor Author

Seems to be a different behavior of urlretrieve function under python 2 and python 3. It fails on python 2 when I try to download second time the same file. Don't know (yet) how to resolve this.

Comment thread Bio/PDB/PDBList.py Outdated
return obsolete

def retrieve_pdb_file(self, pdb_code, obsolete=False, pdir=None):
def retrieve_pdb_file(self, pdb_code, file_format='mmCif', overwrite=False, obsolete=False, pdir=None):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By changing the argument order your have broken backward compatibility, calls written as .retrieve_pdb_file(a, b, c) should still be interpreted as obsolete=b and pdir=c.

Add the new arguments to the end, i.e.

def retrieve_pdb_file(self, pdb_code, obsolete=False, pdir=None, file_format='mmCif', overwrite=False):

Comment thread Tests/test_PDBList.py Outdated
# This test downloads several large files from external database so it may run a while.

# Create temporary directory for files downloaded while testing.
basedir = os.path.join(os.getcwd(), "pdb-test")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using tempfile.mkdtemp(...) here instead might be better - less chance of leaving temporary files in the source code tree?

Rewritten retrieve_pdb_file tests to be more explicit.
@codecov-io

codecov-io commented Oct 7, 2016 •

Copy link
Copy Markdown

Codecov Report

Merging #943 into master will increase coverage by 0.63%.
The diff coverage is 14.08%.

@@            Coverage Diff             @@
##           master     #943      +/-   ##
==========================================
+ Coverage   80.12%   80.75%   +0.63%     
==========================================
  Files         316      322       +6     
  Lines       48492    49415     +923     
==========================================
+ Hits        38854    39906    +1052     
+ Misses       9638     9509     -129
Impacted Files Coverage Δ
Bio/_py3k/init.py 62% <0%> (ø) ✅
Bio/PDB/PDBList.py 14.28% <14.28%> (-0.29%) ❌
Bio/PDB/DSSP.py 54.54% <0%> (-4.08%) ❌
Bio/motifs/transfac.py 82% <0%> (-0.82%) ❌
Bio/Align/AlignInfo.py 88.88% <0%> (-0.67%) ❌
Bio/NMR/xpktools.py 40.32% <0%> (-0.48%) ❌
Bio/motifs/matrix.py 76.31% <0%> (-0.41%) ❌
Bio/SubsMat/init.py 61.49% <0%> (-0.23%) ❌
Bio/SearchIO/ExonerateIO/_base.py 97.58% <0%> (-0.17%) ❌
Bio/Graphics/KGML_vis.py 70.5% <0%> (-0.15%) ❌
... and 88 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 184e40b...0a3e55e. Read the comment docs.

@peterjc

peterjc commented Oct 7, 2016

Copy link
Copy Markdown
Member

There's a typo in the last commit, unimpotrant --> unimportant

Comment thread Bio/PDB/PDBList.py
# choice of the "Biopython License Agreement" or the "BSD 3-Clause License".
# Please see the LICENSE file that should have been included as part of this
# package.
# It may be distributed freely with respect to the original authors.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've not actually started dual licensing yet (see #898) as I'm still hoping to get a lawyer to look at the proposed wording for the header.

Comment thread Bio/PDB/PDBList.py Outdated
pdb = line.strip()
assert len(pdb) == 4
answer.append(pdb)
answer.append(pdb.decode())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work nicely on both Python 2 and 3? Would it be simpler to use from Bio._py3k import _as_string here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this work on both python versions.

Comment thread Bio/PDB/PDBList.py Outdated
for pdb_code in pdb_codes:
self.retrieve_pdb_file(pdb_code, obsolete=obsolete, pdir=pdir, file_format=file_format, overwrite=overwrite)

def download_entire_pdb(self, file_format='mmCif', listfile=None):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original API was:

    def download_entire_pdb(self, listfile=None):

To preserve the order, I suggest:

   def download_entire_pdb(self, listfile=None, file_format='mmCif'):

The docstring should comment on the change in default file format, which I assume was PDB format?

Comment thread Bio/PDB/PDBList.py Outdated
outfile.writelines((x + '\n' for x in entries))

def download_obsolete_entries(self, listfile=None):
def download_obsolete_entries(self, file_format='mmCif', listfile=None):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As noted elsewhere, add the new argument to the end to preserve the old API.

Comment thread Tests/test_PDBList.py Outdated
class TestPBDListGetList(unittest.TestCase):
"""
Test methods responsible for getting lists of entries.
"""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write this as one line please, as per PEP257 https://www.python.org/dev/peps/pep-0257/#one-line-docstrings

i.e.

 """Test methods responsible for getting lists of entries."""

(As the same for the rest of the file)

Comment thread Tests/test_PDBList.py Outdated
finally:
shutil.rmtree(temp_dir)

def test_retrieve_pdb_file_small_pdb(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests look very repetitive, how about something like this?:

    def check(structure, filename, format):
        with self.make_temp_directory(os.getcwd()) as tmp:
            pdblist = PDBList(pdb=tmp, obsolete_pdb=os.path.join(tmp, "obsolete"))
            path = os.path.join(tmp, structure[1:3], filename)
            pdblist.retrieve_pdb_file(structure, file_format="pdb")
            self.assertTrue(os.path.isfile(path))
            os.remove(path)

    def test_retrieve_pdb_file_small_pdb(self):
        self.check("127d", os.path.join(tmp, structure[1:3], "pdb127d.ent", "pdb")

    def test_retrieve_pdb_file_large_pdb(self):
        self.check("3k1q", os.path.join(tmp, structure[1:3], "3k1q-pdb-bundle.tar", "bundle")

    ...

@peterjc

peterjc commented Oct 7, 2016

Copy link
Copy Markdown
Member

@etal @abradle etc - I've not kept up to date the the PDB website changes, could you look over this work on downloading structure files from Biopython please?

…fferent directories.

Due to the urlretrieve implementation in python 2.7, two consecutive calls for the same structure to be downloaded into different directories failed. This fix correct it.
The error occured while trying to download all entries or all obsolete
entries in python 3.x
The file_format parameter was missing in some functions.
@peterjc

peterjc commented Feb 1, 2017

Copy link
Copy Markdown
Member

Are you happy to dual licence your contributions under both the Biopython License Agreement AND the BSD 3-Clause License? See #898.

Also, are you happy to be thanked by name in the NEWS and CONTRIB files?

@peterjc

peterjc commented Feb 1, 2017

Copy link
Copy Markdown
Member

@etal, @JoaoRodrigues, @lennax, @abradle etc - I've not kept up to date the the PDB website changes, could you look over this work on downloading structure files from Biopython please?

Comment thread Tests/test_PDBList.py Outdated
"""Test methods responsible for getting structures."""

@contextlib.contextmanager
def make_temp_directory(self, dir):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even within function scope, I prefer not to overwrite built-ins. directory is an alternative.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point :)

Comment thread Bio/PDB/PDBList.py
file_format = "mmCif"
overwrite = False

if len(sys.argv) > 2:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworking this module could be a good time to switch to argparse; this would reduce the complexity of the control flow.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very true.

@dadoskawina don't worry about that for this pull request.

Comment thread Bio/PDB/PDBList.py
"pdb" (format PDB),
"xml" (PMDML/XML format),

NOTE. The default download format has changed from PDB to PDBx/mmCif

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be worth adding a temporary warning (similar to a deprecation warning) that files are being downloaded in mmCIF. Aside from the legacy grep/awk scripts, I use dozens of executables that only accept PDB format.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, something like this where the function signature is changed to file_format=None to allow us to spot if the user passed in a value or not?:

if file_format is None:
    file_format = "mmCif"
    sys.stderr.write("WARNING: The default download format has changed from PDB to PDBx/mmCif\n")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would be a great place to put it.

@peterjc

peterjc commented Feb 2, 2017

Copy link
Copy Markdown
Member

@dadoskawina are you happy to make the small improvements suggested by @lennax yesterday?

@dadoskawina

dadoskawina commented Feb 18, 2017 via email

Copy link
Copy Markdown
Contributor Author

1. change `dir` variable to `directory`
2. add "deprecation warning"
@peterjc

peterjc commented Feb 28, 2017

Copy link
Copy Markdown
Member

I've tightened up the PEP8/PEP257 style checking since you started work on this, which is why TravisCI failed. You should be able to reproduce this locally and remove the unwanted blank line 195 in Bio/PDB/PDBList.py easily:

$ pip install pydocstyle
$ pydocstyle Bio/ BioSQL/ Tests/ Scripts/ Doc/ --ignore D100,D101,D102,D103,D104,D105,D200,D203,D204,D205,D207,D208,D209,D210,D211,D212,D213,D301,D302,D400,D401,D402,D403,D404
Bio/PDB/PDBList.py:195 in public method `retrieve_pdb_file`:
        D202: No blank lines allowed after function docstring (found 1)

@peterjc

peterjc commented Mar 1, 2017

Copy link
Copy Markdown
Member

CodeCov is warning about the test coverage, but other than that the automatic checks are happy. Thank you.

@lennax does this look ready to merge now? Note I would do a squash-merge given the number of small commits and merges which complicate the history.

@lennax

lennax commented Mar 1, 2017

Copy link
Copy Markdown
Contributor

@peterjc Looks good to me!

@peterjc
peterjc merged commit 765bb51 into biopython:master Mar 1, 2017
peterjc added a commit that referenced this pull request Mar 1, 2017
See previous commit from pull request #943.
@peterjc

peterjc commented Mar 1, 2017

Copy link
Copy Markdown
Member

Thank you all.

peterjc added a commit that referenced this pull request Mar 16, 2017
See pull request #943, merged as a squashed commit
765bb51
MarkusPiotrowski pushed a commit to MarkusPiotrowski/biopython that referenced this pull request Oct 31, 2017
Squashed commit of pull request biopython#943 to update Bio.PDB.PDBList module:

1. Possibility to download structures in PDBx/mmCif, PDB, PDBML/XML and
mmtf formats
2. Possibility to download large structures in PDB-like formatted
bundles
3. New function: download specified set of structures
4. Code python 3.x comatible
5. Unit tests added.

Note. The default download format changed from (deprecated) PDB to
(recommended by RCSB) PDBx/mmCIf.
MarkusPiotrowski pushed a commit to MarkusPiotrowski/biopython that referenced this pull request Oct 31, 2017
See previous commit from pull request biopython#943.
MarkusPiotrowski pushed a commit to MarkusPiotrowski/biopython that referenced this pull request Oct 31, 2017
See pull request biopython#943, merged as a squashed commit
765bb51
@peterjc

peterjc commented Aug 15, 2019

Copy link
Copy Markdown
Member

@dadoskawina : I'm trying to dual license Bio/PDB/PDBList.py on #2212, could you confirm Wiktoria Karwicka was also happy with both the Biopython License Agreement AND the BSD 3-Clause License? Thanks!

@peterjc peterjc mentioned this pull request Aug 15, 2019
8 of 9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants