Skip to content

Seq and SeqRecord integer multiplication #1659

Closed

Description

@peterjc

Python strings support multiplication by integers as follows:

>>> 4 * "A"
'AAAA'
>>> 4 * "ACGT"
'ACGTACGTACGTACGT'
>>> "ACGT" * 4
'ACGTACGTACGTACGT'

Note multiplication by floats is not supported:

>>> "ACGT" * 4.5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'
>>> "ACGT" * 4.0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'

It would be nice to define the relevant methods in the Seq object and UnknownSeq object (although it is less clear to me what that MutableSeq should do here - presumably the same, return a new object rather than acting in place?).

Likewise for the SeqRecord object, which should probably follow the existing meta-data behaviour for addition. i.e. record * 3`` and 3 * recordandrecord + record + record`` should all return the same thing.

This would be particularly useful for constructing test cases (e.g. making a SeqRecord of 1000 base pairs long using "ACGT" * 25 as the sequence).

Activity

  1. added
    good first issue
    on May 21, 2018
  2. chris-rands commented on May 22, 2018

    @chris-rands
    Contributor

    Regarding your question, maybe MutableSeq should return a new object and not in in-place in the same way that in native Python my_list * 3 returns a new list and leaves the original unchanged. This also feels right in terms of methods, I guess __mul__ for record * 3, __rmul__ for 3 * record, and __imul__ for record *= 3 (the in-place case).

  3. peterjc commented on May 22, 2018

    @peterjc
    MemberAuthor

    Good example Chris - yes, the behaviour of a Python list convinces me that left and right multiplication on a MutableSeq should also return a new object rather than be in place (i.e. __mul__ and __rmul__).

    I had not considered in place multiplication, here I think like the Python string we should not support this for the (read only / conceptually immutable) Seq and UnknownSeq etc:

    >>> "ACGT" *= 3
      File "<stdin>", line 1
    SyntaxError: can't assign to literal

    However, for the MutableSeq again following a Python list, supporting in-place multiplication (magic method __imul__) seems fine:

    >>> x = ["A", "C", "G", "T"]
    >>> x *= 3
    >>> x
    ['A', 'C', 'G', 'T', 'A', 'C', 'G', 'T', 'A', 'C', 'G', 'T'
    
  4. chris-rands commented on May 22, 2018

    @chris-rands
    Contributor

    Thanks Peter, string literals don't support *=, but strings assigned to variables do (as do other immutable types like tuples):

    >>> seq = "ACGT"
    >>> seq *= 3
    >>> seq
    'ACGTACGTACGT'
    

    Also Seq objects already support += (without __iadd__ defined in Seq.py, I think python automatically overides it?):

    >>> from Bio.Seq import Seq
    >>> seq = Seq("A")
    >>> seq += "T"
    >>> seq
    Seq('AT', Alphabet())
    

    In the Python docs they say "sequence types should implement addition (meaning concatenation) and multiplication (meaning repetition) by defining the methods _add_(), _radd_(), _iadd_(), _mul_(), _rmul_() and _imul_()",

    (but I don't know if Seq is meant to be implemented as a sequence type; isinstance(Seq('A'), collections.Sequence) is False)

  5. peterjc commented on May 22, 2018

    @peterjc
    MemberAuthor

    Good point - I've never used in-place sequence multiplication so missed that.

    I would say Seq, UnknownSeq, MutableSeq etc are all sequence types, even if not using collections.Sequence which is comparatively new in Python.

    Do you fancy working on this with a pull request?

  6. chris-rands commented on May 23, 2018

    @chris-rands
    Contributor

    I would be happy to work on a pull request, but I'm fairly busy right now, can you wait a few weeks or more for this?

  7. peterjc commented on May 23, 2018

    @peterjc
    MemberAuthor

    That'd be great. This is not urgent, I filed this issue in part as a public TODO note for myself, but also in case someone else wanted to tackle it.

    Note also we'd want to test this with the BioSQL subclasses - I have some thoughts on that, but in the first instance raising a not implemented exception might be simplest.

  8. peterjc commented on Jun 1, 2018

    @peterjc
    MemberAuthor

    Closed via #1669, thanks Chris!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Enhancementgood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions