Skip to content

Add integer multiplication methods for Seq, UnknownSeq and MutableSeq - #1669

Merged
peterjc merged 15 commits into
biopython:masterfrom
chris-rands:Seq_objects_integer_multiplication
Jun 1, 2018
Merged

peterjc merged 15 commits into
biopython:masterfrom
chris-rands:Seq_objects_integer_multiplication

Conversation

@chris-rands

Copy link
Copy Markdown
Contributor

This pull request addresses issue #1659

The issue explains the rationale, but here is a small example of the utility of the new methods:

>>> from Bio.Seq import Seq
>>> Seq('AT') * 2
Seq('ATAT', Alphabet())
>>> 2 * Seq('AT')    
Seq('ATAT', Alphabet())
>>> seq = Seq('AT')
>>> seq *= 2
>>> seq
Seq('ATAT', Alphabet())

The methods should work with all the Seq, UnknownSeq, and MutableSeq objects and with any alphabet. I also implemented some unit tests.

Let me know if this looks ok?

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. (This acknowledgement is optional.)

@chris-rands
chris-rands requested a review from peterjc as a code owner May 31, 2018 12:21
Comment thread Bio/Seq.py Outdated
return self.__class__(str(self) * other, self.alphabet)

def __imul__(self, other):
"""Multiply Seq in-place

@peterjc peterjc May 31, 2018 •

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.

Needs trailing full stop, see PEP257 https://www.python.org/dev/peps/pep-0257/

Likewise for the other docstrings.

Comment thread Bio/Seq.py Outdated
"""Multiply Seq in-place

Note although Seq is immutable, the in-place method is
included to match the behaviour for regular Python strings

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 close that sentence with a full stop please. Likewise for the other docstrings.

Comment thread Bio/Seq.py Outdated

if not isinstance(other, int):
raise TypeError("can't multiply MutableSeq by non-int type")
return self.__class__(str(self) * other, self.alphabet)

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.

I wonder if we can make this much more efficient by working directly with the self.data array, avoiding conversion to and from a string?

Comment thread Bio/Seq.py Outdated
>>> Seq('ATG', generic_dna) * 2
Seq('ATGATG', DNAAlphabet())
"""

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.

Please remove this blank line, likewise for the other methods.

@peterjc

peterjc commented May 31, 2018

Copy link
Copy Markdown
Member

The TravisCI check is failing on some fairly minor style issues:

$ flake8 Bio/
Bio/Seq.py:348:1: D202 No blank lines allowed after function docstring
Bio/Seq.py:348:1: D400 First line should end with a period
Bio/Seq.py:363:1: D202 No blank lines allowed after function docstring
Bio/Seq.py:363:1: D400 First line should end with a period
Bio/Seq.py:378:1: D202 No blank lines allowed after function docstring
Bio/Seq.py:378:1: D400 First line should end with a period
Bio/Seq.py:1396:1: D202 No blank lines allowed after function docstring
Bio/Seq.py:1396:1: D400 First line should end with a period
Bio/Seq.py:1411:1: D202 No blank lines allowed after function docstring
Bio/Seq.py:1411:1: D400 First line should end with a period
Bio/Seq.py:1426:1: D202 No blank lines allowed after function docstring
Bio/Seq.py:1426:1: D400 First line should end with a period
Bio/Seq.py:2052:1: D202 No blank lines allowed after function docstring
Bio/Seq.py:2052:1: D400 First line should end with a period
Bio/Seq.py:2070:1: D202 No blank lines allowed after function docstring
Bio/Seq.py:2070:1: D400 First line should end with a period
Bio/Seq.py:2088:1: D202 No blank lines allowed after function docstring
Bio/Seq.py:2088:1: D400 First line should end with a period

I've made a comment on the MutableSeq implementation - do you think working from the self.data array would be more efficient?

Have you tried using the BioSQL wrapper's DBSeq object (which subclasses Seq)? My expectation is it will break - probably it needs to create a Seq object due to the nature of the DBSeq object. Tests for this would best be added to class SeqInterfaceTest in Tests/common_BioSQL.py - you should be able to test this via Tests/test_BioSQL_sqlite3.py easily (setting up the other BioSQL back-ends is more trouble).

@codecov

codecov Bot commented May 31, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #1669 into master will increase coverage by <.01%.
The diff coverage is 92.85%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master   #1669      +/-   ##
=========================================
+ Coverage    85.5%   85.5%   +<.01%     
=========================================
  Files         327     327              
  Lines       49777   49819      +42     
=========================================
+ Hits        42564   42600      +36     
- Misses       7213    7219       +6
Impacted Files Coverage Δ
BioSQL/BioSeq.py 93.41% <100%> (+0.12%) ⬆️
Bio/Seq.py 98.09% <91.66%> (-0.39%) ⬇️
Bio/_py3k/__init__.py 72.61% <0%> (-3.58%) ⬇️
Bio/Application/__init__.py 86.16% <0%> (-1.19%) ⬇️
Bio/Entrez/Parser.py 82.04% <0%> (-0.21%) ⬇️
Bio/GA/Evolver.py 72.72% <0%> (+18.18%) ⬆️

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 c278cc9...eae4cc6. Read the comment docs.

@chris-rands

Copy link
Copy Markdown
Contributor Author

Thanks Peter, I corrected the style points and I totally agree about using self.data for the MutableSeq methods, did I implement it as you intended?

I am not familiar with BioSQL I'm afraid, can you provide more details on what you you would like there?

@peterjc

peterjc commented May 31, 2018

Copy link
Copy Markdown
Member

Your MutableSeq change looks great.

For BioSQL, I am picturing something like this in the tests (untested):

class SeqInterfaceTest(unittest.TestCase):
...

    def test_seq(self):
        """Make sure Seqs from BioSQL implement the right interface."""
        test_seq = self.item.seq
        alphabet = test_seq.alphabet
        ...
        tripled = test_seq * 3  # check DBSeq.__mul__
        self.assertTrue(isinstance(tripled, Seq))
        self.assertEqual(tripled, str(test_seq) * 3)
        self.assertEqual(tripled.alphabet, alphabet)
        tripled = 3 * test_seq 3  # check DBSeq.__rmul__
        self.assertTrue(isinstance(tripled, Seq))
        self.assertEqual(tripled, str(test_seq) * 3)
        self.assertEqual(tripled.alphabet, alphabet)
        # Something similar for __imul__ as well...

(You could define a new test method if you like, expanding the existing test_seq is my being a bit lazy, but hopefully giving a consistent example.)

@chris-rands

Copy link
Copy Markdown
Contributor Author

Thanks, I can implement those tests, and in terms of the implementation in BioSQL/BioSeq.py itself, can I do something similar to __add__ and __radd__, like:

def __mull__(self, other):
    """Multiply sequence by an integer.

    The sequence is first converted to a Seq object before multiplication.
    The returned object is a Seq object, not a DBSeq object.
    """
    # Let the Seq object deal with the alphabet issues etc
    return self.toseq() * other

 def __rmull__(self, other):
    """Multiply integer by a sequence.

    The sequence is first converted to a Seq object before multiplication.
    The returned object is a Seq object, not a DBSeq object.
    """
    # Let the Seq object deal with the alphabet issues etc
    return other * self.toseq()

 def __imull__(self, other):
    """Multiply sequence by integer in-place.

    The sequence is first converted to a Seq object before multiplication.
    The returned object is a Seq object, not a DBSeq object.
    """
    # Let the Seq object deal with the alphabet issues etc
    return self.toseq() * other

As I said I don't use BioSQL, so I really don't know if this is appropriate or not? Thanks for your patience

@peterjc

peterjc commented Jun 1, 2018

Copy link
Copy Markdown
Member

That looks perfect for the BioSQL changes, yes please.

@peterjc

peterjc commented Jun 1, 2018

Copy link
Copy Markdown
Member

Just needs the BioSQL tests added, and the code side of this should be ready.

Would you also like to write a couple of lines about this for the NEWS.rst file (and add yourself to the contributors to the upcoming release)?

@chris-rands

Copy link
Copy Markdown
Contributor Author

Thanks Peter, I made a few mistakes (now corrected hopefully) and I've added tests but they are failing on my system at least, it looks like the DBSeq methods are not overiding thse Seq methods correctly, what am I missing?

ERROR: test_multiplication (common_BioSQL.SeqInterfaceTest)
Check can multiply DBSeq objects by integers.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/cegg/rands/BIOPYTHON/biopython/Tests/common_BioSQL.py", line 543, in test_multiplication
    tripled = test_seq * 3
  File "/home/cegg/rands/.local/lib/python3.6/site-packages/Bio/Seq.py", line 360, in __mul__
    return self.__class__(str(self) * other, self.alphabet)
TypeError: __init__() missing 3 required positional arguments: 'alphabet', 'start', and 'length'

Comment thread Tests/common_BioSQL.py
alphabet = test_seq.alphabet
tripled = test_seq * 3
# Test DBSeq.__mul__
self.assertTrue(isinstance(tripled, Seq))

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 should probably add a check that this is not a DBSeq, i.e. that is it only a Seq:

self.assertFalse(isinstance(tripled, DBSeq))

(And repeat this later on)

@peterjc

peterjc commented Jun 1, 2018

Copy link
Copy Markdown
Member

I think your commit to "correcting typo in method names" would have fixed that exception which is what I would expect to see if the DEBSeq multiplication methods did not exist. Perhaps you got out of sync in your local testing?

@peterjc

peterjc commented Jun 1, 2018

Copy link
Copy Markdown
Member

If you are comfortable doing a git rebase onto the current master, that'd be great. Otherwise I'll deal with the (minor) merge conflict in the NEWS file.

@peterjc

peterjc commented Jun 1, 2018

Copy link
Copy Markdown
Member

Do you want to be on the list of contributors for Biopython 1.72?

@chris-rands

Copy link
Copy Markdown
Contributor Author

Thanks Peter, you are correct, sorry, I made a mistake with the testing, now all tests pass on my system when I run test_BioSQL_sqlite3.py.

I am already listed in the contributors because of a previous pull request. Are you able to resolve the conflict easily?

@peterjc

peterjc commented Jun 1, 2018

Copy link
Copy Markdown
Member

The merge conflict is trivial - two new news entries, keep both. Git can't decide on its own which order to put them in ;)

@peterjc

peterjc commented Jun 1, 2018

Copy link
Copy Markdown
Member

(I'm going to wait for the penultimate TravisCI and AppVeyor tests to finish)

@peterjc
peterjc force-pushed the Seq_objects_integer_multiplication branch from 994f8c1 to eae4cc6 Compare June 1, 2018 10:37
@peterjc
peterjc merged commit 384b2f3 into biopython:master Jun 1, 2018
@peterjc

peterjc commented Jun 1, 2018

Copy link
Copy Markdown
Member

Rebased to deal with the merge conflict, and applied to master - thank you!

@chris-rands

Copy link
Copy Markdown
Contributor Author

Great, thanks!

@chris-rands
chris-rands deleted the Seq_objects_integer_multiplication branch June 1, 2018 11:13
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.

2 participants