Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions 2bit.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ char *twobitSequence(TwoBit *tb, char *chrom, uint32_t start, uint32_t end) {
break;
}
}
if(tid == 0 && strcmp(tb->cl->chrom[i], chrom) != 0) return NULL;
if(tid == 0 && strcmp(tb->cl->chrom[tid], chrom) != 0) return NULL;

//Get the start/end if not specified
if(start == end && end == 0) {
Expand Down Expand Up @@ -406,7 +406,7 @@ void *twobitBases(TwoBit *tb, char *chrom, uint32_t start, uint32_t end, int fra
}
}

if(tid == 0 && strcmp(tb->cl->chrom[i], chrom) != 0) return NULL;
if(tid == 0 && strcmp(tb->cl->chrom[tid], chrom) != 0) return NULL;

//Get the start/end if not specified
if(start == end && end == 0) {
Expand Down Expand Up @@ -517,7 +517,7 @@ void twobitIndexRead(TwoBit *tb, int storeMasked) {
for(i=0; i<tb->hdr->nChroms; i++) {
if(idx->nBlockSizes[i]) free(idx->nBlockSizes[i]);
}
free(idx->nBlockSizes[i]);
free(idx->nBlockSizes);
}

if(idx->maskBlockCount) free(idx->maskBlockCount);
Expand Down Expand Up @@ -587,13 +587,15 @@ void twobitChromListRead(TwoBit *tb) {
TwoBitCL *cl = calloc(1, sizeof(TwoBitCL));
uint8_t useLong = tb->hdr->version == 1;
size_t offsetSz = (useLong) ? sizeof(uint64_t) : sizeof(uint32_t);
void *offset = calloc(1, offsetSz);

//Allocate cl and do error checking
if(!cl) goto error;
cl->chrom = calloc(tb->hdr->nChroms, sizeof(char*));
cl->offset = calloc(tb->hdr->nChroms, offsetSz);
cl->offset = calloc(tb->hdr->nChroms, sizeof(uint64_t));
if(!cl->chrom) goto error;
if(!cl->offset) goto error;
if(!offset) goto error;

for(i=0; i<tb->hdr->nChroms; i++) {
//Get the string size (not null terminated!)
Expand All @@ -607,8 +609,14 @@ void twobitChromListRead(TwoBit *tb) {
str = NULL;

//Read in the size
if(twobitRead(cl->offset + i, offsetSz, 1, tb) != 1) goto error;
if(twobitRead(offset, offsetSz, 1, tb) != 1) goto error;
if(useLong) {
cl->offset[i] = *((uint64_t*) offset);
} else {
cl->offset[i] = *((uint32_t*) offset);
}
}
free(offset);

tb->cl = cl;
return;
Expand All @@ -625,6 +633,7 @@ void twobitChromListRead(TwoBit *tb) {
}
free(cl);
}
if(offset) free(offset);
}

void twobitChromListDestroy(TwoBit *tb) {
Expand Down
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
1.0.1:
- Fix segfault in version 0 (32-bit) files

1.0.0:
- Added support for version 1 (64-bit) files

Expand Down
4 changes: 2 additions & 2 deletions test/exampleRead.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ int main(int argc, char *argv[]) {

char *seq;
seq = twobitSequence(tb, "chr1", 0, 0);
printf("%s\n", seq);
if(seq) printf("%s\n", seq);
if(seq) free(seq);

seq = twobitSequence(tb, "chr1", 24, 74);
printf("%s\n", seq);
if(seq) printf("%s\n", seq);
if(seq) free(seq);

double *stats;
Expand Down
Loading