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;
}
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;
}
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);
}
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);
}
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);
}
Aggregations