Created
August 28, 2023 16:20
-
-
Save lindenb/3ac9e7539f7d3900577e92fb7e6282ec to your computer and use it in GitHub Desktop.
A first test with WebComponent, extending HTMLElement javascript bioinformatics
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
<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