@euphrasiologist/lwphylo 1.2.4 → 1.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -447,6 +447,112 @@ function getChildArcs(pd) {
447
447
  return arcs;
448
448
  }
449
449
 
450
+ // fanAngles.js
451
+ const TAU = Math.PI * 2;
452
+ const norm = (t) => ((t % TAU) + TAU) % TAU;
453
+
454
+ // Unwrap angles around a reference so they sit within [ref-π, ref+π]
455
+ function unwrapAround(ref, a) {
456
+ let x = a;
457
+ while (x < ref - Math.PI) x += TAU;
458
+ while (x > ref + Math.PI) x -= TAU;
459
+ return x;
460
+ }
461
+
462
+ /**
463
+ * Compute APE "fan" compatible angles:
464
+ * - Tips evenly spaced over [0, span] where span = 2π*(1 - 1/Ntip) - gap
465
+ * - Then + rotate (radians)
466
+ * - Internal nodes = arithmetic mean of child angles (unwrapped)
467
+ *
468
+ * pd: fortified nodes array (has thisId, parentId, children[])
469
+ * opts: { openAngleDeg=0, rotateDeg=0 }
470
+ * returns: Map(nodeId -> angle)
471
+ */
472
+ function fanAngles(pd, opts = {}) {
473
+ const { openAngleDeg = 0, rotateDeg = 0 } = opts;
474
+ const gap = (openAngleDeg / 360) * TAU;
475
+ const rotate = (rotateDeg / 360) * TAU;
476
+
477
+ // Find root and collect tips in cladewise/DFS order
478
+ let root = null;
479
+ const kids = new Map(pd.map(d => [d.thisId, d.children || []]));
480
+ for (const d of pd) if (d.parentId == null) { root = d.thisId; break; }
481
+
482
+ const tipIds = [];
483
+ (function dfs(id) {
484
+ const c = kids.get(id) || [];
485
+ if (!c.length) { tipIds.push(id); return; }
486
+ for (const ch of c) dfs(ch);
487
+ })(root);
488
+
489
+ const N = Math.max(1, tipIds.length);
490
+ // APE: 0 .. 2π*(1 - 1/N) - gap, length.out=N (no last step overlap)
491
+ const maxA = TAU * (1 - 1 / N) - gap;
492
+ const step = N > 1 ? maxA / (N - 1) : 0;
493
+
494
+ const angle = new Map();
495
+ tipIds.forEach((id, i) => {
496
+ angle.set(id, norm(i * step + rotate));
497
+ });
498
+
499
+ // Internal nodes: arithmetic mean of child angles (unwrapped)
500
+ (function setInternal(id) {
501
+ const c = kids.get(id) || [];
502
+ for (const ch of c) setInternal(ch);
503
+ if (c.length > 0) {
504
+ // unwrap child angles around the first child's angle
505
+ const a0 = angle.get(c[0]);
506
+ const unwrapped = c.map(ch => unwrapAround(a0, angle.get(ch)));
507
+ const mean = unwrapped.reduce((s, v) => s + v, 0) / unwrapped.length;
508
+ angle.set(id, norm(mean));
509
+ }
510
+ })(root);
511
+
512
+ return angle;
513
+ }
514
+
515
+ // getArcsFan.js
516
+
517
+ /**
518
+ * Build arcs like APE's circular.plot:
519
+ * For each internal parent, draw a single arc at radius=parent.r
520
+ * going from first child's angle to last child's angle in child order.
521
+ * If last < first (wrap), we draw CW (decreasing) to stay on the block.
522
+ *
523
+ * pd: array with { thisId, parentId, r, children[], angle }
524
+ * returns: [{ parentId, thisId, radius, start, end, sweep }]
525
+ * where sweep=0 means CCW (start→end increasing),
526
+ * sweep=1 means CW (start→end decreasing across wrap).
527
+ */
528
+ function getArcsFan(pd) {
529
+ const byId = new Map(pd.map(d => [d.thisId, d]));
530
+ const arcs = [];
531
+
532
+ for (const p of pd) {
533
+ const c = p.children || [];
534
+ if (c.length < 2) continue;
535
+ const A = c.map(id => byId.get(id)?.angle).filter(a => a != null);
536
+ if (A.length < 2 || !isFinite(p.r) || p.r <= 0) continue;
537
+
538
+ // Children are contiguous in tip order; take first and last
539
+ let start = A[0];
540
+ let end = A[A.length - 1];
541
+
542
+ // Decide direction like APE’s seq(start, end):
543
+ // if end >= start → CCW; else CW across wrap.
544
+ const sweep = end >= start ? 0 : 1;
545
+
546
+ arcs.push({
547
+ parentId: p.parentId,
548
+ thisId: p.thisId,
549
+ radius: p.r,
550
+ start, end, sweep
551
+ });
552
+ }
553
+ return arcs;
554
+ }
555
+
450
556
  /**
451
557
  * Simple wrapper for radial layout:
452
558
  * - data: per-node { angle, r, x, y, ... }
@@ -454,12 +560,29 @@ function getChildArcs(pd) {
454
560
  * - arcs: per-parent arcs spanning all children at parent's radius
455
561
  * - child_arcs: per-child half-arcs (parent.angle → child.angle) at parent's radius
456
562
  */
457
- function radialLayout(node) {
563
+ function radialLayout(node, opts = {}) {
458
564
  const data = {};
459
565
  data.data = radialData(node);
460
566
  data.radii = getRadii(node);
461
567
  data.arcs = getArcs(data.data);
462
568
  data.child_arcs = getChildArcs(data.data);
569
+
570
+ const pd = fortify(node, true);
571
+ const angleMap = fanAngles(pd, {
572
+ openAngleDeg: opts.openAngleDeg ?? 0,
573
+ rotateDeg: opts.rotateDeg ?? 0
574
+ });
575
+
576
+ // stamp angles + x,y back onto pd (r stays your cumulative edge length)
577
+ for (const d of pd) {
578
+ d.angle = angleMap.get(d.thisId) ?? 0;
579
+ d.x = d.r * Math.cos(d.angle);
580
+ d.y = d.r * Math.sin(d.angle);
581
+ }
582
+
583
+ data.data_pd = pd;
584
+ data.arcs_fan = getArcsFan(pd);
585
+
463
586
  return data;
464
587
  }
465
588
 
@@ -1503,7 +1626,7 @@ function drawPhylogeny(
1503
1626
  const tipByLabel = new Map(
1504
1627
  unrootedPhylo.data.filter((d) => d.isTip).map((d) => [d.thisLabel, d])
1505
1628
  );
1506
- const rootToTip = makeRootToTipGetter(byId, { prefer: "r" });
1629
+ const rootToTip = makeRootToTipGetter(byId);
1507
1630
 
1508
1631
  if (showTooltips) {
1509
1632
  nodes