cn.edu.xmu.tidems.control.ui.views.components.MzCalibrationDialog.java Source code

Java tutorial

Introduction

Here is the source code for cn.edu.xmu.tidems.control.ui.views.components.MzCalibrationDialog.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.ui.views.components;

import java.io.File;

import org.eclipse.chemclipse.logging.core.Logger;
import org.eclipse.chemclipse.model.core.IChromatogram;
import org.eclipse.chemclipse.model.core.IScan;
import org.eclipse.chemclipse.msd.model.core.selection.IChromatogramSelectionMSD;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import ch.systemsx.cisd.hdf5.HDF5Factory;
import ch.systemsx.cisd.hdf5.IHDF5Writer;
import cn.edu.xmu.tidems.control.model.TideMSScanProxy;
import cn.edu.xmu.tidems.control.preferences.PreferenceSupplier;
import cn.edu.xmu.tidems.control.support.IConstants;
import cn.edu.xmu.tidems.control.support.TideMSUtil;

public class MzCalibrationDialog extends Dialog {

    private static final Logger logger = Logger.getLogger(MzCalibrationDialog.class);
    private IChromatogramSelectionMSD chromatogramSelection;
    private final Text[] txtTof = new Text[3];
    private final Text[] txtMz = new Text[3];

    public MzCalibrationDialog(final Shell parentShell) {
        super(parentShell);
    }

    @Override
    protected void buttonPressed(final int buttonId) {

        if (buttonId == IDialogConstants.OK_ID) {
            if (checkInput()) {
                final int size = 3;
                final double[] tof = new double[size];
                final double[] mz = new double[size];
                for (int i = 0; i < size; i++) {
                    tof[i] = Double.parseDouble(txtTof[i].getText().trim());
                    mz[i] = Double.parseDouble(txtMz[i].getText().trim());
                }
                final double[] factors = TideMSUtil.polynomialFit(tof, mz, 2);
                updateMZCalibrationFactor(factors);
                close();
            }
        } else {
            close();
        }
    }

    private boolean checkInput() {

        for (int i = 0; i < 3; i++) {
            try {
                Double.parseDouble(txtTof[i].getText().trim());
            } catch (final Exception e) {
                MessageDialog.openWarning(getShell(), "Warning", "Input must be a number");
                txtTof[i].setFocus();
                return false;
            }
            try {
                Double.parseDouble(txtMz[i].getText().trim());
            } catch (final Exception e) {
                MessageDialog.openWarning(getShell(), "Warning", "Input must be a number");
                txtMz[i].setFocus();
                return false;
            }
        }
        return true;
    }

    @Override
    protected void createButtonsForButtonBar(final Composite parent) {

        createButton(parent, IDialogConstants.OK_ID, "Calibrate", true);
        createButton(parent, IDialogConstants.CANCEL_ID, "Cancel", false);
        // parent.pack();
    }

    @Override
    protected Control createDialogArea(final Composite parent) {

        getShell().setText("Calibrate m/z");
        final Composite pane = new Composite(parent, SWT.NONE);
        pane.setLayout(new GridLayout(3, false));
        final GridData gdSpan3 = new GridData(SWT.FILL, SWT.TOP, true, false);
        gdSpan3.horizontalSpan = 3;
        //
        final Label lblDesc = new Label(pane, SWT.NONE);
        lblDesc.setText("m/z = a + b*TOF + c*TOF*TOF");
        lblDesc.setLayoutData(gdSpan3);
        final Label lblDesc2 = new Label(pane, SWT.NONE);
        lblDesc2.setText("(TOF unit: nanosecond)");
        lblDesc2.setLayoutData(gdSpan3);
        //
        new Label(pane, SWT.NONE).setText("No.");
        final Label lblTof = new Label(pane, SWT.NONE);
        lblTof.setText("TOF(ns)");
        lblTof.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, true));
        final Label lblMz = new Label(pane, SWT.NONE);
        lblMz.setText("m/z");
        lblMz.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, true));
        //
        new Label(pane, SWT.NONE).setText("1.");
        txtTof[0] = new Text(pane, SWT.BORDER);
        txtMz[0] = new Text(pane, SWT.BORDER);
        //
        new Label(pane, SWT.NONE).setText("2.");
        txtTof[1] = new Text(pane, SWT.BORDER);
        txtMz[1] = new Text(pane, SWT.BORDER);
        //
        new Label(pane, SWT.NONE).setText("3.");
        txtTof[2] = new Text(pane, SWT.BORDER);
        txtMz[2] = new Text(pane, SWT.BORDER);
        // pane.pack();
        return pane;
    }

    public IChromatogramSelectionMSD getChromatogramSelection() {

        return chromatogramSelection;
    }

    @Override
    protected Point getInitialSize() {

        return new Point(300, 250);
    }

    public void setChromatogramSelection(final IChromatogramSelectionMSD chromatogramSelection) {

        this.chromatogramSelection = chromatogramSelection;
    }

    /**
     * @param factors
     */
    private void updateMZCalibrationFactor(final double[] factors) {

        PreferenceSupplier.setMZCalibrationFactor(factors);
        if (chromatogramSelection != null) {
            try {
                final File file = chromatogramSelection.getChromatogram().getFile();
                IHDF5Writer updator = HDF5Factory.open(file);
                updator.float64().setArrayAttr(IConstants.FILE_ACQUISITION_INFO, "mz_calibration_factors", factors);
                updator.close();
                updator = null;
                final IChromatogram chrom = chromatogramSelection.getChromatogram();
                for (final IScan scan : chrom.getScans()) {
                    if (scan instanceof TideMSScanProxy) {
                        ((TideMSScanProxy) scan).setProxy(true);
                    }
                }
                chromatogramSelection.update(true);
            } catch (final Exception e) {
                logger.warn(e);
            }
        }
    }
}