Search in sources :

Example 1 with Eigenvector

use of ubic.gemma.model.analysis.expression.pca.Eigenvector in project Gemma by PavlidisLab.

the class PrincipalComponentAnalysisServiceImpl method create.

@Override
@Transactional
public PrincipalComponentAnalysis create(ExpressionExperiment ee, DoubleMatrix<CompositeSequence, Integer> u, double[] eigenvalues, DoubleMatrix<Integer, BioMaterial> v, BioAssayDimension bad, int numComponentsToStore, int numLoadingsToStore) {
    PrincipalComponentAnalysis pca = PrincipalComponentAnalysis.Factory.newInstance();
    int actualNumberOfComponentsStored = Math.min(numComponentsToStore, v.columns());
    pca.setNumComponentsStored(actualNumberOfComponentsStored);
    pca.setBioAssayDimension(bad);
    pca.setMaxNumProbesPerComponent(numLoadingsToStore);
    pca.setExperimentAnalyzed(ee);
    /*
         * deal with U. We keep only the first numComponentsToStore components for the first numLoadingsToStore genes.
         */
    for (int i = 0; i < actualNumberOfComponentsStored; i++) {
        List<CompositeSequence> inOrder = u.sortByColumnAbsoluteValues(i, true);
        for (int j = 0; j < Math.min(u.rows(), numLoadingsToStore) - 1; j++) {
            CompositeSequence probe = inOrder.get(j);
            ProbeLoading plr = ProbeLoading.Factory.newInstance(i + 1, u.getRowByName(probe)[i], j, probe);
            pca.getProbeLoadings().add(plr);
        }
    }
    /*
         * deal with V. note we store all of it.
         */
    ByteArrayConverter bac = new ByteArrayConverter();
    for (int i = 0; i < v.columns(); i++) {
        double[] column = v.getColumn(i);
        byte[] eigenVectorBytes = bac.doubleArrayToBytes(column);
        int componentNumber = i + 1;
        log.debug(componentNumber);
        Eigenvector evec = Eigenvector.Factory.newInstance(componentNumber, eigenVectorBytes);
        pca.getEigenVectors().add(evec);
    }
    /*
         * Deal with eigenvalues; note we store all of them.
         */
    double sum = 0.0;
    List<Eigenvalue> eigv = new ArrayList<>();
    for (int i = 0; i < eigenvalues.length; i++) {
        double d = eigenvalues[i];
        sum += d;
        Eigenvalue ev = Eigenvalue.Factory.newInstance();
        ev.setComponentNumber(i + 1);
        ev.setValue(d);
        eigv.add(ev);
    }
    for (int i = 0; i < eigenvalues.length; i++) {
        Eigenvalue eigenvalue = eigv.get(i);
        eigenvalue.setVarianceFraction(eigenvalue.getValue() / sum);
        pca.getEigenValues().add(eigenvalue);
    }
    return this.principalComponentAnalysisDao.create(pca);
}
Also used : ByteArrayConverter(ubic.basecode.io.ByteArrayConverter) Eigenvector(ubic.gemma.model.analysis.expression.pca.Eigenvector) Eigenvalue(ubic.gemma.model.analysis.expression.pca.Eigenvalue) ArrayList(java.util.ArrayList) ProbeLoading(ubic.gemma.model.analysis.expression.pca.ProbeLoading) CompositeSequence(ubic.gemma.model.expression.designElement.CompositeSequence) PrincipalComponentAnalysis(ubic.gemma.model.analysis.expression.pca.PrincipalComponentAnalysis) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ArrayList (java.util.ArrayList)1 Transactional (org.springframework.transaction.annotation.Transactional)1 ByteArrayConverter (ubic.basecode.io.ByteArrayConverter)1 Eigenvalue (ubic.gemma.model.analysis.expression.pca.Eigenvalue)1 Eigenvector (ubic.gemma.model.analysis.expression.pca.Eigenvector)1 PrincipalComponentAnalysis (ubic.gemma.model.analysis.expression.pca.PrincipalComponentAnalysis)1 ProbeLoading (ubic.gemma.model.analysis.expression.pca.ProbeLoading)1 CompositeSequence (ubic.gemma.model.expression.designElement.CompositeSequence)1