<div dir="ltr">re: Joshua's post about the key argument, here is an example of sorting SeqFeatures by location start without (potentially error-prone) monkey patching:<div><br></div><div>import operator<br><div>sorted_features = sorted([f1, f2], key=operator.attrgetter("location.start"))</div></div><div><br></div><div><a href="https://docs.python.org/2/library/operator.html#operator.attrgetter">https://docs.python.org/2/library/operator.html#operator.attrgetter</a> <br></div><div><br></div><div>Cheers,</div><div><br></div><div>Lenna</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jan 31, 2017 at 9:31 AM, Chevreux, Bastien <span dir="ltr"><<a href="mailto:bastien.chevreux@dsm.com" target="_blank">bastien.chevreux@dsm.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div lang="EN-GB" link="blue" vlink="purple">
<div class="m_6537710654685219263WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">> From: Joshua Klein [mailto:<a href="mailto:mobiusklein@gmail.com" target="_blank">mobiusklein@gmail.com</a>]
<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">> […] When assigning to the class itself, not the module, the new<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">> comparator function is called<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">Yay, that worked, learning something new every day. Thanks a million.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">Peter: the ultimate goal of that request was to be able to call sort() on features, with sometimes different and very custom sort criteria. Nothing which would
fit BioPython really.<u></u><u></u></span></p><span class="">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">Best,<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"> Bastien<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Calibri",sans-serif;color:#008fd5">--
<br>
DSM Nutritional Products Microbia Inc | Bioinformatics<br>
60 Westview Street | Lexington, MA 02421 | United States<br>
Phone +1 781 259 7613 | Fax +1 781 259 0615</span><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u><u></u></span></p>
</div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
</span><div>
<div style="border:none;border-top:solid #e1e1e1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif"> Joshua Klein [mailto:<a href="mailto:mobiusklein@gmail.com" target="_blank">mobiusklein@gmail.com</a>]
<br>
<b>Sent:</b> Tuesday, January 31, 2017 7:48 AM<br>
<b>To:</b> Peter Cock <<a href="mailto:p.j.a.cock@googlemail.com" target="_blank">p.j.a.cock@googlemail.com</a>><br>
<b>Cc:</b> Chevreux, Bastien <<a href="mailto:bastien.chevreux@dsm.com" target="_blank">bastien.chevreux@dsm.com</a>>; <a href="mailto:biopython@biopython.org" target="_blank">biopython@biopython.org</a><br>
<b>Subject:</b> Re: [Biopython] Cannot make SeqFeature() comparable?<u></u><u></u></span></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal" style="margin-bottom:12.0pt">--- This mail has been sent from an external source ---
<u></u><u></u></p><div><div class="h5">
<div>
<div>
<div>
<p style="margin:1.2em!important">The reason the original code snippet doesn’t seem to be working as expected is that the
<code><span style="font-size:10.0pt;font-family:Consolas;border:solid #eaeaea 1.0pt;padding:0in;background:#f8f8f8">cmp1</span></code> function is assigned to the
<code><span style="font-size:10.0pt;font-family:Consolas;border:solid #eaeaea 1.0pt;padding:0in;background:#f8f8f8">__lt__</span></code> attribute of the
<code><span style="font-size:10.0pt;font-family:Consolas;border:solid #eaeaea 1.0pt;padding:0in;background:#f8f8f8">SeqFeature</span></code> module, not the
<code><span style="font-size:10.0pt;font-family:Consolas;border:solid #eaeaea 1.0pt;padding:0in;background:#f8f8f8">SeqFeature</span></code> class, which is located at
<code><span style="font-size:10.0pt;font-family:Consolas;border:solid #eaeaea 1.0pt;padding:0in;background:#f8f8f8">SeqFeature.SeqFeature</span></code>. When assigning to the class itself, not the module, the new comparator function is called.<u></u><u></u></p>
<p style="margin:1.2em!important">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.<u></u><u></u></p>
<div>
<p class="MsoNormal"><span style="font-size:1.0pt"><u></u><u></u></span></p>
</div>
</div>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">On Tue, Jan 31, 2017 at 4:07 AM, Peter Cock <<a href="mailto:p.j.a.cock@googlemail.com" target="_blank">p.j.a.cock@googlemail.com</a>> wrote:<u></u><u></u></p>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<p class="MsoNormal">Hi Bastien,<br>
<br>
I'm not immediately sure if "monkey patching" the class<br>
methods at run time like that would work in principle.<br>
If you insert a print into it, it does not seem to be invoked.<br>
<br>
It might be worth trying a modified Biopython, or an<br>
explicit subclass to narrow down where this breaks.<br>
<br>
Or more simply, can you just do the start position<br>
comparison explicitly if that's what you want to use?<br>
<br>
f1.location.start < f2.location.start<br>
<br>
Peter<u></u><u></u></p>
<div>
<div>
<p class="MsoNormal"><br>
<br>
On Mon, Jan 30, 2017 at 11:05 PM, Chevreux, Bastien<br>
<<a href="mailto:bastien.chevreux@dsm.com" target="_blank">bastien.chevreux@dsm.com</a>> wrote:<br>
> Hi there,<br>
><br>
><br>
><br>
> I have a problem making the SeqFeature() class comparable by providing a<br>
> __lt__ function. Consider the following:<br>
><br>
><br>
><br>
> ------------------------------<wbr>------------------------------<wbr>------<br>
><br>
> #!/usr/bin/env python3<br>
><br>
><br>
><br>
> from Bio import SeqFeature<br>
><br>
><br>
><br>
> def cmp1(this,other):<br>
><br>
> return int(this.location.start) < int(other.location.start);<br>
><br>
><br>
><br>
> SeqFeature.__lt__=cmp1;<br>
><br>
> f1 = SeqFeature.SeqFeature(<wbr>SeqFeature.FeatureLocation(10, 200));<br>
><br>
> f2 = SeqFeature.SeqFeature(<wbr>SeqFeature.FeatureLocation(<wbr>1000, 1200));<br>
><br>
><br>
><br>
> if f1<f2:<br>
><br>
> print("f1<f2");<br>
><br>
> else:<br>
><br>
> print("nope, f1>=f2");<br>
><br>
> ------------------------------<wbr>------------------------------<wbr>------<br>
><br>
><br>
><br>
> The code above runs with an error message:<br>
><br>
> if f1<f2:<br>
><br>
> TypeError: unorderable types: SeqFeature() < SeqFeature()<br>
><br>
><br>
><br>
> What I do not understand is that this should be the canonical recipe for<br>
> making any class comparable via LT operator. Compare to the following code<br>
> which runs without problems:<br>
><br>
><br>
><br>
> ------------------------------<wbr>------------------------------<wbr>------<br>
><br>
> #!/usr/bin/env python3<br>
><br>
><br>
><br>
> class myclass():<br>
><br>
> def __init__(self, value):<br>
><br>
> self.bla=value;<br>
><br>
><br>
><br>
> def cmp2(this,other):<br>
><br>
> return this.bla < other.bla;<br>
><br>
><br>
><br>
> myclass.__lt__=cmp2;<br>
><br>
> m1=myclass(1);<br>
><br>
> m2=myclass(2);<br>
><br>
><br>
><br>
> if m1<m2:<br>
><br>
> print("m1<m2");<br>
><br>
> else:<br>
><br>
> print("nope, m1>=m2");<br>
><br>
> ------------------------------<wbr>------------------------------<wbr>------<br>
><br>
><br>
><br>
> What am I missing?<br>
><br>
><br>
><br>
> Best,<br>
><br>
> Bastien<br>
><br>
><br>
><br>
> --<br>
> DSM Nutritional Products Microbia Inc | Bioinformatics<br>
> 60 Westview Street | Lexington, MA 02421 | United States<br>
> Phone +1 781 259 7613 | Fax +1 781 259 0615<br>
><br>
><br>
><br>
><br>
> ______________________________<wbr>__<br>
><br>
> DISCLAIMER:<br>
> This e-mail is for the intended recipient only.<br>
> If you have received it by mistake please let us know by reply and then<br>
> delete it from your system; access, disclosure, copying, distribution or<br>
> reliance on any of it by anyone else is prohibited.<br>
> If you as intended recipient have received this e-mail incorrectly, please<br>
> notify the sender (via e-mail) immediately.<br>
><u></u><u></u></p>
</div>
</div>
<p class="MsoNormal">> ______________________________<wbr>_________________<br>
> Biopython mailing list - <a href="mailto:Biopython@mailman.open-bio.org" target="_blank">Biopython@mailman.open-bio.org</a><br>
> <a href="http://mailman.open-bio.org/mailman/listinfo/biopython" target="_blank">
http://mailman.open-bio.org/<wbr>mailman/listinfo/biopython</a><br>
______________________________<wbr>_________________<br>
Biopython mailing list - <a href="mailto:Biopython@mailman.open-bio.org" target="_blank">Biopython@mailman.open-bio.org</a><br>
<a href="http://mailman.open-bio.org/mailman/listinfo/biopython" target="_blank">http://mailman.open-bio.org/<wbr>mailman/listinfo/biopython</a><u></u><u></u></p>
</blockquote>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
</div></div></div>
</div>
<br>______________________________<wbr>_________________<br>
Biopython mailing list - <a href="mailto:Biopython@mailman.open-bio.org">Biopython@mailman.open-bio.org</a><br>
<a href="http://mailman.open-bio.org/mailman/listinfo/biopython" rel="noreferrer" target="_blank">http://mailman.open-bio.org/<wbr>mailman/listinfo/biopython</a><br></blockquote></div><br></div>