29 if (h == NULL)
return;
43 if (h0 == NULL)
return NULL;
66 static bam_hdr_t *hdr_from_dict(sdict_t *d)
88 int magic_len, has_EOF;
93 perror(
"[W::sam_hdr_read] bgzf_check_EOF");
95 fprintf(stderr,
"[W::%s] EOF marker is absent. The input is probably truncated.\n", __func__);
98 if (magic_len != 4 || strncmp(buf,
"BAM\1", 4)) {
99 if (
hts_verbose >= 1) fprintf(stderr,
"[E::%s] invalid BAM binary header\n", __func__);
116 if (fp->
is_be) ed_swap_4p(&name_len);
130 strncpy(buf,
"BAM\1", 4);
147 name_len = strlen(p) + 1;
149 x = ed_swap_4(name_len);
164 sdict_t *d = (sdict_t*)h->
sdict;
191 free(b->
data); free(b);
197 int m_data = bdst->
m_data;
198 if (m_data < bsrc->l_data) {
200 data = (
uint8_t*)realloc(data, m_data);
212 if (bsrc == NULL)
return NULL;
214 if (bdst == NULL)
return NULL;
221 for (k = l = 0; k < n_cigar; ++k)
230 for (k = l = 0; k < n_cigar; ++k)
244 static inline int aux_type2size(
uint8_t type)
247 case 'A':
case 'c':
case 'C':
251 case 'i':
case 'I':
case 'f':
255 case 'Z':
case 'H':
case 'B':
268 for (i = 0; i < c->
n_cigar; ++i) ed_swap_4p(&cigar[i]);
269 while (s < data + l_data) {
272 size = aux_type2size(*s); ++s;
275 case 2: ed_swap_2p(s); s += 2;
break;
276 case 4: ed_swap_4p(s); s += 4;
break;
277 case 8: ed_swap_8p(s); s += 8;
break;
284 size = aux_type2size(*s); ++s;
285 if (is_host) memcpy(&n, s, 4), ed_swap_4p(s);
286 else ed_swap_4p(s), memcpy(&n, s, 4);
289 case 1: s += n;
break;
290 case 2:
for (i = 0; i < n; ++i, s += 2) ed_swap_2p(s);
break;
291 case 4:
for (i = 0; i < n; ++i, s += 4) ed_swap_4p(s);
break;
292 case 8:
for (i = 0; i < n; ++i, s += 8) ed_swap_8p(s);
break;
304 if ((ret =
bgzf_read(fp, &block_len, 4)) != 4) {
305 if (ret == 0)
return -1;
308 if (
bgzf_read(fp, x, 32) != 32)
return -3;
310 ed_swap_4p(&block_len);
311 for (i = 0; i < 8; ++i) ed_swap_4p(x + i);
313 c->
tid = x[0]; c->
pos = x[1];
314 c->
bin = x[2]>>16; c->
qual = x[2]>>8&0xff; c->
l_qname = x[2]&0xff;
318 b->
l_data = block_len - 32;
332 return 4 + block_len;
350 for (i = 0; i < 8; ++i) ed_swap_4p(x + i);
352 if (ok) ok = (
bgzf_write(fp, ed_swap_4p(&y), 4) >= 0);
355 if (ok) ok = (
bgzf_write(fp, &block_len, 4) >= 0);
360 return ok? 4 + block_len : -1;
377 if (max_len < h->target_len[i]) max_len = h->
target_len[i];
379 for (n_lvls = 0, s = 1<<min_shift; max_len > s; ++n_lvls, s <<= 3);
381 }
else min_shift = 14, n_lvls = 5, fmt =
HTS_FMT_BAI;
409 if ((fp =
hts_open(fn,
"r")) == 0)
return -1;
413 idx = bam_index(fp->
fp.
bgzf, min_shift);
428 static int bam_readrec(
BGZF *fp,
void *ignored,
void *bv,
int *tid,
int *beg,
int *end)
440 static int cram_readrec(
BGZF *ignored,
void *fpv,
void *bv,
int *tid,
int *beg,
int *end)
448 static int sam_bam_cram_readrec(
BGZF *bgzfp,
void *fpv,
void *bv,
int *tid,
int *beg,
int *end)
456 fprintf(stderr,
"[sam_bam_cram_readrec] Not implemented for SAM files -- Exiting\n");
477 if (idx == NULL)
return NULL;
489 if (iter == NULL)
return NULL;
517 fprintf(stderr,
"[cram_itr_query] tid=%d not implemented for CRAM files -- Exiting\n", tid);
529 return hts_itr_query(NULL, tid, beg, end, sam_bam_cram_readrec);
531 return cram_itr_query(idx, tid, beg, end, cram_readrec);
536 static int cram_name2id(
void *fdv,
const char *ref)
546 return hts_itr_querys(idx, region, cram_name2id, cidx->
cram, cram_itr_query, cram_readrec);
560 const char *q, *r, *p;
563 for (p = text; *p; ++p) {
564 if (strncmp(p,
"@SQ", 3) == 0) {
567 for (q = p + 4;; ++q) {
568 if (strncmp(q,
"SN:", 3) == 0) {
570 for (r = q; *r !=
'\t' && *r !=
'\n'; ++r);
571 sn = (
char*)calloc(r - q + 1, 1);
572 strncpy(sn, q, r - q);
574 }
else if (strncmp(q,
"LN:", 3) == 0)
575 ln = strtol(q + 3, (
char**)&q, 10);
576 while (*q !=
'\t' && *q !=
'\n') ++q;
577 if (*q ==
'\n')
break;
583 k =
kh_put(s2i, d, sn, &absent);
586 fprintf(stderr,
"[W::%s] duplicated sequence '%s'\n", __func__, sn);
591 while (*p !=
'\n') ++p;
593 return hdr_from_dict(d);
606 str.
l = str.
m = 0; str.
s = 0;
608 if (fp->
line.
s[0] !=
'@')
break;
609 if (fp->
line.
l > 3 && strncmp(fp->
line.
s,
"@SQ",3) == 0) has_SQ = 1;
613 if (! has_SQ && fp->
fn_aux) {
615 FILE *f = fopen(fp->
fn_aux,
"r");
616 if (f == NULL)
return NULL;
617 while (fgets(line,
sizeof line, f)) {
618 const char *name = strtok(line,
"\t");
619 const char *
length = strtok(NULL,
"\t");
620 ksprintf(&str,
"@SQ\tSN:%s\tLN:%s\n", name, length);
624 if (str.
l == 0) kputsn(
"", 0, &str);
644 p = strstr(h->
text,
"@SQ\t");
665 #define _read_token(_p) (_p); for (; *(_p) && *(_p) != '\t'; ++(_p)); if (*(_p) != '\t') goto err_ret; *(_p)++ = 0
666 #define _read_token_aux(_p) (_p); for (; *(_p) && *(_p) != '\t'; ++(_p)); *(_p)++ = 0 // this is different in that it does not test *(_p)=='\t'
667 #define _get_mem(type_t, _x, _s, _l) ks_resize((_s), (_s)->l + (_l)); *(_x) = (type_t*)((_s)->s + (_s)->l); (_s)->l += (_l)
668 #define _parse_err(cond, msg) do { if ((cond) && hts_verbose >= 1) { fprintf(stderr, "[E::%s] " msg "\n", __func__); goto err_ret; } } while (0)
669 #define _parse_warn(cond, msg) if ((cond) && hts_verbose >= 2) fprintf(stderr, "[W::%s] " msg "\n", __func__)
682 for (i = 0; i < 128; ++i)
689 kputsn_(q, p - q, &str);
692 c->
flag = strtol(p, &p, 0);
693 if (*p++ !=
'\t')
goto err_ret;
696 if (strcmp(q,
"*")) {
699 _parse_warn(c->
tid < 0,
"urecognized reference name; treated as unmapped");
702 c->
pos = strtol(p, &p, 10) - 1;
703 if (*p++ !=
'\t')
goto err_ret;
704 if (c->
pos < 0 && c->
tid >= 0) {
705 _parse_warn(1,
"mapped query cannot have zero coordinate; treated as unmapped");
710 c->
qual = strtol(p, &p, 10);
711 if (*p++ !=
'\t')
goto err_ret;
716 for (q = p; *p && *p !=
'\t'; ++p)
717 if (!isdigit(*p)) ++n_cigar;
718 if (*p++ !=
'\t')
goto err_ret;
719 _parse_err(n_cigar >= 65536,
"too many CIGAR operations");
722 for (i = 0; i < c->
n_cigar; ++i, ++q) {
726 _parse_err(op < 0,
"unrecognized CIGAR operator");
736 c->
bin = hts_reg2bin(c->
pos, c->
pos + i, 14, 5);
739 if (strcmp(q,
"=") == 0) c->
mtid = c->
tid;
740 else if (strcmp(q,
"*") == 0) c->
mtid = -1;
743 c->
mpos = strtol(p, &p, 10) - 1;
744 if (*p++ !=
'\t')
goto err_ret;
746 _parse_warn(1,
"mapped mate cannot have zero coordinate; treated as unmapped");
750 c->
isize = strtol(p, &p, 10);
751 if (*p++ !=
'\t')
goto err_ret;
754 if (strcmp(q,
"*")) {
761 for (i = 0; i < c->
l_qseq; ++i)
767 if (strcmp(q,
"*")) {
769 for (i = 0; i < c->
l_qseq; ++i) t[i] = q[i] - 33;
770 }
else memset(t, 0xff, c->
l_qseq);
774 while (p < s->s + s->
l) {
777 _parse_err(p - q - 1 < 6,
"incomplete aux field");
779 q += 3; type = *q++; ++q;
780 if (type ==
'A' || type ==
'a' || type ==
'c' || type ==
'C') {
783 }
else if (type ==
'i' || type ==
'I') {
785 x = strtol(q, &q, 10);
788 kputc_(
'c', &str); kputc_(x, &str);
791 kputc_(
's', &str); kputsn_((
char*)&y, 2, &str);
794 kputc_(
'i', &str); kputsn_(&y, 4, &str);
798 kputc_(
'C', &str); kputc_(x, &str);
801 kputc_(
'S', &str); kputsn_(&y, 2, &str);
804 kputc_(
'I', &str); kputsn_(&y, 4, &str);
807 }
else if (type ==
'f') {
810 kputc_(
'f', &str); kputsn_(&x, 4, &str);
811 }
else if (type ==
'd') {
814 kputc_(
'd', &str); kputsn_(&x, 8, &str);
815 }
else if (type ==
'Z' || type ==
'H') {
816 kputc_(type, &str);kputsn_(q, p - q, &str);
817 }
else if (type ==
'B') {
820 _parse_err(p - q - 1 < 3,
"incomplete B-typed aux field");
822 for (r = q, n = 0; *r; ++r)
824 kputc_(
'B', &str); kputc_(type, &str); kputsn_(&n, 4, &str);
826 if (type ==
'c')
while (q + 1 < p) {
int8_t x = strtol(q + 1, &q, 0); kputc_(x, &str); }
827 else if (type ==
'C')
while (q + 1 < p) {
uint8_t x = strtoul(q + 1, &q, 0); kputc_(x, &str); }
828 else if (type ==
's')
while (q + 1 < p) {
int16_t x = strtol(q + 1, &q, 0); kputsn_(&x, 2, &str); }
829 else if (type ==
'S')
while (q + 1 < p) {
uint16_t x = strtoul(q + 1, &q, 0); kputsn_(&x, 2, &str); }
830 else if (type ==
'i')
while (q + 1 < p) {
int32_t x = strtol(q + 1, &q, 0); kputsn_(&x, 4, &str); }
831 else if (type ==
'I')
while (q + 1 < p) {
uint32_t x = strtoul(q + 1, &q, 0); kputsn_(&x, 4, &str); }
832 else if (type ==
'f')
while (q + 1 < p) {
float x = strtod(q + 1, &q); kputsn_(&x, 4, &str); }
864 if (fp->
line.
l == 0) {
866 if (ret < 0)
return -1;
872 fprintf(stderr,
"[W::%s] parse error at line %lld\n", __func__, (
long long)fp->
lineno);
887 kputw(c->
flag, str); kputc(
'\t', str);
891 }
else kputsn(
"*\t", 2, str);
892 kputw(c->
pos + 1, str); kputc(
'\t', str);
893 kputw(c->
qual, str); kputc(
'\t', str);
896 for (i = 0; i < c->
n_cigar; ++i) {
900 }
else kputc(
'*', str);
902 if (c->
mtid < 0) kputsn(
"*\t", 2, str);
903 else if (c->
mtid == c->
tid) kputsn(
"=\t", 2, str);
908 kputw(c->
mpos + 1, str); kputc(
'\t', str);
909 kputw(c->
isize, str); kputc(
'\t', str);
912 for (i = 0; i < c->
l_qseq; ++i) kputc(
"=ACMGRSVTWYHKDBN"[
bam_seqi(s, i)], str);
915 if (s[0] == 0xff) kputc(
'*', str);
916 else for (i = 0; i < c->
l_qseq; ++i) kputc(s[i] + 33, str);
917 }
else kputsn(
"*\t*", 3, str);
921 key[0] = s[0]; key[1] = s[1];
923 kputc(
'\t', str); kputsn((
char*)key, 2, str); kputc(
':', str);
925 kputsn(
"A:", 2, str);
928 }
else if (type ==
'C') {
929 kputsn(
"i:", 2, str);
932 }
else if (type ==
'c') {
933 kputsn(
"i:", 2, str);
936 }
else if (type ==
'S') {
938 kputsn(
"i:", 2, str);
942 }
else if (type ==
's') {
944 kputsn(
"i:", 2, str);
948 }
else if (type ==
'I') {
950 kputsn(
"i:", 2, str);
954 }
else if (type ==
'i') {
956 kputsn(
"i:", 2, str);
960 }
else if (type ==
'f') {
966 }
else if (type ==
'd') {
971 }
else if (type ==
'Z' || type ==
'H') {
972 kputc(type, str); kputc(
':', str);
973 while (s < b->data + b->
l_data && *s) kputc(*s++, str);
977 }
else if (type ==
'B') {
984 kputsn(
"B:", 2, str); kputc(sub_type, str);
985 for (i = 0; i < n; ++i) {
987 if (
'c' == sub_type) { kputw(*(
int8_t*)s, str); ++s; }
988 else if (
'C' == sub_type) { kputw(*(
uint8_t*)s, str); ++s; }
989 else if (
's' == sub_type) { kputw(*(
int16_t*)s, str); s += 2; }
990 else if (
'S' == sub_type) { kputw(*(
uint16_t*)s, str); s += 2; }
991 else if (
'i' == sub_type) { kputw(*(
int32_t*)s, str); s += 4; }
992 else if (
'I' == sub_type) { kputuw(*(
uint32_t*)s, str); s += 4; }
993 else if (
'f' == sub_type) {
ksprintf(str,
"%g", *(
float*)s); s += 4; }
1008 kputc(
'\n', &fp->
line);
1027 b->
data[ori_len] = tag[0]; b->
data[ori_len + 1] = tag[1];
1028 b->
data[ori_len + 2] = type;
1029 memcpy(b->
data + ori_len + 3, data, len);
1034 int size = aux_type2size(*s); ++s;
1042 size = aux_type2size(*s); ++s;
1043 memcpy(&n, s, 4); s += 4;
1044 return s + size * n;
1056 int y = tag[0]<<8 | tag[1];
1058 while (s < b->data + b->
l_data) {
1059 int x = (int)s[0]<<8 | s[1];
1061 if (x == y)
return s;
1074 memmove(p, s, l_aux - (s - aux));
1087 else if (type ==
'i' || type ==
'I')
return *(
int32_t*)s;
1095 if (type ==
'd')
return *(
double*)s;
1096 else if (type ==
'f')
return *(
float*)s;
1104 if (type ==
'A')
return *(
char*)s;
1112 if (type ==
'Z' || type ==
'H')
return (
char*)s;
1119 if (format == NULL) {
1121 const char *ext = fn? strrchr(fn,
'.') : NULL;
1122 if (ext == NULL || strchr(ext,
'/'))
return -1;
1125 else if (strcmp(format,
"bam") == 0) strcpy(mode,
"b");
1126 else if (strcmp(format,
"cram") == 0) strcpy(mode,
"c");
1127 else if (strcmp(format,
"sam") == 0) strcpy(mode,
"");
1133 #define STRNCMP(a,b,n) (strncasecmp((a),(b),(n)) || strlen(a)!=(n))
1136 char *end, *beg = (
char*) str;
1137 long int flag = strtol(str, &end, 0);
1138 if ( end!=str )
return flag;
1143 while ( *end && *end!=
',' ) end++;
1178 if ( str.
l == 0 ) kputsn(
"", 0, &str);
1187 #if !defined(BAM_NO_PILEUP)
1199 static cstate_t g_cstate_null = { -1, 0, 0, 0 };
1222 for (k = 0; k < mp->
n; ++k) {
1233 else return mp->
buf[--mp->
n];
1238 if (mp->
n == mp->
max) {
1242 mp->
buf[mp->
n++] = p;
1255 #define _cop(c) ((c)&BAM_CIGAR_MASK)
1256 #define _cln(c) ((c)>>BAM_CIGAR_SHIFT)
1268 for (k = 0, s->
x = c->
pos, s->
y = 0; k < c->n_cigar; ++k) {
1269 int op =
_cop(cigar[k]);
1270 int l =
_cln(cigar[k]);
1275 assert(k < c->n_cigar);
1279 int op, l =
_cln(cigar[s->
k]);
1280 if (pos - s->
x >= l) {
1282 op =
_cop(cigar[s->
k+1]);
1290 for (k = s->
k + 1; k < c->n_cigar; ++k) {
1291 op =
_cop(cigar[k]), l =
_cln(cigar[k]);
1302 op =
_cop(cigar[s->
k]); l =
_cln(cigar[s->
k]);
1304 if (s->
x + l - 1 == pos && s->
k + 1 < c->
n_cigar) {
1305 int op2 =
_cop(cigar[s->
k+1]);
1306 int l2 =
_cln(cigar[s->
k+1]);
1311 for (k = s->
k + 2; k < c->n_cigar; ++k) {
1312 op2 =
_cop(cigar[k]); l2 =
_cln(cigar[k]);
1316 if (l3 > 0) p->
indel = l3;
1320 p->
qpos = s->
y + (pos - s->
x);
1338 struct __bam_plp_t {
1341 int32_t tid, pos, max_tid, max_pos;
1342 int is_eof, max_plp, error, maxcnt;
1349 olap_hash_t *overlaps;
1355 iter = (
bam_plp_t)calloc(1,
sizeof(
struct __bam_plp_t));
1356 iter->mp = mp_init();
1357 iter->head = iter->tail = mp_alloc(iter->mp);
1358 iter->dummy = mp_alloc(iter->mp);
1359 iter->max_tid = iter->max_pos = -1;
1360 iter->maxcnt = 8000;
1371 iter->overlaps =
kh_init(olap_hash);
1376 if ( iter->overlaps )
kh_destroy(olap_hash, iter->overlaps);
1377 mp_free(iter->mp, iter->dummy);
1378 mp_free(iter->mp, iter->head);
1379 if (iter->mp->cnt != 0)
1380 fprintf(stderr,
"[bam_plp_destroy] memory leak: %d. Continue anyway.\n", iter->mp->cnt);
1381 mp_destroy(iter->mp);
1403 static inline int cigar_iref2iseq_set(
uint32_t **cigar,
uint32_t *cigar_max,
int *icig,
int *iseq,
int *iref)
1406 if ( pos < 0 )
return -1;
1410 while ( *cigar<cigar_max )
1415 if ( cig==
BAM_CSOFT_CLIP ) { (*cigar)++; *iseq += ncig; *icig = 0;
continue; }
1420 if ( pos < 0 ) { *icig = ncig + pos; *iseq += *icig; *iref += *icig;
return BAM_CMATCH; }
1421 (*cigar)++; *iseq += ncig; *icig = 0; *iref += ncig;
1424 if ( cig==
BAM_CINS ) { (*cigar)++; *iseq += ncig; *icig = 0;
continue; }
1428 if ( pos<0 ) pos = 0;
1429 (*cigar)++; *icig = 0; *iref += ncig;
1432 fprintf(stderr,
"todo: cigar %d\n", cig);
1438 static inline int cigar_iref2iseq_next(
uint32_t **cigar,
uint32_t *cigar_max,
int *icig,
int *iseq,
int *iref)
1440 while ( *cigar < cigar_max )
1447 if ( *icig >= ncig - 1 ) { *icig = 0; (*cigar)++;
continue; }
1448 (*iseq)++; (*icig)++; (*iref)++;
1451 if ( cig==
BAM_CDEL ) { (*cigar)++; (*iref) += ncig; *icig = 0;
continue; }
1452 if ( cig==
BAM_CINS ) { (*cigar)++; *iseq += ncig; *icig = 0;
continue; }
1453 if ( cig==
BAM_CSOFT_CLIP ) { (*cigar)++; *iseq += ncig; *icig = 0;
continue; }
1455 fprintf(stderr,
"todo: cigar %d\n", cig);
1463 static void tweak_overlap_quality(
bam1_t *a,
bam1_t *b)
1467 int a_icig = 0, a_iseq = 0;
1468 int b_icig = 0, b_iseq = 0;
1473 int a_iref = iref - a->
core.
pos;
1474 int b_iref = iref - b->
core.
pos;
1475 int a_ret = cigar_iref2iseq_set(&a_cigar, a_cigar_max, &a_icig, &a_iseq, &a_iref);
1476 if ( a_ret<0 )
return;
1477 int b_ret = cigar_iref2iseq_set(&b_cigar, b_cigar_max, &b_icig, &b_iseq, &b_iref);
1478 if ( b_ret<0 )
return;
1488 while ( a_iref>=0 && a_iref < iref - a->core.pos )
1489 a_ret = cigar_iref2iseq_next(&a_cigar, a_cigar_max, &a_icig, &a_iseq, &a_iref);
1490 if ( a_ret<0 )
break;
1491 if ( iref < a_iref + a->core.pos ) iref = a_iref + a->
core.
pos;
1493 while ( b_iref>=0 && b_iref < iref - b->core.pos )
1494 b_ret = cigar_iref2iseq_next(&b_cigar, b_cigar_max, &b_icig, &b_iseq, &b_iref);
1495 if ( b_ret<0 )
break;
1496 if ( iref < b_iref + b->core.pos ) iref = b_iref + b->
core.
pos;
1507 int qual = a_qual[a_iseq] + b_qual[b_iseq];
1508 a_qual[a_iseq] = qual>200 ? 200 : qual;
1513 if ( a_qual[a_iseq] >= b_qual[b_iseq] )
1518 a_qual[a_iseq] = 0.8 * a_qual[a_iseq];
1526 b_qual[b_iseq] = 0.8 * b_qual[b_iseq];
1532 fprintf(stderr,
"\n");
1541 if ( !iter->overlaps )
return;
1550 if ( kitr==
kh_end(iter->overlaps) )
1554 kh_value(iter->overlaps, kitr) = node;
1559 tweak_overlap_quality(&a->
b, &node->
b);
1560 kh_del(olap_hash, iter->overlaps, kitr);
1569 if ( !iter->overlaps )
return;
1575 if ( kitr!=
kh_end(iter->overlaps) )
1576 kh_del(olap_hash, iter->overlaps, kitr);
1581 for (kitr =
kh_begin(iter->overlaps); kitr<
kh_end(iter->overlaps); kitr++)
1582 if (
kh_exist(iter->overlaps, kitr) )
kh_del(olap_hash, iter->overlaps, kitr);
1593 if (iter->error) { *_n_plp = -1;
return 0; }
1595 if (iter->is_eof && iter->head->next == 0)
return 0;
1596 while (iter->is_eof || iter->max_tid > iter->tid || (iter->max_tid == iter->tid && iter->max_pos > iter->pos)) {
1600 iter->dummy->next = iter->head;
1601 for (p = iter->head, q = iter->dummy; p->
next; q = p, p = p->
next) {
1603 overlap_remove(iter, &p->
b);
1604 q->
next = p->
next; mp_free(iter->mp, p); p = q;
1605 }
else if (p->
b.
core.
tid == iter->tid && p->
beg <= iter->pos) {
1606 if (n_plp == iter->max_plp) {
1607 iter->max_plp = iter->max_plp? iter->max_plp<<1 : 256;
1610 iter->plp[n_plp].
b = &p->
b;
1611 if (resolve_cigar2(iter->plp + n_plp, iter->pos, &p->
s)) ++n_plp;
1614 iter->head = iter->dummy->next;
1615 *_n_plp = n_plp; *_tid = iter->tid; *_pos = iter->pos;
1617 if (iter->head->next) {
1618 if (iter->tid > iter->head->b.core.tid) {
1619 fprintf(stderr,
"[%s] unsorted input. Pileup aborts.\n", __func__);
1625 if (iter->tid < iter->head->b.core.tid) {
1626 iter->tid = iter->head->b.core.tid; iter->pos = iter->head->beg;
1627 }
else if (iter->pos < iter->head->beg) {
1628 iter->pos = iter->head->beg;
1631 if (n_plp)
return iter->plp;
1632 if (iter->is_eof && iter->head->next == 0)
break;
1639 if (iter->error)
return -1;
1641 if (b->
core.
tid < 0) { overlap_remove(iter, b);
return 0; }
1644 if (iter->tid == b->
core.
tid && iter->pos == b->
core.
pos && iter->mp->cnt > iter->maxcnt)
1646 overlap_remove(iter, b);
1650 overlap_push(iter, iter->tail);
1652 iter->tail->b.id = iter->id++;
1654 iter->tail->beg = b->
core.
pos;
1656 iter->tail->s = g_cstate_null; iter->tail->s.
end = iter->tail->end - 1;
1657 if (b->
core.
tid < iter->max_tid) {
1658 fprintf(stderr,
"[bam_pileup_core] the input is not sorted (chromosomes out of order)\n");
1662 if ((b->
core.
tid == iter->max_tid) && (iter->tail->beg < iter->max_pos)) {
1663 fprintf(stderr,
"[bam_pileup_core] the input is not sorted (reads out of order)\n");
1667 iter->max_tid = b->
core.
tid; iter->max_pos = iter->tail->beg;
1668 if (iter->tail->end > iter->pos || iter->tail->b.core.tid > iter->tid) {
1669 iter->tail->next = mp_alloc(iter->mp);
1670 iter->tail = iter->tail->next;
1672 }
else iter->is_eof = 1;
1679 if (iter->func == 0 || iter->error) { *_n_plp = -1;
return 0; }
1680 if ((plp =
bam_plp_next(iter, _tid, _pos, _n_plp)) != 0)
return plp;
1683 if (iter->is_eof)
return 0;
1685 while ( (ret=iter->func(iter->data, iter->b)) >= 0) {
1690 if ((plp =
bam_plp_next(iter, _tid, _pos, _n_plp)) != 0)
return plp;
1693 if ( ret < -1 ) { iter->error = ret; *_n_plp = -1;
return 0; }
1695 if ((plp =
bam_plp_next(iter, _tid, _pos, _n_plp)) != 0)
return plp;
1703 iter->max_tid = iter->max_pos = -1;
1704 iter->tid = iter->pos = 0;
1706 for (p = iter->head; p->
next;) {
1707 overlap_remove(iter, NULL);
1709 mp_free(iter->mp, p);
1712 iter->head = iter->tail;
1717 iter->maxcnt = maxcnt;
1738 iter->
n_plp = (
int*)calloc(n,
sizeof(
int));
1743 for (i = 0; i < n; ++i) {
1745 iter->
pos[i] = iter->
min;
1753 for (i = 0; i < iter->
n; ++i)
1760 for (i = 0; i < iter->
n; ++i)
1761 iter->
iter[i]->maxcnt = maxcnt;
1768 free(iter->
iter); free(iter->
pos); free(iter->
n_plp); free(iter->
plp);
1776 for (i = 0; i < iter->
n; ++i) {
1777 if (iter->
pos[i] == iter->
min) {
1780 if ( iter->
iter[i]->error )
return -1;
1783 if (iter->
plp[i] && iter->
pos[i] < new_min) new_min = iter->
pos[i];
1785 iter->
min = new_min;
1786 if (new_min == (
uint64_t)-1)
return 0;
1787 *_tid = new_min>>32; *_pos = (
uint32_t)new_min;
1788 for (i = 0; i < iter->
n; ++i) {
1789 if (iter->
pos[i] == iter->
min) {
1790 n_plp[i] = iter->
n_plp[i], plp[i] = iter->
plp[i];
1792 }
else n_plp[i] = 0, plp[i] = 0;
1797 #endif // ~!defined(BAM_NO_PILEUP)