Search in sources :

Example 1 with ProjectionPlotWindow

use of net.sf.mzmine.modules.peaklistmethods.dataanalysis.projectionplots.ProjectionPlotWindow 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)

Aggregations

BorderLayout (java.awt.BorderLayout)1 Pattern (java.util.regex.Pattern)1 JFrame (javax.swing.JFrame)1 JTextField (javax.swing.JTextField)1 PCA (jmprojection.PCA)1 Sammons (jmprojection.Sammons)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