[Biopython] Cannot make SeqFeature() comparable?
Joshua Klein
mobiusklein at gmail.com
Tue Jan 31 12:48:03 UTC 2017
The reason the original code snippet doesn’t seem to be working as expected
is that the cmp1 function is assigned to the __lt__ attribute of the
SeqFeature module, not the SeqFeature class, which is located at
SeqFeature.SeqFeature. When assigning to the class itself, not the module,
the new comparator function is called.
This sort of patching works differently for old-style and new-style
classes, having to do with how special methods are looked up. Old style
classes look up special methods on the instance, new style classes look
them up on the instance’s class.
On Tue, Jan 31, 2017 at 4:07 AM, Peter Cock <p.j.a.cock at googlemail.com>
wrote:
> 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
> _______________________________________________
> Biopython mailing list - Biopython at mailman.open-bio.org
> http://mailman.open-bio.org/mailman/listinfo/biopython
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.open-bio.org/pipermail/biopython/attachments/20170131/e624d956/attachment.html>
More information about the Biopython
mailing list