{"spec_id":"genome-track-multi","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' genome-track-multi: Genome Track Viewer\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 89/100 | Created: 2026-06-02\n\nlibrary(ggplot2)\nlibrary(ragg)\n\nset.seed(42)\n\n# Theme tokens (Imprint palette — theme-adaptive chrome)\nTHEME       <- Sys.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG     <- if (THEME == \"light\") \"#FAF8F1\" else \"#1A1A17\"\nELEVATED_BG <- if (THEME == \"light\") \"#FFFDF6\" else \"#242420\"\nINK         <- if (THEME == \"light\") \"#1A1A17\" else \"#F0EFE8\"\nINK_SOFT    <- if (THEME == \"light\") \"#4A4A44\" else \"#B8B7B0\"\nINK_MUTED   <- if (THEME == \"light\") \"#6B6A63\" else \"#A8A79F\"\n\n# Imprint categorical palette — hybrid-v3 sort, first series always #009E73\nIMPRINT_PALETTE <- c(\n  \"#009E73\",  # 1 brand green\n  \"#C475FD\",  # 2 lavender\n  \"#4467A3\",  # 3 blue\n  \"#BD8233\",  # 4 ochre\n  \"#AE3030\",  # 5 matte red\n  \"#2ABCCD\",  # 6 cyan\n  \"#954477\",  # 7 rose\n  \"#99B314\"   # 8 lime\n)\n\n# Genomic region: chr12: 1,000,000 – 1,100,000 (100 kb window)\nCHR          <- \"chr12\"\nREGION_START <- 1000000L\nREGION_END   <- 1100000L\nTRACK_LEVELS <- c(\"Genes\", \"Coverage\", \"Variants\", \"Regulatory\")\n\n# --- Genes track data -------------------------------------------------------\n# Two genes: RAPH1 (+ strand, 3 exons) and KRAS2 (− strand, 2 exons)\nexons_df <- data.frame(\n  xmin  = c(1010000, 1025000, 1045000, 1070000, 1082000),\n  xmax  = c(1018000, 1035000, 1060000, 1078000, 1095000),\n  ymin  = 0.25,\n  ymax  = 0.75,\n  gene  = c(\"RAPH1 (+)\", \"RAPH1 (+)\", \"RAPH1 (+)\", \"KRAS2 (−)\", \"KRAS2 (−)\"),\n  track = factor(\"Genes\", levels = TRACK_LEVELS),\n  stringsAsFactors = FALSE\n)\n\ngene_bodies <- data.frame(\n  x     = c(1010000, 1070000),\n  xend  = c(1060000, 1095000),\n  y     = 0.5,\n  yend  = 0.5,\n  track = factor(\"Genes\", levels = TRACK_LEVELS)\n)\n\ngene_labels <- data.frame(\n  x     = c(1034000, 1082500),\n  y     = 0.92,\n  label = c(\"RAPH1\", \"KRAS2\"),\n  track = factor(\"Genes\", levels = TRACK_LEVELS),\n  stringsAsFactors = FALSE\n)\n\n# --- Coverage track data ----------------------------------------------------\ncov_pos  <- seq(REGION_START, REGION_END, by = 500)\nn_cov    <- length(cov_pos)\nbase_cov <- 80 + 35 * sin((cov_pos - REGION_START) /\n                           (REGION_END - REGION_START) * pi * 2.5)\n\n# Elevated depth over exonic regions (transcribed segments)\nexon_boost <- numeric(n_cov)\nexon_coords <- list(\n  c(1010000, 1018000), c(1025000, 1035000), c(1045000, 1060000),\n  c(1070000, 1078000), c(1082000, 1095000)\n)\nfor (coords in exon_coords) {\n  idx <- cov_pos >= coords[1] & cov_pos <= coords[2]\n  exon_boost[idx] <- exon_boost[idx] + rnorm(sum(idx), mean = 90, sd = 20)\n}\n\ncoverage_df <- data.frame(\n  position = cov_pos,\n  depth    = pmax(3, base_cov + exon_boost + rnorm(n_cov, 0, 8)),\n  track    = factor(\"Coverage\", levels = TRACK_LEVELS)\n)\n\n# --- Variants track data ----------------------------------------------------\nvariants_df <- data.frame(\n  position = c(1013000, 1016500, 1028000, 1031500, 1039000,\n               1052000, 1057500, 1071000, 1084500, 1089000, 1093000),\n  quality  = c(55, 90, 40, 75, 30, 88, 62, 46, 95, 72, 50),\n  type     = c(\"SNP\", \"SNP\", \"Indel\", \"SNP\", \"SNP\",\n               \"SNP\", \"Indel\", \"SNP\", \"SNP\", \"SNP\", \"Indel\"),\n  track    = factor(\"Variants\", levels = TRACK_LEVELS),\n  stringsAsFactors = FALSE\n)\n\n# --- Regulatory track data --------------------------------------------------\nreg_df <- data.frame(\n  xmin         = c(1006000, 1022000, 1067000),\n  xmax         = c(1011000, 1027000, 1072500),\n  ymin         = 0.15,\n  ymax         = 0.85,\n  element_type = c(\"Promoter\", \"Enhancer\", \"Promoter\"),\n  track        = factor(\"Regulatory\", levels = TRACK_LEVELS),\n  stringsAsFactors = FALSE\n)\nreg_labels <- data.frame(\n  x     = (reg_df$xmin + reg_df$xmax) / 2,\n  y     = 0.50,\n  label = reg_df$element_type,\n  track = reg_df$track,\n  stringsAsFactors = FALSE\n)\n\n# x-range anchors ensure all 4 facets share the same genomic coordinate range\nx_anchor <- data.frame(\n  position = rep(c(REGION_START, REGION_END), 4),\n  y_dummy  = 0,\n  track    = factor(rep(TRACK_LEVELS, each = 2), levels = TRACK_LEVELS)\n)\n\n# --- Plot -------------------------------------------------------------------\np <- ggplot() +\n  geom_blank(data = x_anchor, aes(x = position, y = y_dummy)) +\n\n  # Genes: intron connector lines, then exon rectangles, then gene name labels\n  geom_segment(\n    data = gene_bodies,\n    aes(x = x, xend = xend, y = y, yend = yend),\n    color = INK_SOFT, linewidth = 0.5\n  ) +\n  geom_rect(\n    data = exons_df,\n    aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = gene),\n    color = NA\n  ) +\n  geom_text(\n    data  = gene_labels,\n    aes(x = x, y = y, label = label),\n    color = INK, size = 3.0, fontface = \"italic\"\n  ) +\n\n  # Coverage: filled area chart of read depth\n  geom_area(\n    data      = coverage_df,\n    aes(x = position, y = depth),\n    fill      = IMPRINT_PALETTE[3],\n    alpha     = 0.65,\n    color     = IMPRINT_PALETTE[3],\n    linewidth = 0.25\n  ) +\n\n  # Variants: lollipop chart (stem + head) coloured by variant type\n  geom_segment(\n    data      = variants_df,\n    aes(x = position, xend = position, y = 0, yend = quality, color = type),\n    linewidth = 0.7\n  ) +\n  geom_point(\n    data = variants_df,\n    aes(x = position, y = quality, color = type),\n    size = 2.8\n  ) +\n\n  # Regulatory elements: coloured rectangles with label text\n  geom_rect(\n    data  = reg_df,\n    aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = element_type),\n    alpha = 0.85, color = NA\n  ) +\n  geom_text(\n    data  = reg_labels,\n    aes(x = x, y = y, label = label),\n    color = INK, size = 2.2, fontface = \"bold\"\n  ) +\n\n  facet_grid(track ~ ., scales = \"free_y\", space = \"fixed\") +\n\n  scale_x_continuous(\n    name   = paste(CHR, \"position (Mb)\"),\n    labels = function(x) sprintf(\"%.3f\", x / 1e6),\n    expand = c(0.02, 0)\n  ) +\n  scale_y_continuous(name = NULL, breaks = NULL) +\n\n  # Imprint fill scale: gene strands + regulatory element types\n  scale_fill_manual(\n    name   = NULL,\n    values = c(\n      \"RAPH1 (+)\"    = IMPRINT_PALETTE[1],\n      \"KRAS2 (−)\" = IMPRINT_PALETTE[2],\n      \"Promoter\"     = IMPRINT_PALETTE[5],\n      \"Enhancer\"     = IMPRINT_PALETTE[6]\n    )\n  ) +\n  # Imprint color scale: variant types\n  scale_color_manual(\n    name   = \"Variant\",\n    values = c(\n      \"SNP\"   = IMPRINT_PALETTE[4],\n      \"Indel\" = IMPRINT_PALETTE[7]\n    )\n  ) +\n\n  labs(title = \"genome-track-multi · r · ggplot2 · anyplot.ai\") +\n\n  theme_minimal(base_size = 8) +\n  theme(\n    plot.background    = element_rect(fill = PAGE_BG, color = PAGE_BG),\n    panel.background   = element_rect(fill = PAGE_BG, color = NA),\n    panel.grid.major.x = element_blank(),\n    panel.grid.major.y = element_line(color = INK_MUTED, linewidth = 0.15),\n    panel.grid.minor   = element_blank(),\n    panel.border       = element_rect(color = INK_SOFT, fill = NA, linewidth = 0.4),\n    panel.spacing      = unit(0.15, \"lines\"),\n    axis.title.x       = element_text(color = INK, size = 10,\n                                      margin = margin(t = 6)),\n    axis.text.x        = element_text(color = INK_SOFT, size = 8),\n    axis.text.y        = element_blank(),\n    axis.ticks.y       = element_blank(),\n    plot.title         = element_text(color = INK, size = 12, face = \"plain\",\n                                      margin = margin(b = 10)),\n    strip.text         = element_text(color = INK, size = 9, face = \"bold\",\n                                      hjust = 0, margin = margin(l = 4, r = 4)),\n    strip.background   = element_rect(fill = ELEVATED_BG, color = INK_SOFT,\n                                      linewidth = 0.4),\n    legend.background  = element_rect(fill = ELEVATED_BG, color = INK_SOFT,\n                                      linewidth = 0.4),\n    legend.text        = element_text(color = INK_SOFT, size = 8),\n    legend.title       = element_text(color = INK, size = 9),\n    legend.position    = \"bottom\",\n    legend.key.size    = unit(0.4, \"cm\"),\n    plot.margin        = margin(12, 12, 8, 8)\n  )\n\n# --- Save -------------------------------------------------------------------\nggsave(\n  filename = sprintf(\"plot-%s.png\", THEME),\n  plot     = p,\n  device   = ragg::agg_png,\n  width    = 8,\n  height   = 4.5,\n  units    = \"in\",\n  dpi      = 400\n)\n"}