Skip to content

Commit ae19c8c

Browse files
committed
Organize the MMTF tests
1 parent 8934e94 commit ae19c8c

File tree

9 files changed

+3449
-53
lines changed

9 files changed

+3449
-53
lines changed

biojava-structure/src/main/java/org/biojava/nbio/structure/io/mmtf/MmtfStructureReader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
* Should be ported to biojava code.
4848
*
4949
* @author Anthony Bradley
50+
* @since 5.0
51+
*
5052
*/
5153
public class MmtfStructureReader implements StructureAdapterInterface, Serializable {
5254

biojava-structure/src/main/java/org/biojava/nbio/structure/io/mmtf/MmtfStructureWriter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
import org.rcsb.mmtf.dataholders.MmtfStructure;
2020

2121
/**
22-
* Class to take Biojava structure data and covert to the DataApi for encoding.
22+
* Class to take Biojava structure data and covert to the DataApi for encoding.
2323
* Must implement all the functions in {@link StructureAdapterInterface}.
24+
*
2425
* @author Anthony Bradley
26+
* @since 5.0
2527
*
2628
*/
2729
public class MmtfStructureWriter {
2830

29-
3031
private StructureAdapterInterface mmtfDecoderInterface;
3132

3233
/**

biojava-structure/src/test/java/org/biojava/nbio/structure/io/mmtf/TestBondFinding.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,46 @@
2121

2222
/**
2323
* Test bond finding in BioJava
24+
*
2425
* @author Anthony Bradley
2526
*
2627
*/
2728
public class TestBondFinding {
2829

2930
/**
30-
* Test that the bonds we are finding is consistenty.
31+
* Test that the bonds we are finding are consistent.
32+
*
3133
* @throws IOException
3234
* @throws StructureException
3335
*/
3436
@Test
3537
public void testInterGroupBonds() throws IOException, StructureException {
38+
3639
// Normal
3740
assertEquals(2236, getInterBonds("1QMZ"));
41+
3842
// Disulphide
3943
assertEquals(956, getInterBonds("2QWO"));
44+
4045
// Covalent ligand
4146
assertEquals(2294, getInterBonds("4QDV"));
47+
4248
// DNA
4349
assertEquals(22, getInterBonds("4XSN"));
4450

4551
}
4652

4753
/**
48-
* Find all of the inter group bonds in a structure
54+
* Find all of the inter group bonds in a structure.
55+
*
4956
* @param pdbId the pdb id of the structure to determine
5057
* @return the number of inter group bonds (double counted) in a structure
5158
* @throws IOException
5259
* @throws StructureException
5360
*/
54-
public int getInterBonds(String pdbId) throws IOException, StructureException{
61+
public int getInterBonds(String pdbId) throws IOException, StructureException {
62+
63+
// Download parameters
5564
AtomCache cache = new AtomCache();
5665
cache.setUseMmCif(true);
5766
cache.setFetchBehavior(FetchBehavior.FETCH_FILES);
@@ -64,10 +73,13 @@ public int getInterBonds(String pdbId) throws IOException, StructureException{
6473
dcc.checkDoFirstInstall();
6574
cache.setFileParsingParams(params);
6675
StructureIO.setAtomCache(cache);
67-
int counter =0;
68-
// Now get the structure
76+
77+
// Get the structure
6978
Structure newStruc = StructureIO.getStructure(pdbId);
70-
// Now loop through the atoms
79+
80+
int counter =0;
81+
82+
// Loop through the atoms and count the bonds
7183
for(Chain c: newStruc.getChains()){
7284
for(Group g: c.getAtomGroups()){
7385
List<Atom> theseAtoms = g.getAtoms();

biojava-structure/src/test/java/org/biojava/nbio/structure/io/mmtf/TestMmtfPerformance.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
11
package org.biojava.nbio.structure.io.mmtf;
22

33
import org.biojava.nbio.structure.Structure;
4-
import org.biojava.nbio.structure.StructureIO;
5-
import org.biojava.nbio.structure.TestStructureCrossReferences;
64
import org.biojava.nbio.structure.io.PDBFileParser;
7-
import org.biojava.nbio.structure.io.mmcif.AllChemCompProvider;
8-
import org.biojava.nbio.structure.io.mmcif.ChemCompGroupFactory;
9-
import org.biojava.nbio.structure.io.mmcif.ChemCompProvider;
105
import org.junit.Test;
11-
import org.rcsb.mmtf.dataholders.MmtfStructure;
12-
import org.rcsb.mmtf.decoder.ReaderUtils;
136
import org.slf4j.Logger;
147
import org.slf4j.LoggerFactory;
158

169
import java.io.*;
1710
import java.net.URL;
18-
import java.nio.file.Files;
19-
import java.nio.file.Path;
20-
import java.nio.file.Paths;
2111
import java.util.zip.GZIPInputStream;
2212

23-
import static org.junit.Assert.assertEquals;
24-
import static org.junit.Assert.assertNotNull;
2513
import static org.junit.Assert.assertTrue;
2614

2715
/**
28-
* Created by andreas on 1/9/17.
16+
* Test the performance of MMTF format in BioJava.
17+
*
18+
* @author Andreas Prlic
19+
* on 1/9/17.
20+
*
2921
*/
3022
public class TestMmtfPerformance {
3123

@@ -35,6 +27,7 @@ public class TestMmtfPerformance {
3527

3628
// Returns the contents of the file in a byte array.
3729
public static byte[] getBytesFromFile(File file) throws IOException {
30+
3831
// Get the size of the file
3932
long length = file.length();
4033

biojava-structure/src/test/java/org/biojava/nbio/structure/io/mmtf/TestMmtfRoundTrip.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,37 @@
2424

2525
/**
2626
* Tests to see if roundtripping of MMTF can be done.
27+
*
2728
* @author Anthony Bradley
2829
*
2930
*/
3031
public class TestMmtfRoundTrip {
3132

3233
/**
3334
* Test that we can round trip a simple structure.
35+
*
3436
* @throws IOException an error reading the file
3537
* @throws StructureException an error parsing the structure
3638
*/
3739
@Test
3840
public void testRoundTrip() throws IOException, StructureException {
41+
42+
// Load a structure in MMCIF format
3943
AtomCache cache = new AtomCache();
4044
cache.setUseMmCif(true);
4145
ChemCompGroupFactory.setChemCompProvider(new DownloadChemCompProvider());
4246

43-
4447
StructureIO.setAtomCache(cache);
4548
Structure structure = StructureIO.getStructure("4CUP");
49+
50+
// Write the structure to a MMTF encoding
4651
AdapterToStructureData writerToEncoder = new AdapterToStructureData();
4752
new MmtfStructureWriter(structure, writerToEncoder);
53+
54+
// Load back the structure from the MMTF encoding
4855
MmtfStructureReader mmtfStructureReader = new MmtfStructureReader();
4956
new StructureDataToAdapter(writerToEncoder, mmtfStructureReader);
57+
5058
assertTrue(checkIfAtomsSame(structure,mmtfStructureReader.getStructure()));
5159
}
5260

@@ -58,18 +66,23 @@ public void testRoundTrip() throws IOException, StructureException {
5866
* @return
5967
*/
6068
private boolean checkIfAtomsSame(Structure structOne, Structure structTwo) {
69+
70+
// Check the same number of models
6171
int numModels = structOne.nrModels();
6272
if(numModels!=structTwo.nrModels()){
6373
System.out.println("Error - diff number models: "+structOne.getPDBCode());
6474
return false;
6575
}
76+
6677
for(int i=0;i<numModels;i++){
78+
6779
List<Chain> chainsOne = structOne.getChains(i);
6880
List<Chain> chainsTwo = structTwo.getChains(i);
6981
if(chainsOne.size()!=chainsTwo.size()){
7082
System.out.println("Error - diff number chains: "+structOne.getPDBCode());
7183
return false;
7284
}
85+
7386
// Now make sure they're sorted in the right order
7487
sortChains(chainsOne, chainsTwo);
7588
// Check that each one has the same number of poly, non-poly and water chains
@@ -182,8 +195,9 @@ else if(atomOne.getBonds().size()!=atomTwo.getBonds().size()){
182195
}
183196
return true;
184197
}
198+
185199
/**
186-
* Check both structures have the same number of poly,non-poly and water chains
200+
* Check both structures have the same number of poly, non-poly and water chains
187201
* @param structOne the first structure
188202
* @param structTwo the second structure
189203
* @param i the model index
@@ -193,8 +207,10 @@ private void checkDiffChains(Structure structOne, Structure structTwo, int i) {
193207
assertEquals(structOne.getNonPolyChains(i).size(), structTwo.getNonPolyChains(i).size());
194208
assertEquals(structOne.getWaterChains(i).size(), structTwo.getWaterChains(i).size());
195209
}
210+
196211
/**
197212
* Sort the atom based on PDB serial id
213+
*
198214
* @param atomsOne the first list
199215
* @param atomsTwo the second list
200216
*/
@@ -227,6 +243,7 @@ public int compare(Atom o1, Atom o2) {
227243

228244
/**
229245
* Sort the chains based on chain id.
246+
*
230247
* @param chainsOne the first list of chains
231248
* @param chainsTwo the second list of chains
232249
*/
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.biojava.nbio.structure.io.mmtf;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.IOException;
6+
import java.nio.file.Paths;
7+
import org.biojava.nbio.structure.Structure;
8+
import org.biojava.nbio.structure.StructureException;
9+
import org.biojava.nbio.structure.StructureIO;
10+
import org.junit.Test;
11+
12+
/**
13+
* Test the Biojava MMTF reader.
14+
*
15+
* @author Anthony Bradley
16+
* @author Aleix Lafita
17+
*
18+
*/
19+
public class TestMmtfStructureReader {
20+
21+
/**
22+
* Test reading an MMTF file into a BioJava structure.
23+
*/
24+
@Test
25+
public void testRead() throws IOException {
26+
27+
// Get the MMTF file from the resources folder
28+
ClassLoader classLoader = getClass().getClassLoader();
29+
String resource = "org/biojava/nbio/structure/io/mmtf/4CUP.mmtf";
30+
31+
// Load the structure into memory
32+
Structure structure = MmtfActions.readFromFile((
33+
Paths.get(classLoader.getResource(resource).getPath())));
34+
35+
// Check header properties of the structure
36+
assertEquals(structure.getPDBCode(), "4CUP");
37+
assertEquals(MmtfUtils.dateToIsoString(structure.getPDBHeader().getDepDate()),
38+
"2014-03-21");
39+
40+
assertEquals(structure.getChains().size(), 6);
41+
}
42+
43+
/**
44+
* Compare structures loaded from MMCIF and MMTF files.
45+
*/
46+
@Test
47+
public void compareMmcif() throws IOException, StructureException {
48+
49+
// Get the MMTF and MMCIF files from the resources folder
50+
ClassLoader classLoader = getClass().getClassLoader();
51+
String resource = "org/biojava/nbio/structure/io/mmtf/4CUP";
52+
53+
// Load the structures into memory
54+
Structure mmtf = StructureIO.getStructure(classLoader.getResource(resource).getPath() + ".mmtf");
55+
Structure mmcif = StructureIO.getStructure(classLoader.getResource(resource).getPath() + ".mmcif");
56+
57+
// Compare the SEQRES
58+
assertEquals(mmcif.getChain("A").getSeqResSequence(),
59+
mmtf.getChain("A").getSeqResSequence());
60+
61+
62+
63+
}
64+
65+
}

biojava-structure/src/test/java/org/biojava/nbio/structure/io/mmtf/TestBasicMmtf.java renamed to biojava-structure/src/test/java/org/biojava/nbio/structure/io/mmtf/TestMmtfStructureWriter.java

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package org.biojava.nbio.structure.io.mmtf;
22

3-
import static org.junit.Assert.assertEquals;
4-
53
import java.io.File;
64
import java.io.IOException;
7-
import java.nio.file.Paths;
85
import java.util.ArrayList;
96

107
import org.biojava.nbio.structure.AminoAcidImpl;
@@ -25,63 +22,65 @@
2522
import org.junit.rules.TemporaryFolder;
2623

2724
/**
28-
* Test that Biojava can read and write MMTF data.
25+
* Test the Biojava MMTF writer.
26+
*
2927
* @author Anthony Bradley
28+
* @author Aleix Lafita
3029
*
3130
*/
32-
public class TestBasicMmtf {
31+
public class TestMmtfStructureWriter {
3332

34-
/**
35-
* A test folder for testing writing files.
36-
*/
37-
@Rule
38-
public TemporaryFolder testFolder = new TemporaryFolder();
39-
40-
41-
/**
42-
* Test that Biojava can read a file from the file system.
43-
* @throws IOException
44-
*/
45-
@Test
46-
public void testRead() throws IOException {
47-
ClassLoader classLoader = getClass().getClassLoader();
48-
Structure structure = MmtfActions.readFromFile((Paths.get(classLoader.getResource("org/biojava/nbio/structure/io/mmtf/4CUP.mmtf").getPath())));
49-
assertEquals(structure.getPDBCode(),"4CUP");
50-
assertEquals(structure.getChains().size(),6);
51-
}
33+
@Rule
34+
public TemporaryFolder testFolder = new TemporaryFolder();
5235

5336
/**
5437
* Test the writing of Structure objects to a file.
55-
* @throws IOException
38+
*
39+
* @throws IOException
5640
*/
5741
@Test
5842
public void testWrite() throws IOException {
43+
44+
// Create a structure
5945
Structure structure = new StructureImpl();
46+
47+
// Add some header information
6048
PDBHeader pdbHeader = new PDBHeader();
6149
pdbHeader.setExperimentalTechnique("X-RAY DIFFRACTION");
62-
structure.setEntityInfos(new ArrayList<EntityInfo>());
6350
structure.setPDBHeader(pdbHeader);
51+
52+
// Create one chain
53+
structure.setEntityInfos(new ArrayList<EntityInfo>());
6454
Chain chain = new ChainImpl();
6555
chain.setId("A");
6656
chain.setName("A");
67-
Group group = new AminoAcidImpl();
57+
58+
// Create one group
59+
Group group = new AminoAcidImpl();
6860
group.setPDBName("FKF");
6961
ChemComp chemComp = new ChemComp();
7062
chemComp.setType("TYPfdl");
7163
chemComp.setOne_letter_code("A");
7264
group.setChemComp(chemComp);
65+
66+
// Create one Atom
7367
Atom atom = new AtomImpl();
7468
atom.setName("A");
7569
atom.setElement(Element.Ag);
76-
atom.setCoords(new double[] {1.0,2.0,3.0});
70+
atom.setCoords(new double[] { 1.0, 2.0, 3.0 });
71+
72+
// Link together the objects
7773
chain.addGroup(group);
7874
group.addAtom(atom);
75+
7976
ResidueNumber residueNumber = new ResidueNumber();
8077
residueNumber.setInsCode('A');
8178
residueNumber.setSeqNum(100);
8279
group.setResidueNumber(residueNumber);
80+
8381
structure.addChain(chain);
82+
8483
File tempFile = testFolder.newFile("tmpfile");
85-
MmtfActions.writeToFile(structure, tempFile.toPath());
84+
MmtfActions.writeToFile(structure, tempFile.toPath());
8685
}
8786
}

0 commit comments

Comments
 (0)