holtzy / GenMap-Comparator

An application to compare genetic maps with D3 & Shiny
21 stars 9 forks source link

sort chrom names in natural order #2

Open timflutre opened 6 years ago

timflutre commented 6 years ago

More info about "natural sort order" here.

This can be done via the mixedsortfunction in the gtools R package.

timflutre commented 6 years ago

The online comparator is currently down, but here is a quick example:

map <- structure(list(chr = c("chr1", "chr1", "chr1", "chr2", "chr2",
 "chr2", "chr10", "chr10", "chr10"), mrk = c("loc1", "loc2", "loc3",
 "loc4", "loc5", "loc6", "loc7", "loc8", "loc9"), dist = c(2,
 4, 7, 1, 5, 12, 2, 5, 6)), .Names = c("chr", "mrk", "dist"), row.names = c(NA,
 -9L), class = "data.frame")

With built-in sort, chr10is before chr2:

map[order(map$chr),]
     chr  mrk dist
 1  chr1 loc1    2
 2  chr1 loc2    4
 3  chr1 loc3    7
 7 chr10 loc7    2
 8 chr10 loc8    5
 9 chr10 loc9    6
 4  chr2 loc4    1
 5  chr2 loc5    5
 6  chr2 loc6   12

With gtools' mixedsort, chr10is after chr2:

map[gtools::mixedorder(map$chr),]
     chr  mrk dist
 1  chr1 loc1    2
 2  chr1 loc2    4
 3  chr1 loc3    7
 4  chr2 loc4    1
 5  chr2 loc5    5
 6  chr2 loc6   12
 7 chr10 loc7    2
 8 chr10 loc8    5
 9 chr10 loc9    6