
get_beta_violin_ag <- function(beta,
                               node_summary,
                               ag,
                               ag_species = TRUE,
                               db = "vdjdb") {
    if(ag_species) {
        h <- get_ag_species_hits(node_summary = node_summary, 
                                 db = db, 
                                 ag_species = ag)
    } else {
        h <- get_ag_gene_hits(node_summary = node_summary, 
                              db = db, 
                              ag_gene = ag)
    }
    
    
    h$node_summary$spec <- ifelse(
        test = apply(X=h$node_summary[,h$new_columns], MARGIN=1, FUN=sum)==0,
        yes = "-", no = "+")
    h <- h$node_summary
    h <- aggregate(clone_size~sample+spec+community, data = h, FUN = sum)
    dup <- which(h$community %in% h$community[h$spec=="+"] & h$spec=="-")
    if(length(dup)!=0) {
        h <- h[-dup,]
    }
    
    h$size <- ifelse(test = h$spec == "+", yes = h$clone_size, no = 1)
    o <- merge(x = h, y = beta, by = c("sample", "community"), all.x = TRUE)
    
    g <- ggplot(data = o)+
        geom_sina(aes(x = sample, y = mean, col = spec, size = size), 
                  stroke = 0.02, alpha = 0.4)+
        theme_bw(base_size = 10)+
        ggtitle(label = '', 
                subtitle = paste0("Ag=", ag, ", DB=", db))+
        ylab(label = expression(beta))+
        xlab(label = "repertoire")+
        scale_radius(name = "# cells", 
                     breaks = scales::pretty_breaks(n = 4),
                     range = c(1, 4))+
        scale_color_manual(name = "specificity", 
                           values = c("steelblue", "orange"))+
        guides(size = guide_legend(order = 2), 
               colour = guide_legend(order = 1))
    
    return(g)
}



get_beta_cprob_ag <- function(beta,
                              node_summary,
                              ag,
                              ag_species = TRUE,
                              db = "vdjdb") {
    
    getc <- function(x, h) {
        q <- h[h$sample == x, ]
        z <- cumsum(q$clone_size)
        p <- z/max(z)
        p[is.infinite(p)|is.nan(p)] <- 0
        return(data.frame(p = p, b = q$mean, sample = x))
    }
    
    if(ag_species) {
        h <- get_ag_species_hits(node_summary = node_summary, 
                                 db = db, 
                                 ag_species = ag)
    } else {
        h <- get_ag_gene_hits(node_summary = node_summary, 
                              db = db, 
                              ag_gene = ag)
    }
    
    h$node_summary$spec <- ifelse(
        test = apply(X = h$node_summary[,h$new_columns], MARGIN=1, FUN=sum)==0,
        yes = FALSE, no = TRUE)
    h <- h$node_summary
    h <- merge(x = h, y = beta, by = c("sample", "community"), all.x = TRUE)
    
    ha <- aggregate(clone_size~community+sample+mean+spec, data = h, FUN = sum)
    ha$clone_size[ha$spec == FALSE] <- 0
    ha <- ha[order(ha$mean, decreasing = TRUE),]
    ha <- do.call(rbind, lapply(X = unique(ha$sample), h = ha))
    colnames(ha) <- c("p_ag", "b", "sample")
    
    hb <- aggregate(clone_size~community+sample+mean, data = h, FUN = sum)
    hb <- hb[order(hb$mean, decreasing = TRUE),]
    hb <- do.call(rbind, lapply(X = unique(hb$sample), h = hb))
    colnames(hb) <- c("p_b", "b", "sample")
    
    hab <- merge(x = ha, y = hb, by = c("b", "sample"))
    hab <- hab[order(hab$b, decreasing = TRUE),]
    
    g <- ggplot(data = v)+
        facet_wrap(~sample)+
        geom_line(data = hab[order(hab$p_b, decreasing = TRUE),],
                  aes(x = b, y = p_b, col = Ag),
                  linetype = "dashed", size = .5, alpha = 0.7, col = "black")+
        geom_line(data = hab[order(hab$p_ag, decreasing = TRUE),],
                  aes(x = b, y = p_ag, col = Ag), size = .5, alpha = 0.7)+
        xlab(label = expression(beta))+
        ylab(label = "Cumulative probability")+
        scale_color_discrete(name = "Antigen")+
        theme(legend.position = "top", 
              strip.text.x = element_text(margin = margin(0.02,0,0.02,0, "cm")))
    
    return(list(data = hab, g = g))
}
