From 5a9260d39b73b280f3f91fda69b198de02f113bd Mon Sep 17 00:00:00 2001
From: Michael Hartung <michi@Michaels-MacBook-Pro.local>
Date: Wed, 25 Aug 2021 19:06:11 +0200
Subject: [PATCH] load edges from db

---
 src/app/config.ts                               |  2 ++
 src/app/interfaces.ts                           |  5 +++++
 src/app/main-network.ts                         | 17 ++++++++++++++++-
 .../explorer-page/explorer-page.component.ts    | 13 +++++++++++--
 .../netex-controller.service.ts                 | 11 ++++++++++-
 src/index.html                                  |  7 +++----
 6 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/src/app/config.ts b/src/app/config.ts
index ead040c5..2abf461d 100644
--- a/src/app/config.ts
+++ b/src/app/config.ts
@@ -56,6 +56,7 @@ export interface IConfig {
   edgeGroups: { [key: string]: EdgeGroup };
   interactionDrugProtein: InteractionDrugProteinDB;
   interactionProteinProtein: InteractionProteinProteinDB;
+  autofillEdges: boolean;
   interactions?: InteractionDatabase;
   identifier?: Identifier;
   nodeShadow?: boolean;
@@ -93,6 +94,7 @@ export const defaultConfig: IConfig = {
   interactionProteinProtein: 'STRING',
   nodeShadow: true,
   edgeShadow: true,
+  autofillEdges: true,
   nodeGroups: {
     // all NodeGroups but the default group must be set, if not provided by the user, they will be taken from here
     // IMPORTANT: node color must be hexacode!
diff --git a/src/app/interfaces.ts b/src/app/interfaces.ts
index cab05e03..2c467aad 100644
--- a/src/app/interfaces.ts
+++ b/src/app/interfaces.ts
@@ -37,6 +37,11 @@ export type legendContext = 'explorer' | 'adjacentDrugs' | 'drug' | 'drugTarget'
 /// netexId to expressionlvl
 export type NodeAttributeMap = { string: number } | {};
 
+export interface NetexInteraction {
+  dataset: string;
+  proteinA: string;
+  proteinB: string;
+}
 
 export interface NodeInteraction {
   from: string;
diff --git a/src/app/main-network.ts b/src/app/main-network.ts
index 1abe89b4..e2a99291 100644
--- a/src/app/main-network.ts
+++ b/src/app/main-network.ts
@@ -1,5 +1,5 @@
 import { defaultConfig, IConfig } from './config';
-import {NodeInteraction, Node, getProteinNodeId} from './interfaces';
+import {NodeInteraction, Node, getProteinNodeId, NetexInteraction} from './interfaces';
 import * as merge from 'lodash/fp/merge'; 
 
 export function getDatasetFilename(dataset: Array<[string, string]>): string {
@@ -117,3 +117,18 @@ export function mapCustomEdge(customEdge: NodeInteraction, config: IConfig): any
   };
   return edge;
 }
+
+/** Maps netex retrieved edge to network edge object
+ * Uses the default group for edge objects.
+ * 
+ * @param customEdge 
+ * @param config 
+ * @returns 
+ */
+ export function mapNetexEdge(customEdge: NetexInteraction, config: IConfig): any {
+  const edge = JSON.parse(JSON.stringify(config.edgeGroups.default));
+  edge['from'] = customEdge['proteinA'];
+  edge['to'] = customEdge['proteinB'];
+  edge['dataset'] = customEdge['dataset'];
+  return edge;
+}
diff --git a/src/app/pages/explorer-page/explorer-page.component.ts b/src/app/pages/explorer-page/explorer-page.component.ts
index fa2a95db..7e7c2ef1 100644
--- a/src/app/pages/explorer-page/explorer-page.component.ts
+++ b/src/app/pages/explorer-page/explorer-page.component.ts
@@ -18,7 +18,7 @@ import {
   Tissue,
   Wrapper
 } from '../../interfaces';
-import {mapCustomEdge, mapCustomNode, ProteinNetwork} from '../../main-network';
+import {mapCustomEdge, mapCustomNode, mapNetexEdge, ProteinNetwork} from '../../main-network';
 import {AnalysisService} from '../../services/analysis/analysis.service';
 import {OmnipathControllerService} from '../../services/omnipath-controller/omnipath-controller.service';
 import domtoimage from 'dom-to-image';
@@ -369,10 +369,19 @@ export class ExplorerPageComponent implements OnInit, AfterViewInit {
     if (this.networkPositions) {
       this.proteinData.updateNodePositions(this.networkPositions)
     }
-    this.proteinData.linkNodes();
+    // TODO do we still need this?
+    // this.proteinData.linkNodes();
 
     const {nodes, edges} = this.proteinData.mapDataToNetworkInput(this.myConfig);
 
+    console.log('nodes', nodes)
+    console.log('edges', edges)
+    if (this.myConfig.autofillEdges && nodes.length) {
+      const netexEdges = await this.netex.fetchEdges(nodes, this.myConfig.interactionProteinProtein);
+      console.log(netexEdges.map(netexEdge => mapNetexEdge(netexEdge, this.myConfig)))
+      edges.push(...netexEdges.map(netexEdge => mapNetexEdge(netexEdge, this.myConfig)))
+    }
+
     this.nodeData.nodes = new vis.DataSet(nodes);
     this.nodeData.edges = new vis.DataSet(edges);
     const container = this.networkEl.nativeElement;
diff --git a/src/app/services/netex-controller/netex-controller.service.ts b/src/app/services/netex-controller/netex-controller.service.ts
index 4992123d..bfc7e285 100644
--- a/src/app/services/netex-controller/netex-controller.service.ts
+++ b/src/app/services/netex-controller/netex-controller.service.ts
@@ -4,7 +4,7 @@ import {HttpClient, HttpParams} from '@angular/common/http';
 import {AlgorithmType, QuickAlgorithmType} from '../analysis/analysis.service';
 import { Observable } from 'rxjs';
 import { Tissue, Node, EdgeType} from 'src/app/interfaces';
-import { InteractionDrugProteinDB } from 'src/app/config';
+import { InteractionDrugProteinDB, InteractionProteinProteinDB } from 'src/app/config';
 
 @Injectable({
   providedIn: 'root'
@@ -115,4 +115,13 @@ export class NetexControllerService {
      */
     return this.http.post(`${environment.backend}graph_export/`, graph_data, {responseType: 'text'});
   }
+
+  public async fetchEdges(nodes: Node[], dataset: InteractionProteinProteinDB): Promise<any> {
+    /**
+     * Tries to map every node to a node object in out database
+     * Returns list of mapped nodes if node was found, otherwise original node to not lose information
+     */
+    const payload = {nodes: nodes, dataset: dataset};
+    return this.http.post(`${environment.backend}fetch_edges/`, payload).toPromise();
+  }
 }
diff --git a/src/index.html b/src/index.html
index ea00bbd9..b7e2e5dd 100644
--- a/src/index.html
+++ b/src/index.html
@@ -44,14 +44,13 @@
                       "identifier": "symbol",
                       "nodeShadow": true,
                       "edgeShadow": true,
-                      "interactionDrugProtein": "ChEMBL",
-                      "legendUrl": "https://exbio.wzw.tum.de/covex/assets/leg1.png"
+                      "interactionProteinProtein": "APID",
+                      "interactionDrugProtein": "ChEMBL"
                     }'
                     network='{
                       "nodes": [{"id": "TP53", "group": "0.5"}, {"id": "MYC", "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"}
+                     
                       ]
                     }'
                     style="height: 100%; width: 100vw; display: block;"
-- 
GitLab