You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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
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:
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.
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.
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?
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?
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."
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.
@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).
@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.
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.
@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.
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.
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
@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?
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.
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.
Activity
mdehoon commentedon Jun 2, 2018
This issue was also reported to the mailing list on 26 May 2018.
http://mailman.open-bio.org/pipermail/biopython/2018-June/016446.html
peterjc commentedon Jun 4, 2018
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).mdehoon commentedon Jun 4, 2018
@peterjc
Does that work now?
peterjc commentedon Jun 4, 2018
Your example is missing the relevant alphabet import (the error is confusing since there is a reference to
Alphabetvia the bad-style import star, but it is the module not the class). This is much clearer:So, the current code does work for eval/repr, provided the expected imports are in place:
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:peterjc commentedon Jun 4, 2018
This change isn't too invasive, pull request coming up for you to comment on / review.
mdehoon commentedon Jun 4, 2018
@peterjc eval/repr still only works for very short sequences:
peterjc commentedon Jun 4, 2018
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:And Python 3.6 is similar:
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.
mdehoon commentedon Jun 5, 2018
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 obeyeval(repr(object)) == objectif the sequence itself (which is much more important) doesn't, at least not in general.Useful for what?
peterjc commentedon Jun 5, 2018
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?
peterjc commentedon Jun 5, 2018
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?
peterjc commentedon Jun 5, 2018
I also commented on the mailing list
http://mailman.open-bio.org/pipermail/biopython/2018-June/016450.html
MarkusPiotrowski commentedon Jun 5, 2018
Actually, I'm not in favor of this change. A
seqobject 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.
mdehoon commentedon Jun 5, 2018
@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).
mdehoon commentedon Jun 5, 2018
@peterjc If I may quote two more lines from the Zen of Python:
MarkusPiotrowski commentedon Jun 5, 2018
How about a public attribute
seq(instead of_data) so that one can usemy_seq.seq?mdehoon commentedon Jun 5, 2018
@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.peterjc commentedon Jun 5, 2018
Also
MutableSeqcan't subclass string. Nevertheless, we're making slow progress with making them [update - all theSeqclasses] more string like over time, e.g. Biopython 1.72 will include string-likeSeqmultiplication support.Some string methods like
.replace(...)will be much more complicated to implement in an alphabet aware way.peterjc commentedon Jun 5, 2018
@MarkusPiotrowski a public
.seqattribute being the sequence as a plain string? That would be going back to the original model where.dataserved that role, dropping in favour of making theSeqobject much more string-like in itself and recommendingstr(my_seq)if you really need a string.peterjc commentedon Jun 14, 2018
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.
mmokrejs commentedon Jun 14, 2018
So far I always do
eventually with .lower() appended. However, I would be happy to do
instead.
peterjc commentedon Jun 14, 2018
@mmokrejs Yes, you can already do that:
Although I would recommend the Python 3 ready version:
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)overseq_object.tostr()which was deprecated 6 years ago in Biopython 1.64 - it should be triggering a warning?cab9b20
Did you have any comments on the alphabet in the Seq-object representation, currently:
With this pull request that would become:
mmokrejs commentedon Jun 14, 2018
@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.
I do not mind as I do not use it.
peterjc commentedon Jun 14, 2018
Regarding the warning, perhaps something else is hiding it, e.g.
Or via the
PYTHONWARNINGSenvironment variable,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.
mmokrejs commentedon Jun 15, 2018
@peterjc
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.
peterjc commentedon Jun 20, 2018
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.peterjc commentedon Jul 29, 2020
Closed by #2953.