github.com/Schaudge/hts@v0.0.0-20240223063651-737b4d69d68c/bam/add_example_test.go (about)

     1  // Copyright ©2015 The bíogo Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package bam_test
     6  
     7  import (
     8  	"io"
     9  	"log"
    10  	"os"
    11  
    12  	"github.com/Schaudge/hts/bam"
    13  )
    14  
    15  func ExampleIndex_Add() {
    16  	// Create a BAI for the BAM read from standard in and write it to standard out.
    17  	br, err := bam.NewReader(os.Stdin, 1)
    18  	if err != nil {
    19  		log.Fatalf("failed to open BAM: %v", err)
    20  	}
    21  
    22  	var bai bam.Index
    23  	for {
    24  		r, err := br.Read()
    25  		if err == io.EOF {
    26  			break
    27  		}
    28  		if err != nil {
    29  			log.Fatalf("failed to read BAM record: %v", err)
    30  		}
    31  		err = bai.Add(r, br.LastChunk())
    32  		if err != nil {
    33  			log.Fatalf("failed to add record to BAM index: %v", err)
    34  		}
    35  	}
    36  
    37  	err = bam.WriteIndex(os.Stdout, &bai)
    38  	if err != nil {
    39  		log.Fatalf("failed to write BAM index: %v", err)
    40  	}
    41  }