Skip to content

Commit 22ab7d4

Browse files
abradlejosemduarte
authored andcommitted
Refactored ChargeAdder and fixed bugs where it wasn't adding charges to all groups.
Changed the method to a static. And updated the PDBFileParser and the SimpleMMCifConsumer. Added a basic test to check that charges are being added appropriately.
1 parent 348f91d commit 22ab7d4

File tree

4 files changed

+81
-29
lines changed

4 files changed

+81
-29
lines changed

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

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,49 @@
3434
import org.slf4j.Logger;
3535
import org.slf4j.LoggerFactory;
3636

37+
/**
38+
* A class to add appropriate charge information to a structure.
39+
* @author Anthony Bradley
40+
*
41+
*/
3742
public class ChargeAdder {
3843

3944
private static final Logger logger = LoggerFactory.getLogger(ChargeAdder.class);
4045

41-
private Structure structure;
42-
43-
public ChargeAdder(Structure structure) {
44-
this.structure = structure;
45-
}
46+
/**
47+
* Function to add the charges to a given structure.
48+
*/
49+
public static void addCharges(Structure structure) {
50+
// Loop through the models
51+
for(int i=0; i<structure.nrModels(); i++){
52+
for(Chain c: structure.getChains(i)){
53+
for(Group g: c.getAtomGroups()){
54+
ChemComp thisChemComp = ChemCompGroupFactory.getChemComp(g.getPDBName());
55+
List<ChemCompAtom> chemAtoms = thisChemComp.getAtoms();
56+
for(ChemCompAtom chemCompAtom : chemAtoms) {
57+
Atom atom = g.getAtom(chemCompAtom.getAtom_id());
4658

47-
public void addCharges() {
48-
// Only adds charges to the first MODEL
49-
for(Chain c: structure.getChains()){
50-
for(Group g: c.getAtomGroups()){
51-
ChemComp thisChemComp = ChemCompGroupFactory.getChemComp(g.getPDBName());
52-
List<ChemCompAtom> chemAtoms = thisChemComp.getAtoms();
53-
List<Atom> protAtoms = g.getAtoms();
54-
if(protAtoms.size()!=chemAtoms.size()){
55-
continue;
56-
}
57-
for(int i=0; i<protAtoms.size();i++){
58-
ChemCompAtom cca = chemAtoms.get(i);
59-
Atom a = protAtoms.get(i);
60-
// Get the charge
61-
try {
62-
short charge = Short.parseShort(cca.getCharge());
63-
a.setCharge(charge);
64-
} catch (NumberFormatException e) {
65-
logger.info("Could not parse charge for atom {} of {}. Will set its charge to 0", a.getName(), g.getPDBName());
66-
a.setCharge((short)0);
59+
String stringCharge = chemCompAtom.getCharge();
60+
short shortCharge = 0;
61+
if (stringCharge!=null){
62+
shortCharge = Short.parseShort(stringCharge);
63+
}
64+
else{
65+
logger.warn("Null charge on atom "+chemCompAtom.getAtom_id()+" in group "+thisChemComp.getId());
66+
}
67+
if(atom!=null){
68+
atom.setCharge(shortCharge);
69+
}
70+
// Now do the same for alt locs
71+
for (Group altLoc : g.getAltLocs()) {
72+
Atom altAtom = altLoc.getAtom(chemCompAtom.getAtom_id());
73+
if(altAtom!=null){
74+
altAtom.setCharge(shortCharge);
75+
}
76+
}
6777
}
6878
}
79+
6980
}
7081
}
7182
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2764,8 +2764,7 @@ else if ( params.isParseSecStruc()) {
27642764
}
27652765

27662766
private void addCharges() {
2767-
ChargeAdder adder = new ChargeAdder(structure);
2768-
adder.addCharges();
2767+
ChargeAdder.addCharges(structure);
27692768
}
27702769

27712770
/**

biojava-structure/src/main/java/org/biojava/nbio/structure/io/mmcif/SimpleMMcifConsumer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -992,8 +992,7 @@ private void linkCompounds() {
992992
}
993993

994994
private void addCharges() {
995-
ChargeAdder adder = new ChargeAdder(structure);
996-
adder.addCharges();
995+
ChargeAdder.addCharges(structure);
997996
}
998997

999998
/**
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.biojava.nbio.structure;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.IOException;
6+
7+
import org.biojava.nbio.structure.io.ChargeAdder;
8+
import org.junit.Test;
9+
10+
/**
11+
* Class of functions to test the charge adder.
12+
* @author Anthony Bradley
13+
*
14+
*/
15+
public class TestChargeAdder {
16+
17+
18+
/**
19+
* Test that it works on a very basic level.
20+
* @throws StructureException
21+
* @throws IOException
22+
*/
23+
@Test
24+
public void testBasic() throws IOException, StructureException {
25+
26+
// Get the structure
27+
Structure structure = StructureIO.getStructure("3AAE");
28+
ChargeAdder.addCharges(structure);
29+
// Now count the charges
30+
int chargeCount = 0;
31+
for(Chain chain : structure.getChains()) {
32+
for (Group group : chain.getAtomGroups()) {
33+
for (Atom atom : group.getAtoms()) {
34+
if (atom.getCharge()!=0) {
35+
chargeCount++;
36+
}
37+
}
38+
}
39+
}
40+
// Check that the count is as excpected
41+
assertEquals(chargeCount, 425);
42+
}
43+
}

0 commit comments

Comments
 (0)