UNPKG

@gmod/vcf

Version:

High performance streaming Variant Call Format (VCF) parser in pure JavaScript

106 lines (105 loc) 3.2 kB
/** * Class representing a VCF parser, instantiated with the VCF header. * * @param {object} args * * @param {string} args.header - The VCF header. Supports both LF and CRLF * newlines. * * @param {boolean} args.strict - Whether to parse in strict mode or not * (default true) */ export default class VCFParser { private metadata; strict: boolean; samples: string[]; constructor({ header, strict, }: { header: string; strict?: boolean; }); private parseSamples; /** * Parse a VCF metadata line (i.e. a line that starts with "##") and add its * properties to the object. * * @param {string} line - A line from the VCF. Supports both LF and CRLF * newlines. */ private parseMetadata; /** * Parse a VCF header structured meta string (i.e. a meta value that starts * with "<ID=...") * * @param {string} metaVal - The VCF metadata value * * @returns {Array} - Array with two entries, 1) a string of the metadata ID * and 2) an object with the other key-value pairs in the metadata */ private parseStructuredMetaVal; /** * Get metadata filtered by the elements in args. For example, can pass * ('INFO', 'DP') to only get info on an metadata tag that was like * "##INFO=<ID=DP,...>" * * @param {...string} args - List of metadata filter strings. * * @returns {any} An object, string, or number, depending on the filtering */ getMetadata(...args: string[]): any; /** * Parse a VCF line into an object like * * ```typescript * { * CHROM: 'contigA', * POS: 3000, * ID: ['rs17883296'], * REF: 'G', * ALT: ['T', 'A'], * QUAL: 100, * FILTER: 'PASS', * INFO: { * NS: [3], * DP: [14], * AF: [0.5], * DB: true, * XYZ: ['5'], * }, * SAMPLES: () => ({ * HG00096: { * GT: ['0|0'], * AP: ['0.000', '0.000'], * } * }), * GENOTYPES: () => ({ * HG00096: '0|0' * }) * } * ``` * * SAMPLES and GENOTYPES methods are functions instead of static data fields * because it avoids parsing the potentially long list of samples from e.g. * 1000 genotypes data unless requested. * * The SAMPLES function gives all info about the samples * * The GENOTYPES function only extracts the raw GT string if it exists, for * potentially optimized parsing by programs that need it * * @param {string} line - A string of a line from a VCF */ parseLine(line: string): { CHROM: string | undefined; POS: number; ALT: string[] | undefined; INFO: any; REF: string | undefined; FILTER: string | string[] | undefined; ID: string[] | undefined; QUAL: number | undefined; FORMAT: string | undefined; SAMPLES: () => Record<string, Record<string, (string | number | undefined)[] | undefined>>; GENOTYPES: () => Record<string, string>; }; } export type Variant = ReturnType<VCFParser['parseLine']>;