Skip to content

Instantly share code, notes, and snippets.

@bherr2
Last active October 25, 2021 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bherr2/0ebc137a2c0e0b43b244a52b9ee5428c to your computer and use it in GitHub Desktop.
Save bherr2/0ebc137a2c0e0b43b244a52b9ee5428c to your computer and use it in GitHub Desktop.
HuBMAP CCF Registrations (2)
license: MIT
height: 1024
scrolling: yes
border: yes

HuBMAP CCF Registrations

Report for HuBMAP Data that have CCF "rui locations". See https://hubmapconsortium.github.io/ccf/ for more information.

Access to my data

To access your data, grab your API key and set it using localStorage.setItem('HUBMAP_KEY', 'my-key-goes-here') from the developer console and refresh. Note that keys expire and may cause the query to fail after a while. In that case, simply update the key above or delete it via the developer console.

<!doctype html>
<html lang="en">
<head>
<title>HuBMAP CCF Registrations</title>
<script src="https://cdn.jsdelivr.net/npm/vega@5"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-lite@4"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-embed@6"></script>
<link href="https://cdn.jsdelivr.net/npm/tabulator-tables/dist/css/tabulator.min.css" rel="stylesheet">
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/tabulator-tables/dist/js/tabulator.min.js"></script>
<script src="main.js"></script>
</head>
<body>
<div id="visualization" style="width: 920px"></div>
<div style="text-align: right; width: 920px; margin: 5px;">
<button onclick="setApiKey()">(Optional) Set API Key</button>
<button onclick="downloadTable()">Download Table as CSV</button>
</div>
<div id="table" style="width: 920px; height: 600px;"></div>
</body>
</html>
/* jshint esversion: 6 */
function setApiKey() {
const apiKey = prompt('Enter API Key (leave blank to clear)', '');
localStorage.removeItem('HUBMAP_KEY');
if (apiKey.trim().length > 0) {
localStorage.setItem('HUBMAP_KEY', apiKey);
}
location.reload();
}
function resultsAsDatasets(results) {
const data = results['@graph'];
const items = { };
const locationItems = [];
for (const donor of data) {
const id = donor['@id'];
if (!items[id]) {
items[id] = {
donor: Object.assign({}, donor, {samples: undefined, datasets: undefined}),
blocks: [],
sections: [],
datasets: []
};
}
const item = items[id];
item.datasets = item.datasets.concat(donor.datasets || []);
for (const block of (donor.samples || [])) {
item.blocks.push(Object.assign({}, block, {samples: undefined, datasets: undefined, rui_location: undefined}));
item.datasets = item.datasets.concat(block.datasets || []);
if (block.sections && block.sections.length > 0) {
for (const section of block.sections) {
item.sections.push(Object.assign({}, section, {samples: undefined, datasets: undefined}));
item.datasets = item.datasets.concat(section.datasets || []);
locationItems.push({
donor, rui_location: block.rui_location, block, section, datasets: [...donor.datasets || [], ...block.datasets, ...section.datasets]
});
}
} else {
locationItems.push({
donor, rui_location: block.rui_location, block, section: false, datasets: [...donor.datasets || [], ...block.datasets]
});
}
}
}
const csvItems = locationItems.map(row => {
return {
donor: row.donor.link,
age: row.donor.age,
sex: row.donor.sex,
bmi: row.donor.bmi,
provider: (row.donor.provider_name || 'General Electric').replace(/TMC-/, ''),
block: row.block.link,
section_count: row.block.section_count,
section_size: row.block.section_size,
section: row.section ? row.section.link : '',
section_number: row.section ? row.section.section_number : '',
location: row.rui_location['@id'],
x_dimension: row.rui_location.x_dimension,
y_dimension: row.rui_location.y_dimension,
z_dimension: row.rui_location.z_dimension,
ref_organ: '#' + row.rui_location.placement.target.split('#')[1].replace(/\_Patch|CC1|CC2|CC3/g, ''),
organ: row.rui_location.placement.target.split('#')[1].slice(3).replace(/\_Patch|CC1|CC2|CC3|Left|Right/g, '').replace('Colon', 'Lg Intestine'),
num_as: (row.rui_location.ccf_annotations || []).length,
datasets: row.datasets.map(d => d.link).join('; '),
dataset_count: row.datasets.length,
technologies: [ ...new Set(row.datasets.map(d => d.technology))].sort().join('; ')
};
});
const stats = {
numBlocks: new Set(),
numSections: new Set(),
numRuiLocations: new Set()
};
for (const item of csvItems) {
stats.numBlocks.add(item.block);
stats.numSections.add(item.section);
stats.numRuiLocations.add(item.location);
}
stats.numBlocks = stats.numBlocks.size;
stats.numSections = stats.numSections.size;
stats.numRuiLocations = stats.numRuiLocations.size;
return { data, locationItems, csvItems, stats };
}
let table;
function downloadTable() {
if (table) {
table.download("csv", "data.csv");
}
}
function main() {
let searchUri = 'https://hubmap-link-api.herokuapp.com/hubmap-datasets?format=jsonld';
if (localStorage.getItem('HUBMAP_KEY')) {
searchUri = `${searchUri}&token=${localStorage.getItem('HUBMAP_KEY')}`;
}
Promise.all([
fetch("vis.vl.json").then((result) => result.json()),
fetch(searchUri).then((result) => result.json())
]).then(([spec, jsonData]) => {
// Embed the graph data in the spec for ease of use from Vega Editor
spec.datasets = resultsAsDatasets(jsonData);
const stats = spec.datasets.stats;
console.log(spec.datasets);
spec.vconcat[1].hconcat[0].encoding.x.title += ` (${stats.numBlocks} Total)`;
spec.vconcat[1].hconcat[1].encoding.x.title += ` (${stats.numSections} Total)`;
spec.vconcat[1].hconcat[2].encoding.x.title += ` (${stats.numRuiLocations} Total)`;
table = new Tabulator("#table", {
data: spec.datasets.csvItems,
autoColumns: true
});
return vegaEmbed("#visualization", spec, { "renderer": "svg", "actions": true });
}).then((results) => {
console.log("Visualization successfully loaded");
});
}
main();
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"description": "Report for HuBMAP CCF Registrations",
"autosize": {"type": "fit", "resize": true},
"config": {"view": {"strokeOpacity": 0}, "style": {"guide-label": {"width": 10}}},
"vconcat": [
{
"hconcat": [
{
"mark": {"type": "bar", "tooltip": {"content": "data"}},
"data": {"name": "csvItems"},
"transform": [
{"calculate": "datum.organ", "as": "organ"}
],
"encoding": {
"x": {"aggregate": "distinct", "field": "location", "title": "# RUI Locations"},
"y": {"field": "ref_organ", "type": "nominal", "sort": "y", "title": "Reference Organ"}
}
},
{
"mark": {"type": "bar", "tooltip": {"content": "data"}},
"data": {"name": "csvItems"},
"transform": [
{"calculate": "datum.organ", "as": "organ"}
],
"encoding": {
"x": {"aggregate": "distinct", "field": "location", "title": "# RUI Locations"},
"y": {"field": "organ", "type": "nominal", "sort": "y", "title": "Organ"}
}
}
]
},
{
"hconcat": [
{
"mark": {"type": "bar", "tooltip": {"content": "data"}},
"data": {"name": "csvItems"},
"encoding": {
"x": {"aggregate": "distinct", "field": "block", "title": "# Blocks"},
"y": {"field": "organ", "type": "nominal", "sort": "y", "title": "Organ"}
}
},
{
"mark": {"type": "bar", "tooltip": {"content": "data"}},
"data": {"name": "csvItems"},
"encoding": {
"x": {"aggregate": "distinct", "field": "section", "title": "# Sections"},
"y": {"field": "organ", "type": "nominal", "sort": "y", "title": "Organ"}
}
},
{
"mark": {"type": "bar", "tooltip": {"content": "data"}},
"data": {"name": "csvItems"},
"encoding": {
"x": {"aggregate": "distinct", "field": "location", "title": "# RUI Locations"},
"y": {"field": "organ", "type": "nominal", "sort": "y", "title": "Organ"}
}
}
]
},
{
"hconcat": [
{
"mark": {"type": "bar", "tooltip": {"content": "data"}},
"data": {"name": "csvItems"},
"encoding": {
"x": {"aggregate": "distinct", "field": "block", "title": "# Blocks"},
"y": {"field": "provider", "type": "nominal", "sort": "y", "title": "Tissue Provider"}
}
},
{
"mark": {"type": "bar", "tooltip": {"content": "data"}},
"data": {"name": "csvItems"},
"encoding": {
"x": {"aggregate": "distinct", "field": "section", "title": "# Sections"},
"y": {"field": "provider", "type": "nominal", "sort": "y", "title": "Tissue Provider"}
}
},
{
"mark": {"type": "bar", "tooltip": {"content": "data"}},
"data": {"name": "csvItems"},
"encoding": {
"x": {"aggregate": "distinct", "field": "location", "title": "# RUI Locations"},
"y": {"field": "provider", "type": "nominal", "sort": "y", "title": "Tissue Provider"}
}
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment