Revision: 21191 http://sourceforge.net/p/jmol/code/21191 Author: hansonr Date: 2016-08-09 22:57:00 +0000 (Tue, 09 Aug 2016) Log Message: ----------- concentrating MessagePack/MMTF byte reading in MessagePackReader.java
Modified Paths: -------------- trunk/Jmol/src/org/jmol/adapter/readers/cif/MMTFReader.java trunk/Jmol/src/org/jmol/adapter/readers/cif/MessagePackReader.java Modified: trunk/Jmol/src/org/jmol/adapter/readers/cif/MMTFReader.java =================================================================== --- trunk/Jmol/src/org/jmol/adapter/readers/cif/MMTFReader.java 2016-08-09 22:07:19 UTC (rev 21190) +++ trunk/Jmol/src/org/jmol/adapter/readers/cif/MMTFReader.java 2016-08-09 22:57:00 UTC (rev 21191) @@ -27,7 +27,6 @@ import java.util.Hashtable; import java.util.Map; -import javajs.util.BC; import javajs.util.Lst; import javajs.util.M4; import javajs.util.PT; @@ -75,6 +74,8 @@ * * Many thanks to the MMTF team at RCSB for assistance in this implementation. * + * @author Bob Hanson hans...@stolaf.edu + * */ public class MMTFReader extends MMCifReader { @@ -483,400 +484,7 @@ } - /////////////// MessagePack decoding /////////////// - private Object decode(String key) { - byte[] b = (byte[]) map.get(key); - int type = BC.bytesToInt(b, 0, true); - int n = BC.bytesToInt(b, 4, true); - int param = BC.bytesToInt(b, 8, true); - switch (type) { - case 1: - return getFloats(b, 4, 1); - case 2: // 1-byte - case 3: // 2-byte - case 4: // 4-byte - return getInts(b, 1 << (type - 2)); - case 5: - return rldecode32ToStr(b); - case 6: - return rldecode32ToChar(b, n); - case 7: - return rldecode32(b, n); - case 8: - return rldecode32Delta(b, n); - case 9: - return rldecodef(b, n, param); - case 10: - return unpack16Deltaf(b, n, param); - case 11: - return getFloats(b, 2, param); - case 12: // two-byte - case 13: // one-byte - return unpackf(b, 14 - type, n, param); - case 14: // two-byte - case 15: // one-byte - return unpack(b, 16 - type, n); - default: - Logger.error("MMTF type " + type + " not found!"); - return null; - } + return MessagePackReader.decode((byte[]) map.get(key)); } - - /** - * mmtf type 1 and 11 - * - * byte[4] to float32 - * - * @param b - * @param n - * @param divisor - * @return float[] - */ - protected float[] getFloats(byte[] b, int n, float divisor) { - if (b == null) - return null; - int len = (b.length - 12) / n; - float[] a = new float[len]; - try { - switch (n) { - case 2: - for (int i = 0, j = 12; i < len; i++, j += 2) - a[i] = BC.bytesToShort(b, j, false) / divisor; - break; - case 4: - for (int i = 0, j = 12; i < len; i++, j += 4) - a[i] = BC.bytesToFloat(b, j, false); - break; - } - } catch (Exception e) { - } - return a; - } - - /** - * mmtf types 2-4 - * - * Decode a byte array into a byte, short, or int array. - * - * @param b - * @param nbytes - * 1 (byte), 2 (int16), or 4 (int32) - * @return int array - */ - protected int[] getInts(byte[] b, int nbytes) { - if (b == null) - return null; - int len = (b.length - 12) / nbytes; - int[] a = new int[len]; - switch (nbytes) { - case 1: - for (int i = 0, j = 12; i < len; i++, j++) - a[i] = b[j]; - break; - case 2: - for (int i = 0, j = 12; i < len; i++, j += 2) - a[i] = BC.bytesToShort(b, j, true); - break; - case 4: - for (int i = 0, j = 12; i < len; i++, j += 4) - a[i] = BC.bytesToInt(b, j, true); - break; - } - return a; - } - - /** - * mmtf type 5 - * - * Decode each four bytes as a 1- to 4-character string label where a 0 byte - * indicates end-of-string. - * - * @param b - * a byte array - * @return String[] - */ - protected String[] rldecode32ToStr(byte[] b) { - String[] id = new String[(b.length - 12) / 4]; - out: for (int i = 0, len = id.length, pt = 12; i < len; i++) { - SB sb = new SB(); - for (int j = 0; j < 4; j++) { - switch (b[pt]) { - case 0: - id[i] = sb.toString(); - pt += 4 - j; - continue out; - default: - sb.appendC((char) b[pt++]); - if (j == 3) - id[i] = sb.toString(); - continue; - } - } - } - return id; - } - - /** - * mmtf type 6 - * - * Decode an array of int32 using run-length decoding to one char per int. - * - * @param b - * @param n - * @return array of integers - */ - protected char[] rldecode32ToChar(byte[] b, int n) { - if (b == null) - return null; - char[] ret = new char[n]; - for (int i = 0, pt = 3; i < n;) { - char val = (char) b[((pt++) << 2) + 3]; - for (int j = BC.bytesToInt(b, (pt++) << 2, true); --j >= 0;) - ret[i++] = val; - } - return ret; - } - - /** - * mmtf type 7 - * - * Decode an array of int32 using run-length decoding. - * - * @param b - * @param n - * @return array of integers - */ - protected int[] rldecode32(byte[] b, int n) { - if (b == null) - return null; - int[] ret = new int[n]; - for (int i = 0, pt = 3; i < n;) { - int val = BC.bytesToInt(b, (pt++) << 2, true); - for (int j = BC.bytesToInt(b, (pt++) << 2, true); --j >= 0;) - ret[i++] = val; - } - return ret; - } - - /** - * mmtf type 8 - * - * Decode an array of int32 using run-length decoding of a difference array. - * - * @param b - * @param n - * @return array of integers - */ - protected int[] rldecode32Delta(byte[] b, int n) { - if (b == null) - return null; - int[] ret = new int[n]; - for (int i = 0, pt = 3, val = 0; i < n;) { - int diff = BC.bytesToInt(b, (pt++) << 2, true); - for (int j = BC.bytesToInt(b, (pt++) << 2, true); --j >= 0;) - ret[i++] = (val = val + diff); - } - return ret; - } - - /** - * mmtf type 9 - * - * Decode an array of int32 using run-length decoding. - * - * @param b - * @param n - * @param divisor - * @return array of floats - */ - protected float[] rldecodef(byte[] b, int n, float divisor) { - if (b == null) - return null; - float[] ret = new float[n]; - for (int i = 0, pt = 3; i < n;) { - int val = BC.bytesToInt(b, (pt++) << 2, true); - for (int j = BC.bytesToInt(b, (pt++) << 2, true); --j >= 0;) - ret[i++] = val / divisor; - } - return ret; - } - - /** - * - * mmtf type 10 - * - * Decode an array of int16 using run-length decoding of a difference array. - * - * @param b - * @param n - * @param divisor - * @return array of integers - */ - protected float[] unpack16Deltaf(byte[] b, int n, float divisor) { - if (b == null) - return null; - float[] ret = new float[n]; - for (int i = 0, pt = 6, val = 0, buf = 0; i < n;) { - int diff = BC.bytesToShort(b, (pt++) << 1, true); - if (diff == Short.MAX_VALUE || diff == Short.MIN_VALUE) { - buf += diff; - } else { - ret[i++] = (val = val + diff + buf) / divisor; - buf = 0; - } - } - return ret; - } - - /** - * - * mmtf type 12 and 13 - * - * Unpack an array of int8 or int16 to int32 and divide to give a float32. - * - * untested - * - * @param b - * @param nBytes - * @param n - * @param divisor - * @return array of integers - */ - protected float[] unpackf(byte[] b, int nBytes, int n, float divisor) { - if (b == null) - return null; - float[] ret = new float[n]; - switch (nBytes) { - case 1: - for (int i = 0, pt = 12, offset = 0; i < n;) { - int val = b[pt++]; - if (val == Byte.MAX_VALUE || val == Byte.MIN_VALUE) { - offset += val; - } else { - ret[i++] = (val + offset) / divisor; - offset = 0; - } - } - break; - case 2: - for (int i = 0, pt = 6, offset = 0; i < n;) { - int val = BC.bytesToShort(b, (pt++) << 1, true); - if (val == Short.MAX_VALUE || val == Short.MIN_VALUE) { - offset += val; - } else { - ret[i++] = (val + offset) / divisor; - offset = 0; - } - } - break; - } - return ret; - } - - /** - * - * mmtf type 14 and 15 - * - * Unpack an array of int8 or int16 to int32. - * - * untested - * - * @param b - * @param nBytes - * @param n - * @return array of integers - */ - protected int[] unpack(byte[] b, int nBytes, int n) { - if (b == null) - return null; - int[] ret = new int[n]; - switch (nBytes) { - case 1: - for (int i = 0, pt = 12, offset = 0; i < n;) { - int val = b[pt++]; - if (val == Byte.MAX_VALUE || val == Byte.MIN_VALUE) { - offset += val; - } else { - ret[i++] = val + offset; - offset = 0; - } - } - break; - case 2: - for (int i = 0, pt = 6, offset = 0; i < n;) { - int val = BC.bytesToShort(b, (pt++) << 1, true); - if (val == Short.MAX_VALUE || val == Short.MIN_VALUE) { - offset += val; - } else { - ret[i++] = val + offset; - offset = 0; - } - } - break; - } - return ret; - } - - ///** - //* Decode an array of int16 using run-length decoding - //* of a difference array. - //* - //* @param b - //* @param n - //* @param i0 - //* @return array of integers - //*/ - //protected int[] rldecode16Delta(byte[] b, int n, int i0) { - //if (b == null) - //return null; - //int[] ret = new int[n]; - //for (int i = 0, pt = i0 / 2, val = 0; i < n;) { - //int diff = BC.bytesToShort(b, (pt++) << 1, true); - //for (int j = BC.bytesToShort(b, (pt++) << 1, true); --j >= 0;) - //ret[i++] = (val = val + diff); - //} - //return ret; - //} - - ///** - //* Do a split delta to a float[] array - //* @param xyz label "x", "y", "z", or "bFactor" - //* @param factor for dividing in the end -- 1000f or 100f - //* @return float[] - //* - //*/ - //protected float[] getFloatsSplit(String xyz, float factor) { - //byte[] big = (byte[]) map.get(xyz + "Big"); - //return (big == null ? null : splitDelta(big, - //(byte[]) map.get(xyz + "Small"), fileAtomCount, factor)); - //} - - ///** - //* Do a split delta to a float[] array - //* - //* @param big - //* [n m n m n m...] where n is a "big delta" and m is a number of - //* "small deltas - //* @param small - //* array containing the small deltas - //* @param n - //* the size of the final array - //* @param factor - //* to divide the final result by -- 1000f or 100f here - //* @return float[] - //*/ - //protected float[] splitDelta(byte[] big, byte[] small, int n, float factor) { - //float[] ret = new float[n]; - //for (int i = 0, smallpt = 0, val = 0, datapt = 0, len = big.length >> 2; i < len; i++) { - //ret[datapt++] = (val = val + BC.bytesToInt(big, i << 2, true)) / factor; - //if (++i < len) - //for (int j = BC.bytesToInt(big, i << 2, true); --j >= 0; smallpt++) - //ret[datapt++] = (val = val + BC.bytesToShort(small, smallpt << 1, true)) - // / factor; - //} - //return ret; - //} - // - } Modified: trunk/Jmol/src/org/jmol/adapter/readers/cif/MessagePackReader.java =================================================================== --- trunk/Jmol/src/org/jmol/adapter/readers/cif/MessagePackReader.java 2016-08-09 22:07:19 UTC (rev 21190) +++ trunk/Jmol/src/org/jmol/adapter/readers/cif/MessagePackReader.java 2016-08-09 22:57:00 UTC (rev 21191) @@ -3,8 +3,11 @@ import java.util.Hashtable; import java.util.Map; +import org.jmol.util.Logger; + import javajs.api.GenericBinaryDocument; import javajs.util.BC; +import javajs.util.SB; /** * A simple MessagePack reader. See https://github.com/msgpack/msgpack/blob/master/spec.md @@ -13,10 +16,13 @@ * * Does not implement unsigned int32 or int64 (delivers simple integers in all cases). * Does not use doubles; just floats - * Note: homogeneousArrays == true will deliver null for empty array. + * + * Note: * + * homogeneousArrays == true will deliver null for empty array. * * + * * @author Bob Hanson hans...@stolaf.edu */ @@ -287,4 +293,400 @@ return map; } + /////////////// MMTF MessagePack decoding /////////////// + + public static Object decode(byte[] b) { + int type = BC.bytesToInt(b, 0, true); + int n = BC.bytesToInt(b, 4, true); + int param = BC.bytesToInt(b, 8, true); + switch (type) { + case 1: + return getFloats(b, 4, 1); + case 2: // 1-byte + case 3: // 2-byte + case 4: // 4-byte + return getInts(b, 1 << (type - 2)); + case 5: + return rldecode32ToStr(b); + case 6: + return rldecode32ToChar(b, n); + case 7: + return rldecode32(b, n); + case 8: + return rldecode32Delta(b, n); + case 9: + return rldecodef(b, n, param); + case 10: + return unpack16Deltaf(b, n, param); + case 11: + return getFloats(b, 2, param); + case 12: // two-byte + case 13: // one-byte + return unpackf(b, 14 - type, n, param); + case 14: // two-byte + case 15: // one-byte + return unpack(b, 16 - type, n); + default: + Logger.error("MMTF type " + type + " not found!"); + return null; + } + } + + /** + * mmtf type 1 and 11 + * + * byte[4] to float32 + * + * @param b + * @param n + * @param divisor + * @return float[] + */ + public static float[] getFloats(byte[] b, int n, float divisor) { + if (b == null) + return null; + int len = (b.length - 12) / n; + float[] a = new float[len]; + try { + switch (n) { + case 2: + for (int i = 0, j = 12; i < len; i++, j += 2) + a[i] = BC.bytesToShort(b, j, false) / divisor; + break; + case 4: + for (int i = 0, j = 12; i < len; i++, j += 4) + a[i] = BC.bytesToFloat(b, j, false); + break; + } + } catch (Exception e) { + } + return a; + } + + /** + * mmtf types 2-4 + * + * Decode a byte array into a byte, short, or int array. + * + * @param b + * @param nbytes + * 1 (byte), 2 (int16), or 4 (int32) + * @return int array + */ + public static int[] getInts(byte[] b, int nbytes) { + if (b == null) + return null; + int len = (b.length - 12) / nbytes; + int[] a = new int[len]; + switch (nbytes) { + case 1: + for (int i = 0, j = 12; i < len; i++, j++) + a[i] = b[j]; + break; + case 2: + for (int i = 0, j = 12; i < len; i++, j += 2) + a[i] = BC.bytesToShort(b, j, true); + break; + case 4: + for (int i = 0, j = 12; i < len; i++, j += 4) + a[i] = BC.bytesToInt(b, j, true); + break; + } + return a; + } + + /** + * mmtf type 5 + * + * Decode each four bytes as a 1- to 4-character string label where a 0 byte + * indicates end-of-string. + * + * @param b + * a byte array + * @return String[] + */ + public static String[] rldecode32ToStr(byte[] b) { + String[] id = new String[(b.length - 12) / 4]; + out: for (int i = 0, len = id.length, pt = 12; i < len; i++) { + SB sb = new SB(); + for (int j = 0; j < 4; j++) { + switch (b[pt]) { + case 0: + id[i] = sb.toString(); + pt += 4 - j; + continue out; + default: + sb.appendC((char) b[pt++]); + if (j == 3) + id[i] = sb.toString(); + continue; + } + } + } + return id; + } + + /** + * mmtf type 6 + * + * Decode an array of int32 using run-length decoding to one char per int. + * + * @param b + * @param n + * @return array of integers + */ + public static char[] rldecode32ToChar(byte[] b, int n) { + if (b == null) + return null; + char[] ret = new char[n]; + for (int i = 0, pt = 3; i < n;) { + char val = (char) b[((pt++) << 2) + 3]; + for (int j = BC.bytesToInt(b, (pt++) << 2, true); --j >= 0;) + ret[i++] = val; + } + return ret; + } + + /** + * mmtf type 7 + * + * Decode an array of int32 using run-length decoding. + * + * @param b + * @param n + * @return array of integers + */ + public static int[] rldecode32(byte[] b, int n) { + if (b == null) + return null; + int[] ret = new int[n]; + for (int i = 0, pt = 3; i < n;) { + int val = BC.bytesToInt(b, (pt++) << 2, true); + for (int j = BC.bytesToInt(b, (pt++) << 2, true); --j >= 0;) + ret[i++] = val; + } + return ret; + } + + /** + * mmtf type 8 + * + * Decode an array of int32 using run-length decoding of a difference array. + * + * @param b + * @param n + * @return array of integers + */ + public static int[] rldecode32Delta(byte[] b, int n) { + if (b == null) + return null; + int[] ret = new int[n]; + for (int i = 0, pt = 3, val = 0; i < n;) { + int diff = BC.bytesToInt(b, (pt++) << 2, true); + for (int j = BC.bytesToInt(b, (pt++) << 2, true); --j >= 0;) + ret[i++] = (val = val + diff); + } + return ret; + } + + /** + * mmtf type 9 + * + * Decode an array of int32 using run-length decoding. + * + * @param b + * @param n + * @param divisor + * @return array of floats + */ + public static float[] rldecodef(byte[] b, int n, float divisor) { + if (b == null) + return null; + float[] ret = new float[n]; + for (int i = 0, pt = 3; i < n;) { + int val = BC.bytesToInt(b, (pt++) << 2, true); + for (int j = BC.bytesToInt(b, (pt++) << 2, true); --j >= 0;) + ret[i++] = val / divisor; + } + return ret; + } + + /** + * + * mmtf type 10 + * + * Decode an array of int16 using run-length decoding of a difference array. + * + * @param b + * @param n + * @param divisor + * @return array of integers + */ + public static float[] unpack16Deltaf(byte[] b, int n, float divisor) { + if (b == null) + return null; + float[] ret = new float[n]; + for (int i = 0, pt = 6, val = 0, buf = 0; i < n;) { + int diff = BC.bytesToShort(b, (pt++) << 1, true); + if (diff == Short.MAX_VALUE || diff == Short.MIN_VALUE) { + buf += diff; + } else { + ret[i++] = (val = val + diff + buf) / divisor; + buf = 0; + } + } + return ret; + } + + /** + * + * mmtf type 12 and 13 + * + * Unpack an array of int8 or int16 to int32 and divide to give a float32. + * + * untested + * + * @param b + * @param nBytes + * @param n + * @param divisor + * @return array of integers + */ + public static float[] unpackf(byte[] b, int nBytes, int n, float divisor) { + if (b == null) + return null; + float[] ret = new float[n]; + switch (nBytes) { + case 1: + for (int i = 0, pt = 12, offset = 0; i < n;) { + int val = b[pt++]; + if (val == Byte.MAX_VALUE || val == Byte.MIN_VALUE) { + offset += val; + } else { + ret[i++] = (val + offset) / divisor; + offset = 0; + } + } + break; + case 2: + for (int i = 0, pt = 6, offset = 0; i < n;) { + int val = BC.bytesToShort(b, (pt++) << 1, true); + if (val == Short.MAX_VALUE || val == Short.MIN_VALUE) { + offset += val; + } else { + ret[i++] = (val + offset) / divisor; + offset = 0; + } + } + break; + } + return ret; + } + + /** + * + * mmtf type 14 and 15 + * + * Unpack an array of int8 or int16 to int32. + * + * untested + * + * @param b + * @param nBytes + * @param n + * @return array of integers + */ + public static int[] unpack(byte[] b, int nBytes, int n) { + if (b == null) + return null; + int[] ret = new int[n]; + switch (nBytes) { + case 1: + for (int i = 0, pt = 12, offset = 0; i < n;) { + int val = b[pt++]; + if (val == Byte.MAX_VALUE || val == Byte.MIN_VALUE) { + offset += val; + } else { + ret[i++] = val + offset; + offset = 0; + } + } + break; + case 2: + for (int i = 0, pt = 6, offset = 0; i < n;) { + int val = BC.bytesToShort(b, (pt++) << 1, true); + if (val == Short.MAX_VALUE || val == Short.MIN_VALUE) { + offset += val; + } else { + ret[i++] = val + offset; + offset = 0; + } + } + break; + } + return ret; + } + + ///** + //* Decode an array of int16 using run-length decoding + //* of a difference array. + //* + //* @param b + //* @param n + //* @param i0 + //* @return array of integers + //*/ + //public static int[] rldecode16Delta(byte[] b, int n, int i0) { + //if (b == null) + //return null; + //int[] ret = new int[n]; + //for (int i = 0, pt = i0 / 2, val = 0; i < n;) { + //int diff = BC.bytesToShort(b, (pt++) << 1, true); + //for (int j = BC.bytesToShort(b, (pt++) << 1, true); --j >= 0;) + //ret[i++] = (val = val + diff); + //} + //return ret; + //} + + ///** + //* Do a split delta to a float[] array + //* @param xyz label "x", "y", "z", or "bFactor" + //* @param factor for dividing in the end -- 1000f or 100f + //* @return float[] + //* + //*/ + //public static float[] getFloatsSplit(String xyz, float factor) { + //byte[] big = (byte[]) map.get(xyz + "Big"); + //return (big == null ? null : splitDelta(big, + //(byte[]) map.get(xyz + "Small"), fileAtomCount, factor)); + //} + + ///** + //* Do a split delta to a float[] array + //* + //* @param big + //* [n m n m n m...] where n is a "big delta" and m is a number of + //* "small deltas + //* @param small + //* array containing the small deltas + //* @param n + //* the size of the final array + //* @param factor + //* to divide the final result by -- 1000f or 100f here + //* @return float[] + //*/ + //public static float[] splitDelta(byte[] big, byte[] small, int n, float factor) { + //float[] ret = new float[n]; + //for (int i = 0, smallpt = 0, val = 0, datapt = 0, len = big.length >> 2; i < len; i++) { + //ret[datapt++] = (val = val + BC.bytesToInt(big, i << 2, true)) / factor; + //if (++i < len) + //for (int j = BC.bytesToInt(big, i << 2, true); --j >= 0; smallpt++) + //ret[datapt++] = (val = val + BC.bytesToShort(small, smallpt << 1, true)) + // / factor; + //} + //return ret; + //} + // + + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic patterns at an interface-level. Reveals which users, apps, and protocols are consuming the most bandwidth. Provides multi-vendor support for NetFlow, J-Flow, sFlow and other flows. Make informed decisions using capacity planning reports. http://sdm.link/zohodev2dev _______________________________________________ Jmol-commits mailing list Jmol-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jmol-commits