<div dir="ltr">Hi Michiel,<div><br></div><div>I only just saw your email - Google has mistagged it as spam.</div><div><br></div><div>Multiple sequence alignments are also often viewed as arrays of characters, but critically there the row and column indices would typically be integers (or sequence names), never letters (amino acid or nucleotide).</div><div><br></div><div>I agree with you that subclassing NumPy arrays is tricky (certainly my impression from following the numpy mailing list), to the point that I wonder if we are better off avoiding it? You certainly make a good point that we'd be better off doing that once, rather than N times for these similar use cases. I take it there is no standard NumPy array subclass with the member-element-based indexing property you want to use?</div><div><br></div><div>Peter</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Jan 12, 2020 at 1:00 PM Michiel de Hoon <<a href="mailto:mjldehoon@yahoo.com">mjldehoon@yahoo.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div style="font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;font-size:10px"><div dir="ltr">Dear all,</div><div dir="ltr"><br></div><div dir="ltr">Currently there are four classes in Biopython that model an array where the letters can be used as indices:<br></div><div dir="ltr"><br></div><div dir="ltr">Bio.Align.substitution_matrices: <span>Array class </span><br></div><div dir="ltr"><span>Bio.Align.AlignInfo: <span>PSSM class</span><br></span></div><div dir="ltr"><span><span>Bio.Phylo.TreeConstruction: <span><span>_Matrix class</span></span><br></span></span></div><div dir="ltr"><span><span><span>Bio.motifs.matrix: <span><span><span><span>GenericPositionMatrix</span></span></span></span><br></span></span></span></div><div dir="ltr"><span><span><span><br></span></span></span></div><div dir="ltr"><span><span><span>(and the FreqTable class in </span></span></span>Bio.SubsMat.FreqTable is similar).<br></div><div dir="ltr"><span><span></span></span><div><br></div><span><span></span></span></div><div dir="ltr"><span><span><span>For example, the Array class in <span>Bio.Align.substitution_matrices allows you to do things like</span></span></span></span></div><div dir="ltr"><span><span><span><div dir="ltr"><div>>>> from Bio.Align.substitution_matrices import Array</div>>>> a = Array("ACGT", dims=2)<br><div dir="ltr"><div>>>> a<br>Array([[0., 0., 0., 0.],<br> [0., 0., 0., 0.],<br> [0., 0., 0., 0.],<br> [0., 0., 0., 0.]],<br> alphabet='ACGT')<br><br></div><div dir="ltr">>>> a['C','A'] = 6</div><div dir="ltr"><div dir="ltr">>>> a<br>Array([[0., 0., 0., 0.],<br> [6., 0., 0., 0.],<br> [0., 0., 0., 0.],<br> [0., 0., 0., 0.]],<br> alphabet='ACGT')<br><div dir="ltr">>>> sum(a['C'])<br>6.0<br>>>> a[3,'G'] = 1 <div dir="ltr">>>> a['A',:] = 4<br>>>> a<br>Array([[4., 4., 4., 4.],<br> [6., 0., 0., 0.],<br> [0., 0., 0., 0.],<br><div> [0., 0., 1., 0.]],</div><div> alphabet='ACGT')</div><div dir="ltr"><div>>>> sum(a[:, 'A'])<br>10.0</div><div dir="ltr">>>> <div dir="ltr"><div><span><span><span><span>>>> from numpy import sin</span></span></span></span></div><div><span><span><span><span></span></span></span></span>>>> sin(a)</div> <div>Array([[-0.7568025 , -0.7568025 , -0.7568025 , -0.7568025 ],<br> [-0.2794155 , 0. , 0. , 0. ],<br> [ 0. , 0. , 0. , 0. ],<br> [ 0. , 0. , 0.84147098, 0. ]],<br> alphabet='ACGT')<br><br></div><br></div><div><br></div></div></div><div dir="ltr">This class was implemented as a subclass of a numpy array. This has the big advantage that the array acts as a numpy array (e.g. you can apply numpy functions to it and get back an array of the same class, as in the example above), but unfortunately subclassing numpy arrays is not easy (see the code in Bio.Align.substitution_matrices).</div><div dir="ltr"><br></div><div dir="ltr">Would it then make sense to make this class available as a general-purpose array class where strings can be used as indices?</div><div dir="ltr">For example, inside a new Bio.math module.<br></div><div dir="ltr">Other modules in Biopython could then either make use of this class directly, or subclass it if needed.</div><div dir="ltr"><br></div><div dir="ltr">Thanks,</div><div dir="ltr">-Michiel<br></div></div></div></div></div></div></div></span></span></span><span><span><span></span></span></span></div><div dir="ltr"><br></div></div></div>_______________________________________________<br>
Biopython mailing list - <a href="mailto:Biopython@mailman.open-bio.org" target="_blank">Biopython@mailman.open-bio.org</a><br>
<a href="https://mailman.open-bio.org/mailman/listinfo/biopython" rel="noreferrer" target="_blank">https://mailman.open-bio.org/mailman/listinfo/biopython</a></blockquote></div>