/
utilities.js
171 lines (160 loc) · 5.27 KB
/
utilities.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const initialDefaultState = () => {
return {
entries: [
{
resource: "Rat",
type: "MultiFlatmap",
mode: "main",
id: 1,
state: undefined,
label: "",
discoverId: undefined
}
],
};
}
const getNewMapEntry = async (type, sparcApi) => {
let entry = { };
if (type === "AC") {
entry = {
resource: "Rat",
type: "MultiFlatmap",
mode: "main",
state: undefined,
label: "",
discoverId: undefined
};
} else if (type === "FC") {
entry = {
resource: "FunctionalConnectivity",
type: "Flatmap",
mode: "main",
state: undefined,
label: "Functional",
discoverId: undefined
}
} else if (type === "3D") {
const data = await getBodyScaffoldInfo(sparcApi, "human");
entry = {
resource: data.url,
type: "Scaffold",
mode: "main",
state: undefined,
label: "Human",
discoverId: data.datasetInfo.discoverId,
contextCardUrl: data.datasetInfo.contextCardUrl,
s3uri: data.datasetInfo.s3uri,
version: data.datasetInfo.version,
isBodyScaffold: true,
};
}
return entry;
}
/*
* Initial state for the split flow
*/
const initialState = async (type, sparcApi) => {
const state = initialDefaultState();
if (type === "FC") {
state.entries[0].resource = "FunctionalConnectivity";
state.entries[0].type = "Flatmap";
state.entries[0].label = "Functional";
} else if (type === "WholeBody") {
const data = await getBodyScaffoldInfo(sparcApi, "human");
state.entries[0].resource = data.url;
state.entries[0].contextCardUrl = data.datasetInfo.contextCardUrl;
state.entries[0].discoverId = data.datasetInfo.discoverId,
state.entries[0].s3uri = data.datasetInfo.s3uri;
state.entries[0].version = data.datasetInfo.version;
state.entries[0].type = "Scaffold";
state.entries[0].label = "Human";
state.entries[0].isBodyScaffold = true;
}
return state;
}
const capitalise = term => {
if (term)
return term.charAt(0).toUpperCase() + term.slice(1);
return term;
};
/*
* Provide a list of available species for the flatmap
*/
const availableSpecies = () => {
return {
"Human Female": { taxo: "NCBITaxon:9606", biologicalSex: "PATO:0000383", iconClass: "mapicon-icon_human", displayLatestChanges: true, displayWarning: true },
"Human Male": { taxo: "NCBITaxon:9606", biologicalSex: "PATO:0000384", iconClass: "mapicon-icon_human", displayLatestChanges: true, displayWarning: true },
"Rat": { taxo: "NCBITaxon:10114", iconClass: "mapicon-icon_rat", displayLatestChanges: true, displayWarning: true },
"Mouse": { taxo: "NCBITaxon:10090", iconClass: "mapicon-icon_mouse", displayLatestChanges: true, displayWarning: true },
"Pig": { taxo: "NCBITaxon:9823", iconClass: "mapicon-icon_pig", displayLatestChanges: true, displayWarning: true },
"Cat": { taxo: "NCBITaxon:9685", iconClass: "mapicon-icon_cat", displayLatestChanges: true, displayWarning: true },
}
}
/*
* Look for the key in availableSpecies with the provided condition
*/
const findSpeciesKey = condition => {
if (condition) {
const list = availableSpecies();
for (let key in list) {
if (condition.taxo === list[key].taxo) {
if (condition.biologicalSex && list[key].biologicalSex) {
if (condition.biologicalSex === list[key].biologicalSex)
return key;
} else {
return key;
}
}
}
}
return "";
}
const extractS3BucketNameAndPrefix = uri => {
if (uri) {
const substring = uri.split("//")[1];
if (substring) {
const s3Bucket = substring.split("/")[0];
const n = substring.indexOf('/');
const s3Prefix = substring.substring(n + 1);
return {
s3Bucket,
s3Prefix
};
}
}
return undefined
}
const getBodyScaffoldInfo = async (sparcApi, species) => {
//Get body scaffold information
let url = "";
let datasetInfo = undefined;
const response = await fetch(`${sparcApi}get_body_scaffold_info/${species}`);
if (response.ok) {
const data = await response.json();
//Construct the url endpoint for downloading the scaffold
const bucketInfo = extractS3BucketNameAndPrefix(data.s3uri);
url = `${sparcApi}s3-resource/${bucketInfo.s3Prefix}files/${data.path}?s3BucketName=${bucketInfo.s3Bucket}`;
const contextCardUrl = `${sparcApi}s3-resource/${bucketInfo.s3Prefix}files/${data.contextinfo}?s3BucketName=${bucketInfo.s3Bucket}`;
datasetInfo = {
s3uri: data.s3uri,
contextCardUrl,
discoverId: data.id,
version: data.version,
};
} else {
//Use default url if data is not found for any reason
if (species === "rat") {
url = "https://mapcore-bucket1.s3.us-west-2.amazonaws.com/WholeBody/31-May-2021/ratBody/ratBody_syncmap_metadata.json";
} else if (species === "human") {
url = "https://mapcore-bucket1.s3.us-west-2.amazonaws.com/WholeBody/27-4-23-human/human_body_metadata.json";
}
}
return {url, datasetInfo};
}
exports.availableSpecies = availableSpecies;
exports.capitalise = capitalise;
exports.findSpeciesKey = findSpeciesKey;
exports.initialState = initialState;
exports.initialDefaultState = initialDefaultState;
exports.getBodyScaffoldInfo = getBodyScaffoldInfo;
exports.getNewMapEntry = getNewMapEntry;