UNPKG

@gmod/cram

Version:

read CRAM files with pure Javascript

86 lines (85 loc) 3.59 kB
import CramFile from './cramFile/index.ts'; import { type DecodeOptions } from './cramFile/record.ts'; import type { IndexOpts, Slice } from './craiIndex.ts'; import type { SeqFetch } from './cramFile/file.ts'; import type CramRecord from './cramFile/record.ts'; import type { GenericFilehandle } from 'generic-filehandle2'; export interface CramFileSource { cramFilehandle?: GenericFilehandle; cramUrl?: string; cramPath?: string; } export interface CramIndexLike { getEntriesForRange: (seqId: number, start: number, end: number, opts?: IndexOpts) => Promise<Slice[]>; hasDataForReferenceSequence: (seqId: number) => Promise<boolean>; } export default class IndexedCramFile { cram: CramFile; index: CramIndexLike; /** * * @param {object} args * @param {Index-like} args.index object that supports * getEntriesForRange(seqId,start,end) -> Promise[Array[index entries]] * * @param {CramFile} [args.cram] pre-constructed CramFile. If omitted, * provide cramPath, cramUrl, or cramFilehandle instead. * * @param {string} [args.cramPath] local file path to the CRAM file * @param {string} [args.cramUrl] remote URL of the CRAM file * @param {FileHandle} [args.cramFilehandle] generic-filehandle2 or similar * * @param {Function} [args.seqFetch] async (seqId, start, end) => string * returning reference sequence for a region; seqId is numeric, coords 1-based * * @param {number} [args.cacheSize] optional maximum number of CRAM records * to cache. default 20,000 * * @param {boolean} [args.checkSequenceMD5] - default true. if false, * disables verifying the MD5 checksum of the reference sequence underlying a * slice. In some applications, this check can cause an inconvenient amount * (many megabases) of sequences to be fetched. */ constructor(args: { index: CramIndexLike; } & ({ cram: CramFile; } | ({ cram?: undefined; seqFetch?: SeqFetch; checkSequenceMD5?: boolean; validateChecksums?: boolean; cacheSize?: number; } & CramFileSource))); /** * * @param seq numeric ID of the reference sequence * @param start start of the range of interest. 1-based closed coordinates. * @param end end of the range of interest. 1-based closed coordinates. */ getRecordsForRange(seq: number, start: number, end: number, opts?: { viewAsPairs?: boolean; pairAcrossChr?: boolean; maxInsertSize?: number; /** * Called as the slices covering the query are fetched and decoded, with * cumulative processed bytes and the total to fetch. Reported at slice * granularity (one tick per slice, including instant ticks for cached * slices) since slice byte sizes are known up front from the index. Lets * callers render a determinate progress bar. */ onProgress?: (bytesDownloaded: number, totalBytes?: number) => void; } & DecodeOptions): Promise<CramRecord[]>; getRecordsInSlice({ containerStart, sliceStart, sliceBytes, }: { containerStart: number; sliceStart: number; sliceBytes: number; }, filterFunction: (r: CramRecord) => boolean, decodeOptions?: DecodeOptions): Promise<CramRecord[]>; /** * * @param {number} seqId * @returns {Promise} true if the CRAM file contains data for the given * reference sequence numerical ID */ hasDataForReferenceSequence(seqId: number): Promise<boolean>; }