Sorting a list of chromosomes in the correct order

Question:

A seemingly simple problem, but one that’s proving a bit vexing. I have a list of chromosomes (there are 23 chromosome – chromosomes 1 to 21, then chromosome X and chromosome Y) like so:

['chr11','chr14','chr16','chr13','chr4','chr13','chr2','chr1','chr2','chr3','chr14','chrX',]

I would like to sort this in the following order :

['chr1', 'chr2','chr2','chr3','chr4','chr11','chr13','chr13', 'chr14','chr14','chr16','chrX']

However, due to the lexicographical nature of python’s sort it will sort chr1, chr10, chr11, chr12...chr2, etc. as I have chromosome X, sorting by their integer values also doesn’t seem like an option. would I potentially have to specify a unique key by which to sort the list? Or is there some sort of obvious solution I’m missing.

Asked By: KLM117

||

Answers:

You can use natsorted, what you want is natural sorting after all 😉

l = ['chr11','chr14','chr16','chr13','chr4','chr13','chr2',
     'chr1','chr2','chr3','chr14','chrX','chrY']

from natsort import natsorted

out = natsorted(l)

output:

['chr1', 'chr2', 'chr2', 'chr3', 'chr4', 'chr11', 'chr13',
 'chr13', 'chr14', 'chr14', 'chr16', 'chrX', 'chrY']
Answered By: mozway

You can create a custom key:

key={s:i for i,s in 
    enumerate([f'chr{x}' for x in list(range(1,22))+['X','Y']],1)}

>>> key
{'chr1': 1, 'chr2': 2, 'chr3': 3, 'chr4': 4, 'chr5': 5, 'chr6': 6, 'chr7': 7, 'chr8': 8, 'chr9': 9, 'chr10': 10, 'chr11': 11, 'chr12': 12, 'chr13': 13, 'chr14': 14, 'chr15': 15, 'chr16': 16, 'chr17': 17, 'chr18': 18, 'chr19': 19, 'chr20': 20, 'chr21': 21, 'chrX': 22, 'chrY': 23}

Then use that key as a lookup in sorted:

li = ['chr11','chr14','chr16','chr13','chr4','chr13','chr2',
     'chr1','chr2','chr3','chr14','chrX','chrY']

>>> sorted(li, key=lambda s: key[s])
['chr1', 'chr2', 'chr2', 'chr3', 'chr4', 'chr11', 'chr13', 'chr13', 'chr14', 'chr14', 'chr16', 'chrX', 'chrY']

Alternatively, you can do a natural sort with a regex to parse out the digits:

import re

sli=sorted(li, key=lambda e: 
             [int(s) if s.isdigit() else s for s in re.findall(r'd+|D+', e)])

>>> sli
['chr1', 'chr2', 'chr2', 'chr3', 'chr4', 'chr11', 'chr13', 'chr13', 'chr14', 'chr14', 'chr16', 'chrX', 'chrY']

The custom key would be significantly faster — use that if you have billions to sort.

Answered By: dawg

natsort as already mentioned by @mozway is the fastest way.

Here the solution without using external libraries.

sorted(l, key=lambda x: int(val) if (val:=x[3:]).isnumeric() else ord(val))

It gives the same output.

Answered By: Glauco

You could try to replace X and Y for 22 and 23 respectly inside a lambda function that then replace the char values for nothing and then use only the int part of the string to sort the list

l = ['chr1', 'chr2','chr2','chr3','chr4','chr11','chr13','chr13', 'chr14','chr14','chr16','chrX']

sorted( l, key= lambda x: int(x.replace('X','22').replace('Y','23').replace('chr','')))

# OUTPUT
['chr1', 'chr2', 'chr2', 'chr3', 'chr4', 'chr11', 'chr13', 'chr13', 'chr14', 'chr14', 'chr16','chrX']
Answered By: Matias Bertani
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.