From 92076781ee057faca37a1bd31ed1b09606244cbe Mon Sep 17 00:00:00 2001
From: Xavier Robin <xavier.robin@unibas.ch>
Date: Wed, 5 Jul 2023 09:51:52 +0200
Subject: [PATCH] test charges in PDB reader

---
 modules/io/tests/test_io_pdb.cc               | 36 +++++++++++++++++++
 modules/io/tests/testfiles/pdb/charge.pdb     |  7 ++++
 .../io/tests/testfiles/pdb/charge_faulty.pdb  |  7 ++++
 3 files changed, 50 insertions(+)
 create mode 100644 modules/io/tests/testfiles/pdb/charge.pdb
 create mode 100644 modules/io/tests/testfiles/pdb/charge_faulty.pdb

diff --git a/modules/io/tests/test_io_pdb.cc b/modules/io/tests/test_io_pdb.cc
index 99071118e..0c479a3ca 100644
--- a/modules/io/tests/test_io_pdb.cc
+++ b/modules/io/tests/test_io_pdb.cc
@@ -1131,4 +1131,40 @@ BOOST_AUTO_TEST_CASE(test_pqr_write_atom)
   BOOST_CHECK_EQUAL(fwriter.IsPQR(), true);
 }
 
+// Charges
+BOOST_AUTO_TEST_CASE(test_parse_charge)
+{
+  Logger::Instance().PushVerbosityLevel(0);
+  String fname("testfiles/pdb/charge.pdb");
+  PDBReader reader(fname, IOProfile());
+  mol::EntityHandle ent=mol::CreateEntity();
+  reader.Import(ent);
+
+  BOOST_CHECK(ent.FindAtom("A", 68, "N").GetCharge() == 1.0);
+  BOOST_CHECK(ent.FindAtom("A", 68, "CA").GetCharge() == 0);
+  BOOST_CHECK(ent.FindAtom("A", 68, "CB").GetCharge() == 0);
+  BOOST_CHECK(ent.FindAtom("A", 68, "CG1").GetCharge() == 0);
+  BOOST_CHECK(ent.FindAtom("A", 68, "CG2").GetCharge() == 4);
+  BOOST_CHECK(ent.FindAtom("A", 68, "O").GetCharge() == -1);
+
+}
+BOOST_AUTO_TEST_CASE(faulty_charges)
+{
+  String fname("testfiles/pdb/charge_faulty.pdb");
+  PDBReader reader(fname, IOProfile());
+  mol::EntityHandle ent=mol::CreateEntity();
+  BOOST_CHECK_THROW(reader.Import(ent), IOException);
+//  IOProfile profile;
+//  profile.fault_tolerant=true;
+//  PDBReader reader2(fname, profile);
+//  reader2.Import(ent);
+//
+//  BOOST_CHECK(ent.FindAtom("A", 68, "N").GetCharge() == 1.0);
+//  BOOST_CHECK(ent.FindAtom("A", 68, "CA").GetCharge() == 0);
+//  BOOST_CHECK(ent.FindAtom("A", 68, "CB").GetCharge() == 0);
+//  BOOST_CHECK(ent.FindAtom("A", 68, "CG1").GetCharge() == 0);
+//  BOOST_CHECK(ent.FindAtom("A", 68, "CG2").GetCharge() == 4);
+//  BOOST_CHECK(ent.FindAtom("A", 68, "O").GetCharge() == -1);
+}
+
 BOOST_AUTO_TEST_SUITE_END();
diff --git a/modules/io/tests/testfiles/pdb/charge.pdb b/modules/io/tests/testfiles/pdb/charge.pdb
new file mode 100644
index 000000000..50cb4d674
--- /dev/null
+++ b/modules/io/tests/testfiles/pdb/charge.pdb
@@ -0,0 +1,7 @@
+ATOM      1  N   VAL A  68     -67.790  -2.746   1.520  1.00120.70       1SG  1+
+ATOM      2  CA  VAL A  68     -68.907  -2.689   2.484  1.00120.70       1SG
+ATOM      3  CB  VAL A  68     -68.374  -2.515   3.872  1.00120.70       1SG  0+
+ATOM      4  CG1 VAL A  68     -67.534  -3.751   4.234  1.00120.70       1SG
+ATOM      5  CG2 VAL A  68     -67.591  -1.194   3.924  1.00120.70       1SG  4+
+ATOM      6  C   VAL A  68     -69.796  -1.526   2.177  1.00120.70       1SG
+ATOM      7  O   VAL A  68     -69.882  -1.095   1.029  1.00120.70       1SG  1-
\ No newline at end of file
diff --git a/modules/io/tests/testfiles/pdb/charge_faulty.pdb b/modules/io/tests/testfiles/pdb/charge_faulty.pdb
new file mode 100644
index 000000000..2fb2446db
--- /dev/null
+++ b/modules/io/tests/testfiles/pdb/charge_faulty.pdb
@@ -0,0 +1,7 @@
+ATOM      1  N   VAL A  68     -67.790  -2.746   1.520  1.00120.70       1SG  1+
+ATOM      2  CA  VAL A  68     -68.907  -2.689   2.484  1.00120.70       1SG
+ATOM      3  CB  VAL A  68     -68.374  -2.515   3.872  1.00120.70       1SG  68
+ATOM      4  CG1 VAL A  68     -67.534  -3.751   4.234  1.00120.70       1SG
+ATOM      5  CG2 VAL A  68     -67.591  -1.194   3.924  1.00120.70       1SG  4+
+ATOM      6  C   VAL A  68     -69.796  -1.526   2.177  1.00120.70       1SG
+ATOM      7  O   VAL A  68     -69.882  -1.095   1.029  1.00120.70       1SG  1-
\ No newline at end of file
-- 
GitLab