]> git.donarmstrong.com Git - bamtools.git/blob - src/api/BamAlignment.h
Moved BamAlignment data structure out to its own .h/.cpp. BamAux.h was getting over...
[bamtools.git] / src / api / BamAlignment.h
1 // ***************************************************************************
2 // BamAlignment.h (c) 2009 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 18 September 2010 (DB)
7 // ---------------------------------------------------------------------------
8 // Provides the BamAlignment data structure
9 // ***************************************************************************
10
11 #ifndef BAMALIGNMENT_H
12 #define BAMALIGNMENT_H
13
14 #include <string>
15 #include <vector>
16 #include "BamAux.h"
17
18 namespace BamTools {
19
20 // BamAlignment data structure
21 // explicitly labeled as 'struct' to indicate that (most of) its fields are public
22 struct BamAlignment {
23
24     // constructors & destructor
25     public:
26         BamAlignment(void);
27         BamAlignment(const BamAlignment& other);
28         ~BamAlignment(void);
29
30     // Queries against alignment flags
31     public:        
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
43
44     // Manipulate alignment flags
45     public:        
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
57
58     // Tag data access methods
59     public:
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.
65
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
77         
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
88
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)) 
93         
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
102         
103         // remove tag data
104         // returns true if removal was successful, false if error
105         // N.B. - returns false if TAG does not exist (no removal can occur)
106         // @tag - two character tag name
107         bool RemoveTag(const std::string& tag);
108
109     // Additional data access methods
110     public:
111         // calculates & returns alignment end position, based on starting position and CIGAR operations
112         // @usePadded - if true, counts inserted bases. Default is false, so that alignment end position matches the last base's position in reference
113         // @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.
114         int GetEndPosition(bool usePadded = false, bool zeroBased = true) const;  
115
116     // 'internal' utility methods 
117     private:
118         static bool FindTag(const std::string& tag, char* &pTagData, const unsigned int& tagDataLength, unsigned int& numBytesParsed);
119         static bool SkipToNextTag(const char storageType, char* &pTagData, unsigned int& numBytesParsed);
120
121     // Data members
122     public:
123         std::string Name;              // Read name
124         int32_t     Length;            // Query length
125         std::string QueryBases;        // 'Original' sequence (as reported from sequencing machine)
126         std::string AlignedBases;      // 'Aligned' sequence (includes any indels, padding, clipping)
127         std::string Qualities;         // FASTQ qualities (ASCII characters, not numeric values)
128         std::string TagData;           // Tag data (accessor methods will pull the requested information out)
129         int32_t     RefID;             // ID number for reference sequence
130         int32_t     Position;          // Position (0-based) where alignment starts
131         uint16_t    Bin;               // Bin in BAM file where this alignment resides
132         uint16_t    MapQuality;        // Mapping quality score
133         uint32_t    AlignmentFlag;     // Alignment bit-flag - see Is<something>() methods to query this value, SetIs<something>() methods to manipulate 
134         std::vector<CigarOp> CigarData; // CIGAR operations for this alignment
135         int32_t     MateRefID;         // ID number for reference sequence where alignment's mate was aligned
136         int32_t     MatePosition;      // Position (0-based) where alignment's mate starts
137         int32_t     InsertSize;        // Mate-pair insert size
138           
139     // internal data
140     private:
141         struct BamAlignmentSupportData {
142       
143             // data members
144             std::string AllCharData;
145             uint32_t    BlockLength;
146             uint32_t    NumCigarOperations;
147             uint32_t    QueryNameLength;
148             uint32_t    QuerySequenceLength;
149             bool        HasCoreOnly;
150             
151             // constructor
152             BamAlignmentSupportData(void)
153                 : BlockLength(0)
154                 , NumCigarOperations(0)
155                 , QueryNameLength(0)
156                 , QuerySequenceLength(0)
157                 , HasCoreOnly(false)
158             { }
159         };
160         
161         // contains raw character data & lengths
162         BamAlignmentSupportData SupportData;   
163         
164         // allow these classes access to BamAlignment private members (SupportData)
165         // but client code should not need to touch this data
166         friend class BamReader;
167         friend class BamWriter;
168
169     // Alignment flag query constants
170     // Use the get/set methods above instead
171     private:
172         enum { PAIRED        = 1
173              , PROPER_PAIR   = 2
174              , UNMAPPED      = 4
175              , MATE_UNMAPPED = 8
176              , REVERSE       = 16
177              , MATE_REVERSE  = 32
178              , READ_1        = 64
179              , READ_2        = 128
180              , SECONDARY     = 256
181              , QC_FAILED     = 512
182              , DUPLICATE     = 1024 
183              };
184 };
185
186 // convenience typedef(s)
187 typedef std::vector<BamAlignment> BamAlignmentVector;
188
189 } // namespace BamTools
190
191 #endif // BAMALIGNMENT_H