mayoverse / kinship2 Goto Github PK
View Code? Open in Web Editor NEWPedigree creation, plotting, and analytics
License: GNU General Public License v2.0
Pedigree creation, plotting, and analytics
License: GNU General Public License v2.0
Hi there,
I've found this package really useful! I especially love the pedigree plots, and was able to use this package along with plotly to make an interactive plot for personal use. I would love to clean up this code and contribute it to the package if that might be possible. If it doesn't make sense to contribute an exported function (e.g., to avoid a dependency on plotly
), I'm happy to contribute a vignette or something. Please let me know what might be most suitable, and thanks so much for open sourcing this extremely useful package!
Best,
Bernie
EDIT: I forgot to mention that this enables hovertext with additional information available for each person.
Hi! I may have stumbled upon a bug in your (usually brilliant) plotting algorithm:
library(kinship2)
x = pedigree(id = 1:15,
dadid = c(0,0,0,0,0,1,3,3,5,6,8,8,10,12,13),
momid = c(0,0,0,0,0,2,2,4,4,7,7,9,11,11,14),
sex = c(1,2,1,2,1,1,2,1,2,1,2,1,1,2,1))
as.data.frame(x)
#> id dadid momid sex
#> 1 1 0 0 male
#> 2 2 0 0 female
#> 3 3 0 0 male
#> 4 4 0 0 female
#> 5 5 0 0 male
#> 6 6 1 2 male
#> 7 7 3 2 female
#> 8 8 3 4 male
#> 9 9 5 4 female
#> 10 10 6 7 male
#> 11 11 8 7 female
#> 12 12 8 9 male
#> 13 13 10 11 male
#> 14 14 12 11 female
#> 15 15 13 14 male
A correct plot of the pedigree is shown below. Iโm only able to produce this by specifying the complete spouse order data:
sp = rbind(c(1,2), c(2,3), c(3,4), c(4,5), c(6,7), c(7,8), c(8,9), c(10,11), c(11,12), c(13,14))
x$hints = list(order = 1:15, spouse = cbind(sp, 0))
plot(x)
Without the hints, the top row gets botched:
x$hints = NULL
plot(x)
For some reason individuals 1 and 4 are connected as spouses, although they aren't.
Created on 2022-01-31 by the reprex package (v2.0.1)
Hi,
I was wondering why, in the pedigrees below, sisters 3 and 4 are displayed in the wrong order. I understand that siblings are sometimes shuffled to improve the overall display, but in these symmetrical cases I cannot see any reason for doing so.
I think I have tracked the behaviour down to autohint()
, but it is not clear to me what exactly this tries to optimize, nor why it is overzealous in my examples.
First example:
library(kinship2)
x = pedigree(id = 1:6,
dadid = c(0,0,1,1,1,1),
momid = c(0,0,2,2,3,4),
sex = c(1,2,2,2,1,1))
plot(x)
Another example:
y = pedigree(id = 1:7,
dadid = c(0,0,1,1,0,5,5),
momid = c(0,0,2,2,0,3,4),
sex = c(1,2,2,2,1,1,1))
plot(y)
Created on 2022-03-24 by the reprex package (v2.0.1)
All the best,
Magnus
Hi,
In the example of makekinship, the matrix is said to be a "bdsmatrix" but is in fact when the example is ran a "dscmatrix".
Is it something wanted ?
data(minnbreast)
table(minnbreast$sex)
length(unique(minnbreast$famid))
kin1 <- with(minnbreast,makekinship(famid, id, fatherid, motherid))
dim(kin1)
class(kin1)
# The next line shows that few of the elements of the full matrix are >0
length(kin1@blocks)/ prod(dim(kin1))
The consequence is that no parameter @blocks is available.
Louis
In the example below individuals 3 and 4 are MZ twins, but the plot incorrectly shows 3 and 5 as MZ twins.
x = kinship2::pedigree(
id = 1:7,
dadid = c(0, 0, 1, 1, 1, 3, 4),
momid = c(0, 0, 2, 2, 2, 5, 5),
sex = c(1, 2, 1, 1, 2, 1, 1),
relation = data.frame(id1 = 3, id2 = 4, code = 1)) # 3 and 4 MZ twins
plot(x)
#> Warning in autohint(ped): Unexpected result in autohint, please contact
#> developer
Created on 2023-02-15 with reprex v2.0.2
The following pedigrees plot nicely with the CRAN version (1.8.5) but not with the current dev version (1.9.1):
library(kinship2, quietly = T)
packageVersion("kinship2")
#> [1] '1.8.5'
x1 = pedigree(id = 1:4, dad = c(0,0,1,3), mom = c(0,0,2,2), sex = c(1,2,1,1))
x2 = pedigree(id = 1:7, dad = c(0,0,1,1,1,1,3), mom = c(0,0,2,2,2,2,2),
sex = c(1,2,1,1,1,1,1))
par(mfrow = c(1,2))
plot(x1, mar = c(1,2,1,2))
plot(x2, mar = c(1,2,1,2))
Created on 2022-09-21 with reprex v2.0.2
library(kinship2, quietly = T)
packageVersion("kinship2")
#> [1] '1.9.1'
The first plots badly:
plot(x1)
The second does not plot at all:
plot(x2)
#> Error in myid[i, rval$nid[i, ] > 0] <- cumsum(c(0, rval$n))[i] + 1:rval$n[i]: number of items to replace is not a multiple of replacement length
Created on 2022-09-21 with reprex v2.0.2
I'm pretty sure the error lies in kindepth(..., align = TRUE)
which gives different outputs in the two versions.
Hi,
I wonder if the latest CRAN version of kinship2 introduced a bug affecting alignment in some cases.
Below is an example shown both versions.
Any idea what's going on? A fix would be much appreciated!
Thanks for your good work.
-M
library(kinship2, quietly = T)
packageVersion("kinship2")
#> [1] '1.8.5'
x = pedigree(id = 1:7, dad = c(0,0,0,1,3,0,3), mom = c(0,0,0,2,4,0,6), sex = c(1,2,1,2,1,2,1))
plot(x)
library(kinship2, quietly = T)
packageVersion("kinship2")
#> [1] '1.9.6'
x = pedigree(id = 1:7, dad = c(0,0,0,1,3,0,3), mom = c(0,0,0,2,4,0,6), sex = c(1,2,1,2,1,2,1))
plot(x)
#> Did not plot the following people: 7
Created on 2022-12-29 with reprex v2.0.2
Hi
What does the error ""Mismatched lengths, id and sex" indicate?
I was hoping to set all sex =3 ,as I I only want to calculate "Kinship" for autosomes and I figure sex does not matter.
Thanks
Guy
head of my input file
ped id father mother sex
1 B15_25 NA NA 3
1 B22_2 NA NA 3
1 B106_1 B15_25 B22_2 3
1 B106_2 B15_25 B22_2 3
1 B106_3 B15_25 B22_2 3
1 B106_4 B15_25 B22_2 3
1 B106_5 B15_25 B22_2 3
Greetings,
I have peculiar situation on my side with FamAgg.
The Pedigree I use is quite convoluted as it come from breeding company data.
If I use a "complete" pedigree, several birds are duplicated accross families giving me the following message when using FAData :
Erreur dans kinship2::pedigree(...) :
Duplicate subject id: 125Duplicate subject id: 125Duplicate subject id: 125Duplicate subject id: 125Duplicate subject id: 125Duplicate subject id: 125
If I use a trimmed pedigree, the following error is given :
Generating the kinship matrix...Erreur dans kinship2::pedigree(famid = ped[, "family"], id = ped[, "id"], :
Value of 'momid' not found in the id list 4/3927518502234 5/3927518502234 10/3927518502234 11/3961511284313 4/3971527126014
I understand that the functions are doing their jobs and that poultry husbandry is extremely different from human one.
But in case of human, I suppose that sometimes a pedigree can contain several times a father or a mother, so how can this situation be managed by kinship2 which is the base of FamAgg
Below an example of a poultry family.
Many thanks for your time.
Hi,
This error is generated when runing pedigree()
; it speaks for itself. I'm working on a pipeline which uses kinship2, however, subjects are not restricted to two or no parents (one is also possible). I could implement a work-around which creates a second 'unknown' parent for this module specifically, yet, it might be cleaner if this error could be circumvented directly. Is this possible?
Best
I stumbled on a weird plotting artifact that I can't quite figure out. Despite no evidence that Y and unkF2 were ever a couple (no relation data, no Y-unkF2 sired offspring), they appear as a mated pair in the pedigree plot.
In fact, the plot would be simpler and equally correct if the pair were removed and there were one arc going from unkF2 in the C-unkF2 pair, to the unkF2 in the unkF2-unkM2 pair.
Any idea what creates this plotting artifact?
pedigree.df <- data.frame(id = c('1','2','B','C','D','H','M','U','W','Y','Z','12-1.13','12-1.14','12-1.16','13-1.26','14.1-33','16.1-46','16.1-48','19.1-58','19.1-59','19.1-60','19.1-62','20.1-63','21.1-64','21.1-65','21.1-66','21.1-67','21.1-68','21.1-69','21.1-70','unkF1','unkF2','unkM1','unkM2'), dadid = c(NA,NA,'unkM1',NA,'unkM1','unkM2','Y',NA,NA,NA,'unkM2','Y','Y','Y','B','Y','C','C',NA,'H',NA,'H','C','D','D','D','D','D','D','D',NA,NA,NA,NA), momid = c(NA,NA,'unkF1',NA,'unkF1','unkF2','1',NA,NA,NA,'unkF2','1','1','1','1','1','1','1',NA,'1',NA,'1','unkF2','1','1','1','1','1','1','1',NA,NA,NA,NA), sex = c('2','2','1','1','1','1','1','1','1','1','1','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','3','2','2','1','1'))
with(pedigree.df, pedigree(id, dadid, momid, as.integer(sex)))
Hello, i am trying to include a pedigree name at the top of the pedigree plots.
Is there no option to do so using plot.pedigree? I couldnt find anything in the documentation.
Cheers,
Jochen
Hi @sinnweja,
I'm nearly done for the new package.
I've recently discovered a subtle use case where the kindepth is not nicely computed:
Here is the code:
df <- data.frame(
id = 1:12,
dadid = c(0, 0, 1, 0, 0, 0, 3, 3, 5, 5, 7, 10),
momid = c(0, 0, 2, 0, 0, 0, 4, 4, 6, 6, 9, 8),
sex = c(1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 1, 2)
)
pedi <- with(df, pedigree(id, dadid, momid, sex))
kindepth(pedi, align = TRUE)
# 0 0 1 1 0 0 2 2 1 1 3 3
What would be better is to to have a kindepth of
kindepth(pedi, align = TRUE)
# 0 0 1 1 1 1 2 2 2 2 3 3
The problem seems to come in the loop that update the agood
ancestors.
In the case of a double marriage like in the previous pedigree, the loop will iterate and will add the individuals as follow:
# Initial depth
0 0 1 0 0 0 2 2 1 1 3 3
# id 4 single marryin corrected
0 0 1 1 0 0 2 2 1 1 3 3
# Selected pairs is 9, 7
# abad is
9 5 6
# agood is
7 1 2 3 4
# Then by iteration agood become
# + spouse
7 1 2 3 4
# + ancestors
7 1 2 3 4
# + kids
7 1 2 3 4 8
# + spouse
7 1 2 3 4 8 10
# + ancestors
7 1 2 3 4 8 10 5 6
# + kids
7 1 2 3 4 8 10 5 6 9
The individuals 10, 5, 6 and 9 should not be added to be able to modify them.
What would be the best way to control for this case ?
Best,
Louis
Probably because there is an extra space, the examples pedigree()
are badly rendered in the documentation:
Line 37 in ba65a5f
HI
I am trying to draw a large pedigree but by even limiting the pedigree to only breeding pairs I am struggling.
I have tried different import orders but nothing changes. Do you have any advice through which breeding pairs would be placed closer to each other? Or a way the labels could be made readable?
Thanks
Guy
Cex =0.2
2006l_0.2.pdf
Hi,
Here is an issue regarding the besthint function.
Using the following example:
data(sample.ped)
ped <- with(sample.ped, pedigree(id, father, mother, sex))
besthint(ped, wt = c(100, 10, 1), tolerance = 100)
Doesn't work due to missing momid
and dadid
.
By modifying the besthint with:
momid <- unique(ped$id[ped$mindex[ped$mindex!=0]])
dadid <- unique(ped$id[ped$findex[ped$findex!=0]])
mom <- match(momid, ped$id)
dad <- match(ped$id, dadid)
founders <- ped$id[ped$mindex == 0 & ped$findex == 0] #founders and marry-ins
fpair <- !(is.na(match(momid, founders)) |
is.na(match(dadid, founders)))
fmom <- unique(match(momid[fpair], ped$id)) #row num of founding moms
This works until autohint where it broke:
The problem seems to come from the hint random generation:
hint <-cbind(1:n, rep(0,n))
Could you fix the issue ?
Hi @sinnweja,
Thank you for developing kinship2 package. I found it extremely useful for plotting pedigrees, and it's a time saver!
I plotted my pedigree (see attached file) but would like to modify certain plotting parameters.
I am wondering if you could advise me how to tailor plot.pedigree as I did not fully understand autohints function.
Which parameter can be used to align a spouse without children to the same level of the other one of the couple?
I ran autohint not sure what to change here.
autohint(ped2, hints, packed=F, align=T)
$order
[1] 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 1 2
[37] 1 4 2 3 7 4 9 5 11 6 7 14 8 16 17 9 10 11 21 12 13 14 25 1 2 3 4 1 2 3 4
$spouse
[,1] [,2] [,3]
temp 35 36 0
Which parameter can be used to provide the coordinate of the legend placement? Is it possible other than "location"?
Appreciate your input!!
Best wishes,
Zih-Hua
Hi there,
I've plotted this pedigree by hand and don't see glaring inconsistencies, but for some reason I'm getting an error with quadprog
when plotting this pedigree. Might this have to do with #27 from @LouisLeNezet ? I have used kinship2
multiple times for similarly complex pedigrees without a problem. Unfortunately, it's very hard to traceback the error.
Code, using the attached pedigree:
pedigree <- readr::read_tsv("low-missing-pedigree-fixed.tsv")
ped.object <- pedigree(id = final.pedigree$id, dadid=final.pedigree$sire, momid = final.pedigree$dam, sex = as.integer(final.pedigree$sex.number))
plot(ped.object)
Error:
Error in solve.QP(pp, rep(0, n), t(cmat), dvec) :
constraints are inconsistent, no solution!
sessionInfo()
R version 4.3.1 (2023-06-16)
Platform: aarch64-apple-darwin22.4.0 (64-bit)
Running under: macOS Ventura 13.3.1
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /opt/homebrew/Cellar/r/4.3.1/lib/R/lib/libRlapack.dylib; LAPACK version 3.11.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: America/New_York
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] googlesheets4_1.1.1 sequoia_2.7.2 kinship2_1.9.6 quadprog_1.5-8 Matrix_1.6-1.1 lubridate_1.9.3 forcats_1.0.0
[8] stringr_1.5.1 dplyr_1.1.4 purrr_1.0.2 readr_2.1.4 tidyr_1.3.1 tibble_3.2.1 ggplot2_3.5.0
[15] tidyverse_2.0.0
loaded via a namespace (and not attached):
[1] gtable_0.3.4 xfun_0.42 htmlwidgets_1.6.2 devtools_2.4.5 remotes_2.4.2.1 processx_3.8.2 gargle_1.5.2 lattice_0.21-9
[9] callr_3.7.3 tzdb_0.4.0 vctrs_0.6.5 tools_4.3.1 ps_1.7.5 generics_0.1.3 curl_5.1.0 fansi_1.0.6
[17] pkgconfig_2.0.3 desc_1.4.2 lifecycle_1.0.4 compiler_4.3.1 munsell_0.5.0 httpuv_1.6.11 htmltools_0.5.6.1 usethis_2.2.2
[25] later_1.3.1 pillar_1.9.0 crayon_1.5.2 urlchecker_1.0.1 ellipsis_0.3.2 openssl_2.1.1 cachem_1.0.8 sessioninfo_1.2.2
[33] mime_0.12 tidyselect_1.2.0 digest_0.6.33 stringi_1.8.3 rprojroot_2.0.3 fastmap_1.1.1 grid_4.3.1 colorspace_2.1-0
[41] cli_3.6.2 magrittr_2.0.3 pkgbuild_1.4.2 utf8_1.2.4 withr_3.0.0 rappdirs_0.3.3 prettyunits_1.2.0 scales_1.3.0
[49] promises_1.2.1 bit64_4.0.5 googledrive_2.1.1 timechange_0.2.0 httr_1.4.7 bit_4.0.5 cellranger_1.1.0 askpass_1.2.0
[57] hms_1.1.3 memoise_2.0.1 shiny_1.7.5 knitr_1.45 miniUI_0.1.1.1 profvis_0.3.8 rlang_1.1.3 Rcpp_1.0.12
[65] xtable_1.8-4 glue_1.7.0 pkgload_1.3.3 vroom_1.6.4 jsonlite_1.8.7 rstudioapi_0.15.0 plyr_1.8.9 R6_2.5.1
[73] fs_1.6.3
Hello,
thank you for making kinship2 - i've found the plotting of pedigrees very helpful.
One question i can't seem to resolve is how to incorporate additional information into the plotted pedigrees. For example, i would love to color my plots by year of birth but there doesn't seem to be any way to do this. I know that the plots can be shaded/unshaded by affected status, but this is limited to a 0/1 option as far as i can tell. I have a pedigree that spans several breeding attempts by birds in successive years, and hence colouring each brood by year would be really informative.
Also, i seem to have a couple of issues that i can't explain in my pedigrees. For instance, there's randomly repeated individuals individuals in my pedigrees (e.g. M0046 in the image below) and also the shapes don't correspond to the sexes specified in my pedigree files.
Any suggestions would be much appreciated.
thanks,
D
For some applications, it could be useful to have plot with diamonds in the middle of the tree.
For example, I want to represent any grand mother - grand mother relationship, irrespective of whether the gdma is a maternal or paternal gdma:
To do that I had to hack a couple of checks within the pedigree function:
if(any(!sex[findex] %in% c("male", "unknown"))) {
who <- unique((id[findex])[!sex[findex] %in% c("male", "unknown")])
msg.n <- 1:min(5, length(who)) #Don't list a zillion
stop(paste("Id not male (or unknown), but is a father:",
paste(who[msg.n], collapse= " ")))
}
instead of:
Lines 121 to 126 in ba65a5f
and
if(any(!sex[mindex] %in% c("female", "unknown"))) {
who <- unique((id[mindex])[!sex[mindex] %in% c("female", "unknown")])
msg.n <- 1:min(5, length(who))
stop(paste("Id not female (or unknown), but is a mother:",
paste(who[msg.n], collapse = " ")))
}
instead of:
Lines 136 to 141 in ba65a5f
I understand that it should not be the default behavior, but if an option could allow for this to happen, that would be great since the plotting function seems to have no difficulty with this (although I did not test extensively).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.