2022-11-11

Find ancestor (R function)

We have a pedigree and want to find a sub-pedigree that lists the ancestors of some individuals. Here is an R function for this, but first, lets show an example:

library(package = "pedigreemm")
library(package = "graph")
library(package = "Rgraphviz")

ped <- data.frame( id = c(  1,   2,   3,   4,   5,   6,   7,   8,   9,  10),
                  fid = c( NA,  NA,   2,   2,   4,   2,   5,   5,  NA,   8),
                  mid = c( NA,  NA,   1,  NA,   3,   3,   6,   6,  NA,   9))
ped2 <- with(ped, pedigree(sire = fid, dam = mid, label = id))
g <- as(t(as(ped2, "sparseMatrix")), "graph")
plot(g)

Now the function

traceAncestors <- function(ids, ped, missing = NA) {
  # ids - a vector of individuals, possibly not unique
  # ped - data.frame of global pedigree with id, father, and mother columns
  
  # Take pedigree rows for ids
  sel <- ped[[1]] %in% ids
  ret <- ped[sel, ]
  # Find their parents (new ids)
  ids <- c(ped[[2]][sel], ped[[3]][sel])
  # ... that are unique and known
  ids <- unique(ids[!ids %in% missing])
  # ... that are not ids already
  ids <- ids[!ids %in% ret[[1]]]
  
  # Loop
  while (length(ids) > 0) {
    # Take pedigree rows for new ids
    sel <- ped[[1]] %in% ids
    ret <- rbind(ped[sel, ], ret)
    # Find their parents (new ids)
    ids <- c(ped[[2]][sel], ped[[3]][sel])
    # ... that are unique and known
    ids <- unique(ids[!ids %in% missing])
    # ... that are not ids already
    ids <- ids[!ids %in% ret[[1]]]
  }
  return(ret)
}

And a few examples

> traceAncestors(ids = 4, ped = ped)
  id fid mid
2  2  NA  NA
4  4   2  NA
> 
> traceAncestors(ids = 6, ped = ped)
  id fid mid
1  1  NA  NA
2  2  NA  NA
3  3   2   1
6  6   2   3
> 
> traceAncestors(ids = c(4, 6), ped = ped)
  id fid mid
1  1  NA  NA
2  2  NA  NA
3  3   2   1
4  4   2  NA
6  6   2   3
> 
> traceAncestors(ids = c(4, 6, 10), ped = ped)
   id fid mid
1   1  NA  NA
5   5   4   3
2   2  NA  NA
3   3   2   1
8   8   5   6
9   9  NA  NA
4   4   2  NA
6   6   2   3
10 10   8   9 

2015-08-13

Reliability of pedigree-based and genomic evaluations in selected populations

Paper on "Reliability of pedigree-based and genomic evaluations in selected populations" has finally been published after tedious reviews. This work is a follow up study on reliability of genetic evaluation in selected populations by Piter Bijma (link), but this time connecting pedigree-based and genomic evaluations. The take-home message is that PEV-based reliabilities of genomic predictions are not so much affected by selection as pedigree predictions. This has implications when different breeding program designs are compared using PEV reliabilities - commonly PEV reliability of genomic and pedigree predictions are compared - and suggests that genotyping female selection candidates has been undervalued and should be reconsidered.

2015-03-16

Plant breeding and genetics club videos

Plant breeding and genetics club at K-state have a nice website, which hosts videos from the symposia they organised (there was one in 2013 and one is planned for 2015).

2015-03-07

Potential of genotyping-by-sequencing for genomic selection in livestock populations

Our new paper titled "Potential of genotyping-by-sequencing for genomic selection in livestock populations" has been published in Genetics Selection Evolution. This work shows that genotypes called from low-coverage sequencing data can be equally or even more powerful for genomic prediction than high-quality SNP genotypes. In particular manipulation of coverage allows us to increase number of genotyped individuals at the expense of genotype quality and this can bring us a long way before accuracy of genomic predictions falls significantly. Another useful application is in increasing selection intensity by genotyping more/all selection candidates.

2013-12-01

Read line by line of a file in R

Are you using R for data manipulation for later use with other programs, i.e., a workflow something like this:
  1. read data sets from a disk,
  2. modify the data, and
  3. write it back to a disk.
All fine, but of data set is really big, then you will soon stumble on memory issues. If data processing is simple and you can read only chunks, say only line by line, then the following might be useful:

## File
file <- "myfile.txt"
 
## Create connection
con <- file(description=file, open="r")
 
## Hopefully you know the number of lines from some other source or
com <- paste("wc -l ", file, " | awk '{ print $1 }'", sep="")
n <- system(command=com, intern=TRUE)
 
## Loop over a file connection
for(i in 1:n) {
  tmp <- scan(file=con, nlines=1, quiet=TRUE)
  ## do something on a line of data 
}
Created by Pretty R at inside-R.org