#include <stdio.h>
#include <ctype.h>
+#include <assert.h>
#include "bam.h"
#include "bam_endian.h"
#include "kstring.h"
*/
#include <stdint.h>
-#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
@param ref_id the returned chromosome ID
@param begin the returned start coordinate
@param end the returned end coordinate
+ @return 0 on success; -1 on failure
*/
- void bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *begin, int *end);
+ int bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *begin, int *end);
/*!
@abstract Retrieve data of a tag
return k == kh_end(h)? -1 : kh_value(h, k);
}
-void bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *begin, int *end)
+int bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *begin, int *end)
{
char *s, *p;
int i, l, k;
iter = kh_get(s, h, s); /* get the ref_id */
if (iter == kh_end(h)) { // name not found
*ref_id = -1; free(s);
- return;
+ return -1;
}
*ref_id = kh_value(h, iter);
if (i == k) { /* dump the whole sequence */
*begin = 0; *end = 1<<29; free(s);
- return;
+ return -1;
}
for (p = s + i + 1; i != k; ++i) if (s[i] == '-') break;
*begin = atoi(p);
*end = atoi(p);
} else *end = 1<<29;
if (*begin > 0) --*begin;
- assert(*begin <= *end);
free(s);
+ if (*begin > *end) {
+ fprintf(stderr, "[bam_parse_region] invalid region.\n");
+ return -1;
+ }
+ return 0;
}
int32_t bam_aux2i(const uint8_t *s)
#include <ctype.h>
+#include <assert.h>
#include "bam.h"
#include "khash.h"
#include "ksort.h"
bam_index_t *bam_index_load_local(const char *_fn)
{
- bam_index_t *idx;
FILE *fp;
char *fnidx, *fn;
}
}
free(fnidx); free(fn);
- idx = bam_index_load_core(fp);
- fclose(fp);
- return idx;
+ if (fp) {
+ bam_index_t *idx = bam_index_load_core(fp);
+ fclose(fp);
+ return idx;
+ } else return 0;
}
static void download_from_remote(const char *url)
download_from_remote(fnidx);
idx = bam_index_load_local(fn);
}
+ if (idx == 0) fprintf(stderr, "[bam_index_load] fail to load BAM index.\n");
return idx;
}
FILE *fpidx;
bamFile fp;
bam_index_t *idx;
- assert(fp = bam_open(fn, "r"));
+ if ((fp = bam_open(fn, "r")) == 0) {
+ fprintf(stderr, "[bam_index_build2] fail to open the BAM file.\n");
+ return -1;
+ }
idx = bam_index_core(fp);
bam_close(fp);
if (_fnidx == 0) {
if (fpidx == 0) {
fprintf(stderr, "[bam_index_build2] fail to create the index file.\n");
free(fnidx);
- return 1;
+ return -1;
}
bam_index_save(idx, fpidx);
bam_index_destroy(idx);
#include <stdlib.h>
#include <stdio.h>
+#include <assert.h>
#include "bam.h"
#include "ksort.h"
#include <unistd.h>
+#include <assert.h>
#include <string.h>
#include <ctype.h>
#include "faidx.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
+#include <assert.h>
#include "sam.h"
typedef struct __linkbuf_t {
int ret = 1, is_restart = 1;
if (c->flag&BAM_FUNMAP) return 0; // unmapped read
- assert(x <= pos);
+ assert(x <= pos); // otherwise a bug
p->qpos = -1; p->indel = 0; p->is_del = p->is_head = p->is_tail = 0;
for (k = 0; k < c->n_cigar; ++k) {
int op = bam1_cigar(b)[k] & BAM_CIGAR_MASK; // operation
break;
}
}
- assert(x > pos);
+ assert(x > pos); // otherwise a bug
return ret;
}
buf->func(buf->tid, buf->pos, n_pu, buf->pu, buf->func_data);
}
// update tid and pos
- if (buf->head->next) assert(buf->tid <= buf->head->b.core.tid); // otherwise, not sorted
+ if (buf->head->next) {
+ if (buf->tid > buf->head->b.core.tid) {
+ fprintf(stderr, "[bam_plbuf_push] unsorted input. Pileup aborts.\n");
+ return 1;
+ }
+ }
if (buf->tid < buf->head->b.core.tid) { // come to a new reference sequence
buf->tid = buf->head->b.core.tid; buf->pos = buf->head->beg; // jump to the next reference
} else if (buf->pos < buf->head->beg) { // here: tid == head->b.core.tid
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
-#include <assert.h>
#include <zlib.h>
#include "bam.h"
#include <unistd.h>
+#include <assert.h>
#include "bam.h"
typedef struct {
tview_t *tv = (tview_t*)calloc(1, sizeof(tview_t));
tv->is_dot = 1;
tv->idx = bam_index_load(fn);
+ if (tv->idx == 0) exit(1);
tv->fp = bam_open(fn, "r");
bgzf_set_cache_size(tv->fp, 8 * 1024 *1024);
assert(tv->fp);
#include <stdio.h>
#include <unistd.h>
+#include <assert.h>
#include "bam.h"
#ifndef PACKAGE_VERSION
-#define PACKAGE_VERSION "0.1.4-20 (r365)"
+#define PACKAGE_VERSION "0.1.4-21 (r368)"
#endif
int bam_taf2baf(int argc, char *argv[]);
#include <ctype.h>
-#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
name[l_name++] = c;
}
name[l_name] = '\0';
- assert(ret);
+ if (ret == 0) {
+ fprintf(stderr, "[fai_build_core] the last entry has no sequence\n");
+ free(name); fai_destroy(idx);
+ return 0;
+ }
if (c != '\n') while (razf_read(rz, &c, 1) && c != '\n');
state = 1; len = 0;
offset = razf_tell(rz);
} else {
if (state == 3) {
- fprintf(stderr, "[fai_build_core] inlined empty line is not allowed in sequence '%s'. Abort!\n", name);
- exit(1);
+ fprintf(stderr, "[fai_build_core] inlined empty line is not allowed in sequence '%s'.\n", name);
+ free(name); fai_destroy(idx);
+ return 0;
}
if (state == 2) state = 3;
l1 = l2 = 0;
if (isgraph(c)) ++l2;
} while ((ret = razf_read(rz, &c, 1)) && c != '\n');
if (state == 3 && l2) {
- fprintf(stderr, "[fai_build_core] different line length in sequence '%s'. Abort!\n", name);
- exit(1);
+ fprintf(stderr, "[fai_build_core] different line length in sequence '%s'.\n", name);
+ free(name); fai_destroy(idx);
+ return 0;
}
++l1; len += l2;
if (l2 >= 0x10000) {
- fprintf(stderr, "[fai_build_core] line length exceeds 65535 in sequence '%s'. Abort!\n", name);
- exit(1);
+ fprintf(stderr, "[fai_build_core] line length exceeds 65535 in sequence '%s'.\n", name);
+ free(name); fai_destroy(idx);
+ return 0;
}
if (state == 1) line_len = l1, line_blen = l2, state = 0;
else if (state == 0) {
free(fai);
}
-void fai_build(const char *fn)
+int fai_build(const char *fn)
{
char *str;
RAZF *rz;
str = (char*)calloc(strlen(fn) + 5, 1);
sprintf(str, "%s.fai", fn);
rz = razf_open(fn, "r");
- assert(rz);
+ if (rz == 0) {
+ fprintf(stderr, "[fai_build] fail to open the FASTA file.\n");
+ free(str);
+ return -1;
+ }
fai = fai_build_core(rz);
razf_close(rz);
fp = fopen(str, "w");
- assert(fp);
+ if (fp == 0) {
+ fprintf(stderr, "[fai_build] fail to write FASTA index.\n");
+ fai_destroy(fai); free(str);
+ return -1;
+ }
fai_save(fai, fp);
fclose(fp);
free(str);
fai_destroy(fai);
+ return 0;
}
faidx_t *fai_load(const char *fn)
fai_build(fn);
fp = fopen(str, "r");
if (fp == 0) {
+ fprintf(stderr, "[fai_load] fail to open FASTA index.\n");
free(str);
return 0;
}
fai = fai_read(fp);
fclose(fp);
fai->rz = razf_open(fn, "r");
- if (fai->rz == 0) return 0;
- assert(fai->rz);
free(str);
+ if (fai->rz == 0) {
+ fprintf(stderr, "[fai_load] fail to open FASTA file.\n");
+ return 0;
+ }
return fai;
}
char *s;
faidx_t *fai;
fai = fai_load(argv[1]);
- assert(fai);
+ if (fai == 0) return 1;
for (i = 2; i != argc; ++i) {
printf(">%s\n", argv[i]);
s = fai_fetch(fai, argv[i], &l);
/*!
@abstract Build index for a FASTA or razip compressed FASTA file.
@param fn FASTA file name
+ @return 0 on success; or -1 on failure
@discussion File "fn.fai" will be generated.
*/
- void fai_build(const char *fn);
+ int fai_build(const char *fn);
/*!
@abstract Distroy a faidx_t struct.
h = glf3_header_init();
bgzf_read(fp, magic, 4);
if (strncmp(magic, "GLF\3", 4)) {
- fprintf(stderr, "[glf3_header_read] invalid magic. Abort.\n");
- exit(1);
+ fprintf(stderr, "[glf3_header_read] invalid magic.\n");
+ glf3_header_destroy(h);
+ return 0;
}
bgzf_read(fp, &h->l_text, 4);
if (glf3_is_BE) h->l_text = bam_swap_endian_4(h->l_text);