Skip to content

Commit

Permalink
Merge pull request #1 from daviesrob/0.5.10-mt_fixes
Browse files Browse the repository at this point in the history
0.5.10 mt fixes
  • Loading branch information
Iain Bancarz committed Jan 17, 2013
2 parents 52d4018 + 8348c29 commit c3a60e8
Show file tree
Hide file tree
Showing 36 changed files with 820 additions and 490 deletions.
71 changes: 39 additions & 32 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,38 +54,45 @@ clean:cleanlocal-recur

# DO NOT DELETE

bamlite.o: bamlite.h
bntseq.o: bntseq.h main.h utils.h kseq.h
bwape.o: bwtaln.h bwt.h stdaln.h kvec.h bntseq.h utils.h bwatpx.h kstring.h
bwape.o: khash.h ksort.h
bwape1.o: bwatpx.h bwtaln.h bwt.h stdaln.h bntseq.h kvec.h kstring.h khash.h
bwape1.o: ksort.h
bwape2.o: bwatpx.h bwtaln.h bwt.h stdaln.h bntseq.h kvec.h kstring.h
bwape3.o: bwatpx.h bwtaln.h bwt.h stdaln.h bntseq.h kvec.h kstring.h
bwape4.o: bwatpx.h bwtaln.h bwt.h stdaln.h bntseq.h kvec.h kstring.h
bwapeio1.o: bwatpx.h bwtaln.h bwt.h stdaln.h bntseq.h kvec.h kstring.h
bwapese1.o: bwatpx.h bwtaln.h bwt.h stdaln.h bntseq.h kvec.h kstring.h
bwase.o: stdaln.h bwase.h bntseq.h bwt.h bwtaln.h utils.h kstring.h bwatpx.h
bwase.o: kvec.h
bwase1.o: bwatpx.h bwtaln.h bwt.h stdaln.h bntseq.h kvec.h kstring.h
bwase4.o: bwatpx.h bwtaln.h bwt.h stdaln.h bntseq.h kvec.h kstring.h
bwaseio1.o: bwatpx.h bwtaln.h bwt.h stdaln.h bntseq.h kvec.h kstring.h
bwaseqio.o: bwtaln.h bwt.h stdaln.h utils.h bamlite.h kseq.h
bwt.o: utils.h bwt.h
bwt_lite.o: bwt_lite.h
bwtaln.o: bwtaln.h bwt.h stdaln.h bwtgap.h utils.h
bwtgap.o: bwtgap.h bwt.h bwtaln.h stdaln.h
bamlite.o: bamlite.h utils.h
bntseq.o: bntseq.h kseq.h main.h utils.h
bwape.o: bntseq.h bwatpx.h bwt.h bwtaln.h khash.h ksort.h kstring.h kvec.h
bwape.o: stdaln.h utils.h
bwape1.o: bntseq.h bwatpx.h bwt.h bwtaln.h khash.h ksort.h kstring.h kvec.h
bwape1.o: stdaln.h utils.h
bwape2.o: bntseq.h bwatpx.h bwt.h bwtaln.h kstring.h kvec.h stdaln.h utils.h
bwape3.o: bntseq.h bwatpx.h bwt.h bwtaln.h kstring.h kvec.h stdaln.h utils.h
bwape4.o: bntseq.h bwatpx.h bwt.h bwtaln.h kstring.h kvec.h stdaln.h utils.h
bwapeio1.o: bntseq.h bwatpx.h bwt.h bwtaln.h kstring.h kvec.h stdaln.h
bwapeio1.o: utils.h
bwapese1.o: bntseq.h bwatpx.h bwt.h bwtaln.h kstring.h kvec.h stdaln.h
bwapese1.o: utils.h
bwase.o: bntseq.h bwase.h bwatpx.h bwt.h bwtaln.h kstring.h kvec.h stdaln.h
bwase.o: utils.h
bwase1.o: bntseq.h bwatpx.h bwt.h bwtaln.h kstring.h kvec.h stdaln.h utils.h
bwase4.o: bntseq.h bwatpx.h bwt.h bwtaln.h kstring.h kvec.h stdaln.h utils.h
bwaseio1.o: bntseq.h bwatpx.h bwt.h bwtaln.h kstring.h kvec.h stdaln.h
bwaseio1.o: utils.h
bwaseqio.o: bamlite.h bwt.h bwtaln.h kseq.h stdaln.h utils.h
bwatpx.o: bntseq.h bwt.h bwtaln.h kstring.h kvec.h stdaln.h utils.h
bwt.o: bwt.h utils.h
bwt_lite.o: bwt_lite.h utils.h
bwtaln.o: bwt.h bwtaln.h bwtgap.h stdaln.h utils.h
bwtgap.o: bwt.h bwtaln.h bwtgap.h stdaln.h utils.h
bwtindex.o: bntseq.h bwt.h main.h utils.h
bwtio.o: bwt.h utils.h
bwtmisc.o: bntseq.h utils.h main.h bwt.h
bwtsw2_aux.o: bntseq.h bwt_lite.h utils.h bwtsw2.h bwt.h stdaln.h kstring.h
bwtsw2_aux.o: kseq.h ksort.h
bwtsw2_chain.o: bwtsw2.h bntseq.h bwt_lite.h bwt.h ksort.h
bwtsw2_core.o: bwt_lite.h bwtsw2.h bntseq.h bwt.h kvec.h khash.h ksort.h
bwtsw2_main.o: bwt.h bwtsw2.h bntseq.h bwt_lite.h utils.h
cs2nt.o: bwtaln.h bwt.h stdaln.h
kstring.o: kstring.h
main.o: main.h
simple_dp.o: stdaln.h utils.h kseq.h
stdaln.o: stdaln.h
bwtmisc.o: bntseq.h bwt.h main.h utils.h
bwtsw2.o: bntseq.h bwt.h bwt_lite.h
bwtsw2_aux.o: bntseq.h bwt.h bwt_lite.h bwtsw2.h kseq.h ksort.h kstring.h
bwtsw2_aux.o: stdaln.h utils.h
bwtsw2_chain.o: bntseq.h bwt.h bwt_lite.h bwtsw2.h ksort.h utils.h
bwtsw2_core.o: bntseq.h bwt.h bwt_lite.h bwtsw2.h khash.h ksort.h kvec.h
bwtsw2_core.o: utils.h
bwtsw2_main.o: bntseq.h bwt.h bwt_lite.h bwtsw2.h utils.h
cs2nt.o: bwt.h bwtaln.h stdaln.h utils.h
is.o: utils.h
kstring.o: kstring.h utils.h
main.o: main.h utils.h
simple_dp.o: kseq.h stdaln.h utils.h
stdaln.o: stdaln.h utils.h
utils.o: utils.h
38 changes: 22 additions & 16 deletions bamlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include "utils.h"
#include "bamlite.h"

/*********************
Expand Down Expand Up @@ -53,7 +54,7 @@ int bam_is_be;
bam_header_t *bam_header_init()
{
bam_is_be = bam_is_big_endian();
return (bam_header_t*)calloc(1, sizeof(bam_header_t));
return (bam_header_t*)xcalloc(1, sizeof(bam_header_t));
}

void bam_header_destroy(bam_header_t *header)
Expand All @@ -62,11 +63,11 @@ void bam_header_destroy(bam_header_t *header)
if (header == 0) return;
if (header->target_name) {
for (i = 0; i < header->n_targets; ++i)
free(header->target_name[i]);
if (header->target_name[i]) free(header->target_name[i]);
if (header->target_len) free(header->target_len);
free(header->target_name);
free(header->target_len);
}
free(header->text);
if (header->text) free(header->text);
free(header);
}

Expand All @@ -80,28 +81,33 @@ bam_header_t *bam_header_read(bamFile fp)
magic_len = bam_read(fp, buf, 4);
if (magic_len != 4 || strncmp(buf, "BAM\001", 4) != 0) {
fprintf(stderr, "[bam_header_read] invalid BAM binary header (this is not a BAM file).\n");
return 0;
return NULL;
}
header = bam_header_init();
// read plain text and the number of reference sequences
bam_read(fp, &header->l_text, 4);
if (bam_read(fp, &header->l_text, 4) != 4) goto fail;
if (bam_is_be) bam_swap_endian_4p(&header->l_text);
header->text = (char*)calloc(header->l_text + 1, 1);
bam_read(fp, header->text, header->l_text);
bam_read(fp, &header->n_targets, 4);
header->text = (char*)xcalloc(header->l_text + 1, 1);
if (bam_read(fp, header->text, header->l_text) != header->l_text) goto fail;
if (bam_read(fp, &header->n_targets, 4) != 4) goto fail;
if (bam_is_be) bam_swap_endian_4p(&header->n_targets);
// read reference sequence names and lengths
header->target_name = (char**)calloc(header->n_targets, sizeof(char*));
header->target_len = (uint32_t*)calloc(header->n_targets, 4);
header->target_name = (char**)xcalloc(header->n_targets, sizeof(char*));
header->target_len = (uint32_t*)xcalloc(header->n_targets, 4);
for (i = 0; i != header->n_targets; ++i) {
bam_read(fp, &name_len, 4);
if (bam_read(fp, &name_len, 4) != 4) goto fail;
if (bam_is_be) bam_swap_endian_4p(&name_len);
header->target_name[i] = (char*)calloc(name_len, 1);
bam_read(fp, header->target_name[i], name_len);
bam_read(fp, &header->target_len[i], 4);
header->target_name[i] = (char*)xcalloc(name_len, 1);
if (bam_read(fp, header->target_name[i], name_len) != name_len) {
goto fail;
}
if (bam_read(fp, &header->target_len[i], 4) != 4) goto fail;
if (bam_is_be) bam_swap_endian_4p(&header->target_len[i]);
}
return header;
fail:
bam_header_destroy(header);
return NULL;
}

static void swap_endian_data(const bam1_core_t *c, int data_len, uint8_t *data)
Expand Down Expand Up @@ -146,7 +152,7 @@ int bam_read1(bamFile fp, bam1_t *b)
if (b->m_data < b->data_len) {
b->m_data = b->data_len;
kroundup32(b->m_data);
b->data = (uint8_t*)realloc(b->data, b->m_data);
b->data = (uint8_t*)xrealloc(b->data, b->m_data);
}
if (bam_read(fp, b->data, b->data_len) != b->data_len) return -4;
b->l_aux = b->data_len - c->n_cigar * 4 - c->l_qname - c->l_qseq - (c->l_qseq+1)/2;
Expand Down
9 changes: 5 additions & 4 deletions bamlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

#include <stdint.h>
#include <zlib.h>
#include "utils.h"

typedef gzFile bamFile;
#define bam_open(fn, mode) gzopen(fn, mode)
#define bam_open(fn, mode) xzopen(fn, mode)
#define bam_dopen(fd, mode) gzdopen(fd, mode)
#define bam_close(fp) gzclose(fp)
#define bam_read(fp, buf, size) gzread(fp, buf, size)
#define bam_close(fp) err_gzclose(fp)
#define bam_read(fp, buf, size) err_gzread(fp, buf, size)

typedef struct {
int32_t n_targets;
Expand Down Expand Up @@ -71,7 +72,7 @@ typedef struct {
#define bam1_seqi(s, i) ((s)[(i)/2] >> 4*(1-(i)%2) & 0xf)
#define bam1_aux(b) ((b)->data + (b)->core.n_cigar*4 + (b)->core.l_qname + (b)->core.l_qseq + ((b)->core.l_qseq + 1)/2)

#define bam_init1() ((bam1_t*)calloc(1, sizeof(bam1_t)))
#define bam_init1() ((bam1_t*)xcalloc(1, sizeof(bam1_t)))
#define bam_destroy1(b) do { \
if (b) { free((b)->data); free(b); } \
} while (0)
Expand Down
Loading

0 comments on commit c3a60e8

Please sign in to comment.