{"spec_id":"manhattan-gwas","library":"muix","language":"javascript","code":"// anyplot.ai\n// manhattan-gwas: Manhattan Plot for GWAS\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 88/100 | Updated: 2026-11-08\nimport { ScatterChart } from \"@mui/x-charts/ScatterChart\";\nimport { ChartsReferenceLine } from \"@mui/x-charts/ChartsReferenceLine\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst GENOME_WIDE_P = 5e-8; // genome-wide significance threshold\nconst SUGGESTIVE_P = 1e-5; // suggestive threshold\nconst Y_PAD_SHARE = 0.08; // share of the highest -log10 p added above it\n\n// Columns: set these to your data's column names\nconst CHROMOSOME = \"Chromosome\"; // role: chromosome\nconst POSITION = \"Position (bp)\"; // role: position\nconst P_VALUE = \"P-value\"; // role: p_value\nconst SNP = \"SNP\"; // role: snp_id\n\n// Data: replace this block with your own records, one object per row\n// Simulated GWAS on the 22 autosomes: null p-values everywhere plus three associated loci (chr 6, 11, 17).\nconst data = (() => {\n  let seed = 42;\n  const rand = () => {\n    seed = (seed * 1664525 + 1013904223) % 4294967296;\n    return seed / 4294967296;\n  };\n  const lengthsMb = [248, 242, 198, 190, 181, 170, 159, 145, 138, 133, 135, 133, 114, 107, 101, 90, 83, 80, 58, 63, 46, 50];\n  const peaks = { 6: 14.5, 11: 9.6, 17: 11.3 };\n  const rows = [];\n  const push = (chrom, position, negLogP) =>\n    rows.push({\n      [CHROMOSOME]: String(chrom),\n      [POSITION]: Math.round(position),\n      [P_VALUE]: Number((10 ** -negLogP).toPrecision(3)),\n      [SNP]: `rs${2000000 + rows.length * 7919}`,\n    });\n  lengthsMb.forEach((mb, i) => {\n    const chrom = i + 1;\n    const length = mb * 1e6;\n    for (let k = 0; k < Math.round(mb / 3); k += 1) push(chrom, rand() * length, -Math.log(rand()) / Math.LN10);\n    if (peaks[chrom]) {\n      const peakPos = length * (0.3 + rand() * 0.4);\n      const spread = length * 0.08;\n      for (let k = 0; k < 45; k += 1) {\n        const position = Math.min(Math.max(peakPos + (rand() + rand() + rand() - 1.5) * spread, 1), length);\n        // Association falls off with distance from the causal variant, so each locus tapers into the null band\n        const distance = (position - peakPos) / (spread / 2);\n        push(chrom, position, -Math.log(rand()) / Math.LN10 + peaks[chrom] * Math.exp(-distance * distance));\n      }\n    }\n  });\n  return rows;\n})();\n\n// Plot: reads only data and the constants above\n// Chromosomes are laid end to end in their order of appearance, each as wide as its largest position.\nconst rows = data.filter((d) => d[P_VALUE] != null && d[P_VALUE] > 0);\nconst chromosomes = [...new Set(rows.map((d) => d[CHROMOSOME]))];\nconst chromIndex = new Map(chromosomes.map((c, i) => [c, i]));\nconst chromSpan = chromosomes.map((c) => Math.max(...rows.filter((d) => d[CHROMOSOME] === c).map((d) => d[POSITION])));\nconst offsets = chromSpan.map((_, i) => chromSpan.slice(0, i).reduce((sum, v) => sum + v, 0));\nconst genomeLength = offsets[offsets.length - 1] + chromSpan[chromSpan.length - 1];\nconst centers = chromosomes.map((_, i) => offsets[i] + chromSpan[i] / 2);\nconst labelAtCenter = new Map(centers.map((x, i) => [x, chromosomes[i]]));\n\nconst genomeWide = -Math.log10(GENOME_WIDE_P);\nconst suggestive = -Math.log10(SUGGESTIVE_P);\nconst points = rows.map((d, k) => {\n  const i = chromIndex.get(d[CHROMOSOME]);\n  return { id: k, x: offsets[i] + d[POSITION], y: -Math.log10(d[P_VALUE]), chrom: i, snp: d[SNP], p: d[P_VALUE] };\n});\n// Adjacent chromosomes alternate between two colours; variants past genome-wide significance take a third\nconst groups = [\n  { id: \"alternate-a\", color: t.palette[0], size: 3, points: points.filter((p) => p.y < genomeWide && p.chrom % 2 === 0) },\n  { id: \"alternate-b\", color: t.palette[1], size: 3, points: points.filter((p) => p.y < genomeWide && p.chrom % 2 === 1) },\n  { id: \"significant\", color: t.palette[2], size: 5, points: points.filter((p) => p.y >= genomeWide) },\n];\nconst significant = groups[2].points;\nconst leads = chromosomes\n  .map((_, i) => significant.filter((p) => p.chrom === i).sort((a, b) => b.y - a.y)[0])\n  .filter(Boolean);\nconst yMax = Math.max(genomeWide, ...points.map((p) => p.y)) * (1 + Y_PAD_SHARE);\n\nconst formatP = (p: number) => p.toExponential(1);\n\nconst TITLE = \"manhattan-gwas · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${rows.length.toLocaleString(\"en-US\")} variants on ${chromosomes.length} chromosomes · ${significant.length} past genome-wide significance, highlighted · lead ${SNP} labelled per chromosome`;\nconst HEADER_H = 80;\n\nfunction LeadLabels() {\n  const xScale = useXScale();\n  const yScale = useYScale();\n  return (\n    <g>\n      {leads.map((p) => (\n        <text key={p.snp} x={xScale(p.x)} y={yScale(p.y) - 14} textAnchor=\"middle\" fontSize={15} fontWeight={600} fill={t.ink}>\n          {`${p.snp} · p = ${formatP(p.p)}`}\n        </text>\n      ))}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n\n  return (\n    <div style={{ width, height, display: \"flex\", flexDirection: \"column\" }}>\n      <div style={{ height: HEADER_H, padding: \"16px 40px 0 112px\", boxSizing: \"border-box\" }}>\n        <div style={{ fontSize: 22, fontWeight: 600, color: t.ink, lineHeight: 1.25 }}>{TITLE}</div>\n        <div style={{ fontSize: 15, color: t.inkSoft, marginTop: 6 }}>{SUBTITLE}</div>\n      </div>\n      <ScatterChart\n        width={width}\n        height={height - HEADER_H}\n        skipAnimation\n        hideLegend\n        disableHitArea\n        grid={{ horizontal: true }}\n        margin={{ top: 16, right: 48, bottom: 16, left: 24 }}\n        xAxis={[\n          {\n            scaleType: \"linear\",\n            min: 0,\n            max: genomeLength,\n            tickInterval: centers,\n            tickLabelMinGap: 2,\n            valueFormatter: (v: number) => labelAtCenter.get(v) ?? \"\",\n            label: CHROMOSOME,\n            height: 64,\n            tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            min: 0,\n            max: yMax,\n            label: `−log₁₀(${P_VALUE})`,\n            width: 72,\n            tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        series={groups.map((g) => ({\n          id: g.id,\n          color: g.color,\n          markerSize: g.size,\n          data: g.points.map((p) => ({ x: p.x, y: p.y, id: p.id })),\n          valueFormatter: (_v, { dataIndex }) =>\n            `${g.points[dataIndex].snp} · ${CHROMOSOME} ${chromosomes[g.points[dataIndex].chrom]} · p = ${formatP(g.points[dataIndex].p)}`,\n        }))}\n        sx={{\n          '& [data-series=\"alternate-a\"] .MuiScatterChart-marker, & [data-series=\"alternate-b\"] .MuiScatterChart-marker': {\n            fillOpacity: 0.75,\n          },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid, strokeWidth: 1 },\n        }}\n      >\n        <ChartsReferenceLine\n          y={suggestive}\n          label={`Suggestive, p < ${formatP(SUGGESTIVE_P)}`}\n          labelAlign=\"end\"\n          spacing={{ x: 8, y: 8 }}\n          labelStyle={{ fontSize: 15, fill: t.inkSoft }}\n          lineStyle={{ stroke: t.inkSoft, strokeDasharray: \"3 5\", strokeWidth: 1.5 }}\n        />\n        <ChartsReferenceLine\n          y={genomeWide}\n          label={`Genome-wide significance, p < ${formatP(GENOME_WIDE_P)}`}\n          labelAlign=\"end\"\n          spacing={{ x: 8, y: 8 }}\n          labelStyle={{ fontSize: 15, fill: t.ink }}\n          lineStyle={{ stroke: t.ink, strokeDasharray: \"9 6\", strokeWidth: 2 }}\n        />\n        <LeadLabels />\n      </ScatterChart>\n    </div>\n  );\n}\n"}