Imported Debian patch 0.12.7-3
[bowtie.git] / sam.h
1 /*
2  * sam.h
3  *
4  *  Created on: Sep 23, 2009
5  *      Author: Ben Langmead
6  */
7
8 #ifndef SAM_H_
9 #define SAM_H_
10
11 #include "refmap.h"
12 #include "annot.h"
13 #include "pat.h"
14 #include "random_source.h"
15
16 class ReferenceMap;
17 class AnnotationMap;
18 class PatternSourcePerThread;
19
20 enum {
21         SAM_FLAG_PAIRED = 1,
22         SAM_FLAG_MAPPED_PAIRED = 2,
23         SAM_FLAG_UNMAPPED = 4,
24         SAM_FLAG_MATE_UNMAPPED = 8,
25         SAM_FLAG_QUERY_STRAND = 16,
26         SAM_FLAG_MATE_STRAND = 32,
27         SAM_FLAG_FIRST_IN_PAIR = 64,
28         SAM_FLAG_SECOND_IN_PAIR = 128,
29         SAM_FLAG_NOT_PRIMARY = 256,
30         SAM_FLAG_FAILS_CHECKS = 512,
31         SAM_FLAG_DUPLICATE = 1024
32 };
33
34 /**
35  * Sink that prints lines in SAM format:
36  */
37 class SAMHitSink : public HitSink {
38 public:
39         /**
40          * Construct a single-stream VerboseHitSink (default)
41          */
42         SAMHitSink(OutFileBuf* out,
43                    int offBase,
44                    ReferenceMap *rmap,
45                    AnnotationMap *amap,
46                    bool fullRef,
47                    int defaultMapq,
48                    DECL_HIT_DUMPS2) :
49         HitSink(out, PASS_HIT_DUMPS2),
50         offBase_(offBase), defaultMapq_(defaultMapq),
51         rmap_(rmap), amap_(amap), fullRef_(fullRef) { }
52
53         /**
54          * Construct a multi-stream VerboseHitSink with one stream per
55          * reference string (see --refout)
56          */
57         SAMHitSink(size_t numOuts,
58                    int offBase,
59                    ReferenceMap *rmap,
60                    AnnotationMap *amap,
61                    bool fullRef,
62                    int defaultMapq,
63                    DECL_HIT_DUMPS2) :
64         HitSink(numOuts, PASS_HIT_DUMPS2),
65         offBase_(offBase), defaultMapq_(defaultMapq),
66         rmap_(rmap), amap_(amap), fullRef_(fullRef) { }
67
68         /**
69          * Append a SAM alignment to the given output stream.
70          */
71         static void append(ostream& ss,
72                            const Hit& h,
73                            int mapq,
74                            int xms,
75                            const vector<string>* refnames,
76                            ReferenceMap *rmap,
77                            AnnotationMap *amap,
78                            bool fullRef,
79                            int offBase);
80
81         /**
82          * Append a SAM alignment for an aligned read to the given output
83          * stream.
84          */
85         static void appendAligned(ostream& ss,
86                                   const Hit& h,
87                                   int mapq,
88                                   int xms,
89                                   const vector<string>* refnames,
90                                   ReferenceMap *rmap,
91                                   AnnotationMap *amap,
92                                   bool fullRef,
93                                   int offBase);
94
95         /**
96          * Append a verbose, readable hit to the output stream
97          * corresponding to the hit.
98          */
99         virtual void append(ostream& ss, const Hit& h) {
100                 SAMHitSink::append(ss, h, defaultMapq_, 0, _refnames, rmap_, amap_, fullRef_, offBase_);
101         }
102
103         /**
104          * Append a verbose, readable hit to the output stream
105          * corresponding to the hit.
106          */
107         virtual void append(ostream& ss, const Hit& h, int mapq, int xms) {
108                 SAMHitSink::append(ss, h, mapq, xms, _refnames, rmap_, amap_, fullRef_, offBase_);
109         }
110
111         /**
112          * Write the SAM header lines.
113          */
114         void appendHeaders(OutFileBuf& os,
115                            size_t numRefs,
116                            const vector<string>& refnames,
117                            bool color,
118                            bool nosq,
119                            ReferenceMap *rmap,
120                            const uint32_t* plen,
121                            bool fullRef,
122                            const char *cmdline,
123                            const char *rgline);
124
125 protected:
126
127         /**
128          *
129          */
130         void reportUnOrMax(PatternSourcePerThread& p,
131                            vector<Hit>* hs,
132                            bool un);
133
134         /**
135          * Report a verbose, human-readable alignment to the appropriate
136          * output stream.
137          */
138         virtual void reportHit(const Hit& h) {
139                 reportHit(h, defaultMapq_, 0);
140         }
141
142         /**
143          * Report a SAM alignment with the given mapping quality and XM
144          * field.
145          */
146         virtual void reportHit(const Hit& h, int mapq, int xms);
147
148         /**
149          * Report a batch of SAM alignments (e.g. two mates that should be
150          * printed together) with the given mapping quality and XM field.
151          */
152         virtual void reportHits(vector<Hit>& hs,
153                                 size_t start,
154                                 size_t end,
155                                 int mapq,
156                                 int xms);
157
158         /**
159          * See sam.cpp
160          */
161         virtual void reportMaxed(vector<Hit>& hs, PatternSourcePerThread& p);
162
163         /**
164          * See sam.cpp
165          */
166         virtual void reportUnaligned(PatternSourcePerThread& p) {
167                 reportUnOrMax(p, NULL, true);
168         }
169
170 private:
171         int  offBase_;        /// Add this to reference offsets before outputting.
172                               /// (An easy way to make things 1-based instead of
173                               /// 0-based)
174         int  defaultMapq_;    /// Default mapping quality to report when one is
175                               /// not specified
176         ReferenceMap *rmap_;  /// mapping to reference coordinate system.
177         AnnotationMap *amap_; ///
178         bool fullRef_;        /// print full reference name, not just up to whitespace
179 };
180
181 #endif /* SAM_H_ */