From 30f48c398267752648e19f521616789bcd5119a2 Mon Sep 17 00:00:00 2001 From: On behalf of John Marshall Date: Fri, 24 Sep 2010 15:00:19 +0000 Subject: [PATCH] Add bam_validate1(). --- bam.c | 16 ++++++++++++++++ bam.h | 15 +++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/bam.c b/bam.c index 41850ba..521c1dd 100644 --- a/bam.c +++ b/bam.c @@ -305,6 +305,22 @@ void bam_view1(const bam_header_t *header, const bam1_t *b) free(s); } +int bam_validate1(const bam_header_t *header, const bam1_t *b) +{ + char *s; + + if (b->core.tid < -1 || b->core.mtid < -1) return 0; + if (header && (b->core.tid >= header->n_targets || b->core.mtid >= header->n_targets)) return 0; + + if (b->data_len < b->core.l_qname) return 0; + s = memchr(bam1_qname(b), '\0', b->core.l_qname); + if (s != &bam1_qname(b)[b->core.l_qname-1]) return 0; + + // FIXME: Other fields could also be checked, especially the auxiliary data + + return 1; +} + // FIXME: we should also check the LB tag associated with each alignment const char *bam_get_library(bam_header_t *h, const bam1_t *b) { diff --git a/bam.h b/bam.h index 27a9a96..a8769e2 100644 --- a/bam.h +++ b/bam.h @@ -455,6 +455,21 @@ extern "C" { char *bam_format1_core(const bam_header_t *header, const bam1_t *b, int of); + /*! + @abstract Check whether a BAM record is plausibly valid + @param header associated header structure, or NULL if unavailable + @param b alignment to validate + @return 0 if the alignment is invalid; non-zero otherwise + + @discussion Simple consistency check of some of the fields of the + alignment record. If the header is provided, several additional checks + are made. Not all fields are checked, so a non-zero result is not a + guarantee that the record is valid. However it is usually good enough + to detect when bam_seek() has been called with a virtual file offset + that is not the offset of an alignment record. + */ + int bam_validate1(const bam_header_t *header, const bam1_t *b); + const char *bam_get_library(bam_header_t *header, const bam1_t *b); -- 2.39.2