Public
Edited
Apr 13, 2023
Insert cell
Insert cell
// score_names2.map((name, index) => {
// return {
// label: name,
// position: index,
// format: (value, d) => value ? format(value) : "",
// x: 670 + (index - 2) * 3,
// fill: (value) => null,
// };
// });
Insert cell
color_scale = {
const margin = {top: 10, right: 50, bottom: 20, left: 550};
const width = 300;
const height = 30;

const key = "Value"; // Use "Rank" or "Value" based on your use-case
const colorScale = d3.scaleSequential()
.domain([0, 0.4])
.interpolator(d3.interpolateViridis);

const svg = d3.create("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);

const g = svg.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);

const gradient = g.append("linearGradient")
.attr("id", "gradient")
.attr("x1", "0%")
.attr("y1", "100%")
.attr("x2", "100%")
.attr("y2", "100%")
.attr("spreadMethod", "pad");

for (let i = 0; i <= 1; i += 0.01) {
gradient.append("stop")
.attr("offset", i * 100 + "%")
.attr("stop-color", colorScale(i * 0.4))
.attr("stop-opacity", 1);
}

g.append("rect")
.attr("width", width)
.attr("height", height)
.style("fill", "url(#gradient)")
.attr("transform", `translate(0,0)`);

const axisScale = d3.scaleLinear()
.domain([0, 0.4])
.range([0, width]);

const axisBottom = g => g
.attr("class", `x-axis`)
.attr("transform", `translate(0,${height})`)
.call(d3.axisBottom(axisScale).ticks(5))
.call(g => g.select(".domain").remove());

g.append("g")
.call(axisBottom);

return svg.node();
}
Insert cell
data
Insert cell
Insert cell
Insert cell
function uncollapseNodesByEfoTrue(efoTrue, json) {
const nodes = findNodesByName(efoTrue, json);
nodes.forEach(node => {
node.collapsed = false;
node.children = node._children;
});
}
Insert cell
function collapseNodesByEfoTrue(efoTrue, json) {
const nodes = findNodesByName(efoTrue, json);
nodes.forEach(node => {
node.collapsed = true;
node.children = null;
});
}


Insert cell
function getTextColor(d, key) {
const value = data_attr[d.data.efo_true][key];
let normalizedValue;

if (key === "Rank") {
normalizedValue = 1 - (value - 1) / (500 - 1);
return d3.interpolateGreens(normalizedValue);
} else {
normalizedValue = value;
const colorScale = d3.scaleSequential(normalizedValue)
.domain([0, 0.4])
.interpolator(d3.interpolateViridis);
return colorScale(normalizedValue);
}
}
Insert cell
function fill_zeros_text(key) {
if (countNonZero_attributes(data_attr, key) === 0) {
return "lightgrey";
} else {
return "black";
}
}
Insert cell
function fill_zeros(key) {
if (countNonZero_attributes(data_attr, key) === 0) {
return 0.1;
} else {
return 1;
}
}
Insert cell
countNonZero_attributes(data_attr, "mutation")
Insert cell
function countNonZero_attributes(jsonObj, keys, count = 0) {
if (typeof jsonObj === 'object' && jsonObj !== null) {
if (Array.isArray(jsonObj)) {
jsonObj.forEach(item => {
count = countNonZero_attributes(item, keys, count);
});
} else {
Object.keys(jsonObj).forEach(key => {
if (keys.includes(key) && jsonObj[key] !== 0) {
count += 1;
}
count = countNonZero_attributes(jsonObj[key], keys, count);
});
}
}
return count;
}
Insert cell
countNodesWithEfoTrue(data, "EFO_0004215")
Insert cell
function countNodesWithEfoTrue(jsonObj, efoTrueToCount, count = 0) {
if (typeof jsonObj === 'object' && jsonObj !== null) {
if (jsonObj.hasOwnProperty('efo_true') && jsonObj.efo_true === efoTrueToCount) {
count += 1;
}

if (jsonObj.hasOwnProperty('children') && Array.isArray(jsonObj.children)) {
jsonObj.children.forEach(child => {
count = countNodesWithEfoTrue(child, efoTrueToCount, count);
});
}
}
return count;
}
Insert cell
function mapper(d, key){
const value = data_attr[d.data.efo_true][key];
return key === "Rank" ? Math.round(value) : value.toFixed(4);
}
Insert cell
data_attr["EFO_0000408"]["network"]
Insert cell
data_attr = FileAttachment("attributes_ENSG00000000419 (2).json").json()
Insert cell
Insert cell
test_var = 10
Insert cell
Insert cell
mutable cachedColumnData = null
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data
Insert cell
function toggleNodeVisibility(nodeId, json) {
function findAndUpdateNode(json) {
if (json.id === nodeId) {
if (!_.has(json, 'collapsed')) {
json.collapsed = true;
} else {
json.collapsed = !json.collapsed;
}
json.children = json.collapsed ? null : json._children;
return;
}

if (json.children) {
json.children.forEach(child => {
findAndUpdateNode(child);
});
}
}

findAndUpdateNode(json);
}

// function toggleNodeVisibility(nodeId, json, show = null) {
// function findAndUpdateNode(json) {
// if (json.id === nodeId) {
// // If show is not null, use its value to set the display property
// if (show !== null) {
// json.display = show ? "" : "none";
// }
// // If show is null, toggle the display property
// else {
// json.display = json.display === "none" ? "" : "none";
// }
// return;
// }

// if (json.children) {
// json.children.forEach(child => {
// findAndUpdateNode(child);
// });
// }
// }

// findAndUpdateNode(json);
// // Redraw the tree
// update(root);
// }
Insert cell
toggleNodeVisibility(3, data)
Insert cell
function toggleNodeVisibility3(nodeId, json) {
function findAndUpdateNode(json) {
if (json.id === nodeId) {
if (!_.has(json, 'collapsed')) {
json.collapsed = true;
} else {
json.collapsed = !json.collapsed;
}
json.children = json.collapsed ? null : json._children;
return;
}

if (json.children) {
json.children.forEach(child => {
findAndUpdateNode(child);
});
}
}

findAndUpdateNode(json);
}
Insert cell
function toggleNodeVisibility2(nodeId, json) {
let targetNode;

function findNodeById(json) {
if (json.id === nodeId) {
targetNode = json;
return;
}

if (json.children) {
json.children.some(child => {
findNodeById(child);
return targetNode !== undefined;
});
}
}

findNodeById(json);
return targetNode
if (targetNode) {
if (!_.has(targetNode, 'collapsed')) {
targetNode.collapsed = true;
} else {
targetNode.collapsed = !targetNode.collapsed;
}
targetNode.children = targetNode.children ? null : targetNode._children;
}
}
Insert cell
dat2 = addUniqueIdToNodes(data)
Insert cell
function addUniqueIdToNodes(json, nodeId = 0) {
// Assign a unique ID to the current node
json.id = nodeId++;
// If the current node has children, iterate through them recursively
if (json.children) {
json.children.forEach(child => {
nodeId = addUniqueIdToNodes(child, nodeId);
});
}
return nodeId;
}

Insert cell
data
Insert cell
findAncestors(data, 40886)
Insert cell
function findALLAncestors(json, nodeName) {
if (json.efo_true === nodeName) {
return [{ id: json.id, name: json.efo_true }];
}
if (json.children) {
for (let child of json.children) {
let ancestors = findALLAncestors(child, nodeName);
if (ancestors !== null) {
ancestors.unshift({ id: json.id, name: json.efo_true });
return ancestors;
}
}
}
return null;
}

Insert cell
function findAncestors(json, id) {
if (json.id === id) {
return [];
}
if (json.children) {
for (let child of json.children) {
let ancestors = findAncestors(child, id);
if (ancestors !== null) {
ancestors.push(json.id);
return ancestors;
}
}
}
return null;
}

Insert cell
// findNodesByIndex("EFO_0007450", data)
Insert cell
function NameTo(targetId, json, foundNames = []) {
if (json.id === targetId) {
foundNames.push(json.efo_true);
}

if (json.children) {
json.children.forEach(child => {
findNodesByName(targetId,child, foundNames);
});
}

return foundNames;
}
Insert cell
findNodesByName("EFO_0007450", data)
Insert cell
function findNodesByName(targetId, json, foundNames = []) {
if (json.efo_true === targetId) {
foundNames.push(json.id);
}

if (json.children) {
json.children.forEach(child => {
findNodesByName(targetId,child, foundNames);
});
}

return foundNames;
}

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// defaultCollapsed
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function indexEachBefore2(r, nodes) {
let i = 0;
let y = 0;
r.eachBefore(n => {
if (n.parent && n.parent !== r) {
// If the node has a previous sibling, update the y position based on the previous sibling's totalLines
if (n.previousSibling) {
let nodeHeight = nodeSize * (n.previousSibling.totalLines || 1);
y += nodeHeight;
} else {
n.index = i++;
}
return r
} })}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
//import {DescribeObject} from "@tsenyi/json-object-statistics"
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more