use of ubic.gemma.model.common.description.Characteristic in project Gemma by PavlidisLab.
the class LinearModelAnalyzer method run.
@Override
public Collection<DifferentialExpressionAnalysis> run(ExpressionExperiment expressionExperiment, ExpressionDataDoubleMatrix dmatrix, DifferentialExpressionAnalysisConfig config) {
/*
* I apologize for this being so complicated. Basically there are four phases:
*
* 1. Get the data matrix and factors
*
* 2. Determine baseline groups; build model and contrasts
*
* 3. Run the analysis
*
* 4. Postprocess the analysis
*
* By far the most complex is #2 -- it depends on which factors and what kind they are.
*/
/*
* Initialize our matrix and factor lists...
*/
List<ExperimentalFactor> factors = config.getFactorsToInclude();
/*
* FIXME this is the place to strip put the outliers.
*/
List<BioMaterial> samplesUsed = ExperimentalDesignUtils.getOrderedSamples(dmatrix, factors);
// enforce ordering
dmatrix = new ExpressionDataDoubleMatrix(samplesUsed, dmatrix);
/*
* Do the analysis, by subsets if requested
*/
Collection<DifferentialExpressionAnalysis> results = new HashSet<>();
ExperimentalFactor subsetFactor = config.getSubsetFactor();
if (subsetFactor != null) {
if (factors.contains(subsetFactor)) {
throw new IllegalStateException("Subset factor cannot also be included in the analysis [ Factor was: " + subsetFactor + "]");
}
Map<FactorValue, ExpressionDataDoubleMatrix> subsets = this.makeSubSets(config, dmatrix, samplesUsed, subsetFactor);
LinearModelAnalyzer.log.info("Total number of subsets: " + subsets.size());
/*
* Now analyze each subset
*/
for (FactorValue subsetFactorValue : subsets.keySet()) {
LinearModelAnalyzer.log.info("Analyzing subset: " + subsetFactorValue);
/*
* Checking for DE_Exclude characteristics, which should not be included in the analysis.
* As requested in issue #4458 (bugzilla)
*/
boolean include = true;
for (Characteristic c : subsetFactorValue.getCharacteristics()) {
if (LinearModelAnalyzer.EXCLUDE_CHARACTERISTICS_VALUES.contains(c.getValue())) {
include = false;
break;
}
}
if (!include) {
LinearModelAnalyzer.log.warn(LinearModelAnalyzer.EXCLUDE_WARNING);
continue;
}
List<BioMaterial> bioMaterials = ExperimentalDesignUtils.getOrderedSamples(subsets.get(subsetFactorValue), factors);
/*
* make a EESubSet
*/
ExpressionExperimentSubSet eeSubSet = ExpressionExperimentSubSet.Factory.newInstance();
eeSubSet.setSourceExperiment(expressionExperiment);
eeSubSet.setName("Subset for " + subsetFactorValue);
Collection<BioAssay> bioAssays = new HashSet<>();
for (BioMaterial bm : bioMaterials) {
bioAssays.addAll(bm.getBioAssaysUsedIn());
}
eeSubSet.getBioAssays().addAll(bioAssays);
Collection<ExperimentalFactor> subsetFactors = this.fixFactorsForSubset(subsets.get(subsetFactorValue), eeSubSet, factors);
DifferentialExpressionAnalysisConfig subsetConfig = this.fixConfigForSubset(factors, config, subsetFactorValue);
if (subsetFactors.isEmpty()) {
LinearModelAnalyzer.log.warn("Experimental design is not valid for subset: " + subsetFactorValue + "; skipping");
continue;
}
/*
* Run analysis on the subset.
*/
DifferentialExpressionAnalysis analysis = this.doAnalysis(eeSubSet, subsetConfig, subsets.get(subsetFactorValue), bioMaterials, new ArrayList<>(subsetFactors), subsetFactorValue);
if (analysis == null) {
LinearModelAnalyzer.log.warn("No analysis results were obtained for subset: " + subsetFactorValue);
continue;
}
results.add(analysis);
}
} else {
/*
* Analyze the whole thing as one
*/
DifferentialExpressionAnalysis analysis = this.doAnalysis(expressionExperiment, config, dmatrix, samplesUsed, factors, null);
if (analysis == null) {
LinearModelAnalyzer.log.warn("No analysis results were obtained");
} else {
results.add(analysis);
}
}
return results;
}
use of ubic.gemma.model.common.description.Characteristic in project Gemma by PavlidisLab.
the class BaseAnalyzerConfigurationTest method setup.
@Before
public void setup() throws Exception {
try {
if (Settings.getBoolean("gemma.linearmodels.useR")) {
rc = RConnectionFactory.getRConnection(Settings.getString("gemma.rserve.hostname", "localhost"));
if (rc != null && rc.isConnected()) {
connected = true;
/*
* We have to disconnect right away for test to work under Windows, where only one connection is
* allowed at a time. The classes under test will get their own connections.
*/
if (rc != null && rc.isConnected() && rc instanceof RServeClient)
((RServeClient) rc).disconnect();
}
} else {
// not using R
connected = true;
}
} catch (Exception e) {
log.warn(e.getMessage());
}
/* array designs */
arrayDesign = ArrayDesign.Factory.newInstance();
arrayDesign.setTechnologyType(TechnologyType.ONECOLOR);
arrayDesign.setId(1L);
arrayDesign.setName("MG-U74Test_" + RandomStringUtils.randomAlphanumeric(12));
arrayDesign.setPrimaryTaxon(this.getTaxon("mouse"));
expressionExperiment = ExpressionExperiment.Factory.newInstance();
expressionExperiment.setName("analysistest_" + RandomStringUtils.randomAlphanumeric(12));
expressionExperiment.setId(100009L);
expressionExperiment.setShortName(RandomStringUtils.randomAlphanumeric(12));
/* experimental factor "area" */
experimentalFactorA_Area = ExperimentalFactor.Factory.newInstance();
experimentalFactorA_Area.setName("area");
experimentalFactorA_Area.setType(FactorType.CATEGORICAL);
experimentalFactorA_Area.setId(5001L);
Collection<FactorValue> factorValuesA = new HashSet<>();
factorValueA1 = FactorValue.Factory.newInstance();
factorValueA1.setId(1001L);
factorValueA1.setValue("cerebellum");
Characteristic characteristicA1 = Characteristic.Factory.newInstance();
characteristicA1.setValue(factorValueA1.getValue());
Collection<Characteristic> characteristicsA1 = new HashSet<>();
characteristicsA1.add(characteristicA1);
factorValueA1.setCharacteristics(characteristicsA1);
factorValueA1.setExperimentalFactor(experimentalFactorA_Area);
factorValueA2 = FactorValue.Factory.newInstance();
factorValueA2.setIsBaseline(true);
factorValueA2.setValue("amygdala");
factorValueA2.setId(1002L);
Characteristic characteristicA2 = Characteristic.Factory.newInstance();
characteristicA2.setValue(factorValueA2.getValue());
Collection<Characteristic> characteristicsA2 = new HashSet<>();
characteristicsA2.add(characteristicA2);
factorValueA2.setCharacteristics(characteristicsA2);
factorValueA2.setExperimentalFactor(experimentalFactorA_Area);
factorValuesA.add(factorValueA1);
factorValuesA.add(factorValueA2);
experimentalFactorA_Area.getFactorValues().addAll(factorValuesA);
/* experimental factor "treat" */
experimentalFactorB = ExperimentalFactor.Factory.newInstance();
experimentalFactorB.setName("treat");
experimentalFactorB.setId(5002L);
experimentalFactorB.setType(FactorType.CATEGORICAL);
Collection<FactorValue> factorValuesB = new HashSet<>();
FactorValue factorValueB1 = FactorValue.Factory.newInstance();
factorValueB1.setValue("pcp");
factorValueB1.setId(1003L);
Characteristic characteristicB1 = Characteristic.Factory.newInstance();
characteristicB1.setValue(factorValueB1.getValue());
Collection<Characteristic> characteristicsB1 = new HashSet<>();
characteristicsB1.add(characteristicB1);
factorValueB1.setCharacteristics(characteristicsB1);
factorValueB1.setExperimentalFactor(experimentalFactorB);
factorValueB2 = FactorValue.Factory.newInstance();
factorValueB2.setValue("control_group");
factorValueB2.setId(1004L);
Characteristic characteristicB2 = Characteristic.Factory.newInstance();
characteristicB2.setValue(factorValueB2.getValue());
Collection<Characteristic> characteristicsB2 = new HashSet<>();
characteristicsB2.add(characteristicB2);
factorValueB2.setCharacteristics(characteristicsB2);
factorValueB2.setExperimentalFactor(experimentalFactorB);
factorValuesB.add(factorValueB1);
factorValuesB.add(factorValueB2);
experimentalFactorB.getFactorValues().addAll(factorValuesB);
/* set up the biomaterials */
biomaterials = new ArrayList<>();
// 2 replicates
BioMaterial biomaterial0a = BioMaterial.Factory.newInstance();
biomaterial0a.setName("0a");
Collection<FactorValue> factorValuesForBioMaterial0 = new HashSet<>();
factorValuesForBioMaterial0.add(factorValueA1);
factorValuesForBioMaterial0.add(factorValueB1);
biomaterial0a.getFactorValues().addAll(factorValuesForBioMaterial0);
BioMaterial biomaterial0b = BioMaterial.Factory.newInstance();
biomaterial0b.setName("0b");
biomaterial0b.getFactorValues().addAll(factorValuesForBioMaterial0);
// 2 replicates
BioMaterial biomaterial1a = BioMaterial.Factory.newInstance();
biomaterial1a.setName("1a");
Collection<FactorValue> factorValuesForBioMaterial1 = new HashSet<>();
factorValuesForBioMaterial1.add(factorValueA1);
factorValuesForBioMaterial1.add(factorValueB2);
biomaterial1a.getFactorValues().addAll(factorValuesForBioMaterial1);
BioMaterial biomaterial1b = BioMaterial.Factory.newInstance();
biomaterial1b.setName("1b");
biomaterial1b.getFactorValues().addAll(factorValuesForBioMaterial1);
// 2 replicates
BioMaterial biomaterial2a = BioMaterial.Factory.newInstance();
biomaterial2a.setName("2a");
Collection<FactorValue> factorValuesForBioMaterial2 = new HashSet<>();
factorValuesForBioMaterial2.add(factorValueA2);
factorValuesForBioMaterial2.add(factorValueB1);
biomaterial2a.getFactorValues().addAll(factorValuesForBioMaterial2);
BioMaterial biomaterial2b = BioMaterial.Factory.newInstance();
biomaterial2b.setName("2b");
biomaterial2b.getFactorValues().addAll(factorValuesForBioMaterial2);
// 2 replicates
BioMaterial biomaterial3a = BioMaterial.Factory.newInstance();
biomaterial3a.setName("3a");
Collection<FactorValue> factorValuesForBioMaterial3 = new HashSet<>();
factorValuesForBioMaterial3.add(factorValueA2);
factorValuesForBioMaterial3.add(factorValueB2);
biomaterial3a.getFactorValues().addAll(factorValuesForBioMaterial3);
BioMaterial biomaterial3b = BioMaterial.Factory.newInstance();
biomaterial3b.setName("3b");
biomaterial3b.getFactorValues().addAll(factorValuesForBioMaterial3);
biomaterial0a.setId(100000L);
biomaterial0b.setId(100001L);
biomaterial1a.setId(100002L);
biomaterial1b.setId(100003L);
biomaterial2a.setId(100004L);
biomaterial2b.setId(100005L);
biomaterial3a.setId(100006L);
biomaterial3b.setId(100007L);
biomaterials.add(biomaterial0a);
biomaterials.add(biomaterial0b);
biomaterials.add(biomaterial1a);
biomaterials.add(biomaterial1b);
biomaterials.add(biomaterial2a);
biomaterials.add(biomaterial2b);
biomaterials.add(biomaterial3a);
biomaterials.add(biomaterial3b);
/* set up the bioassays */
bioAssay0a = BioAssay.Factory.newInstance();
bioAssay0a.setName("bioassay 0a");
bioAssay0a.setSampleUsed(biomaterial0a);
bioAssay0a.setArrayDesignUsed(arrayDesign);
bioAssay0b = BioAssay.Factory.newInstance();
bioAssay0b.setName("bioassay 0b");
bioAssay0b.setSampleUsed(biomaterial0b);
bioAssay0b.setArrayDesignUsed(arrayDesign);
bioAssay1a = BioAssay.Factory.newInstance();
bioAssay1a.setName("bioassay 1a");
bioAssay1a.setSampleUsed(biomaterial1a);
bioAssay1a.setArrayDesignUsed(arrayDesign);
bioAssay1b = BioAssay.Factory.newInstance();
bioAssay1b.setName("bioassay 1b");
bioAssay1b.setSampleUsed(biomaterial1b);
bioAssay1b.setArrayDesignUsed(arrayDesign);
bioAssay2a = BioAssay.Factory.newInstance();
bioAssay2a.setName("bioassay 2a");
bioAssay2a.setSampleUsed(biomaterial2a);
bioAssay2a.setArrayDesignUsed(arrayDesign);
bioAssay2b = BioAssay.Factory.newInstance();
bioAssay2b.setName("bioassay 2b");
bioAssay2b.setSampleUsed(biomaterial2b);
bioAssay2b.setArrayDesignUsed(arrayDesign);
bioAssay3a = BioAssay.Factory.newInstance();
bioAssay3a.setName("bioassay 3a");
bioAssay3a.setSampleUsed(biomaterial3a);
bioAssay3a.setArrayDesignUsed(arrayDesign);
bioAssay3b = BioAssay.Factory.newInstance();
bioAssay3b.setName("bioassay 3b");
bioAssay3b.setSampleUsed(biomaterial3b);
bioAssay3b.setArrayDesignUsed(arrayDesign);
bioAssays = new ArrayList<>();
bioAssays.add(bioAssay0a);
bioAssays.add(bioAssay0b);
bioAssays.add(bioAssay1a);
bioAssays.add(bioAssay1b);
bioAssays.add(bioAssay2a);
bioAssays.add(bioAssay2b);
bioAssays.add(bioAssay3a);
bioAssays.add(bioAssay3b);
biomaterial0a.getBioAssaysUsedIn().add(bioAssay0a);
biomaterial0b.getBioAssaysUsedIn().add(bioAssay0b);
biomaterial1a.getBioAssaysUsedIn().add(bioAssay1a);
biomaterial1b.getBioAssaysUsedIn().add(bioAssay1b);
biomaterial2a.getBioAssaysUsedIn().add(bioAssay2a);
biomaterial2b.getBioAssaysUsedIn().add(bioAssay2b);
biomaterial3a.getBioAssaysUsedIn().add(bioAssay3a);
biomaterial3b.getBioAssaysUsedIn().add(bioAssay3b);
expressionExperiment.setBioAssays(bioAssays);
experimentalFactors = new ArrayList<>();
experimentalFactors.add(experimentalFactorA_Area);
experimentalFactors.add(experimentalFactorB);
experimentalDesign = ExperimentalDesign.Factory.newInstance();
experimentalDesign.setName("experimental design");
experimentalDesign.setExperimentalFactors(experimentalFactors);
expressionExperiment.setExperimentalDesign(experimentalDesign);
experimentalFactorA_Area.setExperimentalDesign(experimentalDesign);
experimentalFactorB.setExperimentalDesign(experimentalDesign);
quantitationType = QuantitationType.Factory.newInstance();
quantitationType.setName("quantitation type");
quantitationType.setRepresentation(PrimitiveType.DOUBLE);
quantitationType.setType(StandardQuantitationType.AMOUNT);
quantitationType.setIsPreferred(true);
quantitationType.setIsMaskedPreferred(false);
quantitationType.setIsBackground(false);
quantitationType.setScale(ScaleType.LOG2);
quantitationType.setIsNormalized(false);
quantitationType.setIsBackgroundSubtracted(false);
quantitationType.setIsRatio(false);
expressionExperiment.getQuantitationTypes().add(quantitationType);
bioAssayDimension = BioAssayDimension.Factory.newInstance();
bioAssayDimension.setName("test bioassay dimension");
bioAssayDimension.setBioAssays(bioAssays);
this.configureVectors(biomaterials, null);
}
use of ubic.gemma.model.common.description.Characteristic in project Gemma by PavlidisLab.
the class ExpressionDataFileServiceImpl method getFactorValueString.
private String getFactorValueString(FactorValue fv) {
String result;
if (fv == null)
return "null";
if (fv.getCharacteristics() != null && fv.getCharacteristics().size() > 0) {
StringBuilder fvString = new StringBuilder();
for (Characteristic c : fv.getCharacteristics()) {
fvString.append(c.getValue()).append("_");
}
result = StringUtils.removeEnd(fvString.toString(), "_");
} else if (fv.getMeasurement() != null) {
result = fv.getMeasurement().getValue();
} else if (fv.getValue() != null && !fv.getValue().isEmpty()) {
result = fv.getValue();
} else
return "no_data";
// R-friendly, but no need to add "X" to the beginning since this is a suffix.
return result.replaceAll("[\\W]+", ".");
}
use of ubic.gemma.model.common.description.Characteristic in project Gemma by PavlidisLab.
the class ExpressionDataWriterUtils method constructFactorValueName.
/**
* Replaces spaces and hyphens with underscores.
*
* @param factorValue FV
* @return replaced string
*/
public static String constructFactorValueName(FactorValue factorValue) {
StringBuilder buf = new StringBuilder();
if (factorValue.getCharacteristics().size() > 0) {
for (Characteristic c : factorValue.getCharacteristics()) {
buf.append(StringUtils.strip(c.getValue()));
if (factorValue.getCharacteristics().size() > 1)
buf.append(" | ");
}
} else if (factorValue.getMeasurement() != null) {
buf.append(factorValue.getMeasurement().getValue());
} else if (StringUtils.isNotBlank(factorValue.getValue())) {
buf.append(StringUtils.strip(factorValue.getValue()));
}
String matchedFactorValue = buf.toString();
matchedFactorValue = matchedFactorValue.trim();
matchedFactorValue = matchedFactorValue.replaceAll("-", "_");
matchedFactorValue = matchedFactorValue.replaceAll("\\s", "_");
return matchedFactorValue;
}
use of ubic.gemma.model.common.description.Characteristic in project Gemma by PavlidisLab.
the class GeneralSearchControllerImpl method fillValueObjects.
@SuppressWarnings("unchecked")
private void fillValueObjects(Class<?> entityClass, List<SearchResult> results, SearchSettings settings) {
StopWatch timer = new StopWatch();
timer.start();
Collection<?> vos;
if (ExpressionExperiment.class.isAssignableFrom(entityClass)) {
vos = this.filterEE(expressionExperimentService.loadValueObjects(EntityUtils.getIds(results), false), settings);
if (!SecurityUtil.isUserAdmin()) {
auditableUtil.removeTroubledEes((Collection<ExpressionExperimentValueObject>) vos);
}
} else if (ArrayDesign.class.isAssignableFrom(entityClass)) {
vos = this.filterAD(arrayDesignService.loadValueObjectsByIds(EntityUtils.getIds(results)), settings);
if (!SecurityUtil.isUserAdmin()) {
auditableUtil.removeTroubledArrayDesigns((Collection<ArrayDesignValueObject>) vos);
}
} else if (CompositeSequence.class.isAssignableFrom(entityClass)) {
Collection<CompositeSequenceValueObject> css = new ArrayList<>();
for (SearchResult sr : results) {
CompositeSequenceValueObject csvo = compositeSequenceService.loadValueObject((CompositeSequence) sr.getResultObject());
css.add(csvo);
}
vos = css;
} else if (BibliographicReference.class.isAssignableFrom(entityClass)) {
Collection<BibliographicReference> bss = bibliographicReferenceService.load(EntityUtils.getIds(results));
bss = bibliographicReferenceService.thaw(bss);
vos = bibliographicReferenceService.loadValueObjects(bss);
} else if (Gene.class.isAssignableFrom(entityClass)) {
Collection<Gene> genes = geneService.load(EntityUtils.getIds(results));
genes = geneService.thawLite(genes);
vos = geneService.loadValueObjects(genes);
} else if (Characteristic.class.isAssignableFrom(entityClass)) {
Collection<CharacteristicValueObject> cvos = new ArrayList<>();
for (SearchResult sr : results) {
Characteristic ch = (Characteristic) sr.getResultObject();
cvos.add(new CharacteristicValueObject(ch));
}
vos = cvos;
} else if (CharacteristicValueObject.class.isAssignableFrom(entityClass)) {
Collection<CharacteristicValueObject> cvos = new ArrayList<>();
for (SearchResult sr : results) {
CharacteristicValueObject ch = (CharacteristicValueObject) sr.getResultObject();
cvos.add(ch);
}
vos = cvos;
} else if (BioSequenceValueObject.class.isAssignableFrom(entityClass)) {
return;
} else if (GeneSet.class.isAssignableFrom(entityClass)) {
vos = geneSetService.getValueObjects(EntityUtils.getIds(results));
} else if (ExpressionExperimentSet.class.isAssignableFrom(entityClass)) {
vos = experimentSetService.loadValueObjects(experimentSetService.load(EntityUtils.getIds(results)));
} else if (FactorValue.class.isAssignableFrom(entityClass)) {
Collection<FactorValueValueObject> fvo = new ArrayList<>();
for (SearchResult sr : results) {
fvo.add(new FactorValueValueObject((FactorValue) sr.getResultObject()));
}
vos = fvo;
} else {
throw new UnsupportedOperationException("Don't know how to make value objects for class=" + entityClass);
}
if (vos == null || vos.isEmpty()) {
// it causing front end errors, if vos is empty make sure to get rid of all search results
for (Iterator<SearchResult> it = results.iterator(); it.hasNext(); ) {
it.next();
it.remove();
}
return;
}
// retained objects...
Map<Long, Object> idMap = EntityUtils.getIdMap(vos);
for (Iterator<SearchResult> it = results.iterator(); it.hasNext(); ) {
SearchResult sr = it.next();
if (!idMap.containsKey(sr.getId())) {
it.remove();
continue;
}
sr.setResultObject(idMap.get(sr.getId()));
}
if (timer.getTime() > 1000) {
BaseFormController.log.info("Value object conversion after search: " + timer.getTime() + "ms");
}
}
Aggregations