Skip to content

Commit 0a6972e

Browse files
Code Refactoring to minimize dublicate code
1 parent 15808d6 commit 0a6972e

4 files changed

Lines changed: 41 additions & 67 deletions

File tree

biojava-structure/src/main/java/org/biojava/nbio/structure/AminoAcidImpl.java

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
*/
2424
package org.biojava.nbio.structure;
2525

26-
import java.util.List;
27-
2826
/**
2927
* AminoAcid inherits most from Hetatom. Adds a few AminoAcid
3028
* specific methods.
@@ -169,27 +167,9 @@ public Object clone() {
169167
n.setAminoType(getAminoType());
170168
n.setRecordType(recordType);
171169

172-
// copy the atoms
173-
for (Atom atom1 : atoms) {
174-
Atom atom = (Atom) atom1.clone();
175-
n.addAtom(atom);
176-
atom.setGroup(n);
177-
}
178-
//copy the bonds.
179-
for (int i=0;i<atoms.size();i++) {
180-
Atom atom1 = atoms.get(i);
181-
List<Bond> bonds1 = atom1.getBonds();
182-
if (bonds1 != null) {
183-
for (Bond b : bonds1) {
184-
int atomAIndex = atoms.indexOf(b.getAtomA());
185-
int atomBIndex = atoms.indexOf(b.getAtomB());
186-
// The order of the atoms are the same on the original and the cloned object, which we use here.
187-
Bond newBond = new BondImpl(n.getAtom(atomAIndex), n.getAtom(atomBIndex), b.getBondOrder(), false);
188-
n.getAtom(i).addBond(newBond);
189-
}
190-
}
191-
}
192-
170+
//clone atoms and bonds.
171+
cloneAtomsAndBonds(n);
172+
193173
// copying the alt loc groups if present, otherwise they stay null
194174
if (getAltLocs()!=null && !getAltLocs().isEmpty()) {
195175
for (Group altLocGroup:this.getAltLocs()) {

biojava-structure/src/main/java/org/biojava/nbio/structure/HetatomImpl.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -453,13 +453,32 @@ public Object clone() {
453453

454454
n.setPDBName(getPDBName());
455455

456+
//clone atoms and bonds.
457+
cloneAtomsAndBonds(n);
458+
459+
// copying the alt loc groups if present, otherwise they stay null
460+
if (altLocs!=null) {
461+
for (Group altLocGroup:this.altLocs) {
462+
Group nAltLocGroup = (Group)altLocGroup.clone();
463+
n.addAltLoc(nAltLocGroup);
464+
}
465+
}
466+
467+
if (chemComp!=null)
468+
n.setChemComp(chemComp);
469+
470+
return n;
471+
}
472+
473+
474+
protected void cloneAtomsAndBonds(Group newGroup) {
456475
// copy the atoms
457476
for (Atom atom1 : atoms) {
458477
Atom atom = (Atom) atom1.clone();
459-
n.addAtom(atom);
460-
atom.setGroup(n);
478+
newGroup.addAtom(atom);
479+
atom.setGroup(newGroup);
461480
}
462-
//copy the bonds.
481+
// copy the bonds
463482
for (int i=0;i<atoms.size();i++) {
464483
Atom atom1 = atoms.get(i);
465484
List<Bond> bonds1 = atom1.getBonds();
@@ -468,24 +487,11 @@ public Object clone() {
468487
int atomAIndex = atoms.indexOf(b.getAtomA());
469488
int atomBIndex = atoms.indexOf(b.getAtomB());
470489
// The order of the atoms are the same on the original and the cloned object, which we use here.
471-
Bond newBond = new BondImpl(n.getAtom(atomAIndex), n.getAtom(atomBIndex), b.getBondOrder(), false);
472-
n.getAtom(i).addBond(newBond);
490+
Bond newBond = new BondImpl(newGroup.getAtom(atomAIndex), newGroup.getAtom(atomBIndex), b.getBondOrder(), false);
491+
newGroup.getAtom(i).addBond(newBond);
473492
}
474493
}
475494
}
476-
477-
// copying the alt loc groups if present, otherwise they stay null
478-
if (altLocs!=null) {
479-
for (Group altLocGroup:this.altLocs) {
480-
Group nAltLocGroup = (Group)altLocGroup.clone();
481-
n.addAltLoc(nAltLocGroup);
482-
}
483-
}
484-
485-
if (chemComp!=null)
486-
n.setChemComp(chemComp);
487-
488-
return n;
489495
}
490496

491497
/** the Hibernate database ID

biojava-structure/src/main/java/org/biojava/nbio/structure/NucleotideImpl.java

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
*/
2424
package org.biojava.nbio.structure;
2525

26-
import java.util.List;
27-
2826
/**
2927
* A nucleotide group is almost the same as a Hetatm group.
3028
* @see HetatomImpl
@@ -103,26 +101,8 @@ public Object clone() {
103101

104102
n.setPDBName(getPDBName());
105103

106-
// copy the atoms
107-
for (Atom atom1 : atoms) {
108-
Atom atom = (Atom) atom1.clone();
109-
n.addAtom(atom);
110-
atom.setGroup(n);
111-
}
112-
//copy the bonds.
113-
for (int i=0;i<atoms.size();i++) {
114-
Atom atom1 = atoms.get(i);
115-
List<Bond> bonds1 = atom1.getBonds();
116-
if (bonds1 != null) {
117-
for (Bond b : bonds1) {
118-
int atomAIndex = atoms.indexOf(b.getAtomA());
119-
int atomBIndex = atoms.indexOf(b.getAtomB());
120-
// The order of the atoms are the same on the original and the cloned object, which we use here.
121-
Bond newBond = new BondImpl(n.getAtom(atomAIndex), n.getAtom(atomBIndex), b.getBondOrder(), false);
122-
n.getAtom(i).addBond(newBond);
123-
}
124-
}
125-
}
104+
//clone atoms and bonds.
105+
cloneAtomsAndBonds(n);
126106

127107
// copying the alt loc groups if present, otherwise they stay null
128108
if (getAltLocs()!=null && !getAltLocs().isEmpty()) {

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void testBioUnitCloning() throws StructureException, IOException {
9191

9292
/**
9393
* A Structure with alt locs, we make sure they are being cloned too
94-
*
94+
*
9595
* @throws StructureException
9696
* @throws IOException
9797
*/
@@ -161,16 +161,24 @@ public void testBondCloning() throws IOException, StructureException {
161161
cache.setFileParsingParams(params);
162162

163163
final Structure s = cache.getStructure("2I13");
164-
final List<Bond> bonds = s.getNonPolyChain("G").getAtomGroup(0).getAtom(0).getBonds();
164+
List<Bond> bonds = s.getNonPolyChain("G").getAtomGroup(0).getAtom(0).getBonds();
165165
assertNotNull(bonds);
166166

167-
final Structure s2 = s.clone();
168-
final List<Bond> bonds2 = s2.getNonPolyChain("G").getAtomGroup(0).getAtom(0).getBonds();
167+
Structure s2 = s.clone();
168+
List<Bond> bonds2 = s2.getNonPolyChain("G").getAtomGroup(0).getAtom(0).getBonds();
169169
assertNotNull(bonds2);
170170

171171
assertEquals(bonds.toString(), bonds2.toString());
172172
// But the objects should be different as the atoms are clones
173173
assertNotEquals(bonds.toArray(), bonds2.toArray());
174+
175+
// Also test for polymeric chains
176+
bonds = s.getPolyChain("E").getAtomGroup(0).getAtom(0).getBonds();
177+
assertNotNull(bonds);
178+
179+
s2 = s.clone();
180+
bonds2 = s2.getPolyChain("E").getAtomGroup(0).getAtom(0).getBonds();
181+
assertNotNull(bonds2);
174182
}
175183

176184
}

0 commit comments

Comments
 (0)