Genotype API Documentation - v0.1.0
    Preparing search index...

    Class SAMWriter

    SAM writer for outputting alignments and headers

    Designed to complement the SAMParser with full format compliance. Handles all SAM format requirements including header validation, alignment field formatting, CIGAR string validation, and optional tag serialization.

    const writer = new SAMWriter();
    const samString = writer.writeString([header1, alignment1, alignment2]);
    console.log(samString);
    const writer = new SAMWriter({ validate: true });
    await writer.writeFile('/path/to/output.sam', [header, ...alignments]);
    const writer = new SAMWriter();
    const stream = new WritableStream({...});
    await writer.writeStream(stream, async function*() {
    yield header;
    for (const alignment of alignments) {
    yield alignment;
    }
    }());
    Index

    Constructors

    • Create a new SAM writer with specified options

      Parameters

      • options: {
            validate?: boolean;
            includeLineNumbers?: boolean;
            onError?: (error: string, record?: SAMHeader | SAMAlignment) => void;
            onWarning?: (warning: string, record?: SAMHeader | SAMAlignment) => void;
        } = {}

        Writer configuration options

        • Optionalvalidate?: boolean

          Validate records before writing (default: true)

        • OptionalincludeLineNumbers?: boolean

          Include line numbers in error reporting (default: true)

        • OptionalonError?: (error: string, record?: SAMHeader | SAMAlignment) => void

          Custom error handler

        • OptionalonWarning?: (warning: string, record?: SAMHeader | SAMAlignment) => void

          Custom warning handler

      Returns SAMWriter

    Methods

    • Write SAM records to string format

      Parameters

      Returns string

      Formatted SAM string with proper line separators

      When records are invalid or formatting fails

      const writer = new SAMWriter();
      const records = [header, alignment1, alignment2];
      const samData = writer.writeString(records);
    • Write SAM records to file using Bun's native file I/O

      Parameters

      • filePath: string

        Path where SAM file should be written

      • records: (SAMHeader | SAMAlignment)[]

        Array of SAM headers and alignments to write

      • Optionaloptions: { encoding?: "utf8" | "binary"; mode?: number }

        File writing options

      Returns Promise<void>

      When file cannot be written or records are invalid

      const writer = new SAMWriter();
      await writer.writeFile('/path/to/output.sam', [header, ...alignments]);
    • Write SAM records to a WritableStream

      Parameters

      • stream: WritableStream<Uint8Array<ArrayBufferLike>>

        WritableStream to write formatted SAM data to

      • records: AsyncIterable<SAMHeader | SAMAlignment>

        Async iterable of SAM headers and alignments

      Returns Promise<void>

      When stream writing fails or records are invalid

      const writer = new SAMWriter();
      const stream = new WritableStream({...});
      await writer.writeStream(stream, async function*() {
      yield header;
      for (const alignment of alignments) {
      yield alignment;
      }
      }());