This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Problems with LD.decay() function from sommer R package

Edit/Update: The reason for my problems was that my data (GBS, non-imputed) contained missing data and the function can't handle missing data.

Has anyone successfully used the LD.decay() function in the sommer R package? I'm having more difficulty than I think I should and am not sure why. I have (or had) some suspicions (missing data, chromosome IDs not being recognized), but so far nothing has has resolved the problem. Any help or advice you might be able to provide would be greatly appreciated!

Preview of genotype data This is in 0,1,2 format, but I understand that the sommer R package can convert it for us? In any case, when I tried converting to -1,0,1 format that did not help. (Note In the genotype object name, "t" stands for transposed and "m" stands for matrix.)

> data_t_m[1:5,1:5]
             SNP_1 SNP_2 SNP_3 SNP_4 SNP_5
Individual_1     0     0     1     1     0
Individual_2     0     0     1     1     0
Individual_3     0     0     1     1     0
Individual_4     0     0     1     1     0
Individual_5     0     0     1     1     0

Preview of map data (in data.table format)

          Locus        LG Position
    1:     SNP_1 ZPchr0001     9373
    2:     SNP_2 ZPchr0001     9433
    3:     SNP_3 ZPchr0001    22742
    4:     SNP_4 ZPchr0001    22772
    5:     SNP_5 ZPchr0001    22811
   ---
54743: SNP_54743 ZPchr0458  4232791
54744: SNP_54744 ZPchr0458  4232815
54745: SNP_54745 ZPchr0458  4232866
54746: SNP_54746 ZPchr0458  4232878
54747: SNP_54747 ZPchr0458  4232988

When I run the command (which should work?) LD.decay(data_t_m, map_data),

I get output, but it looks like this (truncated, only the first 3 chromosomes/LGs are shown):

 |======================================================================|100%
$by.LG
$by.LG[[1]]
[1] NA

$by.LG$ZPchr0001
[1] d  r2 p
<0 rows> (or 0-length row.names)

$by.LG$ZPchr0002
[1] d  r2 p
<0 rows> (or 0-length row.names)

$by.LG$ZPchr0003
[1] d  r2 p
<0 rows> (or 0-length row.names)

When I try another approach (adding unlinked = TRUE), I get an error message:

Input

LD.decay(data_t_m, map_data, unlinked = TRUE, gamma = 0.95)

Output error message (perhaps linked to my attempt to convert to -1,0,1 format?

error in evaluating the argument 'x' in selecting a method for function 'as.matrix': incorrect number of dimensions

I don't understand this because based on the sample data provided by sommer, I think my genotype matrix and map data agree with each other (e.g., the dimensions-specifically the number of SNPs):

> dim(data_t_m)
[1]   961 54747
> dim(map_data)
[1] 54747     3

However, when I don't try to convert to -1,0,1 format myself, I get different results:

  |======================================================================| 100%
$by.LG
 ZPchr0001  ZPchr0002  ZPchr0003  ZPchr0004  ZPchr0005  ZPchr0006  ZPchr0007
0.03734856         NA         NA         NA         NA         NA         NA
 ZPchr0008  ZPchr0009  ZPchr0010  ZPchr0011  ZPchr0012  ZPchr0013  ZPchr0014
        NA         NA         NA         NA         NA         NA         NA
 ZPchr0015  ZPchr0016  ZPchr0458
        NA         NA         NA

$all.LG
[1] NA

Obviously these are also not great results... Has anyone else had a similar experience? How did you solve it?

Session Info

> sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Matrix products: default
BLAS:   /panfs/roc/msisoft/R/4.1.0/lib64/R/lib/libRblas.so
LAPACK: /panfs/roc/msisoft/R/4.1.0/lib64/R/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
 [1] sommer_4.1.5      crayon_1.4.2      lattice_0.20-44   MASS_7.3-54
 [5] Matrix_1.3-3      forcats_0.5.1     stringr_1.4.0     dplyr_1.0.7
 [9] purrr_0.3.4       readr_2.1.1       tidyr_1.1.4       tibble_3.1.6
[13] ggplot2_3.3.5     tidyverse_1.3.1   data.table_1.14.2

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8       cellranger_1.1.0 pillar_1.6.5     compiler_4.1.0
 [5] dbplyr_2.1.1     tools_4.1.0      jsonlite_1.7.2   lubridate_1.8.0
 [9] lifecycle_1.0.1  gtable_0.3.0     pkgconfig_2.0.3  rlang_1.0.0
[13] reprex_2.0.1     rstudioapi_0.13  DBI_1.1.2        cli_3.1.1
[17] haven_2.4.3      xml2_1.3.2       withr_2.4.3      httr_1.4.2
[21] fs_1.5.0         generics_0.1.1   vctrs_0.3.8      hms_1.1.1
[25] grid_4.1.0       tidyselect_1.1.1 glue_1.6.1       R6_2.5.1
[29] fansi_1.0.2      readxl_1.3.1     tzdb_0.2.0       modelr_0.1.8
[33] magrittr_2.0.2   backports_1.4.1  scales_1.1.1     ellipsis_0.3.2
[37] rvest_1.0.2      assertthat_0.2.1 colorspace_2.0-2 utf8_1.2.2
[41] stringi_1.7.6    munsell_0.5.0    broom_0.7.11
decay linkage disequilibrium sommer r ld

1 answer

Dear Matthew, you're not providing a minimal example to recreate the issue but looking at the structure of the dataset it seems that you just need to make sure that both the LG and Position columns are numeric and that you don't have missing data in the LG and Position columns. I think that should solve the issue.

Cheers, Eduardo

Hi Eduardo, Thank you for the quick response. I tried your suggestion to convert the map_data columns, but unfortunately it didn't resolve the issue.

I've also provided some additional details from how the analysis was conducted.

This is how I read in the data:

data <- fread("211202_gbs_nov_2021_snps_10percent_miss_dp6_maf05_no_row_or_col_names.csv")
# Transpose data so that columns are markers and rows are individuals
data_t <- t(data)
# Convert data to matrix
data_t_m <- as.matrix(data_t)

And how I set column/row names:

# Set column names
colnames(data_t_m) <- map_data$Locus

# Create vector of individual names (based on row number)
named_samples = vector()
for(i in c(1:nrow(data_t_m))){
named_samples <- append(named_samples, paste0("Individual_", i))
}

# Set rownames
rownames(data_t_m) <- named_samples

I thought that maybe the chromosome names having strings in them could be affecting things, so I changed them:

# Change LG column to numeric
map_data[, LG := sub("ZPchr", "", LG)]
map_data[, LG :=  as.numeric(map_data$LG)]
# Change Position column to numeric
map_data[, Position :=  as.numeric(map_data$Position)]

This succeeded in changing the data types to numeric:

> class(map_data$LG)
[1] "numeric"
> class(map_data$Position)
[1] "numeric"

But did not affect the outcome of the analysis. I still get the same failure that I got before. Could there be some other latent problem? I know there are missing genotypes in my genotype matrix (but not in the map data--I checked that). Could that be the problem? Or is it problematic that the data aren't in -1,0,1 format?

My first attempt involved trying to change the 0,1,2 format by converting the data.table to a tibble:

data <- as_tibble(data) # convert data.table to tibble in order to do the "find and replace"

# Convert to -1,0,1 format; requires tidyverse
#data <- data %>% mutate_if(is.character, str_replace_all, pattern = '0', replacement = '-1')
#data <- data %>% mutate_if(is.character, str_replace_all, pattern = '1', replacement = '0')
#data <- data %>% mutate_if(is.character, str_replace_all, pattern = '2', replacement = '1')

Note: This also involved using stringsAsFactors = TRUE in the fread() function.

Which also involved converting the data type back to numeric:

# Convert data type back to numeric/integer from character. I thought it would help solve the issue of empty results. I was wrong-it didn't solve the problem--but also didn't appear to cause any problems.
class(data_t_m) <- "numeric"

But this was also messy and I'm not sure it's necessary to convert to -1,0,1 format. I suppose I could have also done this find+replace in Excel, but there are so much data that it crashes.

Log in to answer this question.