Skip to content

Add cif-seqres and cif-atom SeqIO drivers - #1635

Merged
peterjc merged 9 commits into
biopython:masterfrom
StefansM:feature-cifio
Oct 19, 2018
Merged

peterjc merged 9 commits into
biopython:masterfrom
StefansM:feature-cifio

Conversation

@StefansM

@StefansM StefansM commented Apr 29, 2018 •

Copy link
Copy Markdown
Contributor

This commit adds the cif-seqres and cif-atom
drivers to the Bio.SeqIO module. These are implemented by the
CifSeqresIterator and CifAtomIterator functions in the
Bio.SeqIO.CifIO module.

Most of the logic for the cif-atom driver is identical to the
pdb-atom driver, as they both read from a Bio.PDB Structure object.
The shared code has been refactored into the AtomIterator function in
the Bio.SeqIO.structure_io module.

The cif-seqres iterator reads three categories: _pdbx_poly_seq_scheme,
which contains the actual sequence; _struct_ref, which contains
database cross-references; and _struct_ref_seq, which is linked to
_struct_ref via the ref_id field and contains the PDB and chain ID of
the cross-reference.

Since so much of the test code is shared between the pdb-* and cif-*
SeqIO readers, the test_SeqIO_PdbIO.py file has been renamed to
test_SeqIO_StructureIO.py, and now contains the tests for both the
pdb-* and cif-* readers. For the sake of consistency, I have also added
2XHE.cif and a_structure.cif to the Tests/PDB directory.

Shared test code is implemented in a superclass returned from a
function in order to convince unittest to run the child test suites but
not the parent suite.

This pull request addresses issue #1573.

Comments are very welcome. I haven't marked this as a WIP because it works as-is, but I am expecting some suggestions about the naming conventions.


I hereby agree to dual licence this and any previous contributions under both
the Biopython License Agreement AND the BSD 3-Clause License.

I have read the CONTRIBUTING.rst file and understand that AppVeyor and
TravisCI will be used to confirm the Biopython unit tests and flake8 style
checks pass with these changes.

I have added my name to the alphabetical contributors listings in the files
NEWS.rst and CONTRIB.rst as part of this pull request, am listed
already, or do not wish to be listed.

@StefansM
StefansM requested a review from peterjc as a code owner April 29, 2018 19:17
@StefansM

Copy link
Copy Markdown
Contributor Author

The obvious next step is to add a driver for MMTF as well :)

@codecov

codecov Bot commented Apr 29, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #1635 into master will increase coverage by 0.02%.
The diff coverage is 95.23%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1635      +/-   ##
==========================================
+ Coverage    85.5%   85.52%   +0.02%     
==========================================
  Files         327      327              
  Lines       49777    49834      +57     
==========================================
+ Hits        42562    42621      +59     
+ Misses       7215     7213       -2
Impacted Files Coverage Δ
Bio/SeqIO/__init__.py 85.05% <ø> (ø) ⬆️
Bio/AlignIO/PhylipIO.py 90.32% <100%> (-0.05%) ⬇️
Bio/SeqIO/PdbIO.py 95.65% <94.68%> (+0.65%) ⬆️
Bio/SeqIO/SeqXmlIO.py 86.89% <0%> (+0.43%) ⬆️
Bio/AlignIO/StockholmIO.py 93.92% <0%> (+0.46%) ⬆️
Bio/AlignIO/EmbossIO.py 75.18% <0%> (+1.5%) ⬆️

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 26fc84d...41f310c. Read the comment docs.

Comment thread Bio/SeqIO/structure_io.py Outdated
@@ -0,0 +1,90 @@
"""
Base function for sequence parsers that read structures Bio.PDB parsers.

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.

Comment thread Tests/test_SeqIO_StructureIO.py Outdated

def SeqresTestGenerator(extension, parser):
"""
Test factory for tests reading SEQRES (or similar) records.

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.

Comment thread Tests/test_SeqIO_StructureIO.py Outdated

def AtomTestGenerator(extension, parser):
"""
Test factory for tests reading ATOM (or similar) records.

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.

@peterjc

peterjc commented May 1, 2018

Copy link
Copy Markdown
Member

I wonder if the new structure_io module could/should be under Bio/SeqIO/ instead? Hmm.

@peterjc
peterjc requested review from JoaoRodrigues and etal May 1, 2018 21:54
@StefansM

StefansM commented May 1, 2018

Copy link
Copy Markdown
Contributor Author

Fixed PEP257 problems. Does anyone know of a tool to pick that up for future commits?

I wonder if the new structure_io module could/should be under Bio/SeqIO/ instead? Hmm.

How do you mean? In a separate package under Bio/SeqIO?

@peterjc

peterjc commented May 2, 2018

Copy link
Copy Markdown
Member

I was wondering if we would put all the structure SeqIO code into a single file, perhaps even the existing Bio/SeqIO/PdbIO.py itself?

@StefansM

StefansM commented May 4, 2018

Copy link
Copy Markdown
Contributor Author

Ah, I see. I've no objection to doing so. It would help prevent pollution of the Bio/SeqIO package with non-driver supporting files. Users (should) only interact with the SeqIO drivers through the Bio.SeqIO interface, so it won't be confusing to have PDB and mmCIF code mixed into the same file, and masochists using the PdbIO module directly won't experience any breakage.

@JoaoRodrigues

Copy link
Copy Markdown
Member

(for some reason all github emails asking for comments/reviews were caught by my spam filter ...)

I approve the change, looks very nice, thanks @StefansM ! I'd support merging all drivers in one file too. You can also call it StructureIO if you don't want to make it sound too PDB-dependent.

@JoaoRodrigues JoaoRodrigues left a comment

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.

👍

@StefansM

Copy link
Copy Markdown
Contributor Author

Thanks, @JoaoRodrigues. I've merged the SeqIO drivers into Bio.SeqIO.PdbIO in order to keep backwards compatibility with anyone using directly calling Bio.SeqIO.PdbIO.{PdbSeqresIterator,PdbAtomIterator}. I also added a description to the Bio.SeqIO docstring.

Should I rebase onto master to fix the conflict with NEWS.rst?

@peterjc

peterjc commented May 15, 2018

Copy link
Copy Markdown
Member

If you are happy to rebase onto the latest master, that'd be great, thanks.

StefansM added 6 commits May 17, 2018 00:11
This commit adds the 'cif-seqres' and 'cif-atom'
drivers to the Bio.SeqIO module. These are implemented by the
'CifSeqresIterator' and 'CifAtomIterator' functions in the
'Bio.SeqIO.CifIO' module.

Most of the logic for the 'cif-atom' driver is identical to the
'pdb-atom' driver, as they both read from a Bio.PDB Structure object.
The shared code has been refactored into the 'AtomIterator' function in
the 'Bio.SeqIO.structure_io' module.

The cif-seqres iterator reads three categories: '_pdbx_poly_seq_scheme',
which contains the actual sequence; '_struct_ref', which contains
database cross-references; and '_struct_ref_seq', which is linked to
_struct_ref via the ref_id field and contains the PDB and chain ID of
the cross-reference.

Since so much of the test code is shared between the pdb-* and cif-*
SeqIO readers, the 'test_SeqIO_PdbIO.py' file has been renamed to
'test_SeqIO_StructureIO.py', and now contains the tests for both the
pdb-* and cif-* readers. For the sake of consistency, I have also added
2XHE.cif and a_structure.cif to the Tests/PDB directory.

Shared test code is implemented in a superclass returned from a
function in order to convince unittest to run the child test suites but
not the parent suite.
Merge the Bio.SeqIO.{Cif,Pdb}IO and the Bio.SeqIO.structure_io modules
together. Modules have been merged into PdbIO.py to retain backwards
compatibility with anyone directly using the iterators from PdbIO
instead of the SeqIO interface.
@StefansM

Copy link
Copy Markdown
Contributor Author

Done :)

@peterjc

peterjc commented May 17, 2018

Copy link
Copy Markdown
Member

This looks nice - what do you think @JoaoRodrigues, OK from your point of view? Are the extra test files reasonable (not too big etc)?

The only think I would add would be a couple of lines to Tests/test_SeqIO.py adding the new file formats to the list of files being tested (and then since this is a legacy print-and-compare test, update the expected output file Tests/output/test_SeqIO by running run_tests.py -g test_SeqIO.py).

(There is a whole chapter on our test framework in the Tutorial if you run into any questions about how that works.)

Add the pdb-atom, pdb-seqres, cif-atom and cif-seqres drivers to
test_SeqIO. This required the following changes:

* Bio.AlignIO.PylipIO: Added a sanitize_name function for use in the
  test_SeqIO test suite. Colons in the sequence ID were not being
  replaced with pipes in test_SeqIO, so sequences read from the
  structure file (1ABC:A) were being compared with the sanitised version
  from the Phylip files (1ABC|A).

* test_SeqIO.py:
    - Added tests for the {cif,pdb}-{atom-seqres} drivers.

    - Added a readlines() method to ForwardOnlyHandle, which is required
      by the PDBParser.

    - Added a condition for the Stockholm parser for sequences that have
      a "start" and "end" annotation. The "stockholm" driver writes and
      parses these like "1ABC:A/X-Y", but the residue range is not
      included in the sequence ID when parsed from a structure.

* output/test_SeqIO: Generated by "run_tests.py -g". I have taken a look
at the additional output, and the parsed sequences are correct.
@StefansM

Copy link
Copy Markdown
Contributor Author

@peterjc - I added some tests to test_SeqIO.py. Turns out the pdb-atom and pdb-seqres drivers weren't being tested, so I added those as well!

Some minor changes, described in the commit message, were required to pass the tests without hitting an assertion. The problems were mostly to do with trying to round-trip sequence IDs in different formats: Stockholm adds a /start-stop suffix to the sequence ID, and Phylip replaces colons with pipes. I'm not familiar enough with the sequence formats to know if my changes to test_SeqIO have masked an existing error with the pdb-* parsers.

Also note that this change does not pass the recommended pre-commit script because of style problems. I forced the commit and left the style changes untouched because it looked like they had been grandfathered in a while ago.

Comment thread Bio/AlignIO/PhylipIO.py Outdated
"""Sanitise sequence identifier for output.

Removes the banned characters "[]()" and replaces the characters ":;"
with "|". The name is not to "width" characters if specified.

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.

Typo? The name is truncated to "width" characters if specified.

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.

Thanks -- fixed in 230d68f.

Comment thread Tests/test_SeqIO.py Outdated
warnings.simplefilter("ignore", PDBConstructionWarning)

# e.g. First line is not a 'HEADER'; can't determine PDB ID
warnings.simplefilter("ignore", BiopythonWarning)

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.

That looks like the warning ought to be BiopythonParserWarning instead? Another pre-existing minor issue.

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.

I agree -- I changed Bio.SeqIO.PdbIO to raise a BiopythonParserWarning instead in 41f310c.

Comment thread Tests/test_SeqIO.py
elif format in ["stockholm", "maf"]:
elif format == "stockholm":
r1_id = r1.id.replace(" ", "_")
if "start" in r1.annotations and "end" in r1.annotations:

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 expand on this, from the commit comment:

      Added a condition for the Stockholm parser for sequences that have
      a "start" and "end" annotation. The "stockholm" driver writes and
      parses these like "1ABC:A/X-Y", but the residue range is not
      included in the sequence ID when parsed from a structure.

Does something in your test cases trigger this? It feels like you may have found a Stockholm bug... but I remember this being complicated, so it could be deliberate.

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.

If a SeqRecord has a start and end annotation, Bio.AlignIO.StockholmIO.StockholmWriter._write_record adds a /start-end suffix to the sequence ID. When the Stockholm reader reads that record, it does not strip the suffix from the ID.

When a sequence is parsed using cif-atom or pdb-atom, the start and end annotations are set. So the round-trip test case looks like this:

  1. SeqRecord parsed by pdb-atom: id=1ABC:X.
  2. Sequence is written by the Stockholm writer: id=1ABC:X/1-100.
  3. Sequence is read by the Stockholm reader: id=1ABC:X/1-100.
  4. Ids of the SeqRecords are compared: "1ABC:X" != "1ABC:X/1-100.

My hack explicitly adds the range suffix to the original SeqRecord's ID when checking the round trip with the Stockholm format. This looks like it could be a bug in the Stockholm reader, but it might be intentional. from StockholmIO.StockholmIterator:

If an accession is provided for an entry in the meta data, IT WILL NOT
be used as the record.id (it will be recorded in the record's
annotations). This is because some files have (sub) sequences from
different parts of the same accession (differentiated by different
start-end positions).

IMO, the current behaviour is unintuitive, but I don't know much about the Stockholm format. Maybe users expect the IDs to contain the range.

Minimal example:

>>> import io
  2 
  3 import Bio.SeqIO
  4 import Bio.SeqRecord
  5 import Bio.Seq
  6 
  7 record = Bio.SeqRecord.SeqRecord(Bio.Seq.Seq("A"), id="foo")
  8 record.annotations["start"] = 1
  9 record.annotations["end"] = 2
 10 buf = io.StringIO()
 11 Bio.SeqIO.write(record, buf, "stockholm")
 12 buf.seek(0)
 13 read = Bio.SeqIO.read(buf, "stockholm")
>>> record
SeqRecord(seq=Seq('A', Alphabet()), id='foo', name='<unknown name>', description='<unknown description>', dbxrefs=[])
>>> read
SeqRecord(seq=Seq('A', SingleLetterAlphabet()), id='foo/1-2', name='foo', description='<unknown description>', dbxrefs=[])

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.

Yeah, that brings back memories. Sotckholm files do generally have the /start-end suffix.

@peterjc

peterjc commented May 21, 2018

Copy link
Copy Markdown
Member

Regarding the git pre-commit hook, there is more than one linked to in #493 - this one ought to be using our .flake8 configuration files (which are what the TravisCI/Tox style checks use):

https://github.com/peterjc/biopython/blob/pre_commit_linting/Scripts/git_pre_commit_hook.py

In any case, the TravisCI/Tox style checks passed 👍

@StefansM

Copy link
Copy Markdown
Contributor Author

I'm using that pre-commit hook. I took a look, and the problem is from flake8, not the script. It occurs when I try to lint files in two different top-level directories.

$ flake8 Tests/test_SeqIO.py Tests/test_SeqIO_PdbIO.py 
$ echo $?
0
$ flake8 Bio/AlignIO/PhylipIO.py
$ echo $?
0
$ flake8 Bio/AlignIO/PhylipIO.py Tests/test_SeqIO.py Tests/test_SeqIO_PdbIO.py 
Bio/AlignIO/PhylipIO.py:170:13: F841 local variable 'number_of_seqs' is assigned to but never used
Bio/AlignIO/PhylipIO.py:171:13: F841 local variable 'length_of_seqs' is assigned to but never used
Bio/AlignIO/PhylipIO.py:208:13: F841 local variable 'length_of_seqs' is assigned to but never used
Bio/AlignIO/PhylipIO.py:364:1: D105 Missing docstring in magic method
Tests/test_SeqIO.py:1:1: D100 Missing docstring in public module
Tests/test_SeqIO.py:179:80: E501 line too long (91 > 79 characters)
Tests/test_SeqIO.py:201:80: E501 line too long (81 > 79 characters)
Tests/test_SeqIO.py:281:1: D102 Missing docstring in public method
Tests/test_SeqIO.py:287:1: D102 Missing docstring in public method
Tests/test_SeqIO.py:290:1: D102 Missing docstring in public method
Tests/test_SeqIO.py:293:1: D102 Missing docstring in public method
Tests/test_SeqIO.py:328:1: D400 First line should end with a period
Tests/test_SeqIO.py:328:1: D401 First line should be in imperative mood
Tests/test_SeqIO.py:346:1: D103 Missing docstring in public function
Tests/test_SeqIO.py:353:1: D400 First line should end with a period
Tests/test_SeqIO.py:353:1: D401 First line should be in imperative mood
Tests/test_SeqIO.py:370:1: D103 Missing docstring in public function
Tests/test_SeqIO.py:542:80: E501 line too long (83 > 79 characters)
Tests/test_SeqIO.py:609:80: E501 line too long (80 > 79 characters)
Tests/test_SeqIO_PdbIO.py:1:1: D100 Missing docstring in public module
Tests/test_SeqIO_PdbIO.py:10:5: F401 'numpy' imported but unused
Tests/test_SeqIO_PdbIO.py:14:5: F401 'numpy.linalg.svd' imported but unused
Tests/test_SeqIO_PdbIO.py:14:5: F401 'numpy.linalg.det' imported but unused
Tests/test_SeqIO_PdbIO.py:25:1: D202 No blank lines allowed after function docstring
Tests/test_SeqIO_PdbIO.py:81:1: D204 1 blank line required after class docstring
Tests/test_SeqIO_PdbIO.py:86:1: D204 1 blank line required after class docstring
Tests/test_SeqIO_PdbIO.py:91:1: D202 No blank lines allowed after function docstring
Tests/test_SeqIO_PdbIO.py:135:80: E501 line too long (83 > 79 characters)
Tests/test_SeqIO_PdbIO.py:152:80: E501 line too long (99 > 79 characters)
$ flake8 --version
3.5.0 (flake8-blind-except: 0.1.1, flake8-docstrings: 1.3.0, pydocstyle: 2.1.1, mccabe: 0.6.1, pycodestyle: 2.3.1, pyflakes: 1.6.0) CPython 3.6.5 on Linux

Bizarre. Easy enough to hack around, but I don't know if this is the intended behaviour.

@peterjc

peterjc commented May 22, 2018

Copy link
Copy Markdown
Member

Ah - we have different .flake8 config files in each top level folder, and TravisCI runs the flake8 on each top level folder separately. This is a hack due to limited flexibility in the .flake8 configuration file.

The git commit hook ought to do the same -> I should note this on #493 and update the hook.

@StefansM

StefansM commented Oct 9, 2018

Copy link
Copy Markdown
Contributor Author

@peterjc I'm going through outstanding PRs of mine – any chance of getting this merged?

@peterjc

peterjc commented Oct 15, 2018

Copy link
Copy Markdown
Member

@etal was happy earlier - any comments from eg @JoaoRodrigues, @mdehoon, @lennax, @jgreener64, @Bernhard10, @rtf-const (as people who've made somewhat relevant contributors to the the PDB code)?

If not, let's merge this at the end of this week?

@peterjc

peterjc commented Oct 19, 2018

Copy link
Copy Markdown
Member

Thank you @etal & @Bernhard10 for looking at this - and than you @StefansM for working on it - merging now...

@peterjc
peterjc merged commit 507ec91 into biopython:master Oct 19, 2018
@peterjc

peterjc commented Oct 25, 2018 •

Copy link
Copy Markdown
Member

It looks like this broken the continuous integration testing, but just on an easily fixed style issue:

$ flake8 Bio/
Bio/SeqIO/PdbIO.py:371:35: E127 continuation line over-indented for visual indent
Bio/SeqIO/PdbIO.py:372:35: E127 continuation line over-indented for visual indent

https://travis-ci.org/biopython/biopython/builds/443619957

update fixed in 84aaa25

@peterjc

peterjc commented Oct 25, 2018

Copy link
Copy Markdown
Member

Ah - this also looks like the source of a Python 3.7 issue, https://ci.appveyor.com/project/biopython/biopython/builds/19635218 - noted in passing on #1832 (comment) related to #1693 - we missed this because we've only recently added automated Python 3.7 testing.

@StefansM

Copy link
Copy Markdown
Contributor Author

Brilliant, thanks @peterjc!

@StefansM
StefansM deleted the feature-cifio branch October 25, 2018 19:44
@peterjc

peterjc commented Oct 26, 2018

Copy link
Copy Markdown
Member

With hindsight we should have rebased this branch or otherwise triggered a fresh TravisCI / AppVeyor test. This should fix another regression, 83ee94d for https://travis-ci.org/biopython/biopython/builds/446638509

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