Atlaslog
Atlaslog / Examples / deck.gl / Business scenarios / Protein Backbone
deck.gl · Business scenario

Protein Backbone

320 residues fold into 20 helical segments; the backbone is colored per segment, and the backbone, the Cα atoms or both can be shown with line width adjustable in real time.

Open standalone → Chinese notes All business examples
/demos/deckgl-nongis-protein.htmlLIVE
single HTML file · CDN dependencies onlyinterface labels are in Chinese

Business context

Backbone and Cα traces are the two most common abstractions in structural biology; people doing cryo-EM model building, homology comparison and molecular dynamics analysis use them to judge secondary-structure direction and domain folding. Data comes from PDB atom records or simulation trajectories. A structure usually has hundreds to thousands of residues; 320 residues in 20 helices here is close to a medium-sized domain.

PDB coordinates live in a crystallographic frame, so Cα atoms are extracted, superimposed, split by chain and secondary structure, then scaled to a common radius so different structures can be compared in one view. Cesium targets ground scale, and three.js must generate ribbon meshes and normals; PathLayer draws at pixel widths, so 320 residues need no geometry and line width is a single parameter.

The helix radius and pitch are scaled up, so the 2.3 Å radius after the scale factor cannot be used to estimate true dimensions. Segment coloring only marks how secondary structure was split; residues within a segment are not otherwise alike. In 'atoms only' mode the spatial relations are inferred from Cα positions alone, and a chain that looks broken is not a missing structure.

How it works

The backbone is generated in 20 helical segments of 16 residues; within a segment each residue advances 3.8 Å along the axis while rotating 1.75 rad per residue around it at a helix radius of 2.3 Å. Each segment direction mixes a 0.35 random component into the previous one so the chain does not fold back on itself. All points are multiplied by 1.6, translated by the bounding-box center, and scaled by the longest edge to a radius of 230 scene units; OrbitView uses a narrow fovy of 20, so the whole chain fits at zoom 0.2.

PathLayer data is 20 paths of 16 points each, and one path takes a single color, so interpolating along a six-color ramp by segment index costs far fewer vertex attributes than a per-vertex getColor; LineLayer can only draw two-point segments, which would need 319 records for 320 residues and still show seams at the joints. Width uses widthUnits: 'pixels' with widthMinPixels: 1 so the backbone does not vanish when zoomed out; the 'atoms' mode keeps only Cα spheres of 2.6 px, where the 3.6-residue period of each helix turn becomes directly visible.

SettingEffect
widthUnits: 'pixels'Line width fixed in screen pixels, slider 1–12
20 pathsOne path per helix, colored by segment index
3.8 Å / 1.75 radAxial step and rotation per residue, about 3.6 residues per turn
capRounded / jointRoundedRounds endpoints and joints so paths show no square corners

Note. Split paths by secondary structure and give each segment one color; this costs fewer attributes than per-vertex coloring and makes helix boundaries legible at a glance.

Source

demos/deckgl-nongis-protein.html
<!--
  示例:蛋白质主链
  要点:320 个残基折成 20 段螺旋,PathLayer 按像素线宽画主链,ScatterplotLayer 叠 Cα 原子
  依赖:deck.gl 9.0 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>蛋白质主链</title>
<style>
  html,body{height:100%;margin:0;background:#0d1117}
  body{display:flex;flex-direction:column;overflow:hidden;
       font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#e6e8ec}
  .bar{position:relative;z-index:5;flex:0 0 auto;padding:8px 12px;background:#161b22;border-bottom:1px solid #252c36;
       display:flex;gap:14px;align-items:center;font-size:12px;color:#8b93a1;flex-wrap:wrap}
  .bar b{color:#58a6ff;font-weight:600}
  .bar label{display:flex;align-items:center;gap:5px;cursor:pointer}
  .bar input[type=checkbox]{accent-color:#4f9ef8}
  .bar input[type=range]{width:150px;accent-color:#4f9ef8}
  .bar button{font:inherit;font-size:12px;padding:3px 10px;border-radius:4px;cursor:pointer;
       border:1px solid #2f3947;background:#1d242e;color:#c9d3e0}
  .bar button:hover{background:#26303d}
  .bar button.on{background:#2c4a7a;border-color:#4f8ad6;color:#dfe9ff}
  .bar .sep{width:1px;height:18px;background:#2a313c;margin:0 4px}
  #cv{flex:1 1 auto;min-height:0;width:100%;display:block}
</style>
</head>
<body>
<div class="bar">
  <span>残基 <b>320</b> · 20 段螺旋</span><span class="sep"></span>
  <button data-v="chain">主链</button>
  <button data-v="atoms">原子</button>
  <button data-v="both" class="on">一起</button>
  <label>线宽 <input id="w" type="range" min="1" max="12" value="4"><b id="wv">4 px</b></label>
</div>
<canvas id="cv"></canvas>
<script src="https://cdn.jsdelivr.net/npm/deck.gl@9.0.35/dist.min.js"></script>
<script>
(function () {
  'use strict';

  if (typeof deck === 'undefined') {
    document.body.innerHTML =
      '<div style="padding:24px;color:#ff8c74">deck.gl 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
    return;
  }

  function rng(seed) {
    var a = seed >>> 0;
    return function () {
      a = (a + 0x6D2B79F5) >>> 0;
      var t = Math.imul(a ^ (a >>> 15), 1 | a);
      t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
      return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
    };
  }

  var RES = 320, SEG = 16, NSEG = RES / SEG, S = 1.6;
  var STOPS = [
    [64, 154, 255], [64, 206, 226], [86, 214, 168],
    [246, 186, 78], [240, 104, 72], [176, 132, 255]
  ];
  var rand = rng(5150);
  var i, j, k;

  function unitVec() {
    var x = rand() * 2 - 1, y = rand() * 2 - 1, z = rand() * 2 - 1;
    var l = Math.sqrt(x * x + y * y + z * z) || 1;
    return [x / l, y / l, z / l];
  }

  var SEG_COLORS = [];
  for (i = 0; i < NSEG; i++) {
    var u = i / (NSEG - 1) * (STOPS.length - 1);
    var s0 = Math.min(STOPS.length - 2, Math.floor(u));
    var f = u - s0;
    SEG_COLORS.push([
      Math.round(STOPS[s0][0] + (STOPS[s0 + 1][0] - STOPS[s0][0]) * f),
      Math.round(STOPS[s0][1] + (STOPS[s0 + 1][1] - STOPS[s0][1]) * f),
      Math.round(STOPS[s0][2] + (STOPS[s0 + 1][2] - STOPS[s0][2]) * f),
      235
    ]);
  }

  var dir = unitVec();
  var start = [0, 0, 0];
  var paths = [], all = [];

  for (k = 0; k < NSEG; k++) {
    var nd = unitVec();
    var mix = [dir[0] * 0.35 + nd[0], dir[1] * 0.35 + nd[1], dir[2] * 0.35 + nd[2]];
    var ml = Math.sqrt(mix[0] * mix[0] + mix[1] * mix[1] + mix[2] * mix[2]) || 1;
    dir = [mix[0] / ml, mix[1] / ml, mix[2] / ml];

    var tmp = Math.abs(dir[2]) < 0.9 ? [0, 0, 1] : [1, 0, 0];
    var u = [
      dir[1] * tmp[2] - dir[2] * tmp[1],
      dir[2] * tmp[0] - dir[0] * tmp[2],
      dir[0] * tmp[1] - dir[1] * tmp[0]
    ];
    var ul = Math.sqrt(u[0] * u[0] + u[1] * u[1] + u[2] * u[2]) || 1;
    u = [u[0] / ul, u[1] / ul, u[2] / ul];
    var v = [
      dir[1] * u[2] - dir[2] * u[1],
      dir[2] * u[0] - dir[0] * u[2],
      dir[0] * u[1] - dir[1] * u[0]
    ];

    var path = [];
    for (j = 0; j < SEG; j++) {
      var aa = j * 1.75;
      var along = j * 3.8;
      var ca = Math.cos(aa) * 2.3;
      var sa = Math.sin(aa) * 2.3;
      path.push([
        (start[0] + dir[0] * along + u[0] * ca + v[0] * sa) * S,
        (start[1] + dir[1] * along + u[1] * ca + v[1] * sa) * S,
        (start[2] + dir[2] * along + u[2] * ca + v[2] * sa) * S
      ]);
      all.push(path[path.length - 1]);
    }
    start = [path[SEG - 1][0] / S, path[SEG - 1][1] / S, path[SEG - 1][2] / S];
    paths.push({ path: path, color: SEG_COLORS[k] });
  }

  var minX = 1e9, minY = 1e9, minZ = 1e9, maxX = -1e9, maxY = -1e9, maxZ = -1e9;
  for (i = 0; i < all.length; i++) {
    if (all[i][0] < minX) minX = all[i][0];
    if (all[i][1] < minY) minY = all[i][1];
    if (all[i][2] < minZ) minZ = all[i][2];
    if (all[i][0] > maxX) maxX = all[i][0];
    if (all[i][1] > maxY) maxY = all[i][1];
    if (all[i][2] > maxZ) maxZ = all[i][2];
  }
  var ox = (minX + maxX) / 2, oy = (minY + maxY) / 2, oz = (minZ + maxZ) / 2;
  var fit = 230 / (Math.max(maxX - minX, maxY - minY, maxZ - minZ) / 2);
  for (i = 0; i < all.length; i++) {
    all[i][0] = (all[i][0] - ox) * fit;
    all[i][1] = (all[i][1] - oy) * fit;
    all[i][2] = (all[i][2] - oz) * fit;
  }

  var view = 'both', lineW = 4;

  function buildLayers() {
    var layers = [];
    if (view !== 'atoms') {
      layers.push(new deck.PathLayer({
        id: 'protein-backbone',
        data: paths,
        coordinateSystem: deck.COORDINATE_SYSTEM.CARTESIAN,
        getPath: function (d) { return d.path; },
        getColor: function (d) { return d.color; },
        getWidth: lineW,
        widthUnits: 'pixels',
        widthMinPixels: 1,
        capRounded: true,
        jointRounded: true,
        pickable: false
      }));
    }
    if (view !== 'chain') {
      layers.push(new deck.ScatterplotLayer({
        id: 'protein-ca',
        data: all,
        coordinateSystem: deck.COORDINATE_SYSTEM.CARTESIAN,
        getPosition: function (d) { return d; },
        getRadius: 2.6,
        radiusUnits: 'pixels',
        radiusMinPixels: 1,
        getFillColor: [236, 242, 250, 240],
        stroked: true,
        getLineColor: [40, 70, 110, 220],
        getLineWidth: 1,
        lineWidthUnits: 'pixels',
        pickable: false
      }));
    }
    return layers;
  }

  var deckgl = new deck.Deck({
    canvas: 'cv',
    width: '100%',
    height: '100%',
    views: new deck.OrbitView({ target: [0, 0, 0], fovy: 20 }),
    initialViewState: { target: [0, 0, 0], rotationX: 26, rotationOrbit: -38, zoom: 0.2 },
    controller: true,
    layers: buildLayers()
  });

  var btns = document.querySelectorAll('.bar button[data-v]');
  for (i = 0; i < btns.length; i++) {
    btns[i].addEventListener('click', function () {
      view = this.getAttribute('data-v');
      for (j = 0; j < btns.length; j++) {
        btns[j].className = btns[j] === this ? 'on' : '';
      }
      deckgl.setProps({ layers: buildLayers() });
    });
  }

  document.getElementById('w').addEventListener('input', function () {
    lineW = +this.value;
    document.getElementById('wv').textContent = lineW + ' px';
    deckgl.setProps({ layers: buildLayers() });
  });

  window.__deck = deckgl;
})();
</script>
</body>
</html>
Language: this page is in English; the demo keeps Chinese interface labels and code comments. The Chinese page carries the full notes and the same runnable file.

More business examples

deck.gl · APIPathLayer deck.gl · APILineLayer deck.gl · APIScatterplotLayer