Skip to content
Snippets Groups Projects
Commit 976347b3 authored by istkabra's avatar istkabra
Browse files

* Saving images of proteins now works

parent 985a3c71
Branches
No related tags found
1 merge request!2Initial working prototype
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
......@@ -40,7 +40,7 @@ public class Canidate {
return vertexList;
}
public double calculateFitness(boolean doOutput) {
public double[] calculateFitness(boolean doOutput) {
double fitness = 0d;
int hydrophobicBonds = calculateBonds();
......@@ -53,7 +53,7 @@ public class Canidate {
+ " [hydrophobicBonds = " + hydrophobicBonds + " | overlaps = " + overlaps + "]\n");
}
return fitness;
return new double[]{fitness, hydrophobicBonds, overlaps};
}
public ArrayList<Vertex> getVertexList() {
......
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
// Example to illustrate Java 2D graphics usage for Genetic Algorithms
// (C) Alexander del Pino
public class GraphicsExample {
public static void main(String[] args) {
int height = 500;
int width = 800;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setColor(Color.YELLOW);
g2.fillRect(0, 0, width, height);
int cellSize = 80;
g2.setColor(new Color(0, 200, 0));
g2.fillRect(50, 50, cellSize, cellSize);
g2.setColor(new Color(255, 0, 0));
g2.fillRect(250, 50, cellSize, cellSize);
g2.setColor(Color.BLACK);
g2.drawLine(50 + cellSize, 50+cellSize/2, 250, 50+cellSize/2);
g2.setColor(new Color(255, 255, 255));
String label = "GA";
Font font = new Font("Serif", Font.PLAIN, 40);
g2.setFont(font);
FontMetrics metrics = g2.getFontMetrics();
int ascent = metrics.getAscent();
int labelWidth = metrics.stringWidth(label);
g2.drawString(label, 50 + cellSize/2 - labelWidth/2 , 50 + cellSize/2 + ascent/2);
String folder = "./visualization/individual/";
String filename = "image.png";
if (!new File(folder).exists()) new File(folder).mkdirs();
try {
ImageIO.write(image, "png", new File(folder + File.separator + filename));
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
}
}
......@@ -9,38 +9,39 @@ public class Main {
ProteinDrawer pdraw = new ProteinDrawer("./visualization/individual/", "image.png");
int[] isHydrophobic = {0, 0, 0, 1, 0, 1, 1, 0, 1}; // 0 = no | 1 = yes
int[] isHydrophobic = {0, 0, 0, 1, 0, 1, 1, 0, 0}; // 0 = no | 1 = yes
// Last outgoing direction has no influence on the protein
int[] outgoingDirection = {0, 1, 2, 1, 2, 3, 3, 1, 0}; // 0 = North | 1 = East | 2 = South | 3 = West
Canidate c = new Canidate(isHydrophobic, outgoingDirection);
c.calculateFitness(true);
double[] fitnessBondOverlap = c.calculateFitness(true);
pdraw.printProtein(c.getVertexList());
Random rd = new Random();
int proteinSize = 20;
int[] isHydrophobicRandom = new int[proteinSize];
int[] outgoingDirectionRandom = new int[proteinSize];
for (int i = 0; i < proteinSize; i++) {
isHydrophobicRandom[i] = rd.nextInt(2);
outgoingDirectionRandom[i] = rd.nextInt(4);
}
System.out.print("Hydrophobic: ");
for (int a : isHydrophobicRandom) {
System.out.print(a + " ");
}
System.out.println();
System.out.print("Outgoing Direction: ");
for (int a : outgoingDirectionRandom) {
System.out.print(a + " ");
}
System.out.println();
Canidate cRandom = new Canidate(isHydrophobicRandom, outgoingDirectionRandom);
cRandom.calculateFitness(true);
pdraw.printProtein(cRandom.getVertexList());
pdraw.drawProteinToFile(c.getVertexList(), fitnessBondOverlap);
// Random rd = new Random();
// int proteinSize = 20;
// int[] isHydrophobicRandom = new int[proteinSize];
// int[] outgoingDirectionRandom = new int[proteinSize];
//
// for (int i = 0; i < proteinSize; i++) {
// isHydrophobicRandom[i] = rd.nextInt(2);
// outgoingDirectionRandom[i] = rd.nextInt(4);
// }
//
// System.out.print("Hydrophobic: ");
// for (int a : isHydrophobicRandom) {
// System.out.print(a + " ");
// }
// System.out.println();
//
// System.out.print("Outgoing Direction: ");
// for (int a : outgoingDirectionRandom) {
// System.out.print(a + " ");
// }
//
// System.out.println();
// Canidate cRandom = new Canidate(isHydrophobicRandom, outgoingDirectionRandom);
// cRandom.calculateFitness(true);
// pdraw.printProtein(cRandom.getVertexList());
}
}
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
......@@ -8,41 +13,141 @@ public class ProteinDrawer {
// Used only for the console output
String consoleEmpty = " ";
String consoleHydrophobic = " o ";
String consoleHydrophilic = " i ";
String consoleMultiple = " z ";
String consoleHydrophobic = "(o)";
String consoleHydrophilic = "(i)";
String consoleHydrophobicMulti = "{o}";
String consoleHydrophilicMulti = "{i}";
String consoleMixed = "{z}";
String consoleConnectionVertical = " | ";
String consoleConnectionHorizontal = "---";
// For image
int pixelsPerCell = 40;
int margin = pixelsPerCell * 2;
int outline = 2;
Font font = new Font("Sans-Serif", Font.PLAIN, 15);
Color imageBackground = new Color(255, 255, 255);
Color imageConnection = new Color(0, 0, 0);
Color imageOutline = new Color(0, 0, 0);
Color imageHydrophobic = new Color(205, 0, 0);
Color imageHydrophilic = new Color(0, 0, 255);
Color imageMixed = new Color(205, 0, 205);
Color imageAminoText = new Color(200, 200, 200);
Color imageText = new Color(0,0,0);
public ProteinDrawer(String folder, String filename) {
this.folder = folder;
this.filename = filename;
}
public void drawProteinToFile(ArrayList<Vertex> vertexListOriginal) {
public void drawProteinToFile(ArrayList<Vertex> vertexListOriginal, double[] fitBondOver) {
// Copy VertexList to be able to manipulate it
ArrayList<Vertex> vertexList = deepCopyVertexList(vertexListOriginal);
Block[][] proteinArray = convertProteinTo2DArray(vertexList);
Cell[][] cellArray = convertProteinTo2DArray(vertexList);
int height = (cellArray.length * pixelsPerCell) + margin * 2;
int width = (cellArray[0].length * pixelsPerCell) + margin * 2;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setFont(font);
FontMetrics metrics = g2.getFontMetrics();
int ascent = metrics.getAscent();
// Background
g2.setColor(imageBackground);
g2.fillRect(0, 0, width, height);
for (int yIndex = cellArray.length-1; yIndex >= 0; yIndex--) {
int yIndexPosition = cellArray.length-1 - yIndex;
for (int xIndex = 0; xIndex < cellArray[0].length; xIndex++) {
Color aminoColor = null;
State cellState = cellArray[yIndex][xIndex].getRelevantDrawState();
switch (cellState) {
case Empty:
break;
case Hydrophobic:
aminoColor = imageHydrophobic;
break;
for (int yIndex = proteinArray.length-1; yIndex >= 0; yIndex--) {
for (int xIndex = 0; xIndex < proteinArray[0].length; xIndex++) {
System.out.print(convertBlockToString(proteinArray[yIndex][xIndex]));
case Hydrophilic:
aminoColor = imageHydrophilic;
break;
case Mixed:
aminoColor = imageMixed;
break;
case ConnectionVertical:
g2.setColor(imageConnection);
g2.fillRect((xIndex * pixelsPerCell) + margin + (pixelsPerCell/2)-outline,
(yIndexPosition * pixelsPerCell) + margin,
outline * 2, pixelsPerCell);
break;
case ConnectionHorizontal:
g2.setColor(imageConnection);
g2.fillRect((xIndex * pixelsPerCell) + margin,
(yIndexPosition * pixelsPerCell) + margin + (pixelsPerCell/2)-outline,
pixelsPerCell, outline * 2);
break;
}
if (aminoColor != null) {
g2.setColor(imageOutline);
g2.fillRect((xIndex * pixelsPerCell) + margin, (yIndexPosition * pixelsPerCell) + margin,
pixelsPerCell, pixelsPerCell);
g2.setColor(aminoColor);
g2.fillRect((xIndex * pixelsPerCell) + outline + margin, (yIndexPosition * pixelsPerCell) + outline + margin,
pixelsPerCell - (outline * 2), pixelsPerCell - (outline * 2));
g2.setColor(imageAminoText);
String label = "";
for (int aminoIndex : cellArray[yIndex][xIndex].aminoIndexes) {
label += aminoIndex + " ";
}
int labelWidth = metrics.stringWidth(label);
int x = (xIndex * pixelsPerCell) + margin + (pixelsPerCell/2) - (labelWidth/2) + outline;
int y = (yIndexPosition * pixelsPerCell) + margin + (pixelsPerCell/2) + (ascent/2) - outline;
g2.drawString(label, x, y);
}
}
System.out.println();
}
System.out.println();
g2.setColor(imageText);
String label = "Fitness: " + fitBondOver[0]
+ " H/H Bonds: " + (int)fitBondOver[1]
+ " Overlaps: " + (int)fitBondOver[2];
int labelWidth = metrics.stringWidth(label);
int x = margin / 4;
int y = margin / 4;
g2.drawString(label, x, y);
if (!new File(folder).exists()) new File(folder).mkdirs();
try {
ImageIO.write(image, "png", new File(folder + File.separator + filename));
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
}
public void printProtein(ArrayList<Vertex> vertexListOriginal) {
// Copy VertexList to be able to manipulate it
ArrayList<Vertex> vertexList = deepCopyVertexList(vertexListOriginal);
Block[][] proteinArray = convertProteinTo2DArray(vertexList);
Cell[][] cellArray = convertProteinTo2DArray(vertexList);
for (int yIndex = proteinArray.length-1; yIndex >= 0; yIndex--) {
for (int xIndex = 0; xIndex < proteinArray[0].length; xIndex++) {
System.out.print(convertBlockToString(proteinArray[yIndex][xIndex]));
for (int yIndex = cellArray.length-1; yIndex >= 0; yIndex--) {
for (int xIndex = 0; xIndex < cellArray[0].length; xIndex++) {
System.out.print(convertCellToString(cellArray[yIndex][xIndex]));
}
System.out.println();
}
......@@ -58,7 +163,7 @@ public class ProteinDrawer {
return vertexList;
}
private Block[][] convertProteinTo2DArray(ArrayList<Vertex> vertexList) {
private Cell[][] convertProteinTo2DArray(ArrayList<Vertex> vertexList) {
// Determine size
int minX = 0;
int maxX = 0;
......@@ -90,69 +195,68 @@ public class ProteinDrawer {
}
// Add vertexes and connections to 2d array
Block[][] proteinArray = new Block[maxY * 2 + 1][maxX * 2 + 1];
Cell[][] cellArray = new Cell[maxY * 2 + 1][maxX * 2 + 1];
for (int yIndex = 0; yIndex < maxY * 2 + 1; yIndex++) {
for (int xIndex = 0; xIndex < maxX * 2 + 1; xIndex++) {
proteinArray[yIndex][xIndex] = new Block();
cellArray[yIndex][xIndex] = new Cell();
}
}
for (int i = 0; i < vertexList.size(); i++) {
Vertex vertex = vertexList.get(i);
if (vertex.isHydrophobic) {
proteinArray[vertex.y][vertex.x].addState(State.Hydrophobic);
cellArray[vertex.y][vertex.x].addState(State.Hydrophobic);
} else {
proteinArray[vertex.y][vertex.x].addState(State.Hydrophilic);
cellArray[vertex.y][vertex.x].addState(State.Hydrophilic);
}
cellArray[vertex.y][vertex.x].addAminoIndex(i);
// Add connection, except on the last one
if (i + 1 != vertexList.size()) {
if (vertex.outgoingDirection == 0) {
proteinArray[vertex.y + 1][vertex.x].addState(State.ConnectionVertical);
cellArray[vertex.y + 1][vertex.x].addState(State.ConnectionVertical);
} else if (vertex.outgoingDirection == 1) {
proteinArray[vertex.y][vertex.x + 1].addState(State.ConnectionHorizontal);
cellArray[vertex.y][vertex.x + 1].addState(State.ConnectionHorizontal);
} else if (vertex.outgoingDirection == 2) {
proteinArray[vertex.y - 1][vertex.x].addState(State.ConnectionVertical);
cellArray[vertex.y - 1][vertex.x].addState(State.ConnectionVertical);
} else if (vertex.outgoingDirection == 3) {
proteinArray[vertex.y][vertex.x - 1].addState(State.ConnectionHorizontal);
cellArray[vertex.y][vertex.x - 1].addState(State.ConnectionHorizontal);
}
}
}
return proteinArray;
return cellArray;
}
private String convertBlockToString (Block block) {
boolean multiple = false;
if (block.states.size() > 1) {
multiple = true;
}
private String convertCellToString(Cell cell) {
State blockState = block.states.get(0);
State cellState = cell.getRelevantDrawState ();
switch (blockState) {
switch (cellState) {
case Empty:
return consoleEmpty;
case Hydrophobic:
if (multiple) {
return consoleMultiple;
} else {
return consoleHydrophobic;
}
case Hydrophilic:
if (multiple) {
return consoleMultiple;
} else {
return consoleHydrophilic;
}
case HydrophobicMulti:
return consoleHydrophobicMulti;
case HydrophilicMulti:
return consoleHydrophilicMulti;
case Mixed:
return consoleMixed;
case ConnectionVertical:
return consoleConnectionVertical;
case ConnectionHorizontal:
return consoleConnectionHorizontal;
}
// Fallback
return consoleEmpty;
......@@ -175,10 +279,11 @@ public class ProteinDrawer {
}
}
class Block {
class Cell {
ArrayList<State> states = new ArrayList<>();
ArrayList<Integer> aminoIndexes = new ArrayList<>();
public Block () {
public Cell() {
states.add(State.Empty);
}
......@@ -188,12 +293,42 @@ class Block {
}
states.add(state);
}
public void addAminoIndex (int aminoIndex) {
aminoIndexes.add(aminoIndex);
}
public State getRelevantDrawState () {
State rc = states.get(0);
if (states.size() > 1) {
if (rc.equals(State.Hydrophobic)) {
for (State s : states) {
if (s.equals(State.Hydrophilic)) {
return State.Mixed;
}
}
return State.HydrophobicMulti;
} else if (rc.equals(State.Hydrophilic)) {
for (State s : states) {
if (s.equals(State.Hydrophobic)) {
return State.Mixed;
}
}
return State.HydrophilicMulti;
}
}
return rc;
}
}
enum State {
Empty,
Hydrophobic,
Hydrophilic,
HydrophobicMulti,
HydrophilicMulti,
Mixed,
ConnectionVertical,
ConnectionHorizontal
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment