]> git.donarmstrong.com Git - bamtools.git/blob - src/api/BamAlignment.h
Changed BamAlignment::SupportData from private to public.
[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: 9 October 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         // 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;
106         
107         // remove tag data
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);
112
113     // Additional data access methods
114     public:
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;  
119
120     // 'internal' utility methods 
121     private:
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);
124
125     // Data members
126     public:
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
142           
143     public:
144         struct BamAlignmentSupportData {
145       
146             // data members
147             std::string AllCharData;
148             uint32_t    BlockLength;
149             uint32_t    NumCigarOperations;
150             uint32_t    QueryNameLength;
151             uint32_t    QuerySequenceLength;
152             bool        HasCoreOnly;
153             
154             // constructor
155             BamAlignmentSupportData(void)
156                 : BlockLength(0)
157                 , NumCigarOperations(0)
158                 , QueryNameLength(0)
159                 , QuerySequenceLength(0)
160                 , HasCoreOnly(false)
161             { }
162         };
163         
164         // ** THIS IS INTERNAL DATA! DO NOT ACCESS OR EDIT FROM CLIENT CODE **
165         //
166         // Intended for use by BamReader & BamWriter ONLY. No, really, I mean it.
167         //
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.
170         //
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;   
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