Skip to content

Instantly share code, notes, and snippets.

@brentp
Last active March 31, 2020 15:35
Show Gist options
  • Save brentp/b6b437caedff22c3c4038cf9bebec03d to your computer and use it in GitHub Desktop.
Save brentp/b6b437caedff22c3c4038cf9bebec03d to your computer and use it in GitHub Desktop.

Need: A simple way to draw pedigrees. This R script will draw a PNG per family in a pedigree file.

Some answers here: https://twitter.com/brent_p/status/1222196118563381248 and others off-line:

using dot/graphviz

Also see: https://twitter.com/Yutaka__Masuda/status/1222214145233182720 which uses:

ped=some.ped
fam=famid
awk -v family=$famid 'BEGIN{print "digraph fam{"} $1==family{if($3!="0"){printf("%s -> %s;\n",$3,$2)}if($4!="0"){printf("%s -> %s;\n",$4,$2)}} END{print "}"}' $ped | dot -Tpng -o fam.png

More info on drawing pedigrees with dot format here The awk above could be expanded to something that shows sex and affection status quite simply. Here is an example dot output:

digraph G {
        mom [shape=oval]
        momdadjoint [shape=diamond,style=filled,label="",height=.01,width=.01] ;
        dad [shape=square]
        kidA [shape=square color=red]

        edge[arrowhead=none];
        {mom dad} -> momdadjoint

        edge[arrowhead=normal];
        momdadjoint -> {kidA kidB kidC}
}

other tools

I have not been successful in getting large pedigrees with missing members to draw in common online software like progeny. While madeline is simple to build and install, it requires using M/F for Male/Female instead of the usual 0 or 1 and it has other odd conventions. It would require a script to pre-process a pedigree file--which could be a good option.

test-cases

Here is an example pedigree that should be possible with a tool:

#famid	id	dadid	momid	sex	affected
1356	1401	8513	8514	2	0
1356	1402	8513	8514	2	0
1356	1442	8513	8514	2	0
1356	8503	8513	8514	1	0
1356	8504	8513	8514	2	0
1356	8505	8513	8514	2	0
1356	8506	8513	8514	2	0
1356	8507	0	0	2	0
1356	8508	8513	8514	2	0
1356	8509	0	0	2	0
1356	8510	8513	8514	1	0
1356	8511	8513	8514	1	0
1356	8512	8513	8514	2	0
1356	8513	0	8507	1	0
1356	8514	8515	8509	2	0
1356	8515	0	0	1	0
1356	8612	8513	8514	1	0

note that sample 8513 has a mom specified, but not a dad. and other samples can have a mom or dad that is not in the pedigree file.

And here is a huuuge pedigree where kinship2 defaults fail:

132829	1829	0	0	1	0
132829	1830	dad_132829_somalier	mom_132829_somalier	2	0
132829	1836	1829	1830	1	0
132829	1837	1836	1838	2	0
132829	1838	1847	1848	2	0
132829	1839	1836	1838	1	0
132829	1840	1836	1838	2	0
132829	1841	1836	1838	1	0
132829	1842	1836	1838	1	0
132829	1843	1836	1838	2	0
132829	1844	1836	1838	2	0
132829	1845	1836	1838	1	0
132829	1846	1836	1838	2	0
132829	1847	0	0	1	0
132829	1848	0	0	2	0
132829	2777	8100	8101	2	0
132829	2778	8100	8101	2	0
132829	2779	8100	8101	1	0
132829	2780	8100	8101	2	0
132829	2788	8125	8126	1	0
132829	2789	8136	8135	2	0
132829	2790	8136	8135	2	0
132829	2946	8107	8108	2	0
132829	46077	1829	1830	1	0
132829	46078	46494	0	2	0
132829	46079	46077	46078	2	0
132829	46080	46077	46078	1	0
132829	46081	46077	46078	1	0
132829	46082	46077	46078	2	0
132829	46083	46077	46078	1	0
132829	46084	46077	46078	1	0
132829	46086	46077	46078	2	0
132829	46494	0	0	1	0
132829	4841	8100	8101	2	0
132829	4875	1836	1838	1	0
132829	51497	8123	8124	2	0
132829	51498	8123	8124	2	0
132829	51499	8123	8124	2	0
132829	51500	8123	8124	2	0
132829	51501	8123	8124	2	0
132829	51502	8123	8124	2	0
132829	51503	8123	8124	2	0
132829	51504	8123	8124	2	0
132829	51505	8136	8135	2	0
132829	51506	8095	8097	1	0
132829	51507	8095	8097	2	0
132829	51508	8095	8097	1	0
132829	51509	8142	8143	1	0
132829	51510	8142	8143	1	0
132829	51511	8142	8143	2	0
132829	51512	8142	8143	2	0
132829	58078	8100	8101	2	0
132829	8090	8095	8097	1	0
132829	8091	8095	8097	1	0
132829	8092	8095	8097	2	0
132829	8093	8095	8097	2	0
132829	8094	8100	8101	1	0
132829	8095	8123	8124	1	0
132829	8096	8095	8097	1	0
132829	8097	8107	8108	2	0
132829	8098	8100	8101	2	0
132829	8099	8100	8101	2	0
132829	8100	8139	8140	1	0
132829	8101	8107	8108	2	0
132829	8102	8107	8108	2	0
132829	8103	8107	8108	2	0
132829	8104	8107	8108	2	0
132829	8105	8107	8108	2	0
132829	8106	8107	8108	1	0
132829	8107	0	0	1	0
132829	8108	0	0	2	0
132829	8109	8107	8108	2	0
132829	8110	8107	8108	1	0
132829	8123	0	0	1	0
132829	8124	0	0	2	0
132829	8125	8123	8124	1	0
132829	8126	8373	8374	2	0
132829	8127	8125	8126	1	0
132829	8128	8125	8126	1	0
132829	8129	8125	8126	1	0
132829	8130	8136	8135	1	0
132829	8131	8136	8135	1	0
132829	8132	8136	8135	2	0
132829	8133	8136	8135	1	0
132829	8134	8136	8135	1	0
132829	8135	8123	8124	2	0
132829	8136	8138	8137	1	0
132829	8137	0	0	2	0
132829	8138	0	0	1	0
132829	8139	0	0	1	0
132829	8140	0	0	2	0
132829	8141	8142	8143	1	0
132829	8142	8123	8124	1	0
132829	8143	8138	8137	2	0
132829	8144	8125	8126	2	0
132829	8145	8125	8126	2	0
132829	8146	8142	8143	1	0
132829	8175	8142	8143	2	0
132829	8373	0	0	1	0
132829	8374	dad_132829_somalier	mom_132829_somalier	2	0
132829	8425	8142	8143	1	0
132829	8426	8142	8143	1	0
132829	8427	8125	8126	2	0
132829	8428	8125	8126	1	0
132829	8429	8136	8135	1	0
132829	8432	8095	8097	1	0
132829	8433	8095	8097	1	0
132829	8598	8142	8143	2	0
132829	8599	8142	8143	2	0
require(kinship2)
# Rscript --vanilla ped.R some.ped
args = commandArgs(trailingOnly=TRUE)
df = read.table(args[1], header=TRUE, comment.char='', stringsAsFactors=FALSE)
df = df[,1:6]
colnames(df)[1:6] = c("famid", "id", "dadid", "momid", "sex", "affected")
df$id = as.character(df$id)
df$famid = as.character(df$famid)
df$momid = as.character(df$momid)
df$dadid = as.character(df$dadid)
for(fam in unique(df$famid)) {
subf = df[df$famid == fam,]
sub = as.data.frame(fixParents(subf$id, subf$dadid, subf$momid, subf$sex, missid=0))
sub$famid = fam
ped = pedigree(id=sub$id, momid=sub$momid, dadid=sub$dadid, sex=sub$sex, famid=sub$famid, missid=0)
fn = paste('pedigree-', fam, '.png', sep="")[1]
print(fn)
png(fn)
plot(ped[fam])
dev.off()
}
@mvelinder
Copy link

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