inst/intdata/R/drake_functions.R

################################################################################
#                                                                              #
#                              General functions                               #
#                                                                              #
################################################################################
theme_report<-function(){
  theme_ipsum(base_size = 8)
}
str_wrap_factor <- function(x, ...) {
  levels(x) <- str_wrap(levels(x), ...)
  x
}
# Function to create a separate chunk to generate a plot for each item in list
split_sub_fig <- function(plots,chunk_caption, caption, maxlength,subcaption,outwidth,figwidth,figasp,ncol,dev,dev_args) {
  
  knitr::opts_knit$set(progress = FALSE, verbose = FALSE)
  
  n <- length(plots)
  numPages <- ceiling(n / maxlength)
  splitPlots <- split(plots, rep(1:ceiling(n/maxlength), each=maxlength)[1:n])
  splitparam<-split(paste(subcaption, names(plots)), rep(1:ceiling(n/maxlength), each=maxlength)[1:n])
  names_cap<-split(names(plots), rep(1:ceiling(n/maxlength), each=maxlength)[1:n])%>%map(~paste(.x,collapse = ", "))%>%map(~paste(caption,.x,sep = ": "))
  
  for (page in 1:numPages) {
    
    cat(
      knit(text=knit_expand(
        text=(
          "```{r {{chunk_caption}}{{page}}, fig.cap=names_cap[[page]], fig.subcap=splitparam[[page]], out.width={{outwidth}},dev={{dev}},dev.args={{dev_args}},fig.pos='H', fig.ncol={{ncol}},fig.width={{figwidth}}, fig.asp={{figasp}}, echo = FALSE, message = FALSE,warning=FALSE,cache=F,eval=T,fig.show='asis', fig.align='default'}
          
for(i in splitPlots[page]){

for(plots in i){
 print(plots)
}

#print(splitPlots[page])

#for(i in 1:length(plots)){
#  print(plots[[i]])
#}

}
```"),
        #chunk_caption=str_replace_all(caption," ","-"),
        caption = caption,
        page = page)))
  }
}
################################################################################
#                                                                              #
#                                Chapter Index                                 #
#                                                                              #
################################################################################
tabulate_required_software<-function(x){
  x%>>%
    select(-Origin)%>>%
    mutate(Tool=cell_spec(Tool, "latex", align = "L{3cm}"),
           
           Description=cell_spec(Description, "latex", align = "L{5cm}"),
           
           Reference=cell_spec(Reference, "latex", align = "C{2cm}",escape=F),
           
           Version=cell_spec(Version, "latex", align = "R{2cm}"),
    )%>>%
    arrange(Tool)%>>%
    {kable(.,"latex", booktabs = T,longtable = T,escape= FALSE,caption = "Tools used for report generation")%>%
        kable_styling(latex_options = c("repeat_header"),font_size = 10)
    }%>>%
    column_spec(1,width = "3cm")%>>%
    column_spec(2,width = "5cm")%>>%
    column_spec(3,width = "2cm")%>>%
    column_spec(4,width = "2cm")%>>%
    return()
  
}
################################################################################
#                                                                              #
#                               Chapter General                                #
#                                                                              #
################################################################################
tabulate_system_dependent_parameter<-function(x){
  system_dependent_parameter<-x%>>%
    clean_names()%>>%
    arrange(protein,enzyme_transporter_process_gene)%>>%
    select(protein,everything())%>>%
    select(-ontogeny)%>>%
    mutate(
      enzyme_transporter_process_gene=cell_spec(enzyme_transporter_process_gene, "latex", align = "L{3cm}",escape=F),
      
      mean_reference_concentration_mycromol_l=cell_spec(mean_reference_concentration_mycromol_l, "latex", align = "R{2cm}",escape=F),
      
      geometric_standard_deviation_of_the_reference_concentration=cell_spec(geometric_standard_deviation_of_the_reference_concentration, "latex", align = "R{2.5cm}",escape=F),
      
      relative_expression_in_the_different_organs=cell_spec(relative_expression_in_the_different_organs, "latex", align = "L{2.5cm}",escape=F),
      
      half_life_liver_hours=cell_spec(half_life_liver_hours, "latex", align = "R{1.5cm}",escape=F),
      
      half_life_intestine_hours=cell_spec(half_life_intestine_hours, "latex", align = "R{1.5cm}",escape=F)
    )%>>%
    rename(`Enzyme / Transporter / Process (Gene)`=enzyme_transporter_process_gene,
           `Mean reference concentration`=mean_reference_concentration_mycromol_l,
           `Geometric standard deviation of the reference concentration`=geometric_standard_deviation_of_the_reference_concentration,
           `Relative expression in the different organs`=relative_expression_in_the_different_organs,
           `Half-life liver [hours]`=half_life_liver_hours,
           `Half-life intestine [hours]`=half_life_intestine_hours)
  
  names(system_dependent_parameter)[3] <- paste0(names(system_dependent_parameter)[3], footnote_marker_alphabet(1,"latex"))
  names(system_dependent_parameter)[4] <- paste0(names(system_dependent_parameter)[4], footnote_marker_alphabet(2,"latex"))
  
  kable(system_dependent_parameter,"latex", booktabs = T,longtable = T,escape= FALSE,caption = "System-dependent parameters and expression of relevant enzymes, transporters and other ADME processes.")%>%
    collapse_rows(1:2, row_group_label_position = 'stack')%>%
    kable_styling(latex_options = c("repeat_header"),font_size = 8)%>%
    #footnote(alphabet = c("µmol protein/l in the tissue of the highest expression. If no information on reference concentration was available it was set to 1.0 μmol/l and the catalytic rate constant (kcat) was optimized","PK-Sim® expression database profile","If no reference is stated, the values correspond to the PK-Sim® default values."),threeparttable = T) %>%
    landscape()%>>%
    column_spec(1,width = "0.5cm")%>>%
    column_spec(2,width = "3cm")%>>%
    column_spec(3,width = "2cm")%>>%
    column_spec(4,width = "2.5cm")%>>%
    column_spec(5,width = "2.5cm")%>>%
    column_spec(6,width = "1.5cm")%>>%
    column_spec(6,width = "1cm")%>>%
    return()
}
plot_general_workflow<-function(){
  grViz(" 
  digraph CFA {
    # Multiple level nodes
    node [shape = box]
    a [label = '@@1']; 
    b [label = '@@2']; 
    c [label = '@@3']; 
    d [label = '@@4'];
    e [label = '@@5'];
    f [label = '@@6'];
    g [label = '@@7'];

    # Connect nodes with edges and labels
    a -> b [label = '']
    b -> c [label = '']
    c -> d [label = '']
    d -> e [label = '']
    e -> f [label = '']
    c -> g [label = '']
    g -> c [label = '']
    
  }

[1]: 'Literature search and data digitization' 
[2]: 'Model building of inital PBPK model'
[3]: 'Model evaluation and refinement'
[4]: 'Coupling with seperately developed inhibitor/inductor'
[5]: 'Model evaluation and refinement'
[6]: 'Prediction of DDI and DGIs'
[7]: 'Sensitivity analysis'
")
}
plot_cyp_expression<-function(){
  tibble::tribble(
    ~Isoform, ~Fraction,
    "CYP3A4/5",                                           30.2,
    "CYP2J2",                                              3,
    "CYP2E1",                                              3,
    "CYP2D6",                                             20,
    "CYP2C19",                                            6.8,
    "CYP2C9",                                           12.8,
    "CYP2C8",                                            4.7,
    "CYP2B6",                                            7.2,
    "CYP2A6",                                            3.4,
    "CYP1A2",                                            8.9
  )%>>%
    {ggplot(data=.,aes(x=fct_reorder(Isoform,Fraction),y=Fraction))+
        geom_bar(stat="identity")+
        labs(x="CYP Isoform",y="Fraction of clinically used drugs metabolized [%]")+
        coord_flip()+
        theme_ipsum_rc(base_size = 8)}
}
plot_relative_expressions<-function(x){
  x%>>%
    mutate(id=str_remove(path,"intdata/tables/system_dependent_parameters/"),
           id=str_remove(id,".csv"))%>>%
    select(-path)%>>%
    clean_names()%>>%
    na.omit()%>>%
    split(.$id)%>>%
    map(~ggplot(data=.,aes(x=relative_expression*100,y=fct_reorder(cmt,relative_expression),label=cmt))+
          geom_barh(stat="identity",width=0.5)+
          labs(y="",x="Relative expression [%]")+
          
          facet_wrap(~id)+
          theme_ipsum_rc(base_size = 8)+
          theme(axis.title.y = element_text(margin = margin(t = 0, r = 100, b = 0, l = 10))))%>>%
    return()
}  
JGWojtyniak/ospsreports documentation built on Nov. 14, 2019, 12:11 p.m.