Skip to content

Commit

Permalink
Implemented span-based searching via a new URL route, route guard, an…
Browse files Browse the repository at this point in the history
…d service call.
  • Loading branch information
alancleary committed Feb 11, 2019
1 parent eaf7a6c commit c8c900b
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 3 deletions.
7 changes: 6 additions & 1 deletion client/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NgModule } from "@angular/core";
import { RouterModule, Routes } from "@angular/router";
// app
import { InstructionsComponent, MultiComponent, SearchComponent } from "./components";
import { DefaultSearchGuard, MultiGuard, SearchGuard } from "./guards";
import { DefaultSearchGuard, MultiGuard, SearchGuard, SpanSearchGuard } from "./guards";

const routes: Routes = [
{
Expand Down Expand Up @@ -38,6 +38,11 @@ const routes: Routes = [
component: SearchComponent,
path: "search/:source/:gene",
},
{
canActivate: [SpanSearchGuard],
component: SearchComponent,
path: "search/:source/:chromosome/:span",
},
];

@NgModule({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class SearchParamsComponent implements OnDestroy, OnInit {
ngOnInit(): void {
// initialize block group and subscribe to store updates
const defaultBlock = new BlockParams();
this.blockGroup = this.fb.group(defaultBlock.formControls());
this.blockGroup = this.fb.group(defaultBlock.formControls());
this.macroTracksService.blockParams
.pipe(takeUntil(this.destroy))
.subscribe((params) => this._updateGroup(this.blockGroup, params));
Expand Down
3 changes: 2 additions & 1 deletion client/src/app/guards/default-search.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export class DefaultSearchGuard implements CanActivate {
const url = "/search" +
"/" + AppConfig.getDefaultServer().id +
"/" + route.params.gene;
this.store.dispatch(new routerActions.Go({path: [url, {routeParam: 1}]}));
// TODO: update url so back button skips search
this.store.dispatch(new routerActions.Go({path: [url]}));
return false;
}
}
3 changes: 3 additions & 0 deletions client/src/app/guards/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { DefaultSearchGuard } from "./default-search.guard";
import { MultiGuard } from "./multi.guard";
import { SearchGuard } from "./search.guard";
import { SpanSearchGuard } from "./span-search.guard";

export const guards: any[] = [
DefaultSearchGuard,
MultiGuard,
SearchGuard,
SpanSearchGuard,
];

export * from "./default-search.guard";
export * from "./multi.guard";
export * from "./search.guard";
export * from "./span-search.guard";
45 changes: 45 additions & 0 deletions client/src/app/guards/span-search.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Angular
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
import { Observable, throwError } from "rxjs";
import { catchError, map, tap } from "rxjs/operators";
// store
import { Store } from "@ngrx/store";
import * as routerActions from "../store/actions/router.actions";
import * as fromRoot from "../store/reducers";
// app
import { MicroTracksService } from "../services";

@Injectable()
export class SpanSearchGuard implements CanActivate {

constructor(
private microTracksService: MicroTracksService,
private router: Router,
private store: Store<fromRoot.State>) { }

// this guard is only triggered when no server is provided, which Search needs,
// so instead of activating it always redirects to the default server
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
const span = route.params.span.split("-");
return this.microTracksService.getSearchFromSpan(
route.params.chromosome,
span[0],
(span.length === 1 ? span[0] : span[1]),
route.params.source)
.pipe(
tap((context) => {
const url = "/search" + "/" + route.params.source + "/" + context.gene;
this.store.dispatch(new routerActions.Go({
path: [url], // TODO: update url so back button skips search
query: {"neighbors": context.neighbors}}));
}),
catchError((error) => {
// TODO: update url so back button skips search
this.store.dispatch(new routerActions.Go({path: ["/instructions"]}));
return throwError(error);
}),
map((context) => false)
)
}
}
1 change: 1 addition & 0 deletions client/src/app/models/server.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export class Server {
plotGlobal: Request;
nearestGene: Request;
chromosome: Request;
spanToSearch: Request;
}
17 changes: 17 additions & 0 deletions client/src/app/services/micro-tracks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,23 @@ export class MicroTracksService extends HttpService {
})));
}

// takes a span for a specific chromosome and retrieves the relevant search
// (query gene and neighbors)
getSearchFromSpan(
chromosome: string,
begin: number,
end: number,
serverID: string):
Observable<{gene: string, neighbors: number}> {
const body = {
chromosome,
begin: String(begin),
end: String(end),
};
return this._makeRequest<{gene: string, neighbors: number}>(serverID, "spanToSearch", body)
.pipe(catchError((error) => throwError(error)));
}

updateParams(params: QueryParams): void {
const path = [];
const query = Object.assign({}, params, {sources: params.sources.join(",")});
Expand Down
4 changes: 4 additions & 0 deletions client/src/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
"chromosome": {
"type": "POST",
"url": "http://localhost:8000/services/v1_1/chromosome/"
},
"spanToSearch": {
"type": "POST",
"url": "http://localhost:8000/services/v1_1/span-to-context/"
}
}
]
Expand Down

0 comments on commit c8c900b

Please sign in to comment.