Skip to content

Commit 51253f1

Browse files
committed
update to fail on duplicate LN tags, with different LN values
1 parent c6c1d19 commit 51253f1

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

header.c

+12-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ static int sam_hrecs_update_hashes(sam_hrecs_t *hrecs,
145145
const char *name = NULL;
146146
const char *altnames = NULL;
147147
hts_pos_t len = -1;
148-
int r;
148+
int r, dup = 0;
149149
khint_t k;
150150

151151
while (tag) {
@@ -154,7 +154,12 @@ static int sam_hrecs_update_hashes(sam_hrecs_t *hrecs,
154154
name = tag->str+3;
155155
} else if (tag->str[0] == 'L' && tag->str[1] == 'N') {
156156
assert(tag->len >= 3);
157+
hts_pos_t tmp = len;
157158
len = strtoll(tag->str+3, NULL, 10);
159+
if (tmp != -1 && tmp != len) {
160+
//LN again with different value, discard
161+
dup = 1;
162+
}
158163
} else if (tag->str[0] == 'A' && tag->str[1] == 'N') {
159164
assert(tag->len >= 3);
160165
altnames = tag->str+3;
@@ -173,6 +178,12 @@ static int sam_hrecs_update_hashes(sam_hrecs_t *hrecs,
173178
return -1; // LN should be present, according to spec.
174179
}
175180

181+
if (dup) {
182+
hts_log_error("Header includes @SQ line \"%s\" with duplicate LN: tag", \
183+
name);
184+
return -1;
185+
}
186+
176187
// Seen already?
177188
k = kh_get(m_s2i, hrecs->ref_hash, name);
178189
if (k < kh_end(hrecs->ref_hash)) {

sam.c

+16-2
Original file line numberDiff line numberDiff line change
@@ -1911,6 +1911,12 @@ static sam_hdr_t *sam_hdr_sanitise(sam_hdr_t *h) {
19111911
cp[h->l_text] = '\0';
19121912
}
19131913

1914+
if (sam_hdr_fill_hrecs(h) < 0) {
1915+
//failed in parsing / validation
1916+
sam_hdr_destroy(h);
1917+
return NULL;
1918+
}
1919+
19141920
return h;
19151921
}
19161922

@@ -1964,8 +1970,16 @@ static sam_hdr_t *sam_hdr_create(htsFile* fp) {
19641970
strncpy(sn, q, r - q);
19651971
q = r;
19661972
} else {
1967-
if (strncmp(q, "LN:", 3) == 0)
1968-
ln = strtoll(q + 3, (char**)&q, 10);
1973+
if (strncmp(q, "LN:", 3) == 0) {
1974+
hts_pos_t tmp = strtoll(q + 3, (char**)&q, 10);
1975+
if (ln != -1 && tmp != ln) {
1976+
//duplicate LN tag with different value
1977+
hts_log_error("111Header includes @SQ line \"%s\" with \
1978+
duplicate LN: tag", sn);
1979+
goto error;
1980+
}
1981+
ln = tmp;
1982+
}
19691983
}
19701984

19711985
while (*q != '\t' && *q != '\n' && *q != '\0')

0 commit comments

Comments
 (0)