Skip to content

Commit 461419c

Browse files
authored
Merge pull request biojava#786 from DK-MV/master
Solved biojava#712: Bonds are now also copied by clone()
2 parents 01620a5 + 0a6972e commit 461419c

6 files changed

Lines changed: 112 additions & 66 deletions

File tree

biojava-alignment/src/test/java/org/biojava/nbio/alignment/TestSubOptimalMSA.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ public void gapPenalty52() {
5555
Profile<DNASequence, NucleotideCompound> msa = Alignments
5656
.getMultipleSequenceAlignment(sequences, gapP);
5757

58-
assertEquals("TTGGGGCCTCTAAACGGGGTCTT\n"
59-
+ "TTGGGGCCTCTAAACGGG-TCTT\n"
60-
+ "TTGGGGC-TCTAA-CGGG-TCTT\n",
58+
assertEquals("TTGGGGCCTCTAAACGGGGTCTT" + System.lineSeparator()
59+
+ "TTGGGGCCTCTAAACGGG-TCTT" + System.lineSeparator()
60+
+ "TTGGGGC-TCTAA-CGGG-TCTT" + System.lineSeparator(),
6161
msa.toString());
6262

6363
ConcurrencyTools.shutdown();
@@ -71,9 +71,9 @@ public void gapPenaltyDefault() {
7171
.getMultipleSequenceAlignment(sequences, gapP);
7272

7373
// TODO test not passing (see issue 288 in github) - Aleix 03.2016
74-
assertEquals("TTGGGGCCTCTAAACGGGGTCTT\n"
75-
+ "TTGGGGCCTCTAAACGGG-TCTT\n"
76-
+ "TTGGGGC-TCTAA-CGGG-TCTT\n",
74+
assertEquals("TTGGGGCCTCTAAACGGGGTCTT" + System.lineSeparator()
75+
+ "TTGGGGCCTCTAAACGGG-TCTT" + System.lineSeparator()
76+
+ "TTGGGGC-TCTAA-CGGG-TCTT" + System.lineSeparator(),
7777
msa.toString());
7878

7979
ConcurrencyTools.shutdown();

biojava-alignment/src/test/java/org/biojava/nbio/phylo/TestForesterWrapper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public void testMSAconversion() throws Exception {
7272
String expected = "";
7373
for (ProteinSequence proteinSequence : proteinSequences.values()) {
7474
msa.addAlignedSequence(proteinSequence);
75-
expected += ">" + proteinSequence.getOriginalHeader() + "\n"
76-
+ proteinSequence.toString() + "\n";
75+
expected += ">" + proteinSequence.getOriginalHeader() + System.lineSeparator()
76+
+ proteinSequence.toString() + System.lineSeparator();
7777
}
7878

7979
// Convert the biojava MSA to a FASTA String
@@ -95,8 +95,8 @@ public String getHeader(ProteinSequence sequence) {
9595

9696
StringBuilder sb = new StringBuilder();
9797
for (int i = 0; i < fMsa.getNumberOfSequences(); i++) {
98-
sb.append(">" + fMsa.getIdentifier(i) + "\n");
99-
sb.append(fMsa.getSequenceAsString(i) + "\n");
98+
sb.append(">" + fMsa.getIdentifier(i) + System.lineSeparator());
99+
sb.append(fMsa.getSequenceAsString(i) + System.lineSeparator());
100100
}
101101
String forester = sb.toString();
102102

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,9 @@ public Object clone() {
167167
n.setAminoType(getAminoType());
168168
n.setRecordType(recordType);
169169

170-
// copy the atoms
171-
for (Atom atom1 : atoms) {
172-
Atom atom = (Atom) atom1.clone();
173-
n.addAtom(atom);
174-
atom.setGroup(n);
175-
}
176-
170+
//clone atoms and bonds.
171+
cloneAtomsAndBonds(n);
172+
177173
// copying the alt loc groups if present, otherwise they stay null
178174
if (getAltLocs()!=null && !getAltLocs().isEmpty()) {
179175
for (Group altLocGroup:this.getAltLocs()) {

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

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

454454
n.setPDBName(getPDBName());
455455

456-
// copy the atoms
457-
for (Atom atom1 : atoms) {
458-
Atom atom = (Atom) atom1.clone();
459-
n.addAtom(atom);
460-
atom.setGroup(n);
461-
}
462-
456+
//clone atoms and bonds.
457+
cloneAtomsAndBonds(n);
458+
463459
// copying the alt loc groups if present, otherwise they stay null
464460
if (altLocs!=null) {
465461
for (Group altLocGroup:this.altLocs) {
@@ -474,6 +470,30 @@ public Object clone() {
474470
return n;
475471
}
476472

473+
474+
protected void cloneAtomsAndBonds(Group newGroup) {
475+
// copy the atoms
476+
for (Atom atom1 : atoms) {
477+
Atom atom = (Atom) atom1.clone();
478+
newGroup.addAtom(atom);
479+
atom.setGroup(newGroup);
480+
}
481+
// copy the bonds
482+
for (int i=0;i<atoms.size();i++) {
483+
Atom atom1 = atoms.get(i);
484+
List<Bond> bonds1 = atom1.getBonds();
485+
if (bonds1 != null) {
486+
for (Bond b : bonds1) {
487+
int atomAIndex = atoms.indexOf(b.getAtomA());
488+
int atomBIndex = atoms.indexOf(b.getAtomB());
489+
// The order of the atoms are the same on the original and the cloned object, which we use here.
490+
Bond newBond = new BondImpl(newGroup.getAtom(atomAIndex), newGroup.getAtom(atomBIndex), b.getBondOrder(), false);
491+
newGroup.getAtom(i).addBond(newBond);
492+
}
493+
}
494+
}
495+
}
496+
477497
/** the Hibernate database ID
478498
*
479499
* @return the id

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,9 @@ public Object clone() {
101101

102102
n.setPDBName(getPDBName());
103103

104-
// copy the atoms
105-
for (Atom atom1 : atoms) {
106-
Atom atom = (Atom) atom1.clone();
107-
n.addAtom(atom);
108-
atom.setGroup(n);
109-
}
110-
104+
//clone atoms and bonds.
105+
cloneAtomsAndBonds(n);
106+
111107
// copying the alt loc groups if present, otherwise they stay null
112108
if (getAltLocs()!=null && !getAltLocs().isEmpty()) {
113109
for (Group altLocGroup:this.getAltLocs()) {

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

Lines changed: 69 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@
2424
*/
2525
package org.biojava.nbio.structure;
2626

27+
import static org.junit.Assert.assertEquals;
28+
import static org.junit.Assert.assertNotEquals;
29+
import static org.junit.Assert.assertNotNull;
2730

2831
import java.io.IOException;
2932
import java.util.Iterator;
33+
import java.util.List;
3034

3135
import org.biojava.nbio.structure.align.util.AtomCache;
3236
import org.biojava.nbio.structure.io.FileParsingParameters;
3337
import org.junit.Test;
34-
import static org.junit.Assert.*;
3538

3639
public class TestCloning {
3740

@@ -40,111 +43,142 @@ public void test1a4wCloning() throws StructureException, IOException {
4043

4144
Structure s;
4245

43-
AtomCache cache = new AtomCache();
44-
FileParsingParameters params = new FileParsingParameters();
46+
final AtomCache cache = new AtomCache();
47+
final FileParsingParameters params = new FileParsingParameters();
4548
params.setAlignSeqRes(true);
4649
cache.setFileParsingParams(params);
4750

4851
StructureIO.setAtomCache(cache);
4952

5053
s = StructureIO.getStructure("1a4w");
5154

52-
Structure c = s.clone();
55+
final Structure c = s.clone();
5356

54-
compareCloned(s,c);
57+
compareCloned(s, c);
5558

5659
}
5760

58-
59-
6061
@Test
6162
public void testAsymUnitCloning() throws StructureException, IOException {
6263

6364
Structure s;
6465

65-
66-
AtomCache cache = new AtomCache();
67-
FileParsingParameters params = new FileParsingParameters();
66+
final AtomCache cache = new AtomCache();
67+
final FileParsingParameters params = new FileParsingParameters();
6868
params.setAlignSeqRes(false);
6969
cache.setFileParsingParams(params);
7070

7171
StructureIO.setAtomCache(cache);
7272

7373
s = StructureIO.getStructure("1stp");
7474

75-
Structure c = s.clone();
75+
final Structure c = s.clone();
7676

77-
compareCloned(s,c);
77+
compareCloned(s, c);
7878
}
7979

8080
@Test
8181
public void testBioUnitCloning() throws StructureException, IOException {
8282

8383
Structure s;
84-
s = StructureIO.getBiologicalAssembly("1stp",1);
84+
s = StructureIO.getBiologicalAssembly("1stp", 1);
8585

86-
Structure c = s.clone();
86+
final Structure c = s.clone();
8787

88-
compareCloned(s,c);
88+
compareCloned(s, c);
8989

9090
}
9191

9292
/**
9393
* A Structure with alt locs, we make sure they are being cloned too
94+
*
9495
* @throws StructureException
9596
* @throws IOException
9697
*/
9798
@Test
9899
public void test3piuCloning() throws StructureException, IOException {
99100

100-
AtomCache cache = new AtomCache();
101-
FileParsingParameters params = new FileParsingParameters();
101+
final AtomCache cache = new AtomCache();
102+
final FileParsingParameters params = new FileParsingParameters();
102103
params.setAlignSeqRes(true);
103104
cache.setFileParsingParams(params);
104105

105106
StructureIO.setAtomCache(cache);
106107

107-
Structure s = StructureIO.getStructure("3piu");
108+
final Structure s = StructureIO.getStructure("3piu");
108109

109-
Structure c = s.clone();
110+
final Structure c = s.clone();
110111

111112
compareCloned(s, c);
112113
}
113114

114-
private void compareCloned(Structure s, Structure c) throws StructureException {
115+
private void compareCloned(final Structure s, final Structure c) throws StructureException {
115116

116117
assertEquals(s.getChains().size(), c.getChains().size());
117118

118-
for ( Chain chain : s.getChains()) {
119+
for (final Chain chain : s.getChains()) {
119120

120-
Chain test = c.getChain(chain.getId());
121+
final Chain test = c.getChain(chain.getId());
121122

122-
assertEquals("Could not correctly clone seqres for chain " + chain.getId() , chain.getSeqResLength(),test.getSeqResLength());
123+
assertEquals("Could not correctly clone seqres for chain " + chain.getId(), chain.getSeqResLength(),
124+
test.getSeqResLength());
123125

124-
assertEquals("Could not correctly clone atom records for chain " + chain.getId() , chain.getAtomLength(),test.getAtomLength());
126+
assertEquals("Could not correctly clone atom records for chain " + chain.getId(), chain.getAtomLength(),
127+
test.getAtomLength());
125128

126129
Iterator<Group> it = test.getAtomGroups().iterator();
127-
for (Group g : chain.getAtomGroups()) {
128-
Group testGroup = it.next();
129-
//if (g.hasAltLoc()) {
130-
// System.out.println(g.toString());
131-
//}
130+
for (final Group g : chain.getAtomGroups()) {
131+
final Group testGroup = it.next();
132+
// if (g.hasAltLoc()) {
133+
// System.out.println(g.toString());
134+
// }
132135
assertEquals(g.getAltLocs().size(), testGroup.getAltLocs().size());
133136
}
134-
137+
135138
it = test.getSeqResGroups().iterator();
136-
for (Group g: chain.getSeqResGroups()) {
137-
Group testGroup = it.next();
139+
for (final Group g : chain.getSeqResGroups()) {
140+
final Group testGroup = it.next();
138141
assertEquals(g.getAltLocs().size(), testGroup.getAltLocs().size());
139142
}
140143
}
141144

142-
Atom[] allAtoms = StructureTools.getAllAtomArray(s);
145+
final Atom[] allAtoms = StructureTools.getAllAtomArray(s);
146+
147+
final Atom[] allAtomsCloned = StructureTools.getAllAtomArray(c);
148+
149+
assertEquals(allAtoms.length, allAtomsCloned.length);
150+
151+
}
152+
153+
@Test
154+
public void testBondCloning() throws IOException, StructureException {
155+
156+
final AtomCache cache = new AtomCache();
157+
cache.setUseMmCif(true);
158+
159+
final FileParsingParameters params = cache.getFileParsingParams();
160+
params.setCreateAtomBonds(true);
161+
cache.setFileParsingParams(params);
162+
163+
final Structure s = cache.getStructure("2I13");
164+
List<Bond> bonds = s.getNonPolyChain("G").getAtomGroup(0).getAtom(0).getBonds();
165+
assertNotNull(bonds);
166+
167+
Structure s2 = s.clone();
168+
List<Bond> bonds2 = s2.getNonPolyChain("G").getAtomGroup(0).getAtom(0).getBonds();
169+
assertNotNull(bonds2);
143170

144-
Atom[] allAtomsCloned = StructureTools.getAllAtomArray(c);
171+
assertEquals(bonds.toString(), bonds2.toString());
172+
// But the objects should be different as the atoms are clones
173+
assertNotEquals(bonds.toArray(), bonds2.toArray());
145174

146-
assertEquals(allAtoms.length,allAtomsCloned.length);
175+
// Also test for polymeric chains
176+
bonds = s.getPolyChain("E").getAtomGroup(0).getAtom(0).getBonds();
177+
assertNotNull(bonds);
147178

179+
s2 = s.clone();
180+
bonds2 = s2.getPolyChain("E").getAtomGroup(0).getAtom(0).getBonds();
181+
assertNotNull(bonds2);
148182
}
149183

150184
}

0 commit comments

Comments
 (0)