Repository navigation
Add cif-seqres and cif-atom SeqIO drivers - #1635
Conversation
|
The obvious next step is to add a driver for MMTF as well :) |
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
| @@ -0,0 +1,90 @@ | |||
| """ | |||
| Base function for sequence parsers that read structures Bio.PDB parsers. | |||
There was a problem hiding this comment.
Please remove the new line to follow PEP257 here
https://www.python.org/dev/peps/pep-0257/#multi-line-docstrings
|
|
||
| def SeqresTestGenerator(extension, parser): | ||
| """ | ||
| Test factory for tests reading SEQRES (or similar) records. |
There was a problem hiding this comment.
Please remove the new line to follow PEP257 here
https://www.python.org/dev/peps/pep-0257/#multi-line-docstrings
|
|
||
| def AtomTestGenerator(extension, parser): | ||
| """ | ||
| Test factory for tests reading ATOM (or similar) records. |
There was a problem hiding this comment.
Please remove the new line to follow PEP257 here
https://www.python.org/dev/peps/pep-0257/#multi-line-docstrings
|
I wonder if the new |
|
Fixed PEP257 problems. Does anyone know of a tool to pick that up for future commits?
How do you mean? In a separate package under Bio/SeqIO? |
|
I was wondering if we would put all the structure SeqIO code into a single file, perhaps even the existing |
|
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 |
|
(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. |
|
Thanks, @JoaoRodrigues. I've merged the SeqIO drivers into Should I rebase onto master to fix the conflict with |
|
If you are happy to rebase onto the latest master, that'd be great, thanks. |
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.
|
Done :) |
|
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 (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.
|
@peterjc - I added some tests to 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 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. |
| """Sanitise sequence identifier for output. | ||
|
|
||
| Removes the banned characters "[]()" and replaces the characters ":;" | ||
| with "|". The name is not to "width" characters if specified. |
There was a problem hiding this comment.
Typo? The name is truncated to "width" characters if specified.
| warnings.simplefilter("ignore", PDBConstructionWarning) | ||
|
|
||
| # e.g. First line is not a 'HEADER'; can't determine PDB ID | ||
| warnings.simplefilter("ignore", BiopythonWarning) |
There was a problem hiding this comment.
That looks like the warning ought to be BiopythonParserWarning instead? Another pre-existing minor issue.
There was a problem hiding this comment.
I agree -- I changed Bio.SeqIO.PdbIO to raise a BiopythonParserWarning instead in 41f310c.
| elif format in ["stockholm", "maf"]: | ||
| elif format == "stockholm": | ||
| r1_id = r1.id.replace(" ", "_") | ||
| if "start" in r1.annotations and "end" in r1.annotations: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
SeqRecordparsed bypdb-atom:id=1ABC:X.- Sequence is written by the Stockholm writer:
id=1ABC:X/1-100. - Sequence is read by the Stockholm reader:
id=1ABC:X/1-100. - Ids of the
SeqRecordsare 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=[])There was a problem hiding this comment.
Yeah, that brings back memories. Sotckholm files do generally have the /start-end suffix.
|
Regarding the git pre-commit hook, there is more than one linked to in #493 - this one ought to be using our In any case, the TravisCI/Tox style checks passed 👍 |
|
I'm using that pre-commit hook. I took a look, and the problem is from $ 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 LinuxBizarre. Easy enough to hack around, but I don't know if this is the intended behaviour. |
|
Ah - we have different The git commit hook ought to do the same -> I should note this on #493 and update the hook. |
|
@peterjc I'm going through outstanding PRs of mine – any chance of getting this merged? |
|
@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? |
|
Thank you @etal & @Bernhard10 for looking at this - and than you @StefansM for working on it - merging now... |
|
It looks like this broken the continuous integration testing, but just on an easily fixed style issue: update fixed in 84aaa25 |
|
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. |
|
Brilliant, thanks @peterjc! |
|
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 |
This commit adds the
cif-seqresandcif-atomdrivers to the Bio.SeqIO module. These are implemented by the
CifSeqresIteratorandCifAtomIteratorfunctions in theBio.SeqIO.CifIOmodule.Most of the logic for the
cif-atomdriver is identical to thepdb-atomdriver, as they both read from a Bio.PDB Structure object.The shared code has been refactored into the
AtomIteratorfunction inthe
Bio.SeqIO.structure_iomodule.The cif-seqres iterator reads three categories:
_pdbx_poly_seq_scheme,which contains the actual sequence;
_struct_ref, which containsdatabase 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.pyfile has been renamed totest_SeqIO_StructureIO.py, and now contains the tests for both thepdb-* 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.rstfile and understand that AppVeyor andTravisCI will be used to confirm the Biopython unit tests and
flake8stylechecks pass with these changes.
I have added my name to the alphabetical contributors listings in the files
NEWS.rstandCONTRIB.rstas part of this pull request, am listedalready, or do not wish to be listed.