Debian Codegraph @

example.c

raw
1 /* example.c libabpoa usage example
2 To compile:
3 gcc -g example.c -I ./include -L ./lib -labpoa -lz -lm -o example
4 or:
5 gcc -g example.c -I ./include ./lib/libabpoa.a -lz -lm -o example
6 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdint.h>
11 #include "include/abpoa.h"
12
13 // for nt
14 // AaCcGgTtNn ==> 0,1,2,3,4
15 unsigned char nt4_table[256] = {
16 0, 1, 2, 3, 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, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 /*'-'*/, 4, 4,
19 4, 4, 4, 4, 4, 4, 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, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
23 4, 4, 4, 4, 3, 3, 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 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
31 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
32 };
33
34 // 65,97=>A, 67,99=>C, 71,103=>G, 84,85,116,117=>T, else=>N
35 const char nt256_table[256] = {
36 'A', 'C', 'G', 'T', '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',
38 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
39 'N', 'N', 'N', 'N', 'N', 'N', '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', 'A', 'N', 'C', 'N', 'N', 'N', 'G', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
43 'N', 'N', 'N', 'N', 'T', 'T', '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 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N',
51 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'
52 };
53
54 int main(void) {
55 int i, j, n_seqs = 10;
56 // char seqs[10][100] = {
57 // "CGTCAATCTATCGAAGCATACGCGGGCAGAGCCGAAGACCTCGGCAATCCA",
58 // "CCACGTCAATCTATCGAAGCATACGCGGCAGCCGAACTCGACCTCGGCAATCAC",
59 // "CGTCAATCTATCGAAGCATACGCGGCAGAGCCCGGAAGACCTCGGCAATCAC",
60 // "CGTCAATGCTAGTCGAAGCAGCTGCGGCAGAGCCGAAGACCTCGGCAATCAC",
61 // "CGTCAATCTATCGAAGCATTCTACGCGGCAGAGCCGACCTCGGCAATCAC",
62 // "CGTCAATCTAGAAGCATACGCGGCAAGAGCCGAAGACCTCGGCCAATCAC",
63 // "CGTCAATCTATCGGTAAAGCATACGCTCTGTAGCCGAAGACCTCGGCAATCAC",
64 // "CGTCAATCTATCTTCAAGCATACGCGGCAGAGCCGAAGACCTCGGCAATC",
65 // "CGTCAATGGATCGAGTACGCGGCAGAGCCGAAGACCTCGGCAATCAC",
66 // "CGTCAATCTAATCGAAGCATACGCGGCAGAGCCGTCTACCTCGGCAATCACGT"
67 // };
68
69 char seqs[10][100] = {
70 "CGATCGATCGATCGATGCATGCATCGATGCATCGATCGATGCATGCAT",
71 "CGATCGATCGATAAAAAAAAAAAAAAAAAAACGATGCATGCATCGATGCATCGATCGATGCATGCAT",
72 "CGATCGATCGATCGATGCATGCATCGATGCATCGATCGATGCATGCAT",
73 "CGATCGATCGATCGATGCATGCATCGATGCATCGATCGATGCATGCAT",
74 "CGATCGATCGATAAAAAAAAAAAAAAAAAAACGATGCATGCATCGATGCATCGATCGATGCATGCAT",
75 "CGATCGATCGATAAAAAAAAAAAAAAAAAAACGATGCATGCATCGATGCATCGATCGATGCATGCAT",
76 "CGATCGATCGATAAAAAAAAAAAAAAAAAAACGATGCATGCATCGATGCATCGATCGATGCATGCAT",
77 "CGATCGATCGATCGATGCATGCATCGATGCATCGATCGATGCATGCAT",
78 "CGATCGATCGATCGATGCATGCATCGATGCATCGATCGATGCATGCAT",
79 "CGATCGATCGATCGATGCATGCATCGATGCATCGATCGATGCATGCAT"
80 };
81
82 // initialize variables
83 abpoa_t *ab = abpoa_init();
84 abpoa_para_t *abpt = abpoa_init_para();
85
86 // alignment parameters
87 // abpt->align_mode = 0; // 0:global 1:local, 2:extension
88 // abpt->mat_fn = strdup("HOXD70.mtx"); abpt->use_score_matrix = 1; // score matrix instead of constant match/mismatch score
89 // abpt->match = 2; // match score
90 // abpt->mismatch = 4; // mismatch penalty
91 // abpt->gap_mode = ABPOA_CONVEX_GAP; // gap penalty mode
92 // abpt->gap_open1 = 4; // gap open penalty #1
93 // abpt->gap_ext1 = 2; // gap extension penalty #1
94 // abpt->gap_open2 = 24; // gap open penalty #2
95 // abpt->gap_ext2 = 1; // gap extension penalty #2
96 // gap_penalty = min{gap_open1 + gap_len * gap_ext1, gap_open2 + gap_len * gap_ext2}
97 // abpt->bw = 10; // extra band used in adaptive banded DP
98 // abpt->bf = 0.01;
99
100 // output options
101 abpt->out_msa = 1; // generate Row-Column multiple sequence alignment(RC-MSA), set 0 to disable
102 abpt->out_cons = 1; // generate consensus sequence, set 0 to disable
103 abpt->w = 6, abpt->k = 9; abpt->min_w = 10; // minimizer-based seeding and partition
104 abpt->progressive_poa = 0;
105 abpt->max_n_cons = 2; // to generate 2 consensus sequences
106 // abpt->sub_aln = 1;
107
108 abpoa_post_set_para(abpt);
109
110 // collect sequence length, trasform ACGT to 0123
111 int *seq_lens = (int*)malloc(sizeof(int) * n_seqs);
112 uint8_t **bseqs = (uint8_t**)malloc(sizeof(uint8_t*) * n_seqs);
113 int **weights = (int**)malloc(sizeof(int*) * n_seqs);
114 for (i = 0; i < n_seqs; ++i) {
115 seq_lens[i] = strlen(seqs[i]);
116 bseqs[i] = (uint8_t*)malloc(sizeof(uint8_t) * seq_lens[i]);
117 weights[i] = (int*)malloc(sizeof(int) * seq_lens[i]);
118 for (j = 0; j < seq_lens[i]; ++j) {
119 bseqs[i][j] = nt4_table[(int)seqs[i][j]];
120 if (j >= 12) weights[i][j] = 2;
121 else weights[i][j] = 0;
122 }
123 }
124
125 // 1. directly output to stdout
126 fprintf(stdout, "=== output to stdout ===\n");
127 abpt->use_qv = 1;
128 // perform abpoa-msa
129 // set weights as NULL if no quality score weights are used
130 abpoa_msa(ab, abpt, n_seqs, NULL, seq_lens, bseqs, weights, stdout);
131
132 // 2. output MSA alignment and consensus sequence stored in (abpoa_cons_t *)
133 abpoa_cons_t *abc = ab->abc;
134 fprintf(stdout, "=== stored in variables ===\n");
135 fprintf(stdout, ">Multiple_sequence_alignment\n");
136 for (i = 0; i < abc->n_seq; ++i) {
137 for (j = 0; j < abc->msa_len; ++j) {
138 fprintf(stdout, "%c", nt256_table[abc->msa_base[i][j]]);
139 }
140 fprintf(stdout, "\n");
141 }
142
143 for (i = 0; i < abc->n_cons; ++i) {
144 fprintf(stdout, ">Consensus_sequence");
145 if (abc->n_cons > 1) {
146 fprintf(stdout, "_%d ", i+1);
147 for (j = 0; j < abc->clu_n_seq[i]; ++j) { // output read ids for each cluster/group
148 fprintf(stdout, "%d", abc->clu_read_ids[i][j]);
149 if (j != abc->clu_n_seq[i]-1) fprintf(stdout, ",");
150 }
151 }
152 fprintf(stdout, "\n");
153 for (j = 0; j < abc->cons_len[i]; ++j)
154 fprintf(stdout, "%c", nt256_table[abc->cons_base[i][j]]);
155 fprintf(stdout, "\n");
156 }
157
158 /* generate DOT partial order graph plot */
159 abpt->out_pog = strdup("example.png"); // dump parital order graph to file
160 if (abpt->out_pog != NULL) abpoa_dump_pog(ab, abpt);
161
162 // free seq-related variables
163 for (i = 0; i < n_seqs; ++i) { free(bseqs[i]); free(weights[i]); }
164 free(bseqs); free(seq_lens); free(weights);
165
166 // free abpoa-related variables
167 abpoa_free(ab); abpoa_free_para(abpt);
168 return 0;
169 }