From 2ec113fc326399415d3af45c3477804617059aa9 Mon Sep 17 00:00:00 2001
From: Michael Hartung <michi@Michaels-MacBook-Pro.local>
Date: Fri, 23 Jul 2021 16:08:10 +0200
Subject: [PATCH] handling of multiple ensg numbers for same gene

---
 .../analysis-panel.component.ts               |  1 +
 .../explorer-page/explorer-page.component.ts  | 36 +++++++++++--------
 src/app/utils.ts                              | 15 ++++++++
 src/index.html                                |  4 +--
 4 files changed, 40 insertions(+), 16 deletions(-)

diff --git a/src/app/components/analysis-panel/analysis-panel.component.ts b/src/app/components/analysis-panel/analysis-panel.component.ts
index 6eeec400..1a7b525b 100644
--- a/src/app/components/analysis-panel/analysis-panel.component.ts
+++ b/src/app/components/analysis-panel/analysis-panel.component.ts
@@ -31,6 +31,7 @@ import {NetworkSettings} from '../../network-settings';
 import {NetexControllerService} from 'src/app/services/netex-controller/netex-controller.service';
 import {defaultConfig, IConfig} from 'src/app/config';
 import { mapCustomEdge, mapCustomNode } from 'src/app/main-network';
+import { removeDuplicateObjectsFromList } from 'src/app/utils';
 
 
 declare var vis: any;
diff --git a/src/app/pages/explorer-page/explorer-page.component.ts b/src/app/pages/explorer-page/explorer-page.component.ts
index 4528fa49..e190b0d2 100644
--- a/src/app/pages/explorer-page/explorer-page.component.ts
+++ b/src/app/pages/explorer-page/explorer-page.component.ts
@@ -23,7 +23,7 @@ import domtoimage from 'dom-to-image';
 import {NetworkSettings} from '../../network-settings';
 import {defaultConfig, EdgeGroup, IConfig, InteractionDatabase, NodeGroup} from '../../config';
 import {NetexControllerService} from 'src/app/services/netex-controller/netex-controller.service';
-import {rgbaToHex, rgbToHex, standardize_color} from '../../utils'
+import {removeDuplicateObjectsFromList, rgbaToHex, rgbToHex, standardize_color} from '../../utils'
 import * as merge from 'lodash/fp/merge';
 
 // import * as 'vis' from 'vis-network';
@@ -272,27 +272,37 @@ export class ExplorerPageComponent implements OnInit, AfterViewInit {
     if (network.nodes.length) {
       network.nodes = await this.netex.mapNodes(network.nodes, this.myConfig.identifier);
     }
+
+    if (this.myConfig.identifier === 'ensg') {
+      // remove possible duplicate IDs
+      network.nodes = removeDuplicateObjectsFromList(network.nodes, 'netexId');
+    }
+
     // at this point, we have nodes synched with the backend
     // use netexIds where posssible, but use original id as node name if no label given
     const nodeIdMap = {};
-    const seenNodeIds = new Set();
-    network.nodes.forEach((node, index, object) => {
-      if (seenNodeIds.has(node.id)) {
-        // remove duplicate ensg nodes, TODO is there a better way to do this?
-        object.splice(index, 1);
-        return;
-      } else {
-        seenNodeIds.add(node.id);
-      }
-      nodeIdMap[node.id] = node.netexId ? node.netexId : node.id;
+    
+    network.nodes.forEach((node) => {
+      // set node label to original id before node id will be set to netex id
       node.label = node.label ? node.label : node.id;
+
+      nodeIdMap[node.id] = node.netexId ? node.netexId : node.id;
       node.id = nodeIdMap[node.id];
     });
-    // adjust edge labels accordingly
+
+    // adjust edge labels accordingly and filter 
+    const edges = new Array();
     network.edges.forEach(edge => {
       edge.from = nodeIdMap[edge.from];
       edge.to = nodeIdMap[edge.to];
+      // check if edges have endpoints
+      if (edge.from !== undefined && edge.to !== undefined) {
+        edges.push(edge);
+      }
     });
+    // remove edges without endpoints
+    network.edges = edges;
+
     this.proteins = network.nodes;
     this.edges = network.edges;
   }
@@ -534,8 +544,6 @@ export class ExplorerPageComponent implements OnInit, AfterViewInit {
     nodeGroups = merge(defaultNodeGroups, nodeGroups);
     // overwrite default node groups
     this.myConfig[key] = nodeGroups;
-    console.log('nodeGroups after preprocessing')
-    console.log(nodeGroups)
   }
 
   /**
diff --git a/src/app/utils.ts b/src/app/utils.ts
index c2406510..72b2bff4 100644
--- a/src/app/utils.ts
+++ b/src/app/utils.ts
@@ -1,5 +1,7 @@
 // From https://stackoverflow.com/a/27709336/3850564
 
+import { Node } from "./interfaces";
+
 export function getGradientColor(startColor: string, endColor: string, percent: number) {
   // strip the leading # if it's there
   startColor = startColor.replace(/^\s*#|\s*$/g, '');
@@ -104,3 +106,16 @@ export function standardize_color(str){
 	return ctx.fillStyle.toString();
 }
 
+export function removeDuplicateObjectsFromList(nodes: Node[], attribute: string): Node[] {
+  const seenIds = new Set();
+  const filteredArray = new Array();
+  for (const node of nodes) {
+    if (seenIds.has(node[attribute])) {
+      continue;
+    }
+    filteredArray.push(node);
+    seenIds.add(node[attribute]);
+  }
+  return filteredArray;
+}
+
diff --git a/src/index.html b/src/index.html
index c1d46c92..5c5e6a18 100644
--- a/src/index.html
+++ b/src/index.html
@@ -37,10 +37,10 @@
                     config='{
                       "nodeGroups": {"selectedNode": {"font": {"size": "18"} }, "0.5": {"font": "18px verdana blue", "type": "0.5er Instanz", "color": "green", "groupName": "0.5", "shape": "star"}, "patientgroup": {"type": "Patient", "detailShowLabel": "true", "color": "#632345", "groupName": "patient group", "shape": "dot", "size": "50"}, "pugGroup": {"type": "woof woof", "color": "grey", "groupName": "Pug Group", "shape": "triangle", "image": "https://static.raymondcamden.com/images/2016/11/pug.png"}},
                       "edgeGroups": {"xxx": {"color": "black", "groupName": "xxx Group", "dashes": [1, 2]}, "notdashes": {"color": "black", "groupName": "not dashes Group"}},
-                      "identifier": "symbol"
+                      "identifier": "ensg"
                     }'
                     network='{
-                      "nodes": [{"id": "MYC", "label": "node w/o group"}, {"id": "TP53", "group": "0.5"}, {"id": "C5", "group": "pugGroup"}, {"id": "Patient No. 5", "group": "patientgroup"}, {"label": "PTEN", "id": "PTEN", "group": 0.5, "value":"5"}],
+                      "nodes": [{"id": "ENSG00000171862", "label": "node w/o group"}, {"id": "ENSG00000284792", "group": "0.5"}, {"id": "ENSG00000106804", "group": "pugGroup"}, {"id": "Patient No. 5", "group": "patientgroup"}, {"label": "PTEN", "id": "PTEN", "group": 0.5, "value":"5"}],
                       "edges": [
                       {"from": "TP53","to": "C5","group": "xxx", "label": "this is a label", "title": "this is a title"},
                       {"from": "Patient No. 5","to": "C5","label": "w/o group"}
-- 
GitLab