Skip to content

Commit 9445fdb

Browse files
author
mpschr
committed
Zscore transformation
1 parent 744b245 commit 9445fdb

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

org.gitools.matrix/src/main/java/org/gitools/matrix/transform/TransformFunctionFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public static List<ConfigurableTransformFunction> get(IMatrixLayer<Double> resul
4040
funcs.add(new ReplaceValueFunction());
4141
funcs.add(new ScaleFunction(resultLayer));
4242
funcs.add(new SumConstantFunction());
43+
funcs.add(new ZScoreFunction());
4344
return funcs;
4445
}
4546

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* #%L
3+
* org.gitools.matrix
4+
* %%
5+
* Copyright (C) 2013 - 2016 Universitat Pompeu Fabra - Biomedical Genomics group
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as
9+
* published by the Free Software Foundation, either version 3 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/gpl-3.0.html>.
20+
* #L%
21+
*/
22+
package org.gitools.matrix.transform;
23+
24+
import org.gitools.api.ApplicationContext;
25+
import org.gitools.api.analysis.IAggregator;
26+
import org.gitools.api.analysis.IProgressMonitor;
27+
import org.gitools.api.matrix.*;
28+
import org.gitools.matrix.model.MatrixLayer;
29+
import org.gitools.matrix.transform.parameters.AggregatorParameter;
30+
import org.gitools.utils.aggregation.MeanAggregator;
31+
import org.gitools.utils.aggregation.MedianAggregator;
32+
import org.gitools.utils.aggregation.StdDevAggregator;
33+
34+
35+
public class ZScoreFunction extends ConfigurableTransformFunction {
36+
37+
public static final String AGGREGATION_PARAM = "Aggregation";
38+
AggregatorParameter aggregatorParameter;
39+
40+
private IProgressMonitor monitor;
41+
private Double estimator;
42+
private Double stdDev;
43+
44+
45+
public ZScoreFunction() {
46+
super("Fold-Change");
47+
}
48+
49+
50+
@Override
51+
public Double apply(Double value, IMatrixPosition position) {
52+
53+
if (estimator == null || stdDev == null || value == null) {
54+
return null;
55+
} else {
56+
return (value - estimator) / stdDev;
57+
}
58+
59+
}
60+
61+
62+
@Override
63+
public void onBeforeIterate(IMatrixIterable<Double> parentIterable) {
64+
super.onBeforeIterate(parentIterable);
65+
monitor = ApplicationContext.getProgressMonitor().subtask();
66+
67+
68+
IMatrix matrix = parentIterable.getPosition().getMatrix();
69+
70+
IMatrixLayer aggLayer = new MatrixLayer("agg", Double.class);
71+
72+
estimator = aggregatorParameter.getParameterValue().aggregate(parentIterable);
73+
stdDev = StdDevAggregator.INSTANCE.aggregate(parentIterable);
74+
75+
monitor.end();
76+
}
77+
78+
79+
80+
@Override
81+
public ZScoreFunction createNew() {
82+
return new ZScoreFunction();
83+
}
84+
85+
@Override
86+
protected void createDefaultParameters() {
87+
aggregatorParameter = new AggregatorParameter();
88+
aggregatorParameter.setDescription("Select if the fold change should be calculated with the row/column median or mean");
89+
IAggregator[] iAggregators = {
90+
MedianAggregator.INSTANCE,
91+
MeanAggregator.INSTANCE
92+
};
93+
aggregatorParameter.setChoices(iAggregators);
94+
addParameter(AGGREGATION_PARAM, aggregatorParameter);
95+
96+
}
97+
98+
public String getName() {
99+
return "Z-score";
100+
}
101+
102+
public String getDescription() {
103+
return "The Z-score reflects the relationship to the estimator of all values";
104+
}
105+
106+
}

0 commit comments

Comments
 (0)