Debian Codegraph @

include/abpoa.h

raw
1 #ifndef ABPOA_H
2 #define ABPOA_H
3
4 #include <stdint.h>
5
6 #define ABPOA_GLOBAL_MODE 0
7 #define ABPOA_LOCAL_MODE 1
8 #define ABPOA_EXTEND_MODE 2
9 //#define ABPOA_SEMI_MODE 3
10
11 // gap mode
12 #define ABPOA_LINEAR_GAP 0
13 #define ABPOA_AFFINE_GAP 1
14 #define ABPOA_CONVEX_GAP 2
15
16 #define ABPOA_EXTRA_B 10
17 #define ABPOA_EXTRA_F 0.01
18
19 #define ABPOA_CIGAR_STR "MIDXSH"
20 #define ABPOA_CMATCH 0
21 #define ABPOA_CINS 1
22 #define ABPOA_CDEL 2
23 #define ABPOA_CDIFF 3
24 #define ABPOA_CSOFT_CLIP 4
25 #define ABPOA_CHARD_CLIP 5
26
27 #define ABPOA_SRC_NODE_ID 0
28 #define ABPOA_SINK_NODE_ID 1
29
30 #define ABPOA_OUT_CONS 0
31 #define ABPOA_OUT_MSA 1
32 #define ABPOA_OUT_CONS_MSA 2
33 #define ABPOA_OUT_GFA 3
34 #define ABPOA_OUT_CONS_GFA 4
35 #define ABPOA_OUT_CONS_FQ 5
36
37 #define ABPOA_HB 0
38 #define ABPOA_MF 1
39
40 #define ABPOA_NONE_VERBOSE 0
41 #define ABPOA_INFO_VERBOSE 1
42 #define ABPOA_DEBUG_VERBOSE 2
43 #define ABPOA_LONG_DEBUG_VERBOSE 3
44
45 // NOTE: upper boundary of in_edge_n is pow(2,30)
46 // for MATCH/MISMATCH: node_id << 34 | query_id << 4 | op
47 // for INSERTION: query_id << 34 | op_len << 4 | op
48 // for DELETION: node_id << 34 | op_len << 4 | op // op_len is always equal to 1
49 // for CLIP query_id << 34 | op_len << 4 | op
50 #define abpoa_cigar_t uint64_t
51
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55
56
57 typedef struct {
58 int n_cigar, m_cigar; abpoa_cigar_t *graph_cigar;
59 int node_s, node_e, query_s, query_e; // for local and extension mode
60 int n_aln_bases, n_matched_bases;
61 int32_t best_score;
62 // uint8_t is_rc:1; // is_rc: best_score is from the reverse complement
63 // now is_rc is determined based on minimizer-based seeding and chaining
64 } abpoa_res_t;
65
66 typedef struct {
67 int m; int *mat; char *mat_fn; // score matrix
68 int use_score_matrix; // set _mat_ based on score matrix file, then _match_/_mismatch_ is not used.
69 int match, max_mat, mismatch, min_mis, gap_open1, gap_open2, gap_ext1, gap_ext2; int inf_min;
70 int sort_input_seq; // sort input sequences by length, in descending order
71 int inc_path_score; // set mismatch/match score based on node weight, i.e., # covered reads * match/mismatch
72 // minimizer seeding parameter
73 int k, w, min_w;
74 int wb; float wf; // extra band width
75 int zdrop, end_bonus; // from minimap2
76 // int simd_flag; // available SIMD instruction
77 // alignment mode
78 uint8_t ret_cigar:1, rev_cigar:1, out_msa:1, out_cons:1, out_gfa:1, out_fq:1, use_read_ids:1, amb_strand:1;
79 // sub_aln: reads align to subgraph, total read count is based on subgraph coverage, i.e., node.n_span_read, not total input read count
80 uint8_t sub_aln:1, use_qv:1, disable_seeding:1, progressive_poa:1, put_gap_on_right:1, put_gap_at_end:1; // put indel on the left-most side of the alignment: minimap2-like, or right-most: wfa2-like
81 char *incr_fn, *out_pog;
82 int align_mode, gap_mode, max_n_cons, cons_algrm; // consensus calling algorithm: 0: partial order graph, 1: majority voting
83 double min_freq; // for multiploid data
84 int verbose; // to control output msg
85 int batch_index; // index of current file in batch mode (for output header)
86
87 // char LogTable65536[65536];
88 // char bit_table16[65536];
89 } abpoa_para_t;
90
91 typedef struct {
92 int node_id;
93 int in_edge_n, in_edge_m, *in_id; int *in_edge_weight; // in_edge_weight: for additional path score
94 int out_edge_n, out_edge_m, *out_id; int *out_edge_weight; // out_edge_weight: edge-wise weight
95 // read_weight: read-wise weight, valid when use_qv=1
96 // n_read: # of reads covering this node
97 // n_span_read: # of reads spanning this node, including reads skipping this node
98 int *read_weight, n_read, m_read, n_span_read;
99 uint64_t **read_ids; int read_ids_n; // for each edge
100
101 int aligned_node_n, aligned_node_m, *aligned_node_id; // mismatch; aligned node will have same rank
102 // int heaviest_weight, heaviest_out_id; // for consensus
103 uint8_t base; // 0~m
104 // ID, pos ???
105 } abpoa_node_t;
106
107 typedef struct {
108 abpoa_node_t *node; int node_n, node_m, index_rank_m;
109 int *index_to_node_id;
110 int *node_id_to_index, *node_id_to_max_pos_left, *node_id_to_max_pos_right, *node_id_to_max_remain, *node_id_to_msa_rank;
111 uint8_t is_topological_sorted:1, is_called_cons:1, is_set_msa_rank:1;
112 } abpoa_graph_t;
113
114 typedef struct {
115 int n_cons, n_seq, msa_len; // # cons, # of total seq, length of row-column MSA (including gaps)
116 int *clu_n_seq; // # of reads in each read cluster/group, size: n_cons
117 int **clu_read_ids; // read ids for each cluster/group, size: n_cons * clu_n_seq[i]
118 int *cons_len; // length of each consensus sequence, size: n_cons
119 int **cons_node_ids; // node id of each consensus, size: n_cons * cons_len[i]
120 uint8_t **cons_base; // sequence base of each consensus, size: n_cons * cons_len[i]
121 uint8_t **msa_base; // sequence base of RC-MSA, size: (n_seq + n_cons) * msa_len
122 int **cons_cov; // coverage of each consensus base, size: n_cons * cons_len[i]
123 int **cons_phred_score; // phred score for each consensus base, size: n_cons * cons_len[i]
124 } abpoa_cons_t;
125
126 typedef struct {
127 int l, m; char *s;
128 } abpoa_str_t;
129
130 typedef struct {
131 int n_seq, m_seq;
132 abpoa_str_t *seq, *name, *comment, *qual;
133 uint8_t *is_rc;
134 } abpoa_seq_t;
135
136 typedef struct abpoa_simd_matrix_t abpoa_simd_matrix_t;
137
138 typedef struct {
139 abpoa_graph_t *abg;
140 abpoa_seq_t *abs;
141 abpoa_simd_matrix_t *abm;
142 abpoa_cons_t *abc;
143 } abpoa_t;
144
145 // init for abpoa parameters
146 abpoa_para_t *abpoa_init_para(void);
147 void abpoa_set_mat_from_file(abpoa_para_t *abpt, char *mat_fn);
148 void abpoa_post_set_para(abpoa_para_t *abpt);
149 void abpoa_free_para(abpoa_para_t *abpt);
150
151 // init for alignment
152 abpoa_t *abpoa_init(void);
153 void abpoa_free(abpoa_t *ab);
154
155 // perform msa
156 int abpoa_msa(abpoa_t *ab, abpoa_para_t *abpt, int n_seqs, char **seq_names, int *seq_lens, uint8_t **seqs, int **qual_weights, FILE *out_fp);
157 void abpoa_clean_msa_cons(abpoa_t *ab); // clean MSA & consensus-related memory to avoid leak, called before incremental MSA
158
159 int abpoa_msa1(abpoa_t *ab, abpoa_para_t *abpt, char *read_fn, FILE *out_fp);
160
161 // clean alignment graph
162 void abpoa_reset(abpoa_t *ab, abpoa_para_t *abpt, int qlen);
163
164 // restore graph from GFA/FASTA file
165 abpoa_t *abpoa_restore_graph(abpoa_t *ab, abpoa_para_t *abpt);
166
167 // for development:
168 // align a sequence to a graph
169 int abpoa_align_sequence_to_graph(abpoa_t *ab, abpoa_para_t *abpt, uint8_t *query, int qlen, abpoa_res_t *res);
170 // align a sequence to a graph between beg_node_id and end_node_id (both are excluded)
171 void abpoa_subgraph_nodes(abpoa_t *ab, abpoa_para_t *abpt, int inc_beg, int inc_end, int *exc_beg, int *exc_end);
172 int abpoa_align_sequence_to_subgraph(abpoa_t *ab, abpoa_para_t *abpt, int beg_node_id, int end_node_id, uint8_t *query, int qlen, abpoa_res_t *res);
173
174 // add a node to a graph
175 // para:
176 // base: 0123 for ACGT
177 int abpoa_add_graph_node(abpoa_graph_t *abg, uint8_t base);
178
179 // add an edge to a graph
180 // para:
181 // from_id/to_id: ids of from and to nodes
182 // check_edge: set as 1 if this edge maybe alread exist and only need to update weight, set as 0 if the edge is new
183 // add_read_id: set as 1 if read_id is used (to use row-column algorithm/generate MSA result/multiple consensus)
184 // read_id: is of sequence
185 // read_ids_n: size of read_id array, each one is 64-bit (1+(tot_read_n-1)/64)
186 int abpoa_add_graph_edge(abpoa_graph_t *abg, int from_id, int to_id, int check_edge, int w, uint8_t add_read_id, uint8_t add_read_weight, int read_id, int read_ids_n, int tot_read_n);
187
188 // add an alignment to a graph
189 // para:
190 // query: 0123 for ACGT
191 // qlen: query length
192 // n_cigar/abpoa_cigar: from alignment result (abpoa_res_t)
193 // read_id: id of sequence
194 // tot_read_n: total number of sequence
195 int abpoa_add_graph_alignment(abpoa_t *ab, abpoa_para_t *abpt, uint8_t *query, int *weight, int qlen, int *qpos_to_node_id, abpoa_res_t res, int read_id, int tot_read_n, int inc_both_ends);
196 int abpoa_add_subgraph_alignment(abpoa_t *ab, abpoa_para_t *abpt, int beg_node_id, int end_node_id, uint8_t *query, int *weight, int qlen, int *qpos_to_node_id, abpoa_res_t res, int read_id, int tot_read_n, int inc_both_ends);
197
198 void abpoa_BFS_set_node_index(abpoa_graph_t *abg, int src_id, int sink_id);
199 void abpoa_BFS_set_node_remain(abpoa_graph_t *abg, int src_id, int sink_id);
200
201 // topological sortting of graph
202 void abpoa_topological_sort(abpoa_graph_t *abg, abpoa_para_t *abpt);
203
204 // generate consensus sequence from graph
205 // para:
206 // out_fp: consensus sequence output in FASTA format, set as NULL to disable
207 // cons_seq, cons_l, cons_n: store consensus sequences in variables, set cons_n as NULL to disable.
208 // cons_seq: store consensus sequences
209 // cons_l: store consensus sequences length
210 // cons_n: store number of consensus sequences
211 // Note: cons_seq and cons_l need to be freed by user.
212 void abpoa_generate_consensus(abpoa_t *ab, abpoa_para_t *abpt);
213 void abpoa_output_fx_consensus(abpoa_t *ab, abpoa_para_t *abpt, FILE *out_fp);
214
215 // generate column multiple sequence alignment from graph
216 void abpoa_generate_rc_msa(abpoa_t *ab, abpoa_para_t *abpt);
217 void abpoa_output_rc_msa(abpoa_t *ab, abpoa_para_t *abpt, FILE *out_fp);
218
219 // generate graph in GFA format to _out_fp_
220 void abpoa_generate_gfa(abpoa_t *ab, abpoa_para_t *abpt, FILE *out_fp);
221
222 // output cons/msa
223 void abpoa_output(abpoa_t *ab, abpoa_para_t *abpt, FILE *out_fp);
224
225 // generate DOT graph plot and dump graph into PDF/PNG format file
226 void abpoa_dump_pog(abpoa_t *ab, abpoa_para_t *abpt);
227
228 #ifdef __cplusplus
229 }
230 #endif
231
232 #endif