Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# glyparse (development version)

* `parse_strucgp_struc()` rejects unmatched depth markers and malformed content instead of silently turning truncated input into valid structures. (#60)
* `parse_pglyco_struc()` and `auto_parse()` reject structures with unclosed pGlyco branches instead of silently accepting truncated input. (#59)
* `parse_iupac_short()` now preserves the base monosaccharide's donor carbon for modified residues, such as C2 for `Neu5Ac9Ac` and `Fru6S`, instead of incorrectly defaulting to C1.

Expand Down
23 changes: 23 additions & 0 deletions src/direct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@ static Record pglyco(const S &x) {
fail("Invalid pGlyco structure");
return r;
}
static void validate_strucgp_markers(const S &x) {
std::vector<char> stack;
for (size_t i = 0; i < x.size();) {
char c = x[i];
if (c >= 'A' && c <= 'Z') {
if ((stack.empty() && i != 0) || i + 1 >= x.size() ||
x[i + 1] < '1' || x[i + 1] > '5')
fail("Invalid StrucGP node");
stack.push_back(c);
i += 2;
} else if (c >= 'a' && c <= 'z') {
if (stack.empty() || c != std::tolower(static_cast<unsigned char>(stack.back())))
fail("Unbalanced StrucGP depth marker");
stack.pop_back();
++i;
} else {
fail("Invalid StrucGP depth marker");
}
}
if (x.empty() || !stack.empty())
fail("Unbalanced StrucGP depth marker");
}
static void strucgp_node(const S &x, Record &r, int parent, int depth = 0) {
if (x.empty())
return;
Expand Down Expand Up @@ -357,6 +379,7 @@ Record parse_direct(const S &x, const S &format, const Vocab &v) {
if (format == "pglyco")
return pglyco(x);
if (format == "strucgp") {
validate_strucgp_markers(x);
Record r;
r.anomer = "??";
strucgp_node(x, r, 0);
Expand Down
4 changes: 0 additions & 4 deletions tests/testthat/test-native.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ test_that("native ports preserve established tokenization and recovery", {
)),
c(NA_character_, "GalNAc(a1-")
)
expect_identical(
as.character(parse_strucgp_struc("A1B1a")),
"Hex(??-?)Hex(??-"
)
expect_identical(
as.character(parse_linucs("[][b-D-Glcp]{[(4+1)][a-D-Glcp\n]{}}")),
"Glc(a1-4)Glc(b1-"
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/test-parse-strucgp.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,26 @@ test_that("StrucGP array assembly preserves branched topology", {
rep("??-?", 8)
)
})

test_that("StrucGP rejects unmatched markers and skipped content", {
invalid <- c(
"A1B1a",
"A1B2bX",
"A1B2b",
"A1B2a",
"A1B2baB3b",
"A1B2bXB3ba"
)
expect_error(parse_strucgp_struc(invalid), "Can't parse")
expect_true(all(is.na(parse_strucgp_struc(invalid, on_failure = "na"))))
expect_true(all(is.na(auto_parse(invalid, on_failure = "na"))))

input <- c(valid = "A1B2bB3ba", invalid = "A1B2bXB3ba", missing = NA)
result <- parse_strucgp_struc(input, on_failure = "na")
expect_identical(names(result), names(input))
expect_identical(unname(is.na(result)), c(FALSE, TRUE, TRUE))
expect_identical(
unname(as.character(result[1])),
as.character(parse_strucgp_struc("A1B2bB3ba"))
)
})
Loading