-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.R
80 lines (69 loc) · 2.26 KB
/
parser.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
77
78
79
80
#' Read Panicle Spatial Graph
#'
#'
#' Read the XML file that stores panicles architecture
#' data (the P-TRAP output that ends by .ricepr)
#'
#' And returns a `tidygraph` in which each node has
#' spatial coordinates
#'
#' @param path Path to the panicle XML file
#'
#' @importFrom magrittr %>%
#' @importFrom rlang .data
#'
#' @export
read_panicle <- function(path) {
vertices <- xml2::read_xml(path) %>%
xml2::xml_find_all(".//vertex")
nodes <- tibble::tibble(x = vertices %>%
xml2::xml_attr("x") %>%
as.numeric(),
y = vertices %>%
xml2::xml_attr("y") %>%
as.numeric(),
type = vertices %>%
xml2::xml_attr("type"),
id = vertices %>%
xml2::xml_attr("id")) %>%
dplyr::mutate(rank = 1:dplyr::n(),
type = dplyr::case_when(type == "Seconday" ~ "Secondary",
TRUE ~ type))
nodes_rank <- purrr::set_names(x = nodes$rank,
nm = nodes$id)
edges <- xml2::read_xml(path) %>%
xml2::xml_find_all(".//edge")
edges <- tibble::tibble(from = edges %>%
xml2::xml_attr("vertex1"),
to = edges %>%
xml2::xml_attr("vertex2")) %>%
dplyr::mutate(from = nodes_rank[.data$from],
to = nodes_rank[.data$to]) %>%
dplyr::mutate_all(unname)
tst <- tidygraph::tbl_graph(nodes = nodes,
edges = edges)
}
#' Read Grain Spatial Graph
#'
#' Read the XML file that stores grain location
#' (the P-TRAP output that ends by .ricegr)
#'
#' And returns a tibble that stores seed
#' coordinates
#'
#' @param path Path to the panicle XML file
#'
#' @export
read_grains <- function(path)
{
grain <-
path %>%
xml2::read_xml() %>%
xml2::xml_find_all(".//particle")
grain <- tibble::tibble(x = grain %>%
xml2::xml_attr("cx") %>%
as.numeric(),
y = grain %>%
xml2::xml_attr("cy") %>%
as.numeric())
}