Skip to content

Hiding alphabets #1674

Closed
Closed

Description

@mdehoon

Seq objects show both their sequence content and the alphabet associated with them.
For example, the first example in our Biopython Tutorial & Cookbook starts as follows:

>>> from Bio.Seq import Seq
>>> my_seq = Seq("AGTACACTGGT")
>>> my_seq
Seq('AGTACACTGGT', Alphabet())

I don't think we need to show the alphabet here. It takes up screen space, and oftentimes it's uninformative (as in the example above); the other examples in the same section of the tutorial show SingleLetterAlphabet and IUPACAmbiguousDNA. Even in the latter case, I don't think users need to be reminded every time that they are dealing with DNA.

Perhaps more importantly, this is confusing for new users, who may wonder what alphabets are and why we use them. I don't have a good answer to that question.

Therefore I suggest to change the repr function of Seq objects to show the sequence only. The example above would then show

>>> from Bio.Seq import Seq
>>> my_seq = Seq("AGTACACTGGT")
>>> my_seq
Seq('AGTACACTGGT')

Then the section on alphabets in the Tutorial can move to the end of the chapter, for people who actually want to use Alphabets.

For each sequence object, the alphabet would still be accessible as the attribute to the Seq object:

>>> my_seq.alphabet
Alphabet()

Activity

  1. mdehoon commented on Jun 2, 2018

    @mdehoon
    ContributorAuthor
  2. peterjc commented on Jun 4, 2018

    @peterjc
    Member

    As a compromise, how about hiding the alphabet in the __repr__ if it is the default alphabet?

    That also maintains the design expectation in Python that eval(repr(my_seq)) == my_seq (although in practise this only works for simpler objects).

  3. mdehoon commented on Jun 4, 2018

    @mdehoon
    ContributorAuthor

    @peterjc

    That also maintains the design expectation in Python that eval(repr(my_seq)) == my_seq (although in practise this only works for simpler objects).

    Does that work now?

    >>> from Bio.Seq import *
    >>> s = Seq("ACGT")
    >>> eval(repr(s))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line 1, in <module>
    TypeError: 'module' object is not callable
  4. peterjc commented on Jun 4, 2018

    @peterjc
    Member

    Your example is missing the relevant alphabet import (the error is confusing since there is a reference to Alphabet via the bad-style import star, but it is the module not the class). This is much clearer:

    >>> from Bio.Seq import Seq
    >>> s = Seq("ACGT")
    >>> eval(repr(s))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line 1, in <module>
    NameError: name 'Alphabet' is not defined
    

    So, the current code does work for eval/repr, provided the expected imports are in place:

    >>> from Bio.Seq import Seq
    >>> from Bio.Alphabet import Alphabet
    >>> s = Seq("ACGT")
    >>> eval(repr(s))
    Seq('ACGT', Alphabet())

    If we change the __repr__ to omit the default alphabet, it becomes simpler as you won't need the alphabet import in this eval/repr round trip:

    >>> from Bio.Seq import Seq
    >>> eval("Seq('ACGT')")
    Seq('ACGT', Alphabet())
  5. peterjc commented on Jun 4, 2018

    @peterjc
    Member

    This change isn't too invasive, pull request coming up for you to comment on / review.

  6. mdehoon commented on Jun 4, 2018

    @mdehoon
    ContributorAuthor

    @peterjc eval/repr still only works for very short sequences:

    >>> from Bio.Alphabet import Alphabet
    >>> from Bio.Seq import Seq
    >>> s = Seq("ACGT" * 20)
    >>> s
    Seq('ACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTAC...CGT', Alphabet())
    >>> eval(repr(s))
    Seq('ACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTACGTAC...CGT', Alphabet())
    >>> _ == s
    False
  7. peterjc commented on Jun 4, 2018

    @peterjc
    Member

    Yes, here we made a practical choice some time back to show the sequence truncated and include the last three bases. Long sequences are far more common than long lines of text as might be typically processed in Python, so this is important for keeping interactive use of the terminal and debugging logs practical (which I see as the two main use-cases for the repr(...) output).

    Quoting the built-in help / repr(...) docstring from Python 2.7:

    Help on built-in function repr in module __builtin__:
    
    repr(...)
        repr(object) -> string
        
        Return the canonical string representation of the object.
        For most object types, eval(repr(object)) == object.
    

    And Python 3.6 is similar:

    Help on built-in function repr in module builtins:
    
    repr(obj, /)
        Return the canonical string representation of the object.
        
        For many object types, including most builtins, eval(repr(obj)) == obj.
    

    I'm happy with hiding the default alphabet from the __repr__ (see #1676), meaning for short code examples which ignore the alphabet you don't get distracted by it.

    But for code which uses the alphabets as intended to at least distinguish DNA, RNA and proteins (and perhaps even more fine grained than that), it is useful to show it.

  8. mdehoon commented on Jun 5, 2018

    @mdehoon
    ContributorAuthor

    I completely agree that we should show truncated sequences if they are long, but that means breaking eval(repr(object)) == object, and then I don't see a reason why the alphabet should strictly obey eval(repr(object)) == object if the sequence itself (which is much more important) doesn't, at least not in general.

    But for code which uses the alphabets as intended to at least distinguish DNA, RNA and proteins (and perhaps even more fine grained than that), it is useful to show it.

    Useful for what?

  9. peterjc commented on Jun 5, 2018

    @peterjc
    Member

    I concede all the interactive use-cases are not very strong. e.g. Knowing before hand if the nucleotide or protein specific methods are going to work? But you can just try-it-and-see, and catch an exception there. Likewise knowing before hand if the gap character has been specified for calling un-gap?

    However, does it not seem more important for debugging purposes, including logging messages?

  10. peterjc commented on Jun 5, 2018

    @peterjc
    Member

    The documentation changes in particular would need a careful review, but #1676 shows how this could look (always hiding the alphabet in the Seq object __repr__ methods).

    If we go ahead with this we may want to go further and de-emphasise the alphabet even more in the Tutorial?

  11. peterjc commented on Jun 5, 2018

    @peterjc
    Member

    I also commented on the mailing list
    http://mailman.open-bio.org/pipermail/biopython/2018-June/016450.html

    Dear Biopythoneers,

    I know that Michiel has long expressed a wish to get rid of
    the current alphabet system in Biopython, and I agree that
    the historic design is overly complicated and often gets in
    the way - but we don't have any concrete proposals to replace
    it. Part of the problem here is coming up with a replacement
    with the least painful transition - and being practical the less
    people use the alphabets, the less trouble any changes would
    cause.

    The proposal here would de-emphasis the use of alphabets,
    reflecting the fact that for the vast majority of scripts and
    code you can just ignore them.

    There are still corner cases - for example, for some of the
    SeqIO output filetypes we currently need to use the Seq's
    alphabet to label the sequence type (RNA, DNA, protein).

    Still, overall I can see it being quite practical to downplay
    the alphabet objects in our user facing documentation,
    and hiding them in the Seq objects' __repr__ helps there.

    Is this a case where in the Zen of Python where practicality
    wins out over being explicit about what a sequence object
    contains?

    "Explicit is better than implicit.
    ...
    Although practicality beats purity."

    https://www.python.org/dev/peps/pep-0020/

    Thoughts and comments welcome here on on the issue,
    #1674

    Peter

  12. MarkusPiotrowski commented on Jun 5, 2018

    @MarkusPiotrowski
    Contributor

    Actually, I'm not in favor of this change. A seq object is not a simple string and an important difference is that it has an alphabet. Especially beginners should be made aware of this.

    I see the problem at the opposite end, that is that we do not make extensive use of alphabets, e.g. a strict checking that a sequence does only contain valid letters. I think this has been already discussed elsewhere. If we had this, we could easily check everywhere if we are dealing with DNA, RNA or protein.

  13. mdehoon commented on Jun 5, 2018

    @mdehoon
    ContributorAuthor

    @MarkusPiotrowski I agree, but that is a separate issue.
    In any case, if we were to validate against the alphabet, then I still don't want Python to remind me at every line if my sequence is a piece of DNA or protein (or a SingleLetterAlphabet).

  14. mdehoon commented on Jun 5, 2018

    @mdehoon
    ContributorAuthor

    @peterjc If I may quote two more lines from the Zen of Python:

    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    
  15. MarkusPiotrowski commented on Jun 5, 2018

    @MarkusPiotrowski
    Contributor

    How about a public attribute seq (instead of _data) so that one can use my_seq.seq?

  16. mdehoon commented on Jun 5, 2018

    @mdehoon
    ContributorAuthor

    @MarkusPiotrowski My other pet peeve is that Seq doesn't subclass from string; if it did, then we never had to use my_seq._data or my_seq.seq and we could just use my_seq as is. However, inheriting Seq from string would break UnknownSeq. I guess we cannot solve all problems at the same time.

  17. peterjc commented on Jun 5, 2018

    @peterjc
    Member

    Also MutableSeq can't subclass string. Nevertheless, we're making slow progress with making them [update - all the Seq classes] more string like over time, e.g. Biopython 1.72 will include string-like Seq multiplication support.

    Some string methods like .replace(...) will be much more complicated to implement in an alphabet aware way.

  18. peterjc commented on Jun 5, 2018

    @peterjc
    Member

    @MarkusPiotrowski a public .seq attribute being the sequence as a plain string? That would be going back to the original model where .data served that role, dropping in favour of making the Seq object much more string-like in itself and recommending str(my_seq) if you really need a string.

  19. peterjc commented on Jun 14, 2018

    @peterjc
    Member

    Given we are not in full agreement about completely removing the alphabet from the Seq objects' __repr__, I'm proposing for Biopython 1.72 to only hide the alphabet when the default is used.

    This is a small step in downplaying the alphabet, meaning people working with the objects interactively are less bothered by them if they don't care about the alphabet.

    If there are no objections, I would like to apply #1687 this week for inclusion in Biopython 1.72.

  20. mmokrejs commented on Jun 14, 2018

    @mmokrejs

    So far I always do

    print "Blah blah %s blah %s" % (record.seq.tostring(), foo)
    

    eventually with .lower() appended. However, I would be happy to do

    print "Blah blah %s blah %s" % (record.seq, foo)
    

    instead.

  21. peterjc commented on Jun 14, 2018

    @peterjc
    Member

    @mmokrejs Yes, you can already do that:

    print "Blah blah %s blah %s" % (record.seq, foo)

    Although I would recommend the Python 3 ready version:

    print("Blah blah %s blah %s" % (record.seq, foo))

    Or, if you prefer, the new Python string format command. All of those work because str(seq_object) gives the sequence as a plain string.

    Incidentally, we've long recommended str(seq_object) over seq_object.tostr() which was deprecated 6 years ago in Biopython 1.64 - it should be triggering a warning?
    cab9b20

    >>> from Bio.Seq import Seq
    >>> print(Seq("ACGT").tostring())
    Bio/Seq.py:407: BiopythonDeprecationWarning: This method is obsolete; please use str(my_seq) instead of my_seq.tostring().
      BiopythonDeprecationWarning)
    ACGT

    Did you have any comments on the alphabet in the Seq-object representation, currently:

    >>> from Bio.Seq import Seq
    >>> Seq("ACGT")
    Seq('ACGT', Alphabet())

    With this pull request that would become:

    >>> from Bio.Seq import Seq
    >>> Seq("ACGT")
    Seq('ACGT')
  22. mmokrejs commented on Jun 14, 2018

    @mmokrejs

    @peterjc Hmm, I am at 1.68 version on those machines so that maybe explains why it is not showing in my STDERR logs. I hope it will not be flooded by them in future, one is enough. I don't have time/will to rewrite the syntax of my code nor to do py2to3 conversion.

    Did you have any comments on the alphabet in the Seq-object representation?

    I do not mind as I do not use it.

  23. peterjc commented on Jun 14, 2018

    @peterjc
    Member

    Regarding the warning, perhaps something else is hiding it, e.g.

    $ python -W ignore -c 'from Bio.Seq import Seq; print(Seq("ACGT").tostring())'
    ACGT
    

    Or via the PYTHONWARNINGS environment variable,

    $ PYTHONWARNINGS=ignore python -c 'from Bio.Seq import Seq; print(Seq("ACGT").tostring())'
    ACGT
    

    Note that with the default warning settings you would see the message and once and only once.

    Anyway, thank you for your comments on the propsed Seq-object representation changes.

  24. mmokrejs commented on Jun 15, 2018

    @mmokrejs

    @peterjc

    Regarding the warning, perhaps something else is hiding it, e.g.

    I went to check the lines I found by grep yesterday ... well, the matches were in comment sections so practically they were not executable. Sorry for the noise.

  25. peterjc commented on Jun 20, 2018

    @peterjc
    Member

    OK, I've merged #1687 ready for Biopython 1.72, which only hides the alphabet if it is the default value.

    This might be the first step to removing or radically reworking the alphabets, but as it stands, this is still a useful simplification to the repr(...) output which does not hide anything important.

  26. peterjc commented on Jul 29, 2020

    @peterjc
    Member

    Closed by #2953.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions