Skip to content

Instantly share code, notes, and snippets.

@zkamvar
Created August 6, 2015 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zkamvar/95af8adaf1b3f01a4995 to your computer and use it in GitHub Desktop.
Save zkamvar/95af8adaf1b3f01a4995 to your computer and use it in GitHub Desktop.
Place microsatellites in the correct order for genind objects.
resort_microsat <- function(x){
# Gather all alleles, convert to numeric, and reorder
alls <- adegenet::alleles(x)
alln <- lapply(alls, as.numeric)
alln <- lapply(alln, order)
# Loop over the names and paste together the locus name and the sorted alleles.
alls <- lapply(names(alls), function(i) paste(i, alls[[i]][ alln[[i]] ], sep = "."))
# Convert to a vector an match the name to the column names of the data matrix.
cols <- unlist(alls, use.names = FALSE)
cols <- match(cols, colnames(tab(x)))
return(x[, cols])
}
@zkamvar
Copy link
Author

zkamvar commented Aug 6, 2015

Here's a small working demonstration

library("adegenet")
library("pegas")
myFile <- system.file("files/nancycats.gtx",package="adegenet")
cats <- import2genind(myFile)
## 
##  Converting data from GENETIX to a genind object... 
## 
## ...done.

This works:

data(nancycats)
hw.test(nancycats, B=0)
##           chi^2  df  Pr(chi^2 >)
## fca8  395.80006 120 0.000000e+00
## fca23 239.34221  55 0.000000e+00
## fca43 434.33397  45 0.000000e+00
## fca45  66.11849  36 1.622163e-03
## fca77 270.52066  66 0.000000e+00
## fca78 402.80002  28 0.000000e+00
## fca90 217.19836  66 0.000000e+00
## fca96 193.36764  66 1.965095e-14
## fca37 291.00731 153 1.209777e-10

This has problems:

hw.test(cats, B=0)
## Warning in O - E: longer object length is not a multiple of shorter object
## length

## Warning in (O - E)^2/E: longer object length is not a multiple of shorter
## object length

## Warning in O - E: longer object length is not a multiple of shorter object
## length

## Warning in (O - E)^2/E: longer object length is not a multiple of shorter
## object length

## Warning in O - E: longer object length is not a multiple of shorter object
## length

## Warning in (O - E)^2/E: longer object length is not a multiple of shorter
## object length

## Warning in O - E: longer object length is not a multiple of shorter object
## length

## Warning in (O - E)^2/E: longer object length is not a multiple of shorter
## object length

## Warning in O - E: longer object length is not a multiple of shorter object
## length

## Warning in (O - E)^2/E: longer object length is not a multiple of shorter
## object length

## Warning in O - E: longer object length is not a multiple of shorter object
## length

## Warning in (O - E)^2/E: longer object length is not a multiple of shorter
## object length

## Warning in O - E: longer object length is not a multiple of shorter object
## length

## Warning in (O - E)^2/E: longer object length is not a multiple of shorter
## object length

## Warning in O - E: longer object length is not a multiple of shorter object
## length

## Warning in (O - E)^2/E: longer object length is not a multiple of shorter
## object length

## Warning in O - E: longer object length is not a multiple of shorter object
## length

## Warning in (O - E)^2/E: longer object length is not a multiple of shorter
## object length

##             chi^2  df  Pr(chi^2 >)
## fca8  46504.45655 120 0.000000e+00
## fca23  2023.71668  55 0.000000e+00
## fca43  7957.23992  45 0.000000e+00
## fca45    93.25975  36 5.681616e-07
## fca77 21636.11122  66 0.000000e+00
## fca78  1072.03868  28 0.000000e+00
## fca90 11361.25571  66 0.000000e+00
## fca96   825.46134  66 0.000000e+00
## fca37  2283.46490 153 0.000000e+00

This fixes everything:

hw.test(resort_microsat(cats), B=0)
##           chi^2  df  Pr(chi^2 >)
## fca8  395.80006 120 0.000000e+00
## fca23 239.34221  55 0.000000e+00
## fca43 434.33397  45 0.000000e+00
## fca45  66.11849  36 1.622163e-03
## fca77 270.52066  66 0.000000e+00
## fca78 402.80002  28 0.000000e+00
## fca90 217.19836  66 0.000000e+00
## fca96 193.36764  66 1.965095e-14
## fca37 291.00731 153 1.209777e-10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment