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

     1  // Copyright ©2012 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 sam
     6  
     7  // A Flags represents a BAM record's alignment FLAG field.
     8  type Flags uint16
     9  
    10  const (
    11  	Paired        Flags = 1 << iota // The read is paired in sequencing, no matter whether it is mapped in a pair.
    12  	ProperPair                      // The read is mapped in a proper pair.
    13  	Unmapped                        // The read itself is unmapped; conflictive with ProperPair.
    14  	MateUnmapped                    // The mate is unmapped.
    15  	Reverse                         // The read is mapped to the reverse strand.
    16  	MateReverse                     // The mate is mapped to the reverse strand.
    17  	Read1                           // This is read1.
    18  	Read2                           // This is read2.
    19  	Secondary                       // Not primary alignment.
    20  	QCFail                          // QC failure.
    21  	Duplicate                       // Optical or PCR duplicate.
    22  	Supplementary                   // Supplementary alignment, indicates alignment is part of a chimeric alignment.
    23  )
    24  
    25  // String representation of BAM alignment flags:
    26  //  0x001 - p - Paired
    27  //  0x002 - P - ProperPair
    28  //  0x004 - u - Unmapped
    29  //  0x008 - U - MateUnmapped
    30  //  0x010 - r - Reverse
    31  //  0x020 - R - MateReverse
    32  //  0x040 - 1 - Read1
    33  //  0x080 - 2 - Read2
    34  //  0x100 - s - Secondary
    35  //  0x200 - f - QCFail
    36  //  0x400 - d - Duplicate
    37  //  0x800 - S - Supplementary
    38  //
    39  // Note that flag bits are represented high order to the right.
    40  func (f Flags) String() string {
    41  	// If 0x01 is unset, no assumptions can be made about 0x02, 0x08, 0x20, 0x40 and 0x80
    42  	const pairedMask = ProperPair | MateUnmapped | MateReverse | MateReverse | Read1 | Read2
    43  	if f&1 == 0 {
    44  		f &^= pairedMask
    45  	}
    46  
    47  	const flags = "pPuUrR12sfdS"
    48  
    49  	b := make([]byte, len(flags))
    50  	for i, c := range flags {
    51  		if f&(1<<uint(i)) != 0 {
    52  			b[i] = byte(c)
    53  		} else {
    54  			b[i] = '-'
    55  		}
    56  	}
    57  
    58  	return string(b)
    59  }