use of ubic.gemma.model.expression.experiment.FactorValue in project Gemma by PavlidisLab.
the class DifferentialExpressionAnalysisUtil method checkValidForLm.
/**
* Check that the factorValues are measurements, or that there are at least two assays for at least one factor
* value. Otherwise the model fit will be perfect and pvalues will not be returned.
*
* @param experimentalFactor exp. factor
* @param expressionExperiment the experiment
* @return true if it's okay, false otherwise.
*/
public static boolean checkValidForLm(BioAssaySet expressionExperiment, ExperimentalFactor experimentalFactor) {
if (experimentalFactor.getFactorValues().size() < 2) {
return false;
}
if (experimentalFactor.getType().equals(FactorType.CONTINUOUS)) {
return true;
}
/*
* Check to make sure more than one factor value is actually used.
*/
boolean replicatesok = false;
Map<FactorValue, Integer> counts = new HashMap<>();
for (BioAssay ba : expressionExperiment.getBioAssays()) {
BioMaterial bm = ba.getSampleUsed();
for (FactorValue fv : bm.getFactorValues()) {
if (fv.getExperimentalFactor().equals(experimentalFactor)) {
if (!counts.containsKey(fv)) {
counts.put(fv, 0);
}
counts.put(fv, counts.get(fv) + 1);
if (counts.get(fv) > 1) {
replicatesok = true;
}
}
}
}
if (counts.size() < 2) {
DifferentialExpressionAnalysisUtil.log.warn(experimentalFactor + " has only " + counts.size() + " levels used in the current set, it cannot be analyzed");
return false;
}
return replicatesok;
}
use of ubic.gemma.model.expression.experiment.FactorValue in project Gemma by PavlidisLab.
the class DifferentialExpressionAnalysisUtil method generateFactorValuePairings.
/**
* Generates all possible factor value pairings for the given experimental factors.
*
* @param experimentalFactors exp. factors
* @return A collection of hashSets, where each hashSet is a pairing.
*/
private static Collection<Set<FactorValue>> generateFactorValuePairings(Collection<ExperimentalFactor> experimentalFactors) {
/* set up the possible pairings */
Collection<FactorValue> allFactorValues = new HashSet<>();
for (ExperimentalFactor experimentalFactor : experimentalFactors) {
allFactorValues.addAll(experimentalFactor.getFactorValues());
}
Collection<Set<FactorValue>> factorValuePairings = new HashSet<>();
for (FactorValue factorValue : allFactorValues) {
for (FactorValue f : allFactorValues) {
if (f.getExperimentalFactor().equals(factorValue.getExperimentalFactor()))
continue;
HashSet<FactorValue> factorValuePairing = new HashSet<>();
factorValuePairing.add(factorValue);
factorValuePairing.add(f);
if (!factorValuePairings.contains(factorValuePairing)) {
factorValuePairings.add(factorValuePairing);
}
}
}
return factorValuePairings;
}
use of ubic.gemma.model.expression.experiment.FactorValue in project Gemma by PavlidisLab.
the class DifferentialExpressionAnalysisUtil method getRelevantFactorValues.
/**
* Isolate a biomaterial's factor values for a specific factor(s).
*
* @return the factor values the biomaterial has for the given factors.
*/
private static Collection<FactorValue> getRelevantFactorValues(Collection<ExperimentalFactor> factors, BioMaterial biomaterial) {
Collection<FactorValue> factorValues = biomaterial.getFactorValues();
Collection<FactorValue> factorValuesToCheck = new HashSet<>();
for (FactorValue factorValue : factorValues) {
if (factors.contains(factorValue.getExperimentalFactor())) {
factorValuesToCheck.add(factorValue);
}
}
return factorValuesToCheck;
}
use of ubic.gemma.model.expression.experiment.FactorValue in project Gemma by PavlidisLab.
the class DifferentialExpressionAnalysisUtil method checkBlockDesign.
/**
* Returns true if all of the following conditions hold true: each biomaterial has more than 2 factor values, each
* biomaterial has a factor value from one of the input factors paired with a factor value from the other input
* factors, and all factor values from 1 factor have been paired with all factor values from the other factors,
* across all biomaterials.
*
* @param biomaterials biomaterials
* @param experimentalFactors exp. factors
* @return false if not a complete block design.
*/
private static boolean checkBlockDesign(Collection<BioMaterial> biomaterials, Collection<ExperimentalFactor> experimentalFactors) {
Collection<Set<FactorValue>> factorValuePairings = DifferentialExpressionAnalysisUtil.generateFactorValuePairings(experimentalFactors);
/* check to see if the biomaterial's factor value pairing is one of the possible combinations */
Map<Collection<FactorValue>, BioMaterial> seenPairings = new HashMap<>();
for (BioMaterial m : biomaterials) {
Collection<FactorValue> factorValuesFromBioMaterial = m.getFactorValues();
if (factorValuesFromBioMaterial.size() < experimentalFactors.size()) {
DifferentialExpressionAnalysisUtil.log.warn("Biomaterial must have at least " + experimentalFactors.size() + "factor value. Incomplete block design. " + m);
return false;
}
/*
* Find a combination of factors used in the model that this biomaterial has.
*/
boolean ok = false;
for (Set<FactorValue> pairing : factorValuePairings) {
if (factorValuesFromBioMaterial.containsAll(pairing)) {
ok = true;
break;
}
}
if (!ok) {
/*
* This amounts to a missing value.
*/
throw new IllegalArgumentException("Biomaterial does not have a combination of factors matching the model; design error?: " + m);
// continue;
}
seenPairings.put(factorValuesFromBioMaterial, m);
}
if (seenPairings.size() != factorValuePairings.size()) {
DifferentialExpressionAnalysisUtil.log.warn("Biomaterial not paired with all factor values for each of " + experimentalFactors.size() + " experimental factors. Found " + seenPairings.size() + " pairings but should have " + factorValuePairings.size() + ". Incomplete block design.");
return false;
}
return true;
}
use of ubic.gemma.model.expression.experiment.FactorValue in project Gemma by PavlidisLab.
the class ExpressionDataMatrixColumnSort method orderBiomaterialsBySortedFactors.
/**
* Sort biomaterials according to a list of ordered factors
*
* @param start biomaterials to sort
* @param factors sorted list of factors to define sort order for biomaterials, cannot be null
*/
private static List<BioMaterial> orderBiomaterialsBySortedFactors(List<BioMaterial> start, List<ExperimentalFactor> factors) {
if (start.size() == 1) {
return start;
}
if (start.size() == 0) {
throw new IllegalArgumentException("Must provide some biomaterials");
}
if (factors == null) {
throw new IllegalArgumentException("Must provide sorted factors, or at least an empty list");
}
if (factors.isEmpty()) {
// we're done.
return start;
}
ExperimentalFactor simplest = factors.get(0);
if (simplest == null) {
// we're done.
return start;
}
/*
* Order this chunk by the selected factor
*/
Map<FactorValue, List<BioMaterial>> fv2bms = ExpressionDataMatrixColumnSort.buildFv2BmMap(start);
List<BioMaterial> ordered = ExpressionDataMatrixColumnSort.orderByFactor(simplest, fv2bms, start);
// Abort ordering, so we are ordered only by the first continuous factor.
if (ExperimentalDesignUtils.isContinuous(simplest)) {
assert ordered != null;
return ordered;
}
LinkedList<ExperimentalFactor> factorsStillToDo = new LinkedList<>();
factorsStillToDo.addAll(factors);
factorsStillToDo.remove(simplest);
if (factorsStillToDo.size() == 0) {
/*
* No more ordering is necessary.
*/
return ordered;
}
ExpressionDataMatrixColumnSort.log.debug("Factors: " + factors.size());
/*
* Recurse in and order each chunk. First split it up, but retaining the order we just made.
*/
LinkedHashMap<FactorValue, List<BioMaterial>> chunks = ExpressionDataMatrixColumnSort.chunkOnFactor(simplest, ordered);
if (chunks == null) {
// this means we should bail, gracefully.
return start;
}
/*
* Process each chunk.
*/
List<BioMaterial> result = new ArrayList<>();
for (FactorValue fv : chunks.keySet()) {
List<BioMaterial> chunk = chunks.get(fv);
if (chunk.size() < 2) {
result.addAll(chunk);
} else {
List<BioMaterial> orderedChunk = ExpressionDataMatrixColumnSort.orderBiomaterialsBySortedFactors(chunk, factorsStillToDo);
result.addAll(orderedChunk);
}
}
return result;
}
Aggregations