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

* Continuing massive refactor

  * Implements Visualizer Interface
  * Breaks up ProteinDrawer
     * VisualizerNESWtoFile
     * VisualizerNESWtoConsole
parent 39f99480
No related branches found
No related tags found
No related merge requests found
package Enums;
public enum State {
Empty,
Hydrophobic,
Hydrophilic,
HydrophobicMulti,
HydrophilicMulti,
Mixed,
ConnectionVertical,
ConnectionHorizontal
}
package Interfaces; package Interfaces;
import Enums.State;
import MainClasses.Vertex;
import Visualization.Cell;
import java.util.ArrayList;
import java.util.List;
public interface Visualizer { public interface Visualizer {
void drawProtein(ArrayList<Vertex> vertexListOriginal, double fit, int bond, int over, int gen);
static 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;
}
static Cell[][] 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
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++) {
cellArray[yIndex][xIndex] = new Cell();
}
}
for (int i = 0; i < vertexList.size(); i++) {
Vertex vertex = vertexList.get(i);
if (vertex.isHydrophobic) {
cellArray[vertex.y][vertex.x].addState(State.Hydrophobic);
} else {
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) {
cellArray[vertex.y + 1][vertex.x].addState(State.ConnectionVertical);
} else if (vertex.outgoingDirection == 1) {
cellArray[vertex.y][vertex.x + 1].addState(State.ConnectionHorizontal);
} else if (vertex.outgoingDirection == 2) {
cellArray[vertex.y - 1][vertex.x].addState(State.ConnectionVertical);
} else if (vertex.outgoingDirection == 3) {
cellArray[vertex.y][vertex.x - 1].addState(State.ConnectionHorizontal);
}
}
}
return cellArray;
}
void setFilename(String filename);
int getMaxH();
int getMaxW();
} }
...@@ -2,6 +2,7 @@ package MainClasses; ...@@ -2,6 +2,7 @@ package MainClasses;
import Enums.Selection; import Enums.Selection;
import java.awt.*;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
...@@ -25,6 +26,27 @@ public class Config { ...@@ -25,6 +26,27 @@ public class Config {
static int K; // Number of selected Candidates to face off in a tournament selection static int K; // Number of selected Candidates to face off in a tournament selection
static String IMAGE_SEQUENCE_PATH; static String IMAGE_SEQUENCE_PATH;
// For images
public static final Font font = new Font("Sans-Serif", Font.PLAIN, 15);
public static final Color imageBackground = new Color(255, 255, 255);
public static final Color imageConnection = new Color(0, 0, 0);
public static final Color imageOutline = new Color(0, 0, 0);
public static final Color imageHydrophobic = new Color(205, 0, 0);
public static final Color imageHydrophilic = new Color(0, 0, 255);
public static final Color imageMixed = new Color(205, 0, 205);
public static final Color imageAminoText = new Color(0, 190, 190);
public static final Color imageText = new Color(0,0,0);
// For console output
public static final String consoleEmpty = " ";
public static final String consoleHydrophobic = "(o)";
public static final String consoleHydrophilic = "(i)";
public static final String consoleHydrophobicMulti = "{o}";
public static final String consoleHydrophilicMulti = "{i}";
public static final String consoleMixed = "{z}";
public static final String consoleConnectionVertical = " | ";
public static final String consoleConnectionHorizontal = "---";
// Points per hydrophobic bond // Points per hydrophobic bond
static int POINTS_PER_BOND; static int POINTS_PER_BOND;
......
...@@ -3,14 +3,12 @@ package MainClasses; ...@@ -3,14 +3,12 @@ package MainClasses;
import Enums.DirectionNESW; import Enums.DirectionNESW;
import Evaluators.EvaluatorNESW; import Evaluators.EvaluatorNESW;
import InitialGenerationCreators.Curl; import InitialGenerationCreators.Curl;
import Interfaces.Evaluator; import Interfaces.*;
import Interfaces.InitialGenerationCreator;
import Interfaces.Mutator;
import Interfaces.Selector;
import Mutators.Crossover; import Mutators.Crossover;
import Mutators.SinglePoint; import Mutators.SinglePoint;
import Selectors.OnlyBest; import Selectors.OnlyBest;
import Visualization.ProteinDrawer; import Visualization.Visualizers.VisualizerNESWtoConsole;
import Visualization.Visualizers.VisualizerNESWtoFile;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
...@@ -20,7 +18,7 @@ import java.util.Random; ...@@ -20,7 +18,7 @@ import java.util.Random;
public class GeneticAlgorithm { public class GeneticAlgorithm {
Random rand = new Random(); Random rand = new Random();
ProteinDrawer pdraw; Visualizer visualizer;
int[] isHydrophobic; int[] isHydrophobic;
Candidate[] population; Candidate[] population;
...@@ -40,7 +38,8 @@ public class GeneticAlgorithm { ...@@ -40,7 +38,8 @@ public class GeneticAlgorithm {
// Initialize with protein // Initialize with protein
public GeneticAlgorithm (int[] protein) { public GeneticAlgorithm (int[] protein) {
this.isHydrophobic = protein; this.isHydrophobic = protein;
this.pdraw = new ProteinDrawer(Config.IMAGE_SEQUENCE_PATH); // this.visualizer = new VisualizerNESWtoConsole();
this.visualizer = new VisualizerNESWtoFile(Config.IMAGE_SEQUENCE_PATH);
// this.initialGenCreator = new RandomDirection<>(this.rand, DirectionNESW.class); // this.initialGenCreator = new RandomDirection<>(this.rand, DirectionNESW.class);
// this.initialGenCreator = new StraightLine(); // this.initialGenCreator = new StraightLine();
...@@ -106,8 +105,8 @@ public class GeneticAlgorithm { ...@@ -106,8 +105,8 @@ public class GeneticAlgorithm {
int bonds = this.evaluator.evaluateBonds(this.population[bestIndex]); int bonds = this.evaluator.evaluateBonds(this.population[bestIndex]);
int overlaps = this.evaluator.evaluateOverlaps(this.population[bestIndex]); int overlaps = this.evaluator.evaluateOverlaps(this.population[bestIndex]);
this.pdraw.setFilename(String.format("gen_%07d.jpg",gen)); this.visualizer.setFilename(String.format("gen_%07d.jpg",gen));
this.pdraw.drawProteinToFile(this.population[bestIndex].getVertexList(), bestFitness, bonds, overlaps, gen); this.visualizer.drawProtein(this.population[bestIndex].getVertexList(), bestFitness, bonds, overlaps, gen);
System.out.println("The fitness is: " + bestFitness System.out.println("The fitness is: " + bestFitness
+ " [hydrophobicBonds = " + bonds + " | overlaps = " + overlaps + "]"); + " [hydrophobicBonds = " + bonds + " | overlaps = " + overlaps + "]");
...@@ -134,4 +133,12 @@ public class GeneticAlgorithm { ...@@ -134,4 +133,12 @@ public class GeneticAlgorithm {
return bestIndex; return bestIndex;
} }
public int getMaxH() {
return this.visualizer.getMaxH();
}
public int getMaxW() {
return this.visualizer.getMaxH();
}
} }
...@@ -15,8 +15,9 @@ public class Main { ...@@ -15,8 +15,9 @@ public class Main {
GeneticAlgorithm ga = new GeneticAlgorithm(protein); GeneticAlgorithm ga = new GeneticAlgorithm(protein);
ga.simulateGenerations(); ga.simulateGenerations();
// if (Config.isVidoable && Config.doVideo)
try { try {
VideoCreator.createVideo(config.getProperties()); VideoCreator.createVideo(config.getProperties(), ga.getMaxH(), ga.getMaxW());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
package Visualization;
import Enums.State;
import java.util.ArrayList;
public class Cell {
public ArrayList<State> states = new ArrayList<>();
public ArrayList<Integer> aminoIndexes = new ArrayList<>();
public Cell() {
states.add(State.Empty);
}
public void addState (State state) {
if (states.get(0).equals(State.Empty)) {
states.clear();
}
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;
}
}
package Visualization; package Visualization;
import MainClasses.Config;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
...@@ -28,14 +30,14 @@ public class VideoCreator{ ...@@ -28,14 +30,14 @@ public class VideoCreator{
} }
}; };
private static int[] resizeImages(String imagesPath) throws IOException { private static int[] resizeImages(String imagesPath, int maxH, int maxW) throws IOException {
dir = new File(imagesPath); dir = new File(imagesPath);
int[] widthHeight = new int[2]; int[] widthHeight = new int[2];
if (dir.isDirectory()) { if (dir.isDirectory()) {
// reads input images and determines maximum required size // reads input images and determines maximum required size
int maxHeight = ProteinDrawer.maxHeight; int maxHeight = maxH;
int maxWidth = ProteinDrawer.maxWidth; int maxWidth = maxW;
if (maxHeight <= 0 || maxWidth <= 0) { if (maxHeight <= 0 || maxWidth <= 0) {
int counter = 1; int counter = 1;
...@@ -71,7 +73,7 @@ public class VideoCreator{ ...@@ -71,7 +73,7 @@ public class VideoCreator{
// draws input image to the top left corner // draws input image to the top left corner
Graphics2D g2d = outputImage.createGraphics(); Graphics2D g2d = outputImage.createGraphics();
g2d.setColor(ProteinDrawer.imageBackground); g2d.setColor(Config.imageBackground);
g2d.fillRect(0,0, maxWidth, maxHeight); g2d.fillRect(0,0, maxWidth, maxHeight);
g2d.drawImage(inputImage, 0, 0, inputImage.getWidth(), inputImage.getHeight(), null); g2d.drawImage(inputImage, 0, 0, inputImage.getWidth(), inputImage.getHeight(), null);
g2d.dispose(); g2d.dispose();
...@@ -85,13 +87,13 @@ public class VideoCreator{ ...@@ -85,13 +87,13 @@ public class VideoCreator{
} }
// MainClasses.Main function // MainClasses.Main function
public static void createVideo(Properties properties) throws IOException { public static void createVideo(Properties properties, int maxH, int maxW) throws IOException {
String imagesPath = properties.getProperty("imageSequencePath"); String imagesPath = properties.getProperty("imageSequencePath");
String videoPathAndFile = properties.getProperty("videoPathAndFile"); String videoPathAndFile = properties.getProperty("videoPathAndFile");
int imgInterval = Integer.parseInt(properties.getProperty("imgInterval")); int imgInterval = Integer.parseInt(properties.getProperty("imgInterval"));
dir = new File(imagesPath); dir = new File(imagesPath);
int[] widthHeight = VideoCreator.resizeImages(imagesPath); int[] widthHeight = VideoCreator.resizeImages(imagesPath, maxH, maxW);
File file = new File(videoPathAndFile); File file = new File(videoPathAndFile);
if (!file.exists()) { if (!file.exists()) {
......
package Visualization.Visualizers;
import Enums.State;
import Interfaces.Visualizer;
import MainClasses.Config;
import MainClasses.Vertex;
import Visualization.Cell;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class VisualizerNESWtoConsole implements Visualizer {
int maxHeight;
int maxWidth;
public VisualizerNESWtoConsole() {
this.maxHeight = 0;
this.maxWidth = 0;
}
public void drawProtein(ArrayList<Vertex> vertexListOriginal, double fit, int bond, int over, int gen) {
// Copy VertexList to be able to manipulate it
ArrayList<Vertex> vertexList = Visualizer.deepCopyVertexList(vertexListOriginal);
Cell[][] cellArray = Visualizer.convertProteinTo2DArray(vertexList);
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();
}
System.out.println();
}
private String convertCellToString(Cell cell) {
State cellState = cell.getRelevantDrawState ();
switch (cellState) {
case Empty:
return Config.consoleEmpty;
case Hydrophobic:
return Config.consoleHydrophobic;
case Hydrophilic:
return Config.consoleHydrophilic;
case HydrophobicMulti:
return Config.consoleHydrophobicMulti;
case HydrophilicMulti:
return Config.consoleHydrophilicMulti;
case Mixed:
return Config.consoleMixed;
case ConnectionVertical:
return Config.consoleConnectionVertical;
case ConnectionHorizontal:
return Config.consoleConnectionHorizontal;
}
// Fallback
return Config.consoleEmpty;
}
@Override
public void setFilename(String format) {
// Only here so file based visualizers work
}
@Override
public int getMaxH() {
return this.maxHeight;
}
@Override
public int getMaxW() {
return this.maxWidth;
}
}
package Visualization; package Visualization.Visualizers;
import Enums.State;
import Interfaces.Visualizer;
import MainClasses.Config;
import MainClasses.Vertex; import MainClasses.Vertex;
import Visualization.Cell;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.*; import java.awt.*;
...@@ -8,53 +12,33 @@ import java.awt.image.BufferedImage; ...@@ -8,53 +12,33 @@ import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
public class ProteinDrawer { public class VisualizerNESWtoFile implements Visualizer {
String folder; String folder;
String filename; String filename;
// Used only for the console output int maxHeight;
String consoleEmpty = " "; int maxWidth;
String consoleHydrophobic = "(o)";
String consoleHydrophilic = "(i)";
String consoleHydrophobicMulti = "{o}";
String consoleHydrophilicMulti = "{i}";
String consoleMixed = "{z}";
String consoleConnectionVertical = " | ";
String consoleConnectionHorizontal = "---";
// For image // For image sizing
int pixelsPerCell = 40; int pixelsPerCell = 40;
int margin = pixelsPerCell * 2; int margin = pixelsPerCell * 2;
int outline = 2; int outline = 2;
public static final Font font = new Font("Sans-Serif", Font.PLAIN, 15);
public static final Color imageBackground = new Color(255, 255, 255);
public static final Color imageConnection = new Color(0, 0, 0);
public static final Color imageOutline = new Color(0, 0, 0);
public static final Color imageHydrophobic = new Color(205, 0, 0);
public static final Color imageHydrophilic = new Color(0, 0, 255);
public static final Color imageMixed = new Color(205, 0, 205);
public static final Color imageAminoText = new Color(180, 180, 180);
public static final Color imageText = new Color(0,0,0);
public static int maxHeight; public VisualizerNESWtoFile(String folder) {
public static int maxWidth;
public ProteinDrawer(String folder) {
this.folder = folder; this.folder = folder;
this.filename = "image.jpg"; // Default this.filename = "image.jpg"; // Default
maxHeight = 0; this.maxHeight = 0;
maxWidth = 0; this.maxWidth = 0;
} }
public void drawProteinToFile(ArrayList<Vertex> vertexListOriginal, double fit, int bond, int over, int gen) { public void drawProtein(ArrayList<Vertex> vertexListOriginal, double fit, int bond, int over, int gen) {
// Copy VertexList to be able to manipulate it // Copy VertexList to be able to manipulate it
ArrayList<Vertex> vertexList = deepCopyVertexList(vertexListOriginal); ArrayList<Vertex> vertexList = Visualizer.deepCopyVertexList(vertexListOriginal);
Cell[][] cellArray = convertProteinTo2DArray(vertexList); Cell[][] cellArray = Visualizer.convertProteinTo2DArray(vertexList);
int height = (cellArray.length * pixelsPerCell) + margin * 2; int height = (cellArray.length * pixelsPerCell) + margin * 2;
int width = (cellArray[0].length * pixelsPerCell) + margin * 2; int width = (cellArray[0].length * pixelsPerCell) + margin * 2;
...@@ -69,12 +53,12 @@ public class ProteinDrawer { ...@@ -69,12 +53,12 @@ public class ProteinDrawer {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics(); Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setFont(font); g2.setFont(Config.font);
FontMetrics metrics = g2.getFontMetrics(); FontMetrics metrics = g2.getFontMetrics();
int ascent = metrics.getAscent(); int ascent = metrics.getAscent();
// Background // Background
g2.setColor(imageBackground); g2.setColor(Config.imageBackground);
g2.fillRect(0, 0, width, height); g2.fillRect(0, 0, width, height);
for (int yIndex = cellArray.length-1; yIndex >= 0; yIndex--) { for (int yIndex = cellArray.length-1; yIndex >= 0; yIndex--) {
...@@ -91,27 +75,27 @@ public class ProteinDrawer { ...@@ -91,27 +75,27 @@ public class ProteinDrawer {
case Hydrophobic: case Hydrophobic:
case HydrophobicMulti: case HydrophobicMulti:
aminoColor = imageHydrophobic; aminoColor = Config.imageHydrophobic;
break; break;
case Hydrophilic: case Hydrophilic:
case HydrophilicMulti: case HydrophilicMulti:
aminoColor = imageHydrophilic; aminoColor = Config.imageHydrophilic;
break; break;
case Mixed: case Mixed:
aminoColor = imageMixed; aminoColor = Config.imageMixed;
break; break;
case ConnectionVertical: case ConnectionVertical:
g2.setColor(imageConnection); g2.setColor(Config.imageConnection);
g2.fillRect((xIndex * pixelsPerCell) + margin + (pixelsPerCell/2)-outline, g2.fillRect((xIndex * pixelsPerCell) + margin + (pixelsPerCell/2)-outline,
(yIndexPosition * pixelsPerCell) + margin, (yIndexPosition * pixelsPerCell) + margin,
outline * 2, pixelsPerCell); outline * 2, pixelsPerCell);
break; break;
case ConnectionHorizontal: case ConnectionHorizontal:
g2.setColor(imageConnection); g2.setColor(Config.imageConnection);
g2.fillRect((xIndex * pixelsPerCell) + margin, g2.fillRect((xIndex * pixelsPerCell) + margin,
(yIndexPosition * pixelsPerCell) + margin + (pixelsPerCell/2)-outline, (yIndexPosition * pixelsPerCell) + margin + (pixelsPerCell/2)-outline,
pixelsPerCell, outline * 2); pixelsPerCell, outline * 2);
...@@ -119,14 +103,14 @@ public class ProteinDrawer { ...@@ -119,14 +103,14 @@ public class ProteinDrawer {
} }
if (aminoColor != null) { if (aminoColor != null) {
g2.setColor(imageOutline); g2.setColor(Config.imageOutline);
g2.fillRect((xIndex * pixelsPerCell) + margin, (yIndexPosition * pixelsPerCell) + margin, g2.fillRect((xIndex * pixelsPerCell) + margin, (yIndexPosition * pixelsPerCell) + margin,
pixelsPerCell, pixelsPerCell); pixelsPerCell, pixelsPerCell);
g2.setColor(aminoColor); g2.setColor(aminoColor);
g2.fillRect((xIndex * pixelsPerCell) + outline + margin, (yIndexPosition * pixelsPerCell) + outline + margin, g2.fillRect((xIndex * pixelsPerCell) + outline + margin, (yIndexPosition * pixelsPerCell) + outline + margin,
pixelsPerCell - (outline * 2), pixelsPerCell - (outline * 2)); pixelsPerCell - (outline * 2), pixelsPerCell - (outline * 2));
g2.setColor(imageAminoText); g2.setColor(Config.imageAminoText);
String label = ""; String label = "";
for (int aminoIndex : cellArray[yIndex][xIndex].aminoIndexes) { for (int aminoIndex : cellArray[yIndex][xIndex].aminoIndexes) {
label += aminoIndex + " "; label += aminoIndex + " ";
...@@ -139,7 +123,7 @@ public class ProteinDrawer { ...@@ -139,7 +123,7 @@ public class ProteinDrawer {
} }
} }
g2.setColor(imageText); g2.setColor(Config.imageText);
String label = "Gen: " + gen String label = "Gen: " + gen
+ " Fitness: " + String.format("%.4f", fit) + " Fitness: " + String.format("%.4f", fit)
+ " H/H Bonds: " + bond + " H/H Bonds: " + bond
...@@ -159,196 +143,18 @@ public class ProteinDrawer { ...@@ -159,196 +143,18 @@ public class ProteinDrawer {
} }
} }
public void printProtein(ArrayList<Vertex> vertexListOriginal) { @Override
// Copy VertexList to be able to manipulate it
ArrayList<Vertex> vertexList = deepCopyVertexList(vertexListOriginal);
Cell[][] cellArray = convertProteinTo2DArray(vertexList);
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();
}
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 Cell[][] 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
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++) {
cellArray[yIndex][xIndex] = new Cell();
}
}
for (int i = 0; i < vertexList.size(); i++) {
Vertex vertex = vertexList.get(i);
if (vertex.isHydrophobic) {
cellArray[vertex.y][vertex.x].addState(State.Hydrophobic);
} else {
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) {
cellArray[vertex.y + 1][vertex.x].addState(State.ConnectionVertical);
} else if (vertex.outgoingDirection == 1) {
cellArray[vertex.y][vertex.x + 1].addState(State.ConnectionHorizontal);
} else if (vertex.outgoingDirection == 2) {
cellArray[vertex.y - 1][vertex.x].addState(State.ConnectionVertical);
} else if (vertex.outgoingDirection == 3) {
cellArray[vertex.y][vertex.x - 1].addState(State.ConnectionHorizontal);
}
}
}
return cellArray;
}
private String convertCellToString(Cell cell) {
State cellState = cell.getRelevantDrawState ();
switch (cellState) {
case Empty:
return consoleEmpty;
case Hydrophobic:
return consoleHydrophobic;
case Hydrophilic:
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;
}
public String getFolder() {
return folder;
}
public void setFolder(String folder) {
this.folder = folder;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) { public void setFilename(String filename) {
this.filename = filename; this.filename = filename;
} }
}
class Cell {
ArrayList<State> states = new ArrayList<>();
ArrayList<Integer> aminoIndexes = new ArrayList<>();
public Cell() {
states.add(State.Empty);
}
public void addState (State state) {
if (states.get(0).equals(State.Empty)) {
states.clear();
}
states.add(state);
}
public void addAminoIndex (int aminoIndex) { @Override
aminoIndexes.add(aminoIndex); public int getMaxH() {
return this.maxHeight;
} }
public State getRelevantDrawState () { @Override
State rc = states.get(0); public int getMaxW() {
if (states.size() > 1) { return this.maxWidth;
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