cn.edu.xmu.tidems.control.support.TideMSUtil.java Source code

Java tutorial

Introduction

Here is the source code for cn.edu.xmu.tidems.control.support.TideMSUtil.java

Source

/*******************************************************************************
 * Copyright (c) 2016, 2017 Trig Chen.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Trig Chen - initial API and implementation
 *******************************************************************************/
package cn.edu.xmu.tidems.control.support;

import java.io.File;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;

import org.apache.commons.math3.fitting.PolynomialCurveFitter;
import org.apache.commons.math3.fitting.WeightedObservedPoints;

import cn.edu.xmu.tidems.control.model.TideMSIon;

public class TideMSUtil {

    // private static final Logger logger = Logger.getLogger(TideMSUtil.class);
    public static double[] calculateMzFromTOF(final double[] tofs, final double[] factors) {

        if ((factors == null) || (tofs == null) || (tofs.length == 0)) {
            return tofs;
        }
        double[] mzs = new double[tofs.length];
        switch (factors.length) {
        case 0:
            mzs = tofs;
            break;
        case 1:
            for (int i = 0; i < tofs.length; i++) {
                mzs[i] = factors[0] * tofs[i] * tofs[i];
                if (mzs[i] < TideMSIon.MIN_ION) {
                    mzs[i] = TideMSIon.MIN_ION;
                }
                if (mzs[i] > TideMSIon.MAX_ION) {
                    mzs[i] = TideMSIon.MAX_ION;
                }
            }
            break;
        case 2:
            for (int i = 0; i < tofs.length; i++) {
                mzs[i] = factors[0] + (factors[1] * tofs[i] * tofs[i]);
                if (mzs[i] < TideMSIon.MIN_ION) {
                    mzs[i] = TideMSIon.MIN_ION;
                }
                if (mzs[i] > TideMSIon.MAX_ION) {
                    mzs[i] = TideMSIon.MAX_ION;
                }
            }
            break;
        default:
            for (int i = 0; i < tofs.length; i++) {
                mzs[i] = factors[0] + (factors[1] * tofs[i]) + (factors[2] * tofs[i] * tofs[i]);
                if (mzs[i] < TideMSIon.MIN_ION) {
                    mzs[i] = TideMSIon.MIN_ION;
                }
                if (mzs[i] > TideMSIon.MAX_ION) {
                    mzs[i] = TideMSIon.MAX_ION;
                }
            }
            break;
        }
        return mzs;
    }

    public static double calculateMzFromTOF(final double tof, final double[] factors) {

        if (factors == null) {
            return tof;
        }
        double mz = 1.0D;
        switch (factors.length) {
        case 0:
            mz = tof;
            break;
        case 1:
            mz = factors[0] * tof * tof;
            break;
        case 2:
            mz = factors[0] + (factors[1] * tof * tof);
            break;
        default:
            mz = factors[0] + (factors[1] * tof) + (factors[2] * tof * tof);
            break;
        }
        if (mz < TideMSIon.MIN_ION) {
            mz = TideMSIon.MIN_ION;
        }
        if (mz > TideMSIon.MAX_ION) {
            mz = TideMSIon.MAX_ION;
        }
        return mz;
    }

    public static double[] polynomialFit(final double[] x, final double[] y, final int order) {

        if ((x == null) || (x.length == 0) || (y == null) || (y.length == 0)) {
            throw new IllegalArgumentException("Input arrays x/y  can't be null or empty.");
        }
        if (x.length != y.length) {
            throw new IllegalArgumentException("The length of arrays x, y must be equal.");
        }
        if (order < 1) {
            throw new IllegalArgumentException("Order must be greater than 0.");
        }
        if (x.length <= order) {
            throw new IllegalArgumentException("The length of array must be greater than order.");
        }
        final WeightedObservedPoints obs = new WeightedObservedPoints();
        for (int i = 0; i < x.length; i++) {
            obs.add(x[i], y[i]);
        }
        final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(order);
        final double[] coeff = fitter.fit(obs.toList());
        return coeff;
    }

    public static int getMinValue(final int[] data) {

        if ((data == null) || (data.length == 0)) {
            throw new IllegalArgumentException("Input array can't be null or empty.");
        }
        int min = data[0];
        for (final int d : data) {
            if (d < min) {
                min = d;
            }
        }
        return min;
    }

    public static float getMinValue(final float[] data) {

        if ((data == null) || (data.length == 0)) {
            throw new IllegalArgumentException("Input array can't be null or empty.");
        }
        float min = data[0];
        for (final float d : data) {
            if (d < min) {
                min = d;
            }
        }
        return min;
    }

    public static double getMinValue(final double[] data) {

        if ((data == null) || (data.length == 0)) {
            throw new IllegalArgumentException("Input array can't be null or empty.");
        }
        double min = data[0];
        for (final double d : data) {
            if (d < min) {
                min = d;
            }
        }
        return min;
    }

    public static int getMaxValue(final int[] data) {

        if ((data == null) || (data.length == 0)) {
            throw new IllegalArgumentException("Input array can't be null or empty.");
        }
        int max = data[0];
        for (final int d : data) {
            if (d > max) {
                max = d;
            }
        }
        return max;
    }

    public static double getMaxValue(final double[] data) {

        if ((data == null) || (data.length == 0)) {
            throw new IllegalArgumentException("Input array can't be null or empty.");
        }
        double max = data[0];
        for (final double d : data) {
            if (d > max) {
                max = d;
            }
        }
        return max;
    }

    public static float getMaxValue(final float[] data) {

        if ((data == null) || (data.length == 0)) {
            throw new IllegalArgumentException("Input array can't be null or empty.");
        }
        float max = data[0];
        for (final float d : data) {
            if (d > max) {
                max = d;
            }
        }
        return max;
    }

    public static boolean isHDF5File(final File file) {

        if ((!file.exists()) || (file.isDirectory()) || (file.length() < 1024)) {
            return false;
        }
        try {
            final RandomAccessFile raf = new RandomAccessFile(file, "r");
            final long sig = raf.readLong(); // The first 8 byte are format signatures of hdf5 file
            raf.close();
            return sig == -8554512424533091830L; // 0x89 48 44 46 0d 0a 1a 0a
        } catch (final Exception e) {
            return false;
        }
    }

    public static boolean isLatinString(final String str) {

        try {
            return str.equals(new String(str.getBytes("ISO-8859-1"), "ISO-8859-1"));
        } catch (final UnsupportedEncodingException e) {
            return false;
        }
    }
}