diff --git a/src/app/api.service.ts b/src/app/api.service.ts new file mode 100644 index 0000000000000000000000000000000000000000..f91427a526183b4fb20e5ed92667aba6c628c0e9 --- /dev/null +++ b/src/app/api.service.ts @@ -0,0 +1,18 @@ +import { Injectable } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; +import { environment } from '../environments/environment'; + + +const networkUrl = `${environment.backend}` + 'network'; +@Injectable({ + providedIn: 'root' +}) +export class ApiService { + + constructor(private http: HttpClient) { } + + async getNetwork() { + return this.http.get(networkUrl).toPromise(); + } + +} diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 9847797a68414930de3ef0e4629c44b920cace6f..c57f5f284c0877f639bd0a887a5ec5a0e8eec221 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -9,6 +9,8 @@ import { AppComponent } from './app.component'; import {ExplorerPageComponent} from './pages/explorer-page/explorer-page.component'; import {AboutPageComponent} from './pages/about-page/about-page.component'; import {HomePageComponent} from './pages/home-page/home-page.component'; +import { HttpClientModule } from '@angular/common/http'; + @NgModule({ declarations: [ @@ -21,7 +23,8 @@ import {HomePageComponent} from './pages/home-page/home-page.component'; BrowserModule, AppRoutingModule, FormsModule, - CommonModule + CommonModule, + HttpClientModule ], providers: [], bootstrap: [AppComponent] diff --git a/src/app/pages/explorer-page/explorer-page.component.ts b/src/app/pages/explorer-page/explorer-page.component.ts index b7b63484ab7c7c13bc1f6c57702058d0228ce8aa..02422ebd0e42303698e98e46c26ca0b9656c0f83 100644 --- a/src/app/pages/explorer-page/explorer-page.component.ts +++ b/src/app/pages/explorer-page/explorer-page.component.ts @@ -1,6 +1,9 @@ import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core'; import {ActivatedRoute, Router} from '@angular/router'; +import { HttpClient } from '@angular/common/http'; +import { environment } from '../../../environments/environment'; import {Effect, ProteinNetwork} from '../protein-network'; +import { ApiService } from '../../api.service'; declare var vis: any; @@ -23,14 +26,20 @@ export class ExplorerPageComponent implements OnInit, AfterViewInit { public proteinData: ProteinNetwork; + public proteinGroups: any; + public effects: any; + public edges: any; + private network: any; private nodeData: { nodes: any, edges: any } = {nodes: null, edges: null}; + private networkData: any = []; + private seed = 1; // TODO: Remove this @ViewChild('network', {static: false}) networkEl: ElementRef; - constructor(private route: ActivatedRoute, private router: Router) { + constructor(private route: ActivatedRoute, private router: Router, private api: ApiService) { this.groupId = 'IFI16'; this.geneNames.push('IFI16'); this.proteinNames.push('Gamma-interface-inducible protein 16'); @@ -70,12 +79,21 @@ export class ExplorerPageComponent implements OnInit, AfterViewInit { ngOnInit() { } + + async ngAfterViewInit() { if (!this.network) { await this.createNetwork(); } } + private async getNetwork() { + const data: any = await this.api.getNetwork(); + this.proteinGroups = data.proteinGroups; + this.effects = data.effects; + this.edges = data.edges; + } + public zoomToNode(id: string) { const coords = this.network.getPositions(id)[id]; this.network.moveTo({ @@ -99,8 +117,8 @@ export class ExplorerPageComponent implements OnInit, AfterViewInit { } private async createNetwork() { - const {proteinGroups, effects, edges: proteinEdges} = await this.loadNetwork(); - this.proteinData = new ProteinNetwork(proteinGroups, effects, proteinEdges); + await this.getNetwork(); + this.proteinData = new ProteinNetwork(this.proteinGroups, this.effects, this.edges); this.proteinData.loadPositions(); this.proteinData.linkNodes(); @@ -114,8 +132,6 @@ export class ExplorerPageComponent implements OnInit, AfterViewInit { const {nodes, edges} = this.mapDataToNodes(this.proteinData); - console.log(nodes, edges); - this.nodeData.nodes = new vis.DataSet(nodes); this.nodeData.edges = new vis.DataSet(edges); @@ -203,58 +219,6 @@ export class ExplorerPageComponent implements OnInit, AfterViewInit { this.nodeData.nodes.add(Array.from(addNodes.values())); } - private async loadNetwork(): Promise<any> { - // TODO: Load from backend instead of generating random stuff here... - - const proteinGroupCount = 10000; - const effectCount = 50; - const edgeCount = 2000; - - const proteinGroups = []; - const effects = []; - const edges = []; - - for (let i = 0; i < effectCount; i++) { - effects.push({ - id: i, - name: `eff${i}`, - }); - } - - const proteinGroupIds = []; - for (let i = 0; i < edgeCount; i++) { - const proteinGroupId = Math.floor(this.random() * proteinGroupCount); - const effectId = Math.floor(this.random() * effects.length); - let found = false; - for (const edge of edges) { - if (edge.proteinGroupId === proteinGroupId && edge.effectId === effectId) { - found = true; - break; - } - } - if (!found) { - edges.push({ - proteinGroupId, effectId - }); - if (proteinGroupIds.indexOf(proteinGroupId) === -1) { - proteinGroupIds.push(proteinGroupId); - } - } - } - - for (const proteinGroupId of proteinGroupIds) { - proteinGroups.push({ - id: proteinGroupId, - name: `pg${proteinGroupId}`, - }); - } - - return { - proteinGroups, - effects, - edges - }; - } private mapProteinGroupToNode(proteinGroup: any): any { return {