Repository navigation
Update of CodonTable - #1501
Update of CodonTable#1501
Conversation
|
@peterjc Can we turn off
Knuth's style here means: line break before the binary operator |
|
|
||
| def line_wrap(text, indent=0, max_len=78): | ||
| def line_wrap(text, indent=0, max_len=78, string=False): | ||
| """Return a wrapped line if lenght is larger max_len.""" |
There was a problem hiding this comment.
Also might be worth expanding the docstring to explain the use of the new string argument - I can see you use this to split string arguments over multiple lines, but it would only work if the strings are single quote delimited.
(The implementation is fine given it is specific to this helper script, but making this more explicit seems safer for future users of this code.)
| # Start of auto-generated output from Scripts/update_ncbi_codon_table.py # | ||
| ########################################################################## | ||
|
|
||
| """[1:]) |
There was a problem hiding this comment.
Personally I found the five separate print statements clearer, but don't have a strong view on this change.
| >>> translate(coding_dna3, table=27) # Table 27: TGA -> STOP or W | ||
| 'MARKW' | ||
|
|
||
| It will howver raise a BiopythonWarning (not shown) |
| The restriction enzyme list in Bio.Restriction has been updated to the | ||
| November 2017 release of REBASE. | ||
|
|
||
| New codon tables 27-31 from NCBI were added to Bio.Data.CodonTable. |
There was a problem hiding this comment.
Also mention these include some codons with unambiguous bases which can dependent on context encode an amino acid or a stop codon?
|
You actually add all the missing tables 27-31, which is good. I've noted some minor things like typos, otherwise this looks fine. Good work on reworking the test suite. Personally I would have split that into a second commit (making it easier to test the functional changes vs the test changes), but I take it you have confirmed the old tests still pass? |
peterjc
left a comment
There was a problem hiding this comment.
After the conversion to a proper unit test, you also need to remove Tests/output/test_CodonTable from the old print-and-compare test.
(See also the couple of typos I noted, and suggestion to document the string argument in the line wrapping function).
|
@peterjc Thank you, I will do the required changes. Regarding the tests, I think I included all the test from the old file in the new version. To be sure I will run the old testfile on this branch (I did in the beginning, but not at the final stage). I also mailed to the e-mail list, asking for opinions how to deal with the problematic tables. Do you have an opinion regarding |
|
I am sympathetic to line breaks before binary operators, and would be happy to adopt W504 in place of W503 to enforce this - but that is not yet merged into pydocstyle/flake8: Given W503 is now not on the default pydocstyle ignore list, and not compatible with the updated PEP8, I am happy for us to ignore it. Please update Update: Confusingly flake8 does have W504, even if pycodestyle does not. |
|
@peterjc +1 for agreeing to ignore W503. However, we need to do this separately, otherwise this PR will always fail the style checks on TravisCI ... |
|
Sure, we can ignore W503 as its own PR (and merge that quickly), that's probably a better plan. |
|
Cross reference pull request #1503 |
b13f290 to
fab655d
Compare
|
That's strange, obviously Python 2 has problems with line continuation in doctests? I ran I'm going to use ELLIPSIS |
Codecov Report
@@ Coverage Diff @@
## master #1501 +/- ##
==========================================
+ Coverage 84.79% 84.84% +0.05%
==========================================
Files 323 323
Lines 49398 49383 -15
==========================================
+ Hits 41885 41901 +16
+ Misses 7513 7482 -31
Continue to review full report at Codecov.
|
|
If ellipsis doesn't work, just put the very long line directly in the doctest - crude but effective? |
|
Excellent. Thus far no replies on the mailing list: I'm happy to merge this - but given the wider than usual implications of this change, I'd like a third set of eyes to look at this first. Maybe @bow? |
bow
left a comment
There was a problem hiding this comment.
@MarkusPiotrowski The changes look good! I have two comments: one for the code style and another for a (seemingly) remote but not impossible case.
| version = '' | ||
| for line in open("gc.prt").readlines(): | ||
| if not version and line.startswith("-- Version"): | ||
| version = line.split('Version')[1].strip() |
There was a problem hiding this comment.
How likely is it that Version occurs more than once? I've found it is often better to be explicit with the number of expected split()s. In this case, if we only expect it once, I think simply doing .split('Version', 1) makes the code more robust.
| BiopythonWarning) | ||
| break | ||
| except KeyError: | ||
| pass # KeyError is the expected behaviour for most tables |
There was a problem hiding this comment.
This may be more of a style thing, but I often feel that except <Exception>: pass can be better rewritten to convey intent.
How about something like this?
for codon in stop_codons:
if codon not in forward_table:
continue
if to_stop:
<raise exception>
<warn>
We can even remove the continue clause:
for codon in {c for c in stop_codons if c in forward_table}:
if to_stop:
<raise exception>
<warn>
This would raise the exception on the first ambiguous codons if to_stop is True, but would raise warnings for all the ambiguous codons otherwise.
There was a problem hiding this comment.
A wrong understanding of 'EAFP' from my side. I take you second approach, but adding a break, otherwise the warning may be raised several times for the same codon table.
There was a problem hiding this comment.
Unfortunately, this doesn't work. codon in forward_table gives a TypeError for ambiguous tables, while forward_table[codon] works nicely (either returning an amino acid or raising a KeyError, as expected).
I don't know if this is a bug or intended, the __getitem__ method in AmbiguousForwardTable is quite complex...
So if you don't insist, I would let it as it is.
Example:
>>> from Bio.Data import CodonTable
>>> table = CodonTable.ambiguous_dna_by_id[1]
>>> table.forward_table['ATG']
'M'
>>> 'ATG' in table.forward_table
Traceback (most recent call last):
File "<pyshell#61>", line 1, in <module>
'ATG' in table.forward_table
File "C:\Program Files\Anaconda32\lib\site-packages\Bio\Data\CodonTable.py", line 375, in __getitem__
self.ambiguous_nucleotide)
File "C:\Program Files\Anaconda32\lib\site-packages\Bio\Data\CodonTable.py", line 209, in list_possible_proteins
c1, c2, c3 = codon
TypeError: 'int' object is not iterable
There was a problem hiding this comment.
Aha, I had thought earlier forward_table should behave like a plain dict. That is apparently not the case.
The error we're seeing is because forward_table (or rather, its class) doesn't define a __contains__ method, which makes the in operation resort to using __getitem__ but using integers instead of ATG.
Without an expected __contains__ behavior, I can understand that the exception <E>: pass style is the one that does what we want ~ so let's keep it that way for now.
There was a problem hiding this comment.
Given it is dictionary like, do you think is the expected behaviour of the missing __contains__method clearly defined? If so we could add it?
There was a problem hiding this comment.
I'm thinking about adding it. I'll see how it works
59f7e16 to
ae7503d
Compare
|
That was a bit too quick, lookups which return |
|
Some of the past design choices were likely practical for implementation (e.g. the |
|
I feel the latest change is ok as is. If If we ever want to tackle these corner cases, I don't think making the codon table behave as if it were a But these feel like overengineering ~ I'm happy with the latest changes as is. |
|
@peterjc Sorry, I don't understand your last comment. |
|
@MarkusPiotrowski I meant it took me a while to understand the codon tables, and when they did and didn't raise a KeyError. For reference, this was originally used in |
| "Such codons will be translated as amino acid." | ||
| .format(codon, forward_table[codon]), | ||
| BiopythonWarning) | ||
| break |
There was a problem hiding this comment.
I had to study this for loop with the break some time to understand what it does. How about something like:
dual_coding = [c for c in stop_codons if c in forward_table]
if dual_coding:
if to_stop:
raise ValueError("You cannot use 'tostop=True' with this table as it "
"contains %i codons which can be both stop and an "
"amino acid." % len(dual_coding))
warnings.warn("This table contains %i codons which code for both "
"stop and an amino acid. Such codons will be translated ",
"as the amino acid." % len(dual_coding), BiopythonWarning)(You could equally use c = dual_coding[0] and keep the existing proposed messages, which are helpfully explicit with an example codon)
ae7503d to
3736a4e
Compare
|
Sorry, I forgot about this. Thanks for that final polish. |
update_ncbi_codon_table.pyto give a PEP8 compliant outputBio.Data.CodonTable.py(and some coding style issues)assertstatements inBio.Data.Codon.Table.py. They were moved totest_CodonTable.pytest_CodonTable.pyto use theunittestframework. Added tests for each codon table.Seq.pyto raise a warning or exception when codon tables with 'dual coding' stop codons are used (see NCBI Genetic Code Tables 27 to 31 + ambiguous codons #1224). The use of such tables during translation will generally raise aBiopythonWarning. Ifto_stop=TrueaValueErroris raised.test_Seq.pyto include these cases.NEWS.rstfor the addition of the new codon tables.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 am happy be thanked by name in the
NEWS.rstandCONTRIB.rstfiles,and have added myself to those files as part of this pull request.