Skip to content

Commit f24019f

Browse files
abradlejosemduarte
authored andcommitted
Added handling of '?' and other non integer string for the charge.
Added a test to ensure this is handled appropriately.
1 parent 22ab7d4 commit f24019f

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

biojava-structure/src/main/java/org/biojava/nbio/structure/io/ChargeAdder.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,21 @@ public static void addCharges(Structure structure) {
5454
ChemComp thisChemComp = ChemCompGroupFactory.getChemComp(g.getPDBName());
5555
List<ChemCompAtom> chemAtoms = thisChemComp.getAtoms();
5656
for(ChemCompAtom chemCompAtom : chemAtoms) {
57-
Atom atom = g.getAtom(chemCompAtom.getAtom_id());
58-
57+
Atom atom = g.getAtom(chemCompAtom.getAtom_id());
5958
String stringCharge = chemCompAtom.getCharge();
6059
short shortCharge = 0;
6160
if (stringCharge!=null){
62-
shortCharge = Short.parseShort(stringCharge);
61+
if(!stringCharge.equals("?")){
62+
try{
63+
shortCharge = Short.parseShort(stringCharge);
64+
}
65+
catch(NumberFormatException e){
66+
logger.warn("Number format exception. Parsing '"+stringCharge+"' to short");
67+
}
68+
}
69+
else{
70+
logger.warn("? charge on atom "+chemCompAtom.getAtom_id()+" in group "+thisChemComp.getId());
71+
}
6372
}
6473
else{
6574
logger.warn("Null charge on atom "+chemCompAtom.getAtom_id()+" in group "+thisChemComp.getId());

biojava-structure/src/test/java/org/biojava/nbio/structure/TestChargeAdder.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,34 @@ public void testBasic() throws IOException, StructureException {
3838
}
3939
}
4040
// Check that the count is as excpected
41-
assertEquals(chargeCount, 425);
41+
assertEquals(425, chargeCount);
4242
}
43+
44+
45+
/**
46+
* Test that it can parse '?' values in the CCD.
47+
* @throws StructureException
48+
* @throws IOException
49+
*/
50+
@Test
51+
public void testQuestionMark() throws IOException, StructureException {
52+
// Get the structure
53+
Structure structure = StructureIO.getStructure("3PE6");
54+
ChargeAdder.addCharges(structure);
55+
// Now count the charges
56+
int chargeCount = 0;
57+
for(Chain chain : structure.getChains()) {
58+
for (Group group : chain.getAtomGroups()) {
59+
for (Atom atom : group.getAtoms()) {
60+
if (atom.getCharge()!=0) {
61+
chargeCount++;
62+
}
63+
}
64+
}
65+
}
66+
assertEquals(39, chargeCount);
67+
}
68+
69+
70+
4371
}

0 commit comments

Comments
 (0)