Debian Codegraph @

contrib/SeqLib/bwa/bwamem.h

raw
1 #ifndef BWAMEM_H_
2 #define BWAMEM_H_
3
4 #include "bwt.h"
5 #include "bntseq.h"
6 #include "bwa.h"
7
8 #define MEM_MAPQ_COEF 30.0
9 #define MEM_MAPQ_MAX 60
10
11 struct __smem_i;
12 typedef struct __smem_i smem_i;
13
14 #define MEM_F_PE 0x2
15 #define MEM_F_NOPAIRING 0x4
16 #define MEM_F_ALL 0x8
17 #define MEM_F_NO_MULTI 0x10
18 #define MEM_F_NO_RESCUE 0x20
19 #define MEM_F_REF_HDR 0x100
20 #define MEM_F_SOFTCLIP 0x200
21 #define MEM_F_SMARTPE 0x400
22
23 typedef struct {
24 int a, b; // match score and mismatch penalty
25 int o_del, e_del;
26 int o_ins, e_ins;
27 int pen_unpaired; // phred-scaled penalty for unpaired reads
28 int pen_clip5,pen_clip3;// clipping penalty. This score is not deducted from the DP score.
29 int w; // band width
30 int zdrop; // Z-dropoff
31
32 uint64_t max_mem_intv;
33
34 int T; // output score threshold; only affecting output
35 int flag; // see MEM_F_* macros
36 int min_seed_len; // minimum seed length
37 int min_chain_weight;
38 int max_chain_extend;
39 float split_factor; // split into a seed if MEM is longer than min_seed_len*split_factor
40 int split_width; // split into a seed if its occurence is smaller than this value
41 int max_occ; // skip a seed if its occurence is larger than this value
42 int max_chain_gap; // do not chain seed if it is max_chain_gap-bp away from the closest seed
43 int n_threads; // number of threads
44 int chunk_size; // process chunk_size-bp sequences in a batch
45 float mask_level; // regard a hit as redundant if the overlap with another better hit is over mask_level times the min length of the two hits
46 float drop_ratio; // drop a chain if its seed coverage is below drop_ratio times the seed coverage of a better chain overlapping with the small chain
47 float XA_drop_ratio; // when counting hits for the XA tag, ignore alignments with score < XA_drop_ratio * max_score; only effective for the XA tag
48 float mask_level_redun;
49 float mapQ_coef_len;
50 int mapQ_coef_fac;
51 int max_ins; // when estimating insert size distribution, skip pairs with insert longer than this value
52 int max_matesw; // perform maximally max_matesw rounds of mate-SW for each end
53 int max_XA_hits, max_XA_hits_alt; // if there are max_hits or fewer, output them all
54 int8_t mat[25]; // scoring matrix; mat[0] == 0 if unset
55 } mem_opt_t;
56
57 typedef struct {
58 int64_t rb, re; // [rb,re): reference sequence in the alignment
59 int qb, qe; // [qb,qe): query sequence in the alignment
60 int rid; // reference seq ID
61 int score; // best local SW score
62 int truesc; // actual score corresponding to the aligned region; possibly smaller than $score
63 int sub; // 2nd best SW score
64 int alt_sc;
65 int csub; // SW score of a tandem hit
66 int sub_n; // approximate number of suboptimal hits
67 int w; // actual band width used in extension
68 int seedcov; // length of regions coverged by seeds
69 int secondary; // index of the parent hit shadowing the current hit; <0 if primary
70 int secondary_all;
71 int seedlen0; // length of the starting seed
72 int n_comp:30, is_alt:2; // number of sub-alignments chained together
73 float frac_rep;
74 uint64_t hash;
75 } mem_alnreg_t;
76
77 typedef struct { size_t n, m; mem_alnreg_t *a; } mem_alnreg_v;
78
79 typedef struct {
80 int low, high; // lower and upper bounds within which a read pair is considered to be properly paired
81 int failed; // non-zero if the orientation is not supported by sufficient data
82 double avg, std; // mean and stddev of the insert size distribution
83 } mem_pestat_t;
84
85 typedef struct { // This struct is only used for the convenience of API.
86 int64_t pos; // forward strand 5'-end mapping position
87 int rid; // reference sequence index in bntseq_t; <0 for unmapped
88 int flag; // extra flag
89 uint32_t is_rev:1, is_alt:1, mapq:8, NM:22; // is_rev: whether on the reverse strand; mapq: mapping quality; NM: edit distance
90 int n_cigar; // number of CIGAR operations
91 uint32_t *cigar; // CIGAR in the BAM encoding: opLen<<4|op; op to integer mapping: MIDSH=>01234
92 char *XA; // alternative mappings
93
94 int score, sub, alt_sc;
95 } mem_aln_t;
96
97 #ifdef __cplusplus
98 extern "C" {
99 #endif
100
101 smem_i *smem_itr_init(const bwt_t *bwt);
102 void smem_itr_destroy(smem_i *itr);
103 void smem_set_query(smem_i *itr, int len, const uint8_t *query);
104 void smem_config(smem_i *itr, int min_intv, int max_len, uint64_t max_intv);
105 const bwtintv_v *smem_next(smem_i *itr);
106
107 mem_opt_t *mem_opt_init(void);
108 void mem_fill_scmat(int a, int b, int8_t mat[25]);
109
110 /**
111 * Align a batch of sequences and generate the alignments in the SAM format
112 *
113 * This routine requires $seqs[i].{l_seq,seq,name} and write $seqs[i].sam.
114 * Note that $seqs[i].sam may consist of several SAM lines if the
115 * corresponding sequence has multiple primary hits.
116 *
117 * In the paired-end mode (i.e. MEM_F_PE is set in $opt->flag), query
118 * sequences must be interleaved: $n must be an even number and the 2i-th
119 * sequence and the (2i+1)-th sequence constitute a read pair. In this
120 * mode, there should be enough (typically >50) unique pairs for the
121 * routine to infer the orientation and insert size.
122 *
123 * @param opt alignment parameters
124 * @param bwt FM-index of the reference sequence
125 * @param bns Information of the reference
126 * @param pac 2-bit encoded reference
127 * @param n number of query sequences
128 * @param seqs query sequences; $seqs[i].seq/sam to be modified after the call
129 * @param pes0 insert-size info; if NULL, infer from data; if not NULL, it should be an array with 4 elements,
130 * corresponding to each FF, FR, RF and RR orientation. See mem_pestat() for more info.
131 */
132 void mem_process_seqs(const mem_opt_t *opt, const bwt_t *bwt, const bntseq_t *bns, const uint8_t *pac, int64_t n_processed, int n, bseq1_t *seqs, const mem_pestat_t *pes0);
133
134 /**
135 * Find the aligned regions for one query sequence
136 *
137 * Note that this routine does not generate CIGAR. CIGAR should be
138 * generated later by mem_reg2aln() below.
139 *
140 * @param opt alignment parameters
141 * @param bwt FM-index of the reference sequence
142 * @param bns Information of the reference
143 * @param pac 2-bit encoded reference
144 * @param l_seq length of query sequence
145 * @param seq query sequence
146 *
147 * @return list of aligned regions.
148 */
149 mem_alnreg_v mem_align1(const mem_opt_t *opt, const bwt_t *bwt, const bntseq_t *bns, const uint8_t *pac, int l_seq, const char *seq);
150
151 /**
152 * Generate CIGAR and forward-strand position from alignment region
153 *
154 * @param opt alignment parameters
155 * @param bns Information of the reference
156 * @param pac 2-bit encoded reference
157 * @param l_seq length of query sequence
158 * @param seq query sequence
159 * @param ar one alignment region
160 *
161 * @return CIGAR, strand, mapping quality and forward-strand position
162 */
163 mem_aln_t mem_reg2aln(const mem_opt_t *opt, const bntseq_t *bns, const uint8_t *pac, int l_seq, const char *seq, const mem_alnreg_t *ar);
164 mem_aln_t mem_reg2aln2(const mem_opt_t *opt, const bntseq_t *bns, const uint8_t *pac, int l_seq, const char *seq, const mem_alnreg_t *ar, const char *name);
165
166 /**
167 * Infer the insert size distribution from interleaved alignment regions
168 *
169 * This function can be called after mem_align1(), as long as paired-end
170 * reads are properly interleaved.
171 *
172 * @param opt alignment parameters
173 * @param l_pac length of concatenated reference sequence
174 * @param n number of query sequences; must be an even number
175 * @param regs region array of size $n; 2i-th and (2i+1)-th elements constitute a pair
176 * @param pes inferred insert size distribution (output)
177 */
178 void mem_pestat(const mem_opt_t *opt, int64_t l_pac, int n, const mem_alnreg_v *regs, mem_pestat_t pes[4]);
179
180 #ifdef __cplusplus
181 }
182 #endif
183
184 #endif