-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadataMapperPC2.js
More file actions
188 lines (151 loc) · 5.33 KB
/
metadataMapperPC2.js
File metadata and controls
188 lines (151 loc) · 5.33 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
const convert = require('sbgnml-to-cytoscape');
const pcServices = require('./pcServices');
let jp = require('jsonpath');
const Promise = require('bluebird');
//Map metadata from BioPax to nodes
//Returns a cy cytoscape json
//Requires valid BioPax and sbgn files
module.exports = function (biopax, sbgn) {
//Convert sbgn to json
let cyGraph = convert(sbgn);
//Get mapped node list
let nodes = cyGraph.nodes;
return processBioPax(biopax, nodes).then(data => {cyGraph.nodes = data; return cyGraph});
}
//Get data from pc2 via traverse
//Returns either null or a data object
function getData(id, path) {
//Append pc2 address, if id is not already an uri
if (id.indexOf('http') <= -1) {
id = 'http://pathwaycommons.org/pc2/' + id;
}
return pcServices.traversePC2(id, path).then(data => data.traverseEntry[0].value);
}
//Validate value and push the result to an array
//Effects : modifies the given array
function pushData(value, key, result) {
if (value.length !== 0) {
result.push([key, value]);
}
return result;
}
//Build a sub tree array for a biopax element
function buildBioPaxTree(id) {
let result = [];
//Get type
//let type = biopaxElement['@type']
//if (type) result.push(['Type', type]);
//Create a array of all promises to resolve
let promiseArray = [
getData(id, 'Entity/dataSource'),
getData(id, 'SimplePhysicalEntity/entityReference/displayName'),
getData(id, 'Entity/comment'),
getData(id, 'Named/name'),
getData(id, 'Named/standardName'),
getData(id, 'Entity/cellularLocation/term'),
getData(id, 'SimplePhysicalEntity/entityReference/xref/db'),
getData(id, 'SimplePhysicalEntity/entityReference/xref/id'),
getData(id, 'Entity/xref/db'),
getData(id, 'Entity/xref/id')
];
//Wait for all promises to resolve
return Promise.all(promiseArray).then(data => {
//Push basic results to result
result = pushData(data[0], 'Data Source', result);
result = pushData(data[1], 'Display Name', result);
result = pushData(data[2], 'Comment', result);
result = pushData(data[3], 'Names', result);
result = pushData(data[4], 'Standard Name', result);
result = pushData(data[5], 'Cellular Location', result);
//Merge Database ID's
let erefDatabases = data[6];
let erefDatabaseIds = data[7];
let xrefDatabases = data[8];
let xrefDatabaseIds = data[9];
if (erefDatabases.length !== 0 || xrefDatabases !== 0) {
result.push(['Databases', erefDatabases.concat(xrefDatabases)]);
result.push(['Database IDs', erefDatabaseIds.concat(xrefDatabaseIds)]);
}
//Return subtree
return result;
})
}
//Remove all characters after nth instance of underscore
//Requires the string to contain at least 1 underscore
function removeAfterUnderscore(word, numberOfElements) {
let splitWord = word.split('_');
let newWord = '';
for (let i = 0; i < numberOfElements; i++) {
if (i != (numberOfElements - 1)) {
newWord += splitWord[i] + '_';
} else {
newWord += splitWord[i];
}
}
return newWord;
}
//Check if ID even exists in the biopax file
//Returns teh matching element or null
function getElementFromBioPax(id, biopaxFile) {
//Append pc2 address, if id is not already an uri
if (id.indexOf('http') <= -1) {
id = 'http://pathwaycommons.org/pc2/' + id;
}
let str = "$..[?(@.pathid==\"" + id + "\")]";
//Get element matching the id
let result = jp.query(biopaxFile, str);
if (result[0]) return result;
else return null;
}
//Get subtree for each node
//Requires tree to be a valid biopax tree
function getBioPaxSubtree(nodeId, biopax) {
//Remove extra identifiers appended by cytoscape.js
let fixedNodeId = removeAfterUnderscore(nodeId, 2);
//Resolve issues if there is no appended identifiers
if (nodeId.indexOf('_') <= -1) {
fixedNodeId = nodeId;
}
//Conduct a basic search
let basicSearch = getElementFromBioPax(fixedNodeId, biopax);
if (basicSearch) return buildBioPaxTree(fixedNodeId);
//Check if id is an unification reference
fixedNodeId = 'UnificationXref_' + nodeId;
//Conduct a unification ref search
let uniSearch = getElementFromBioPax(fixedNodeId, biopax);
if (uniSearch) return buildBioPaxTree(fixedNodeId);
//Check if id is an external identifier
fixedNodeId = removeAfterUnderscore(nodeId, 2);
fixedNodeId = 'http://identifiers.org/' + fixedNodeId.replace(/_/g, '/');
//Conduct a external identifier search
let extSearch = getElementFromBioPax(fixedNodeId, biopax);
if (extSearch) return buildBioPaxTree(fixedNodeId);
return Promise.resolve(1);
}
//Replace all instances of a substring with a given string
//Returns a string
function replaceAll(str, find, replace) {
return str.replace(new RegExp(find, 'g'), replace);
}
//Process biopax file
function processBioPax(data, nodes) {
data = replaceAll(data, "@id", 'pathid');
//fs.writeFileSync('test', data);
data = JSON.parse(data);
//Get Graph Elements
let graph = data['@graph'];
let result = [];
let promises = [];
return Promise.map(nodes, function (el) {
//Get element values
let id = el.data.id;
promises.push(getBioPaxSubtree(id, graph).then(value => {
el.data.parsedMetadata = value;
result.push(el);
}));
}, { concurrency: 8 }).then(function (data) {
return Promise.map(promises, function(el) {}, { concurrency: 8 }).then(
value => result
)
});
}