Skip to content

Commit

Permalink
Update to DecoderToReader and downstream refactoring. As per Jose's s…
Browse files Browse the repository at this point in the history
…uggestions.
  • Loading branch information
abradle committed Apr 18, 2016
1 parent cd0d9f2 commit ea7b9e5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.rcsb.mmtf.decoder;

import java.util.HashSet;
import java.util.Set;

import org.rcsb.mmtf.api.StructureDataInterface;
import org.rcsb.mmtf.api.StructureAdapterInterface;

Expand All @@ -21,19 +18,25 @@ public class DecoderToReader {
private StructureDataInterface dataApi;

// Intialises the counters.
private int modelCounter = 0;
private int chainCounter = 0;
private int groupCounter = 0;
private int atomCounter = 0;
private int currentAtomIndex = 0;
private Set<String> chainIdSet;
private int modelCounter;
private int chainCounter;
private int groupCounter;
private int atomInGroupCounter;
private int atomInStructureCounter;


/**
* Passes data from the data interface to the inflator interface.
* @param inputApi the interface to the decoded data
* @param inputInflator the interface to put the data into the client object
*/
public void read(StructureDataInterface inputApi, StructureAdapterInterface inputInflator){
public DecoderToReader(StructureDataInterface inputApi, StructureAdapterInterface inputInflator){
// Set the counters to zero
modelCounter = 0;
chainCounter = 0;
groupCounter = 0;
atomInGroupCounter = 0;
atomInStructureCounter = 0;
// Set the api and the inflator
dataApi = inputApi;
structInflator = inputInflator;
Expand All @@ -55,15 +58,13 @@ public void read(StructureDataInterface inputApi, StructureAdapterInterface inpu
// Now do any required cleanup
structInflator.finalizeStructure();
}

/**
* Add the main atomic information to the data model
*/
private final void addAtomicInformation() {
for (int modelChains: dataApi.getChainsPerModel()) {
structInflator.setModelInfo(modelCounter, modelChains);
// A list to check if we need to set or update the chains
chainIdSet = new HashSet<>();
int totChainsThisModel = chainCounter + modelChains;
int lastChainCounter = chainCounter;
for (int chainIndex = lastChainCounter; chainIndex < totChainsThisModel; chainIndex++) {
Expand All @@ -83,12 +84,7 @@ private void addOrUpdateChainInfo(int chainIndex) {
String currentChainName = dataApi.getChainNames()[chainIndex];
int groupsThisChain = dataApi.getGroupsPerChain()[chainIndex];
// If we've already seen this chain -> just update it
if (chainIdSet.contains(currentChainId)) {
structInflator.setChainInfo(currentChainId, currentChainName, groupsThisChain);
} else {
structInflator.setChainInfo(currentChainId, currentChainName, groupsThisChain);
chainIdSet.add(currentChainId);
}
structInflator.setChainInfo(currentChainId, currentChainName, groupsThisChain);
int nextInd = groupCounter + groupsThisChain;
int lastGroupCount = groupCounter;
// Now iteratr over the group
Expand All @@ -115,13 +111,13 @@ private int addGroup(int currentGroupIndex) {
dataApi.getGroupChemCompType(groupInd), atomCount, dataApi.getNumBonds(), dataApi.getGroupSingleLetterCode(groupInd),
dataApi.getGroupSequenceIndices()[currentGroupIndex], dataApi.getSecStructList()[currentGroupIndex]);
// A counter for the atom information
atomCounter = 0;
atomInGroupCounter = 0;
// Now read the next atoms
for (int i = 0; i < atomCount; i++) {
addAtomData(dataApi.getGroupAtomNames(groupInd), dataApi.getGroupElementNames(groupInd), dataApi.getGroupAtomCharges(groupInd), currentAtomIndex);
currentAtomIndex++;
addAtomData(dataApi.getGroupAtomNames(groupInd), dataApi.getGroupElementNames(groupInd), dataApi.getGroupAtomCharges(groupInd), atomInStructureCounter);
atomInStructureCounter++;
// Now increment the atom counter for this group
atomCounter++;
atomInGroupCounter++;
}
addGroupBonds(dataApi.getGroupBondIndices(groupInd), dataApi.getGroupBondOrders(groupInd));
return atomCount;
Expand All @@ -137,9 +133,9 @@ private int addGroup(int currentGroupIndex) {
private void addAtomData(String[] atomNames, String[] elementNames, int[] atomCharges,
int currentAtomIndex) {
// Now get all the relevant atom level information here
String atomName = atomNames[atomCounter];
String element = elementNames[atomCounter];
int charge = atomCharges[atomCounter];
String atomName = atomNames[atomInGroupCounter];
String element = elementNames[atomInGroupCounter];
int charge = atomCharges[atomInGroupCounter];
char alternativeLocationId = dataApi.getAltLocIds()[currentAtomIndex];
int serialNumber = dataApi.getAtomIds()[currentAtomIndex];
float x = dataApi.getxCoords()[currentAtomIndex];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
public class DecoderUtils {


/**
* Parses the bioassembly data and inputs it to the structure inflator
* @param dataApi the interface to the decoded data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ public static byte[] deflateGzip(byte[] inputBytes){
*/
public static MmtfBean getDataFromFile(String filePath) throws IOException {
// Now return the gzip deflated and deserialized byte array
MessagePackDeserializer messagePackDeserializer = new MessagePackDeserializer();
return messagePackDeserializer.deserialize(readFile(filePath));
return new MessagePackDeserializer().deserialize(readFile(filePath));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.lang.reflect.InvocationTargetException;

import org.junit.Test;
import org.rcsb.mmtf.api.StructureAdapterInterface;
import org.rcsb.mmtf.dataholders.MmtfBean;
import org.unitils.reflectionassert.ReflectionAssert;

Expand Down Expand Up @@ -57,9 +56,7 @@ public void testDecodeAllFields() throws IOException, IntrospectionException, Il
@Test
public void testReader() {
DummyApiImpl dummyApiImpl = new DummyApiImpl();
DecoderToReader decoderToReader = new DecoderToReader();
StructureAdapterInterface inputInflator = new DummyTransferImpl();
decoderToReader.read(dummyApiImpl, inputInflator);
new DecoderToReader(dummyApiImpl, new DummyTransferImpl());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.FileOutputStream;
import java.io.IOException;

import org.rcsb.mmtf.api.StructureDataInterface;
import org.rcsb.mmtf.serializers.MessagePackSerializer;

/**
Expand All @@ -18,7 +19,7 @@ public class WriterUtils {
* @param path the full path to write to
* @throws IOException an error related to byte array transfers
*/
public static void writeDataToFile(WriterToEncoder writerToEncoder, String path) throws IOException {
public static void writeDataToFile(StructureDataInterface writerToEncoder, String path) throws IOException {
byte[] byteArray = getDataAsByteArr(writerToEncoder);
FileOutputStream fos = new FileOutputStream(path);
fos.write(byteArray);
Expand All @@ -32,7 +33,7 @@ public static void writeDataToFile(WriterToEncoder writerToEncoder, String path)
* @return a byte array of the data
* @throws IOException an error related to byte array transfers
*/
public static byte[] getDataAsByteArr(WriterToEncoder writerToEncoder) throws IOException {
public static byte[] getDataAsByteArr(StructureDataInterface writerToEncoder) throws IOException {
MessagePackSerializer messagePackSerializer = new MessagePackSerializer();
// Get to bean
DefaultEncoder getToBean = new DefaultEncoder(writerToEncoder);
Expand Down

0 comments on commit ea7b9e5

Please sign in to comment.