@gmod/cram
Version:
read CRAM files with pure Javascript
160 lines (159 loc) • 7.32 kB
TypeScript
import type CramContainerCompressionScheme from './container/compressionScheme.ts';
import type decodeRecord from './slice/decodeRecord.ts';
export interface RefRegion {
start: number;
end: number;
seq: string;
}
interface ReadFeatureBase {
pos: number;
refPos: number;
}
/**
* Read features describe differences between a read and the reference sequence.
* Each feature has a code indicating the type of difference, a position in the
* read (pos), and a position on the reference (refPos).
*/
export type ReadFeature =
/** I=insertion, S=soft clip, b=bases, i=single-base insertion — all carry a sequence string */
(ReadFeatureBase & {
code: 'I' | 'S' | 'b' | 'i';
data: string;
})
/** B=base and quality pair — [substituted base, quality score] */
| (ReadFeatureBase & {
code: 'B';
data: [string, number];
})
/** X=base substitution — data is the substitution matrix index, ref/sub filled in by addReferenceSequence */
| (ReadFeatureBase & {
code: 'X';
data: number;
ref?: string;
sub?: string;
})
/** D=deletion, N=reference skip, H=hard clip, P=padding, Q=single quality score */
| (ReadFeatureBase & {
code: 'D' | 'N' | 'H' | 'P' | 'Q';
data: number;
})
/** q=quality scores for a stretch of bases */
| (ReadFeatureBase & {
code: 'q';
data: number[];
});
export interface DecodeOptions {
/** Whether to parse tags. If false, raw tag data is stored for lazy parsing. Default true. */
decodeTags?: boolean;
}
export declare const defaultDecodeOptions: Required<DecodeOptions>;
export interface MateRecord {
readName?: string;
sequenceId: number;
alignmentStart: number;
flags?: number;
uniqueId?: number;
}
export declare const BamFlags: readonly [readonly [1, "Paired"], readonly [2, "ProperlyPaired"], readonly [4, "SegmentUnmapped"], readonly [8, "MateUnmapped"], readonly [16, "ReverseComplemented"], readonly [32, "MateReverseComplemented"], readonly [64, "Read1"], readonly [128, "Read2"], readonly [256, "Secondary"], readonly [512, "FailedQc"], readonly [1024, "Duplicate"], readonly [2048, "Supplementary"]];
export declare const CramFlags: readonly [readonly [1, "PreservingQualityScores"], readonly [2, "Detached"], readonly [4, "WithMateDownstream"], readonly [8, "DecodeSequenceAsStar"]];
export declare const MateFlags: readonly [readonly [1, "OnNegativeStrand"], readonly [2, "Unmapped"]];
type FlagsDecoder<Type> = {
[Property in Type as `is${Capitalize<string & Property>}`]: (flags: number) => boolean;
};
type FlagsEncoder<Type> = {
[Property in Type as `set${Capitalize<string & Property>}`]: (flags: number) => number;
};
export declare const BamFlagsDecoder: FlagsDecoder<"Paired" | "ProperlyPaired" | "SegmentUnmapped" | "MateUnmapped" | "ReverseComplemented" | "MateReverseComplemented" | "Read1" | "Read2" | "Secondary" | "FailedQc" | "Duplicate" | "Supplementary"> & FlagsEncoder<"Paired" | "ProperlyPaired" | "SegmentUnmapped" | "MateUnmapped" | "ReverseComplemented" | "MateReverseComplemented" | "Read1" | "Read2" | "Secondary" | "FailedQc" | "Duplicate" | "Supplementary">;
export declare const CramFlagsDecoder: FlagsDecoder<"PreservingQualityScores" | "Detached" | "WithMateDownstream" | "DecodeSequenceAsStar"> & FlagsEncoder<"PreservingQualityScores" | "Detached" | "WithMateDownstream" | "DecodeSequenceAsStar">;
export declare const MateFlagsDecoder: FlagsDecoder<"OnNegativeStrand" | "Unmapped"> & FlagsEncoder<"OnNegativeStrand" | "Unmapped">;
/**
* Class of each CRAM record returned by this API.
*/
export default class CramRecord {
tags: Record<string, string | number | number[] | undefined>;
flags: number;
cramFlags: number;
readBases?: string | null;
_refRegion?: RefRegion;
readFeatures?: ReadFeature[];
alignmentStart: number;
lengthOnRef: number | undefined;
readLength: number;
templateLength?: number;
templateSize?: number;
private _readName?;
private _readNameRaw?;
_syntheticReadName?: string;
mateRecordNumber?: number;
mate?: MateRecord;
uniqueId: number;
sequenceId: number;
readGroupId: number;
mappingQuality: number | undefined;
qualityScores: Uint8Array | null | undefined;
get readName(): string | undefined;
constructor({ flags, cramFlags, readLength, mappingQuality, lengthOnRef, qualityScores, mateRecordNumber, readBases, readFeatures, mate, readGroupId, readNameRaw, sequenceId, uniqueId, templateSize, alignmentStart, tags, }: ReturnType<typeof decodeRecord>);
/**
* Get a single quality score at the given index.
* @param index 0-based index into the quality scores
* @returns the quality score at that index, or undefined if not available
*/
qualityScoreAt(index: number): number | undefined;
/** @returns {boolean} true if the read is paired, regardless of whether both segments are mapped */
isPaired(): boolean;
/** @returns {boolean} true if the read is paired, and both segments are mapped */
isProperlyPaired(): boolean;
/** @returns {boolean} true if the read itself is unmapped; conflictive with isProperlyPaired */
isSegmentUnmapped(): boolean;
/** @returns {boolean} true if the mate is unmapped; conflictive with isProperlyPaired */
isMateUnmapped(): boolean;
/** @returns {boolean} true if the read is mapped to the reverse strand */
isReverseComplemented(): boolean;
/** @returns {boolean} true if the mate is mapped to the reverse strand */
isMateReverseComplemented(): boolean;
isRead1(): boolean;
isRead2(): boolean;
isSecondary(): boolean;
isFailedQc(): boolean;
isDuplicate(): boolean;
isSupplementary(): boolean;
isDetached(): boolean;
hasMateDownStream(): boolean;
isPreservingQualityScores(): boolean;
isUnknownBases(): boolean;
/**
* Get the original sequence of this read.
* @returns {String} sequence basepairs
*/
getReadBases(): string | null | undefined;
/**
* Get the CIGAR string describing this read's alignment against the
* reference, reconstructed from the read features. Substitutions and
* verbatim bases are reported as alignment matches (M), following the plain
* CIGAR convention where M covers both matches and mismatches. Unmapped
* reads return '*'.
*
* See CRAMv3 §10.2 (Read features):
* https://samtools.github.io/hts-specs/CRAMv3.pdf
*
* @returns {string} the CIGAR string, e.g. "50M2I48M"
*/
getCigarString(): string;
getPairOrientation(): string | undefined;
/**
* Annotates this feature with the given reference sequence basepair
* information. This will add a `sub` and a `ref` item to base
* substitution read features given the actual substituted and reference
* base pairs, and will make the `getReadBases()` method work.
*
* @param {object} refRegion
* @param {number} refRegion.start
* @param {number} refRegion.end
* @param {string} refRegion.seq
* @param {CramContainerCompressionScheme} compressionScheme
* @returns {undefined} nothing
*/
addReferenceSequence(refRegion: RefRegion, compressionScheme: CramContainerCompressionScheme): void;
toJSON(): Record<string, unknown>;
}
export { CramRecord };