| 5 | import cytoscape from "cytoscape" |
| 6 | |
| 7 | function layoutDecoderGraph(cy) { |
| 8 | const rootNodes = cy.nodes().filter(n => n.indegree() == 0) |
| 9 | let columnItems = new Map() |
| 10 | let cellWidth = 90 |
| 11 | let cellHeight = 50 |
| 12 | |
| 13 | cy.elements().depthFirstSearch({ |
| 14 | roots: rootNodes, |
| 15 | visit: (node, edge, previous, i, depth) => { |
| 16 | let column = columnItems.get(depth) |
| 17 | let sortData = [node.data("seqlogprob"), node.data("pool"), node.data("seq_id")] |
| 18 | |
| 19 | if (!columnItems.has(depth)) { |
| 20 | column = [[node.id(), sortData]] |
| 21 | columnItems.set(depth, column) |
| 22 | } else { |
| 23 | column.push([node.id(), sortData]) |
| 24 | columnItems.set(depth, column) |
| 25 | } |
| 26 | }, |
| 27 | directed: true |
| 28 | }); |
| 29 | |
| 30 | // set node width based on label |
| 31 | cy.nodes().forEach(n => { |
| 32 | // skip compound |
| 33 | if (n.hasClass("compound")) return |
| 34 | // set label to label (seqlogprob) |
| 35 | let seqlogprob = n.data("seqlogprob") |
| 36 | // if seqlogprob is a number |
| 37 | if (typeof seqlogprob === "number") { |
| 38 | seqlogprob = seqlogprob.toFixed(2) |
| 39 | } else if (typeof seqlogprob === "string") { |
| 40 | // parse inf, -inf |
| 41 | if (seqlogprob == "inf") { |
| 42 | seqlogprob = Infinity |
| 43 | } else if (seqlogprob == "-inf") { |
| 44 | seqlogprob = -Infinity |
| 45 | } else { |
| 46 | seqlogprob = -9999 |
| 47 | } |
| 48 | } else { |
| 49 | seqlogprob = -9999 |
| 50 | } |
| 51 | let label = n.data("label") + "\n(" + seqlogprob + ")" |
| 52 | n.data("label", label) |
| 53 | |
| 54 | let width = label.length * 4 |
| 55 | if (n.data("root")) { |
| 56 | width = 20 |
| 57 | } |
| 58 | n.style("width", Math.max(width, 30)) |
| 59 | }) |
| 60 | |
| 61 | Array.from(columnItems.keys()).forEach(k => { |
| 62 | let column = columnItems.get(k) |
| 63 | |
| 64 | function compare(a,b) { |