Debian Codegraph @

incre_example.c

raw
1 /* incre_example.c: incremental MSA example using abPOA library
2 To compile:
3 gcc -g incre_example.c -I ./include -L ./lib -labpoa -lz -lm -o incre_example
4 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdint.h>
9 #include "include/abpoa.h"
10
11 // for nt
12 // AaCcGgTtNn ==> 0,1,2,3,4
13 unsigned char nt4_table[256] = {
14 0, 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
15 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
16 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 /*'-'*/, 4, 4,
17 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
18 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
19 4, 4, 4, 4, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
20 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
21 4, 4, 4, 4, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
22 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
23 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
24 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
25 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
26 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
27 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
28 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
29 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
30 };
31
32 // 65,97=>A, 67,99=>C, 71,103=>G, 84,85,116,117=>T, else=>N
33 const char nt256_table[256] = {
34 'A', 'C', 'G', 'T', 'N', '-', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
35 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', '-', 'N', 'N', 'N', 'N',
36 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
37 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
38 'N', 'A', 'N', 'C', 'N', 'N', 'N', 'G', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
39 'N', 'N', 'N', 'N', 'T', 'T', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
40 'N', 'A', 'N', 'C', 'N', 'N', 'N', 'G', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
41 'N', 'N', 'N', 'N', 'T', 'T', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
42 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
43 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
44 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
45 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
46 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
47 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
48 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
49 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'
50 };
51
52 int main(void) {
53 int i, j, n_seqs = 10, first_part = 5, second_part=3, third_part=2; // first_part: number of sequences in the first incremental MSA
54 // append the remaining sequences later
55 // char seqs[10][100] = {
56 // "CGTCAATCTATCGAAGCATACGCGGGCAGAGCCGAAGACCTCGGCAATCCA",
57 // "CCACGTCAATCTATCGAAGCATACGCGGCAGCCGAACTCGACCTCGGCAATCAC",
58 // "CGTCAATCTATCGAAGCATACGCGGCAGAGCCCGGAAGACCTCGGCAATCAC",
59 // "CGTCAATGCTAGTCGAAGCAGCTGCGGCAGAGCCGAAGACCTCGGCAATCAC",
60 // "CGTCAATCTATCGAAGCATTCTACGCGGCAGAGCCGACCTCGGCAATCAC",
61 // "CGTCAATCTAGAAGCATACGCGGCAAGAGCCGAAGACCTCGGCCAATCAC",
62 // "CGTCAATCTATCGGTAAAGCATACGCTCTGTAGCCGAAGACCTCGGCAATCAC",
63 // "CGTCAATCTATCTTCAAGCATACGCGGCAGAGCCGAAGACCTCGGCAATC",
64 // "CGTCAATGGATCGAGTACGCGGCAGAGCCGAAGACCTCGGCAATCAC",
65 // "CGTCAATCTAATCGAAGCATACGCGGCAGAGCCGTCTACCTCGGCAATCACGT"
66 // };
67
68 char seqs[10][100] = {
69 "CGATCGATCGATCGATGCATGCATCGATGCATCGATCGATGCATGCAT",
70 "CGATCGATCGATAAAAAAAAAAAAAAAAAAACGATGCATGCATCGATGCATCGATCGATGCATGCAT",
71 "CGATCGATCGATCGATGCATGCATCGATGCATCGATCGATGCATGCAT",
72 "CGATCGATCGATCGATGCATGCATCGATGCATCGATCGATGCATGCAT",
73 "CGATCGATCGATAAAAAAAAAAAAAAAAAAACGATGCATGCATCGATGCATCGATCGATGCATGCAT",
74 "CGATCGATCGATAAAAAAAAAAAAAAAAAAACGATGCATGCATCGATGCATCGATCGATGCATGCAT",
75 "CGATCGATCGATAAAAAAAAAAAAAAAAAAACGATGCATGCATCGATGCATCGATCGATGCATGCAT",
76 "CGATCGATCGATCGATGCATGCATCGATGCATCGATCGATGCATGCAT",
77 "CGATCGATCGATCGATGCATGCATCGATGCATCGATCGATGCATGCAT",
78 "CGATCGATCGATCGATGCATGCATCGATGCATCGATCGATGCATGCAT"
79 };
80
81 // initialize variables
82 abpoa_t *ab = abpoa_init();
83 abpoa_para_t *abpt = abpoa_init_para();
84
85 // alignment parameters
86 // abpt->align_mode = 0; // 0:global 1:local, 2:extension
87 // abpt->mat_fn = strdup("HOXD70.mtx"); abpt->use_score_matrix = 1; // score matrix instead of constant match/mismatch score
88 // abpt->match = 2; // match score
89 // abpt->mismatch = 4; // mismatch penalty
90 // abpt->gap_mode = ABPOA_CONVEX_GAP; // gap penalty mode
91 // abpt->gap_open1 = 4; // gap open penalty #1
92 // abpt->gap_ext1 = 2; // gap extension penalty #1
93 // abpt->gap_open2 = 24; // gap open penalty #2
94 // abpt->gap_ext2 = 1; // gap extension penalty #2
95 // gap_penalty = min{gap_open1 + gap_len * gap_ext1, gap_open2 + gap_len * gap_ext2}
96 // abpt->bw = 10; // extra band used in adaptive banded DP
97 // abpt->bf = 0.01;
98
99 // output options
100 abpt->out_msa = 1; // generate Row-Column multiple sequence alignment(RC-MSA), set 0 to disable
101 abpt->out_cons = 1; // generate consensus sequence, set 0 to disable
102 abpt->max_n_cons = 2; // to generate 2 consensus sequences
103
104 abpoa_post_set_para(abpt);
105
106 // collect sequence length, trasform ACGT to 0123
107 int *seq_lens = (int*)malloc(sizeof(int) * n_seqs);
108 uint8_t **bseqs = (uint8_t**)malloc(sizeof(uint8_t*) * n_seqs);
109 for (i = 0; i < n_seqs; ++i) {
110 seq_lens[i] = strlen(seqs[i]);
111 bseqs[i] = (uint8_t*)malloc(sizeof(uint8_t) * seq_lens[i]);
112 for (j = 0; j < seq_lens[i]; ++j) {
113 bseqs[i][j] = nt4_table[(int)seqs[i][j]];
114 }
115 }
116 // abpt->verbose = ABPOA_DEBUG_VERBOSE;
117 // 1. align the first part of sequences
118 abpoa_msa(ab, abpt, first_part, NULL, seq_lens, bseqs, NULL, stdout);
119 // 2. clean MSA & consensus-related memory to avoid leak, always call before incremental MSA
120 abpoa_clean_msa_cons(ab);
121
122 // 3. append the second part of sequences one by one
123 printf("\nExisting graph has %d sequences, %d nodes. Now append the remaining %d sequences one by one.\n\n", ab->abs->n_seq, ab->abg->node_n, second_part);
124 abpt->out_msa = 0, abpt->out_cons = 0; // disable output for intermediate append
125 for (i = 0; i < second_part; ++i) {
126 abpoa_msa(ab, abpt, 1, NULL, seq_lens + first_part + i, bseqs + first_part + i, NULL, stdout);
127 }
128 printf("\nAfter appending the second part, existing graph has %d sequences, %d nodes. Now append the remaining %d sequences together.\n\n", ab->abs->n_seq, ab->abg->node_n, n_seqs - first_part - second_part);
129 // 4. append the third part of sequences together
130 abpt->out_msa = 1, abpt->out_cons = 1;
131 abpoa_msa(ab, abpt, third_part, NULL, seq_lens + first_part + second_part, bseqs + first_part + second_part, NULL, stdout);
132
133 printf("\nFinal graph has %d sequences, %d nodes.\n", ab->abs->n_seq, ab->abg->node_n);
134
135 // free seq-related variables
136 for (i = 0; i < n_seqs; ++i) { free(bseqs[i]); }
137 free(bseqs); free(seq_lens);
138
139 // free abpoa-related variables
140 abpoa_free(ab); abpoa_free_para(abpt);
141 return 0;
142 }