diff --git a/package-lock.json b/package-lock.json index 7902331..cfaa6e2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@molgenis/vip-report-api", - "version": "2.0.0", + "version": "2.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@molgenis/vip-report-api", - "version": "2.0.0", + "version": "2.1.0", "license": "LGPL-3.0", "dependencies": { "ascii85": "^1.0.2", diff --git a/package.json b/package.json index c4872b0..6faf700 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@molgenis/vip-report-api", - "version": "2.0.0", + "version": "2.1.0", "description": "Report API for Variant Call Format (VCF) Report Templates", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/ApiClient.ts b/src/ApiClient.ts index 8f6bc38..e8da8bf 100644 --- a/src/ApiClient.ts +++ b/src/ApiClient.ts @@ -33,10 +33,11 @@ interface Data { interface BinaryData extends BinaryDataNode { vcfGz: Buffer; + fastaGz: BinaryDataNode; } interface BinaryDataNode { - [key: string]: Uint8Array | BinaryDataNode; + [key: string]: Buffer | BinaryDataNode; } export class ApiClient implements Api { @@ -66,6 +67,21 @@ export class ApiClient implements Api { return Promise.resolve(this.reportData.binary.vcfGz); } + getFastaGz(contig: string, pos: number): Promise { + let buffer: Buffer | null = null; + for (const [key, value] of Object.entries(this.reportData.binary.fastaGz)) { + const pair = key.split(':'); + if (pair[0] === contig) { + const interval = pair[1].split('-'); + if (pos >= parseInt(interval[0], 10) && pos <= parseInt(interval[1], 10)) { + buffer = value as Buffer; + break; + } + } + } + return Promise.resolve(buffer); + } + private get(resource: string, params: Params = {}): Promise> { return new Promise((resolve, reject) => { if (!this.reportData.data[resource]) { diff --git a/src/ApiData.ts b/src/ApiData.ts index ada761f..cb6afa8 100644 --- a/src/ApiData.ts +++ b/src/ApiData.ts @@ -33,6 +33,7 @@ export interface Items { export interface EncodedData extends EncodedDataContainer { vcfGz: string; + fastaGz: EncodedDataContainer; } export interface Resource { diff --git a/src/__tests__/ApiClient.test.ts b/src/__tests__/ApiClient.test.ts index ce0d23d..6b31965 100644 --- a/src/__tests__/ApiClient.test.ts +++ b/src/__tests__/ApiClient.test.ts @@ -50,7 +50,15 @@ beforeEach(() => { } }, base85: { - vcfGz: new Base85().encode(Buffer.from(gzipSync(fs.readFileSync(__dirname + '/trio.vcf')))) + vcfGz: new Base85().encode(Buffer.from(gzipSync(fs.readFileSync(__dirname + '/trio.vcf')))), + fastaGz: { + '1:17350500-17350600': new Base85().encode( + Buffer.from(gzipSync(fs.readFileSync(__dirname + '/interval0.fasta'))) + ), + '2:47637200-47637300': new Base85().encode( + Buffer.from(gzipSync(fs.readFileSync(__dirname + '/interval1.fasta'))) + ) + } } }; api = new ApiClient(reportData); @@ -636,3 +644,20 @@ test('getVcfGz', async () => { // null check, because size check differs between local machine and Travis expect(vcfGz).not.toBe(null); }); + +test('getFastaGz', async () => { + const fastaGz = await api.getFastaGz('1', 17350550); + // null check, because size check differs between local machine and Travis + expect(fastaGz).not.toBe(null); +}); + +test('getFastaGz - unknown interval', async () => { + const fastaGz = await api.getFastaGz('1', 17350450); + // null check, because size check differs between local machine and Travis + expect(fastaGz).toBe(null); +}); + +test('getFastaGz - existing interval in other contig', async () => { + const fastaGz = await api.getFastaGz('1', 47637250); + expect(fastaGz).toBe(null); +}); diff --git a/src/__tests__/interval0.fasta b/src/__tests__/interval0.fasta new file mode 100644 index 0000000..2ebf724 --- /dev/null +++ b/src/__tests__/interval0.fasta @@ -0,0 +1,2 @@ +>1:17350500-17350600 +CTCCGTTCCACCAGTAGCTGGGGCAGCTGGTGCTACAGCAGGCACAGAGAATGCACTCGTAGAGCCCGTCCTGTATGGGGAGAAAAGAGAGGCAGGAGCTT \ No newline at end of file diff --git a/src/__tests__/interval1.fasta b/src/__tests__/interval1.fasta new file mode 100644 index 0000000..646cc65 --- /dev/null +++ b/src/__tests__/interval1.fasta @@ -0,0 +1,2 @@ +>2:47637200-47637300 +TTGTTAAATTTTTAAAATTTTATTTTTACTTAGGCTTCTCCTGGCAATCTCTCTCAGTTTGAAGACATTCTCTTTGGTAACAATGATATGTCAGCTTCCAT \ No newline at end of file