| 1 |
#include "samtools.pysam.h" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
#include <config.h> |
| 129 |
|
| 130 |
#include <stdio.h> |
| 131 |
#include <stdlib.h> |
| 132 |
#include <strings.h> |
| 133 |
#include <math.h> |
| 134 |
#include <limits.h> |
| 135 |
#include <float.h> |
| 136 |
#include <ctype.h> |
| 137 |
|
| 138 |
#include <htslib/sam.h> |
| 139 |
#include <htslib/hfile.h> |
| 140 |
|
| 141 |
#include "samtools.h" |
| 142 |
#include "sam_opts.h" |
| 143 |
#include "bam_plbuf.h" |
| 144 |
#include "consensus_pileup.h" |
| 145 |
|
| 146 |
#ifdef __SSE__ |
| 147 |
# include <xmmintrin.h> |
| 148 |
#else |
| 149 |
# define _mm_prefetch(a,b) |
| 150 |
#endif |
| 151 |
|
| 152 |
#ifndef MIN |
| 153 |
# define MIN(a,b) ((a)<(b)?(a):(b)) |
| 154 |
#endif |
| 155 |
#ifndef MAX |
| 156 |
# define MAX(a,b) ((a)>(b)?(a):(b)) |
| 157 |
#endif |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
#define MOD_CUTOFF 0.46 |
| 176 |
|
| 177 |
enum format { |
| 178 |
FASTQ, |
| 179 |
FASTA, |
| 180 |
PILEUP |
| 181 |
}; |
| 182 |
|
| 183 |
typedef unsigned char uc; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
typedef struct { |
| 188 |
int smap[101]; |
| 189 |
int umap[101]; |
| 190 |
int omap[101]; |
| 191 |
} qcal_t; |
| 192 |
|
| 193 |
typedef struct { |
| 194 |
|
| 195 |
char *reg; |
| 196 |
int use_qual; |
| 197 |
int min_qual; |
| 198 |
int adj_qual; |
| 199 |
int use_mqual; |
| 200 |
double scale_mqual; |
| 201 |
int nm_adjust; |
| 202 |
int nm_halo; |
| 203 |
int sc_cost; |
| 204 |
int low_mqual; |
| 205 |
int high_mqual; |
| 206 |
int min_depth; |
| 207 |
double call_fract; |
| 208 |
double het_fract; |
| 209 |
int mode; |
| 210 |
enum format fmt; |
| 211 |
int cons_cutoff; |
| 212 |
int ambig; |
| 213 |
int line_len; |
| 214 |
int default_qual; |
| 215 |
int het_only; |
| 216 |
int all_bases; |
| 217 |
int show_del; |
| 218 |
int show_ins; |
| 219 |
int mark_ins; |
| 220 |
int excl_flags; |
| 221 |
int incl_flags; |
| 222 |
int min_mqual; |
| 223 |
double P_het; |
| 224 |
double P_indel; |
| 225 |
double het_scale; |
| 226 |
double homopoly_fix; |
| 227 |
double homopoly_redux; |
| 228 |
qcal_t qcal; |
| 229 |
|
| 230 |
|
| 231 |
samFile *fp; |
| 232 |
FILE *fp_out; |
| 233 |
sam_hdr_t *h; |
| 234 |
hts_idx_t *idx; |
| 235 |
hts_itr_t *iter; |
| 236 |
kstring_t ks_line; |
| 237 |
kstring_t ks_ins_seq; |
| 238 |
kstring_t ks_ins_qual; |
| 239 |
int last_tid; |
| 240 |
hts_pos_t last_pos; |
| 241 |
} consensus_opts; |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
#define CONS_DISCREP 4 |
| 254 |
#define CONS_ALL 15 |
| 255 |
|
| 256 |
#define CONS_MQUAL 16 |
| 257 |
|
| 258 |
typedef struct { |
| 259 |
|
| 260 |
|
| 261 |
int call; |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
int het_call; |
| 266 |
|
| 267 |
|
| 268 |
int het_logodd; |
| 269 |
|
| 270 |
|
| 271 |
int phred; |
| 272 |
|
| 273 |
|
| 274 |
int depth; |
| 275 |
|
| 276 |
|
| 277 |
float discrep; |
| 278 |
} consensus_t; |
| 279 |
|
| 280 |
#define P_HET 1e-3 |
| 281 |
#define P_INDEL 2e-4 |
| 282 |
#define P_HOMOPOLY 0.5 |
| 283 |
#define P_HET_SCALE 1.0 |
| 284 |
|
| 285 |
#define LOG10 2.30258509299404568401 |
| 286 |
#define TENOVERLOG10 4.34294481903251827652 |
| 287 |
#define TENLOG2OVERLOG10 3.0103 |
| 288 |
|
| 289 |
#ifdef __GNUC__ |
| 290 |
#define ALIGNED(x) __attribute((aligned(x))) |
| 291 |
#else |
| 292 |
#define ALIGNED(x) |
| 293 |
#endif |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
static double e_tab_a[1002] ALIGNED(16); |
| 298 |
static double *e_tab = &e_tab_a[500]; |
| 299 |
static double e_tab2_a[1002] ALIGNED(16); |
| 300 |
static double *e_tab2 = &e_tab2_a[500]; |
| 301 |
static double e_log[501] ALIGNED(16); |
| 302 |
|
| 303 |
|
| 304 |
typedef struct { |
| 305 |
double prior[25] ALIGNED(16); |
| 306 |
double lprior15[15] ALIGNED(16); |
| 307 |
|
| 308 |
double pMM[101] ALIGNED(16); |
| 309 |
double p__[101] ALIGNED(16); |
| 310 |
double p_M[101] ALIGNED(16); |
| 311 |
double po_[101] ALIGNED(16); |
| 312 |
double poM[101] ALIGNED(16); |
| 313 |
double poo[101] ALIGNED(16); |
| 314 |
double puu[101] ALIGNED(16); |
| 315 |
double pum[101] ALIGNED(16); |
| 316 |
double pmm[101] ALIGNED(16); |
| 317 |
|
| 318 |
|
| 319 |
double poly_mul; |
| 320 |
} cons_probs; |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
static cons_probs cons_prob_recall, cons_prob_precise; |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
#define MODE_SIMPLE 0 // freq counting |
| 376 |
|
| 377 |
#define MODE_BAYES_116 1 // Samtools 1.16 (no indel param) |
| 378 |
#define MODE_RECALL 2 // so called as it's the params from Gap5 |
| 379 |
#define MODE_PRECISE 3 // a more precise set; +FN, --FP |
| 380 |
#define MODE_MIXED 4 // Combination of GAP5/BAYES |
| 381 |
|
| 382 |
#define QCAL_FLAT 0 |
| 383 |
#define QCAL_HIFI 1 |
| 384 |
#define QCAL_HISEQ 2 |
| 385 |
#define QCAL_ONT_R10_4_SUP 3 |
| 386 |
#define QCAL_ONT_R10_4_DUP 4 |
| 387 |
#define QCAL_ULTIMA 5 |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
static qcal_t static_qcal[6] = { |
| 394 |
{ |
| 395 |
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 396 |
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
| 397 |
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, |
| 398 |
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, |
| 399 |
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, |
| 400 |
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, |
| 401 |
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, |
| 402 |
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, |
| 403 |
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, |
| 404 |
90, 91, 92, 93, 94, 95, 96, 97, 98, 99}, |
| 405 |
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 406 |
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
| 407 |
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, |
| 408 |
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, |
| 409 |
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, |
| 410 |
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, |
| 411 |
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, |
| 412 |
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, |
| 413 |
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, |
| 414 |
90, 91, 92, 93, 94, 95, 96, 97, 98, 99}, |
| 415 |
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 416 |
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
| 417 |
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, |
| 418 |
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, |
| 419 |
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, |
| 420 |
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, |
| 421 |
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, |
| 422 |
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, |
| 423 |
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, |
| 424 |
90, 91, 92, 93, 94, 95, 96, 97, 98, 99} |
| 425 |
}, |
| 426 |
|
| 427 |
{ |
| 428 |
{10, 11, 11, 12, 13, 14, 15, 16, 18, 19, |
| 429 |
20, 21, 22, 23, 24, 25, 27, 28, 29, 30, |
| 430 |
31, 32, 33, 33, 34, 35, 36, 36, 37, 38, |
| 431 |
38, 39, 39, 40, 40, 41, 41, 41, 41, 42, |
| 432 |
42, 42, 42, 43, 43, 43, 43, 43, 43, 43, |
| 433 |
44, 44, 44, 44, 44, 44, 44, 44, 44, 44, |
| 434 |
44, 44, 44, 44, 44, 44, 44, 44, 44, 44, |
| 435 |
44, 44, 44, 44, 44, 44, 44, 44, 44, 44, |
| 436 |
44, 44, 44, 44, 44, 44, 44, 44, 44, 44, |
| 437 |
44, 44, 44, 44, 44, 44, 44, 44, 44, 44, |
| 438 |
}, |
| 439 |
{ 4, 4, 4, 4, 5, 6, 6, 7, 8, 9, |
| 440 |
10, 11, 11, 12, 13, 14, 15, 15, 16, 17, |
| 441 |
18, 19, 19, 20, 20, 21, 22, 23, 23, 24, |
| 442 |
25, 25, 25, 26, 26, 26, 27, 27, 28, 28, |
| 443 |
28, 28, 27, 27, 27, 28, 28, 28, 28, 27, |
| 444 |
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, |
| 445 |
27, 27, 26, 26, 25, 26, 26, 27, 27, 27, |
| 446 |
26, 26, 26, 26, 26, 26, 26, 26, 27, 27, |
| 447 |
28, 29, 28, 28, 28, 27, 27, 27, 27, 27, |
| 448 |
27, 28, 28, 30, 30, 30, 30, 30, 30, 30, |
| 449 |
}, |
| 450 |
{ 8, 8, 8, 8, 9, 10, 11, 12, 13, 14, |
| 451 |
15, 15, 16, 17, 18, 19, 19, 20, 20, 21, |
| 452 |
21, 22, 22, 23, 23, 23, 24, 24, 24, 25, |
| 453 |
25, 25, 25, 25, 25, 26, 26, 26, 26, 27, |
| 454 |
27, 27, 27, 27, 27, 28, 28, 28, 28, 28, |
| 455 |
29, 29, 29, 29, 29, 29, 30, 30, 30, 30, |
| 456 |
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, |
| 457 |
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, |
| 458 |
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, |
| 459 |
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, |
| 460 |
} |
| 461 |
}, |
| 462 |
|
| 463 |
{ |
| 464 |
{ 2, 2, 2, 3, 3, 4, 5, 5, 6, 7, |
| 465 |
8, 9, 10, 11, 11, 12, 13, 14, 15, 16, |
| 466 |
17, 17, 18, 19, 20, 21, 22, 22, 23, 24, |
| 467 |
25, 26, 27, 28, 28, 29, 30, 31, 32, 33, |
| 468 |
34, 34, 35, 36, 37, 38, 39, 39, 40, 41, |
| 469 |
42, 43, 44, 45, 45, 46, 47, 48, 49, 50, |
| 470 |
51, 51, 52, 53, 54, 55, 56, 56, 57, 58, |
| 471 |
59, 60, 61, 62, 62, 63, 64, 65, 66, 67, |
| 472 |
68, 68, 69, 70, 71, 72, 73, 73, 74, 75, |
| 473 |
76, 77, 78, 79, 79, 80, 81, 82, 83, 84, |
| 474 |
}, |
| 475 |
{ 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, |
| 476 |
13, 14, 15, 16, 17, 19, 20, 21, 22, 23, |
| 477 |
25, 26, 27, 28, 29, 31, 32, 33, 34, 35, |
| 478 |
37, 38, 39, 40, 41, 43, 44, 45, 46, 47, |
| 479 |
49, 50, 51, 52, 53, 55, 56, 57, 58, 59, |
| 480 |
61, 62, 63, 64, 65, 67, 68, 69, 70, 71, |
| 481 |
73, 74, 75, 76, 77, 79, 80, 81, 82, 83, |
| 482 |
85, 86, 87, 88, 89, 91, 92, 93, 94, 95, |
| 483 |
97, 98, 99, 100, 101, 103, 104, 105, 106, 107, |
| 484 |
109, 110, 111, 112, 113, 115, 116, 117, 118, 119, |
| 485 |
}, |
| 486 |
{ 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, |
| 487 |
13, 14, 15, 16, 17, 19, 20, 21, 22, 23, |
| 488 |
25, 26, 27, 28, 29, 31, 32, 33, 34, 35, |
| 489 |
37, 38, 39, 40, 41, 43, 44, 45, 46, 47, |
| 490 |
49, 50, 51, 52, 53, 55, 56, 57, 58, 59, |
| 491 |
61, 62, 63, 64, 65, 67, 68, 69, 70, 71, |
| 492 |
73, 74, 75, 76, 77, 79, 80, 81, 82, 83, |
| 493 |
85, 86, 87, 88, 89, 91, 92, 93, 94, 95, |
| 494 |
97, 98, 99, 100, 101, 103, 104, 105, 106, 107, |
| 495 |
109, 110, 111, 112, 113, 115, 116, 117, 118, 119, |
| 496 |
} |
| 497 |
}, |
| 498 |
{ |
| 499 |
{ 0, 2, 2, 2, 3, 4, 4, 5, 6, 7, |
| 500 |
7, 8, 9, 12, 13, 14, 15, 15, 16, 17, |
| 501 |
18, 19, 20, 22, 24, 25, 26, 27, 28, 29, |
| 502 |
30, 31, 33, 34, 36, 37, 38, 38, 39, 39, |
| 503 |
40, 40, 40, 40, 40, 40, 40, 41, 40, 40, |
| 504 |
41, 41, 40, 40, 40, 40, 41, 40, 40, 40, |
| 505 |
40, 41, 41, 40, 40, 41, 40, 40, 39, 41, |
| 506 |
40, 41, 40, 40, 41, 41, 41, 40, 40, 40, |
| 507 |
40, 40, 40, 40, 40, 40, 40, 40, 40, 40, |
| 508 |
40, 40, 40, 40, 40, 40, 40, 40, 40, 40, |
| 509 |
}, |
| 510 |
{ 0, 2, 2, 2, 3, 4, 5, 6, 7, 8, |
| 511 |
8, 9, 9, 10, 10, 10, 11, 12, 12, 13, |
| 512 |
13, 13, 14, 14, 15, 16, 16, 17, 18, 18, |
| 513 |
19, 19, 20, 21, 22, 23, 24, 25, 25, 25, |
| 514 |
25, 25, 25, 25, 25, 25, 26, 26, 26, 26, |
| 515 |
26, 26, 26, 26, 27, 27, 27, 27, 27, 27, |
| 516 |
27, 27, 27, 27, 27, 27, 27, 28, 28, 28, |
| 517 |
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, |
| 518 |
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, |
| 519 |
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, |
| 520 |
}, |
| 521 |
{ 0, 4, 6, 6, 6, 7, 7, 8, 9, 9, |
| 522 |
9, 10, 10, 11, 11, 12, 12, 13, 13, 14, |
| 523 |
15, 15, 15, 16, 16, 17, 17, 18, 18, 19, |
| 524 |
19, 20, 20, 21, 22, 22, 23, 23, 24, 24, |
| 525 |
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, |
| 526 |
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, |
| 527 |
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, |
| 528 |
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, |
| 529 |
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, |
| 530 |
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, |
| 531 |
} |
| 532 |
}, |
| 533 |
{ |
| 534 |
{10, 11, 11, 12, 13, 14, 15, 16, 18, 19, |
| 535 |
20, 21, 22, 23, 24, 25, 27, 28, 29, 30, |
| 536 |
31, 32, 33, 33, 34, 35, 36, 36, 37, 38, |
| 537 |
38, 39, 39, 40, 40, 41, 41, 41, 41, 42, |
| 538 |
42, 42, 42, 43, 43, 43, 43, 43, 43, 43, |
| 539 |
44, 44, 44, 44, 44, 44, 44, 44, 44, 44, |
| 540 |
44, 44, 44, 44, 44, 44, 44, 44, 44, 44, |
| 541 |
44, 44, 44, 44, 44, 44, 44, 44, 44, 44, |
| 542 |
44, 44, 44, 44, 44, 44, 44, 44, 44, 44, |
| 543 |
44, 44, 44, 44, 44, 44, 44, 44, 44, 44, |
| 544 |
}, |
| 545 |
{ 4, 4, 4, 4, 5, 6, 6, 7, 8, 9, |
| 546 |
10, 11, 11, 12, 13, 14, 15, 15, 16, 17, |
| 547 |
18, 19, 19, 20, 20, 21, 22, 23, 23, 24, |
| 548 |
25, 25, 25, 26, 26, 26, 27, 27, 28, 28, |
| 549 |
28, 28, 27, 27, 27, 28, 28, 28, 28, 27, |
| 550 |
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, |
| 551 |
27, 27, 26, 26, 25, 26, 26, 27, 27, 27, |
| 552 |
26, 26, 26, 26, 26, 26, 26, 26, 27, 27, |
| 553 |
28, 29, 28, 28, 28, 27, 27, 27, 27, 27, |
| 554 |
27, 28, 28, 30, 30, 30, 30, 30, 30, 30, |
| 555 |
}, |
| 556 |
{ 8, 8, 8, 8, 9, 10, 11, 12, 13, 14, |
| 557 |
15, 15, 16, 17, 18, 19, 19, 20, 20, 21, |
| 558 |
21, 22, 22, 23, 23, 23, 24, 24, 24, 25, |
| 559 |
25, 25, 25, 25, 25, 26, 26, 26, 26, 27, |
| 560 |
27, 27, 27, 27, 27, 28, 28, 28, 28, 28, |
| 561 |
29, 29, 29, 29, 29, 29, 30, 30, 30, 30, |
| 562 |
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, |
| 563 |
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, |
| 564 |
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, |
| 565 |
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, |
| 566 |
} |
| 567 |
}, |
| 568 |
{ |
| 569 |
{ 2, 2, 3, 4, 5, 6, 6, 7, 8, 9, |
| 570 |
10, 10, 11, 12, 13, 14, 14, 15, 16, 17, |
| 571 |
18, 18, 19, 21, 22, 23, 23, 24, 25, 26, |
| 572 |
27, 27, 28, 29, 30, 31, 31, 32, 33, 34, |
| 573 |
35, 35, 36, 37, 38, 39, 39, 40, 42, 43, |
| 574 |
44, 44, 45, 46, 47, 48, 48, 49, 50, 51, |
| 575 |
52, 52, 53, 54, 55, 56, 56, 57, 58, 59, |
| 576 |
60, 60, 61, 63, 64, 65, 65, 66, 67, 68, |
| 577 |
69, 69, 70, 71, 72, 73, 73, 74, 75, 76, |
| 578 |
77, 77, 78, 79, 80, 81, 81, 82, 84, 85, |
| 579 |
}, |
| 580 |
{ 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, |
| 581 |
5, 5, 6, 6, 7, 7, 8, 8, 9, 10, |
| 582 |
10, 10, 11, 12, 13, 13, 13, 14, 15, 16, |
| 583 |
16, 16, 17, 18, 18, 19, 19, 20, 20, 21, |
| 584 |
21, 22, 22, 22, 22, 23, 23, 24, 24, 25, |
| 585 |
25, 25, 25, 25, 25, 25, 26, 26, 26, 26, |
| 586 |
26, 26, 27, 27, 27, 27, 27, 27, 27, 27, |
| 587 |
27, 28, 28, 28, 28, 28, 28, 28, 28, 28, |
| 588 |
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, |
| 589 |
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, |
| 590 |
}, |
| 591 |
{ 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, |
| 592 |
5, 5, 6, 6, 7, 7, 8, 8, 9, 10, |
| 593 |
10, 10, 11, 12, 13, 13, 13, 14, 15, 16, |
| 594 |
16, 16, 17, 18, 18, 19, 19, 20, 20, 21, |
| 595 |
21, 22, 22, 22, 22, 23, 23, 24, 24, 25, |
| 596 |
25, 25, 25, 25, 25, 25, 26, 26, 26, 26, |
| 597 |
26, 26, 27, 27, 27, 27, 27, 27, 27, 27, |
| 598 |
27, 28, 28, 28, 28, 28, 28, 28, 28, 28, |
| 599 |
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, |
| 600 |
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, |
| 601 |
} |
| 602 |
} |
| 603 |
}; |
| 604 |
|
| 605 |
int set_qcal(qcal_t *q, int id) { |
| 606 |
if (id < 0 || id >= sizeof(static_qcal)/sizeof(*static_qcal)) |
| 607 |
return -1; |
| 608 |
|
| 609 |
memcpy(q, &static_qcal[id], sizeof(*q)); |
| 610 |
return 0; |
| 611 |
} |
| 612 |
|
| 613 |
int load_qcal(qcal_t *q, const char *fn) { |
| 614 |
int i; |
| 615 |
|
| 616 |
if (strcmp(fn, ":hifi") == 0) |
| 617 |
return set_qcal(q, QCAL_HIFI); |
| 618 |
if (strcmp(fn, ":hiseq") == 0) |
| 619 |
return set_qcal(q, QCAL_HISEQ); |
| 620 |
if (strcmp(fn, ":r10.4_sup") == 0) |
| 621 |
return set_qcal(q, QCAL_ONT_R10_4_SUP); |
| 622 |
if (strcmp(fn, ":r10.4_dup") == 0) |
| 623 |
return set_qcal(q, QCAL_ONT_R10_4_DUP); |
| 624 |
if (strcmp(fn, ":ultima") == 0) |
| 625 |
return set_qcal(q, QCAL_ULTIMA); |
| 626 |
|
| 627 |
|
| 628 |
for (i = 0; i < 101; i++) |
| 629 |
q->smap[i] = q->umap[i] = q->omap[i] = i; |
| 630 |
|
| 631 |
if (strcmp(fn, ":flat") == 0) |
| 632 |
return 0; |
| 633 |
|
| 634 |
hFILE *fp = hopen(fn, "r"); |
| 635 |
if (!fp) |
| 636 |
return -1; |
| 637 |
|
| 638 |
kstring_t line = KS_INITIALIZE; |
| 639 |
int max = 0; |
| 640 |
int last_qual = 0; |
| 641 |
while (line.l = 0, kgetline(&line, (kgets_func *)hgets, fp) >= 0) { |
| 642 |
int v, s, u, o; |
| 643 |
if (*line.s == '#') |
| 644 |
continue; |
| 645 |
if (sscanf(line.s, "QUAL %d %d %d %d", &v, &s, &u, &o) != 4) |
| 646 |
goto err; |
| 647 |
while (v > last_qual) { |
| 648 |
q->smap[last_qual+1] = q->smap[last_qual]; |
| 649 |
q->umap[last_qual+1] = q->umap[last_qual]; |
| 650 |
q->omap[last_qual+1] = q->omap[last_qual]; |
| 651 |
last_qual++; |
| 652 |
} |
| 653 |
if (v >= 0 && v < 100) { |
| 654 |
q->smap[v] = s; |
| 655 |
q->umap[v] = u; |
| 656 |
q->omap[v] = o; |
| 657 |
} |
| 658 |
if (v < max) { |
| 659 |
fprintf(samtools_stderr, "Qual calibration file is not in ascending order\n"); |
| 660 |
return hclose(fp) ? -2 : -1; |
| 661 |
} |
| 662 |
max = v; |
| 663 |
} |
| 664 |
|
| 665 |
for (i = max+1; i < 101; i++) { |
| 666 |
q->smap[i] = q->smap[max]; |
| 667 |
q->umap[i] = q->umap[max]; |
| 668 |
q->omap[i] = q->omap[max]; |
| 669 |
} |
| 670 |
|
| 671 |
ks_free(&line); |
| 672 |
return hclose(fp) < 0 ? -2 : 0; |
| 673 |
|
| 674 |
err: |
| 675 |
ks_free(&line); |
| 676 |
return hclose(fp) < 0 ? -2 : -1; |
| 677 |
} |
| 678 |
|
| 679 |
static void consensus_init(double p_het, double p_indel, double het_scale, |
| 680 |
double poly_mul, |
| 681 |
qcal_t *qcal, int mode, cons_probs *cp) { |
| 682 |
int i; |
| 683 |
|
| 684 |
|
| 685 |
for (i = -500; i <= 500; i++) |
| 686 |
e_tab[i] = exp(i); |
| 687 |
for (i = -500; i <= 500; i++) |
| 688 |
e_tab2[i] = exp(i/10.); |
| 689 |
for (i = 0; i <= 500; i++) |
| 690 |
e_log[i] = log(i); |
| 691 |
|
| 692 |
|
| 693 |
cp->poly_mul = poly_mul; |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
for (i = 0; i < 25; i++) |
| 699 |
cp->prior[i] = p_het / 6; |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
cp->prior[0]=cp->prior[6]=cp->prior[12]=cp->prior[18]=cp->prior[24] = 1; |
| 704 |
|
| 705 |
|
| 706 |
for (i = 4; i < 24; i+=5) |
| 707 |
cp->prior[i] = p_indel / 6; |
| 708 |
|
| 709 |
|
| 710 |
for (i = 20; i < 24; i++) |
| 711 |
cp->prior[i] = p_indel / 6; |
| 712 |
|
| 713 |
cp->lprior15[0] = log(cp->prior[0]); |
| 714 |
cp->lprior15[1] = log(cp->prior[1]); |
| 715 |
cp->lprior15[2] = log(cp->prior[2]); |
| 716 |
cp->lprior15[3] = log(cp->prior[3]); |
| 717 |
cp->lprior15[4] = log(cp->prior[4]); |
| 718 |
cp->lprior15[5] = log(cp->prior[6]); |
| 719 |
cp->lprior15[6] = log(cp->prior[7]); |
| 720 |
cp->lprior15[7] = log(cp->prior[8]); |
| 721 |
cp->lprior15[8] = log(cp->prior[9]); |
| 722 |
cp->lprior15[9] = log(cp->prior[12]); |
| 723 |
cp->lprior15[10] = log(cp->prior[13]); |
| 724 |
cp->lprior15[11] = log(cp->prior[14]); |
| 725 |
cp->lprior15[12] = log(cp->prior[18]); |
| 726 |
cp->lprior15[13] = log(cp->prior[19]); |
| 727 |
cp->lprior15[14] = log(cp->prior[24]); |
| 728 |
|
| 729 |
for (i = 1; i < 101; i++) { |
| 730 |
double prob = 1 - pow(10, -qcal->smap[i] / 10.0); |
| 731 |
|
| 732 |
|
| 733 |
cp->pMM[i] = log(prob); |
| 734 |
|
| 735 |
|
| 736 |
cp->p__[i] = log((1-prob)/3); |
| 737 |
|
| 738 |
|
| 739 |
cp->p_M[i] = log((exp(cp->pMM[i]) + exp(cp->p__[i]))/2); |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
cp->p_M[i] += log(het_scale); |
| 760 |
|
| 761 |
if (mode == MODE_BAYES_116) { |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
cp->pmm[i] = cp->pMM[i]; |
| 767 |
cp->poM[i] = cp->p_M[i]; |
| 768 |
cp->pum[i] = cp->p_M[i]; |
| 769 |
cp->po_[i] = cp->p__[i]; |
| 770 |
cp->poo[i] = cp->p__[i]; |
| 771 |
cp->puu[i] = cp->p__[i]; |
| 772 |
|
| 773 |
} else { |
| 774 |
|
| 775 |
prob = 1 - pow(10, -qcal->omap[i] / 10.0); |
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
cp->poo[i] = log((1-prob)/3); |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
if (cp->poo[i] > cp->pMM[i]-.5) |
| 784 |
cp->poo[i] = cp->pMM[i]-.5; |
| 785 |
|
| 786 |
cp->po_[i] = log((exp(cp->poo[i]) + exp(cp->p__[i]))/2); |
| 787 |
cp->poM[i] = log((exp(cp->poo[i]) + exp(cp->pMM[i]))/2); |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
if (cp->poM[i] > cp->p_M[i]+.5) |
| 795 |
cp->poM[i] = cp->p_M[i]+.5; |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
prob = 1 - pow(10, -qcal->umap[i] / 10.0); |
| 803 |
cp->pmm[i] = log(prob); |
| 804 |
cp->puu[i] = log((1-prob)/3); |
| 805 |
if (cp->puu[i] > cp->pMM[i]-.5) |
| 806 |
cp->puu[i] = cp->pMM[i]-.5; |
| 807 |
|
| 808 |
cp->pum[i] = log((exp(cp->puu[i]) + exp(cp->pmm[i]))/2); |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
cp->pMM[0] = cp->pMM[1]; |
| 813 |
cp->p__[0] = cp->p__[1]; |
| 814 |
cp->p_M[0] = cp->p_M[1]; |
| 815 |
|
| 816 |
cp->pmm[0] = cp->pmm[1]; |
| 817 |
cp->poo[0] = cp->poo[1]; |
| 818 |
cp->po_[0] = cp->po_[1]; |
| 819 |
cp->poM[0] = cp->poM[1]; |
| 820 |
cp->puu[0] = cp->puu[1]; |
| 821 |
cp->pum[0] = cp->pum[1]; |
| 822 |
} |
| 823 |
|
| 824 |
static inline double fast_exp(double y) { |
| 825 |
if (y >= -50 && y <= 50) |
| 826 |
return e_tab2[(int)(y*10)]; |
| 827 |
|
| 828 |
if (y < -500) |
| 829 |
y = -500; |
| 830 |
if (y > 500) |
| 831 |
y = 500; |
| 832 |
|
| 833 |
return e_tab[(int)y]; |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
static inline double fast_log2(double val) |
| 838 |
{ |
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
union { double d; uint64_t x; } u = {val}; |
| 845 |
const int E = ((u.x >> 52) & 2047) - 1024; |
| 846 |
|
| 847 |
u.x &= ~(2047LL << 52); |
| 848 |
u.x += 1023LL << 52; |
| 849 |
|
| 850 |
val = ((-1/3.) * u.d + 2) * u.d - 2/3.; |
| 851 |
|
| 852 |
return E + val; |
| 853 |
} |
| 854 |
|
| 855 |
#define ph_log(x) (-TENLOG2OVERLOG10*fast_log2((x))) |
| 856 |
|
| 857 |
|
| 858 |
int nins(const bam1_t *b){ |
| 859 |
int i, indel = 0; |
| 860 |
uint32_t *cig = bam_get_cigar(b); |
| 861 |
for (i = 0; i < b->core.n_cigar; i++) { |
| 862 |
int op = bam_cigar_op(cig[i]); |
| 863 |
if (op == BAM_CINS || op == BAM_CDEL) |
| 864 |
indel += bam_cigar_oplen(cig[i]); |
| 865 |
} |
| 866 |
return indel; |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
void homopoly_qual_fix(bam1_t *b) { |
| 883 |
static double ph2err[256] = {0}; |
| 884 |
int i; |
| 885 |
if (!ph2err[0]) { |
| 886 |
for (i = 0; i < 256; i++) |
| 887 |
ph2err[i] = pow(10, i/-10.0); |
| 888 |
} |
| 889 |
uint8_t *seq = bam_get_seq(b); |
| 890 |
uint8_t *qual = bam_get_qual(b); |
| 891 |
for (i = 0; i < b->core.l_qseq; i++) { |
| 892 |
int s = i; |
| 893 |
int base = bam_seqi(seq, i); |
| 894 |
while (i+1 < b->core.l_qseq && bam_seqi(seq, i+1) == base) |
| 895 |
i++; |
| 896 |
|
| 897 |
|
| 898 |
if (s == i) |
| 899 |
continue; |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
int j, k; |
| 907 |
for (j = s, k = i; j < k; j++,k--) { |
| 908 |
double e = ph2err[qual[j]] + ph2err[qual[k]]; |
| 909 |
qual[j] = qual[k] = -fast_log2(e/2)*3.0104+.49; |
| 910 |
} |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
double nm_local(const pileup_t *p, const bam1_t *b, hts_pos_t pos) { |
| 918 |
int *nm = (int *)p->cd; |
| 919 |
if (!nm) |
| 920 |
return 0; |
| 921 |
pos -= b->core.pos; |
| 922 |
if (pos < 0) |
| 923 |
return nm[0] & ((1<<24)-1); |
| 924 |
if (pos >= b->core.l_qseq) |
| 925 |
return nm[b->core.l_qseq-1] & ((1<<24)-1); |
| 926 |
|
| 927 |
return (nm[pos] & ((1<<24)-1)) / 10.0; |
| 928 |
} |
| 929 |
|
| 930 |
int poly_len(const pileup_t *p, const bam1_t *b, hts_pos_t pos) { |
| 931 |
int *nm = (int *)p->cd; |
| 932 |
if (!nm) |
| 933 |
return 0; |
| 934 |
pos -= b->core.pos; |
| 935 |
if (pos >= 0 && pos < b->core.l_qseq) |
| 936 |
return nm[pos] >> 24; |
| 937 |
else |
| 938 |
return 0; |
| 939 |
} |
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
int nm_init(void *client_data, samFile *fp, sam_hdr_t *h, pileup_t *p) { |
| 952 |
consensus_opts *opts = (consensus_opts *)client_data; |
| 953 |
if (!opts->use_mqual) |
| 954 |
return 1; |
| 955 |
|
| 956 |
const bam1_t *b = &p->b; |
| 957 |
int qlen = b->core.l_qseq, i; |
| 958 |
if (qlen <= 0) |
| 959 |
return 0; |
| 960 |
int *local_nm = calloc(qlen, sizeof(*local_nm)); |
| 961 |
if (!local_nm) |
| 962 |
return -1; |
| 963 |
p->cd = local_nm; |
| 964 |
|
| 965 |
double poly_adj = opts->homopoly_fix ? opts->homopoly_fix : 1; |
| 966 |
|
| 967 |
if (opts->adj_qual) { |
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
uint8_t *qual = bam_get_qual(b); |
| 982 |
uint8_t *seq = bam_get_seq(b); |
| 983 |
const int qhalo = 8; |
| 984 |
int qmin = qual[0]; |
| 985 |
const int qhalop = 2; |
| 986 |
int qminp = qual[0]; |
| 987 |
int base = bam_seqi(seq, 0), polyl = 0, polyr = 0; |
| 988 |
|
| 989 |
|
| 990 |
for (i = 1; i < qlen; i++) { |
| 991 |
if (bam_seqi(seq, i) != base) |
| 992 |
break; |
| 993 |
if (i < qhalop && qminp > qual[i]) |
| 994 |
qminp = qual[i]; |
| 995 |
} |
| 996 |
|
| 997 |
|
| 998 |
for (i = 0; i < qlen && i < qhalo; i++) { |
| 999 |
if (qmin > qual[i]) |
| 1000 |
qmin = qual[i]; |
| 1001 |
} |
| 1002 |
|
| 1003 |
for (;i < qlen-qhalo; i++) { |
| 1004 |
if (opts->homopoly_fix && bam_seqi(seq, i) != base) { |
| 1005 |
polyl = i; |
| 1006 |
base = bam_seqi(seq, i); |
| 1007 |
qminp = qual[i]; |
| 1008 |
int j; |
| 1009 |
for (j = i+1; j < qlen; j++) { |
| 1010 |
if (bam_seqi(seq, j) != base) |
| 1011 |
break; |
| 1012 |
if (i < qhalop && qminp > qual[j]) |
| 1013 |
qminp = qual[j]; |
| 1014 |
} |
| 1015 |
polyr = j-1; |
| 1016 |
} else { |
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
polyr = polyl; |
| 1021 |
} |
| 1022 |
int pl = polyr-polyl; |
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
int t = (opts->mode == MODE_BAYES_116) |
| 1029 |
? (qual[i] + 5*qmin)/4 |
| 1030 |
: qual[i]/3 + (qminp-pl*2)*poly_adj; |
| 1031 |
|
| 1032 |
|
| 1033 |
local_nm[i] += t < qual[i] ? qual[i]-t : 0; |
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
qminp = qual[i]; |
| 1038 |
int k; |
| 1039 |
for (k = MAX(polyl,i-qhalop); k <= MIN(polyr,i+qhalop); k++) |
| 1040 |
if (qminp > qual[k]) |
| 1041 |
qminp = qual[k]; |
| 1042 |
|
| 1043 |
if (qmin > qual[i+qhalo]) |
| 1044 |
qmin = qual[i+qhalo]; |
| 1045 |
else if (qmin <= qual[i-qhalo]) { |
| 1046 |
int j; |
| 1047 |
qmin = 99; |
| 1048 |
for (j = i-qhalo+1; j <= i+qhalo; j++) |
| 1049 |
if (qmin > qual[j]) |
| 1050 |
qmin = qual[j]; |
| 1051 |
} |
| 1052 |
} |
| 1053 |
for (; i < qlen; i++) { |
| 1054 |
int t = (opts->mode == MODE_BAYES_116) |
| 1055 |
? (qual[i] + 5*qmin)/4 |
| 1056 |
: qual[i]/3 + qminp*poly_adj; |
| 1057 |
local_nm[i] += t < qual[i] ? qual[i]-t : 0; |
| 1058 |
} |
| 1059 |
} |
| 1060 |
|
| 1061 |
|
| 1062 |
if (opts->homopoly_fix) |
| 1063 |
homopoly_qual_fix((bam1_t *)b); |
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
uint8_t *seq = bam_get_seq(b); |
| 1068 |
for (i = 0; i < qlen; i++) { |
| 1069 |
int base = bam_seqi(seq, i); |
| 1070 |
int poly = 0, j, k; |
| 1071 |
for (j = i+1; j < qlen; j++) |
| 1072 |
if (bam_seqi(seq, j) != base) |
| 1073 |
break; |
| 1074 |
|
| 1075 |
|
| 1076 |
poly = j-i-1; if (poly > 100) poly = 100; |
| 1077 |
const int HALO=0; |
| 1078 |
for (k = i-HALO; k < j+HALO; k++) |
| 1079 |
if (k >= 0 && k < qlen) |
| 1080 |
local_nm[k] = ((MAX(poly, local_nm[k]>>24))<<24) |
| 1081 |
| (local_nm[k] & ((1<<24)-1)); |
| 1082 |
|
| 1083 |
i = j-1; |
| 1084 |
} |
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
const int halo = opts->nm_halo; |
| 1089 |
const uint8_t *md = bam_aux_get(b, "MD"); |
| 1090 |
if (!md) |
| 1091 |
return 1; |
| 1092 |
md = (const uint8_t *)bam_aux2Z(md); |
| 1093 |
|
| 1094 |
|
| 1095 |
uint32_t *cig = bam_get_cigar(b); |
| 1096 |
int ncig = b->core.n_cigar; |
| 1097 |
|
| 1098 |
if ( (cig[0] & BAM_CIGAR_MASK) == BAM_CSOFT_CLIP || |
| 1099 |
((cig[0] & BAM_CIGAR_MASK) == BAM_CHARD_CLIP && ncig > 1 && |
| 1100 |
(cig[1] & BAM_CIGAR_MASK) == BAM_CSOFT_CLIP)) { |
| 1101 |
for (i = 0; i < halo && i < qlen; i++) |
| 1102 |
local_nm[i]+=opts->sc_cost; |
| 1103 |
for (; i < halo*2 && i < qlen; i++) |
| 1104 |
local_nm[i]+=opts->sc_cost>>1; |
| 1105 |
} |
| 1106 |
if ( (cig[ncig-1] & BAM_CIGAR_MASK) == BAM_CSOFT_CLIP || |
| 1107 |
((cig[ncig-1] & BAM_CIGAR_MASK) == BAM_CHARD_CLIP && ncig > 1 && |
| 1108 |
(cig[ncig-2] & BAM_CIGAR_MASK) == BAM_CSOFT_CLIP)) { |
| 1109 |
for (i = qlen-1; i >= qlen-halo && i >= 0; i--) |
| 1110 |
local_nm[i]+=opts->sc_cost; |
| 1111 |
for (; i >= qlen-halo*2 && i >= 0; i--) |
| 1112 |
local_nm[i]+=opts->sc_cost>>1; |
| 1113 |
} |
| 1114 |
|
| 1115 |
|
| 1116 |
int pos = 0; |
| 1117 |
while (*md) { |
| 1118 |
if (isdigit(*md)) { |
| 1119 |
uint8_t *endptr; |
| 1120 |
long i = strtol((char *)md, (char **)&endptr, 10); |
| 1121 |
md = endptr; |
| 1122 |
pos += i; |
| 1123 |
continue; |
| 1124 |
} |
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
if (*md == '^') { |
| 1129 |
while (*++md && !isdigit(*md)) |
| 1130 |
continue; |
| 1131 |
continue; |
| 1132 |
} |
| 1133 |
|
| 1134 |
|
| 1135 |
for (i = pos-halo*2 >= 0 ?pos-halo*2 :0; i < pos-halo && i < qlen; i++) |
| 1136 |
local_nm[i]+=5; |
| 1137 |
for (; i < pos+halo && i < qlen; i++) |
| 1138 |
local_nm[i]+=10; |
| 1139 |
for (; i < pos+halo*2 && i < qlen; i++) |
| 1140 |
local_nm[i]+=5; |
| 1141 |
md++; |
| 1142 |
} |
| 1143 |
|
| 1144 |
return 1; |
| 1145 |
} |
| 1146 |
|
| 1147 |
void nm_free(void *client_data, samFile *fp, sam_hdr_t *h, pileup_t *p) { |
| 1148 |
free(p->cd); |
| 1149 |
p->cd = NULL; |
| 1150 |
} |
| 1151 |
|
| 1152 |
#ifdef DO_HDW |
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
double lnfact(double n) { |
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
return ((n+0.5)*log(n) - n + log(2*M_PI)/2) + log(1 + 1/(12.0*n)); |
| 1162 |
|
| 1163 |
} |
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
double bincoef(int n, double k) { |
| 1177 |
return exp(lnfact(n) - lnfact(k) - lnfact(n-k)); |
| 1178 |
} |
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
double binprobhalf(int n, double k) { |
| 1185 |
return bincoef(n, k) * pow(0.5, n); |
| 1186 |
} |
| 1187 |
|
| 1188 |
double lnbinprobhalf(int n, double k) { |
| 1189 |
|
| 1190 |
return lnfact(n) - lnfact(k) - lnfact(n-k) - 0.69315*n; |
| 1191 |
} |
| 1192 |
#endif |
| 1193 |
|
| 1194 |
static |
| 1195 |
int calculate_consensus_gap5(hts_pos_t pos, int flags, int depth, |
| 1196 |
pileup_t *plp, consensus_opts *opts, |
| 1197 |
consensus_t *cons, int default_qual, |
| 1198 |
cons_probs *cp) { |
| 1199 |
int i, j; |
| 1200 |
static int init_done =0; |
| 1201 |
static double q2p[101], mqual_pow[256]; |
| 1202 |
double min_e_exp = DBL_MIN_EXP * log(2) + 1; |
| 1203 |
|
| 1204 |
double S[15] ALIGNED(16) = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
| 1205 |
double sumsC[6] = {0,0,0,0,0,0}; |
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
#ifdef K2 |
| 1213 |
int hashN[1<<(K2*4+2)] = {0}; |
| 1214 |
int hash1[1<<2] = {0}; |
| 1215 |
#endif |
| 1216 |
|
| 1217 |
|
| 1218 |
static int map_sing[15] ALIGNED(16) = |
| 1219 |
{0, 5, 5, 5, 5, |
| 1220 |
1, 5, 5, 5, |
| 1221 |
2, 5, 5, |
| 1222 |
3, 5, |
| 1223 |
4}; |
| 1224 |
static int map_het[15] ALIGNED(16) = |
| 1225 |
{0, 1, 2, 3, 4, |
| 1226 |
6, 7, 8, 9, |
| 1227 |
12, 13, 14, |
| 1228 |
18, 19, |
| 1229 |
24}; |
| 1230 |
|
| 1231 |
if (!init_done) { |
| 1232 |
init_done = 1; |
| 1233 |
|
| 1234 |
for (i = 0; i <= 100; i++) { |
| 1235 |
q2p[i] = pow(10, -i/10.0); |
| 1236 |
} |
| 1237 |
|
| 1238 |
for (i = 0; i < 255; i++) { |
| 1239 |
|
| 1240 |
mqual_pow[i] = 1-pow(10, -(i*.9)/10.0); |
| 1241 |
|
| 1242 |
|
| 1243 |
} |
| 1244 |
|
| 1245 |
mqual_pow[255] = mqual_pow[10]; |
| 1246 |
} |
| 1247 |
|
| 1248 |
|
| 1249 |
int counts[6] = {0}; |
| 1250 |
#ifdef DO_FRACT |
| 1251 |
int counts2[2][6] = {{0}}; |
| 1252 |
#endif |
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
#ifdef K2 |
| 1257 |
const pileup_t *ptmp = plp; |
| 1258 |
for (; ptmp; ptmp = ptmp->next) { |
| 1259 |
const pileup_t *p = ptmp; |
| 1260 |
if (p->qual < opts->min_qual) |
| 1261 |
continue; |
| 1262 |
|
| 1263 |
int hb = 0; |
| 1264 |
#define _ 0 |
| 1265 |
static int X[16] = {_,0,1,_,2,_,_,_,3,_,_,_,_,_,_,_}; |
| 1266 |
#undef _ |
| 1267 |
uint8_t *seq = bam_get_seq(&p->b); |
| 1268 |
int i, base1 = X[p->base4]; |
| 1269 |
hash1[base1]++; |
| 1270 |
for (i = p->seq_offset-K2; i <= p->seq_offset+K2; i++) { |
| 1271 |
int base = i >= 0 && i < p->b.core.l_qseq ? X[bam_seqi(seq,i)] : _; |
| 1272 |
hb = (hb<<2)|base; |
| 1273 |
} |
| 1274 |
hashN[hb]++; |
| 1275 |
} |
| 1276 |
#endif |
| 1277 |
|
| 1278 |
int td = depth; |
| 1279 |
depth = 0; |
| 1280 |
#ifdef DO_POLY_DIST |
| 1281 |
int poly_dist[2][100] = {0}; |
| 1282 |
#endif |
| 1283 |
for (; plp; plp = plp->next) { |
| 1284 |
pileup_t *p = plp; |
| 1285 |
|
| 1286 |
if (p->next) |
| 1287 |
_mm_prefetch(p->next, _MM_HINT_T0); |
| 1288 |
|
| 1289 |
if (p->qual < opts->min_qual) |
| 1290 |
continue; |
| 1291 |
|
| 1292 |
if (p->ref_skip) |
| 1293 |
continue; |
| 1294 |
|
| 1295 |
#ifdef K2 |
| 1296 |
int hb = 0; |
| 1297 |
#define _ 0 |
| 1298 |
static int X[16] = {_,0,1,_,2,_,_,_,3,_,_,_,_,_,_,_}; |
| 1299 |
int i, base1 = X[p->base4]; |
| 1300 |
for (i = p->seq_offset-K2; i <= p->seq_offset+K2; i++) { |
| 1301 |
int base = i >= 0 && i < p->b.core.l_qseq ? X[bam_seqi(seq,i)] : _; |
| 1302 |
hb = (hb<<2)|base; |
| 1303 |
} |
| 1304 |
#undef _ |
| 1305 |
#endif |
| 1306 |
|
| 1307 |
const bam1_t *b = &p->b; |
| 1308 |
uint8_t base = p->base4; |
| 1309 |
uint8_t *qual_arr = bam_get_qual(b); |
| 1310 |
uint8_t qual = p->qual; |
| 1311 |
|
| 1312 |
if (qual == 255 || (qual == 0 && *qual_arr == 255)) |
| 1313 |
qual = default_qual; |
| 1314 |
|
| 1315 |
#ifdef K2 |
| 1316 |
|
| 1317 |
qual -= -TENOVERLOG10*log(hashN[hb] / (hash1[base1]+.1)); |
| 1318 |
if (qual < 1) |
| 1319 |
qual = 1; |
| 1320 |
#endif |
| 1321 |
|
| 1322 |
|
| 1323 |
static int L[32] = { |
| 1324 |
5,0,1,5, 2,5,5,5, 3,5,5,5, 5,5,5,5, |
| 1325 |
4,4,4,4, 4,4,4,4, 4,4,4,4, 4,4,4,4, |
| 1326 |
}; |
| 1327 |
|
| 1328 |
|
| 1329 |
base = L[base]; |
| 1330 |
|
| 1331 |
double MM, __, _M, oo, oM, o_, uu, um, mm, qe; |
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
if (flags & CONS_MQUAL) { |
| 1339 |
int mqual = b->core.qual; |
| 1340 |
if (opts->nm_adjust) { |
| 1341 |
|
| 1342 |
mqual /= (nm_local(p, b, b->core.pos + p->seq_offset+1)+1); |
| 1343 |
mqual *= 1 + 2*(0.5-(td>30?30:td)/60.0); |
| 1344 |
} |
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
mqual *= opts->scale_mqual; |
| 1349 |
|
| 1350 |
|
| 1351 |
if (mqual < opts->low_mqual) |
| 1352 |
mqual = opts->low_mqual; |
| 1353 |
if (mqual > opts->high_mqual) |
| 1354 |
mqual = opts->high_mqual; |
| 1355 |
|
| 1356 |
double _p = 1-q2p[qual]; |
| 1357 |
double _m = mqual_pow[mqual]; |
| 1358 |
qual = ph_log(1-(_m * _p + (1 - _m)/4)); |
| 1359 |
|
| 1360 |
|
| 1361 |
} |
| 1362 |
|
| 1363 |
|
| 1364 |
if (qual < 1) |
| 1365 |
qual = 1; |
| 1366 |
|
| 1367 |
double poly = poly_len(p, b, b->core.pos + p->seq_offset+1); |
| 1368 |
#ifdef DO_POLY_DIST |
| 1369 |
poly_dist[bam_is_rev(b)][MIN(99,(int)poly)]++; |
| 1370 |
#endif |
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
int qual2 = MAX(1, qual-(poly-2)*cp->poly_mul); |
| 1377 |
|
| 1378 |
|
| 1379 |
__ = cp->p__[qual]; |
| 1380 |
MM = cp->pMM[qual] - __; |
| 1381 |
_M = cp->p_M[qual] - __; |
| 1382 |
|
| 1383 |
|
| 1384 |
oo = cp->poo[qual2] - __; |
| 1385 |
oM = cp->poM[qual2] - __; |
| 1386 |
o_ = cp->po_[qual2] - __; |
| 1387 |
|
| 1388 |
|
| 1389 |
uu = cp->puu[qual2] - __; |
| 1390 |
um = cp->pum[qual2] - __; |
| 1391 |
mm = cp->pmm[qual2] - __; |
| 1392 |
|
| 1393 |
if (flags & CONS_DISCREP) { |
| 1394 |
qe = q2p[qual]; |
| 1395 |
sumsC[base] += 1 - qe; |
| 1396 |
} |
| 1397 |
|
| 1398 |
|
| 1399 |
counts[base]++; |
| 1400 |
#ifdef DO_FRACT |
| 1401 |
counts2[bam_is_rev(b)][base]++; |
| 1402 |
#endif |
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
switch (base) { |
| 1408 |
case 0: |
| 1409 |
S[0] += MM; |
| 1410 |
S[1] += _M; |
| 1411 |
S[2] += _M; |
| 1412 |
S[3] += _M; |
| 1413 |
S[4] += oM; |
| 1414 |
S[8] += o_; |
| 1415 |
S[11] += o_; |
| 1416 |
S[13] += o_; |
| 1417 |
S[14] += oo; |
| 1418 |
break; |
| 1419 |
|
| 1420 |
case 1: |
| 1421 |
S[1] += _M; |
| 1422 |
S[5] += MM; |
| 1423 |
S[6] += _M; |
| 1424 |
S[7] += _M; |
| 1425 |
S[8] += oM; |
| 1426 |
S[4] += o_; |
| 1427 |
S[11] += o_; |
| 1428 |
S[13] += o_; |
| 1429 |
S[14] += oo; |
| 1430 |
|
| 1431 |
|
| 1432 |
break; |
| 1433 |
|
| 1434 |
case 2: |
| 1435 |
S[ 2] += _M; |
| 1436 |
S[ 6] += _M; |
| 1437 |
S[ 9] += MM; |
| 1438 |
S[10] += _M; |
| 1439 |
S[11] += oM; |
| 1440 |
S[4] += o_; |
| 1441 |
S[8] += o_; |
| 1442 |
S[13] += o_; |
| 1443 |
S[14] += oo; |
| 1444 |
break; |
| 1445 |
|
| 1446 |
case 3: |
| 1447 |
S[ 3] += _M; |
| 1448 |
S[ 7] += _M; |
| 1449 |
S[10] += _M; |
| 1450 |
S[12] += MM; |
| 1451 |
S[13] += oM; |
| 1452 |
S[4] += o_; |
| 1453 |
S[8] += o_; |
| 1454 |
S[11] += o_; |
| 1455 |
S[14] += oo; |
| 1456 |
|
| 1457 |
|
| 1458 |
break; |
| 1459 |
|
| 1460 |
case 4: |
| 1461 |
|
| 1462 |
S[0] += uu; S[1 ]+= uu; S[2 ]+= uu; S[3 ]+= uu; S[4 ]+= um; |
| 1463 |
S[5 ]+= uu; S[6 ]+= uu; S[7 ]+= uu; S[8 ]+= um; |
| 1464 |
S[9 ]+= uu; S[10]+= uu; S[11]+= um; |
| 1465 |
S[12]+= uu; S[13]+= um; |
| 1466 |
S[14]+= mm; |
| 1467 |
break; |
| 1468 |
|
| 1469 |
case 5: |
| 1470 |
S[0] += MM; S[1 ]+= MM; S[2 ]+= MM; S[3 ]+= MM; S[4 ]+= oM; |
| 1471 |
S[5 ]+= MM; S[6 ]+= MM; S[7 ]+= MM; S[8 ]+= oM; |
| 1472 |
S[9 ]+= MM; S[10]+= MM; S[11]+= oM; |
| 1473 |
S[12]+= MM; S[13]+= oM; |
| 1474 |
S[14]+= oo; |
| 1475 |
break; |
| 1476 |
} |
| 1477 |
|
| 1478 |
depth++; |
| 1479 |
} |
| 1480 |
|
| 1481 |
#ifdef DO_POLY_DIST |
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
int d1 = 0, d2 = 0; |
| 1490 |
double nd1 = 0, nd2 = 0; |
| 1491 |
int k; |
| 1492 |
for (k = 0; k < 100; k++) { |
| 1493 |
if (!poly_dist[0][k] && !poly_dist[1][k]) |
| 1494 |
continue; |
| 1495 |
|
| 1496 |
|
| 1497 |
d1 += (k+1)*poly_dist[0][k]; |
| 1498 |
d2 += (k+1)*poly_dist[1][k]; |
| 1499 |
nd1 += poly_dist[0][k]; |
| 1500 |
nd2 += poly_dist[1][k]; |
| 1501 |
} |
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
int n1 = 0, n2 = 0, l1 = 0, l2 = 0; |
| 1510 |
for (k = 0; k < 100; k++) { |
| 1511 |
int poly12 = poly_dist[0][k]+poly_dist[1][k]; |
| 1512 |
if (n1 < poly12) { |
| 1513 |
n2 = n1; l2 = l1; |
| 1514 |
n1 = poly12; |
| 1515 |
l1 = k; |
| 1516 |
} else if (n2 < poly12) { |
| 1517 |
n2 = poly12; |
| 1518 |
l2 = k; |
| 1519 |
} |
| 1520 |
} |
| 1521 |
|
| 1522 |
const double N = 5; |
| 1523 |
nd1 += 1; |
| 1524 |
nd2 += 1; |
| 1525 |
|
| 1526 |
|
| 1527 |
int pn1p = poly_dist[0][l1]; |
| 1528 |
int pn1m = poly_dist[1][l1]; |
| 1529 |
|
| 1530 |
int pn2p = poly_dist[0][l2]; |
| 1531 |
int pn2m = poly_dist[1][l2]; |
| 1532 |
|
| 1533 |
|
| 1534 |
double s1 = (pn1p+N) / (pn2p+N); s1 = s1>1?1/s1:s1; |
| 1535 |
|
| 1536 |
double s2 = (pn1m+N) / (pn2m+N); s2 = s2>1?1/s2:s2; |
| 1537 |
|
| 1538 |
|
| 1539 |
double sbias = s1 / s2; sbias = sbias>1?1/sbias:sbias; |
| 1540 |
|
| 1541 |
if (pn2p+pn2m > 0 && l1 != l2) { |
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
sbias = 10*log(sbias); |
| 1549 |
S[ 4] += sbias; |
| 1550 |
S[ 8] += sbias; |
| 1551 |
S[11] += sbias; |
| 1552 |
S[13] += sbias; |
| 1553 |
} else { |
| 1554 |
sbias = 0; |
| 1555 |
} |
| 1556 |
#endif |
| 1557 |
|
| 1558 |
|
| 1559 |
double shift, max, max_het, norm[15]; |
| 1560 |
int call = 0, het_call = 0; |
| 1561 |
double tot1 = 0, tot2 = 0; |
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
shift = -DBL_MAX; |
| 1572 |
max = -DBL_MAX; |
| 1573 |
max_het = -DBL_MAX; |
| 1574 |
|
| 1575 |
#ifdef DO_FRACT |
| 1576 |
|
| 1577 |
|
| 1578 |
for (j = 0; j < 15; j++) { |
| 1579 |
if (j == 0 || j == 5 || j == 9 || j == 12 || j == 14) |
| 1580 |
continue; |
| 1581 |
|
| 1582 |
double c1p = counts2[0][map_het[j]%5]; |
| 1583 |
double c1m = counts2[1][map_het[j]%5]; |
| 1584 |
double c2p = counts2[0][map_het[j]/5]; |
| 1585 |
double c2m = counts2[1][map_het[j]/5]; |
| 1586 |
|
| 1587 |
double c1 = c1p + c1m; |
| 1588 |
double c2 = c2p + c2m; |
| 1589 |
|
| 1590 |
if (c1 && c2) { |
| 1591 |
|
| 1592 |
const int N = 10; |
| 1593 |
double b1 = 1 - (N+MIN(c1p,c1m))/(N+MAX(c1p,c1m)); |
| 1594 |
double b2 = 1 - (N+MIN(c2p,c2m))/(N+MAX(c2p,c2m)); |
| 1595 |
if (b1 > 0.5) S[j] -= b1; |
| 1596 |
if (b2 > 0.5) S[j] -= b2; |
| 1597 |
|
| 1598 |
|
| 1599 |
c1 += 1e-5; |
| 1600 |
c2 += 1e-5; |
| 1601 |
if (c2 > c1) { |
| 1602 |
double tmp = c2; |
| 1603 |
c2 = c1; |
| 1604 |
c1 = tmp; |
| 1605 |
} |
| 1606 |
|
| 1607 |
if (c2 < opts->min_depth) |
| 1608 |
S[j] -= 100; |
| 1609 |
if (c2 / (c1+1e-5) <= opts->het_fract) |
| 1610 |
S[j] -= 100; |
| 1611 |
} |
| 1612 |
} |
| 1613 |
#endif |
| 1614 |
|
| 1615 |
#ifdef DO_HDW |
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
for (j = 0; j < 15; j++) { |
| 1621 |
if (j == 0 || j == 5 || j == 9 || j == 12 || j == 14) |
| 1622 |
continue; |
| 1623 |
|
| 1624 |
double c1 = counts[map_het[j]%5]; |
| 1625 |
double c2 = counts[map_het[j]/5]; |
| 1626 |
|
| 1627 |
if (c1 && c2) { |
| 1628 |
c1 += 1e-5; |
| 1629 |
c2 += 1e-5; |
| 1630 |
if (c2 > c1) { |
| 1631 |
double tmp = c2; |
| 1632 |
c2 = c1; |
| 1633 |
c1 = tmp; |
| 1634 |
} |
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
double c12 = c1+c2; |
| 1639 |
if (c12 > 20) { |
| 1640 |
c2 *= 20/(c12); |
| 1641 |
c12 = 20; |
| 1642 |
c1 = 20-c2; |
| 1643 |
} |
| 1644 |
|
| 1645 |
|
| 1646 |
c1+=1; |
| 1647 |
c2+=1; |
| 1648 |
c12+=2; |
| 1649 |
S[j] += lnbinprobhalf(c12, c2) + fast_log2(c12)*0.69+.2; |
| 1650 |
} |
| 1651 |
} |
| 1652 |
#endif |
| 1653 |
|
| 1654 |
for (j = 0; j < 15; j++) { |
| 1655 |
S[j] += cp->lprior15[j]; |
| 1656 |
if (shift < S[j]) |
| 1657 |
shift = S[j]; |
| 1658 |
|
| 1659 |
|
| 1660 |
if (j != 0 && j != 5 && j != 9 && j != 12 && j != 14) { |
| 1661 |
if (max_het < S[j]) { |
| 1662 |
max_het = S[j]; |
| 1663 |
het_call = j; |
| 1664 |
} |
| 1665 |
continue; |
| 1666 |
} |
| 1667 |
|
| 1668 |
if (max < S[j]) { |
| 1669 |
max = S[j]; |
| 1670 |
call = j; |
| 1671 |
} |
| 1672 |
} |
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
for (j = 0; j < 15; j++) { |
| 1684 |
S[j] -= shift; |
| 1685 |
double e = fast_exp(S[j]); |
| 1686 |
S[j] = (S[j] > min_e_exp) ? e : DBL_MIN; |
| 1687 |
norm[j] = 0; |
| 1688 |
} |
| 1689 |
|
| 1690 |
for (j = 0; j < 15; j++) { |
| 1691 |
norm[j] += tot1; |
| 1692 |
norm[14-j] += tot2; |
| 1693 |
tot1 += S[j]; |
| 1694 |
tot2 += S[14-j]; |
| 1695 |
} |
| 1696 |
|
| 1697 |
|
| 1698 |
if (!depth || depth == counts[5] ) { |
| 1699 |
cons->call = 4; |
| 1700 |
cons->het_call = 0; |
| 1701 |
cons->het_logodd = 0; |
| 1702 |
cons->phred = 0; |
| 1703 |
cons->depth = 0; |
| 1704 |
cons->discrep = 0; |
| 1705 |
return 0; |
| 1706 |
} |
| 1707 |
|
| 1708 |
cons->depth = depth; |
| 1709 |
|
| 1710 |
|
| 1711 |
if (norm[call] == 0) norm[call] = DBL_MIN; |
| 1712 |
|
| 1713 |
|
| 1714 |
int ph; |
| 1715 |
if (S[call] == 1 && norm[call] < .01) |
| 1716 |
ph = ph_log(norm[call]) + .5; |
| 1717 |
else |
| 1718 |
ph = ph_log(1-S[call]/(norm[call]+S[call])) + .5; |
| 1719 |
|
| 1720 |
cons->call = map_sing[call]; |
| 1721 |
cons->phred = ph < 0 ? 0 : ph; |
| 1722 |
|
| 1723 |
if (norm[het_call] == 0) norm[het_call] = DBL_MIN; |
| 1724 |
ph = TENLOG2OVERLOG10 * (fast_log2(S[het_call]) |
| 1725 |
- fast_log2(norm[het_call])) + .5; |
| 1726 |
|
| 1727 |
cons->het_call = map_het[het_call]; |
| 1728 |
cons->het_logodd = ph; |
| 1729 |
|
| 1730 |
|
| 1731 |
if (flags & CONS_DISCREP) { |
| 1732 |
double m = sumsC[0]+sumsC[1]+sumsC[2]+sumsC[3]+sumsC[4]; |
| 1733 |
double c; |
| 1734 |
if (cons->het_logodd > 0) |
| 1735 |
c = sumsC[cons->het_call%5] + sumsC[cons->het_call/5]; |
| 1736 |
else |
| 1737 |
c = sumsC[cons->call]; |
| 1738 |
cons->discrep = (m-c)/sqrt(m); |
| 1739 |
} |
| 1740 |
|
| 1741 |
return 0; |
| 1742 |
} |
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
int calculate_consensus_gap5m(hts_pos_t pos, int flags, int depth, |
| 1751 |
pileup_t *plp, consensus_opts *opts, |
| 1752 |
consensus_t *cons, int default_qual, |
| 1753 |
cons_probs *cp_r, cons_probs *cp_p) { |
| 1754 |
if (opts->mode != MODE_MIXED) |
| 1755 |
return calculate_consensus_gap5(pos, flags, depth, plp, opts, |
| 1756 |
cons, default_qual, |
| 1757 |
opts->mode == MODE_PRECISE |
| 1758 |
? cp_p : cp_r); |
| 1759 |
|
| 1760 |
|
| 1761 |
consensus_t consP, consR; |
| 1762 |
|
| 1763 |
calculate_consensus_gap5(pos, flags, depth, plp, opts, |
| 1764 |
&consP, default_qual, cp_p); |
| 1765 |
|
| 1766 |
calculate_consensus_gap5(pos, flags, depth, plp, opts, |
| 1767 |
&consR, default_qual, cp_r); |
| 1768 |
|
| 1769 |
#define MIN(a,b) ((a)<(b)?(a):(b)) |
| 1770 |
#define MAX(a,b) ((a)>(b)?(a):(b)) |
| 1771 |
|
| 1772 |
|
| 1773 |
memcpy(cons, &consP, sizeof(consP)); |
| 1774 |
|
| 1775 |
if (consP.phred > 0 && consR.phred > 0 && consP.call == consR.call) { |
| 1776 |
|
| 1777 |
|
| 1778 |
cons->phred += MIN(20, consR.phred); |
| 1779 |
|
| 1780 |
} else if (consP.het_logodd >= 0 && consR.het_logodd >= 0 && |
| 1781 |
consP.het_call == consR.het_call) { |
| 1782 |
|
| 1783 |
|
| 1784 |
cons->het_logodd += MIN(20, consR.het_logodd); |
| 1785 |
|
| 1786 |
} else if (consP.het_logodd >= 0) { |
| 1787 |
|
| 1788 |
|
| 1789 |
int q2 = MAX(consR.phred, consR.het_logodd); |
| 1790 |
cons->het_logodd = MAX(1, (cons->het_logodd - q2/2)); |
| 1791 |
|
| 1792 |
} else if (consR.het_logodd >= 70) { |
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
int q1 = consP.phred; |
| 1798 |
int q2 = consR.het_logodd; |
| 1799 |
memcpy(cons, &consR, sizeof(consR)); |
| 1800 |
cons->het_logodd = MIN(15, MAX((q2-q1*2)/2, 1+q2/(q1+1.0))); |
| 1801 |
|
| 1802 |
} else if (consR.het_logodd >= 0) { |
| 1803 |
|
| 1804 |
int q1 = consP.phred; |
| 1805 |
int q2 = consR.het_logodd; |
| 1806 |
memcpy(cons, &consR, sizeof(consR)); |
| 1807 |
cons->het_logodd = MAX(1,q2 - 0.3*q1) |
| 1808 |
+ 5*(consP.het_call == consR.het_call); |
| 1809 |
cons->phred = 0; |
| 1810 |
|
| 1811 |
} else if (consR.het_logodd < 0) { |
| 1812 |
|
| 1813 |
|
| 1814 |
consR.phred = consR.phred / 2; |
| 1815 |
if (consR.phred > consP.phred) |
| 1816 |
memcpy(cons, &consR, sizeof(consR)); |
| 1817 |
cons->phred = MAX(10, cons->phred); |
| 1818 |
} |
| 1819 |
|
| 1820 |
return 0; |
| 1821 |
} |
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
|
| 1827 |
static void dump_fastq(consensus_opts *opts, |
| 1828 |
const char *name, |
| 1829 |
const char *seq, size_t seq_l, |
| 1830 |
const char *qual, size_t qual_l) { |
| 1831 |
enum format fmt = opts->fmt; |
| 1832 |
int line_len = opts->line_len; |
| 1833 |
FILE *fp = opts->fp_out; |
| 1834 |
|
| 1835 |
fprintf(fp, "%c%s\n", ">@"[fmt==FASTQ], name); |
| 1836 |
size_t i; |
| 1837 |
for (i = 0; i < seq_l; i += line_len) |
| 1838 |
fprintf(fp, "%.*s\n", (int)MIN(line_len, seq_l - i), seq+i); |
| 1839 |
|
| 1840 |
if (fmt == FASTQ) { |
| 1841 |
fprintf(fp, "+\n"); |
| 1842 |
for (i = 0; i < seq_l; i += line_len) |
| 1843 |
fprintf(fp, "%.*s\n", (int)MIN(line_len, seq_l - i), qual+i); |
| 1844 |
} |
| 1845 |
} |
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
static int readaln2(void *dat, samFile *fp, sam_hdr_t *h, bam1_t *b) { |
| 1854 |
consensus_opts *opts = (consensus_opts *)dat; |
| 1855 |
|
| 1856 |
for (;;) { |
| 1857 |
int ret = opts->iter |
| 1858 |
? sam_itr_next(fp, opts->iter, b) |
| 1859 |
: sam_read1(fp, h, b); |
| 1860 |
if (ret < 0) |
| 1861 |
return ret; |
| 1862 |
|
| 1863 |
|
| 1864 |
if (opts->incl_flags && !(b->core.flag & opts->incl_flags)) |
| 1865 |
continue; |
| 1866 |
if (opts->excl_flags && (b->core.flag & opts->excl_flags)) |
| 1867 |
continue; |
| 1868 |
if (b->core.qual < opts->min_mqual) |
| 1869 |
continue; |
| 1870 |
|
| 1871 |
return ret; |
| 1872 |
} |
| 1873 |
} |
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
|
| 1881 |
|
| 1882 |
|
| 1883 |
|
| 1884 |
|
| 1885 |
|
| 1886 |
|
| 1887 |
|
| 1888 |
|
| 1889 |
|
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
|
| 1906 |
static int calculate_consensus_simple(const pileup_t *plp, |
| 1907 |
consensus_opts *opts, int *qual) { |
| 1908 |
int i, min_qual = opts->min_qual; |
| 1909 |
int tot_depth = 0; |
| 1910 |
|
| 1911 |
|
| 1912 |
|
| 1913 |
|
| 1914 |
static int seqi2A[16] = { 0,8,0,4, 0,4,0,2, 0,4,0,2, 0,2,0,1 }; |
| 1915 |
static int seqi2C[16] = { 0,0,8,4, 0,0,4,2, 0,0,4,2, 0,0,2,1 }; |
| 1916 |
static int seqi2G[16] = { 0,0,0,0, 8,4,4,1, 0,0,0,0, 4,2,2,1 }; |
| 1917 |
static int seqi2T[16] = { 0,0,0,0, 0,0,0,0, 8,4,4,2, 8,2,2,1 }; |
| 1918 |
|
| 1919 |
|
| 1920 |
|
| 1921 |
|
| 1922 |
int freq[17] = {0}; |
| 1923 |
uint64_t score[17] = {0}; |
| 1924 |
|
| 1925 |
|
| 1926 |
for (; plp; plp = plp->next) { |
| 1927 |
const pileup_t *p = plp; |
| 1928 |
if (p->next) |
| 1929 |
_mm_prefetch(p->next, _MM_HINT_T0); |
| 1930 |
|
| 1931 |
int q = p->qual; |
| 1932 |
if (q < min_qual) |
| 1933 |
|
| 1934 |
|
| 1935 |
|
| 1936 |
|
| 1937 |
continue; |
| 1938 |
|
| 1939 |
|
| 1940 |
int b = p->base4; |
| 1941 |
|
| 1942 |
|
| 1943 |
if (b < 16) { |
| 1944 |
int Q = seqi2A[b] * (opts->use_qual ? q : 1); |
| 1945 |
freq[1] += Q?1:0; |
| 1946 |
score[1] += Q?Q:0; |
| 1947 |
Q = seqi2C[b] * (opts->use_qual ? q : 1); |
| 1948 |
freq[2] += Q?1:0; |
| 1949 |
score[2] += Q?Q:0; |
| 1950 |
Q = seqi2G[b] * (opts->use_qual ? q : 1); |
| 1951 |
freq[4] += Q?1:0; |
| 1952 |
score[4] += Q?Q:0; |
| 1953 |
Q = seqi2T[b] * (opts->use_qual ? q : 1); |
| 1954 |
freq[8] += Q?1:0; |
| 1955 |
score[8] += Q?Q:0; |
| 1956 |
} else { |
| 1957 |
freq[16] ++; |
| 1958 |
score[16]+=8 * (opts->use_qual ? q : 1); |
| 1959 |
} |
| 1960 |
tot_depth++; |
| 1961 |
} |
| 1962 |
|
| 1963 |
|
| 1964 |
uint64_t tscore = 0; |
| 1965 |
for (i = 0; i < 5; i++) |
| 1966 |
tscore += score[1<<i]; |
| 1967 |
|
| 1968 |
|
| 1969 |
int call1 = 15, call2 = 15; |
| 1970 |
uint64_t score1 = 0, score2 = 0; |
| 1971 |
for (i = 0; i < 5; i++) { |
| 1972 |
int c = 1<<i; |
| 1973 |
if (score1 < score[c]) { |
| 1974 |
score2 = score1; |
| 1975 |
call2 = call1; |
| 1976 |
score1 = score[c]; |
| 1977 |
call1 = c; |
| 1978 |
} else if (score2 < score[c]) { |
| 1979 |
score2 = score[c]; |
| 1980 |
call2 = c; |
| 1981 |
} |
| 1982 |
} |
| 1983 |
|
| 1984 |
|
| 1985 |
uint64_t used_score = score1; |
| 1986 |
int used_base = call1; |
| 1987 |
if (score2 >= opts->het_fract * score1 && opts->ambig) { |
| 1988 |
used_base |= call2; |
| 1989 |
used_score += score2; |
| 1990 |
} |
| 1991 |
|
| 1992 |
|
| 1993 |
if (tot_depth < opts->min_depth || |
| 1994 |
used_score < opts->call_fract * tscore) { |
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
used_base = call1 == 16 ? 16 : 0; |
| 1999 |
} |
| 2000 |
|
| 2001 |
|
| 2002 |
const char *het = |
| 2003 |
"NACMGRSVTWYHKDBN" |
| 2004 |
"*ac?g???t???????"; |
| 2005 |
|
| 2006 |
|
| 2007 |
if (qual) |
| 2008 |
*qual = used_base ? 100.0 * used_score / tscore : 0; |
| 2009 |
|
| 2010 |
return het[used_base]; |
| 2011 |
} |
| 2012 |
|
| 2013 |
static int empty_pileup2(consensus_opts *opts, sam_hdr_t *h, int tid, |
| 2014 |
hts_pos_t start, hts_pos_t end) { |
| 2015 |
const char *name = sam_hdr_tid2name(h, tid); |
| 2016 |
hts_pos_t i; |
| 2017 |
|
| 2018 |
int err = 0; |
| 2019 |
for (i = start; i < end; i++) |
| 2020 |
err |= fprintf(opts->fp_out, "%s\t%"PRIhts_pos"\t0\t0\tN\t0\t*\t*\n", name, i+1) < 0; |
| 2021 |
|
| 2022 |
return err ? -1 : 0; |
| 2023 |
} |
| 2024 |
|
| 2025 |
|
| 2026 |
|
| 2027 |
|
| 2028 |
|
| 2029 |
static int basic_pileup(void *cd, samFile *fp, sam_hdr_t *h, pileup_t *p, |
| 2030 |
int depth, hts_pos_t pos, int nth, int is_insert) { |
| 2031 |
unsigned char *qp, *cp; |
| 2032 |
char *rp; |
| 2033 |
int ref, cb, cq; |
| 2034 |
consensus_opts *opts = (consensus_opts *)cd; |
| 2035 |
int tid = p->b.core.tid; |
| 2036 |
|
| 2037 |
|
| 2038 |
|
| 2039 |
if (!opts->show_ins && nth) |
| 2040 |
return 0; |
| 2041 |
|
| 2042 |
if (opts->iter) { |
| 2043 |
if (opts->iter->beg >= pos || opts->iter->end < pos) |
| 2044 |
return 0; |
| 2045 |
} |
| 2046 |
|
| 2047 |
if (opts->all_bases) { |
| 2048 |
if (tid != opts->last_tid && opts->last_tid >= -1) { |
| 2049 |
if (opts->last_tid >= 0) { |
| 2050 |
|
| 2051 |
hts_pos_t len = sam_hdr_tid2len(opts->h, opts->last_tid); |
| 2052 |
if (opts->iter) |
| 2053 |
len = MIN(opts->iter->end, len); |
| 2054 |
if (empty_pileup2(opts, opts->h, opts->last_tid, |
| 2055 |
opts->last_pos, len) < 0) |
| 2056 |
return -1; |
| 2057 |
} |
| 2058 |
|
| 2059 |
opts->last_pos = opts->iter ? opts->iter->beg : 0; |
| 2060 |
} |
| 2061 |
|
| 2062 |
|
| 2063 |
if (!opts->iter && tid > opts->last_tid && opts->all_bases > 1) { |
| 2064 |
while (++opts->last_tid < tid) { |
| 2065 |
hts_pos_t len = sam_hdr_tid2len(opts->h, opts->last_tid); |
| 2066 |
if (empty_pileup2(opts, opts->h, opts->last_tid, 0, len) < 0) |
| 2067 |
return -1; |
| 2068 |
} |
| 2069 |
} |
| 2070 |
|
| 2071 |
|
| 2072 |
if (opts->last_pos >= 0 && pos > opts->last_pos+1) { |
| 2073 |
if (empty_pileup2(opts, opts->h, p->b.core.tid, opts->last_pos, |
| 2074 |
pos-1) < 0) |
| 2075 |
return -1; |
| 2076 |
} else if (opts->last_pos < 0) { |
| 2077 |
if (empty_pileup2(opts, opts->h, p->b.core.tid, |
| 2078 |
opts->iter ? opts->iter->beg : 0, pos-1) < 0) |
| 2079 |
return -1; |
| 2080 |
} |
| 2081 |
} |
| 2082 |
|
| 2083 |
if (opts->mode != MODE_SIMPLE) { |
| 2084 |
consensus_t cons; |
| 2085 |
calculate_consensus_gap5m(pos, opts->use_mqual ? CONS_MQUAL : 0, |
| 2086 |
depth, p, opts, &cons, opts->default_qual, |
| 2087 |
&cons_prob_recall, &cons_prob_precise); |
| 2088 |
if (cons.depth < opts->min_depth) { |
| 2089 |
cb = 'N'; |
| 2090 |
cq = 0; |
| 2091 |
} else if (cons.het_logodd > 0 && opts->ambig) { |
| 2092 |
cb = "AMRWa" |
| 2093 |
"MCSYc" |
| 2094 |
"RSGKg" |
| 2095 |
"WYKTt" |
| 2096 |
"acgt*"[cons.het_call]; |
| 2097 |
cq = cons.het_logodd; |
| 2098 |
} else{ |
| 2099 |
cb = "ACGT*"[cons.call]; |
| 2100 |
cq = cons.phred; |
| 2101 |
} |
| 2102 |
if (cq < opts->cons_cutoff && cb != '*') { |
| 2103 |
cb = 'N'; |
| 2104 |
cq = 0; |
| 2105 |
} |
| 2106 |
} else { |
| 2107 |
cb = calculate_consensus_simple(p, opts, &cq); |
| 2108 |
} |
| 2109 |
if (cb < 0) |
| 2110 |
return -1; |
| 2111 |
|
| 2112 |
if (!p) |
| 2113 |
return 0; |
| 2114 |
|
| 2115 |
if (!opts->show_del && cb == '*') |
| 2116 |
return 0; |
| 2117 |
|
| 2118 |
|
| 2119 |
kstring_t *ks = &opts->ks_line; |
| 2120 |
ks->l = 0; |
| 2121 |
ref = p->b.core.tid; |
| 2122 |
rp = (char *)sam_hdr_tid2name(h, ref); |
| 2123 |
|
| 2124 |
int err = 0; |
| 2125 |
err |= kputs(rp, ks) < 0; |
| 2126 |
err |= kputc_('\t', ks) < 0; |
| 2127 |
err |= kputw(pos, ks) < 0; |
| 2128 |
err |= kputc_('\t', ks) < 0; |
| 2129 |
err |= kputw(nth, ks) < 0; |
| 2130 |
err |= kputc_('\t', ks) < 0; |
| 2131 |
err |= kputw(depth, ks) < 0; |
| 2132 |
err |= kputc_('\t', ks) < 0; |
| 2133 |
err |= kputc_(cb, ks) < 0; |
| 2134 |
err |= kputc_('\t', ks) < 0; |
| 2135 |
err |= kputw(cq, ks) < 0; |
| 2136 |
err |= kputc_('\t', ks) < 0; |
| 2137 |
if (err) |
| 2138 |
return -1; |
| 2139 |
|
| 2140 |
|
| 2141 |
if (ks_resize(ks, ks->l + depth*2 + 2) < 0) |
| 2142 |
return -1; |
| 2143 |
|
| 2144 |
cp = (unsigned char *)ks->s + ks->l; |
| 2145 |
ks->l += depth*2 + 2; |
| 2146 |
qp = cp+depth+1; |
| 2147 |
for (; p; p = p->next) { |
| 2148 |
|
| 2149 |
if (p->next && p->next->next) |
| 2150 |
_mm_prefetch(p->next->next, _MM_HINT_T0); |
| 2151 |
if (p->b_is_rev) { |
| 2152 |
*cp++ = p->base == '*' ? '#' : tolower(p->base); |
| 2153 |
} else { |
| 2154 |
*cp++ = p->base; |
| 2155 |
} |
| 2156 |
*qp++ = MIN(p->qual,93) + '!'; |
| 2157 |
} |
| 2158 |
*cp++ = '\t'; |
| 2159 |
*qp++ = '\n'; |
| 2160 |
if (fwrite(ks->s, 1, ks->l, opts->fp_out) != ks->l) |
| 2161 |
return -1; |
| 2162 |
|
| 2163 |
opts->last_pos = pos; |
| 2164 |
opts->last_tid = tid; |
| 2165 |
|
| 2166 |
return 0; |
| 2167 |
} |
| 2168 |
|
| 2169 |
static int basic_fasta(void *cd, samFile *fp, sam_hdr_t *h, pileup_t *p, |
| 2170 |
int depth, hts_pos_t pos, int nth, int is_insert) { |
| 2171 |
int cb, cq; |
| 2172 |
consensus_opts *opts = (consensus_opts *)cd; |
| 2173 |
int tid = p->b.core.tid; |
| 2174 |
kstring_t *seq = &opts->ks_ins_seq; |
| 2175 |
kstring_t *qual = &opts->ks_ins_qual; |
| 2176 |
|
| 2177 |
if (!opts->show_ins && nth) |
| 2178 |
return 0; |
| 2179 |
|
| 2180 |
if (opts->iter) { |
| 2181 |
if (opts->iter->beg >= pos || opts->iter->end < pos) |
| 2182 |
return 0; |
| 2183 |
} |
| 2184 |
|
| 2185 |
next_ref: |
| 2186 |
if (tid != opts->last_tid) { |
| 2187 |
if (opts->last_tid != -1) { |
| 2188 |
if (opts->all_bases) { |
| 2189 |
|
| 2190 |
int i, N; |
| 2191 |
if (opts->iter) { |
| 2192 |
opts->last_pos = MAX(opts->last_pos, opts->iter->beg-1); |
| 2193 |
N = opts->iter->end; |
| 2194 |
} else { |
| 2195 |
N = INT_MAX; |
| 2196 |
} |
| 2197 |
N = MIN(N, sam_hdr_tid2len(opts->h, opts->last_tid)) |
| 2198 |
- opts->last_pos; |
| 2199 |
if (N > 0) { |
| 2200 |
if (ks_expand(seq, N+1) < 0) |
| 2201 |
return -1; |
| 2202 |
if (ks_expand(qual, N+1) < 0) |
| 2203 |
return -1; |
| 2204 |
for (i = 0; i < N; i++) { |
| 2205 |
seq->s[seq->l++] = 'N'; |
| 2206 |
qual->s[qual->l++] = '!'; |
| 2207 |
} |
| 2208 |
seq->s[seq->l] = 0; |
| 2209 |
qual->s[qual->l] = 0; |
| 2210 |
} |
| 2211 |
} |
| 2212 |
dump_fastq(opts, sam_hdr_tid2name(opts->h, opts->last_tid), |
| 2213 |
seq->s, seq->l, qual->s, qual->l); |
| 2214 |
} |
| 2215 |
|
| 2216 |
seq->l = 0; qual->l = 0; |
| 2217 |
|
| 2218 |
if (!opts->iter && opts->all_bases > 1 && ++opts->last_tid < tid) { |
| 2219 |
opts->last_pos = 0; |
| 2220 |
goto next_ref; |
| 2221 |
} |
| 2222 |
|
| 2223 |
opts->last_tid = tid; |
| 2224 |
if (opts->iter) |
| 2225 |
opts->last_pos = opts->iter->beg; |
| 2226 |
else |
| 2227 |
opts->last_pos = opts->all_bases ? 0 : pos-1; |
| 2228 |
} |
| 2229 |
|
| 2230 |
|
| 2231 |
if (opts->mode != MODE_SIMPLE) { |
| 2232 |
consensus_t cons; |
| 2233 |
calculate_consensus_gap5m(pos, opts->use_mqual ? CONS_MQUAL : 0, |
| 2234 |
depth, p, opts, &cons, opts->default_qual, |
| 2235 |
&cons_prob_recall, &cons_prob_precise); |
| 2236 |
if (cons.depth < opts->min_depth) { |
| 2237 |
cb = 'N'; |
| 2238 |
cq = 0; |
| 2239 |
} else if (cons.het_logodd > 0 && opts->ambig) { |
| 2240 |
cb = "AMRWa" |
| 2241 |
"MCSYc" |
| 2242 |
"RSGKg" |
| 2243 |
"WYKTt" |
| 2244 |
"acgt*"[cons.het_call]; |
| 2245 |
cq = cons.het_logodd; |
| 2246 |
} else { |
| 2247 |
cb = "ACGT*"[cons.call]; |
| 2248 |
cq = cons.phred; |
| 2249 |
} |
| 2250 |
if (cq < opts->cons_cutoff && cb != '*' && |
| 2251 |
cons.het_call % 5 != 4 && cons.het_call / 5 != 4) { |
| 2252 |
|
| 2253 |
|
| 2254 |
|
| 2255 |
cb = 'N'; |
| 2256 |
cq = 0; |
| 2257 |
} |
| 2258 |
} else { |
| 2259 |
cb = calculate_consensus_simple(p, opts, &cq); |
| 2260 |
} |
| 2261 |
if (cb < 0) |
| 2262 |
return -1; |
| 2263 |
|
| 2264 |
if (!p) |
| 2265 |
return 0; |
| 2266 |
|
| 2267 |
if (!opts->show_del && cb == '*') { |
| 2268 |
opts->last_pos = pos; |
| 2269 |
opts->last_tid = tid; |
| 2270 |
return 0; |
| 2271 |
} |
| 2272 |
if (opts->mark_ins && nth && cb != '*') { |
| 2273 |
kputc('_', seq); |
| 2274 |
kputc('_', qual); |
| 2275 |
} |
| 2276 |
|
| 2277 |
|
| 2278 |
|
| 2279 |
|
| 2280 |
if (pos > opts->last_pos) { |
| 2281 |
if (opts->last_pos >= 0 || opts->all_bases) { |
| 2282 |
|
| 2283 |
if (ks_expand(seq, pos - opts->last_pos) < 0 || |
| 2284 |
ks_expand(qual, pos - opts->last_pos) < 0) |
| 2285 |
return -1; |
| 2286 |
memset(seq->s + seq->l, 'N', pos - (opts->last_pos+1)); |
| 2287 |
memset(qual->s + qual->l, '!', pos - (opts->last_pos+1)); |
| 2288 |
seq->l += pos - (opts->last_pos+1); |
| 2289 |
qual->l += pos - (opts->last_pos+1); |
| 2290 |
} |
| 2291 |
} |
| 2292 |
if ((nth && opts->show_ins && cb != '*') |
| 2293 |
|| cb != '*' || (pos > opts->last_pos && opts->show_del)) { |
| 2294 |
int err = 0; |
| 2295 |
err |= kputc(cb, seq) < 0; |
| 2296 |
err |= kputc(MIN(cq, '~'-'!')+'!', qual) < 0; |
| 2297 |
if (err) |
| 2298 |
return -1; |
| 2299 |
} |
| 2300 |
|
| 2301 |
opts->last_pos = pos; |
| 2302 |
opts->last_tid = tid; |
| 2303 |
|
| 2304 |
return 0; |
| 2305 |
} |
| 2306 |
|
| 2307 |
|
| 2308 |
|
| 2309 |
|
| 2310 |
static void usage_exit(FILE *fp, int exit_status) { |
| 2311 |
fprintf(fp, "Usage: samtools consensus [options] <in.bam>\n"); |
| 2312 |
fprintf(fp, "\nOptions:\n"); |
| 2313 |
fprintf(fp, " -r, --region REG Limit query to REG. Requires an index\n"); |
| 2314 |
fprintf(fp, " -f, --format FMT Output in format FASTA, FASTQ or PILEUP [FASTA]\n"); |
| 2315 |
fprintf(fp, " -l, --line-len INT Wrap FASTA/Q at line length INT [70]\n"); |
| 2316 |
fprintf(fp, " -o, --output FILE Output consensus to FILE\n"); |
| 2317 |
fprintf(fp, " -m, --mode STR Switch consensus mode to \"simple\"/\"bayesian\" [bayesian]\n"); |
| 2318 |
fprintf(fp, " -a Output all bases (start/end of reference)\n"); |
| 2319 |
fprintf(fp, " --rf, --incl-flags STR|INT\n"); |
| 2320 |
fprintf(fp, " Only include reads with any flag bit set [0]\n"); |
| 2321 |
fprintf(fp, " --ff, --excl-flags STR|INT\n"); |
| 2322 |
fprintf(fp, " Exclude reads with any flag bit set\n"); |
| 2323 |
fprintf(fp, " [UNMAP,SECONDARY,QCFAIL,DUP]\n"); |
| 2324 |
fprintf(fp, " --min-MQ INT Exclude reads with mapping quality below INT [0]\n"); |
| 2325 |
fprintf(fp, " --min-BQ INT Exclude reads with base quality below INT [0]\n"); |
| 2326 |
fprintf(fp, " --show-del yes/no Whether to show deletion as \"*\" [no]\n"); |
| 2327 |
fprintf(fp, " --show-ins yes/no Whether to show insertions [yes]\n"); |
| 2328 |
fprintf(fp, " --mark-ins Add '+' before every inserted base/qual [off]\n"); |
| 2329 |
fprintf(fp, " -A, --ambig Enable IUPAC ambiguity codes [off]\n"); |
| 2330 |
fprintf(fp, " -d, --min-depth INT Minimum depth of INT [1]\n"); |
| 2331 |
fprintf(fp, "\nFor simple consensus mode:\n"); |
| 2332 |
fprintf(fp, " -q, --(no-)use-qual Use quality values in calculation [off]\n"); |
| 2333 |
fprintf(fp, " -c, --call-fract INT At least INT portion of bases must agree [0.75]\n"); |
| 2334 |
fprintf(fp, " -H, --het-fract INT Minimum fraction of 2nd-most to most common base [0.15]\n"); |
| 2335 |
fprintf(fp, "\nFor default \"Bayesian\" consensus mode:\n"); |
| 2336 |
fprintf(fp, " -C, --cutoff C Consensus cutoff quality C [10]\n"); |
| 2337 |
fprintf(fp, " --(no-)adj-qual Modify quality with local minima [on]\n"); |
| 2338 |
fprintf(fp, " --(no-)use-MQ Use mapping quality in calculation [on]\n"); |
| 2339 |
fprintf(fp, " --(no-)adj-MQ Modify mapping quality by local NM [on]\n"); |
| 2340 |
fprintf(fp, " --NM-halo INT Size of window for NM count in --adj-MQ [50]\n"); |
| 2341 |
fprintf(fp, " --scale-MQ FLOAT Scale mapping quality by FLOAT [1.00]\n"); |
| 2342 |
fprintf(fp, " --low-MQ INT Cap minimum mapping quality [1]\n"); |
| 2343 |
fprintf(fp, " --high-MQ INT Cap maximum mapping quality [60]\n"); |
| 2344 |
fprintf(fp, " --P-het FLOAT Probability of heterozygous site[%.1e]\n", |
| 2345 |
P_HET); |
| 2346 |
fprintf(fp, " --P-indel FLOAT Probability of indel sites[%.1e]\n", |
| 2347 |
P_INDEL); |
| 2348 |
fprintf(fp, " --het-scale FLOAT Heterozygous SNP probability multiplier[%.1e]\n", |
| 2349 |
P_HET_SCALE); |
| 2350 |
fprintf(fp, " -p, --homopoly-fix Spread low-qual bases to both ends of homopolymers\n"); |
| 2351 |
fprintf(fp, " --homopoly-score FLOAT\n" |
| 2352 |
" Qual fraction adjustment for -p option [%g]\n", P_HOMOPOLY); |
| 2353 |
fprintf(fp, " -t, --qual-calibration FILE / :config (see man page)\n"); |
| 2354 |
fprintf(fp, " Load quality calibration file\n"); |
| 2355 |
fprintf(fp, "\n"); |
| 2356 |
fprintf(fp, " -X, --config STR Use pre-defined configuration set. STR from:\n"); |
| 2357 |
fprintf(fp, " hiseq, hifi, r10.4_sup, r10.4_dup and ultima\n"); |
| 2358 |
|
| 2359 |
fprintf(fp, "\nGlobal options:\n"); |
| 2360 |
sam_global_opt_help(fp, "-.---@-."); |
| 2361 |
samtools_exit(exit_status); |
| 2362 |
} |
| 2363 |
|
| 2364 |
int main_consensus(int argc, char **argv) { |
| 2365 |
int c, ret = 1; |
| 2366 |
|
| 2367 |
consensus_opts opts = { |
| 2368 |
|
| 2369 |
.mode = MODE_RECALL, |
| 2370 |
.use_qual = 0, |
| 2371 |
.min_qual = 0, |
| 2372 |
.adj_qual = 1, |
| 2373 |
.use_mqual = 1, |
| 2374 |
.scale_mqual = 1.00, |
| 2375 |
.nm_adjust = 1, |
| 2376 |
.nm_halo = 50, |
| 2377 |
.sc_cost = 60, |
| 2378 |
.low_mqual = 1, |
| 2379 |
.high_mqual = 60, |
| 2380 |
.min_depth = 1, |
| 2381 |
.call_fract = 0.75, |
| 2382 |
.het_fract = 0.5, |
| 2383 |
.het_only = 0, |
| 2384 |
.fmt = FASTA, |
| 2385 |
.cons_cutoff = 10, |
| 2386 |
.ambig = 0, |
| 2387 |
.line_len = 70, |
| 2388 |
.default_qual = 10, |
| 2389 |
.all_bases = 0, |
| 2390 |
.show_del = 0, |
| 2391 |
.show_ins = 1, |
| 2392 |
.mark_ins = 0, |
| 2393 |
.incl_flags = 0, |
| 2394 |
.excl_flags = BAM_FUNMAP | BAM_FSECONDARY | BAM_FQCFAIL | BAM_FDUP, |
| 2395 |
.min_mqual = 0, |
| 2396 |
.P_het = P_HET, |
| 2397 |
.P_indel = P_INDEL, |
| 2398 |
.het_scale = P_HET_SCALE, |
| 2399 |
.homopoly_fix = 0, |
| 2400 |
.homopoly_redux = 0.01, |
| 2401 |
|
| 2402 |
|
| 2403 |
.ks_line = {0,0}, |
| 2404 |
.ks_ins_seq = {0,0}, |
| 2405 |
.ks_ins_qual = {0,0}, |
| 2406 |
.fp = NULL, |
| 2407 |
.fp_out = samtools_stdout, |
| 2408 |
.iter = NULL, |
| 2409 |
.idx = NULL, |
| 2410 |
.last_tid = -1, |
| 2411 |
.last_pos = -1, |
| 2412 |
}; |
| 2413 |
|
| 2414 |
set_qcal(&opts.qcal, QCAL_FLAT); |
| 2415 |
|
| 2416 |
sam_global_args ga = SAM_GLOBAL_ARGS_INIT; |
| 2417 |
static const struct option lopts[] = { |
| 2418 |
SAM_OPT_GLOBAL_OPTIONS('-', 0, 'O', '-', '-', '@'), |
| 2419 |
{"use-qual", no_argument, NULL, 'q'}, |
| 2420 |
{"no-use-qual", no_argument, NULL, 'q'+1000}, |
| 2421 |
{"adj-qual", no_argument, NULL, 'q'+100}, |
| 2422 |
{"no-adj-qual", no_argument, NULL, 'q'+101}, |
| 2423 |
{"use-MQ", no_argument, NULL, 'm'+1000}, |
| 2424 |
{"no-use-MQ", no_argument, NULL, 'm'+1001}, |
| 2425 |
{"adj-MQ", no_argument, NULL, 'm'+100}, |
| 2426 |
{"no-adj-MQ", no_argument, NULL, 'm'+101}, |
| 2427 |
{"NM-halo", required_argument, NULL, 'h'+100}, |
| 2428 |
{"SC-cost", required_argument, NULL, 'h'+101}, |
| 2429 |
{"scale-MQ", required_argument, NULL, 14}, |
| 2430 |
{"low-MQ" , required_argument, NULL, 9}, |
| 2431 |
{"high-MQ", required_argument, NULL, 10}, |
| 2432 |
{"min-depth", required_argument, NULL, 'd'}, |
| 2433 |
{"call-fract", required_argument, NULL, 'c'}, |
| 2434 |
{"het-fract", required_argument, NULL, 'H'}, |
| 2435 |
{"region", required_argument, NULL, 'r'}, |
| 2436 |
{"format", required_argument, NULL, 'f'}, |
| 2437 |
{"cutoff", required_argument, NULL, 'C'}, |
| 2438 |
{"ambig", no_argument, NULL, 'A'}, |
| 2439 |
{"line-len", required_argument, NULL, 'l'}, |
| 2440 |
{"default-qual", required_argument, NULL, 1}, |
| 2441 |
{"het-only", no_argument, NULL, 6}, |
| 2442 |
{"show-del", required_argument, NULL, 7}, |
| 2443 |
{"show-ins", required_argument, NULL, 8}, |
| 2444 |
{"mark-ins", no_argument, NULL, 18}, |
| 2445 |
{"output", required_argument, NULL, 'o'}, |
| 2446 |
{"incl-flags", required_argument, NULL, 11}, |
| 2447 |
{"rf", required_argument, NULL, 11}, |
| 2448 |
{"excl-flags", required_argument, NULL, 12}, |
| 2449 |
{"ff", required_argument, NULL, 12}, |
| 2450 |
{"min-MQ", required_argument, NULL, 13}, |
| 2451 |
{"min-BQ", required_argument, NULL, 16}, |
| 2452 |
{"P-het", required_argument, NULL, 15}, |
| 2453 |
{"P-indel", required_argument, NULL, 17}, |
| 2454 |
{"het-scale", required_argument, NULL, 19}, |
| 2455 |
{"mode", required_argument, NULL, 'm'}, |
| 2456 |
{"homopoly-fix", no_argument, NULL, 'p'}, |
| 2457 |
{"homopoly-score", required_argument, NULL, 'p'+100}, |
| 2458 |
{"homopoly-redux", required_argument, NULL, 'p'+200}, |
| 2459 |
{"qual-calibration", required_argument, NULL, 't'}, |
| 2460 |
{"config", required_argument, NULL, 'X'}, |
| 2461 |
{NULL, 0, NULL, 0} |
| 2462 |
}; |
| 2463 |
|
| 2464 |
while ((c = getopt_long(argc, argv, "@:qd:c:H:r:5f:C:aAl:o:m:pt:X:", |
| 2465 |
lopts, NULL)) >= 0) { |
| 2466 |
switch (c) { |
| 2467 |
case 'a': opts.all_bases++; break; |
| 2468 |
case 'q': opts.use_qual=1; break; |
| 2469 |
case 'q'+1000: opts.use_qual=0; break; |
| 2470 |
case 'm'+1000: opts.use_mqual=1; break; |
| 2471 |
case 'm'+1001: opts.use_mqual=0; break; |
| 2472 |
case 14: opts.scale_mqual = atof(optarg); break; |
| 2473 |
case 9: opts.low_mqual = atoi(optarg); break; |
| 2474 |
case 10: opts.high_mqual = atoi(optarg); break; |
| 2475 |
case 'd': opts.min_depth = atoi(optarg); break; |
| 2476 |
case 'c': opts.call_fract = atof(optarg); break; |
| 2477 |
case 'H': opts.het_fract = atof(optarg); break; |
| 2478 |
case 'r': opts.reg = optarg; break; |
| 2479 |
case 'C': opts.cons_cutoff = atoi(optarg); break; |
| 2480 |
case 'A': opts.ambig = 1; break; |
| 2481 |
case 'p': opts.homopoly_fix = P_HOMOPOLY; break; |
| 2482 |
case 'p'+100: opts.homopoly_fix = atof(optarg); break; |
| 2483 |
case 'p'+200: |
| 2484 |
|
| 2485 |
opts.homopoly_redux = atof(optarg); break; |
| 2486 |
case 1: opts.default_qual = atoi(optarg); break; |
| 2487 |
case 6: opts.het_only = 1; break; |
| 2488 |
case 7: opts.show_del = (*optarg == 'y' || *optarg == 'Y'); break; |
| 2489 |
case 8: opts.show_ins = (*optarg == 'y' || *optarg == 'Y'); break; |
| 2490 |
case 18: opts.mark_ins = 1; break; |
| 2491 |
case 13: opts.min_mqual = atoi(optarg); break; |
| 2492 |
case 16: opts.min_qual = atoi(optarg); break; |
| 2493 |
case 15: opts.P_het = atof(optarg); break; |
| 2494 |
case 17: opts.P_indel = atof(optarg); break; |
| 2495 |
case 19: opts.het_scale = atof(optarg); break; |
| 2496 |
case 'q'+100: opts.adj_qual = 1; break; |
| 2497 |
case 'q'+101: opts.adj_qual = 0; break; |
| 2498 |
case 'm'+100: opts.nm_adjust = 1; break; |
| 2499 |
case 'm'+101: opts.nm_adjust = 0; break; |
| 2500 |
case 'h'+100: opts.nm_halo = atoi(optarg); break; |
| 2501 |
case 'h'+101: opts.sc_cost = atoi(optarg); break; |
| 2502 |
|
| 2503 |
case 'm': |
| 2504 |
if (strcasecmp(optarg, "simple") == 0) { |
| 2505 |
opts.mode = MODE_SIMPLE; |
| 2506 |
} else if (strcasecmp(optarg, "bayesian_m") == 0) { |
| 2507 |
|
| 2508 |
|
| 2509 |
|
| 2510 |
opts.mode = MODE_MIXED; |
| 2511 |
} else if (strcasecmp(optarg, "bayesian_p") == 0) { |
| 2512 |
|
| 2513 |
|
| 2514 |
opts.mode = MODE_PRECISE; |
| 2515 |
} else if (strcasecmp(optarg, "bayesian_r") == 0 || |
| 2516 |
strcasecmp(optarg, "bayesian") == 0) { |
| 2517 |
|
| 2518 |
opts.mode = MODE_RECALL; |
| 2519 |
} else if (strcasecmp(optarg, "bayesian_116") == 0) { |
| 2520 |
opts.mode = MODE_BAYES_116; |
| 2521 |
} else { |
| 2522 |
fprintf(samtools_stderr, "Unknown mode %s\n", optarg); |
| 2523 |
return 1; |
| 2524 |
} |
| 2525 |
break; |
| 2526 |
|
| 2527 |
case 'l': |
| 2528 |
if ((opts.line_len = atoi(optarg)) <= 0) |
| 2529 |
opts.line_len = INT_MAX; |
| 2530 |
break; |
| 2531 |
|
| 2532 |
case 'f': |
| 2533 |
if (strcasecmp(optarg, "fasta") == 0) { |
| 2534 |
opts.fmt = FASTA; |
| 2535 |
} else if (strcasecmp(optarg, "fastq") == 0) { |
| 2536 |
opts.fmt = FASTQ; |
| 2537 |
} else if (strcasecmp(optarg, "pileup") == 0) { |
| 2538 |
opts.fmt = PILEUP; |
| 2539 |
} else { |
| 2540 |
fprintf(samtools_stderr, "Unknown format %s\n", optarg); |
| 2541 |
return 1; |
| 2542 |
} |
| 2543 |
break; |
| 2544 |
|
| 2545 |
case 'o': |
| 2546 |
if (!(opts.fp_out = fopen(optarg, "w"))) { |
| 2547 |
perror(optarg); |
| 2548 |
return 1; |
| 2549 |
} |
| 2550 |
break; |
| 2551 |
|
| 2552 |
case 'X': |
| 2553 |
if (strcasecmp(optarg, "hifi") == 0) { |
| 2554 |
set_qcal(&opts.qcal, QCAL_HIFI); |
| 2555 |
opts.mode = MODE_RECALL; |
| 2556 |
opts.homopoly_fix = 0.3; |
| 2557 |
opts.homopoly_redux = 0.01; |
| 2558 |
opts.low_mqual = 5; |
| 2559 |
opts.scale_mqual = 1.5; |
| 2560 |
opts.het_scale = 0.37; |
| 2561 |
} else if (strcasecmp(optarg, "hiseq") == 0) { |
| 2562 |
opts.mode = MODE_RECALL; |
| 2563 |
set_qcal(&opts.qcal, QCAL_HISEQ); |
| 2564 |
opts.homopoly_redux = 0.01; |
| 2565 |
} else if (strcasecmp(optarg, "r10.4_sup") == 0) { |
| 2566 |
|
| 2567 |
|
| 2568 |
|
| 2569 |
|
| 2570 |
|
| 2571 |
set_qcal(&opts.qcal, QCAL_ONT_R10_4_SUP); |
| 2572 |
opts.mode = MODE_RECALL; |
| 2573 |
opts.homopoly_fix = 0.3; |
| 2574 |
opts.homopoly_redux = 0.01; |
| 2575 |
opts.low_mqual = 5; |
| 2576 |
opts.scale_mqual = 1.5; |
| 2577 |
opts.het_scale = 0.37; |
| 2578 |
|
| 2579 |
|
| 2580 |
|
| 2581 |
|
| 2582 |
|
| 2583 |
} else if (strcasecmp(optarg, "r10.4_dup") == 0) { |
| 2584 |
|
| 2585 |
|
| 2586 |
set_qcal(&opts.qcal, QCAL_ONT_R10_4_DUP); |
| 2587 |
opts.mode = MODE_RECALL; |
| 2588 |
opts.homopoly_fix = 0.3; |
| 2589 |
opts.homopoly_redux = 0.01; |
| 2590 |
opts.low_mqual = 5; |
| 2591 |
opts.scale_mqual = 1.5; |
| 2592 |
opts.het_scale = 0.37; |
| 2593 |
} else if (strcasecmp(optarg, "ultima") == 0) { |
| 2594 |
|
| 2595 |
opts.mode = MODE_RECALL; |
| 2596 |
set_qcal(&opts.qcal, QCAL_ULTIMA); |
| 2597 |
opts.homopoly_fix = 0.3; |
| 2598 |
opts.homopoly_redux = 0.01; |
| 2599 |
opts.het_scale = 0.37; |
| 2600 |
opts.scale_mqual = 2; |
| 2601 |
opts.low_mqual = 10; |
| 2602 |
} else { |
| 2603 |
|
| 2604 |
|
| 2605 |
|
| 2606 |
|
| 2607 |
fprintf(samtools_stderr, "Unrecognised configuration name: \"%s\"\n", |
| 2608 |
optarg); |
| 2609 |
return 1; |
| 2610 |
} |
| 2611 |
break; |
| 2612 |
|
| 2613 |
case 11: |
| 2614 |
if ((opts.incl_flags = bam_str2flag(optarg)) < 0) { |
| 2615 |
print_error("consensus", "could not parse --rf %s", optarg); |
| 2616 |
return 1; |
| 2617 |
} |
| 2618 |
break; |
| 2619 |
case 12: |
| 2620 |
if ((opts.excl_flags = bam_str2flag(optarg)) < 0) { |
| 2621 |
print_error("consensus", "could not parse --ff %s", optarg); |
| 2622 |
return 1; |
| 2623 |
} |
| 2624 |
break; |
| 2625 |
|
| 2626 |
case 't': |
| 2627 |
if (load_qcal(&opts.qcal, optarg) < 0) { |
| 2628 |
print_error("consensus", |
| 2629 |
"failed to load quality calibration '%s'", |
| 2630 |
optarg); |
| 2631 |
return -1; |
| 2632 |
} |
| 2633 |
break; |
| 2634 |
|
| 2635 |
default: if (parse_sam_global_opt(c, optarg, lopts, &ga) == 0) break; |
| 2636 |
|
| 2637 |
case '?': |
| 2638 |
usage_exit(samtools_stderr, EXIT_FAILURE); |
| 2639 |
} |
| 2640 |
} |
| 2641 |
|
| 2642 |
#if 0 |
| 2643 |
|
| 2644 |
int i; |
| 2645 |
qcal_t *q = &opts.qcal; |
| 2646 |
fprintf(samtools_stderr, "{"); |
| 2647 |
for (i = 0; i < 100; i++) |
| 2648 |
fprintf(samtools_stderr, "%2d,%s", q->smap[i],(i+1)%10?" ":"\n"); |
| 2649 |
fprintf(samtools_stderr, "},\n{"); |
| 2650 |
for (i = 0; i < 100; i++) |
| 2651 |
fprintf(samtools_stderr, "%2d,%s", q->umap[i],(i+1)%10?" ":"\n"); |
| 2652 |
fprintf(samtools_stderr, "},\n{"); |
| 2653 |
for (i = 0; i < 100; i++) |
| 2654 |
fprintf(samtools_stderr, "%2d,%s", q->omap[i],(i+1)%10?" ":"\n"); |
| 2655 |
fprintf(samtools_stderr, "}\n"); |
| 2656 |
#endif |
| 2657 |
|
| 2658 |
if (opts.mode != MODE_SIMPLE) { |
| 2659 |
if (opts.mode == MODE_PRECISE) |
| 2660 |
|
| 2661 |
|
| 2662 |
consensus_init(opts.P_het, opts.P_indel, |
| 2663 |
0.3 * opts.het_scale, opts.homopoly_redux, |
| 2664 |
&opts.qcal, MODE_PRECISE, &cons_prob_precise); |
| 2665 |
|
| 2666 |
if (opts.mode == MODE_MIXED) |
| 2667 |
|
| 2668 |
|
| 2669 |
|
| 2670 |
consensus_init(pow(opts.P_het, 0.7), pow(opts.P_indel, 0.7), |
| 2671 |
0.3 * opts.het_scale, opts.homopoly_redux, |
| 2672 |
&opts.qcal, MODE_PRECISE, &cons_prob_precise); |
| 2673 |
|
| 2674 |
|
| 2675 |
consensus_init(opts.P_het, opts.P_indel, opts.het_scale, |
| 2676 |
opts.mode == MODE_RECALL ? opts.homopoly_redux : 0.01, |
| 2677 |
&opts.qcal, MODE_RECALL, &cons_prob_recall); |
| 2678 |
} |
| 2679 |
|
| 2680 |
if (argc != optind+1) { |
| 2681 |
if (argc == optind) usage_exit(samtools_stdout, EXIT_SUCCESS); |
| 2682 |
else usage_exit(samtools_stderr, EXIT_FAILURE); |
| 2683 |
} |
| 2684 |
opts.fp = sam_open_format(argv[optind], "r", &ga.in); |
| 2685 |
if (opts.fp == NULL) { |
| 2686 |
print_error_errno("consensus", "Cannot open input file \"%s\"", |
| 2687 |
argv[optind]); |
| 2688 |
goto err; |
| 2689 |
} |
| 2690 |
if (ga.nthreads > 0) |
| 2691 |
hts_set_threads(opts.fp, ga.nthreads); |
| 2692 |
|
| 2693 |
if (hts_set_opt(opts.fp, CRAM_OPT_DECODE_MD, 0)) { |
| 2694 |
fprintf(samtools_stderr, "Failed to set CRAM_OPT_DECODE_MD value\n"); |
| 2695 |
goto err; |
| 2696 |
} |
| 2697 |
|
| 2698 |
if (!(opts.h = sam_hdr_read(opts.fp))) { |
| 2699 |
fprintf(samtools_stderr, "Failed to read header for \"%s\"\n", argv[optind]); |
| 2700 |
goto err; |
| 2701 |
} |
| 2702 |
|
| 2703 |
if (opts.reg) { |
| 2704 |
opts.idx = sam_index_load(opts.fp, argv[optind]); |
| 2705 |
if (!opts.idx) { |
| 2706 |
print_error("consensus", "Cannot load index for input file \"%s\"", |
| 2707 |
argv[optind]); |
| 2708 |
goto err; |
| 2709 |
} |
| 2710 |
opts.iter = sam_itr_querys(opts.idx, opts.h, opts.reg); |
| 2711 |
if (!opts.iter) { |
| 2712 |
print_error("consensus", "Failed to parse region \"%s\"", |
| 2713 |
opts.reg); |
| 2714 |
goto err; |
| 2715 |
} |
| 2716 |
} |
| 2717 |
|
| 2718 |
if (opts.fmt == PILEUP) { |
| 2719 |
if (pileup_loop(opts.fp, opts.h, readaln2, |
| 2720 |
opts.mode != MODE_SIMPLE ? nm_init : NULL, |
| 2721 |
basic_pileup, |
| 2722 |
opts.mode != MODE_SIMPLE ? nm_free : NULL, |
| 2723 |
&opts) < 0) |
| 2724 |
goto err; |
| 2725 |
|
| 2726 |
if (opts.all_bases) { |
| 2727 |
int tid = opts.iter ? opts.iter->tid : opts.last_tid; |
| 2728 |
int len = sam_hdr_tid2len(opts.h, tid); |
| 2729 |
int pos = opts.last_pos; |
| 2730 |
if (opts.iter) { |
| 2731 |
len = MIN(opts.iter->end, len); |
| 2732 |
pos = MAX(opts.iter->beg, pos); |
| 2733 |
} |
| 2734 |
if (empty_pileup2(&opts, opts.h, tid, pos, len) < 0) |
| 2735 |
goto err; |
| 2736 |
} |
| 2737 |
while (!opts.iter && opts.all_bases > 1 && |
| 2738 |
++opts.last_tid < opts.h->n_targets) { |
| 2739 |
int len = sam_hdr_tid2len(opts.h, opts.last_tid); |
| 2740 |
if (empty_pileup2(&opts, opts.h, opts.last_tid, 0, len) < 0) |
| 2741 |
goto err; |
| 2742 |
} |
| 2743 |
|
| 2744 |
} else { |
| 2745 |
if (pileup_loop(opts.fp, opts.h, readaln2, |
| 2746 |
opts.mode != MODE_SIMPLE ? nm_init : NULL, |
| 2747 |
basic_fasta, |
| 2748 |
opts.mode != MODE_SIMPLE ? nm_free : NULL, |
| 2749 |
&opts) < 0) |
| 2750 |
goto err; |
| 2751 |
|
| 2752 |
next_ref_q: |
| 2753 |
if (opts.all_bases) { |
| 2754 |
|
| 2755 |
int tid = opts.iter ? opts.iter->tid : opts.last_tid; |
| 2756 |
int len = sam_hdr_tid2len(opts.h, tid); |
| 2757 |
int pos = opts.last_pos; |
| 2758 |
if (opts.iter) { |
| 2759 |
len = MIN(opts.iter->end, len); |
| 2760 |
pos = MAX(opts.iter->beg, pos); |
| 2761 |
opts.last_tid = opts.iter->tid; |
| 2762 |
} |
| 2763 |
if (pos < len) { |
| 2764 |
if (ks_expand(&opts.ks_ins_seq, len-pos+1) < 0) |
| 2765 |
goto err; |
| 2766 |
if (ks_expand(&opts.ks_ins_qual, len-pos+1) < 0) |
| 2767 |
goto err; |
| 2768 |
while (pos++ < len) { |
| 2769 |
opts.ks_ins_seq.s [opts.ks_ins_seq.l++] = 'N'; |
| 2770 |
opts.ks_ins_qual.s[opts.ks_ins_qual.l++] = '!'; |
| 2771 |
} |
| 2772 |
opts.ks_ins_seq.s [opts.ks_ins_seq.l] = 0; |
| 2773 |
opts.ks_ins_qual.s[opts.ks_ins_qual.l] = 0; |
| 2774 |
} |
| 2775 |
} |
| 2776 |
if (opts.last_tid >= 0) |
| 2777 |
dump_fastq(&opts, sam_hdr_tid2name(opts.h, opts.last_tid), |
| 2778 |
opts.ks_ins_seq.s, opts.ks_ins_seq.l, |
| 2779 |
opts.ks_ins_qual.s, opts.ks_ins_qual.l); |
| 2780 |
|
| 2781 |
if (!opts.iter && opts.all_bases > 1 && |
| 2782 |
++opts.last_tid < opts.h->n_targets) { |
| 2783 |
opts.last_pos = 0; |
| 2784 |
opts.ks_ins_seq.l = opts.ks_ins_qual.l = 0; |
| 2785 |
goto next_ref_q; |
| 2786 |
} |
| 2787 |
|
| 2788 |
|
| 2789 |
|
| 2790 |
|
| 2791 |
} |
| 2792 |
|
| 2793 |
ret = 0; |
| 2794 |
|
| 2795 |
err: |
| 2796 |
if (opts.iter) |
| 2797 |
hts_itr_destroy(opts.iter); |
| 2798 |
if (opts.idx) |
| 2799 |
hts_idx_destroy(opts.idx); |
| 2800 |
|
| 2801 |
if (opts.fp && sam_close(opts.fp) < 0) { |
| 2802 |
print_error_errno("consensus", "Closing input file \"%s\"", |
| 2803 |
argv[optind]); |
| 2804 |
ret = 1; |
| 2805 |
} |
| 2806 |
|
| 2807 |
if (opts.h) |
| 2808 |
sam_hdr_destroy(opts.h); |
| 2809 |
sam_global_args_free(&ga); |
| 2810 |
|
| 2811 |
if (opts.fp_out && opts.fp_out != samtools_stdout) |
| 2812 |
ret |= fclose(opts.fp_out) != 0; |
| 2813 |
else |
| 2814 |
ret |= fflush(samtools_stdout) != 0; |
| 2815 |
|
| 2816 |
ks_free(&opts.ks_line); |
| 2817 |
ks_free(&opts.ks_ins_seq); |
| 2818 |
ks_free(&opts.ks_ins_qual); |
| 2819 |
|
| 2820 |
if (ret) |
| 2821 |
print_error("consensus", "failed"); |
| 2822 |
|
| 2823 |
return ret; |
| 2824 |
} |