Skip to content

Commit 35d769a

Browse files
committed
Better error reporting from expression task.
Refs #185
1 parent 16ec195 commit 35d769a

File tree

4 files changed

+82
-50
lines changed

4 files changed

+82
-50
lines changed

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/commands/MastermapCommandTask.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.cytoscape.work.SynchronousTaskManager;
2121
import org.cytoscape.work.TaskIterator;
2222
import org.cytoscape.work.TaskMonitor;
23+
import org.cytoscape.work.TaskMonitor.Level;
2324
import org.cytoscape.work.Tunable;
2425
import org.cytoscape.work.util.ListSingleSelection;
2526

@@ -56,10 +57,10 @@ public class MastermapCommandTask extends AbstractTask implements ObservableTask
5657

5758

5859
@Inject private SynchronousTaskManager<?> taskManager;
59-
@Inject private CreateEnrichmentMapTaskFactory.Factory taskFactoryFactory;
60+
@Inject private CreateEnrichmentMapTaskFactory.Factory createEMTaskFactory;
6061

6162

62-
private Long[] result = { null };
63+
private Long[] resultNetworkSUID = { null };
6364

6465

6566
public MastermapCommandTask() {
@@ -76,6 +77,13 @@ public void run(TaskMonitor tm) throws Exception {
7677
throw new IllegalArgumentException("rootFolder is invalid: " + rootFolder);
7778
}
7879

80+
runTask(tm);
81+
82+
tm.setStatusMessage("Done");
83+
}
84+
85+
86+
private void runTask(TaskMonitor tm) {
7987
// Scan root folder (note: throws exception if no data sets were found)
8088
var resolverTask = new DataSetResolverTask(rootFolder);
8189

@@ -104,14 +112,12 @@ public void run(TaskMonitor tm) throws Exception {
104112
params.setNetworkName(filterArgs.networkName);
105113
}
106114

107-
var taskFactory = taskFactoryFactory.create(params, dataSets);
108-
var tasks = taskFactory.createTaskIterator();
109-
110-
taskManager.execute(tasks, TaskUtil.taskFinished(CreateEMNetworkTask.class, networkTask -> {
111-
result[0] = networkTask.getResults(Long.class); // get SUID of created network
112-
}));
115+
var tasks = createEMTaskFactory.create(params, dataSets).createTaskIterator();
113116

114-
tm.setStatusMessage("Done");
117+
taskManager.execute(tasks, TaskUtil.taskFinished(CreateEMNetworkTask.class,
118+
(task) -> resultNetworkSUID[0] = task.getResults(Long.class),
119+
(error) -> tm.showMessage(Level.ERROR, "Failed to create EnrichmentMap network. Check task log for details.")
120+
));
115121
}
116122

117123

@@ -156,11 +162,12 @@ public List<Class<?>> getResultClasses() {
156162

157163
@Override
158164
public <R> R getResults(Class<? extends R> type) {
165+
Long suid = resultNetworkSUID[0];
159166
if(String.class.equals(type)) {
160-
return type.cast(String.valueOf(result[0]));
167+
return type.cast(String.valueOf(suid));
161168
}
162169
if(Long.class.equals(type)) {
163-
return type.cast(result[0]);
170+
return type.cast(suid);
164171
}
165172
return null;
166173
}

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/model/GeneExpression.java

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@
4545

4646
import java.util.Arrays;
4747

48-
import org.apache.commons.math3.util.Precision;
49-
5048

5149
/**
5250
* Class representing the expression of one gene/protein
@@ -57,7 +55,7 @@ public class GeneExpression {
5755
private String description;
5856
private float[] expression;
5957

60-
public GeneExpression(String name, String description) {
58+
private GeneExpression(String name, String description) {
6159
this.name = name;
6260
this.description = description;
6361
}
@@ -72,37 +70,6 @@ public GeneExpression(String name, String description, float dummyVal) {
7270
this.expression = new float[] { dummyVal };
7371
}
7472

75-
/**
76-
* Create an array of the expression values.
77-
*/
78-
public void setExpression(String[] expres) {
79-
// ignore the first two cells --> only if there are at least 3 cells
80-
int size = expres.length;
81-
82-
if (size > 2) {
83-
expression = new float[size - 2];
84-
for (int i = 2; i < size; i++) {
85-
expression[i - 2] = parseAndRound(expres[i]);
86-
}
87-
} else {
88-
expression = new float[1];
89-
try {
90-
expression[0] = parseAndRound(expres[1]);
91-
} catch (NumberFormatException e) {
92-
// if the column doesn't contain doubles then just assume that the expression file is empty
93-
expression[0] = 0.0f;
94-
}
95-
}
96-
}
97-
98-
99-
private float parseAndRound(String exp) {
100-
float f = Float.parseFloat(exp);
101-
float r = Precision.round(f, 4);
102-
return r;
103-
}
104-
105-
10673
/**
10774
* Row normalize the current gene expression set. Row normalization involved
10875
* subtracting the mena of the row from each expression value in the row and

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/parsers/ExpressionFileReaderTask.java

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@
4747
import java.util.List;
4848
import java.util.Map;
4949

50+
import org.apache.commons.math3.util.Precision;
5051
import org.baderlab.csplugins.enrichmentmap.model.EMDataSet;
5152
import org.baderlab.csplugins.enrichmentmap.model.EnrichmentMap;
5253
import org.baderlab.csplugins.enrichmentmap.model.GeneExpression;
5354
import org.baderlab.csplugins.enrichmentmap.model.GeneExpressionMatrix;
5455
import org.baderlab.csplugins.enrichmentmap.util.NullTaskMonitor;
5556
import org.cytoscape.work.AbstractTask;
5657
import org.cytoscape.work.TaskMonitor;
58+
import org.cytoscape.work.TaskMonitor.Level;
5759

5860
/**
5961
* Parse expression file. The user can also use a rank file instead of an
@@ -114,7 +116,7 @@ public GeneExpressionMatrix parse(TaskMonitor taskMonitor) throws IOException {
114116
int expressionUniverse = 0;
115117
boolean twoColumns = false;
116118

117-
taskMonitor.setStatusMessage("Parsing GCT file - " + maxValue + " rows");
119+
taskMonitor.setStatusMessage("Parsing Expression file - " + maxValue + " rows");
118120

119121
for(int i = 0; i < lines.size(); i++) {
120122
String line = lines.get(i);
@@ -193,8 +195,8 @@ public GeneExpressionMatrix parse(TaskMonitor taskMonitor) throws IOException {
193195
description = tokens[1];
194196
}
195197

196-
GeneExpression expres = new GeneExpression(Name, description);
197-
expres.setExpression(tokens);
198+
float[] expressionsAsFloat = parseExpressions(tokens);
199+
GeneExpression expres = new GeneExpression(Name, description, expressionsAsFloat);
198200
expression.put(genekey, expres);
199201
}
200202
expressionUniverse++;
@@ -211,10 +213,49 @@ public GeneExpressionMatrix parse(TaskMonitor taskMonitor) throws IOException {
211213
return expressionMatrix;
212214
}
213215

216+
217+
public static float[] parseExpressions(String[] expres) {
218+
// ignore the first two cells --> only if there are at least 3 cells
219+
int size = expres.length;
220+
float[] expression;
221+
222+
if (size > 2) {
223+
expression = new float[size - 2];
224+
for (int i = 2; i < size; i++) {
225+
try {
226+
expression[i - 2] = parseAndRound(expres[i]);
227+
} catch(NumberFormatException e) {
228+
throw new NumberFormatException("The expression file contains the text '" + expres[i] + "' where a number was expected.");
229+
}
230+
}
231+
} else {
232+
expression = new float[1];
233+
try {
234+
expression[0] = parseAndRound(expres[1]);
235+
} catch (NumberFormatException e) {
236+
// if the column doesn't contain doubles then just assume that the expression file is empty
237+
expression[0] = 0.0f;
238+
}
239+
}
240+
return expression;
241+
}
242+
243+
244+
private static float parseAndRound(String exp) {
245+
float f = Float.parseFloat(exp);
246+
float r = Precision.round(f, 4);
247+
return r;
248+
}
249+
214250

215251
@Override
216252
public void run(TaskMonitor taskMonitor) throws Exception {
217-
taskMonitor.setTitle("Parsing GCT file");
218-
parse(taskMonitor);
253+
taskMonitor.setTitle("Parsing Expression file");
254+
try {
255+
parse(taskMonitor);
256+
} catch(NumberFormatException e) {
257+
taskMonitor.showMessage(Level.ERROR, e.getMessage());
258+
throw e;
259+
}
219260
}
220261
}

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/util/TaskUtil.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,23 @@ public void allFinished(FinishStatus finishStatus) { }
6565
};
6666
}
6767

68+
public static <T> TaskObserver taskFinished(Class<T> taskType, Consumer<T> finishConsumer, Consumer<FinishStatus> failConsumer) {
69+
return new TaskObserver() {
70+
@Override
71+
public void taskFinished(ObservableTask task) {
72+
if(taskType.isInstance(task)) {
73+
finishConsumer.accept(taskType.cast(task));
74+
}
75+
}
76+
@Override
77+
public void allFinished(FinishStatus finishStatus) {
78+
if(finishStatus.getType() == Type.FAILED) {
79+
failConsumer.accept(finishStatus);
80+
}
81+
}
82+
};
83+
}
84+
6885
public static ListSingleSelection<String> lssFromEnum(Enum<?> ... values) {
6986
List<String> names = new ArrayList<>(values.length);
7087
for(Enum<?> value : values) {

0 commit comments

Comments
 (0)