knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE
)
# Not sure why I need to load all those packages manually
library(dplyr)
library(igraph)
library(ggplot2)
library(magrittr)
library(stringr)
library(purrr)
library(ggraph)
library(ptrapr)


# all panicle files are in extdata
panicle_paths <- 
  system.file(package = "ptrapr") %>%
  paste0("/extdata") %>%
  list.files(full.names = TRUE) %>%
  # remove the extension (variable part)
  # each panicle has two files with different extension
  str_remove("ricegr|ricepr") %>%
  unique()

# parse them at once
panicles <- 
  panicle_paths %>%
  map(~read_full_panicle(pr_file = paste0(., "ricepr"), 
                     gr_file = paste0(., "ricegr")))

# plot them all
plot_panicle <- function(panicle)
  {
  p <- 
  panicle %>%
    ggraph(layout = "nicely") +
    geom_edge_link(arrow = grid::arrow(length = unit(0.08,
                                                     "inches"),
                                       type = "closed"),
                   colour = "grey30") +
    geom_node_point(aes(colour = type),
                    size = 2,
                    alpha =.7) +
    coord_fixed() +
    theme_minimal()

  # print plot
  p
}

panicles %>% map(plot_panicle)

# panicles %>% 
#   map(~invert_edges(.,check_before = TRUE)) %>%
#   map(plot_panicle)
panicle_fixed <- 
  panicles %>% 
  map(~invert_edges(.,check_before = TRUE))
tileplot_branch <- function(pan_tbl) {
  p <- 
    pan_tbl %>% 
  ggplot(aes(x = node_rank,
             y = primary_rank,
             fill = type)) +
  geom_tile(colour = "grey80",
            size = 2) 

    p %>% print
}


# THIS DOES NOT WORK
panicle_fixed[[4]] %>% plot_panicle()
panicle_fixed[[4]] %>% as_long_data_frame()
panicle_fixed[[4]] %>% panicle_tibble(silently = FALSE) #%>% tileplot_branch()

# THIS gives a warning
panicle_fixed[[5]] %>% panicle_tibble() #%>% tileplot_branch()

# the others look fine, but check better, compare figures
panicle_fixed[[9]] %>% panicle_tibble() %>% tileplot_branch()


panicle_fixed %>%
  map(panicle_tibble) %>%
  map(tileplot_branch)


pdf(file = "docs/figs/test_pans.pdf",
    width = 30,
    height = 18)
panicle_fixed %>%
  map(
    ~list(plot_panicle(.) %>% ggplotGrob(),
          panicle_tibble(.) %>% tileplot_branch() %>% ggplotGrob())
  ) %>%
  map(~cowplot::plot_grid(plotlist = ., nrow = 2))
dev.off()


# This one has a spikelet before a branch
panicle_fixed[[8]] %>% plot_panicle()
panicle_fixed[[8]] %>% panicle_tibble() %>% tileplot_branch()


othomantegazza/ptrapr documentation built on May 4, 2019, 4:16 a.m.