-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlayout_rootToTipLayout.js.html
137 lines (104 loc) · 5.58 KB
/
layout_rootToTipLayout.js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: layout/rootToTipLayout.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: layout/rootToTipLayout.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>import {makeVertexFromNode} from "./layoutHelpers";
import {max, min} from "d3";
export const rootToTipVertices=(tree)=>{
if(!tree.annotations.date){
console.warn("tree must be annotated with dates to use the root to tip layout")
return [];
}
return tree.externalNodes.map(n=>({...makeVertexFromNode(n),x:n.annotations.date,y:n.divergence}));
}
// TODO add edges from tips to parent on trendline to compare outliers.
const makeTrendlineEdge=predicate=>(vertices)=>{
const usedVertices = vertices.filter(predicate);
const regression = leastSquares(usedVertices);
let x1 = min(vertices, d => d.x);
let x2 = max(vertices, d => d.x);
let y1 = 0.0;
let y2 = max(usedVertices, d => d.y);
if (usedVertices.length > 1 && regression.slope > 0.0) {
x1 = regression.xIntercept;
y2 =regression.y(x2)
}else if(usedVertices> 1 && regression.slope < 0.0 ){
x2 = regression.xIntercept;
y1= regression.y(x1);
y2=0;
}
const startPoint = {key:"startPoint",x:x1,y:y1};
const endPoint = {key:"endPoint",x:x2,y:y2};
return {edges:[{
v0: startPoint,
v1: endPoint,
key: "trendline",
id:"trendline",
classes:["trendline"],
x:startPoint.x,
y:endPoint.y,
textLabel:{ // TODO update this for regression labeling
dx:[endPoint.x,startPoint.x],
dy: -6,
alignmentBaseline: "hanging",
textAnchor:"middle",
},
}],regression:regression}
};
export const rootToTipLayout = (predicate =()=>true) => tree =>{
const vertices = rootToTipVertices(tree);
const {edges,regression} = makeTrendlineEdge(predicate)(vertices);
return {vertices,edges,regression};
};
/**
* returns slope, intercept and r-square of the line
* @param data
* @returns {{slope: number, xIntercept: number, yIntercept: number, rSquare: number, y: (function(*): number)}}
*/
function leastSquares(data) {
const xBar = data.reduce((a, b) => (a + b.x), 0.0) / data.length;
const yBar = data.reduce((a, b) => (a + b.y), 0.0) / data.length;
const ssXX = data.map((d) => Math.pow(d.x - xBar, 2))
.reduce((a, b) => a + b, 0.0);
const ssYY = data.map((d) => Math.pow(d.y - yBar, 2))
.reduce((a, b) => a + b, 0.0);
const ssXY = data.map((d) => (d.x - xBar) * (d.y - yBar))
.reduce((a, b) => a + b, 0.0);
const slope = ssXY / ssXX;
const yIntercept = yBar - (xBar * slope);
const xIntercept = -(yIntercept / slope);
const rSquare = Math.pow(ssXY, 2) / (ssXX * ssYY);
return {
slope, xIntercept, yIntercept, rSquare, y: function (x) {
return x * slope + yIntercept
}
};
}</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-bauble.html">bauble</a></li><li><a href="module-figtree.html">figtree</a></li><li><a href="module-tree.html">tree</a></li></ul><h3>Classes</h3><ul><li><a href="Axis.html">Axis</a></li><li><a href="BaubleManager.html">BaubleManager</a></li><li><a href="CircleBauble.html">CircleBauble</a></li><li><a href="Decoration.html">Decoration</a></li><li><a href="Label.html">Label</a></li><li><a href="Legend.html">Legend</a></li><li><a href="module-bauble.Bauble.html">Bauble</a></li><li><a href="module-figtree.FigTree.html">FigTree</a></li><li><a href="module-tree.Tree.html">Tree</a></li><li><a href="module-tree-Node.html">Node</a></li><li><a href="ScaleBar.html">ScaleBar</a></li></ul><h3>Global</h3><ul><li><a href="global.html#branch">branch</a></li><li><a href="global.html#branchLabel">branchLabel</a></li><li><a href="global.html#branchPathGenerator">branchPathGenerator</a></li><li><a href="global.html#circle">circle</a></li><li><a href="global.html#dateToDecimal">dateToDecimal</a></li><li><a href="global.html#decimalToDate">decimalToDate</a></li><li><a href="global.html#equalAngleLayout">equalAngleLayout</a></li><li><a href="global.html#internalNodeLabel">internalNodeLabel</a></li><li><a href="global.html#label">label</a></li><li><a href="global.html#leapYear">leapYear</a></li><li><a href="global.html#leastSquares">leastSquares</a></li><li><a href="global.html#legend">legend</a></li><li><a href="global.html#makeEdges">makeEdges</a></li><li><a href="global.html#makeVertexFromNode">makeVertexFromNode</a></li><li><a href="global.html#rectangle">rectangle</a></li><li><a href="global.html#rectangularHilightedLayout">rectangularHilightedLayout</a></li><li><a href="global.html#scaleBar">scaleBar</a></li><li><a href="global.html#tipLabel">tipLabel</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.3</a> on Sat Mar 21 2020 11:15:18 GMT+0000 (Greenwich Mean Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>