{"spec_id":"volcano-basic","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nvolcano-basic: Volcano Plot for Statistical Significance\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 89/100 | Updated: 2026-05-14\n\"\"\"\n\nimport os\nimport time\nfrom pathlib import Path\n\nimport numpy as np\nfrom bokeh.io import output_file, save\nfrom bokeh.models import ColumnDataSource, HoverTool, Span\nfrom bokeh.plotting import figure\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\n\n# Okabe-Ito palette\nCOLOR_UP = \"#C475FD\"  # vermillion\nCOLOR_DOWN = \"#4467A3\"  # blue\nCOLOR_NS = \"#AAAAAA\"  # neutral gray\n\n# Data - Simulated differential expression results\nnp.random.seed(42)\nn_genes = 2000\n\n# Generate log2 fold changes (effect sizes)\nlog2_fc = np.random.normal(0, 1.5, n_genes)\n\n# Generate p-values: most genes non-significant, some highly significant\n# Use inverse relationship with fold change magnitude for realism\nbase_pvals = np.random.uniform(0.001, 1, n_genes)\nfold_effect = np.abs(log2_fc) / np.max(np.abs(log2_fc))\npvals = base_pvals * (1 - 0.7 * fold_effect) + 0.01 * np.random.random(n_genes)\npvals = np.clip(pvals, 1e-50, 1)\n\nneg_log10_pval = -np.log10(pvals)\n\n# Significance thresholds\npval_threshold = -np.log10(0.05)  # ~1.3\nfc_threshold = 1.0  # log2(2) = 1\n\n# Classify points by significance\nsignificant_up = (neg_log10_pval > pval_threshold) & (log2_fc > fc_threshold)\nsignificant_down = (neg_log10_pval > pval_threshold) & (log2_fc < -fc_threshold)\n\n# Create separate data sources for each category (enables proper legend)\nsource_up = ColumnDataSource(\n    data={\n        \"x\": log2_fc[significant_up],\n        \"y\": neg_log10_pval[significant_up],\n        \"description\": [\n            f\"Up-regulated: FC={x:.2f}, p={10 ** (-y):.2e}\"\n            for x, y in zip(log2_fc[significant_up], neg_log10_pval[significant_up], strict=True)\n        ],\n    }\n)\n\nsource_down = ColumnDataSource(\n    data={\n        \"x\": log2_fc[significant_down],\n        \"y\": neg_log10_pval[significant_down],\n        \"description\": [\n            f\"Down-regulated: FC={x:.2f}, p={10 ** (-y):.2e}\"\n            for x, y in zip(log2_fc[significant_down], neg_log10_pval[significant_down], strict=True)\n        ],\n    }\n)\n\nsource_ns = ColumnDataSource(\n    data={\n        \"x\": log2_fc[~(significant_up | significant_down)],\n        \"y\": neg_log10_pval[~(significant_up | significant_down)],\n        \"description\": [\n            f\"Not significant: FC={x:.2f}, p={10 ** (-y):.2e}\"\n            for x, y in zip(\n                log2_fc[~(significant_up | significant_down)], neg_log10_pval[~(significant_up | significant_down)], strict=True\n            )\n        ],\n    }\n)\n\n# Create figure\np = figure(\n    width=4800,\n    height=2700,\n    title=\"volcano-basic · bokeh · anyplot.ai\",\n    x_axis_label=\"Log₂ Fold Change\",\n    y_axis_label=\"-Log₁₀ (P-value)\",\n)\n\n# Hover tool\nhover = HoverTool(tooltips=[(\"\", \"@description\")])\np.add_tools(hover)\n\n# Plot points by category (non-significant first, then significant on top)\np.scatter(x=\"x\", y=\"y\", source=source_ns, color=COLOR_NS, size=18, alpha=0.5, legend_label=\"Not significant\")\n\np.scatter(x=\"x\", y=\"y\", source=source_down, color=COLOR_DOWN, size=25, alpha=0.7, legend_label=\"Down-regulated\")\n\np.scatter(x=\"x\", y=\"y\", source=source_up, color=COLOR_UP, size=25, alpha=0.7, legend_label=\"Up-regulated\")\n\n# Add threshold lines\nhline = Span(\n    location=pval_threshold, dimension=\"width\", line_color=INK_SOFT, line_dash=\"dashed\", line_width=3, line_alpha=0.5\n)\np.add_layout(hline)\n\nvline_pos = Span(\n    location=fc_threshold, dimension=\"height\", line_color=INK_SOFT, line_dash=\"dashed\", line_width=3, line_alpha=0.5\n)\np.add_layout(vline_pos)\n\nvline_neg = Span(\n    location=-fc_threshold, dimension=\"height\", line_color=INK_SOFT, line_dash=\"dashed\", line_width=3, line_alpha=0.5\n)\np.add_layout(vline_neg)\n\n# Styling - scaled for 4800x2700 canvas\np.title.text_font_size = \"28pt\"\np.title.text_color = INK\np.xaxis.axis_label_text_font_size = \"22pt\"\np.yaxis.axis_label_text_font_size = \"22pt\"\np.xaxis.axis_label_text_color = INK\np.yaxis.axis_label_text_color = INK\np.xaxis.major_label_text_font_size = \"18pt\"\np.yaxis.major_label_text_font_size = \"18pt\"\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_color = INK_SOFT\np.xaxis.axis_line_color = INK_SOFT\np.yaxis.axis_line_color = INK_SOFT\np.xaxis.major_tick_line_color = INK_SOFT\np.yaxis.major_tick_line_color = INK_SOFT\n\n# Grid styling\np.xgrid.grid_line_color = INK\np.ygrid.grid_line_color = INK\np.xgrid.grid_line_alpha = 0.1\np.ygrid.grid_line_alpha = 0.1\n\n# Background\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = INK_SOFT\n\n# Legend styling\np.legend.location = \"top_right\"\np.legend.label_text_font_size = \"18pt\"\np.legend.label_text_color = INK_SOFT\np.legend.glyph_height = 40\np.legend.glyph_width = 40\np.legend.spacing = 15\np.legend.padding = 20\np.legend.background_fill_color = ELEVATED_BG\np.legend.border_line_color = INK_SOFT\n\n# Save HTML\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot with headless Chrome\nW, H = 4800, 2700\nopts = Options()\nfor arg in (\n    \"--headless=new\",\n    \"--no-sandbox\",\n    \"--disable-dev-shm-usage\",\n    \"--disable-gpu\",\n    f\"--window-size={W},{H}\",\n    \"--hide-scrollbars\",\n):\n    opts.add_argument(arg)\ndriver = webdriver.Chrome(options=opts)\ndriver.set_window_size(W, H)\ndriver.get(f\"file://{Path(f'plot-{THEME}.html').resolve()}\")\ntime.sleep(3)\ndriver.save_screenshot(f\"plot-{THEME}.png\")\ndriver.quit()\n"}