Search in sources :

Example 1 with PrincipalComponentAnalysis

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

the class SVDServiceHelperImpl method updatePca.

private PrincipalComponentAnalysis updatePca(ExpressionExperiment ee, ExpressionDataSVD svd, DoubleMatrix<Integer, BioMaterial> v, BioAssayDimension b) {
    principalComponentAnalysisService.removeForExperiment(ee);
    PrincipalComponentAnalysis pca = principalComponentAnalysisService.create(ee, svd.getU(), svd.getEigenvalues(), v, b, SVDServiceHelperImpl.MAX_NUM_COMPONENTS_TO_PERSIST, SVDServiceHelperImpl.MAX_LOADINGS_TO_PERSIST);
    // I wish this wasn't needed.
    ee = expressionExperimentService.thawLite(ee);
    auditTrailService.addUpdateEvent(ee, PCAAnalysisEvent.class, "SVD computation", null);
    return pca;
}
Also used : PrincipalComponentAnalysis(ubic.gemma.model.analysis.expression.pca.PrincipalComponentAnalysis)

Example 2 with PrincipalComponentAnalysis

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

the class SVDServiceHelperImpl method getTopLoadedVectors.

@Override
public Map<ProbeLoading, DoubleVectorValueObject> getTopLoadedVectors(ExpressionExperiment ee, int component, int count) {
    PrincipalComponentAnalysis pca = principalComponentAnalysisService.loadForExperiment(ee);
    Map<ProbeLoading, DoubleVectorValueObject> result = new HashMap<>();
    if (pca == null) {
        return result;
    }
    List<ProbeLoading> topLoadedProbes = principalComponentAnalysisService.getTopLoadedProbes(ee, component, count);
    if (topLoadedProbes == null) {
        SVDServiceHelperImpl.log.warn("No probes?");
        return result;
    }
    Map<Long, ProbeLoading> probes = new LinkedHashMap<>();
    Set<CompositeSequence> p = new HashSet<>();
    for (ProbeLoading probeLoading : topLoadedProbes) {
        CompositeSequence probe = probeLoading.getProbe();
        probes.put(probe.getId(), probeLoading);
        p.add(probe);
    }
    if (probes.isEmpty())
        return result;
    assert probes.size() <= count;
    Collection<ExpressionExperiment> ees = new HashSet<>();
    ees.add(ee);
    Collection<DoubleVectorValueObject> dvVos = processedExpressionDataVectorService.getProcessedDataArraysByProbe(ees, p);
    if (dvVos.isEmpty()) {
        SVDServiceHelperImpl.log.warn("No vectors came back from the call; check the Gene2CS table?");
        return result;
    }
    // note that this might have come from a cache.
    /*
         * This is actually expected, because we go through the genes.
         */
    BioAssayDimension bioAssayDimension = pca.getBioAssayDimension();
    assert bioAssayDimension != null;
    assert !bioAssayDimension.getBioAssays().isEmpty();
    for (DoubleVectorValueObject vct : dvVos) {
        ProbeLoading probeLoading = probes.get(vct.getDesignElement().getId());
        if (probeLoading == null) {
            /*
                 * This is okay, we will skip this probe. It was another probe for a gene that _was_ highly loaded.
                 */
            continue;
        }
        assert bioAssayDimension.getBioAssays().size() == vct.getData().length;
        vct.setRank(probeLoading.getLoadingRank().doubleValue());
        vct.setExpressionExperiment(new ExpressionExperimentValueObject(ee));
        result.put(probeLoading, vct);
    }
    if (result.isEmpty()) {
        SVDServiceHelperImpl.log.warn("No results, something went wrong; there were " + dvVos.size() + " vectors to start but they all got filtered out.");
    }
    return result;
}
Also used : ProbeLoading(ubic.gemma.model.analysis.expression.pca.ProbeLoading) CompositeSequence(ubic.gemma.model.expression.designElement.CompositeSequence) ExpressionExperiment(ubic.gemma.model.expression.experiment.ExpressionExperiment) BioAssayDimension(ubic.gemma.model.expression.bioAssayData.BioAssayDimension) PrincipalComponentAnalysis(ubic.gemma.model.analysis.expression.pca.PrincipalComponentAnalysis) ExpressionExperimentValueObject(ubic.gemma.model.expression.experiment.ExpressionExperimentValueObject) DoubleVectorValueObject(ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject)

Example 3 with PrincipalComponentAnalysis

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

the class SVDServiceHelperImpl method svd.

@Override
public SVDValueObject svd(ExpressionExperiment ee) {
    assert ee != null;
    Collection<ProcessedExpressionDataVector> vectors = processedExpressionDataVectorService.getProcessedDataVectors(ee);
    if (vectors.isEmpty()) {
        throw new IllegalArgumentException("Experiment must have processed data already to do SVD");
    }
    processedExpressionDataVectorService.thaw(vectors);
    ExpressionDataDoubleMatrix mat = new ExpressionDataDoubleMatrix(vectors);
    SVDServiceHelperImpl.log.info("Starting SVD");
    ExpressionDataSVD svd = new ExpressionDataSVD(mat);
    SVDServiceHelperImpl.log.info("SVD done, postprocessing and storing results.");
    /*
         * Save the results
         */
    DoubleMatrix<Integer, BioMaterial> v = svd.getV();
    BioAssayDimension b = mat.getBestBioAssayDimension();
    PrincipalComponentAnalysis pca = this.updatePca(ee, svd, v, b);
    return this.svdFactorAnalysis(pca);
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) BioAssayDimension(ubic.gemma.model.expression.bioAssayData.BioAssayDimension) PrincipalComponentAnalysis(ubic.gemma.model.analysis.expression.pca.PrincipalComponentAnalysis) ExpressionDataDoubleMatrix(ubic.gemma.core.datastructure.matrix.ExpressionDataDoubleMatrix) ProcessedExpressionDataVector(ubic.gemma.model.expression.bioAssayData.ProcessedExpressionDataVector)

Example 4 with PrincipalComponentAnalysis

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

the class ExpressionExperimentServiceImpl method remove.

@Override
@Transactional
public void remove(Long id) {
    final ExpressionExperiment ee = this.load(id);
    if (!securityService.isEditable(ee)) {
        throw new SecurityException("Error performing 'ExpressionExperimentService.remove(ExpressionExperiment expressionExperiment)' --> " + " You do not have permission to edit this experiment.");
    }
    // Remove subsets
    Collection<ExpressionExperimentSubSet> subsets = this.getSubSets(ee);
    for (ExpressionExperimentSubSet subset : subsets) {
        expressionExperimentSubSetService.remove(subset);
    }
    // Remove differential expression analyses
    Collection<DifferentialExpressionAnalysis> diffAnalyses = this.differentialExpressionAnalysisDao.findByInvestigation(ee);
    for (DifferentialExpressionAnalysis de : diffAnalyses) {
        this.differentialExpressionAnalysisDao.remove(de);
    }
    // remove any sample coexpression matrices
    this.sampleCoexpressionAnalysisDao.removeForExperiment(ee);
    // Remove PCA
    Collection<PrincipalComponentAnalysis> pcas = this.principalComponentAnalysisService.findByExperiment(ee);
    for (PrincipalComponentAnalysis pca : pcas) {
        this.principalComponentAnalysisService.remove(pca);
    }
    /*
         * Delete any expression experiment sets that only have this one ee in it. If possible remove this experiment
         * from other sets, and update them. IMPORTANT, this section assumes that we already checked for gene2gene
         * analyses!
         */
    Collection<ExpressionExperimentSet> sets = this.expressionExperimentSetService.find(ee);
    for (ExpressionExperimentSet eeSet : sets) {
        if (eeSet.getExperiments().size() == 1 && eeSet.getExperiments().iterator().next().equals(ee)) {
            AbstractService.log.info("Removing from set " + eeSet);
            this.expressionExperimentSetService.remove(// remove the set because in only contains this experiment
            eeSet);
        } else {
            AbstractService.log.info("Removing " + ee + " from " + eeSet);
            eeSet.getExperiments().remove(ee);
            // update set to not reference this experiment.
            this.expressionExperimentSetService.update(eeSet);
        }
    }
    this.expressionExperimentDao.remove(ee);
}
Also used : PrincipalComponentAnalysis(ubic.gemma.model.analysis.expression.pca.PrincipalComponentAnalysis) DifferentialExpressionAnalysis(ubic.gemma.model.analysis.expression.diff.DifferentialExpressionAnalysis) ExpressionExperimentSet(ubic.gemma.model.analysis.expression.ExpressionExperimentSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with PrincipalComponentAnalysis

use of ubic.gemma.model.analysis.expression.pca.PrincipalComponentAnalysis 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

PrincipalComponentAnalysis (ubic.gemma.model.analysis.expression.pca.PrincipalComponentAnalysis)5 Transactional (org.springframework.transaction.annotation.Transactional)2 ProbeLoading (ubic.gemma.model.analysis.expression.pca.ProbeLoading)2 BioAssayDimension (ubic.gemma.model.expression.bioAssayData.BioAssayDimension)2 CompositeSequence (ubic.gemma.model.expression.designElement.CompositeSequence)2 ArrayList (java.util.ArrayList)1 ByteArrayConverter (ubic.basecode.io.ByteArrayConverter)1 ExpressionDataDoubleMatrix (ubic.gemma.core.datastructure.matrix.ExpressionDataDoubleMatrix)1 ExpressionExperimentSet (ubic.gemma.model.analysis.expression.ExpressionExperimentSet)1 DifferentialExpressionAnalysis (ubic.gemma.model.analysis.expression.diff.DifferentialExpressionAnalysis)1 Eigenvalue (ubic.gemma.model.analysis.expression.pca.Eigenvalue)1 Eigenvector (ubic.gemma.model.analysis.expression.pca.Eigenvector)1 DoubleVectorValueObject (ubic.gemma.model.expression.bioAssayData.DoubleVectorValueObject)1 ProcessedExpressionDataVector (ubic.gemma.model.expression.bioAssayData.ProcessedExpressionDataVector)1 BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)1 ExpressionExperiment (ubic.gemma.model.expression.experiment.ExpressionExperiment)1 ExpressionExperimentValueObject (ubic.gemma.model.expression.experiment.ExpressionExperimentValueObject)1