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