Skip to content

Commit

Permalink
Merge pull request #52 from molgenis/feat/fasta
Browse files Browse the repository at this point in the history
Retrieve embedded reference sequence data
  • Loading branch information
dennishendriksen authored Apr 21, 2021
2 parents 39963e3 + 2ff6512 commit ca5f422
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
18 changes: 17 additions & 1 deletion src/ApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -66,6 +67,21 @@ export class ApiClient implements Api {
return Promise.resolve(this.reportData.binary.vcfGz);
}

getFastaGz(contig: string, pos: number): Promise<Buffer | null> {
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<T extends Resource>(resource: string, params: Params = {}): Promise<PagedItems<T>> {
return new Promise((resolve, reject) => {
if (!this.reportData.data[resource]) {
Expand Down
1 change: 1 addition & 0 deletions src/ApiData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface Items<T extends Resource> {

export interface EncodedData extends EncodedDataContainer {
vcfGz: string;
fastaGz: EncodedDataContainer;
}

export interface Resource {
Expand Down
27 changes: 26 additions & 1 deletion src/__tests__/ApiClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
2 changes: 2 additions & 0 deletions src/__tests__/interval0.fasta
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
>1:17350500-17350600
CTCCGTTCCACCAGTAGCTGGGGCAGCTGGTGCTACAGCAGGCACAGAGAATGCACTCGTAGAGCCCGTCCTGTATGGGGAGAAAAGAGAGGCAGGAGCTT
2 changes: 2 additions & 0 deletions src/__tests__/interval1.fasta
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
>2:47637200-47637300
TTGTTAAATTTTTAAAATTTTATTTTTACTTAGGCTTCTCCTGGCAATCTCTCTCAGTTTGAAGACATTCTCTTTGGTAACAATGATATGTCAGCTTCCAT

0 comments on commit ca5f422

Please sign in to comment.