Skip to content

Commit 721f980

Browse files
committed
Changed *_parse_region functions to use 64-bit coords.
1 parent c680ecd commit 721f980

7 files changed

Lines changed: 27 additions & 21 deletions

File tree

faidx.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -730,10 +730,12 @@ static char *fai_retrieve(const faidx_t *fai, const faidx1_t *val,
730730
return s;
731731
}
732732

733-
static int fai_get_val(const faidx_t *fai, const char *str, int *len, faidx1_t *val, long *fbeg, long *fend) {
733+
static int fai_get_val(const faidx_t *fai, const char *str,
734+
int *len, faidx1_t *val, int64_t *fbeg, int64_t *fend) {
734735
khiter_t iter;
735736
khash_t(s) *h;
736-
int id, beg, end;
737+
int id;
738+
int64_t beg, end;
737739

738740
if (!fai_parse_region(fai, str, &id, &beg, &end, 0)) {
739741
hts_log_warning("Reference %s not found in FASTA file, returning empty sequence", str);
@@ -765,7 +767,7 @@ static int fai_get_val(const faidx_t *fai, const char *str, int *len, faidx1_t *
765767
char *fai_fetch(const faidx_t *fai, const char *str, int *len)
766768
{
767769
faidx1_t val;
768-
long beg, end;
770+
int64_t beg, end;
769771

770772
if (fai_get_val(fai, str, len, &val, &beg, &end)) {
771773
return NULL;
@@ -778,7 +780,7 @@ char *fai_fetch(const faidx_t *fai, const char *str, int *len)
778780

779781
char *fai_fetchqual(const faidx_t *fai, const char *str, int *len) {
780782
faidx1_t val;
781-
long beg, end;
783+
int64_t beg, end;
782784

783785
if (fai_get_val(fai, str, len, &val, &beg, &end)) {
784786
return NULL;
@@ -878,7 +880,8 @@ int faidx_has_seq(const faidx_t *fai, const char *seq)
878880
return 1;
879881
}
880882

881-
const char *fai_parse_region(const faidx_t *fai, const char *s, int *tid, int *beg, int *end, int flags)
883+
const char *fai_parse_region(const faidx_t *fai, const char *s,
884+
int *tid, int64_t *beg, int64_t *end, int flags)
882885
{
883886
return hts_parse_region(s, tid, beg, end, (hts_name2id_f)fai_name2id, (void *)fai, flags);
884887
}

hts.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,7 +2686,7 @@ static void *hts_memrchr(const void *s, int c, size_t n) {
26862686
* beg/end will be set, plus tid if getid has been supplied.
26872687
* On failure NULL is returned.
26882688
*/
2689-
const char *hts_parse_region(const char *s, int *tid, int *beg, int *end,
2689+
const char *hts_parse_region(const char *s, int *tid, int64_t *beg, int64_t *end,
26902690
hts_name2id_f getid, void *hdr, int flags)
26912691
{
26922692
if (!s || !tid || !beg || !end || !getid)
@@ -2861,7 +2861,8 @@ const char *hts_parse_reg(const char *s, int *beg, int *end)
28612861

28622862
hts_itr_t *hts_itr_querys(const hts_idx_t *idx, const char *reg, hts_name2id_f getid, void *hdr, hts_itr_query_func *itr_query, hts_readrec_func *readrec)
28632863
{
2864-
int tid, beg, end;
2864+
int tid;
2865+
int64_t beg, end;
28652866

28662867
if (strcmp(reg, ".") == 0)
28672868
return itr_query(idx, HTS_IDX_START, 0, 0, readrec);

htslib/faidx.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#ifndef HTSLIB_FAIDX_H
3030
#define HTSLIB_FAIDX_H
3131

32+
#include <inttypes.h>
3233
#include "hts_defs.h"
3334

3435
#ifdef __cplusplus
@@ -234,7 +235,7 @@ int faidx_seq_len(const faidx_t *fai, const char *seq);
234235
@param flags Parsing method, see HTS_PARSE_* in hts.h.
235236
@return pointer to end of parsed s if successs, NULL if not.
236237
*/
237-
const char *fai_parse_region(const faidx_t *fai, const char *s, int *tid, int *beg, int *end, int flags);
238+
const char *fai_parse_region(const faidx_t *fai, const char *s, int *tid, int64_t *beg, int64_t *end, int flags);
238239

239240
#ifdef __cplusplus
240241
}

htslib/hts.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ const char *hts_parse_reg(const char *str, int *beg, int *end);
819819
@return Pointer to the colon or '\0' after the reference sequence name,
820820
or NULL if @a str could not be parsed.
821821
*/
822-
const char *hts_parse_region(const char *str, int *tid, int *beg, int *end,
822+
const char *hts_parse_region(const char *str, int *tid, int64_t *beg, int64_t *end,
823823
hts_name2id_f getid, void *hdr, int flags);
824824

825825

htslib/sam.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ typedef struct {
281281
int bam_hdr_write(BGZF *fp, const bam_hdr_t *h) HTS_RESULT_USED;
282282
void bam_hdr_destroy(bam_hdr_t *h);
283283
int bam_name2id(bam_hdr_t *h, const char *ref);
284-
const char *sam_parse_region(bam_hdr_t *h, const char *s, int *tid, int *beg, int *end, int flags);
284+
const char *sam_parse_region(bam_hdr_t *h, const char *s, int *tid, int64_t *beg, int64_t *end, int flags);
285285
bam_hdr_t* bam_hdr_dup(const bam_hdr_t *h0);
286286

287287
bam1_t *bam_init1(void);

sam.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ int bam_name2id(bam_hdr_t *h, const char *ref)
298298
return k == kh_end(d)? -1 : kh_val(d, k);
299299
}
300300

301-
const char *sam_parse_region(bam_hdr_t *h, const char *s, int *tid, int *beg, int *end, int flags) {
301+
const char *sam_parse_region(bam_hdr_t *h, const char *s, int *tid, int64_t *beg, int64_t *end, int flags) {
302302
return hts_parse_region(s, tid, beg, end, (hts_name2id_f)bam_name2id, h, flags);
303303
}
304304

test/test-parse-reg.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,17 @@
4141
#include <string.h>
4242
#include <stdlib.h>
4343
#include <limits.h>
44+
#include <stdint.h>
45+
#include <inttypes.h>
4446

4547
#include <htslib/hts.h>
4648
#include <htslib/sam.h>
4749

4850
void reg_expected(bam_hdr_t *hdr, const char *reg, int flags,
49-
char *reg_exp, int tid_exp, int beg_exp, int end_exp) {
51+
char *reg_exp, int tid_exp, int64_t beg_exp, int64_t end_exp) {
5052
const char *reg_out;
51-
int tid_out = -1, beg_out = -1, end_out = -1;
53+
int tid_out = -1;
54+
int64_t beg_out = -1, end_out = -1;
5255

5356
reg_out = sam_parse_region(hdr, reg, &tid_out, &beg_out, &end_out, flags);
5457

@@ -57,16 +60,12 @@ void reg_expected(bam_hdr_t *hdr, const char *reg, int flags,
5760
(reg_exp && tid_out != tid_exp) ||
5861
(reg_exp && beg_out != beg_exp) ||
5962
(reg_exp && end_out != end_exp)) {
60-
fprintf(stderr, "Parsing \"%s\" expected return \"%s\", %d:%d-%d, "
61-
"but got \"%s\", %d:%d-%d\n",
63+
fprintf(stderr, "Parsing \"%s\" expected return \"%s\", %d:%"PRId64"-%"PRId64", "
64+
"but got \"%s\", %d:%"PRId64"-%"PRId64"\n",
6265
reg,
6366
reg_exp?reg_exp:"(null)", tid_exp, beg_exp, end_exp,
6467
reg_out?reg_out:"(null)", tid_out, beg_out, end_out);
6568
exit(1);
66-
// } else {
67-
// fprintf(stderr, "%s parsed as expected result: \"%s\" %d:%d-%d\n",
68-
// reg,
69-
// reg_out?reg_out:"(null)", tid_out, beg_out, end_out);
7069
}
7170
}
7271

@@ -88,6 +87,7 @@ int reg_test(char *fn) {
8887
// 5 chr1,chr3
8988

9089
// Check range extensions.
90+
// NB: fix INT_MAX once we merge the 64-bit pos code.
9191
reg_expected(hdr, "chr1", 0, "", 0, 0, INT_MAX);
9292
reg_expected(hdr, "chr1:50", 0, "", 0, 49, INT_MAX);
9393
reg_expected(hdr, "chr1:50", HTS_PARSE_ONE_COORD, "", 0, 49, 50);
@@ -186,13 +186,14 @@ int main(int argc, char **argv) {
186186

187187
const char *reg = argv[2];
188188
while (*reg) {
189-
int tid, beg, end;
189+
int tid;
190+
int64_t beg, end;
190191
reg = sam_parse_region(hdr, reg, &tid, &beg, &end, flags);
191192
if (!reg) {
192193
fprintf(stderr, "Failed to parse region\n");
193194
exit(1);
194195
}
195-
printf("%-20s %12d %12d\n",
196+
printf("%-20s %12"PRId64" %12"PRId64"\n",
196197
tid == -1 ? "*" : hdr->target_name[tid],
197198
beg, end);
198199
}

0 commit comments

Comments
 (0)