Changeset 4520


Ignore:
Timestamp:
Sep 10, 2013, 4:45:18 PM (10 years ago)
Author:
Fredrik Levander
Message:

Fixes #815. Incorporating msnumpress support.

Location:
trunk/api/core/src
Files:
4 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/api/core/src/org/proteios/io/Base64Util.java

    r4381 r4520  
    2525
    2626import org.proteios.core.Base64Coder;
     27import ms.numpress.MSNumpress;
    2728import java.nio.ByteBuffer;
    2829import java.nio.ByteOrder;
     
    3334
    3435/**
    35  * This class supports conversion to/from Base64-coded data.
    36  *
    37  * Used e.g. to encode the data (peak) part of mass spectra
    38  * in an mzData file.
     36 * This class supports conversion to/from Base64-coded data. Used e.g. to encode
     37 * the data (peak) part of mass spectra in an mzData file.
    3938 *
    4039 * @author Olle
     
    4443public class Base64Util
    4544{
     45 
    4646  private static final org.apache.log4j.Logger log = org.apache.log4j.LogManager
    4747    .getLogger("org.proteios.io");
    4848
    49   /**
    50    * Decodes Base64 coded data String
    51    * and returns a String
    52    * containing the extracted data.
    53    *
    54    * Uses class org.proteios.core.Base64Coder to decode data.
    55    *
     49
     50  /**
     51   * Decodes Base64 coded data String and returns a String containing the
     52   * extracted data. Uses class org.proteios.core.Base64Coder to decode data.
     53   *
    5654   * @param dataString String A string with Base64 coded data.
    5755   * @return String A string with decoded data.
     
    6967
    7068  /**
    71    * Decodes Base64 coded data String
    72    * and returns an ArrayList<double>
    73    * containing the extracted data.
    74    *
    75    * Uses class org.proteios.core.Base64Coder to decode data.
    76    *
     69   * Decodes Base64 coded data String and returns an ArrayList<double>
     70   * containing the extracted data. Uses class org.proteios.core.Base64Coder
     71   * to decode data.
     72   *
    7773   * @param doublePrecision boolean flag, true if precision == "64".
    7874   * @param bigEndian boolean flag indicating endian type.
     
    8076   * @return ArrayList<double> with extracted values.
    8177   */
    82   public static List<Double> decode(boolean doublePrecision, boolean bigEndian, String dataString)
     78  public static List<Double> decode(boolean doublePrecision,
     79      boolean bigEndian, String numpress, String dataString)
    8380  {
    8481    /*
     
    8683     */
    8784    boolean zLibCompression = false;
    88     List<Double> resultArray = decode(doublePrecision, bigEndian, zLibCompression, dataString);
     85    List<Double> resultArray = decode(doublePrecision, bigEndian,
     86      zLibCompression, numpress, dataString);
    8987    /*
    9088     * Return result as list
     
    9593
    9694  /**
    97    * Decodes Base64 coded data String
    98    * and returns an ArrayList<double>
    99    * containing the extracted data.
    100    *
    101    * Uses class org.proteios.core.Base64Coder to decode data.
    102    *
     95   * Decodes Base64 coded data String and returns an ArrayList<double>
     96   * containing the extracted data. Uses class org.proteios.core.Base64Coder
     97   * to decode data.
     98   *
    10399   * @param doublePrecision boolean flag, true if precision == "64".
    104100   * @param bigEndian boolean flag indicating endian type.
     
    107103   * @return ArrayList<double> with extracted values.
    108104   */
    109   public static List<Double> decode(boolean doublePrecision, boolean bigEndian, boolean zLibCompression, String dataString)
     105  public static List<Double> decode(boolean doublePrecision,
     106      boolean bigEndian, boolean zLibCompression, String numpress,
     107      String dataString)
    110108  {
    111109    /*
     
    118116      Inflater decompresser = new Inflater();
    119117      decompresser.setInput(dataByteArray, 0, dataByteArray.length);
    120       byte[] uncompressedByteArray = new byte[3*dataByteArray.length];
     118      byte[] uncompressedByteArray = new byte[3 * dataByteArray.length];
    121119      int uncompressedArrayLength = 0;
    122120      try
    123121      {
    124         uncompressedArrayLength = decompresser.inflate(uncompressedByteArray);
     122        uncompressedArrayLength = decompresser
     123          .inflate(uncompressedByteArray);
    125124      }
    126125      catch (DataFormatException e)
     
    131130      // Use uncompressed byte array
    132131      dataByteArray = new byte[uncompressedArrayLength];
    133       for (int i=0; i < uncompressedArrayLength; i++)
     132      for (int i = 0; i < uncompressedArrayLength; i++)
    134133      {
    135134        dataByteArray[i] = uncompressedByteArray[i];
    136135      }
    137136    }
    138     ByteBuffer dataByteBuffer = ByteBuffer.wrap(dataByteArray);
    139     /*
    140      * Get size of result list from
    141      * byte array size and precision.
    142      */
    143     int arraySize = dataByteArray.length;
    144     int bytesPerValue = 4;
    145     if (doublePrecision) {
    146       /*
    147        * Precision == "64"
    148        */
    149       bytesPerValue = 8;
    150     }
    151     int dataLength = arraySize/bytesPerValue;
    152137    /*
    153138     * Create result arrayList
    154139     */
    155140    List<Double> resultArray = new ArrayList<Double>(1);
    156     /*
    157      * Java works with big endian by default.
    158      * If the data is little endian, the
    159      * byte order is changed.
    160      *
    161      * This configuration influences how data
    162      * is retrieved using methods like
    163      * getFloat() and getDouble(),
    164      */
    165     if (!bigEndian) {
    166       dataByteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    167     }
    168     /*
    169      * Put the data into the result list
    170      */
    171     if (!doublePrecision) {
    172       /*
    173        * Precision == "32"
    174        */
    175       for (int i = 0; i < dataLength; i++) {
    176         float value = dataByteBuffer.getFloat();
    177         resultArray.add(i, new Float(value).doubleValue());
    178       }
    179     } else {
    180       /*
    181        * Precision == "64"
    182        */
    183       for (int i = 0; i < dataLength; i++) {
    184         double value = dataByteBuffer.getDouble();
    185         resultArray.add(i, value);
     141    if (numpress != null)
     142    {
     143      int length = dataByteArray.length;
     144      log.debug("Numpress decompression:"+numpress+". Array length:"+length);
     145      double[] result = MSNumpress.decode(numpress, dataByteArray, length);
     146      for (int i=0;i<result.length;i++)
     147      {
     148        resultArray.add(result[i]);
     149      }
     150    }
     151    else
     152    {
     153      ByteBuffer dataByteBuffer = ByteBuffer.wrap(dataByteArray);
     154      /*
     155       * Get size of result list from byte array size and precision.
     156       */
     157      int arraySize = dataByteArray.length;
     158      int bytesPerValue = 4;
     159      if (doublePrecision)
     160      {
     161        /*
     162         * Precision == "64"
     163         */
     164        bytesPerValue = 8;
     165      }
     166      int dataLength = arraySize / bytesPerValue;
     167      /*
     168       * Java works with big endian by default. If the data is little
     169       * endian, the byte order is changed. This configuration influences
     170       * how data is retrieved using methods like getFloat() and
     171       * getDouble(),
     172       */
     173      if (!bigEndian)
     174      {
     175        dataByteBuffer.order(ByteOrder.LITTLE_ENDIAN);
     176      }
     177      /*
     178       * Put the data into the result list
     179       */
     180      if (!doublePrecision)
     181      {
     182        /*
     183         * Precision == "32"
     184         */
     185        for (int i = 0; i < dataLength; i++)
     186        {
     187          float value = dataByteBuffer.getFloat();
     188          resultArray.add(i, new Float(value).doubleValue());
     189        }
     190      }
     191      else
     192      {
     193        /*
     194         * Precision == "64"
     195         */
     196        for (int i = 0; i < dataLength; i++)
     197        {
     198          double value = dataByteBuffer.getDouble();
     199          resultArray.add(i, value);
     200        }
    186201      }
    187202    }
     
    194209
    195210  /**
    196    * Encodes Base64 coded data String
    197    * and returns a String
    198    * containing the encoded data.
    199    *
    200    * Uses class org.proteios.core.Base64Coder to encode data.
    201    *
     211   * Encodes Base64 coded data String and returns a String containing the
     212   * encoded data. Uses class org.proteios.core.Base64Coder to encode data.
     213   *
    202214   * @param dataString String A string with data to be Base64 encoded.
    203215   * @return String A string with encoded data.
     
    215227
    216228  /**
    217    * Encodes Base64 coded data list<double>
    218    * and returns a <String>
    219    * containing the encoded data.
    220    *
    221    * Uses class org.proteios.core.Base64Coder to encode data.
    222    *
     229   * Encodes Base64 coded data list<double> and returns a <String> containing
     230   * the encoded data. Uses class org.proteios.core.Base64Coder to encode
     231   * data.
     232   *
    223233   * @param doublePrecision boolean flag, true if precision == "64".
    224234   * @param bigEndian boolean flag indicating endian type.
     
    226236   * @return <String> with encoded values.
    227237   */
    228   public static String encode(boolean doublePrecision, boolean bigEndian, List<? extends Number> inDataList)
    229   {
    230     /*
    231      * Get size of byte array from
    232      * input list size and precision.
     238  public static String encode(boolean doublePrecision, boolean bigEndian,
     239      List<? extends Number> inDataList)
     240  {
     241    /*
     242     * Get size of byte array from input list size and precision.
    233243     */
    234244    int dataLength = inDataList.size();
    235245    int bytesPerValue = 4;
    236     if (doublePrecision) {
     246    if (doublePrecision)
     247    {
    237248      /*
    238249       * Precision == "64"
     
    240251      bytesPerValue = 8;
    241252    }
    242     int arraySize = bytesPerValue*dataLength;
     253    int arraySize = bytesPerValue * dataLength;
    243254    /*
    244255     * Create dataByteBuffer ByteBuffer
     
    247258    ByteBuffer dataByteBuffer = ByteBuffer.wrap(dataByteArray);
    248259    /*
    249      * Java works with big endian by default.
    250      * If the data is little endian, the
    251      * byte order is changed.
    252      *
    253      * Note! This configuration must be set before
    254      * the ByteBuffer is loaded with data using
    255      * methods like putFloat() and putDouble(),
    256      * as it influences how the data will be stored.
    257      */
    258     if (!bigEndian) {
     260     * Java works with big endian by default. If the data is little endian,
     261     * the byte order is changed. Note! This configuration must be set
     262     * before the ByteBuffer is loaded with data using methods like
     263     * putFloat() and putDouble(), as it influences how the data will be
     264     * stored.
     265     */
     266    if (!bigEndian)
     267    {
    259268      dataByteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    260269    }
     
    262271     * Put the data into the byte buffer
    263272     */
    264     if (!doublePrecision) {
     273    if (!doublePrecision)
     274    {
    265275      /*
    266276       * Precision == "32"
    267277       */
    268       for (int i = 0; i < dataLength; i++) {
     278      for (int i = 0; i < dataLength; i++)
     279      {
    269280        if (inDataList.get(i) instanceof Double)
    270281        {
    271           Double doubleVal = (Double)inDataList.get(i);
     282          Double doubleVal = (Double) inDataList.get(i);
    272283          dataByteBuffer.putFloat(doubleVal.floatValue());
    273284        }
    274285        else if (inDataList.get(i) instanceof Integer)
    275286        {
    276           Integer intVal = (Integer)inDataList.get(i);
     287          Integer intVal = (Integer) inDataList.get(i);
    277288          dataByteBuffer.putInt(intVal.intValue());
    278289        }
    279290      }
    280     } else {
     291    }
     292    else
     293    {
    281294      /*
    282295       * Precision == "64"
    283296       */
    284       for (int i = 0; i < dataLength; i++) {
    285         Double doubleVal = (Double)inDataList.get(i);
     297      for (int i = 0; i < dataLength; i++)
     298      {
     299        Double doubleVal = (Double) inDataList.get(i);
    286300        dataByteBuffer.putDouble(doubleVal.doubleValue());
    287301      }
  • trunk/api/core/src/org/proteios/io/MzMLFileReader.java

    r4304 r4520  
    2828package org.proteios.io;
    2929
     30import ms.numpress.MSNumpress;
    3031import org.proteios.core.InvalidDataException;
    3132import org.xml.sax.SAXException;
     
    7980  private final String ACC_CHARGE_STATE = "MS:1000041";
    8081  private final String ACC_INTENSITY = "MS:1000042";
     82
    8183  private final HashMap<String, String> accInstrumentModelHashMap = new HashMap<String, String>();
    8284  /*
     
    98100  private String precision = new String("");
    99101  private String compression = new String("");
     102  private String numpress = null;
    100103  private String endian = new String("");
    101104  private int dataLength = 0;
     
    12051208                getSpectrumInstrumentData()
    12061209                  .setInstrumentSerialNo(instrumentSerialNo);
    1207                 log
    1208                   .debug("instrumentSerialNo: \"" + instrumentSerialNo + "\"");
     1210                log.debug("instrumentSerialNo: \"" + instrumentSerialNo + "\"");
    12091211              }
    12101212              // Check if instrument model set in referenceable
     
    12201222                getSpectrumInstrumentData().getAdditional()
    12211223                  .add(stringPair);
    1222                 log
    1223                   .debug("instrumentModel: \"" + instrumentModelStr + "\"");
     1224                log.debug("instrumentModel: \"" + instrumentModelStr + "\"");
    12241225              }
    12251226            }
     
    12551256              {
    12561257                inIntensityTargetBinaryDataArrayBlock = true;
    1257                 log
    1258                   .debug("intensity binaryDataArray block found");
     1258                log.debug("intensity binaryDataArray block found");
    12591259              }
    12601260              /*
     
    12691269              {
    12701270                setCompression(compression);
    1271                 log
    1272                   .debug("compression: \"" + compression + "\"");
     1271                log.debug("compression: \"" + compression + "\"");
    12731272              }
    12741273            }
     
    13221321            SpectrumImpl currentSpectrum = new SpectrumImpl();
    13231322            setSpectrum(currentSpectrum);
    1324             log
    1325               .debug("SpectrumId (" + spectrumIndex + ") = \"" + getCurrentSpectrumId() + "\" found.");
     1323            log.debug("SpectrumId (" + spectrumIndex + ") = \"" + getCurrentSpectrumId() + "\" found.");
    13261324          }
    13271325        }
     
    14921490            {
    14931491              // Add analyzer data to last list entry
    1494               analyzerList.get(listSize - 1).getAnalyzer().add(
    1495                 stringPair);
     1492              analyzerList.get(listSize - 1).getAnalyzer()
     1493                .add(stringPair);
    14961494            }
    14971495          }
     
    15921590            compression = new String("zlib");
    15931591          }
     1592          else if (currentCvParamAccessionStr
     1593            .equals(MSNumpress.ACC_NUMPRESS_LINEAR) || currentCvParamAccessionStr
     1594            .equals(MSNumpress.ACC_NUMPRESS_SLOF) || currentCvParamAccessionStr
     1595            .equals(MSNumpress.ACC_NUMPRESS_PIC))
     1596          {
     1597            numpress = currentCvParamAccessionStr;
     1598            log.debug ("Numpress compression detected:"+numpress);
     1599          }
     1600
    15941601          else if (currentCvParamAccessionStr.equals(ACC_MZ_ARRAY))
    15951602          {
     
    16811688            .getRetentionTimeInMinutes() == null)
    16821689          {
    1683             log
    1684               .debug("Accession value when looking for scan time = \"" + currentCvParamAccessionStr + "\"");
     1690            log.debug("Accession value when looking for scan time = \"" + currentCvParamAccessionStr + "\"");
    16851691            cvParamProcessed = true;
    16861692            // Check unit
     
    16881694            String currentCvParamUnitAccessionStr = XMLImportUtil
    16891695              .seekAttribute("unitAccession", parser);
    1690             log
    1691               .debug("currentCvParamUnitAccessionStr = \"" + currentCvParamUnitAccessionStr + "\"");
     1696            log.debug("currentCvParamUnitAccessionStr = \"" + currentCvParamUnitAccessionStr + "\"");
    16921697            if (currentCvParamUnitAccessionStr != null)
    16931698            {
     
    17171722            retentionTimeStr = XMLImportUtil.seekAttribute("value",
    17181723              parser);
    1719             log
    1720               .debug("retentionTimeStr =\"" + retentionTimeStr + "\"");
     1724            log.debug("retentionTimeStr =\"" + retentionTimeStr + "\"");
    17211725            if (retentionTimeStr != null && !retentionTimeStr
    17221726              .equals(""))
     
    17371741                getSpectrum().setRetentionTimeInMinutes(
    17381742                  retentionTime);
    1739                 log
    1740                   .debug("cvParam block with retentionTimeInMinutes found, time = " + retentionTime);
     1743                log.debug("cvParam block with retentionTimeInMinutes found, time = " + retentionTime);
    17411744              }
    17421745            }
     
    17451748            .equals(ACC_SELECTED_MASS_TO_CHARGE_RATIO))
    17461749          {
    1747             log
    1748               .debug("Accession value when looking for precursor m/z = \"" + currentCvParamAccessionStr + "\"");
     1750            log.debug("Accession value when looking for precursor m/z = \"" + currentCvParamAccessionStr + "\"");
    17491751            cvParamProcessed = true;
    17501752            if (inTargetPrecursorBlock)
     
    17631765            .equals(ACC_CHARGE_STATE))
    17641766          {
    1765             log
    1766               .debug("Accession value when looking for precursor charge state = \"" + currentCvParamAccessionStr + "\"");
     1767            log.debug("Accession value when looking for precursor charge state = \"" + currentCvParamAccessionStr + "\"");
    17671768            cvParamProcessed = true;
    17681769            if (inTargetPrecursorBlock)
     
    17801781          else if (currentCvParamAccessionStr.equals(ACC_INTENSITY))
    17811782          {
    1782             log
    1783               .debug("Accession value when looking for precursor intensity = \"" + currentCvParamAccessionStr + "\"");
     1783            log.debug("Accession value when looking for precursor intensity = \"" + currentCvParamAccessionStr + "\"");
    17841784            cvParamProcessed = true;
    17851785            if (inTargetPrecursorBlock)
     
    17921792                  Double.valueOf(intensity));
    17931793              }
    1794               log
    1795                 .debug("precursor intensity =\"" + intensity + "\"");
     1794              log.debug("precursor intensity =\"" + intensity + "\"");
    17961795            }
    17971796          }
     
    18721871            {
    18731872              // Add analyzer data to last list entry
    1874               analyzerList.get(listSize - 1).getAnalyzer().add(
    1875                 stringPair);
     1873              analyzerList.get(listSize - 1).getAnalyzer()
     1874                .add(stringPair);
    18761875            }
    18771876          }
     
    19131912        setEndian("little");
    19141913        setDataLength(dataLength);
     1914        numpress = null;
    19151915        log.debug("dataLength: " + dataLength);
    1916         log
    1917           .debug("endian: \"" + endian + "\" dataLength: " + dataLength);
     1916        log.debug("endian: \"" + endian + "\" dataLength: " + dataLength);
    19181917      }
    19191918    }
     
    19251924        log.debug("binary block found");
    19261925
    1927         log
    1928           .debug("precision: \"" + precision + "\" endian: \"" + endian + "\" dataLength: " + dataLength);
     1926        log.debug("precision: \"" + precision + "\" endian: \"" + endian + "\" dataLength: " + dataLength);
    19291927      }
    19301928    }
     
    20282026            spectrumArray[i] = getSpectrum();
    20292027            spectrumTagsFound++;
    2030             log
    2031               .debug("SpectrumId (" + i + ") = \"" + getCurrentSpectrumId() + "\" processed, spectrumTagsFound = " + spectrumTagsFound);
    2032             log
    2033               .debug("-----------------------------------------------");
     2028            log.debug("SpectrumId (" + i + ") = \"" + getCurrentSpectrumId() + "\" processed, spectrumTagsFound = " + spectrumTagsFound);
     2029            log.debug("-----------------------------------------------");
    20342030          }
    20352031        }
     
    20792075         */
    20802076        decodedBase64List = dataItem(doublePrecision, bigEndian,
    2081           zLibCompression, dataBase64);
     2077          zLibCompression, numpress, dataBase64);
    20822078
    20832079        /*
     
    21542150   */
    21552151  private List<Double> dataItem(boolean doublePrecision, boolean bigEndian,
    2156       boolean zLibCompression, String dataBase64Raw)
     2152      boolean zLibCompression, String numpress, String dataBase64Raw)
    21572153  {
    21582154    /*
     
    21972193    }
    21982194    String dataBase64 = dataBase64StrBuf.toString();
    2199     log
    2200       .debug("nLines = " + nLines + " lineLength = " + lineLength + " nChars = " + nChars + " dataBase64Raw.length() = " + dataBase64Raw
    2201         .length());
     2195    log.debug("nLines = " + nLines + " lineLength = " + lineLength + " nChars = " + nChars + " dataBase64Raw.length() = " + dataBase64Raw
     2196      .length());
    22022197
    22032198    /*
     
    22112206      {
    22122207        decodedBase64 = Base64Util.decode(doublePrecision, bigEndian,
    2213           zLibCompression, dataBase64);
     2208          zLibCompression, numpress, dataBase64);
    22142209      }
    22152210    }
     
    22642259    List<SpectrumInterface> spectrumList = new ArrayList<SpectrumInterface>();
    22652260    spectrumList = fetchSpectrum(spectrumIds);
    2266     log
    2267       .debug("spectrumIds.size() = " + spectrumIds.size() + ", spectrumList.size() = " + spectrumList
    2268         .size());
     2261    log.debug("spectrumIds.size() = " + spectrumIds.size() + ", spectrumList.size() = " + spectrumList
     2262      .size());
    22692263
    22702264    /*
  • trunk/api/core/src/org/proteios/io/PeakListFileImpl.java

    r3274 r4520  
    15401540      if (dataBase64.length() > 0)
    15411541      {
    1542         decodedBase64 = Base64Util.decode(doublePrecision, bigEndian,
     1542        decodedBase64 = Base64Util.decode(doublePrecision, bigEndian, null,
    15431543          dataBase64);
    15441544      }
  • trunk/api/core/src/org/proteios/io/mzdata/MzDataImpDataBlock.java

    r3207 r4520  
    345345    if (dataBase64 != null) {
    346346      if (dataBase64.length() > 0) {
    347         decodedBase64 = Base64Util.decode(doublePrecision, bigEndian, dataBase64);
     347        decodedBase64 = Base64Util.decode(doublePrecision, bigEndian, null, dataBase64);
    348348      }
    349349    }
Note: See TracChangeset for help on using the changeset viewer.