Skip to content

Commit e3288ad

Browse files
Merge pull request #288 from ProgrammingLife2017/snp
Detect if children can be SNP'ed together
2 parents 5cf55c4 + 45b5a07 commit e3288ad

14 files changed

+811
-211
lines changed

ProgrammingLife.iml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
</content>
1313
<orderEntry type="inheritedJdk" />
1414
<orderEntry type="sourceFolder" forTests="false" />
15-
<orderEntry type="library" name="Maven: com.github.uphy:javafx-console:ce9860eb36" level="project" />
1615
<orderEntry type="library" name="Maven: junit:junit:4.12" level="project" />
1716
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
1817
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.5" level="project" />
1918
<orderEntry type="library" name="Maven: com.diffplug.durian:durian:3.4.0" level="project" />
19+
<orderEntry type="library" name="Maven: com.github.uphy:javafx-console:ce9860eb36" level="project" />
2020
<orderEntry type="library" name="Maven: org.mapdb:mapdb:3.0.4" level="project" />
2121
<orderEntry type="library" name="Maven: org.jetbrains.kotlin:kotlin-stdlib:1.0.7" level="project" />
2222
<orderEntry type="library" name="Maven: org.jetbrains.kotlin:kotlin-runtime:1.0.7" level="project" />

src/main/java/programminglife/gui/controller/GraphController.java

+41-44
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.util.LinkedList;
1616

1717
/**
18-
* Created by Martijn van Meerten on 8-5-2017.
1918
* Controller for drawing the graph.
2019
*/
2120
public class GraphController {
@@ -32,6 +31,7 @@ public class GraphController {
3231
private double zoomLevel = 1;
3332

3433
private int centerNodeInt;
34+
private boolean drawSNP = false;
3535

3636
/**
3737
* Initialize controller object.
@@ -49,55 +49,45 @@ public int getCenterNodeInt() {
4949
return this.centerNodeInt;
5050
}
5151

52+
/**
53+
* Utility function for benchmarking purposes.
54+
* @param description the description to print
55+
* @param r the {@link Runnable} to run/benchmark
56+
*/
57+
private void time(String description, Runnable r) {
58+
long start = System.nanoTime();
59+
r.run();
60+
Console.println(String.format("%s: %d ms", description, (System.nanoTime() - start) / 1000000));
61+
}
62+
5263
/**
5364
* Method to draw the subGraph decided by a center node and radius.
5465
* @param center the node of which the radius starts.
5566
* @param radius the amount of layers to be drawn.
5667
*/
5768
public void draw(int center, int radius) {
58-
long startTimeProgram = System.nanoTime();
59-
GraphicsContext gc = canvas.getGraphicsContext2D();
60-
61-
long startTimeSubGraph = System.nanoTime();
62-
63-
DrawableSegment centerNode = new DrawableSegment(graph, center);
64-
centerNodeInt = centerNode.getIdentifier();
65-
subGraph = new SubGraph(centerNode, radius);
66-
67-
long finishTimeSubGraph = System.nanoTime();
68-
69-
long startLayoutTime = System.nanoTime();
70-
subGraph.layout();
71-
long finishTimeLayout = System.nanoTime();
72-
73-
long startTimeColorize = System.nanoTime();
74-
colorize();
75-
long finishTimeColorize = System.nanoTime();
76-
77-
78-
long startTimeDrawing = System.nanoTime();
79-
draw(gc);
80-
long finishTimeDrawing = System.nanoTime();
81-
82-
highlightNode(center, Color.DARKORANGE);
83-
centerOnNodeId(center);
84-
85-
long finishTime = System.nanoTime();
86-
long differenceTimeProgram = finishTime - startTimeProgram;
87-
long differenceTimeDrawing = finishTimeDrawing - startTimeDrawing;
88-
long differenceTimeLayout = finishTimeLayout - startLayoutTime;
89-
long differenceTimeSubGraph = finishTimeSubGraph - startTimeSubGraph;
90-
long differenceTimeColorize = finishTimeColorize - startTimeColorize;
91-
long msDifferenceTimeProgram = differenceTimeProgram / 1000000;
92-
long millisecondTimeDrawing = differenceTimeDrawing / 1000000;
93-
long msDifferenceTimeLayout = differenceTimeLayout / 1000000;
94-
long msDifferenceTimeSubGraph = differenceTimeSubGraph / 1000000;
95-
long msDifferenceTimeColorize = differenceTimeColorize / 1000000;
96-
Console.println("time of SubGraph: " + msDifferenceTimeSubGraph);
97-
Console.println("Time of layout: " + msDifferenceTimeLayout);
98-
Console.println("Time of Colorize: " + msDifferenceTimeColorize);
99-
Console.println("Time of Drawing: " + millisecondTimeDrawing);
100-
Console.println("Time of Total Program: " + msDifferenceTimeProgram);
69+
time("Total drawing", () -> {
70+
DrawableSegment centerNode = new DrawableSegment(graph, center);
71+
centerNodeInt = centerNode.getIdentifier();
72+
GraphicsContext gc = canvas.getGraphicsContext2D();
73+
74+
time("Find subgraph", () -> subGraph = new SubGraph(centerNode, radius));
75+
76+
if (drawSNP) {
77+
time("Replace SNPs", subGraph::replaceSNPs);
78+
}
79+
time("Layout subgraph", subGraph::layout);
80+
81+
time("Colorize", this::colorize);
82+
83+
time("Calculate genomes through edges", subGraph::calculateGenomes);
84+
time("Drawing", () -> {
85+
draw(gc);
86+
});
87+
88+
centerOnNodeId(center);
89+
highlightNode(center, Color.DARKORANGE);
90+
});
10191
}
10292

10393
/**
@@ -357,6 +347,13 @@ public void highlightByGenome(int genomeID) {
357347
highlightNodes(drawNodeList, Color.YELLOW);
358348
}
359349

350+
/**
351+
* Sets if the glyph snippets will be drawn or not.
352+
*/
353+
void setSNP() {
354+
drawSNP = !drawSNP;
355+
}
356+
360357
/**
361358
* Returns the node clicked on else returns null.
362359
* @param x position horizontally where clicked

src/main/java/programminglife/gui/controller/GuiController.java

+21-11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import javafx.stage.FileChooser;
2121
import javafx.stage.FileChooser.ExtensionFilter;
2222
import javafx.stage.Stage;
23+
import javafx.scene.canvas.Canvas;
2324
import jp.uphy.javafx.console.ConsoleView;
2425
import programminglife.ProgrammingLife;
2526
import programminglife.controller.MiniMapController;
@@ -65,8 +66,11 @@ public class GuiController implements Observer {
6566
@FXML private MenuItem btnInstructions;
6667
@FXML private Menu menuRecentGFA;
6768
@FXML private Menu menuRecentGFF;
68-
@FXML private RadioMenuItem btnToggle;
69+
70+
@FXML private RadioMenuItem btnSNP;
71+
@FXML private RadioMenuItem btnConsole;
6972
@FXML private RadioMenuItem btnMiniMap;
73+
7074
@FXML private Button btnZoomReset;
7175
@FXML private Button btnTranslateReset;
7276
@FXML private Button btnDraw;
@@ -84,7 +88,7 @@ public class GuiController implements Observer {
8488
@FXML private AnchorPane anchorLeftControlPanel;
8589
@FXML private AnchorPane anchorGraphPanel;
8690
@FXML private AnchorPane anchorGraphInfo;
87-
@FXML private javafx.scene.canvas.Canvas miniMap;
91+
@FXML private Canvas miniMap;
8892

8993
private double orgSceneX, orgSceneY;
9094

@@ -276,18 +280,24 @@ private void fileChooser(ExtensionFilter filter, boolean isGFA) {
276280
*/
277281
private void initMenuBar() {
278282
btnOpenGFA.setOnAction((ActionEvent event) -> fileChooser(extFilterGFA, true));
279-
btnOpenGFF.setOnAction((ActionEvent event) -> fileChooser(extFilterGFF, false));
280-
281283
btnOpenGFA.setAccelerator(new KeyCodeCombination(KeyCode.O, KeyCodeCombination.CONTROL_DOWN));
284+
btnOpenGFF.setOnAction((ActionEvent event) -> fileChooser(extFilterGFF, false));
282285

283-
btnMiniMap.setOnAction(event -> miniMapController.toggleVisibility());
284-
btnMiniMap.setAccelerator(new KeyCodeCombination(KeyCode.M, KeyCodeCombination.CONTROL_DOWN));
285286
btnQuit.setOnAction(event -> Alerts.quitAlert());
286-
btnQuit.setAccelerator(new KeyCodeCombination(KeyCode.E, KeyCodeCombination.CONTROL_DOWN));
287+
btnQuit.setAccelerator(new KeyCodeCombination(KeyCode.Q, KeyCodeCombination.CONTROL_DOWN));
288+
287289
btnAbout.setOnAction(event -> Alerts.infoAboutAlert());
288290
btnAbout.setAccelerator(new KeyCodeCombination(KeyCode.I, KeyCodeCombination.CONTROL_DOWN));
289291
btnInstructions.setOnAction(event -> Alerts.infoInstructionAlert());
290292
btnInstructions.setAccelerator(new KeyCodeCombination(KeyCode.H, KeyCodeCombination.CONTROL_DOWN));
293+
294+
btnMiniMap.setOnAction(event -> miniMapController.toggleVisibility());
295+
btnMiniMap.setAccelerator(new KeyCodeCombination(KeyCode.M, KeyCodeCombination.CONTROL_DOWN));
296+
btnSNP.setOnAction(event -> {
297+
graphController.setSNP();
298+
Platform.runLater(this::draw);
299+
});
300+
btnSNP.setAccelerator(new KeyCodeCombination(KeyCode.G, KeyCodeCombination.CONTROL_DOWN));
291301
}
292302

293303

@@ -533,17 +543,17 @@ private void initConsole() {
533543
AnchorPane.setRightAnchor(console, 0.d);
534544
AnchorPane.setLeftAnchor(console, 0.d);
535545

536-
btnToggle.setOnAction(event -> {
537-
if (btnToggle.isSelected()) {
546+
btnConsole.setOnAction(event -> {
547+
if (btnConsole.isSelected()) {
538548
st.show();
539549
} else {
540550
st.close();
541551
}
542552
});
543553

544554
st.show();
545-
btnToggle.setSelected(true);
546-
root.visibleProperty().bind(btnToggle.selectedProperty());
555+
btnConsole.setSelected(true);
556+
root.visibleProperty().bind(btnConsole.selectedProperty());
547557

548558
Console.setOut(console.getOut());
549559
}

src/main/java/programminglife/model/drawing/DrawableDummy.java

+6
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ public String details() {
7676
return toString();
7777
}
7878

79+
@Override
80+
public DrawableSNP createSNPIfPossible(SubGraph subGraph) {
81+
return null;
82+
}
83+
7984
@Override
8085
public void colorize(SubGraph sg) {
8186
double genomeFraction = 0.d;
@@ -97,6 +102,7 @@ public void colorize(SubGraph sg) {
97102

98103
this.setStrokeWidth(strokeWidth);
99104
this.setStrokeColor(strokeColor);
105+
100106
}
101107

102108
/**

src/main/java/programminglife/model/drawing/DrawableNode.java

+35-4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,34 @@ public DrawableNode(GenomeGraph graph, int id) {
5050
}
5151
}
5252

53+
54+
@Override
55+
public final boolean equals(Object o) {
56+
if (this == o) {
57+
return true;
58+
}
59+
60+
if (!this.getClass().equals(o.getClass())) {
61+
return false;
62+
}
63+
return this.getIdentifier() == ((DrawableNode) o).getIdentifier();
64+
}
65+
66+
@Override
67+
public final int hashCode() {
68+
int result = getGraph().hashCode();
69+
result = 31 * result + getIdentifier();
70+
return result;
71+
}
72+
73+
/**
74+
* Get the ID.
75+
* @return the ID
76+
*/
77+
public final int getIdentifier() {
78+
return this.id;
79+
}
80+
5381
/**
5482
* Get the {@link GenomeGraph}.
5583
* @return the graph
@@ -110,6 +138,13 @@ private boolean isDrawDimensionsUpToDate() {
110138
*/
111139
public abstract String details();
112140

141+
/**
142+
* Checks if the children of this {@link DrawableNode} can be merged as a SNP.
143+
* @param subGraph the {@link SubGraph} this {@link DrawableNode} is in
144+
* @return null if children cannot be SNP'ed, SNP with (parent, child and mutation) otherwise
145+
*/
146+
public abstract DrawableSNP createSNPIfPossible(SubGraph subGraph);
147+
113148
/**
114149
* Color this according to contents.
115150
* @param subGraph {@link SubGraph} to colorize.
@@ -188,10 +223,6 @@ final XYCoordinate getRightBorderCenter() {
188223
return new XYCoordinate(location.getX() + getWidth(), location.getY() + 0.5 * getHeight());
189224
}
190225

191-
public final int getIdentifier() {
192-
return id;
193-
}
194-
195226
public final XYCoordinate getLocation() {
196227
return location;
197228
}

0 commit comments

Comments
 (0)