-
Notifications
You must be signed in to change notification settings - Fork 1
/
plcoHTMLElements.js
147 lines (128 loc) · 5.52 KB
/
plcoHTMLElements.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
(async function () {
custom = {
loadScript: url => new Promise(function (resolve, reject) {
let s = document.createElement('script')
s.src = url
s.onload = resolve
document.head.appendChild(s)
})
}
if (typeof plco === 'undefined') {
await custom.loadScript('https://episphere.github.io/plco/plco.js')
}
custom.timeout = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(this)
}, 1000)
})
}
custom.ids = {
}
custom.generateId = () => {
return [0, 1, 2, 3].map(_ => Math.round(Math.random() * 10)).join('')
}
custom.parse = (string) => {
return string.split(';')
.reduce((total, cur) => {
if (cur.includes(':'))
total[cur.split(':')[0].trim()] = cur.split(':')[1].trim()
return total
}, {})
}
custom.listParse = (string) => {
// '[{id: 3080, sex: male}; {id:3080, sex:female}]'
return string.trim().substring(1, string.trim().length - 1).split(';')
.filter(str => str !== "")
.reduce((total, cur) => {
return total.concat(
cur.trim().substring(1, cur.trim().length - 1).split(',')
.reduce((whole, part) => {
if (part.includes(':'))
whole[part.split(':')[0].trim()] = part.split(':')[1].trim()
return whole
}, {}))
}, [])
}
custom.plot = class Plot extends HTMLElement {
constructor () {
super()
}
async connectedCallback() {
if (this.hasAttribute('data-inputs'))
this.arrayOfObjects = custom.listParse(this.getAttribute('data-inputs'))
else
this.arrayOfObjects = [{ phenotype_id: 3080, sex: 'female', ancestry: 'east_asian' }, { phenotype_id: 3080, sex: 'female', ancestry: 'european' }]
if (this.hasAttribute('custom-layout'))
this.customLayout = custom.parse(this.getAttribute('custom-layout'))
if (this.hasAttribute('custom-config'))
this.customConfig = custom.parse(this.getAttribute('custom-config'))
try {
await custom.timeout()
let id = custom.generateId()
while (id in custom.ids) {
id = custom.generateId()
}
custom.ids[id] = id
const div = document.createElement('div')
div.id = id
this.appendChild(div)
return id
} catch (e) {
console.error(e)
}
}
}
custom.manhattan = class ManhattanPlot extends custom.plot {
async connectedCallback() {
if (this.hasAttribute('p-val-nlog-min'))
this.p_val = Number.parseInt(this.getAttribute('p-val-nlog-min'))
if (this.hasAttribute('chromosome'))
this.chromosome = Number.parseInt(this.getAttribute('chromosome'))
let id = await super.connectedCallback()
try {
if (this.arrayOfObjects.length === 1) {
const { phenotype_id = 3080, sex = 'female', ancestry = 'east_asian' } = this.arrayOfObjects[0]
await plco.plot.manhattan(id, phenotype_id, sex, ancestry, p_val, chromosome, false, this.customLayout, this.customConfig)
} else
await plco.plot.manhattan2(id, this.arrayOfObjects, this.p_val, this.chromosome, false, this.customLayout, this.customConfig)
} catch (e) {
console.error(e)
document.querySelector(`manhattan-plot div[id="${id}"]`).innerHTML = e
}
}
}
custom.qq = class QQPlot extends custom.plot {
async connectedCallback() {
let id = await super.connectedCallback()
try {
if (this.arrayOfObjects.length === 1) {
const { phenotype_id = 3080, sex = 'female', ancestry = 'east_asian' } = this.arrayOfObjects[0]
await plco.plot.qq(id, phenotype_id, sex, ancestry, false, this.customLayout, this.customConfig)
} else
await plco.plot.qq2(id, this.arrayOfObjects, false, this.customLayout, this.customConfig)
} catch (e) {
console.error(e)
document.querySelector(`qq-plot div[id="${id}"]`).innerHTML = e
}
}
}
custom.pca = class PCAPlot extends custom.plot {
async connectedCallback() {
let id = await super.connectedCallback()
try {
if (this.arrayOfObjects.length === 1) {
const { phenotype_id = 3080, sex = 'female', ancestry = 'east_asian' } = this.arrayOfObjects[0]
await plco.plot.pca(id, phenotype_id, sex, ancestry, false, this.customLayout, this.customConfig)
} else
await plco.plot.pca2(id, this.arrayOfObjects, false, this.customLayout, this.customConfig)
} catch (e) {
console.error(e)
document.querySelector(`pca-plot div[id="${id}"]`).innerHTML = e
}
}
}
customElements.define('manhattan-plot', custom.manhattan)
customElements.define('qq-plot', custom.qq)
customElements.define('pca-plot', custom.pca)
})()