Created
February 10, 2016 12:43
-
-
Save jsvine/6ed721172a7f5019332b to your computer and use it in GitHub Desktop.
Script to convert Organ Procurement and Transplantation Network website data reports into spreadsheet-friendly tab-separated values (TSV) files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* OPTM Report -> TSV | |
The script below takes reports generated by the Organ Procurement and Transplantation | |
Network website and converts them into spreadsheet-friendly tab-separated values (TSV) files. | |
Instructions: | |
1. Create an advanced data report at https://optn.transplant.hrsa.gov/converge/LatestData/advancedData.asp. In Step 4, keep the default options ("Counts" + "Portrait"). | |
2. On the results page, open up your browser's console. | |
a. On Chrome: command-option-j | |
b. On Firefox: command-option-k | |
c. On Safari: command-option-i | |
3. Paste the JavaScript code below into the console, and press enter/return. | |
This should download a file of your data, or open the file in a new window. | |
=== Subscribe to Data Is Plural: https://tinyletter.com/data-is-plural === */ | |
(function () { | |
var zip = function (arrays) { | |
return [].map.call(arrays[0], function (_, i) { | |
return arrays.map(function (array) { return array[i] }); | |
}); | |
}; | |
var to_tsv = function (data) { | |
return data.map(function (row) { | |
return row.join("\t"); | |
}).join("\n").replace(/</g, "<"); | |
}; | |
var title = document.querySelector(".reportTitle").innerHTML; | |
var bys = title.split(" by ")[1].split(", "); | |
var thead_trs = document | |
.querySelector(".dataGrid thead") | |
.querySelectorAll("tr"); | |
var thead_arrays = [].map.call(thead_trs, function (tr) { | |
return [].map.call(tr.querySelectorAll("th"), function (th) { | |
return th.innerHTML | |
}); | |
}); | |
var thead_zipped = zip(thead_arrays); | |
var theaders = thead_zipped.map(function (z) { | |
return z.join(" - "); | |
}); | |
var header = bys.concat(theaders); | |
var parse = function (x) { | |
if (x.search(/^[\d,]+$/) > -1) { | |
return parseInt(x.replace(/,/g, "")); | |
} | |
return x.replace(/ /g, ""); | |
}; | |
var rows = document.querySelectorAll(".dataGrid[cellpadding='1'] tbody tr"); | |
var last_seen = {}; | |
var data = [].map.call(rows, function (el, i) { | |
var cells = el.querySelectorAll("td:not([class='noprint']), th:not([class='noprint'])"); | |
return [].map.call(cells, function (cell, i) { | |
var p = parse(cell.innerHTML); | |
if (p === "") { | |
return last_seen[i]; | |
} else { | |
last_seen[i] = p; | |
return p; | |
} | |
}); | |
}); | |
var tsv = to_tsv([header].concat(data)); | |
var uri = "data:text/tsv;charset=utf-8," + tsv; | |
var link = document.createElement("a"); | |
var slug = title.toLowerCase().replace(/[^a-z]+/g, "-"); | |
link.setAttribute("href", encodeURI(uri)); | |
link.setAttribute("target", "_blank"); | |
link.setAttribute("download", "optn-data-" + slug + ".tsv"); | |
link.click(); | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment