sbgnml-to-cytoscape
Version:
A tool to convert sbgn-ml files to json for Cytoscape.js
42 lines (33 loc) • 1.07 kB
JavaScript
const convert = require('xml-js');
const objPath = require('object-path');
const nodesConverter = require('./nodesConverter');
const edgesConverter = require('./edgesConverter');
module.exports = (sbgnmlText) => {
if (sbgnmlText === null) {
throw new Error(`'${sbgnmlText} is invalid input.`);
}
const converted = convert.xml2js(sbgnmlText, {compact: true, spaces: 2, trim: true, nativeType: true });
const result = objPath.get(converted, 'sbgn.map', undefined);
if (result === undefined) {
return {nodes: [], edges: []};
}
const glyphs = [];
const arcs = [];
if (result.glyph) {
if (Array.isArray(result.glyph)) {
glyphs.push(...result.glyph);
} else {
glyphs.push(result.glyph);
}
}
if (result.arc) {
if (Array.isArray(result.arc)) {
arcs.push(...result.arc);
} else {
arcs.push(result.arc);
}
}
const {nodes: nodes, nodeIdSet: nodeIdSet, portIdMap} = nodesConverter(glyphs);
const edges = edgesConverter(arcs, nodeIdSet, portIdMap);
return {nodes: nodes, edges: edges};
};