Skip to content

Commit 5d17685

Browse files
committed
Added three new demos (reader, writer and roundtrip) to Biojava.
Updated the Writer to use the writer interface. Updated the others to comply with upstrea formatting
1 parent fe171cc commit 5d17685

File tree

6 files changed

+131
-38
lines changed

6 files changed

+131
-38
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package demo;
2+
3+
import java.io.IOException;
4+
5+
import org.biojava.nbio.structure.Structure;
6+
import org.biojava.nbio.structure.io.mmtf.MmtfStructureReader;
7+
import org.rcsb.mmtf.dataholders.MmtfBean;
8+
import org.rcsb.mmtf.decoder.BeanToGet;
9+
import org.rcsb.mmtf.decoder.GetToInflator;
10+
import org.rcsb.mmtf.deserializers.MessagePackDeserializer;
11+
12+
public class DemoMmtfReader {
13+
14+
/**
15+
* Utility function to get a Biojava structure from a byte array.
16+
* @param inputByteArray Must be uncompressed (i.e. with entropy compression methods like gzip)
17+
* @param parsingParams
18+
* @return
19+
* @throws IOException
20+
*/
21+
public static Structure getBiojavaStruct(byte[] inputByteArray) throws IOException {
22+
// Get the reader - this is the bit that people need to implement.
23+
MmtfStructureReader mmtfStructureReader = new MmtfStructureReader();
24+
// Set up the deserializer
25+
MessagePackDeserializer messagePackDeserializer = new MessagePackDeserializer();
26+
// Get the data
27+
MmtfBean mmtfBean = messagePackDeserializer.deserialize(inputByteArray);
28+
// Set up the data API
29+
BeanToGet beanToGet = new BeanToGet(mmtfBean);
30+
// Set up the inflator
31+
GetToInflator getToInflator = new GetToInflator();
32+
// Do the inflation
33+
getToInflator.read(beanToGet, mmtfStructureReader);
34+
// Get the structue
35+
return mmtfStructureReader.getStructure();
36+
}
37+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package demo;
2+
3+
import java.io.IOException;
4+
5+
import org.biojava.nbio.structure.Structure;
6+
import org.biojava.nbio.structure.io.mmtf.MmtfStructureReader;
7+
import org.biojava.nbio.structure.io.mmtf.MmtfStructureWriter;
8+
import org.rcsb.mmtf.dataholders.MmtfBean;
9+
import org.rcsb.mmtf.decoder.BeanToGet;
10+
import org.rcsb.mmtf.decoder.GetToInflator;
11+
import org.rcsb.mmtf.deserializers.MessagePackDeserializer;
12+
import org.rcsb.mmtf.encoder.GetToBean;
13+
import org.rcsb.mmtf.encoder.InflatorToGet;
14+
import org.rcsb.mmtf.serializers.MessagePackSerializer;
15+
16+
public class DemoMmtfRoundTrip {
17+
18+
public static Structure roundTrip(Structure structure) throws IOException {
19+
// Set up the transform from the inflator to the get api
20+
InflatorToGet inflatorToGet = new InflatorToGet();
21+
// Get the writer - this is what people implement
22+
MmtfStructureWriter mmtfStructureWriter = new MmtfStructureWriter(structure);
23+
// Do thte inflation
24+
mmtfStructureWriter.write(inflatorToGet);
25+
// Get the bean
26+
GetToBean getToBean = new GetToBean(inflatorToGet);
27+
// Serialize
28+
MessagePackSerializer messagePackSerializer = new MessagePackSerializer();
29+
byte[] byteArr = messagePackSerializer.serialize(getToBean.getMmtfBean());
30+
// Get the reader - this is the bit that people need to implement.
31+
MmtfStructureReader mmtfStructureReader = new MmtfStructureReader();
32+
// Set up the deserializer
33+
MessagePackDeserializer messagePackDeserializer = new MessagePackDeserializer();
34+
// Get the data
35+
MmtfBean mmtfBean = messagePackDeserializer.deserialize(byteArr);
36+
// Set up the data API
37+
BeanToGet beanToGet = new BeanToGet(mmtfBean);
38+
// Set up the inflator
39+
GetToInflator getToInflator = new GetToInflator();
40+
// Do the inflation
41+
getToInflator.read(beanToGet, mmtfStructureReader);
42+
// Get the structue
43+
return mmtfStructureReader.getStructure();
44+
}
45+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package demo;
2+
3+
import java.io.IOException;
4+
5+
import org.biojava.nbio.structure.Structure;
6+
import org.biojava.nbio.structure.io.mmtf.MmtfStructureWriter;
7+
import org.rcsb.mmtf.encoder.GetToBean;
8+
import org.rcsb.mmtf.encoder.InflatorToGet;
9+
import org.rcsb.mmtf.serializers.MessagePackSerializer;
10+
11+
public class DemoMmtfWriter {
12+
13+
/**
14+
* Utility function to get a byte array from a Biojava structure
15+
* @param inputByteArray Must be uncompressed (i.e. with entropy compression methods like gzip)
16+
* @param parsingParams
17+
* @return
18+
* @throws IOException
19+
*/
20+
public static byte[] getByteArray(Structure structure) throws IOException {
21+
// Set up the transform from the inflator to the get api
22+
InflatorToGet inflatorToGet = new InflatorToGet();
23+
// Get the writer - this is what people implement
24+
MmtfStructureWriter mmtfStructureWriter = new MmtfStructureWriter(structure);
25+
// Now deflate
26+
mmtfStructureWriter.write(inflatorToGet);
27+
// Get to bean
28+
GetToBean getToBean = new GetToBean(inflatorToGet);
29+
MessagePackSerializer messagePackSerializer = new MessagePackSerializer();
30+
return messagePackSerializer.serialize(getToBean.getMmtfBean());
31+
}
32+
}

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.biojava.nbio.structure.xtal.CrystalCell;
3131
import org.biojava.nbio.structure.xtal.SpaceGroup;
3232
import org.rcsb.mmtf.api.MmtfDecoderInterface;
33-
import org.rcsb.mmtf.decoder.GetApiToInflatorInterface;
3433

3534

3635
/**
@@ -442,19 +441,5 @@ public void setHeaderInfo(float rFree, float rWork, float resolution, String tit
442441
}
443442
}
444443

445-
/**
446-
* Utility function to get a Biojava structure from a byte array.
447-
* @param inputByteArray Must be uncompressed (i.e. with entropy compression methods like gzip)
448-
* @param parsingParams
449-
* @return
450-
*/
451-
public static Structure getBiojavaStruct(byte[] inputByteArray) {
452-
// Make the decoder
453-
MmtfStructureReader biojavaStructureDecoder = new MmtfStructureReader();
454-
GetApiToInflatorInterface ds = new GetApiToInflatorInterface(inputByteArray);
455-
ds.getStructFromByteArray(biojavaStructureDecoder);
456-
// Now return this structure
457-
return biojavaStructureDecoder.getStructure();
458-
}
459444

460445
}

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,35 @@
1515
import org.biojava.nbio.structure.io.mmcif.model.ChemComp;
1616
import org.biojava.nbio.structure.quaternary.BioAssemblyInfo;
1717
import org.rcsb.mmtf.api.MmtfDecoderInterface;
18+
import org.rcsb.mmtf.api.MmtfWriter;
1819

1920
/**
2021
* Class to take Biojava structure data and covert to the DataApi for encoding.
2122
* Must implement all the functions in {@link MmtfDecoderInterface}.
2223
* @author Anthony Bradley
2324
*
2425
*/
25-
public class MmtfStructureWriter {
26+
public class MmtfStructureWriter implements MmtfWriter {
2627

2728

2829
private MmtfDecoderInterface mmtfDecoderInterface;
30+
private Structure structure;
2931

3032
/**
3133
* The constructor requires a structre input.
32-
* @param structure ths structure to be encoded
34+
* @param mmtfDecoderInterface the interface to be used
35+
* @param structure the structure to be encoded
3336
*/
34-
public MmtfStructureWriter(MmtfDecoderInterface mmtfDecoderInterface) {
35-
this.mmtfDecoderInterface = mmtfDecoderInterface;
37+
public MmtfStructureWriter(Structure data) {
38+
this.structure = data;
39+
3640
}
3741

3842

39-
public void encodeStructure(Structure structure) {
43+
public void write(MmtfDecoderInterface decoder) {
44+
45+
this.mmtfDecoderInterface = decoder;
46+
4047
// Reset structure to consider altloc groups with the same residue number but different group names as seperate groups
4148
MmtfUtils.fixMicroheterogenity(structure);
4249
// Generate the secondary structure
@@ -64,15 +71,13 @@ public void encodeStructure(Structure structure) {
6471
Chain chain = modelChains.get(chainInModelIndex);
6572
List<Group> groups = chain.getAtomGroups();
6673
List<Group> sequenceGroups = chain.getSeqResGroups();
67-
// TODO we need to include the sequence to group mapping information....
6874
mmtfDecoderInterface.setChainInfo(chain.getChainID(), chain.getInternalChainID(), groups.size());
6975
for(int groupInChainIndex=0; groupInChainIndex<groups.size(); groupInChainIndex++){
7076
// Get the major compy of this group
7177
Group group = groups.get(groupInChainIndex);
7278
List<Atom> atomsInGroup = MmtfUtils.getAtomsForGroup(group);
7379
// Get the group type
7480
ChemComp chemComp = group.getChemComp();
75-
// TODO We need to specify what type of group is being added
7681
mmtfDecoderInterface.setGroupInfo(group.getPDBName(), group.getResidueNumber().getSeqNum(), group.getResidueNumber().getInsCode().charValue(),
7782
chemComp.getPdbx_type(), atomsInGroup.size(), chemComp.getOne_letter_code().charAt(0), sequenceGroups.indexOf(group));
7883
for (Atom atom : atomsInGroup){
@@ -155,4 +160,5 @@ private void storeBioassemblyInformation(Map<String, Integer> chainIdToIndexMap,
155160
}
156161
}
157162
}
163+
158164
}

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@
3030
import org.biojava.nbio.structure.secstruc.SecStrucCalc;
3131
import org.biojava.nbio.structure.xtal.CrystalCell;
3232
import org.biojava.nbio.structure.xtal.SpaceGroup;
33+
import org.rcsb.mmtf.utils.CodecUtils;
3334

3435
/**
35-
* A utils class of functions needed for Biojava.
36+
* A utils class of functions needed for Biojava to read and write to mmtf.
3637
* @author Anthony Bradley
3738
*
3839
*/
@@ -61,7 +62,7 @@ public static AtomCache setUpBioJava() {
6162
/**
6263
* Set up the configuration parameters for BioJava. - with an extra URL
6364
*/
64-
public AtomCache setUpBioJava(String extraUrl) {
65+
public static AtomCache setUpBioJava(String extraUrl) {
6566
// Set up the atom cache etc
6667
AtomCache cache = new AtomCache();
6768
cache.setUseMmCif(true);
@@ -290,24 +291,11 @@ public static Map<double[], int[]> getTransformMap(BioAssemblyInfo bioassemblyIn
290291
}
291292
Map<double[], int[]> outMap = new HashMap<>();
292293
for (Entry<Matrix4d, List<Integer>> entry : matMap.entrySet()) {
293-
outMap.put(convertToDoubleArray(entry.getKey()), convertToIntArray(entry.getValue()));
294+
outMap.put(convertToDoubleArray(entry.getKey()), CodecUtils.convertToIntArray(entry.getValue()));
294295
}
295296
return outMap;
296297
}
297298

298-
/**
299-
* Convert a List<Integer> to an int[]
300-
* @param integerList the input list
301-
* @return the output array
302-
*/
303-
private static int[] convertToIntArray(List<Integer> integerList) {
304-
int[] integerArray = new int[integerList.size()];
305-
for(int i=0; i<integerList.size(); i++){
306-
integerArray[i] = integerList.get(i);
307-
}
308-
return integerArray;
309-
}
310-
311299
/**
312300
* Convert a four-d matrix to a double array. Row-packed.
313301
* @param transformationMatrix the input matrix4d object

0 commit comments

Comments
 (0)