-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy paththerapy.js
More file actions
200 lines (178 loc) · 6.58 KB
/
Copy paththerapy.js
File metadata and controls
200 lines (178 loc) · 6.58 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const { logger } = require('../logging');
const { ncit: NCIT_SOURCE_DEFN } = require('../sources');
const { orderPreferredOntologyTerms, rid } = require('../graphkb');
/**
* Given a CIViC EvidenceItem record,
* compile a list of its therapies into a list of combination of therapies, and
* returns modified 'therapies' & 'therapyInteractionType' properties
*
* record.therapies will be transformed into:
* - a list of 1 list of 1-or-many therapies ('COMBINATION' || 'SEQUENTIAL'), or
* - a list of 1-or-many lists of 1 therapy ('SUBSTITUTES'), or
* - a list of 1 null
*
* @param {object} evidenceItem the original CIViC EvidenceItem
* @returns {object} the modified EvidenceItem
*/
const resolveTherapies = (evidenceItem) => {
const record = JSON.parse(JSON.stringify(evidenceItem)); // Deep copy
// No therapy
if (record.therapies === null || record.therapies.length === 0) {
record.therapies = [null];
return record;
}
// One or more therapies
if (record.therapies.length === 1 || record.therapyInteractionType === 'SUBSTITUTES') {
record.therapies = record.therapies.map(therapy => [therapy]);
record.therapyInteractionType = null;
} else if (
record.therapyInteractionType === 'COMBINATION'
|| record.therapyInteractionType === 'SEQUENTIAL'
) {
record.therapies = [record.therapies];
} else {
logger.error(`(evidence: ${record.id}) unsupported therapy interaction type (${record.therapyInteractionType}) for a multiple therapy (${record.therapies.length}) statement`);
throw new Error('Did not find unique record');
}
// Since duplicates can occure (from civic !?), lets remove them
// Need to strignify/parse since we're comparing arrays of objects
const unique = new Set();
record.therapies.forEach(therapy => unique.add(JSON.stringify(therapy)));
record.therapies = [];
unique.forEach(therapy => record.therapies.push(JSON.parse(therapy)));
return record;
};
/**
* Given a Therapy record from CIViC,
* returns a Therapy record from GraphKB
*
* @param {ApiConnection} conn the API connection object for GraphKB
* @param {object} therapyRecord a therapy from CIViC
* @returns {object} Therapy record from GraphKB
*/
const getTherapy = async (conn, therapyRecord) => {
const name = therapyRecord.name.toLowerCase().trim();
const ncitId = therapyRecord.ncitId && typeof therapyRecord.ncitId === 'string'
? therapyRecord.ncitId.toLowerCase().trim()
: therapyRecord.ncitId;
if (ncitId) {
// Trying with the ncitId and the name
try {
return await conn.getUniqueRecordBy({
filters: [
{ source: { filters: { name: NCIT_SOURCE_DEFN.name }, target: 'Source' } },
{ sourceId: ncitId },
{ name },
],
sort: orderPreferredOntologyTerms,
target: 'Therapy',
});
} catch (err) {
logger.warn(`Failed to fetch therapy with NCIt id (${ncitId}) & name (${therapyRecord.name}) from graphkb`);
}
// Trying with the ncitId only
// Choosing the most recently created one
try {
const matchingTherapies = await conn.getRecords({
filters: {
AND: [
{ source: { filters: { name: NCIT_SOURCE_DEFN.name }, target: 'Source' } },
{ sourceId: ncitId },
],
},
target: 'Therapy',
});
// In-place sorting
matchingTherapies.sort((a, b) => b.createdAt - a.createdAt);
// returning 1st one (latest created)
return matchingTherapies[0];
} catch (err) {
logger.error(`Failed to fetch therapy with NCIt id (${ncitId}) from graphkb`);
throw err;
}
}
let originalError;
// Trying instead with the name
// Using the getTherapy method from the connection object
try {
// With the name as-is first
return await conn.getTherapy(name);
} catch (err) {
originalError = err;
}
try {
// Then with the name parsed
const match = /^\s*(\S+)\s*\([^)]+\)$/.exec(name);
if (match) {
return await conn.getTherapy(match[1]);
}
} catch (err) { }
// Logging errors
throw originalError;
};
/**
* Given a list of CIViC Therapy Records,
*
* (If one therapy)
* returns the corresponding Therapy record from GraphKB
*
* (If a combination of therapies)
* will add a therapy combination if there is not an existing record,
* will link the therapy combination to its individual elements with 'ElementOf' edges, then
* returns the corresponding Therapy record from GraphKB
*
* @param {ApiConnection} conn the API connection object for GraphKB
* @param {string} sourceRid
* @param {object[]} therapiesRecords
* @param {string} combinationType
* @returns {object} the corresponding Therapy record from GraphKB
*/
const addOrFetchTherapy = async (conn, sourceRid, therapiesRecords, combinationType) => {
/* ONE OR NO THERAPY */
if (therapiesRecords.length === 0) {
return null;
}
if (therapiesRecords.length === 1) {
if (therapiesRecords[0] === null) {
return null;
}
// Get the corresponding Therapy record from GraphKB
return getTherapy(conn, therapiesRecords[0]);
}
/* COMBINATION OF THERAPIES */
// For each therapy, get the corresponding Therapy record from GraphKB
const therapies = await Promise.all(
therapiesRecords.map(
async therapy => getTherapy(conn, therapy),
),
);
// concatenating sourceIds and names
const sourceId = therapies.map(e => e.sourceId).sort().join(' + ');
const name = therapies.map(e => e.name).sort().join(' + ');
// Add a Therapy Vertice for the combined therapies
const combinedTherapy = await conn.addRecord({
content: {
combinationType, name, source: sourceRid, sourceId,
},
existsOk: true,
target: 'Therapy',
});
// Add ElementOf Edges between corresponding records
for (const therapy of therapies) {
await conn.addRecord({
content: {
in: rid(combinedTherapy),
out: rid(therapy),
source: sourceRid,
},
existsOk: true,
target: 'ElementOf',
});
}
return combinedTherapy;
};
module.exports = {
addOrFetchTherapy,
getTherapy,
resolveTherapies,
};