Is There A Way To Sort A Biopython Alignment By A Feature Other Then Id?
2
0
Entering edit mode
12.4 years ago
Burke ▴ 290

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?

alignment python biopython • 4.5k views
ADD COMMENT
3
Entering edit mode

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 REPLY
5
Entering edit mode
12.4 years ago

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 COMMENT
0
Entering edit mode

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 REPLY
3
Entering edit mode
12.4 years ago
Peter 6.0k

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

ADD COMMENT
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 1712 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6