Search in sources :

Example 1 with PCA

use of jmprojection.PCA in project mzmine2 by mzmine.

the class ClusteringTask method run.

@Override
public void run() {
    status = TaskStatus.PROCESSING;
    logger.info("Clustering");
    double[][] rawData;
    if (typeOfData == ClusteringDataType.VARIABLES) {
        rawData = createMatrix(false);
        dataset = createVariableWekaDataset(rawData);
    } else {
        rawData = createMatrix(true);
        dataset = createSampleWekaDataset(rawData);
    }
    // Run the clustering algorithm
    ClusteringAlgorithm clusteringAlgorithm = clusteringStep.getModule();
    ParameterSet clusteringParameters = clusteringStep.getParameterSet();
    ClusteringResult result = clusteringAlgorithm.performClustering(dataset, clusteringParameters);
    String cluster = "";
    if (clusteringAlgorithm.getName().toString().equals("Hierarchical clusterer")) {
        progress = 0;
        // Getting the result of the clustering in Newick format
        cluster = result.getHiearchicalCluster();
        // Getting the number of clusters counting the number of times the
        // word "cluster" is in the result
        Pattern p = Pattern.compile("Cluster", Pattern.LITERAL | Pattern.CASE_INSENSITIVE);
        int numberOfClusters = p.split(cluster, -1).length - 1;
        if (numberOfClusters == 0) {
            numberOfClusters = 1;
        }
        // Visualization window for each cluster
        for (int i = 0; i < numberOfClusters; i++) {
            String c = null;
            String clusterNumber = "Cluster " + i;
            if (cluster.indexOf(clusterNumber) > 0) {
                int nextNumber = i + 1;
                String clusterNumber2 = "Cluster " + nextNumber;
                if (cluster.indexOf(clusterNumber2) < 0) {
                    c = cluster.substring(cluster.indexOf(clusterNumber) + clusterNumber.length(), cluster.length());
                } else {
                    c = cluster.substring(cluster.indexOf(clusterNumber) + clusterNumber.length(), cluster.indexOf(clusterNumber2));
                }
            } else {
                c = cluster;
            }
            JFrame visualizationWindow = new JFrame(clusterNumber);
            visualizationWindow.setSize(600, 500);
            visualizationWindow.setLayout(new BorderLayout());
            HierarchyVisualizer visualizer = new HierarchyVisualizer(c);
            visualizationWindow.add(visualizer, BorderLayout.CENTER);
            visualizer.fitToScreen();
            // Text field with the clustering result in Newick format
            JTextField data = new JTextField(c);
            visualizationWindow.add(data, BorderLayout.SOUTH);
            visualizationWindow.setVisible(true);
            visualizationWindow.pack();
            visualizationWindow.setVisible(true);
        }
        progress = 100;
    } else {
        List<Integer> clusteringResult = result.getClusters();
        // Report window
        Desktop desktop = MZmineCore.getDesktop();
        if (typeOfData == ClusteringDataType.SAMPLES) {
            String[] sampleNames = new String[selectedRawDataFiles.length];
            for (int i = 0; i < selectedRawDataFiles.length; i++) {
                sampleNames[i] = selectedRawDataFiles[i].getName();
            }
            ClusteringReportWindow reportWindow = new ClusteringReportWindow(sampleNames, clusteringResult.toArray(new Integer[0]), "Clustering Report");
            reportWindow.setVisible(true);
        } else {
            String[] variableNames = new String[selectedRows.length];
            for (int i = 0; i < selectedRows.length; i++) {
                variableNames[i] = selectedRows[i].getID() + " - " + selectedRows[i].getAverageMZ() + " - " + selectedRows[i].getAverageRT();
                if (selectedRows[i].getPeakIdentities() != null && selectedRows[i].getPeakIdentities().length > 0) {
                    variableNames[i] += " - " + selectedRows[i].getPeakIdentities()[0].getName();
                }
            }
            ClusteringReportWindow reportWindow = new ClusteringReportWindow(variableNames, clusteringResult.toArray(new Integer[0]), "Clustering Report");
            reportWindow.setVisible(true);
        }
        // Visualization
        if (typeOfData == ClusteringDataType.VARIABLES) {
            for (int ind = 0; ind < selectedRows.length; ind++) {
                groupsForSelectedVariables[ind] = clusteringResult.get(ind);
            }
        } else {
            for (int ind = 0; ind < selectedRawDataFiles.length; ind++) {
                groupsForSelectedRawDataFiles[ind] = clusteringResult.get(ind);
            }
        }
        this.finalNumberOfGroups = result.getNumberOfGroups();
        parameterValuesForGroups = new Object[finalNumberOfGroups];
        for (int i = 0; i < finalNumberOfGroups; i++) {
            parameterValuesForGroups[i] = "Group " + i;
        }
        int numComponents = xAxisDimension;
        if (yAxisDimension > numComponents) {
            numComponents = yAxisDimension;
        }
        if (result.getVisualizationType() == VisualizationType.PCA) {
            // Scale data and do PCA
            Preprocess.scaleToUnityVariance(rawData);
            PCA pcaProj = new PCA(rawData, numComponents);
            projectionStatus = pcaProj.getProjectionStatus();
            double[][] pcaResult = pcaProj.getState();
            if (status == TaskStatus.CANCELED) {
                return;
            }
            component1Coords = pcaResult[xAxisDimension - 1];
            component2Coords = pcaResult[yAxisDimension - 1];
        } else if (result.getVisualizationType() == VisualizationType.SAMMONS) {
            // Scale data and do Sammon's mapping
            Preprocess.scaleToUnityVariance(rawData);
            Sammons sammonsProj = new Sammons(rawData);
            projectionStatus = sammonsProj.getProjectionStatus();
            sammonsProj.iterate(100);
            double[][] sammonsResult = sammonsProj.getState();
            if (status == TaskStatus.CANCELED) {
                return;
            }
            component1Coords = sammonsResult[xAxisDimension - 1];
            component2Coords = sammonsResult[yAxisDimension - 1];
        }
        ProjectionPlotWindow newFrame = new ProjectionPlotWindow(desktop.getSelectedPeakLists()[0], this, parameters);
        newFrame.setVisible(true);
    }
    status = TaskStatus.FINISHED;
    logger.info("Finished computing Clustering visualization.");
}
Also used : ParameterSet(net.sf.mzmine.parameters.ParameterSet) Pattern(java.util.regex.Pattern) HierarchyVisualizer(weka.gui.hierarchyvisualizer.HierarchyVisualizer) JTextField(javax.swing.JTextField) PCA(jmprojection.PCA) ProjectionPlotWindow(net.sf.mzmine.modules.peaklistmethods.dataanalysis.projectionplots.ProjectionPlotWindow) BorderLayout(java.awt.BorderLayout) Desktop(net.sf.mzmine.desktop.Desktop) JFrame(javax.swing.JFrame) Sammons(jmprojection.Sammons)

Example 2 with PCA

use of jmprojection.PCA in project mzmine2 by mzmine.

the class PCADataset method run.

@Override
public void run() {
    status = TaskStatus.PROCESSING;
    logger.info("Computing PCA projection plot");
    // Generate matrix of raw data (input to PCA)
    final boolean useArea = (parameters.getParameter(ProjectionPlotParameters.peakMeasurementType).getValue() == PeakMeasurementType.AREA);
    if (selectedRows.length == 0) {
        this.status = TaskStatus.ERROR;
        errorMessage = "No peaks selected for PCA plot";
        return;
    }
    if (selectedRawDataFiles.length == 0) {
        this.status = TaskStatus.ERROR;
        errorMessage = "No raw data files selected for PCA plot";
        return;
    }
    double[][] rawData = new double[selectedRawDataFiles.length][selectedRows.length];
    for (int rowIndex = 0; rowIndex < selectedRows.length; rowIndex++) {
        PeakListRow peakListRow = selectedRows[rowIndex];
        for (int fileIndex = 0; fileIndex < selectedRawDataFiles.length; fileIndex++) {
            RawDataFile rawDataFile = selectedRawDataFiles[fileIndex];
            Feature p = peakListRow.getPeak(rawDataFile);
            if (p != null) {
                if (useArea)
                    rawData[fileIndex][rowIndex] = p.getArea();
                else
                    rawData[fileIndex][rowIndex] = p.getHeight();
            }
        }
    }
    int numComponents = xAxisPC;
    if (yAxisPC > numComponents)
        numComponents = yAxisPC;
    // Scale data and do PCA
    Preprocess.scaleToUnityVariance(rawData);
    // Replace NaN values with 0.0
    for (int i = 0; i < rawData.length; i++) {
        for (int j = 0; j < rawData[i].length; j++) {
            if (Double.isNaN(rawData[i][j]))
                rawData[i][j] = 0.0;
        }
    }
    PCA pcaProj = new PCA(rawData, numComponents);
    projectionStatus = pcaProj.getProjectionStatus();
    double[][] result = pcaProj.getState();
    if (status == TaskStatus.CANCELED)
        return;
    component1Coords = result[xAxisPC - 1];
    component2Coords = result[yAxisPC - 1];
    ProjectionPlotWindow newFrame = new ProjectionPlotWindow(peakList, this, parameters);
    newFrame.setVisible(true);
    status = TaskStatus.FINISHED;
    logger.info("Finished computing projection plot.");
}
Also used : PeakListRow(net.sf.mzmine.datamodel.PeakListRow) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) Feature(net.sf.mzmine.datamodel.Feature) PCA(jmprojection.PCA)

Aggregations

PCA (jmprojection.PCA)2 BorderLayout (java.awt.BorderLayout)1 Pattern (java.util.regex.Pattern)1 JFrame (javax.swing.JFrame)1 JTextField (javax.swing.JTextField)1 Sammons (jmprojection.Sammons)1 Feature (net.sf.mzmine.datamodel.Feature)1 PeakListRow (net.sf.mzmine.datamodel.PeakListRow)1 RawDataFile (net.sf.mzmine.datamodel.RawDataFile)1 Desktop (net.sf.mzmine.desktop.Desktop)1 ProjectionPlotWindow (net.sf.mzmine.modules.peaklistmethods.dataanalysis.projectionplots.ProjectionPlotWindow)1 ParameterSet (net.sf.mzmine.parameters.ParameterSet)1 HierarchyVisualizer (weka.gui.hierarchyvisualizer.HierarchyVisualizer)1