Ejemplo n.º 1
0
        /// <summary>
        /// Convenience function for decoding binary data encoded by MSNumpress.
        /// </summary>
        /// <param name="cvAccession">The PSI-MS obo CV accession of the encoded data.</param>
        /// <param name="data">array of byte to be decoded.</param>
        /// <param name="dataSize">number of bytes from data to decode.</param>
        /// <returns>The decoded doubles</returns>
        /// <exception cref="ArgumentException">
        /// Cannot decode numLin data, need at least 8 initial bytes for fixed point.
        /// or
        /// Corrupt numLin data!
        /// or
        /// Cannot decode numPic data, need at least 8 initial bytes for fixed point.
        /// or
        /// Corrupt numPic data!
        /// or
        /// '" + cvAccession + "' is not a numpress compression term
        /// </exception>
        /// <remarks>
        /// If the passed cvAccession is one of
        ///
        ///    ACC_NUMPRESS_LINEAR = "MS:1002312"
        ///    ACC_NUMPRESS_PIC    = "MS:1002313"
        ///    ACC_NUMPRESS_SLOF   = "MS:1002314"
        ///
        /// the corresponding decode function will be called.
        /// </remarks>
        public static double[] decode(string cvAccession, byte[] data, int dataSize)
        {
            if (cvAccession == ACC_NUMPRESS_LINEAR)
            {
                if (dataSize < 8 || data.Length < 8)
                {
                    throw new ArgumentException("Cannot decode numLin data, need at least 8 initial bytes for fixed point.");
                }

                double[] buffer       = new double[dataSize * 2];
                int      nbrOfDoubles = MSNumpress.decodeLinear(data, dataSize, buffer);
                if (nbrOfDoubles < 0)
                {
                    throw new ArgumentException("Corrupt numLin data!");
                }

                double[] result = new double[nbrOfDoubles];
                Array.Copy(buffer, 0, result, 0, nbrOfDoubles);

                return(result);
            }

            if (cvAccession == ACC_NUMPRESS_SLOF)
            {
                double[] result = new double[(dataSize - 8) / 2];
                MSNumpress.decodeSlof(data, dataSize, result);

                return(result);
            }

            if (cvAccession == ACC_NUMPRESS_PIC)
            {
                if (dataSize < 8 || data.Length < 8)
                {
                    throw new ArgumentException("Cannot decode numPic data, need at least 8 initial bytes for fixed point.");
                }

                double[] buffer       = new double[dataSize * 2];
                int      nbrOfDoubles = MSNumpress.decodePic(data, dataSize, buffer);
                if (nbrOfDoubles < 0)
                {
                    throw new ArgumentException("Corrupt numPic data!");
                }

                double[] result = new double[nbrOfDoubles];
                Array.Copy(buffer, 0, result, 0, nbrOfDoubles);
                return(result);
            }

            throw new ArgumentException("'" + cvAccession + "' is not a numpress compression term");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Convenience function for encoding floating point data using MSNumpress.
        /// </summary>
        /// <param name="cvAccession">The PSI-MS obo CV accession to use to encode the data.</param>
        /// <param name="data">array of double to be encoded.</param>
        /// <param name="dataSize">number of doubles from data to encode.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">
        /// Corrupt numLin data!
        /// or
        /// Corrupt numPic data!
        /// or
        /// '" + cvAccession + "' is not a numpress compression term
        /// </exception>
        public static byte[] encode(string cvAccession, double[] data, int dataSize)
        {
            if (cvAccession == ACC_NUMPRESS_LINEAR)
            {
                byte[] buffer = new byte[dataSize * 5 + 8];

                double fixedPoint = MSNumpress.optimalLinearFixedPoint(data, dataSize);
                int    nbrOfBytes = MSNumpress.encodeLinear(data, dataSize, buffer, fixedPoint);
                if (nbrOfBytes < 0)
                {
                    throw new ArgumentException("Corrupt numLin data!");
                }

                byte[] result = new byte[nbrOfBytes];
                Array.Copy(buffer, 0, result, 0, nbrOfBytes);

                return(result);
            }

            if (cvAccession == ACC_NUMPRESS_SLOF)
            {
                double fixedPoint = MSNumpress.optimalSlofFixedPoint(data, dataSize);
                byte[] result     = new byte[dataSize * 2 + 8];
                int    nbrOfBytes = MSNumpress.encodeSlof(data, dataSize, result, fixedPoint);

                return(result);
            }

            if (cvAccession == ACC_NUMPRESS_PIC)
            {
                byte[] buffer     = new byte[dataSize * 5];
                int    nbrOfBytes = MSNumpress.encodePic(data, dataSize, buffer);
                if (nbrOfBytes < 0)
                {
                    throw new ArgumentException("Corrupt numPic data!");
                }

                byte[] result = new byte[nbrOfBytes];
                Array.Copy(buffer, 0, result, 0, nbrOfBytes);

                return(result);
            }

            throw new ArgumentException("'" + cvAccession + "' is not a numpress compression term");
        }