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