Skip to content

Instantly share code, notes, and snippets.

@lindenb
Created August 28, 2023 16:20
Show Gist options
  • Save lindenb/3ac9e7539f7d3900577e92fb7e6282ec to your computer and use it in GitHub Desktop.
Save lindenb/3ac9e7539f7d3900577e92fb7e6282ec to your computer and use it in GitHub Desktop.
A first test with WebComponent, extending HTMLElement javascript bioinformatics
<html>
<head>
</head>
<body>
<bio-anchor build="hg38">rs25</bio-anchor> and
<bio-anchor build="hg38">ENSG00000005108</bio-anchor>
<bio-anchor build="hg38">chr1:1234-1235</bio-anchor>
<bio-anchor build="hg38">xxx</bio-anchor>
</body>
<tail>
<script>
class BioAnchor extends HTMLElement {
constructor() {
super();
const shadow = this.attachShadow({
mode: 'open'
});
}
connectedCallback() {
const build = this.hasAttribute("build")?this.getAttribute("build"):"";
const s = this.textContent;
var match;
const wrapper = document.createElement('span');
if(s.match(/^rs[0-9]+$/i)) {
wrapper.appendChild(this.createAnchor({"href":"https://www.ncbi.nlm.nih.gov/snp/"+s,"title":s+" in DBSNP","text":s}));
}
else if(s.match(/^ENSG[0-9]+$/)) {
wrapper.appendChild(this.createAnchor({"href":"http://www.ensembl.org/homo_sapiens/Gene/Summary?g="+s+"&db=core","title":s+" in Ensembl","text":s}));
}
else if(s.match(/^chr[0-9XY]+\:[0-9,]+\-[0-9,]+$/) && build!=="") {
wrapper.appendChild(this.createAnchor({
"href":"https://genome.ucsc.edu/cgi-bin/hgTracks?db="+build+"&position="+encodeURI(s),"title":s+" in UCSC"+build,"text":s}));
}
else
{
wrapper.appendChild(document.createTextNode(s));
}
this.shadowRoot.appendChild(wrapper);
}
createAnchor(hash) {
var a = document.createElement('a');
if('title' in hash) a.title=hash.title;
if('target' in hash) a.target=hash.target;
if('href' in hash) a.href=hash.href;
if('text' in hash) a.appendChild(document.createTextNode(hash.text));
return a;
}
attributeChangedCallback(name, oldValue, newValue) {
if(name=="build") this.build=newValue;
}
}
customElements.define('bio-anchor', BioAnchor);
</script>
</tail>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment