[Biopython] Cannot make SeqFeature() comparable?

Peter Cock p.j.a.cock at googlemail.com
Tue Jan 31 09:07:50 UTC 2017


Hi Bastien,

I'm not immediately sure if "monkey patching" the class
methods at run time like that would work in principle.
If you insert a print into it, it does not seem to be invoked.

It might be worth trying a modified Biopython, or an
explicit subclass to narrow down where this breaks.

Or more simply, can you just do the start position
comparison explicitly if that's what you want to use?

f1.location.start < f2.location.start

Peter


On Mon, Jan 30, 2017 at 11:05 PM, Chevreux, Bastien
<bastien.chevreux at dsm.com> wrote:
> Hi there,
>
>
>
> I have a problem making the SeqFeature() class comparable by providing a
> __lt__ function. Consider the following:
>
>
>
> ------------------------------------------------------------------
>
> #!/usr/bin/env python3
>
>
>
> from Bio import SeqFeature
>
>
>
> def cmp1(this,other):
>
>     return int(this.location.start) < int(other.location.start);
>
>
>
> SeqFeature.__lt__=cmp1;
>
> f1 = SeqFeature.SeqFeature(SeqFeature.FeatureLocation(10, 200));
>
> f2 = SeqFeature.SeqFeature(SeqFeature.FeatureLocation(1000, 1200));
>
>
>
> if f1<f2:
>
>     print("f1<f2");
>
> else:
>
>     print("nope, f1>=f2");
>
> ------------------------------------------------------------------
>
>
>
> The code above runs with an error message:
>
>     if f1<f2:
>
> TypeError: unorderable types: SeqFeature() < SeqFeature()
>
>
>
> What I do not understand is that this should be the canonical recipe for
> making any class comparable via LT operator. Compare to the following code
> which runs without problems:
>
>
>
> ------------------------------------------------------------------
>
> #!/usr/bin/env python3
>
>
>
> class myclass():
>
>     def __init__(self, value):
>
>         self.bla=value;
>
>
>
> def cmp2(this,other):
>
>     return this.bla < other.bla;
>
>
>
> myclass.__lt__=cmp2;
>
> m1=myclass(1);
>
> m2=myclass(2);
>
>
>
> if m1<m2:
>
>     print("m1<m2");
>
> else:
>
>     print("nope, m1>=m2");
>
> ------------------------------------------------------------------
>
>
>
> What am I missing?
>
>
>
> Best,
>
>   Bastien
>
>
>
> --
> DSM Nutritional Products Microbia Inc | Bioinformatics
> 60 Westview Street | Lexington, MA 02421 | United States
> Phone +1 781 259 7613 | Fax +1 781 259 0615
>
>
>
>
> ________________________________
>
> DISCLAIMER:
> This e-mail is for the intended recipient only.
> If you have received it by mistake please let us know by reply and then
> delete it from your system; access, disclosure, copying, distribution or
> reliance on any of it by anyone else is prohibited.
> If you as intended recipient have received this e-mail incorrectly, please
> notify the sender (via e-mail) immediately.
>
> _______________________________________________
> Biopython mailing list  -  Biopython at mailman.open-bio.org
> http://mailman.open-bio.org/mailman/listinfo/biopython


More information about the Biopython mailing list