import { Api, ApiClient, AppMetadata, Cram, DecisionTree, Item, Json, PagedItems, Params, Phenotype, ReportData, Sample, } from "@molgenis/vip-report-api"; import { VcfMetadata, VcfRecord } from "@molgenis/vip-report-vcf"; import { fetchCrai as fetchCraiGRCh38, fetchCram as fetchCramGRCh38, fetchDatabaseFamily, fetchDatabaseNoVep, fetchDatabaseSamples0, fetchDatabaseSamples1, fetchDatabaseSamples100, fetchDatabaseStr, fetchFastaGz as fetchFastaGzGRCh38, fetchGenesGz as fetchGenesGzGRCh38, fetchStrCrai as fetchStrCraiGRCh38, fetchStrCram as fetchStrCramGRCh38, fetchSqlWasm, } from "./GRCh38/static"; import AsyncLock from "async-lock"; import initSqlJs from "sql.js"; import { ReportDatabase } from "@molgenis/vip-report-api"; const lock = new AsyncLock(); /** * API client that uses mocked data as data source. */ export class MockApiClient implements Api { private static dataSetIds = [ "GRCh38 Family", "GRCh38 Family no VEP", "GRCh38 Samples 0", "GRCh38 Samples 1", "GRCh38 Samples 100", "GRCh38 Samples 1 STR", ]; private dataSetId: string; private apiClient: Api | undefined; constructor() { this.dataSetId = MockApiClient.dataSetIds[0]!; } async getInfoOrder() { const apiClient = await this.getApiClient(); return apiClient.getInfoOrder(); } async getConfig(): Promise { const apiClient = await this.getApiClient(); return apiClient.getConfig(); } async getFastaGz(contig: string, pos: number): Promise { const apiClient = await this.getApiClient(); return apiClient.getFastaGz(contig, pos); } async getGenesGz(): Promise { const apiClient = await this.getApiClient(); return apiClient.getGenesGz(); } async getCram(sampleId: string): Promise { const apiClient = await this.getApiClient(); return apiClient.getCram(sampleId); } async getDecisionTree(): Promise { const apiClient = await this.getApiClient(); return apiClient.getDecisionTree(); } async getSampleTree(): Promise { const apiClient = await this.getApiClient(); return apiClient.getSampleTree(); } async getAppMetadata(): Promise { const apiClient = await this.getApiClient(); return apiClient.getAppMetadata(); } async getPhenotypes(params: Params): Promise> { const apiClient = await this.getApiClient(); return apiClient.getPhenotypes(params); } async getRecordById(id: number, sampleIds: number[] | undefined): Promise> { const apiClient = await this.getApiClient(); return apiClient.getRecordById(id, sampleIds); } async getRecords(params: Params): Promise> { const apiClient = await this.getApiClient(); return apiClient.getRecords(params); } async getRecordsMeta(): Promise { const apiClient = await this.getApiClient(); return apiClient.getRecordsMeta(); } async getSampleById(id: number): Promise> { const apiClient = await this.getApiClient(); return apiClient.getSampleById(id); } async getSamples(params: Params): Promise> { const apiClient = await this.getApiClient(); return apiClient.getSamples(params); } getDatasetIds(): string[] { return MockApiClient.dataSetIds; } selectDataset(id: string): void { this.dataSetId = id; this.apiClient = undefined; } private async getApiClient(): Promise { if (this.apiClient === undefined) { await lock.acquire("apiClient", async () => { if (this.apiClient === undefined) { this.apiClient = await createApiClient(this.dataSetId); } }); } if (this.apiClient === undefined) throw new Error(); return this.apiClient; } } async function createApiClient(id: string): Promise { let reportData: ReportData; switch (id) { case "GRCh38 Family": reportData = await fetchReportDataGRCh38Family(); break; case "GRCh38 Family no VEP": reportData = await fetchReportDataGRCh38FamilyNoVep(); break; case "GRCh38 Samples 0": reportData = await fetchReportDataGRCh38NoSample(); break; case "GRCh38 Samples 1": reportData = await fetchReportDataGRCh38Data1Sample(); break; case "GRCh38 Samples 100": reportData = await fetchReportDataGRCh38Data100Samples(); break; case "GRCh38 Samples 1 STR": reportData = await fetchReportDataGRCh38Str(); break; default: throw new Error(`unknown dataset id '${id}'`); } const SQL = await initSqlJs({ wasmBinary: reportData.wasmBinary!.buffer as ArrayBuffer }); const reportDatabase = new ReportDatabase(new SQL.Database(reportData.database)); return new ApiClient(reportDatabase, reportData); } async function fetchReportDataGRCh38Family(): Promise { return { database: await fetchDatabaseFamily(), fastaGz: await fetchFastaGzGRCh38(), genesGz: await fetchGenesGzGRCh38(), cram: { Patient: { cram: await fetchCramGRCh38(), crai: await fetchCraiGRCh38(), }, }, wasmBinary: await fetchSqlWasm(), }; } async function fetchReportDataGRCh38FamilyNoVep() { return { database: await fetchDatabaseNoVep(), fastaGz: await fetchFastaGzGRCh38(), genesGz: await fetchGenesGzGRCh38(), cram: { Patient: { cram: await fetchCramGRCh38(), crai: await fetchCraiGRCh38(), }, }, wasmBinary: await fetchSqlWasm(), }; } async function fetchReportDataGRCh38Data1Sample(): Promise { return { database: await fetchDatabaseSamples1(), fastaGz: await fetchFastaGzGRCh38(), genesGz: await fetchGenesGzGRCh38(), cram: { SAMPLE1: { cram: await fetchCramGRCh38(), crai: await fetchCraiGRCh38(), }, }, wasmBinary: await fetchSqlWasm(), }; } async function fetchReportDataGRCh38Data100Samples(): Promise { return { database: await fetchDatabaseSamples100(), fastaGz: await fetchFastaGzGRCh38(), genesGz: await fetchGenesGzGRCh38(), wasmBinary: await fetchSqlWasm(), }; } async function fetchReportDataGRCh38Str(): Promise { return { database: await fetchDatabaseStr(), fastaGz: await fetchFastaGzGRCh38(), genesGz: await fetchGenesGzGRCh38(), cram: { Patient: { cram: await fetchStrCramGRCh38(), crai: await fetchStrCraiGRCh38(), }, }, wasmBinary: await fetchSqlWasm(), }; } async function fetchReportDataGRCh38NoSample(): Promise { return { database: await fetchDatabaseSamples0(), fastaGz: await fetchFastaGzGRCh38(), genesGz: await fetchGenesGzGRCh38(), wasmBinary: await fetchSqlWasm(), }; }