-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.tsx
120 lines (103 loc) · 4.36 KB
/
index.tsx
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
import React from 'react'
import { MetaNode } from '@/spec/metanode'
import { z } from 'zod'
import { additional_info_icon, gene_icon } from '@/icons'
// How the schema validation works: https://codex.so/zod-validation-en
export const MyGeneIDOutputC = z.object({
SYMBOL_OR_ALIAS: z.string().optional(),
MATCH_SOURCE: z.string().optional(),
ALIAS: z.string().optional(),
ENSEMBL: z.string(),
ENTREZID: z.string(),
GENENAME: z.string(),
REFSEQ: z.string().optional(),
SYMBOL: z.string(),
UNIPROT: z.string().optional(),
KEGG: z.string().optional(),
MARRVEL: z.string().optional()
})
export type MyGeneIDOutput = z.infer<typeof MyGeneIDOutputC>
// important ref: https://rsinohara.github.io/json-to-zod-react/
export const MyGeneIDOutputArrayC = z.array(
MyGeneIDOutputC
)
export type MyGeneIDOutputArray = z.infer<typeof MyGeneIDOutputArrayC>
export function uniqJsonSubset(data:MyGeneIDOutputArray) {
//let datastr = JSON.stringify(data); datastr = datastr.replace("^[",""); datastr = datastr.replace("]$",""); const dataobj = JSON.parse(datastr);
//const [dataobj] = data; // did not work
//const dataobj = data; //const dataobj = [data][0];
// will not work if more than one ENTREZID for the same SYMBOL.
let uniqENTREZID : string[] = Array.from(new Set( data.map(a => a.ENTREZID)));
let uniqSYMBOL : string[] = Array.from(new Set( data.map(a => a.SYMBOL)));
let uniqGENENAME : string[] = Array.from(new Set( data.map(a => a.GENENAME)));
let uniqdataobj = uniqENTREZID.map((id, idx) => {
return {
ENTREZID : uniqENTREZID[idx],
SYMBOL : uniqSYMBOL[idx],
GENENAME : uniqGENENAME[idx],
};
});
return uniqdataobj;
}
export async function GetGeneSetIDConv(geneset:string[], species_id:string = "hsa", geneid_type:string = "SYMBOL_OR_ALIAS") {
//const species_id = "hsa";
//const geneid_type = "SYMBOL_OR_ALIAS";
const gene_id = geneset.join(",");
//console.log(gene_id)
const json_or_txt = "json"
//const req = await fetch(`https://bdcw.org/geneid/rest/species/${species_id}/GeneIDType/${geneid_type}/GeneListStr/${gene_id}/View/${json_or_txt}`)
const req = await fetch(`https://sc-cfdewebdev.sdsc.edu/geneid/rest/species/${species_id}/GeneIDType/${geneid_type}/GeneListStr/${gene_id}/View/${json_or_txt}`)
const res = await req.json();
// Above, try catch is not explicitly needed since metanodes handle that.
let uniqdataobj = uniqJsonSubset(res);
return uniqdataobj;
}
// A unique name for your data type is used here
export const ConvertedGeneID = MetaNode('ConvertedGeneID')
// Human readble descriptors about this node should go here
.meta({
label: 'Gene IDs table',
description: 'Gene IDs table',
icon: [gene_icon, additional_info_icon],
})
// this should have a codec which can encode or decode the data type represented by this node
// using zod, a compile-time and runtime type-safe codec can be constructed
.codec(MyGeneIDOutputArrayC)
// react component rendering your data goes here
.view(data => {
//return (...data) => {
//window.alert("Type of data is:" + typeof(data));
//const dataobj = [data][0];
const dataobj = data; //const dataobj = [data][0];
//window.alert(dataobj[0]);
//for (let i=0; i<arr.length; i++) {document.writeln(arr[i]);}
// extract all SYMBOL and ENTREZID: https://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array
// var, let, const: https://www.tutorialsteacher.com/typescript/typescript-variable
// not used but keeping for knowing the syntax
//let ENTREZID : string[] = dataobj.map(a => a.ENTREZID);
//let SYMBOL : string[] = dataobj.map(a => a.SYMBOL);
//let GENENAME : string[] = dataobj.map(a => a.GENENAME);
// const picked = (({ a, c }) => ({ a, c }))(object);
//let dataobj1 = Array.from(new Set(dataobj.map(a => ({"ENTREZID": a.ENTREZID,
// "SYMBOL" : a.SYMBOL, "GENENAME": a.GENENAME}))) ];
let uniqdataobj = uniqJsonSubset(data);
return(
<div><table>
<tr>
<th>SYMBOL</th>
<th>NCBI ENTREZ URL</th>
<th>GENE NAME</th>
</tr>
{uniqdataobj.map((val, key) => {
return (
<tr key={key}>
<td>{val.SYMBOL}</td>
<td><a href = {`https://www.ncbi.nlm.nih.gov/gene/${val.ENTREZID}`}>{val.SYMBOL}</a></td>
<td>{val.GENENAME}</td>
</tr>
)
})}
</table></div>
)
})
.build()