use of ubic.gemma.model.analysis.expression.pca.ProbeLoading 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.ProbeLoading in project Gemma by PavlidisLab.
the class DEDVController method handleRequestInternal.
/**
* Handle case of text export of the results.
*/
@RequestMapping("/downloadDEDV.html")
protected ModelAndView handleRequestInternal(HttpServletRequest request) {
StopWatch watch = new StopWatch();
watch.start();
// might not be any
Collection<Long> geneIds = ControllerUtils.extractIds(request.getParameter("g"));
// might not be there
Collection<Long> eeIds = ControllerUtils.extractIds(request.getParameter("ee"));
ModelAndView mav = new ModelAndView(new TextView());
if (eeIds == null || eeIds.isEmpty()) {
mav.addObject("text", "Input empty for finding DEDVs: " + geneIds + " and " + eeIds);
return mav;
}
String threshSt = request.getParameter("thresh");
String resultSetIdSt = request.getParameter("rs");
Double thresh = 100.0;
if (StringUtils.isNotBlank(threshSt)) {
try {
thresh = Double.parseDouble(threshSt);
} catch (NumberFormatException e) {
throw new RuntimeException("Threshold was not a valid value: " + threshSt);
}
}
Map<ExpressionExperimentValueObject, Map<Long, Collection<DoubleVectorValueObject>>> result;
if (request.getParameter("pca") != null) {
int component = Integer.parseInt(request.getParameter("component"));
Long eeId = eeIds.iterator().next();
Map<ProbeLoading, DoubleVectorValueObject> topLoadedVectors = this.svdService.getTopLoadedVectors(eeId, component, thresh.intValue());
if (topLoadedVectors == null)
return null;
mav.addObject("text", format4File(topLoadedVectors.values()));
return mav;
}
/*
* The following should be set if we're viewing diff. ex results.
*/
Long resultSetId = null;
if (StringUtils.isNumeric(resultSetIdSt)) {
resultSetId = Long.parseLong(resultSetIdSt);
}
if (resultSetId != null) {
/*
* Diff ex case.
*/
Long eeId = eeIds.iterator().next();
Collection<DoubleVectorValueObject> diffExVectors = processedExpressionDataVectorService.getDiffExVectors(resultSetId, thresh, MAX_RESULTS_TO_RETURN);
if (diffExVectors == null || diffExVectors.isEmpty()) {
mav.addObject("text", "No results");
return mav;
}
/*
* Organize the vectors in the same way expected by the ee+gene type of request.
*/
ExpressionExperimentValueObject ee = expressionExperimentService.loadValueObject(expressionExperimentService.load(eeId));
result = new HashMap<>();
Map<Long, Collection<DoubleVectorValueObject>> gmap = new HashMap<>();
for (DoubleVectorValueObject dv : diffExVectors) {
for (Long g : dv.getGenes()) {
if (!gmap.containsKey(g)) {
gmap.put(g, new HashSet<DoubleVectorValueObject>());
}
gmap.get(g).add(dv);
}
}
result.put(ee, gmap);
} else {
// Generic listing.
result = getDEDV(eeIds, geneIds);
}
if (result == null || result.isEmpty()) {
mav.addObject("text", "No results");
return mav;
}
mav.addObject("text", format4File(result));
watch.stop();
Long time = watch.getTime();
if (time > 100) {
log.info("Retrieved and Formated" + result.keySet().size() + " DEDVs for eeIDs: " + eeIds + " and GeneIds: " + geneIds + " in : " + time + " ms.");
}
return mav;
}
use of ubic.gemma.model.analysis.expression.pca.ProbeLoading 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);
}
use of ubic.gemma.model.analysis.expression.pca.ProbeLoading in project Gemma by PavlidisLab.
the class DEDVController method getDEDVForPcaVisualization.
/**
* AJAX
*/
public VisualizationValueObject[] getDEDVForPcaVisualization(Long eeId, int component, int count) {
StopWatch watch = new StopWatch();
watch.start();
Map<ProbeLoading, DoubleVectorValueObject> topLoadedVectors = this.svdService.getTopLoadedVectors(eeId, component, count);
if (topLoadedVectors == null)
return null;
Map<Long, LinkedHashMap<BioAssayValueObject, LinkedHashMap<ExperimentalFactor, Double>>> layouts;
Collection<DoubleVectorValueObject> values = topLoadedVectors.values();
if (values.isEmpty()) {
return null;
}
layouts = experimentalDesignVisualizationService.sortVectorDataByDesign(values);
return makeVisCollection(values, null, null, layouts);
}
Aggregations