Skip to content

Commit 407310a

Browse files
nh13claude
andauthored
fix: correct BED coordinate system calculation (#103)
BED format uses 0-based, half-open intervals [start, end). Changed length calculation from `end - start + 1` to `end - start`. Files fixed: - regions_bed.c: length calculation and validation warning - dwgsim.c: 3 occurrences of region length calculation Closes #102 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent ae9cd92 commit 407310a

2 files changed

Lines changed: 6 additions & 6 deletions

File tree

src/dwgsim.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ void dwgsim_core(dwgsim_opt_t * opt)
498498
// recalculate the total length
499499
tot_len = 0;
500500
for(i=0;i<regions_bed->n;i++) {
501-
tot_len += regions_bed->end[i] - regions_bed->start[i] + 1;
501+
tot_len += regions_bed->end[i] - regions_bed->start[i]; // BED half-open
502502
}
503503
}
504504
if(NULL != contigs) {
@@ -538,7 +538,7 @@ void dwgsim_core(dwgsim_opt_t * opt)
538538
m = 0;
539539
for(i=0;i<regions_bed->n;i++) {
540540
if(contig_i == regions_bed->contig[i]) {
541-
m += regions_bed->end[i] - regions_bed->start[i] + 1;
541+
m += regions_bed->end[i] - regions_bed->start[i]; // BED half-open
542542
}
543543
}
544544
if(0 == m) {
@@ -692,7 +692,7 @@ void dwgsim_core(dwgsim_opt_t * opt)
692692
// convert in the bed file
693693
for(i=0;i<regions_bed->n;i++) { // TODO: regions are in sorted order... so optimize
694694
if(contig_i == regions_bed->contig[i]) {
695-
j = regions_bed->end[i] - regions_bed->start[i] + 1;
695+
j = regions_bed->end[i] - regions_bed->start[i]; // BED half-open
696696
if(pos < j) {
697697
pos = regions_bed->start[i] + pos - 1; // zero-based
698698
break;

src/regions_bed.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ regions_bed_txt *regions_bed_init(FILE *fp, contigs_t *c)
5252
i = 0;
5353
prev_contig = prev_start = prev_end = -1;
5454
while(0 < fscanf(fp, "%1023s\t%u\t%u", name, &start, &end)) {
55-
len = end - start + 1;
55+
len = end - start; // BED is 0-based, half-open [start, end)
5656
// find the contig
5757
while(i < c->n && 0 != strcmp(name, c->contigs[i].name)) {
5858
i++;
@@ -78,8 +78,8 @@ regions_bed_txt *regions_bed_init(FILE *fp, contigs_t *c)
7878
exit(1);
7979
}
8080

81-
if(end - start + 1 != len) {
82-
fprintf(stderr, "Warning: len != end - start + 1 [%s,%u,%u,%u]\n", name, start, end, len);
81+
if(end - start != len) {
82+
fprintf(stderr, "Warning: len != end - start [%s,%u,%u,%u]\n", name, start, end, len);
8383
}
8484

8585
if(prev_contig == i && start <= prev_end && prev_start <= start) {

0 commit comments

Comments
 (0)