Search in sources :

Example 1 with IndexIterator

use of ucar.ma2.IndexIterator in project mzmine2 by mzmine.

the class NetCDFReadTask method startReading.

public void startReading() throws IOException {
    // Open NetCDF-file
    try {
        inputFile = NetcdfFile.open(file.getPath());
    } catch (Exception e) {
        logger.severe(e.toString());
        throw (new IOException("Couldn't open input file" + file));
    }
    /*
     * DEBUG: dump all variables for (Variable v : inputFile.getVariables()) {
     * System.out.println("variable " + v.getShortName()); }
     */
    // Find mass_values and intensity_values variables
    massValueVariable = inputFile.findVariable("mass_values");
    if (massValueVariable == null) {
        logger.severe("Could not find variable mass_values");
        throw (new IOException("Could not find variable mass_values"));
    }
    assert (massValueVariable.getRank() == 1);
    Attribute massScaleFacAttr = massValueVariable.findAttribute("scale_factor");
    if (massScaleFacAttr != null) {
        massValueScaleFactor = massScaleFacAttr.getNumericValue().doubleValue();
    }
    intensityValueVariable = inputFile.findVariable("intensity_values");
    if (intensityValueVariable == null) {
        logger.severe("Could not find variable intensity_values");
        throw (new IOException("Could not find variable intensity_values"));
    }
    assert (intensityValueVariable.getRank() == 1);
    Attribute intScaleFacAttr = intensityValueVariable.findAttribute("scale_factor");
    if (intScaleFacAttr != null) {
        intensityValueScaleFactor = intScaleFacAttr.getNumericValue().doubleValue();
    }
    // Read number of scans
    Variable scanIndexVariable = inputFile.findVariable("scan_index");
    if (scanIndexVariable == null) {
        logger.severe("Could not find variable scan_index from file " + file);
        throw (new IOException("Could not find variable scan_index from file " + file));
    }
    totalScans = scanIndexVariable.getShape()[0];
    // Read scan start positions
    // Extra element is required, because element totalScans+1 is used to
    // find the stop position for last scan
    int[] scanStartPositions = new int[totalScans + 1];
    Array scanIndexArray = null;
    try {
        scanIndexArray = scanIndexVariable.read();
    } catch (Exception e) {
        logger.severe(e.toString());
        throw (new IOException("Could not read from variable scan_index from file " + file));
    }
    IndexIterator scanIndexIterator = scanIndexArray.getIndexIterator();
    int ind = 0;
    while (scanIndexIterator.hasNext()) {
        scanStartPositions[ind] = ((Integer) scanIndexIterator.next()).intValue();
        ind++;
    }
    scanIndexIterator = null;
    scanIndexArray = null;
    scanIndexVariable = null;
    // Calc stop position for the last scan
    // This defines the end index of the last scan
    scanStartPositions[totalScans] = (int) massValueVariable.getSize();
    // Start scan RT
    double[] retentionTimes = new double[totalScans];
    Variable scanTimeVariable = inputFile.findVariable("scan_acquisition_time");
    if (scanTimeVariable == null) {
        logger.severe("Could not find variable scan_acquisition_time from file " + file);
        throw (new IOException("Could not find variable scan_acquisition_time from file " + file));
    }
    Array scanTimeArray = null;
    try {
        scanTimeArray = scanTimeVariable.read();
    } catch (Exception e) {
        logger.severe(e.toString());
        throw (new IOException("Could not read from variable scan_acquisition_time from file " + file));
    }
    IndexIterator scanTimeIterator = scanTimeArray.getIndexIterator();
    ind = 0;
    while (scanTimeIterator.hasNext()) {
        if (scanTimeVariable.getDataType().getPrimitiveClassType() == float.class) {
            retentionTimes[ind] = ((Float) scanTimeIterator.next()) / 60d;
        }
        if (scanTimeVariable.getDataType().getPrimitiveClassType() == double.class) {
            retentionTimes[ind] = ((Double) scanTimeIterator.next()) / 60d;
        }
        ind++;
    }
    // End scan RT
    // Cleanup
    scanTimeIterator = null;
    scanTimeArray = null;
    scanTimeVariable = null;
    // Fix problems caused by new QStar data converter
    // assume scan is missing when scan_index[i]<0
    // for these scans, fix variables:
    // - scan_acquisition_time: interpolate/extrapolate using times of
    // present scans
    // - scan_index: fill with following good value
    // Calculate number of good scans
    numberOfGoodScans = 0;
    for (int i = 0; i < totalScans; i++) {
        if (scanStartPositions[i] >= 0) {
            numberOfGoodScans++;
        }
    }
    // Is there need to fix something?
    if (numberOfGoodScans < totalScans) {
        // Fix scan_acquisition_time
        // - calculate average delta time between present scans
        double sumDelta = 0;
        int n = 0;
        for (int i = 0; i < totalScans; i++) {
            // Is this a present scan?
            if (scanStartPositions[i] >= 0) {
                // Yes, find next present scan
                for (int j = i + 1; j < totalScans; j++) {
                    if (scanStartPositions[j] >= 0) {
                        sumDelta += (retentionTimes[j] - retentionTimes[i]) / ((double) (j - i));
                        n++;
                        break;
                    }
                }
            }
        }
        double avgDelta = sumDelta / (double) n;
        // - fill missing scan times using nearest good scan and avgDelta
        for (int i = 0; i < totalScans; i++) {
            // Is this a missing scan?
            if (scanStartPositions[i] < 0) {
                // Yes, find nearest present scan
                int nearestI = Integer.MAX_VALUE;
                for (int j = 1; 1 < 2; j++) {
                    if ((i + j) < totalScans) {
                        if (scanStartPositions[i + j] >= 0) {
                            nearestI = i + j;
                            break;
                        }
                    }
                    if ((i - j) >= 0) {
                        if (scanStartPositions[i - j] >= 0) {
                            nearestI = i + j;
                            break;
                        }
                    }
                    // Out of bounds?
                    if (((i + j) >= totalScans) && ((i - j) < 0)) {
                        break;
                    }
                }
                if (nearestI != Integer.MAX_VALUE) {
                    retentionTimes[i] = retentionTimes[nearestI] + (i - nearestI) * avgDelta;
                } else {
                    if (i > 0) {
                        retentionTimes[i] = retentionTimes[i - 1];
                    } else {
                        retentionTimes[i] = 0;
                    }
                    logger.severe("ERROR: Could not fix incorrect QStar scan times.");
                }
            }
        }
        // Fix scanStartPositions by filling gaps with next good value
        for (int i = 0; i < totalScans; i++) {
            if (scanStartPositions[i] < 0) {
                for (int j = i + 1; j < (totalScans + 1); j++) {
                    if (scanStartPositions[j] >= 0) {
                        scanStartPositions[i] = scanStartPositions[j];
                        break;
                    }
                }
            }
        }
    }
    // Collect information about retention times, start positions and
    // lengths for scans
    scansRetentionTimes = new Hashtable<Integer, Double>();
    scansIndex = new Hashtable<Integer, Integer[]>();
    for (int i = 0; i < totalScans; i++) {
        Integer scanNum = new Integer(i);
        Integer[] startAndLength = new Integer[2];
        startAndLength[0] = scanStartPositions[i];
        startAndLength[1] = scanStartPositions[i + 1] - scanStartPositions[i];
        scansRetentionTimes.put(scanNum, new Double(retentionTimes[i]));
        scansIndex.put(scanNum, startAndLength);
    }
    scanStartPositions = null;
    retentionTimes = null;
}
Also used : Variable(ucar.nc2.Variable) Attribute(ucar.nc2.Attribute) IOException(java.io.IOException) IndexIterator(ucar.ma2.IndexIterator) IOException(java.io.IOException) DataPoint(net.sf.mzmine.datamodel.DataPoint) SimpleDataPoint(net.sf.mzmine.datamodel.impl.SimpleDataPoint) Array(ucar.ma2.Array)

Aggregations

IOException (java.io.IOException)1 DataPoint (net.sf.mzmine.datamodel.DataPoint)1 SimpleDataPoint (net.sf.mzmine.datamodel.impl.SimpleDataPoint)1 Array (ucar.ma2.Array)1 IndexIterator (ucar.ma2.IndexIterator)1 Attribute (ucar.nc2.Attribute)1 Variable (ucar.nc2.Variable)1