-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakeFullGraph.R
76 lines (59 loc) · 2.69 KB
/
MakeFullGraph.R
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
##################################################
## Project: Soybase Pedigree Visualization
## Script purpose: Script to make and save an igraph
## object of the full soybase pedigree
## Date: 2021-04-20
## Author: Jay Gillenwater
##################################################
# Check for required packages and install if they are not already installed
requiredPackages <- c("tidyverse",
"igraph")
toInstall <- requiredPackages[!(requiredPackages %in% installed.packages()[,"Package"])]
if(length(toInstall)) install.packages(toInstall)
library(tidyverse)
library(igraph)
# Read in the parentage table
AllParentage <- read_csv("./Data/SoybasePedigreeData.csv")
ComplexCrosses <- AllParentage %>% filter(grepl("\\*", Female) | grepl("\\*", Male))
SimpleCrosses <- AllParentage %>% filter(!grepl("\\*", Female) & !grepl("\\*", Male))
ComplexParents <- ComplexCrosses %>%
select(Female, Male) %>%
unlist() %>%
as.character()
ComplexParents <- ComplexParents[grepl("\\*", ComplexParents) %>% which()]
ComplexParents[39] <- "((((Peking*Scott)*(Peking*Scott))*Scott)*Blackhawk)"
ComplexParentsDF <- tibble(Parent = ComplexParents) %>%
mutate(OpenBracket = str_count(Parent, "\\("),
CloseBracket = str_count(Parent, "\\)"))
MatchedBrackets <- ComplexParentsDF %>%
filter(OpenBracket == CloseBracket)
MatchedBrackets$Parent[250] <- "AP1953*(((Asgrow A3205*(Asgrow 3127*Fayette))*G2436A))"
ComplexParentPedigrees <- vector('list', length = length(MatchedBrackets$Parent))
for(i in 1:length(ComplexParentPedigrees)){
Res <- tryCatch(
{
ParseCross(MatchedBrackets$Parent[[i]])
},
error = function(cond){
message(paste("Parse failed for cross:", MatchedBrackets$Parent[[i]], "Index:", i))
return(NA)
}
)
ComplexParentPedigrees[[i]] <- Res
}
AllComplex <- reduce(ComplexParentPedigrees, rbind)
AllCrosses <- reduce(list(AllComplex, ComplexCrosses, SimpleCrosses), bind_rows)
AllCrosses %>% pivot_longer(c(Female, Male)) %>%
dplyr::select(value, Cultivar, name) %>%
rename(source = value, target = Cultivar) %>%
dplyr::filter(!is.na(source)) %>%
map_dfc(., CleanParent) %>%
distinct() -> EdgeList_AllCrosses
AllNodes <- data.frame(id = unique(unlist(select(EdgeList_AllCrosses, source, target)))) %>% distinct()
NodeData <- read_csv("./Data/CrossData_withTraits.csv") %>%
select(-one_of(c("Female", "Male"))) %>%
mutate(PI_Num = str_remove(PI_Num, " "))
VertexData <- left_join(distinct(AllNodes), NodeData, by = c("id" = "Cultivar"))
VertexData <- VertexData[!duplicated(VertexData$id), ]
AllCrosses_igraph <- graph_from_data_frame(EdgeList_AllCrosses, vertices = VertexData)
save(AllCrosses_igraph, file = "./Data/AllCrossesGraph.RData")