Question: Is There A Way To Sort A Biopython Alignment By A Feature Other Then Id?
0
gravatar for Burke
6.4 years ago by
Burke270
Bethesda, MD
Burke270 wrote:

I would like to know if there is there a way to sort a biopython alignment by a feature other then ID? In the biopython cookbook (http://www.bio-cloud.info/Biopython/en/ch6.html) there is a way to sort the alignment by ID. Is it possible to sort the sequence records in the alignment by another feature such as description or name?

ADD COMMENTlink modified 6.4 years ago by Peter5.7k • written 6.4 years ago by Burke270
3

Note the official tutorial is http://biopython.org/DIST/docs/tutorial/Tutorial.html - what you are looking at is an out of dae experimental reformatting.

ADD REPLYlink written 6.4 years ago by Peter5.7k
5
gravatar for Damian Kao
6.4 years ago by
Damian Kao15k
USA
Damian Kao15k wrote:

I am not sure if the alignment object is same as a python iterable array. You can try:

myAligment.sort(key = lambda x : x[column you want to sort by])

If that doesn't work, you can always restructure your alignment into an array of tuples:

newArray = [(x[column you want to sorty by], x) for x in myAlignment]

Then sort by the first element of the tuple which contain data of the column you want to sorty by:

newArray.sort(key = lambda x : x[0])
ADD COMMENTlink written 6.4 years ago by Damian Kao15k

DK: Thanks for the reply! While I get an error with the first portion ("TypeError: sort() got an unexpected keyword argument 'key'") the second option seems to be working fine.

ADD REPLYlink modified 6.4 years ago • written 6.4 years ago by Burke270
3
gravatar for Peter
6.4 years ago by
Peter5.7k
Scotland, UK
Peter5.7k wrote:

In Biopython 1.60 or later (due to be released soon), you can give the alignment's sort method a key function argument.

ADD COMMENTlink written 6.4 years ago by Peter5.7k

Thanks Peter! I appreciate it and I think it will be very helpful in the future when other look at this question.

ADD REPLYlink written 6.2 years ago by Burke270
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1301 users visited in the last hour