diff --git a/header.c b/header.c index d83b2b866..02283f603 100644 --- a/header.c +++ b/header.c @@ -1,5 +1,5 @@ /* -Copyright (c) 2018-2020, 2023 Genome Research Ltd. +Copyright (c) 2018-2020, 2023, 2025 Genome Research Ltd. Authors: James Bonfield , Valeriu Ohan Redistribution and use in source and binary forms, with or without @@ -145,7 +145,7 @@ static int sam_hrecs_update_hashes(sam_hrecs_t *hrecs, const char *name = NULL; const char *altnames = NULL; hts_pos_t len = -1; - int r; + int r, invLN = 0; khint_t k; while (tag) { @@ -154,7 +154,11 @@ static int sam_hrecs_update_hashes(sam_hrecs_t *hrecs, name = tag->str+3; } else if (tag->str[0] == 'L' && tag->str[1] == 'N') { assert(tag->len >= 3); + hts_pos_t tmp = len; len = strtoll(tag->str+3, NULL, 10); + if (tmp != -1 && tmp != len) { //duplicate and different LN + invLN = 1; + } } else if (tag->str[0] == 'A' && tag->str[1] == 'N') { assert(tag->len >= 3); altnames = tag->str+3; @@ -173,6 +177,12 @@ static int sam_hrecs_update_hashes(sam_hrecs_t *hrecs, return -1; // LN should be present, according to spec. } + if (invLN) { + hts_log_error("Header includes @SQ line \"%s\" with multiple LN:" + " tag with different values.", name); + return -1; // LN should not be duplicated or be same + } + // Seen already? k = kh_get(m_s2i, hrecs->ref_hash, name); if (k < kh_end(hrecs->ref_hash)) { diff --git a/sam.c b/sam.c index ce5833bf7..62282adcd 100644 --- a/sam.c +++ b/sam.c @@ -1,6 +1,6 @@ /* sam.c -- SAM and BAM file I/O and manipulation. - Copyright (C) 2008-2010, 2012-2024 Genome Research Ltd. + Copyright (C) 2008-2010, 2012-2025 Genome Research Ltd. Copyright (C) 2010, 2012, 2013 Broad Institute. Author: Heng Li @@ -1964,8 +1964,16 @@ static sam_hdr_t *sam_hdr_create(htsFile* fp) { strncpy(sn, q, r - q); q = r; } else { - if (strncmp(q, "LN:", 3) == 0) - ln = strtoll(q + 3, (char**)&q, 10); + if (strncmp(q, "LN:", 3) == 0) { + hts_pos_t tmp = strtoll(q + 3, (char**)&q, 10); + if (ln != -1 && ln != tmp) { //duplicate & different LN + hts_log_error("Header includes @SQ line \"%s\" with" + " multiple LN: tag with different values.", sn); + goto error; + } else { + ln = tmp; + } + } } while (*q != '\t' && *q != '\n' && *q != '\0')