]> git.donarmstrong.com Git - bamtools.git/blob - src/api/BamAlignment.h
Extracted BamReaderPrivate & BamWriterPrivate from inner classes.
[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: 19 November 2010 (DB)
7 // ---------------------------------------------------------------------------
8 // Provides the BamAlignment data structure
9 // ***************************************************************************
10
11 #ifndef BAMALIGNMENT_H
12 #define BAMALIGNMENT_H
13
14 #include <api/api_global.h>
15 #include <api/BamAux.h>
16 #include <string>
17 #include <vector>
18
19 namespace BamTools {
20
21 // forward declare BamAlignment's friend classes
22 namespace Internal {
23     class BamReaderPrivate;
24     class BamWriterPrivate;
25 } // namespace Internal
26
27 // BamAlignment data structure
28 // explicitly labeled as 'struct' to indicate that (most of) its fields are public
29 struct API_EXPORT BamAlignment {
30
31     // constructors & destructor
32     public:
33         BamAlignment(void);
34         BamAlignment(const BamAlignment& other);
35         ~BamAlignment(void);
36
37     // Queries against alignment flags
38     public:        
39         bool IsDuplicate(void) const;           // Returns true if this read is a PCR duplicate       
40         bool IsFailedQC(void) const;            // Returns true if this read failed quality control      
41         bool IsFirstMate(void) const;           // Returns true if alignment is first mate on read        
42         bool IsMapped(void) const;              // Returns true if alignment is mapped        
43         bool IsMateMapped(void) const;          // Returns true if alignment's mate is mapped        
44         bool IsMateReverseStrand(void) const;   // Returns true if alignment's mate mapped to reverse strand        
45         bool IsPaired(void) const;              // Returns true if alignment part of paired-end read        
46         bool IsPrimaryAlignment(void) const;    // Returns true if reported position is primary alignment       
47         bool IsProperPair(void) const;          // Returns true if alignment is part of read that satisfied paired-end resolution     
48         bool IsReverseStrand(void) const;       // Returns true if alignment mapped to reverse strand
49         bool IsSecondMate(void) const;          // Returns true if alignment is second mate on read
50
51     // Manipulate alignment flags
52     public:        
53         void SetIsDuplicate(bool ok);           // Sets "PCR duplicate" flag        
54         void SetIsFailedQC(bool ok);            // Sets "failed quality control" flag        
55         void SetIsFirstMate(bool ok);           // Sets "alignment is first mate" flag        
56         void SetIsMateUnmapped(bool ok);        // Sets "alignment's mate is mapped" flag        
57         void SetIsMateReverseStrand(bool ok);   // Sets "alignment's mate mapped to reverse strand" flag        
58         void SetIsPaired(bool ok);              // Sets "alignment part of paired-end read" flag        
59         void SetIsProperPair(bool ok);          // Sets "alignment is part of read that satisfied paired-end resolution" flag        
60         void SetIsReverseStrand(bool ok);       // Sets "alignment mapped to reverse strand" flag        
61         void SetIsSecondaryAlignment(bool ok);  // Sets "position is primary alignment" flag        
62         void SetIsSecondMate(bool ok);          // Sets "alignment is second mate on read" flag        
63         void SetIsUnmapped(bool ok);            // Sets "alignment is mapped" flag
64
65     // Tag data access methods
66     public:
67         // -------------------------------------------------------------------------------------
68         // N.B. - The following tag access methods may not be used on BamAlignments fetched
69         // using BamReader::GetNextAlignmentCore().  Attempting to use them will not result in 
70         // error message (to keep output clean) but will ALWAYS return false.  Only user-created
71         // BamAlignments or those retrieved using BamReader::GetNextAlignment() are valid here.
72
73         // add tag data (create new TAG entry with TYPE and VALUE)
74         // TYPE is one of {A, i, f, Z, H} depending on VALUE - see SAM/BAM spec for details
75         // returns true if new data added, false if error or TAG already exists
76         // N.B. - will NOT modify existing tag. Use EditTag() instead
77         // @tag   - two character tag name
78         // @type  - single character tag type (see SAM/BAM spec for details)
79         // @value - value to associate with tag
80         bool AddTag(const std::string& tag, const std::string& type, const std::string& value); // type must be Z or H
81         bool AddTag(const std::string& tag, const std::string& type, const uint32_t& value);    // type must be A or i
82         bool AddTag(const std::string& tag, const std::string& type, const int32_t& value);     // type must be A or i
83         bool AddTag(const std::string& tag, const std::string& type, const float& value);       // type must be A, i, or f
84         
85         // edit tag data (sets existing TAG with TYPE to VALUE or adds new TAG if not already present)
86         // TYPE is one of {A, i, f, Z, H} depending on VALUE - see SAM/BAM spec for details
87         // returns true if edit was successfaul, false if error
88         // @tag   - two character tag name
89         // @type  - single character tag type (see SAM/BAM spec for details)
90         // @value - new value for tag
91         bool EditTag(const std::string& tag, const std::string& type, const std::string& value); // type must be Z or H
92         bool EditTag(const std::string& tag, const std::string& type, const uint32_t& value);    // type must be A or i
93         bool EditTag(const std::string& tag, const std::string& type, const int32_t& value);     // type must be A or i
94         bool EditTag(const std::string& tag, const std::string& type, const float& value);       // type must be A, i, or f
95
96         // specific tag data access methods - these only remain for legacy support
97         // returns whether specific tag could be retrieved
98         bool GetEditDistance(uint32_t& editDistance) const; // get "NM" tag data (equivalent to GetTag("NM", editDistance))
99         bool GetReadGroup(std::string& readGroup) const;    // get "RG" tag data (equivalent to GetTag("RG", readGroup)) 
100         
101         // generic tag data access methods 
102         // returns whether tag is found & tag type is compatible with DESTINATION
103         // @tag - two character tag name
104         // @destination - if found, tag value is stored here
105         bool GetTag(const std::string& tag, std::string& destination) const;    // access variable-length char or hex strings 
106         bool GetTag(const std::string& tag, uint32_t& destination) const;       // access unsigned integer data
107         bool GetTag(const std::string& tag, int32_t& destination) const;        // access signed integer data
108         bool GetTag(const std::string& tag, float& destination) const;          // access floating point data
109         
110         // retrieve the tag type code for TAG
111         // returns true if tag could be found and type determined
112         bool GetTagType(const std::string& tag, char& type) const;
113         
114         // remove tag data
115         // returns true if removal was successful, false if error
116         // N.B. - returns false if TAG does not exist (no removal can occur)
117         // @tag - two character tag name
118         bool RemoveTag(const std::string& tag);
119
120     // Additional data access methods
121     public:
122         // calculates & returns alignment end position, based on starting position and CIGAR operations
123         // @usePadded - if true, counts inserted bases. Default is false, so that alignment end position matches the last base's position in reference
124         // @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.
125         int GetEndPosition(bool usePadded = false, bool zeroBased = true) const;  
126
127     // 'internal' utility methods 
128     private:
129         static bool FindTag(const std::string& tag, char* &pTagData, const unsigned int& tagDataLength, unsigned int& numBytesParsed);
130         static bool SkipToNextTag(const char storageType, char* &pTagData, unsigned int& numBytesParsed);
131
132     // Data members
133     public:
134         std::string Name;              // Read name
135         int32_t     Length;            // Query length
136         std::string QueryBases;        // 'Original' sequence (as reported from sequencing machine)
137         std::string AlignedBases;      // 'Aligned' sequence (includes any indels, padding, clipping)
138         std::string Qualities;         // FASTQ qualities (ASCII characters, not numeric values)
139         std::string TagData;           // Tag data (accessor methods will pull the requested information out)
140         int32_t     RefID;             // ID number for reference sequence
141         int32_t     Position;          // Position (0-based) where alignment starts
142         uint16_t    Bin;               // Bin in BAM file where this alignment resides
143         uint16_t    MapQuality;        // Mapping quality score
144         uint32_t    AlignmentFlag;     // Alignment bit-flag - see Is<something>() methods to query this value, SetIs<something>() methods to manipulate 
145         std::vector<CigarOp> CigarData; // CIGAR operations for this alignment
146         int32_t     MateRefID;         // ID number for reference sequence where alignment's mate was aligned
147         int32_t     MatePosition;      // Position (0-based) where alignment's mate starts
148         int32_t     InsertSize;        // Mate-pair insert size
149           
150     // Internal data, inaccessible to client code
151     // but available BamReaderPrivate & BamWriterPrivate
152     private:
153         struct BamAlignmentSupportData {
154       
155             // data members
156             std::string AllCharData;
157             uint32_t    BlockLength;
158             uint32_t    NumCigarOperations;
159             uint32_t    QueryNameLength;
160             uint32_t    QuerySequenceLength;
161             bool        HasCoreOnly;
162             
163             // constructor
164             BamAlignmentSupportData(void)
165                 : BlockLength(0)
166                 , NumCigarOperations(0)
167                 , QueryNameLength(0)
168                 , QuerySequenceLength(0)
169                 , HasCoreOnly(false)
170             { }
171         };
172         BamAlignmentSupportData SupportData;
173         friend class Internal::BamReaderPrivate;
174         friend class Internal::BamWriterPrivate;
175         
176     // Alignment flag query constants
177     // Use the get/set methods above instead
178     private:
179         enum { PAIRED        = 1
180              , PROPER_PAIR   = 2
181              , UNMAPPED      = 4
182              , MATE_UNMAPPED = 8
183              , REVERSE       = 16
184              , MATE_REVERSE  = 32
185              , READ_1        = 64
186              , READ_2        = 128
187              , SECONDARY     = 256
188              , QC_FAILED     = 512
189              , DUPLICATE     = 1024 
190              };
191 };
192
193 // convenience typedef(s)
194 typedef std::vector<BamAlignment> BamAlignmentVector;
195
196 } // namespace BamTools
197
198 #endif // BAMALIGNMENT_H