Debian Codegraph @

src/abpoa.c

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <getopt.h>
5 #include "abpoa.h"
6 #include "abpoa_graph.h"
7 #include "abpoa_align.h"
8 #include "abpoa_seq.h"
9 #include "utils.h"
10
11 char NAME[20] = "abPOA";
12 char PROG[20] = "abpoa";
13 #define _ba BOLD UNDERLINE "a" NONE
14 #define _bb BOLD UNDERLINE "b" NONE
15 #define _bP BOLD UNDERLINE "P" NONE
16 #define _bO BOLD UNDERLINE "O" NONE
17 #define _bA BOLD UNDERLINE "A" NONE
18 char DESCRIPTION[100] = _ba "daptive " _bb "anded " _bP "artial " _bO "rder " _bA "lignment";
19 char VERSION[20] = "1.5.6";
20 char CONTACT[30] = "[email protected]";
21
22 const struct option abpoa_long_opt [] = {
23 { "align-mode", 1, NULL, 'm' },
24
25 { "match", 1, NULL, 'M' },
26 { "mismatch", 1, NULL, 'X' },
27 { "matrix", 1, NULL, 't' },
28 { "gap-open", 1, NULL, 'O' },
29 { "gap-ext", 1, NULL, 'E' },
30
31 { "extra-b", 1, NULL, 'b' },
32 { "extra-f", 1, NULL, 'f' },
33 { "zdrop", 1, NULL, 'z' },
34 { "bonus", 1, NULL, 'e' },
35
36 { "seeding", 0, NULL, 'S'},
37 { "k-mer", 1, NULL, 'k' },
38 { "window", 1, NULL, 'w' },
39 { "min-poa-win", 1, NULL, 'n' },
40 { "progressive", 0, NULL, 'p'},
41
42 { "inc-path-score", 0, NULL, 'G'},
43 { "sort-by-len", 0, NULL, 'L'},
44 { "gap-on-right", 0, NULL, 'R' },
45 { "gap-at-end", 0, NULL, 'J' },
46 { "use-qual-weight", 0, NULL, 'Q'},
47 { "amino-acid", 0, NULL, 'c'},
48 { "in-list", 0, NULL, 'l' },
49 { "increment", 1, NULL, 'i' },
50
51 { "amb-strand", 0, NULL, 's' },
52 { "output", 1, NULL, 'o' },
53 { "result", 1, NULL, 'r' },
54 { "out-pog", 1, NULL, 'g' },
55 { "cons-algrm", 1, NULL, 'a'},
56 { "maxnum-cons", 1, NULL, 'd', },
57 { "min-freq", 1, NULL, 'q', },
58
59 { "help", 0, NULL, 'h' },
60 { "version", 0, NULL, 'v' },
61 { "verbose", 1, NULL, 'V'},
62
63 { 0, 0, 0, 0}
64 };
65
66 int abpoa_usage(void)
67 {
68 err_printf("\n");
69 err_printf("%s: %s \n\n", PROG, DESCRIPTION);
70 err_printf("Version: %s\t", VERSION);
71 err_printf("Contact: %s\n\n", CONTACT);
72 err_printf("Usage: %s [options] <in.fa/fq> > cons.fa/msa.fa/abpoa.gfa\n\n", PROG);
73 err_printf("Options:\n");
74 err_printf(" Alignment:\n");
75 err_printf(" -m --aln-mode INT alignment mode [%d]\n", ABPOA_GLOBAL_MODE);
76 err_printf(" %d: global, %d: local, %d: extension\n", ABPOA_GLOBAL_MODE, ABPOA_LOCAL_MODE, ABPOA_EXTEND_MODE);
77 err_printf(" -M --match INT match score [%d]\n", ABPOA_MATCH);
78 err_printf(" -X --mismatch INT mismatch penalty [%d]\n", ABPOA_MISMATCH);
79 err_printf(" -t --matrix FILE scoring matrix file, \'-M\' and \'-X\' are not used when \'-t\' is used [Null]\n");
80 err_printf(" e.g., \'HOXD70.mtx, BLOSUM62.mtx\'\n");
81 err_printf(" -O --gap-open INT(,INT) gap opening penalty (O1,O2) [%d,%d]\n", ABPOA_GAP_OPEN1, ABPOA_GAP_OPEN2);
82 err_printf(" -E --gap-ext INT(,INT) gap extension penalty (E1,E2) [%d,%d]\n", ABPOA_GAP_EXT1, ABPOA_GAP_EXT2);
83 err_printf(" %s provides three gap penalty modes, cost of a g-long gap:\n", NAME);
84 err_printf(" - convex (default): min{O1+g*E1, O2+g*E2}\n");
85 err_printf(" - affine (set O2 as 0): O1+g*E1\n");
86 err_printf(" - linear (set O1 as 0): g*E1\n");
87 err_printf(" -s --amb-strand ambiguous strand mode [False]\n");
88 err_printf(" for each input sequence, try the reverse complement if the current\n");
89 err_printf(" alignment score is too low, and pick the strand with a higher score\n");
90 err_printf(" Adaptive banded DP:\n");
91 err_printf(" -b --extra-b INT first adaptive banding parameter [%d]\n", ABPOA_EXTRA_B);
92 err_printf(" set b as < 0 to disable adaptive banded DP\n");
93 err_printf(" -f --extra-f FLOAT second adaptive banding parameter [%.2f]\n", ABPOA_EXTRA_F);
94 err_printf(" the number of extra bases added on both sites of the band is\n");
95 err_printf(" b+f*L, where L is the length of the aligned sequence\n");
96 err_printf(" Heuristics for graph alignment: (**under development**)\n");
97 err_printf(" -G --inc-path-score include log-scaled path score for graph alignment [False]\n");
98 err_printf(" -L --sort-by-len sort input sequences by length in descending order [False]\n");
99 err_printf(" -R --gap-on-right put indel on the right-most side of the alignment, default is left [False]\n");
100 err_printf(" -J --gap-at-end always put indel at the end of the alignment if possible [False]\n");
101 // err_printf(" -z --zdrop INT Z-drop score in extension alignment [-1]\n");
102 // err_printf(" set as <= 0 to disable Z-drop extension\n");
103 // err_printf(" -e --bonus INT end bonus score in extension alignment [-1]\n");
104 // err_printf(" set as <= 0 to disable end bounus\n");
105 err_printf(" Minimizer-based seeding and partition (only effective in global alignment mode):\n");
106 err_printf(" -S --seeding enable minimizer-based seeding and anchoring [False]\n");
107 err_printf(" -k --k-mer INT minimizer k-mer size [%d]\n", ABPOA_MMK);
108 err_printf(" -w --window INT minimizer window size [%d]\n", ABPOA_MMW);
109 err_printf(" -n --min-poa-win INT min. size of window to perform POA [%d]\n", ABPOA_MIN_POA_WIN);
110 err_printf(" -p --progressive build guide tree and perform progressive partial order alignment [False]\n");
111 // err_printf(" -n --par-size minimal partition size [%d]\n", ABPOA_W);
112
113 err_printf(" Input/Output:\n");
114 err_printf(" -Q --use-qual-weight take base quality score from FASTQ input file as graph edge weight for consensus calling [False]\n");
115 err_printf(" effective only when input sequences are in FASTQ format and consensus calling with heaviest bundling\n");
116 err_printf(" -c --amino-acid input sequences are amino acid (default is nucleotide) [False]\n");
117 err_printf(" -l --in-list input file is a list of sequence file names [False]\n");
118 err_printf(" each line is one sequence file containing a set of sequences\n");
119 err_printf(" which will be aligned by abPOA to generate a consensus sequence\n");
120 err_printf(" -i --incrmnt FILE incrementally align sequences to an existing graph/MSA [Null]\n");
121 err_printf(" graph could be in GFA or MSA format generated by abPOA\n");
122 err_printf(" -o --output FILE output to FILE [stdout]\n");
123 err_printf(" -r --result INT output result mode [%d]\n", ABPOA_OUT_CONS);
124 err_printf(" - %d: consensus in FASTA format\n", ABPOA_OUT_CONS);
125 err_printf(" - %d: MSA in PIR format\n", ABPOA_OUT_MSA);
126 err_printf(" - %d: both 0 & 1\n", ABPOA_OUT_CONS_MSA);
127 err_printf(" - %d: graph in GFA format\n", ABPOA_OUT_GFA);
128 err_printf(" - %d: graph with consensus path in GFA format\n", ABPOA_OUT_CONS_GFA);
129 err_printf(" - %d: consensus in FASTQ format\n", ABPOA_OUT_CONS_FQ);
130 err_printf(" -a --cons-algrm INT consensus algorithm [%d]\n", ABPOA_HB);
131 err_printf(" - %d: heaviest bundling path in partial order graph\n", ABPOA_HB);
132 err_printf(" - %d: most frequent bases at each position\n", ABPOA_MF);
133 err_printf(" -d --maxnum-cons INT max. number of consensus sequence to generate [1]\n");
134 err_printf(" if >=2, sequences will be clustered based on MSA and consensus will be generated within each cluster\n");
135 err_printf(" up to 10 is supported currently (experimental for >=2)\n");
136 err_printf(" -q --min-freq FLOAT min. frequency of each cluster (only effective when -d/--maxnum-cons >= 2) [%.2f]\n", MULTIP_MIN_FREQ);
137 err_printf(" -g --out-pog FILE dump final alignment graph to FILE (.pdf/.png) [Null]\n\n");
138
139 err_printf(" -h --help print this help usage information\n");
140 err_printf(" -v --version show version number\n");
141 err_printf(" -V --verbose INT verbose level (0-2). 0: none, 1: information, 2: debug [0]\n");
142
143
144 err_printf("\n");
145 return 1;
146 }
147
148 int abpoa_main(char *file_fn, int is_list, abpoa_para_t *abpt){
149 double realtime0 = realtime();
150 // TODO abpoa_init for each input file ???
151 abpoa_t *ab = abpoa_init();
152 if (is_list) { // input file list
153 FILE *list_fp = fopen(file_fn, "r"); char read_fn[1024];
154 int batch_index = 1;
155 while (fgets(read_fn, sizeof(read_fn), list_fp)) {
156 read_fn[strlen(read_fn)-1] = '\0';
157 abpt->batch_index = batch_index;
158 abpoa_msa1(ab, abpt, read_fn, stdout);
159 batch_index++;
160 }
161 fclose(list_fp);
162 } else // input file
163 abpoa_msa1(ab, abpt, file_fn, stdout);
164
165 abpoa_free(ab);
166 err_func_printf(__func__, "Real time: %.3f sec; CPU: %.3f sec; Peak RSS: %.3f GB.", realtime() - realtime0, cputime(), peakrss() / 1024.0 / 1024.0 / 1024.0);
167 return 0;
168 }
169
170 int main(int argc, char **argv) {
171 int c, m, in_list=0; char *s; abpoa_para_t *abpt = abpoa_init_para();
172 while ((c = getopt_long(argc, argv, "m:M:X:t:O:E:b:f:z:e:GLRJQSk:w:n:i:clpso:r:g:a:d:q:hvV:", abpoa_long_opt, NULL)) >= 0) {
173 switch(c)
174 {
175 case 'm': m = atoi(optarg);
176 if (m != ABPOA_GLOBAL_MODE && m != ABPOA_EXTEND_MODE && m != ABPOA_LOCAL_MODE) {
177 err_printf("Unknown alignment mode: %d.\n", m); return 1;
178 } abpt->align_mode=m; break;
179 case 'M': abpt->match = atoi(optarg); break;
180 case 'X': abpt->mismatch = atoi(optarg); break;
181 case 't': abpt->use_score_matrix = 1; abpt->mat_fn = strdup(optarg); break;
182 case 'O': abpt->gap_open1 = strtol(optarg, &s, 10); if (*s == ',') abpt->gap_open2 = strtol(s+1, &s, 10); else abpt->gap_open2 = 0; break;
183 case 'E': abpt->gap_ext1 = strtol(optarg, &s, 10); if (*s == ',') abpt->gap_ext2 = strtol(s+1, &s, 10); else abpt->gap_ext2 = 0; break;
184
185 case 'G': abpt->inc_path_score = 1; break;
186 case 'L': abpt->sort_input_seq = 1; break;
187 case 'R': abpt->put_gap_on_right = 1; break;
188 case 'J': abpt->put_gap_at_end = 1; break;
189 case 'b': abpt->wb = atoi(optarg); break;
190 case 'f': abpt->wf = atof(optarg); break;
191 case 'z': abpt->zdrop = atoi(optarg); break;
192 case 'e': abpt->end_bonus= atoi(optarg); break;
193
194 case 'Q': abpt->use_qv = 1; break;
195 case 'S': abpt->disable_seeding = 0; break;
196 case 'k': abpt->k = atoi(optarg); break;
197 case 'w': abpt->w = atoi(optarg); break;
198 case 'n': abpt->min_w = atoi(optarg); break;
199
200 case 'c': abpt->m = 27; abpt->mat = (int*)_err_realloc(abpt->mat, abpt->m * abpt->m * sizeof(int)); break;
201 case 'i': abpt->incr_fn = strdup(optarg); break;
202 case 'l': in_list = 1; break;
203 case 'p': abpt->progressive_poa = 1; break;
204 case 's': abpt->amb_strand = 1; break;
205 case 'o': if (strcmp(optarg, "-") != 0) {
206 if (freopen(optarg, "wb", stdout) == NULL)
207 err_fatal(__func__, "Failed to open the output file %s", optarg);
208 } break;
209 case 'r': if (atoi(optarg) == ABPOA_OUT_CONS) abpt->out_cons = 1, abpt->out_msa = 0;
210 else if (atoi(optarg) == ABPOA_OUT_MSA) abpt->out_cons = 0, abpt->out_msa = 1;
211 else if (atoi(optarg) == ABPOA_OUT_CONS_MSA) abpt->out_cons = abpt->out_msa = 1;
212 else if (atoi(optarg) == ABPOA_OUT_GFA) abpt->out_cons = 0, abpt->out_gfa = 1;
213 else if (atoi(optarg) == ABPOA_OUT_CONS_GFA) abpt->out_cons = 1, abpt->out_gfa = 1;
214 else if (atoi(optarg) == ABPOA_OUT_CONS_FQ) abpt->out_cons = 1, abpt->out_fq = 1;
215 else err_printf("Error: unknown output result mode: %s.\n", optarg);
216 break;
217 case 'g': abpt->out_pog= strdup(optarg); break;
218
219 case 'a': abpt->cons_algrm = atoi(optarg); break;
220 case 'd': abpt->max_n_cons = atoi(optarg);
221 if (abpt->max_n_cons < 1 || abpt->max_n_cons > 10) {
222 err_printf("Error: max number of consensus sequences should be 1~10.\n");
223 return 1;
224 }
225 break;
226 case 'q': abpt->min_freq = atof(optarg); break;
227
228 case 'h': return abpoa_usage();
229 case 'V': abpt->verbose = atoi(optarg); break;
230 case 'v': printf("%s\n", VERSION); goto End; break;
231 default:
232 err_printf("Error: unknown option: %s.\n", optarg);
233 return abpoa_usage();
234 break;
235 }
236 }
237
238 if (argc - optind != 1) return abpoa_usage();
239
240 abpoa_post_set_para(abpt);
241 fprintf(stderr, "[%s] CMD: ", __func__);
242 for (c = 0; c < argc; ++c)
243 fprintf(stderr, " %s", argv[c]);
244 fprintf(stderr, "\n");
245 abpoa_main(argv[optind], in_list, abpt);
246
247 End:
248 abpoa_free_para(abpt);
249 return 0;
250 }