1 // ***************************************************************************
2 // BamAlignment.h (c) 2009 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 19 November 2010 (DB)
7 // ---------------------------------------------------------------------------
8 // Provides the BamAlignment data structure
9 // ***************************************************************************
11 #ifndef BAMALIGNMENT_H
12 #define BAMALIGNMENT_H
14 #include <api/api_global.h>
15 #include <api/BamAux.h>
21 // BamAlignment data structure
22 // explicitly labeled as 'struct' to indicate that (most of) its fields are public
23 struct API_EXPORT BamAlignment {
25 // constructors & destructor
28 BamAlignment(const BamAlignment& other);
31 // Queries against alignment flags
33 bool IsDuplicate(void) const; // Returns true if this read is a PCR duplicate
34 bool IsFailedQC(void) const; // Returns true if this read failed quality control
35 bool IsFirstMate(void) const; // Returns true if alignment is first mate on read
36 bool IsMapped(void) const; // Returns true if alignment is mapped
37 bool IsMateMapped(void) const; // Returns true if alignment's mate is mapped
38 bool IsMateReverseStrand(void) const; // Returns true if alignment's mate mapped to reverse strand
39 bool IsPaired(void) const; // Returns true if alignment part of paired-end read
40 bool IsPrimaryAlignment(void) const; // Returns true if reported position is primary alignment
41 bool IsProperPair(void) const; // Returns true if alignment is part of read that satisfied paired-end resolution
42 bool IsReverseStrand(void) const; // Returns true if alignment mapped to reverse strand
43 bool IsSecondMate(void) const; // Returns true if alignment is second mate on read
45 // Manipulate alignment flags
47 void SetIsDuplicate(bool ok); // Sets "PCR duplicate" flag
48 void SetIsFailedQC(bool ok); // Sets "failed quality control" flag
49 void SetIsFirstMate(bool ok); // Sets "alignment is first mate" flag
50 void SetIsMateUnmapped(bool ok); // Sets "alignment's mate is mapped" flag
51 void SetIsMateReverseStrand(bool ok); // Sets "alignment's mate mapped to reverse strand" flag
52 void SetIsPaired(bool ok); // Sets "alignment part of paired-end read" flag
53 void SetIsProperPair(bool ok); // Sets "alignment is part of read that satisfied paired-end resolution" flag
54 void SetIsReverseStrand(bool ok); // Sets "alignment mapped to reverse strand" flag
55 void SetIsSecondaryAlignment(bool ok); // Sets "position is primary alignment" flag
56 void SetIsSecondMate(bool ok); // Sets "alignment is second mate on read" flag
57 void SetIsUnmapped(bool ok); // Sets "alignment is mapped" flag
59 // Tag data access methods
61 // -------------------------------------------------------------------------------------
62 // N.B. - The following tag access methods may not be used on BamAlignments fetched
63 // using BamReader::GetNextAlignmentCore(). Attempting to use them will not result in
64 // error message (to keep output clean) but will ALWAYS return false. Only user-created
65 // BamAlignments or those retrieved using BamReader::GetNextAlignment() are valid here.
67 // add tag data (create new TAG entry with TYPE and VALUE)
68 // TYPE is one of {A, i, f, Z, H} depending on VALUE - see SAM/BAM spec for details
69 // returns true if new data added, false if error or TAG already exists
70 // N.B. - will NOT modify existing tag. Use EditTag() instead
71 // @tag - two character tag name
72 // @type - single character tag type (see SAM/BAM spec for details)
73 // @value - value to associate with tag
74 bool AddTag(const std::string& tag, const std::string& type, const std::string& value); // type must be Z or H
75 bool AddTag(const std::string& tag, const std::string& type, const uint32_t& value); // type must be A or i
76 bool AddTag(const std::string& tag, const std::string& type, const int32_t& value); // type must be A or i
77 bool AddTag(const std::string& tag, const std::string& type, const float& value); // type must be A, i, or f
79 // edit tag data (sets existing TAG with TYPE to VALUE or adds new TAG if not already present)
80 // TYPE is one of {A, i, f, Z, H} depending on VALUE - see SAM/BAM spec for details
81 // returns true if edit was successfaul, false if error
82 // @tag - two character tag name
83 // @type - single character tag type (see SAM/BAM spec for details)
84 // @value - new value for tag
85 bool EditTag(const std::string& tag, const std::string& type, const std::string& value); // type must be Z or H
86 bool EditTag(const std::string& tag, const std::string& type, const uint32_t& value); // type must be A or i
87 bool EditTag(const std::string& tag, const std::string& type, const int32_t& value); // type must be A or i
88 bool EditTag(const std::string& tag, const std::string& type, const float& value); // type must be A, i, or f
90 // specific tag data access methods - these only remain for legacy support
91 // returns whether specific tag could be retrieved
92 bool GetEditDistance(uint32_t& editDistance) const; // get "NM" tag data (equivalent to GetTag("NM", editDistance))
93 bool GetReadGroup(std::string& readGroup) const; // get "RG" tag data (equivalent to GetTag("RG", readGroup))
95 // generic tag data access methods
96 // returns whether tag is found & tag type is compatible with DESTINATION
97 // @tag - two character tag name
98 // @destination - if found, tag value is stored here
99 bool GetTag(const std::string& tag, std::string& destination) const; // access variable-length char or hex strings
100 bool GetTag(const std::string& tag, uint32_t& destination) const; // access unsigned integer data
101 bool GetTag(const std::string& tag, int32_t& destination) const; // access signed integer data
102 bool GetTag(const std::string& tag, float& destination) const; // access floating point data
104 // retrieve the tag type code for TAG
105 // returns true if tag could be found and type determined
106 bool GetTagType(const std::string& tag, char& type) const;
109 // returns true if removal was successful, false if error
110 // N.B. - returns false if TAG does not exist (no removal can occur)
111 // @tag - two character tag name
112 bool RemoveTag(const std::string& tag);
114 // Additional data access methods
116 // calculates & returns alignment end position, based on starting position and CIGAR operations
117 // @usePadded - if true, counts inserted bases. Default is false, so that alignment end position matches the last base's position in reference
118 // @zeroBased - if true, returns 0-based coordinate; else returns 1-based. Setting this to false is useful when using BAM data along with other, half-open formats.
119 int GetEndPosition(bool usePadded = false, bool zeroBased = true) const;
121 // 'internal' utility methods
123 static bool FindTag(const std::string& tag, char* &pTagData, const unsigned int& tagDataLength, unsigned int& numBytesParsed);
124 static bool SkipToNextTag(const char storageType, char* &pTagData, unsigned int& numBytesParsed);
128 std::string Name; // Read name
129 int32_t Length; // Query length
130 std::string QueryBases; // 'Original' sequence (as reported from sequencing machine)
131 std::string AlignedBases; // 'Aligned' sequence (includes any indels, padding, clipping)
132 std::string Qualities; // FASTQ qualities (ASCII characters, not numeric values)
133 std::string TagData; // Tag data (accessor methods will pull the requested information out)
134 int32_t RefID; // ID number for reference sequence
135 int32_t Position; // Position (0-based) where alignment starts
136 uint16_t Bin; // Bin in BAM file where this alignment resides
137 uint16_t MapQuality; // Mapping quality score
138 uint32_t AlignmentFlag; // Alignment bit-flag - see Is<something>() methods to query this value, SetIs<something>() methods to manipulate
139 std::vector<CigarOp> CigarData; // CIGAR operations for this alignment
140 int32_t MateRefID; // ID number for reference sequence where alignment's mate was aligned
141 int32_t MatePosition; // Position (0-based) where alignment's mate starts
142 int32_t InsertSize; // Mate-pair insert size
145 struct BamAlignmentSupportData {
148 std::string AllCharData;
149 uint32_t BlockLength;
150 uint32_t NumCigarOperations;
151 uint32_t QueryNameLength;
152 uint32_t QuerySequenceLength;
156 BamAlignmentSupportData(void)
158 , NumCigarOperations(0)
160 , QuerySequenceLength(0)
165 // ** THIS IS INTERNAL DATA! DO NOT ACCESS OR EDIT FROM CLIENT CODE **
167 // Intended for use by BamReader & BamWriter ONLY. No, really, I mean it.
169 // BamTools makes some assumptions about this data being pristine, so please don't tinker with it.
170 // The regular data fields above should be sufficient for client code.
172 // Technical/design note - Ideally, this would be a private data member with BamReader & BamWriter
173 // allowed direct 'friend' access. However older compilers (especially gcc before v4.1 ) do not
174 // propagate the friend access to BamReader/Writer's implementation inner classes.
175 BamAlignmentSupportData SupportData;
177 // Alignment flag query constants
178 // Use the get/set methods above instead
194 // convenience typedef(s)
195 typedef std::vector<BamAlignment> BamAlignmentVector;
197 } // namespace BamTools
199 #endif // BAMALIGNMENT_H