Skip to content

Commit 68ccb69

Browse files
committed
Additional testing of the reader and writer.
Altered the demo function.
1 parent c2a3294 commit 68ccb69

File tree

5 files changed

+97
-4
lines changed

5 files changed

+97
-4
lines changed

biojava-structure/src/main/java/demo/DemoMmtfReader.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import org.biojava.nbio.structure.Structure;
66
import org.biojava.nbio.structure.StructureException;
77
import org.biojava.nbio.structure.io.mmtf.MmtfActions;
8-
import org.biojava.nbio.structure.io.mmtf.MmtfUtils;
98

109
/**
1110
* Class to show how to read a Biojava structure using MMTF
@@ -14,8 +13,14 @@
1413
*/
1514
public class DemoMmtfReader {
1615

16+
/**
17+
* Main function to run the demo
18+
* @param args no args to specify
19+
* @throws IOException
20+
* @throws StructureException
21+
*/
1722
public static void main(String[] args) throws IOException, StructureException {
18-
Structure structure = MmtfActions.readFromFile("/path/to/file");
23+
Structure structure = MmtfActions.readFromWeb("4cup");
1924
System.out.println(structure.getChains().size());
2025
}
2126

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,13 @@ private void addBonds(Atom atom, List<Atom> atomsInGroup, List<Atom> allAtoms) {
152152
private void storeEntityInformation(List<Chain> allChains, List<EntityInfo> entityInfos) {
153153
for (EntityInfo entityInfo : entityInfos) {
154154
String description = entityInfo.getDescription();
155-
String type = entityInfo.getType().getEntityType();
155+
String type;
156+
if (entityInfo.getType()==null){
157+
type = null;
158+
}
159+
else{
160+
type = entityInfo.getType().getEntityType();
161+
}
156162
List<Chain> entityChains = entityInfo.getChains();
157163
if (entityChains.isEmpty()){
158164
// Error mapping chain to entity
@@ -181,7 +187,7 @@ private void storeBioassemblyInformation(Map<String, Integer> chainIdToIndexMap,
181187
for(Entry<double[], int[]> transformEntry : transformMap.entrySet()) {
182188
mmtfDecoderInterface.setBioAssemblyTrans(bioAssemblyIndex, transformEntry.getValue(), transformEntry.getKey());
183189
}
184-
bioAssemblyIndex+=1;
190+
bioAssemblyIndex+=1;
185191
}
186192
}
187193

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ public static float[] getUnitCellAsArray(PDBCrystallographicInfo xtalInfo) {
185185
* @return the array of strings describing the methods used.
186186
*/
187187
public static String[] techniquesToStringArray(Set<ExperimentalTechnique> experimentalTechniques) {
188+
if(experimentalTechniques==null){
189+
return new String[0];
190+
}
188191
String[] outArray = new String[experimentalTechniques.size()];
189192
int index = 0;
190193
for (ExperimentalTechnique experimentalTechnique : experimentalTechniques) {
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.biojava.nbio.structure.io.mmtf;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.util.ArrayList;
8+
9+
import org.biojava.nbio.structure.AminoAcidImpl;
10+
import org.biojava.nbio.structure.Atom;
11+
import org.biojava.nbio.structure.AtomImpl;
12+
import org.biojava.nbio.structure.Chain;
13+
import org.biojava.nbio.structure.ChainImpl;
14+
import org.biojava.nbio.structure.Element;
15+
import org.biojava.nbio.structure.EntityInfo;
16+
import org.biojava.nbio.structure.Group;
17+
import org.biojava.nbio.structure.PDBHeader;
18+
import org.biojava.nbio.structure.ResidueNumber;
19+
import org.biojava.nbio.structure.Structure;
20+
import org.biojava.nbio.structure.StructureImpl;
21+
import org.junit.Rule;
22+
import org.junit.Test;
23+
import org.junit.rules.TemporaryFolder;
24+
25+
/**
26+
* Test that Biojava can read and write MMTF data.
27+
* @author Anthony Bradley
28+
*
29+
*/
30+
public class TestBasicMmtf {
31+
32+
/**
33+
* A test folder for testing writing files.
34+
*/
35+
@Rule
36+
public TemporaryFolder testFolder = new TemporaryFolder();
37+
38+
39+
/**
40+
* Test that Biojava can read a file from the file system.
41+
* @throws IOException
42+
*/
43+
@Test
44+
public void testRead() throws IOException {
45+
ClassLoader classLoader = getClass().getClassLoader();
46+
Structure structure = MmtfActions.readFromFile((classLoader.getResource("org/biojava/nbio/structure/io/mmtf/4CUP.mmtf").getPath()));
47+
assertEquals(structure.getPDBCode(),"4CUP");
48+
assertEquals(structure.getChains().size(),6);
49+
}
50+
51+
/**
52+
* Test the writing of Structure objects to a file.
53+
* @throws IOException
54+
*/
55+
@Test
56+
public void testWrite() throws IOException {
57+
Structure structure = new StructureImpl();
58+
PDBHeader pdbHeader = new PDBHeader();
59+
pdbHeader.setExperimentalTechnique("X-RAY DIFFRACTION");
60+
structure.setEntityInfos(new ArrayList<EntityInfo>());
61+
structure.setPDBHeader(pdbHeader);
62+
Chain chain = new ChainImpl();
63+
Group group = new AminoAcidImpl();
64+
group.setPDBName("FKF");
65+
Atom atom = new AtomImpl();
66+
atom.setName("A");
67+
atom.setElement(Element.Ag);
68+
atom.setCoords(new double[] {1.0,2.0,3.0});
69+
chain.addGroup(group);
70+
group.addAtom(atom);
71+
ResidueNumber residueNumber = new ResidueNumber();
72+
residueNumber.setInsCode('A');
73+
residueNumber.setSeqNum(100);
74+
group.setResidueNumber(residueNumber);
75+
structure.addChain(chain);
76+
File tempFile = testFolder.newFile("tmpfile");
77+
MmtfActions.writeToFile(structure, tempFile.getAbsolutePath());
78+
}
79+
}
Binary file not shown.

0 commit comments

Comments
 (0)