Skip to content
Snippets Groups Projects
Commit 985a3c71 authored by istkabra's avatar istkabra
Browse files

* Refactoring

* Added examples from professor
* Updated language level to 8
parent 1ed2a6a3
No related branches found
No related tags found
1 merge request!2Initial working prototype
......@@ -8,4 +8,16 @@
<artifactId>ProteinFolding</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
......@@ -8,7 +8,7 @@ public class Canidate {
int[] isHydrophobic; // 0 = no | 1 = yes
int[] outgoingDirection; // 0 = North | 1 = East | 2 = South | 3 = West
List<Vertex> vertexList;
ArrayList<Vertex> vertexList;
public Canidate(int[] isH, int[] oD) {
this.isHydrophobic = isH;
......@@ -16,24 +16,8 @@ public class Canidate {
this.vertexList = constructVertexes();
}
public double calculateFitness(boolean doOutput) {
double fitness = 0d;
int hydrophobicBonds = calculateBonds();
int overlaps = calculateOverlaps();
fitness =(double) (hydrophobicBonds * POINTS_PER_BOND) / (double) (overlaps + 1);
if (doOutput) {
System.out.println("The fitness is: " + fitness
+ " [hydrophobicBonds = " + hydrophobicBonds + " | overlaps = " + overlaps + "]\n");
}
return fitness;
}
private List<Vertex> constructVertexes() {
List<Vertex> vertexList = new ArrayList<Vertex>();
private ArrayList<Vertex> constructVertexes() {
ArrayList<Vertex> vertexList = new ArrayList<>();
int currentX = 0;
int currentY = 0;
......@@ -56,6 +40,26 @@ public class Canidate {
return vertexList;
}
public double calculateFitness(boolean doOutput) {
double fitness = 0d;
int hydrophobicBonds = calculateBonds();
int overlaps = calculateOverlaps();
fitness =(double) (hydrophobicBonds * POINTS_PER_BOND) / (double) (overlaps + 1);
if (doOutput) {
System.out.println("The fitness is: " + fitness
+ " [hydrophobicBonds = " + hydrophobicBonds + " | overlaps = " + overlaps + "]\n");
}
return fitness;
}
public ArrayList<Vertex> getVertexList() {
return vertexList;
}
private int calculateBonds() {
int bonds = 0;
......@@ -91,118 +95,4 @@ public class Canidate {
return overlaps / 2;
}
public void printProtein() {
// Determine size
int minX = 0;
int maxX = 0;
int minY = 0;
int maxY = 0;
for (Vertex vertex : vertexList) {
if (vertex.x < minX) {
minX = vertex.x;
}
if (vertex.x > maxX) {
maxX = vertex.x;
}
if (vertex.y < minY) {
minY = vertex.y;
}
if (vertex.y > maxY) {
maxY = vertex.y;
}
}
// Fix min values to 0 and double vertex coordinates to make room for connections
maxX += Math.abs(minX);
maxY += Math.abs(minY);
for (Vertex vertex : vertexList) {
vertex.x += Math.abs(minX);
vertex.x *= 2;
vertex.y += Math.abs(minY);
vertex.y *= 2;
}
// Add vertexes and connections to 2d array
String[][] proteinArray = new String[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] = " ";
}
}
for (int i = 0; i < vertexList.size(); i++) {
Vertex vertex = vertexList.get(i);
if (proteinArray[vertex.y][vertex.x] == " ") {
if (vertex.isHydrophobic) {
proteinArray[vertex.y][vertex.x] = " x ";
} else {
proteinArray[vertex.y][vertex.x] = " o ";
}
} else {
proteinArray[vertex.y][vertex.x] = " z ";
}
// Add connection, except on the last one
if (i+1 != vertexList.size()) {
if (vertex.outgoingDirection == 0) {
proteinArray[vertex.y+1][vertex.x] = " | ";
}
else if (vertex.outgoingDirection == 1) {
proteinArray[vertex.y][vertex.x+1] = "---";
}
else if (vertex.outgoingDirection == 2) {
proteinArray[vertex.y-1][vertex.x] = " | ";
}
else if (vertex.outgoingDirection == 3) {
proteinArray[vertex.y][vertex.x-1] = "---";
}
}
}
for (int yIndex = maxY*2; yIndex >= 0; yIndex--) {
for (int xIndex = 0; xIndex < maxX*2+1; xIndex++) {
System.out.print(proteinArray[yIndex][xIndex]);
}
System.out.println();
}
System.out.println();
}
}
// Helper class representing a single amino acid
class Vertex {
int x;
int y;
boolean isHydrophobic;
int outgoingDirection;
public Vertex(int x, int y, boolean isHydrophobic, int outgoingDirection) {
this.x = x;
this.y = y;
this.isHydrophobic = isHydrophobic;
this.outgoingDirection = outgoingDirection;
}
public boolean equalsPosition(Vertex vertex) {
return x == vertex.x && y == vertex.y;
}
public boolean neighbouringPosition(Vertex vertex) {
if (x == vertex.x && y == vertex.y + 1) {
return true; // South
}
else if (x == vertex.x && y == vertex.y -1) {
return true; // North
}
else if (x == vertex.x + 1 && y == vertex.y + 1) {
return true; // West
}
else if (x == vertex.x - 1 && y == vertex.y + 1) {
return true; // East
}
else {
return false;
}
}
}
// benchmark sequences for the 2d HP model
// 0 = hydrophil, "white"
// 1 = hydrophob, "black"
// data source: Ron Unger, John Moult: Genetic Algorithms for Protein Folding Simulations,
// Journal of Molecular Biology, Vol. 231, No. 1, May 1993
public class Examples {
public static final String SEQ20 = "10100110100101100101";
public static final String SEQ24 = "110010010010010010010011";
public static final String SEQ25 = "0010011000011000011000011";
public static final String SEQ36 = "000110011000001111111001100001100100";
public static final String SEQ48 = "001001100110000011111111110000001100110010011111";
public static final String SEQ50 = "11010101011110100010001000010001000101111010101011";
}
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);
}
}
}
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Main {
public static void main(String[] args) {
ProteinDrawer pdraw = new ProteinDrawer("./visualization/individual/", "image.png");
int[] isHydrophobic = {0, 0, 0, 1, 0, 1, 1, 0, 1}; // 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);
c.printProtein();
pdraw.printProtein(c.getVertexList());
Random rd = new Random();
int proteinSize = 20;
......@@ -38,6 +41,6 @@ public class Main {
System.out.println();
Canidate cRandom = new Canidate(isHydrophobicRandom, outgoingDirectionRandom);
cRandom.calculateFitness(true);
cRandom.printProtein();
pdraw.printProtein(cRandom.getVertexList());
}
}
import java.util.ArrayList;
import java.util.List;
public class ProteinDrawer {
String folder;
String filename;
// Used only for the console output
String consoleEmpty = " ";
String consoleHydrophobic = " o ";
String consoleHydrophilic = " i ";
String consoleMultiple = " z ";
String consoleConnectionVertical = " | ";
String consoleConnectionHorizontal = "---";
public ProteinDrawer(String folder, String filename) {
this.folder = folder;
this.filename = filename;
}
public void drawProteinToFile(ArrayList<Vertex> vertexListOriginal) {
// Copy VertexList to be able to manipulate it
ArrayList<Vertex> vertexList = deepCopyVertexList(vertexListOriginal);
Block[][] proteinArray = 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]));
}
System.out.println();
}
System.out.println();
}
public void printProtein(ArrayList<Vertex> vertexListOriginal) {
// Copy VertexList to be able to manipulate it
ArrayList<Vertex> vertexList = deepCopyVertexList(vertexListOriginal);
Block[][] proteinArray = 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]));
}
System.out.println();
}
System.out.println();
}
private ArrayList<Vertex> deepCopyVertexList (List<Vertex> vertexListOriginal) {
ArrayList<Vertex> vertexList = new ArrayList<>();
for (Vertex v : vertexListOriginal) {
Vertex vNew = new Vertex(v.x, v.y, v.isHydrophobic, v.outgoingDirection);
vertexList.add(vNew);
}
return vertexList;
}
private Block[][] convertProteinTo2DArray(ArrayList<Vertex> vertexList) {
// Determine size
int minX = 0;
int maxX = 0;
int minY = 0;
int maxY = 0;
for (Vertex vertex : vertexList) {
if (vertex.x < minX) {
minX = vertex.x;
}
if (vertex.x > maxX) {
maxX = vertex.x;
}
if (vertex.y < minY) {
minY = vertex.y;
}
if (vertex.y > maxY) {
maxY = vertex.y;
}
}
// Fix min values to 0 and double vertex coordinates to make room for connections
maxX += Math.abs(minX);
maxY += Math.abs(minY);
for (Vertex vertex : vertexList) {
vertex.x += Math.abs(minX);
vertex.x *= 2;
vertex.y += Math.abs(minY);
vertex.y *= 2;
}
// Add vertexes and connections to 2d array
Block[][] proteinArray = new Block[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();
}
}
for (int i = 0; i < vertexList.size(); i++) {
Vertex vertex = vertexList.get(i);
if (vertex.isHydrophobic) {
proteinArray[vertex.y][vertex.x].addState(State.Hydrophobic);
} else {
proteinArray[vertex.y][vertex.x].addState(State.Hydrophilic);
}
// 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);
} else if (vertex.outgoingDirection == 1) {
proteinArray[vertex.y][vertex.x + 1].addState(State.ConnectionHorizontal);
} else if (vertex.outgoingDirection == 2) {
proteinArray[vertex.y - 1][vertex.x].addState(State.ConnectionVertical);
} else if (vertex.outgoingDirection == 3) {
proteinArray[vertex.y][vertex.x - 1].addState(State.ConnectionHorizontal);
}
}
}
return proteinArray;
}
private String convertBlockToString (Block block) {
boolean multiple = false;
if (block.states.size() > 1) {
multiple = true;
}
State blockState = block.states.get(0);
switch (blockState) {
case Empty:
return consoleEmpty;
case Hydrophobic:
if (multiple) {
return consoleMultiple;
} else {
return consoleHydrophobic;
}
case Hydrophilic:
if (multiple) {
return consoleMultiple;
} else {
return consoleHydrophilic;
}
case ConnectionVertical:
return consoleConnectionVertical;
case ConnectionHorizontal:
return consoleConnectionHorizontal;
}
// Fallback
return consoleEmpty;
}
public String getFolder() {
return folder;
}
public void setFolder(String folder) {
this.folder = folder;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
}
class Block {
ArrayList<State> states = new ArrayList<>();
public Block () {
states.add(State.Empty);
}
public void addState (State state) {
if (states.get(0).equals(State.Empty)) {
states.clear();
}
states.add(state);
}
}
enum State {
Empty,
Hydrophobic,
Hydrophilic,
ConnectionVertical,
ConnectionHorizontal
}
\ No newline at end of file
// Helper class representing a single amino acid
class Vertex {
int x;
int y;
boolean isHydrophobic;
int outgoingDirection;
public Vertex(int x, int y, boolean isHydrophobic, int outgoingDirection) {
this.x = x;
this.y = y;
this.isHydrophobic = isHydrophobic;
this.outgoingDirection = outgoingDirection;
}
public boolean equalsPosition(Vertex vertex) {
return x == vertex.x && y == vertex.y;
}
public boolean neighbouringPosition(Vertex vertex) {
if (x == vertex.x && y == vertex.y + 1) {
return true; // South
}
else if (x == vertex.x && y == vertex.y -1) {
return true; // North
}
else if (x == vertex.x + 1 && y == vertex.y + 1) {
return true; // West
}
else if (x == vertex.x - 1 && y == vertex.y + 1) {
return true; // East
}
else {
return false;
}
}
}
\ 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