{"spec_id":"sequence-logo-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// sequence-logo-basic: Sequence Logo for Motif Visualization\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 85/100 | Updated: 2026-10-23\n\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ChartsGrid } from \"@mui/x-charts/ChartsGrid\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\nimport Box from \"@mui/material/Box\";\nimport Typography from \"@mui/material/Typography\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst LETTER_COLOR: Record<string, string> = {\n  A: t.palette[0], // #009E73\n  C: t.palette[2], // #4467A3\n  G: t.palette[3], // #BD8233\n  T: t.palette[4], // #AE3030\n};\n\n// Columns: set these to your data's column names\nconst POSITION = \"Position\"; // role: position\nconst LETTER = \"Nucleotide\"; // role: letter\nconst FREQUENCY = \"Frequency\"; // role: frequency\n\n// Data: replace this block with your own records, one object per row\nconst data = [\n  { [POSITION]: 1, [LETTER]: \"A\", [FREQUENCY]: 0.3 },\n  { [POSITION]: 1, [LETTER]: \"C\", [FREQUENCY]: 0.25 },\n  { [POSITION]: 1, [LETTER]: \"G\", [FREQUENCY]: 0.2 },\n  { [POSITION]: 1, [LETTER]: \"T\", [FREQUENCY]: 0.25 },\n  { [POSITION]: 2, [LETTER]: \"A\", [FREQUENCY]: 0.15 },\n  { [POSITION]: 2, [LETTER]: \"C\", [FREQUENCY]: 0.55 },\n  { [POSITION]: 2, [LETTER]: \"G\", [FREQUENCY]: 0.1 },\n  { [POSITION]: 2, [LETTER]: \"T\", [FREQUENCY]: 0.2 },\n  { [POSITION]: 3, [LETTER]: \"A\", [FREQUENCY]: 0.05 },\n  { [POSITION]: 3, [LETTER]: \"C\", [FREQUENCY]: 0.05 },\n  { [POSITION]: 3, [LETTER]: \"G\", [FREQUENCY]: 0.05 },\n  { [POSITION]: 3, [LETTER]: \"T\", [FREQUENCY]: 0.85 },\n  { [POSITION]: 4, [LETTER]: \"A\", [FREQUENCY]: 0.9 },\n  { [POSITION]: 4, [LETTER]: \"C\", [FREQUENCY]: 0.04 },\n  { [POSITION]: 4, [LETTER]: \"G\", [FREQUENCY]: 0.03 },\n  { [POSITION]: 4, [LETTER]: \"T\", [FREQUENCY]: 0.03 },\n  { [POSITION]: 5, [LETTER]: \"A\", [FREQUENCY]: 0.04 },\n  { [POSITION]: 5, [LETTER]: \"C\", [FREQUENCY]: 0.04 },\n  { [POSITION]: 5, [LETTER]: \"G\", [FREQUENCY]: 0.88 },\n  { [POSITION]: 5, [LETTER]: \"T\", [FREQUENCY]: 0.04 },\n  { [POSITION]: 6, [LETTER]: \"A\", [FREQUENCY]: 0.03 },\n  { [POSITION]: 6, [LETTER]: \"C\", [FREQUENCY]: 0.9 },\n  { [POSITION]: 6, [LETTER]: \"G\", [FREQUENCY]: 0.04 },\n  { [POSITION]: 6, [LETTER]: \"T\", [FREQUENCY]: 0.03 },\n  { [POSITION]: 7, [LETTER]: \"A\", [FREQUENCY]: 0.05 },\n  { [POSITION]: 7, [LETTER]: \"C\", [FREQUENCY]: 0.05 },\n  { [POSITION]: 7, [LETTER]: \"G\", [FREQUENCY]: 0.85 },\n  { [POSITION]: 7, [LETTER]: \"T\", [FREQUENCY]: 0.05 },\n  { [POSITION]: 8, [LETTER]: \"A\", [FREQUENCY]: 0.7 },\n  { [POSITION]: 8, [LETTER]: \"C\", [FREQUENCY]: 0.1 },\n  { [POSITION]: 8, [LETTER]: \"G\", [FREQUENCY]: 0.12 },\n  { [POSITION]: 8, [LETTER]: \"T\", [FREQUENCY]: 0.08 },\n  { [POSITION]: 9, [LETTER]: \"A\", [FREQUENCY]: 0.2 },\n  { [POSITION]: 9, [LETTER]: \"C\", [FREQUENCY]: 0.3 },\n  { [POSITION]: 9, [LETTER]: \"G\", [FREQUENCY]: 0.3 },\n  { [POSITION]: 9, [LETTER]: \"T\", [FREQUENCY]: 0.2 },\n  { [POSITION]: 10, [LETTER]: \"A\", [FREQUENCY]: 0.25 },\n  { [POSITION]: 10, [LETTER]: \"C\", [FREQUENCY]: 0.25 },\n  { [POSITION]: 10, [LETTER]: \"G\", [FREQUENCY]: 0.25 },\n  { [POSITION]: 10, [LETTER]: \"T\", [FREQUENCY]: 0.25 },\n];\n\n// Plot: reads only data and the constants above\nconst positions = Array.from(new Set(data.map((d) => `${d[POSITION]}`)));\nconst letters = Array.from(new Set(data.map((d) => d[LETTER])));\nconst MAX_BITS = Math.log2(letters.length);\n\n// Letters without a fixed colour (an RNA U, amino acids) take the palette\n// slots LETTER_COLOR leaves free, in order of appearance, never a used hue.\nconst usedColors = new Set(Object.values(LETTER_COLOR));\nconst freeColors = t.palette.filter((c: string) => !usedColors.has(c));\nconst otherLetters = letters.filter((l) => !(l in LETTER_COLOR));\nconst letterColor = (letter: string) =>\n  LETTER_COLOR[letter] ?? freeColors[otherLetters.indexOf(letter)];\n\nconst STACKS = positions.map((posLabel) => {\n  const rows = data.filter((d) => `${d[POSITION]}` === posLabel);\n  const entropy = rows.reduce((bits, r) => {\n    const p = r[FREQUENCY];\n    return p > 0 ? bits - p * Math.log2(p) : bits;\n  }, 0);\n  const ic = MAX_BITS - entropy;\n  const sorted = [...rows].sort((a, b) => a[FREQUENCY] - b[FREQUENCY]);\n  let cumulative = 0;\n  return sorted\n    .filter((r) => r[FREQUENCY] > 0)\n    .map((r) => {\n      const bits = r[FREQUENCY] * ic;\n      const letter = r[LETTER];\n      const segment = {\n        letter,\n        from: cumulative,\n        to: cumulative + bits,\n        color: letterColor(letter),\n      };\n      cumulative += bits;\n      return segment;\n    });\n});\n\nconst CAP_HEIGHT_RATIO = 0.72;\nconst VERTICAL_STRETCH = 1 / CAP_HEIGHT_RATIO;\n\nfunction SequenceLogoGlyphs() {\n  const xScale = useXScale() as any;\n  const yScale = useYScale() as any;\n  const bandwidth = xScale.bandwidth();\n  const paddingX = bandwidth * 0.08;\n  const usableWidth = bandwidth - 2 * paddingX;\n\n  return (\n    <g>\n      {STACKS.map((segments, posIndex) =>\n        segments.map((seg) => {\n          const xLeft = xScale(positions[posIndex]) + paddingX;\n          const yBottom = yScale(seg.from);\n          const yTop = yScale(seg.to);\n          const heightPx = yBottom - yTop;\n          if (heightPx < 1.5) return null;\n          const translateY = yBottom * (1 - VERTICAL_STRETCH);\n          return (\n            <text\n              key={`${posIndex}-${seg.letter}`}\n              x={xLeft + usableWidth / 2}\n              y={yBottom}\n              textAnchor=\"middle\"\n              textLength={usableWidth}\n              lengthAdjust=\"spacingAndGlyphs\"\n              fontSize={heightPx}\n              fontWeight={700}\n              fontFamily=\"Arial, Liberation Sans, DejaVu Sans, sans-serif\"\n              fill={seg.color}\n              transform={`translate(0 ${translateY}) scale(1 ${VERTICAL_STRETCH})`}\n            >\n              {seg.letter}\n            </text>\n          );\n        }),\n      )}\n    </g>\n  );\n}\n\nconst TITLE_H = 56;\n\nexport default function Chart() {\n  const W = window.ANYPLOT_SIZE.width;\n  const H = window.ANYPLOT_SIZE.height;\n\n  return (\n    <Box sx={{ width: W, height: H, bgcolor: t.pageBg, display: \"flex\", flexDirection: \"column\" }}>\n      <Typography\n        sx={{\n          color: t.ink,\n          fontSize: 22,\n          fontWeight: 500,\n          textAlign: \"center\",\n          height: TITLE_H,\n          lineHeight: `${TITLE_H}px`,\n          flexShrink: 0,\n        }}\n      >\n        sequence-logo-basic · javascript · muix · anyplot.ai\n      </Typography>\n      <ChartsContainer\n        width={W}\n        height={H - TITLE_H}\n        series={[]}\n        skipAnimation\n        margin={{ top: 30, right: 40, bottom: 20, left: 20 }}\n        xAxis={[\n          {\n            scaleType: \"band\",\n            data: positions,\n            height: 64,\n            label: POSITION,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            scaleType: \"linear\",\n            min: 0,\n            max: MAX_BITS,\n            width: 84,\n            label: \"Information content (bits)\",\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        sx={{\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft, strokeWidth: 1 },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid },\n        }}\n      >\n        <ChartsGrid horizontal />\n        <SequenceLogoGlyphs />\n        <ChartsXAxis />\n        <ChartsYAxis />\n      </ChartsContainer>\n    </Box>\n  );\n}\n"}