From c26d8c6ba4f46941ca4ca56591ef6c0ee657c472 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 14 Nov 2026 11:54:02 +0100 Subject: [PATCH 1/2] rugnux: a PDB model is recognised whatever record it starts with The positional-input classifier knew a PDB file by its first record, from a short list (HEADER, TITLE, REMARK, CRYST1, ATOM, ...). A model written without a title section starts with whatever comes first in the format - rugnux's own refined model starts with its HELIX or LINK records - and was taken for an image file, so `rugnux --mode refine ` ended in "Several inputs are read together in --mode mx only" while the same file worked under --model. The list is now every record type the format puts before the coordinates. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK --- rugnux/RugnuxInputKind.cpp | 12 +++++++++--- tests/RugnuxInputKindTest.cpp | 13 ++++++++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/rugnux/RugnuxInputKind.cpp b/rugnux/RugnuxInputKind.cpp index 565ead494..547a38888 100644 --- a/rugnux/RugnuxInputKind.cpp +++ b/rugnux/RugnuxInputKind.cpp @@ -21,9 +21,15 @@ const char *RugnuxInputKindName(RugnuxInputKind kind) { } namespace { - // The record names a PDB file starts with (its first line that is not blank) - constexpr const char *PDB_RECORDS[] = {"HEADER", "TITLE ", "COMPND", "SOURCE", "KEYWDS", "EXPDTA", "AUTHOR", - "REVDAT", "REMARK", "SEQRES", "CRYST1", "MODEL ", "ATOM ", "HETATM"}; + // The records a PDB file can start with (its first line that is not blank): every record type of the format + // that precedes the coordinates. A file written by a program starts with whatever that program has to say - + // a refined model with no title section starts with its HELIX or LINK records. + constexpr const char *PDB_RECORDS[] = {"HEADER", "OBSLTE", "TITLE ", "SPLIT ", "CAVEAT", "COMPND", "SOURCE", + "KEYWDS", "EXPDTA", "NUMMDL", "MDLTYP", "AUTHOR", "REVDAT", "SPRSDE", + "JRNL ", "REMARK", "DBREF ", "DBREF1", "DBREF2", "SEQADV", "SEQRES", + "MODRES", "HET ", "HETNAM", "HETSYN", "FORMUL", "HELIX ", "SHEET ", + "SSBOND", "LINK ", "LINKR ", "CISPEP", "SITE ", "CRYST1", "ORIGX1", + "SCALE1", "MTRIX1", "MODEL ", "ATOM ", "HETATM"}; // The rule, fed one line at a time; done() once the answer cannot change. class Sniffer { diff --git a/tests/RugnuxInputKindTest.cpp b/tests/RugnuxInputKindTest.cpp index 7eb42fc4f..78779e4f2 100644 --- a/tests/RugnuxInputKindTest.cpp +++ b/tests/RugnuxInputKindTest.cpp @@ -45,13 +45,24 @@ TEST_CASE("RugnuxInputKind_Classify", "[rugnux_inputs]") { CHECK(ClassifyRugnuxInput(Write("ik_image2.cbf", "data_image\n_array_data.data\n_atom_site.x\n")) == K::Images); CHECK(ClassifyRugnuxInput(Write("ik_image.img", "{\nHEADER_BYTES= 512;\nDIM=2;\n}")) == K::Images); CHECK(ClassifyRugnuxInput(Write("ik_binary", std::string("\0\1\2\3\4\5\6\7\0\0\0\0\0\0\0\0x", 17))) == K::Images); + // A PDB file need not start with a title section: a refined model written without one starts with its + // HELIX or LINK records (the first line decides the format, the atom decides the model) + const std::string ATOM = "ATOM 1 CA ALA A 1 1.000 2.000 3.000 1.00 10.00 C\nEND\n"; + CHECK(ClassifyRugnuxInput(Write("ik_link_first.pdb", + "LINK SG CYS A 14 CAB HEC A 201 1555 1555 1.82\n" + ATOM)) == K::Model); + CHECK(ClassifyRugnuxInput(Write("ik_helix_first.pdb", + "HELIX 1 1 VAL A 4 LYS A 14 0\nCRYST1 50.000 50.000 50.000 90.00 90.00 90.00 P 1\n" + ATOM)) + == K::Model); + CHECK(ClassifyRugnuxInput(Write("ik_ssbond_first.pdb", + "SSBOND 1 CYS A 6 CYS A 127 1555 1555 2.03\n" + ATOM)) == K::Model); // A PDB file without an atom is not a model CHECK(ClassifyRugnuxInput(Write("ik_noatom.pdb", "HEADER TEST\nEND\n")) == K::Images); // Both coordinates and reflections: the user has to say CHECK(ClassifyRugnuxInput(Write("ik_both.cif", MODEL_CIF + "loop_\n_refln.index_h\n1\n")) == K::Ambiguous); for (const char *f: {"ik_model.cif", "ik_sf.cif", "ik_diffrn.cif", "ik_model.pdb", "ik_model.pdb.gz", "ik_sf.cif.gz", "ik_model_named.mtz", "ik_mtz_named.pdb", "ik_image.h5", "ik_image.cbf", "ik_image2.cbf", - "ik_image.img", "ik_binary", "ik_noatom.pdb", "ik_both.cif"}) + "ik_image.img", "ik_binary", "ik_noatom.pdb", "ik_both.cif", "ik_link_first.pdb", + "ik_helix_first.pdb", "ik_ssbond_first.pdb"}) std::filesystem::remove(f); } From 1db6a7d00c5080178f9582c6c7eee411793e699c Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 14 Nov 2026 11:54:02 +0100 Subject: [PATCH 2/2] Model output: records that describe the model written Three things wrong with the PDB/mmCIF a refinement wrote: - A LINK record read with the model was written back whatever the atoms do in the refined model. The records are never restraints here (a record the dictionary has no link for is ignored by prepare_topology, and the bond count with and without them is the same), but the next program takes each as a bond: phenix.refine made four "custom bonds" of a placed model's clashes between NCS copies and wrote them as LINKs at 0.7 A, and rugnux carried them on to its output at 4-5 A. A record whose two atoms are farther than 3.5 A apart in the model written (longer than any bond, a metal's included) is now left out, with a log line; hydrogen-bond records stay. This also drops a metal's outer-sphere records that AddMetalLinks refused as bonds, which otherwise reach the next program as bonds. - A HELIX record's blank length (columns 72-76) was read as 0 by the vendored gemmi PDB reader and written back as 0, which phenix.model_vs_data refuses ("Bad helix length"). Blank now stays "not given" (-1), as Helix::length means it, and is written blank. - The REMARK 3 TLS block was inserted before CRYST1, i.e. after HELIX and LINK; it now follows the title section, where the format has it. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK --- gemmi_gph/pdb.cpp | 4 +++- tests/ModelValidationTest.cpp | 45 +++++++++++++++++++++++++++++++++++ writer/WriteModel.cpp | 35 +++++++++++++++++++++++++-- 3 files changed, 81 insertions(+), 3 deletions(-) diff --git a/gemmi_gph/pdb.cpp b/gemmi_gph/pdb.cpp index cca0e8a98..9cd8359bb 100644 --- a/gemmi_gph/pdb.cpp +++ b/gemmi_gph/pdb.cpp @@ -1009,7 +1009,9 @@ void populate_structure_from_pdb_stream(AnyStream& line_reader, const std::strin helix.end.chain_name = read_string(line+30, 2); helix.end.res_id = read_res_id(line+33, line+27); helix.set_helix_class_as_int(read_int(line+38, 2)); - if (len > 72) + // a blank length field (columns 72-76) is "not given", as Helix::length = -1 says; read_int() would + // make it 0, which the next writer then puts on the record and a reader checks against the residues + if (len > 72 && std::string(line+72, std::min(len, 77) - 72).find_first_not_of(" \r\n") != std::string::npos) helix.length = read_int(line+72, 5); st.helices.emplace_back(helix); diff --git a/tests/ModelValidationTest.cpp b/tests/ModelValidationTest.cpp index 07432ebbb..01e7dda0f 100644 --- a/tests/ModelValidationTest.cpp +++ b/tests/ModelValidationTest.cpp @@ -852,6 +852,51 @@ TEST_CASE("WriteModel_KeepsTheDataGroupWhenItIsASubgroup", "[ModelValidation]") std::filesystem::remove("write_model_sub_test_model.pdb"); } +// The records written beside the coordinates describe them. A LINK whose two atoms are nowhere near each +// other in the model written - a bond a program made of a clash in a placed model, a metal's outer-sphere +// contact - claims a bond the next program would restrain, and is left out; one at bonding distance stays. A +// HELIX record's blank length stays blank: read as 0 and written back as 0, it made phenix refuse the file. +TEST_CASE("WriteModel_RecordsDescribeTheModelWritten", "[ModelValidation]") { + Logger logger("WriteModel_RecordsDescribeTheModelWritten"); + const char *pdb = + "HELIX 1 1 CYS A 1 ALA A 3 1\n" + "LINK SG CYS A 1 SG CYS A 2 1555 1555 2.03\n" + "LINK SG CYS A 1 CA ALA A 3 1555 1555 6.00\n" + "CRYST1 40.000 50.000 60.000 90.00 90.00 90.00 P 1 1\n" + "ATOM 1 SG CYS A 1 10.000 10.000 10.000 1.00 20.00 S\n" + "ATOM 2 SG CYS A 2 12.030 10.000 10.000 1.00 20.00 S\n" + "ATOM 3 CA ALA A 3 16.000 10.000 10.000 1.00 20.00 C\n" + "END\n"; + const auto input = WriteTemp("write_model_records_input.pdb", pdb); + const gemmi::Structure st = gemmi::read_structure_gz(input, gemmi::CoorFormat::Detect); + REQUIRE(st.connections.size() == 2); + REQUIRE(st.helices.size() == 1); + CHECK(st.helices[0].length == -1); + + const UnitCell cell{.a = 40, .b = 50, .c = 60, .alpha = 90, .beta = 90, .gamma = 90}; + const gemmi::SpaceGroup *p1 = gemmi::find_spacegroup_by_name("P 1"); + REQUIRE(p1 != nullptr); + WritePlacedModel(st, cell, *p1, "write_model_records", logger); + + for (const char *written : {"write_model_records_model.pdb", "write_model_records_model.cif"}) { + REQUIRE(std::filesystem::exists(written)); + const gemmi::Structure back = gemmi::read_structure_gz(written, gemmi::CoorFormat::Detect); + REQUIRE(back.connections.size() == 1); + CHECK(back.connections[0].partner2.res_id.seqid.num == 2); + REQUIRE(back.helices.size() == 1); + CHECK(back.helices[0].length == -1); + } + std::string line; + std::ifstream in("write_model_records_model.pdb"); + while (std::getline(in, line) && line.rfind("HELIX", 0) != 0) + ; + REQUIRE(line.size() >= 76); + CHECK(line.substr(71, 5) == " "); + + for (const char *f : {"write_model_records_input.pdb", "write_model_records_model.pdb", "write_model_records_model.cif"}) + std::filesystem::remove(f); +} + // CC(model, data) has to follow where the signal actually is, or it cannot support the one-sided // claim it exists for. The check is closed: the "observed" intensities are the model's own with // Gaussian noise added, and how much noise is chosen per shell - almost none in the first, some in diff --git a/writer/WriteModel.cpp b/writer/WriteModel.cpp index 932a70366..665762aa1 100644 --- a/writer/WriteModel.cpp +++ b/writer/WriteModel.cpp @@ -28,6 +28,28 @@ bool coordinates_obey(const gemmi::SpaceGroup &coordinates_group, const gemmi::S label_ops.begin(), label_ops.end()); } +// Farther apart than this, two atoms are bonded to nothing: the longest bonds a LINK can describe are a metal's +// to its ligands (K-O, Cs-O: up to ~3.3 A). +constexpr double LONGEST_BOND = 3.5; + +// A LINK / struct_conn record claims a bond, and the next program (REFMAC, phenix.refine) restrains it as one +// whether or not the atoms are anywhere near each other - which is how a record a model arrived with pulls two +// atoms together it was never meant to join: a bond phenix made of a clash in a placed model and wrote out, or a +// metal's outer-sphere contact that refinement left as a contact (AddMetalLinks). The records are a statement +// about the coordinates written beside them, so one whose two atoms are not within bonding distance in this +// model is left out. Hydrogen-bond records make no such claim and stay. +void DropUnbondedRecords(gemmi::Structure &st) { + const gemmi::Model &model = st.first_model(); + std::erase_if(st.connections, [&](const gemmi::Connection &c) { + if (c.type == gemmi::Connection::Hydrog) + return false; + const gemmi::const_CRA a = model.find_cra(c.partner1, true), b = model.find_cra(c.partner2, true); + if (!a.atom || !b.atom) + return false; + return st.cell.find_nearest_image(a.atom->pos, b.atom->pos, c.asu).dist() > LONGEST_BOND; + }); +} + // `placed` in `cell`, labelled with `space_group` wherever its coordinates obey it (see WritePlacedModel). // takes_model_group: the model's own group was kept instead. gemmi::Structure LabelledModel(const gemmi::Structure &placed, const UnitCell &cell, @@ -50,6 +72,7 @@ gemmi::Structure LabelledModel(const gemmi::Structure &placed, const UnitCell &c st.cell = cell; st.spacegroup_hm = (takes_model_group ? *model_group : space_group).xhm(); st.setup_cell_images(); + DropUnbondedRecords(st); return st; } @@ -91,7 +114,11 @@ std::string WithTlsRemarks(const gemmi::Structure &st, const std::string &pdb) { line(fmt::format(" S{}1: {:8.4f} S{}2: {:8.4f} S{}3: {:8.4f}", i + 1, g.S[i][0], i + 1, g.S[i][1], i + 1, g.S[i][2])); } - // before the crystal's record; the model's own REMARK 3 left out + // where the format has its remarks, after the title section and before everything else (gemmi writes + // HELIX and LINK before CRYST1, and a REMARK among them is out of order); the model's own REMARK 3 left out + constexpr const char *TITLE_SECTION[] = {"HEADER", "OBSLTE", "TITLE ", "SPLIT ", "CAVEAT", "COMPND", "SOURCE", + "KEYWDS", "EXPDTA", "NUMMDL", "MDLTYP", "AUTHOR", "REVDAT", "SPRSDE", + "JRNL ", "REMARK"}; std::istringstream in(pdb); std::ostringstream out; std::string text; @@ -99,7 +126,8 @@ std::string WithTlsRemarks(const gemmi::Structure &st, const std::string &pdb) { while (std::getline(in, text)) { if (text.rfind("REMARK 3", 0) == 0) continue; - if (!written && (text.rfind("CRYST1", 0) == 0 || text.rfind("ATOM", 0) == 0 || text.rfind("HETATM", 0) == 0)) { + if (!written && std::none_of(std::begin(TITLE_SECTION), std::end(TITLE_SECTION), + [&](const char *rec) { return text.rfind(rec, 0) == 0; })) { out << r.str(); written = true; } @@ -125,6 +153,9 @@ void WritePlacedModel(const gemmi::Structure &placed, bool takes_model_group = false; gemmi::Structure st = LabelledModel(placed, cell, space_group, takes_model_group); const gemmi::SpaceGroup &label = takes_model_group ? *st.find_spacegroup() : space_group; + if (st.connections.size() < placed.connections.size()) + logger.Info("Model validation: {} LINK record(s) whose atoms are farther than {} A apart in this model " + "left out of {}", placed.connections.size() - st.connections.size(), LONGEST_BOND, cif_path); // Fills in entity types and label_asym_id for a model read from a PDB, which carries neither. // Both are no-ops where the input already had them, i.e. for an mmCIF input. // setup_entities and make_mmcif_document can both throw, and this runs BEFORE the reflection files