Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions backEnd/src/main/java/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import datatree.TempReadWriteTree;
import parser.Parser;
import mutation.Mutations;

import java.util.ArrayList;
import java.util.List;

import phylogenetictree.PhylogeneticTree;
import ribbonnodes.RibbonNode;

Expand Down Expand Up @@ -48,22 +50,24 @@ public class Controller implements FrontEndBackEndInterface {
* Constructor.
*/
public Controller() {
String gfaFile = "data/TB328.gfa";
String gfaFile = "data/TB328.gfa";
genomeGraph = Parser.parse(gfaFile);
genomeGraph.generateGenomes();
genomeGraph.findStartAndCalculateX();
phylogeneticTree.parseTree("data/340tree.rooted.TKK.nwk",
new ArrayList<>(genomeGraph.getGenomes().keySet()));
phylogeneticTree.parseTree("data/340tree.rooted.TKK.nwk",
new ArrayList<>(genomeGraph.getGenomes().keySet()));
dataTree = new DataTree(new DataNode(phylogeneticTree.getRoot(),
null, 0));

dataTree.setMinStrandsToReturn(genomeGraph.getStrandNodes().size() / 8);

if (gfaFile.equals("data/TB328.gfa")) {
TempReadWriteTree.readFile(dataTree, genomeGraph.getStrandNodes(), "data/tempTree.txt");
TempReadWriteTree.readFile(dataTree, genomeGraph.getStrandNodes(), "data/tempTree.txt");
} else {
dataTree.addStrands(new ArrayList<>(genomeGraph.getGenomes().values()));

}
ribbonController = new RibbonController(genomeGraph, dataTree);
ribbonController.setMaxStrandsToReturn(3000);
Mutations mutations = new Mutations(genomeGraph);
mutations.computeAllMutations();
dc = this;
Expand Down Expand Up @@ -92,8 +96,8 @@ public ArrayList<RibbonNode> getRibbonNodes(int minX, int maxX, int zoomLevel) {
public PhylogeneticTree loadPhylogeneticTree(int treeId) {
if (treeId == 0) {
phylogeneticTree = new PhylogeneticTree();
phylogeneticTree.parseTree("testGenomeNwk",
new ArrayList<>(genomeGraph.getGenomes().keySet()));
phylogeneticTree.parseTree("testGenomeNwk",
new ArrayList<>(genomeGraph.getGenomes().keySet()));
return phylogeneticTree;
} else {
return phylogeneticTree;
Expand All @@ -115,14 +119,15 @@ public List<String> setActiveGenomes(ArrayList<String> activeGenomes) {
/**
* Get the singleton dc.
* If dc is not instantiated yet, do this first.
*
* @return The controller dc.
*/
public static Controller getDC() {
if (dc == null) {
dc = new Controller();
}
return dc;
if (dc == null) {
dc = new Controller();
}
return dc;
}


}
2 changes: 1 addition & 1 deletion backEnd/src/main/java/controller/GenomeGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void calculateXfromStart(Strand start) {
for (StrandEdge edge : strand.getEdges()) {
Strand nextStrand = strandNodes.get(edge.getEnd());
if (nextStrand.getX() < strand.getX() + 1) {
nextStrand.setX(strand.getX() + 1);
nextStrand.setX(strand.getX()+strand.getSequence().length() + 1);
nextStrands.add(nextStrand);
}
}
Expand Down
96 changes: 63 additions & 33 deletions backEnd/src/main/java/controller/RibbonController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
*/
public final class RibbonController {

/**
* The graph that contains the geographic information of the stands.
*/
/**
* The graph that contains the geographic information of the stands.
*/
private GenomeGraph genomeGraph;

/**
Expand All @@ -32,6 +32,11 @@ public final class RibbonController {
*/
private HashMap<String, Color> colorMap;

/**
* The maximal amount of strands to return.
*/
private int maxStrandsToReturn = 0;

/**
* Create ribbonController object.
*
Expand Down Expand Up @@ -80,7 +85,6 @@ public ArrayList<RibbonNode> getRibbonNodes(int minX, int maxX, int zoomLevel) {
ArrayList<Genome> actGen = genomeGraph.getActiveGenomes();



ArrayList<RibbonNode> result = new ArrayList<>();
ArrayList<Strand> filteredNodes = dataTree.getStrands(minX, maxX, actGen, zoomLevel + 1);

Expand All @@ -97,11 +101,16 @@ public ArrayList<RibbonNode> getRibbonNodes(int minX, int maxX, int zoomLevel) {
}


result.sort((RibbonNode o1, RibbonNode o2) -> Integer.valueOf(o1.getX()).compareTo(o2.getX()));
calcYcoordinates(result);
result.sort((RibbonNode o1, RibbonNode o2) -> new Integer(o1.getX()).compareTo(o2.getX()));
addEdges(result);
addMutationLabels(result);
spreadYCoordinates(result);
collapseRibbons(result, Math.max(0, 5 - zoomLevel));

if(zoomLevel<10){
addMutationLabels(result);
}

System.out.println(result.size() + " nodes returned");
return result;

}
Expand All @@ -112,24 +121,34 @@ public ArrayList<RibbonNode> getRibbonNodes(int minX, int maxX, int zoomLevel) {
* @param nodes The ribbonNode Graph to collapse.
* @return A collapsed graph.
*/
public ArrayList<RibbonNode> collapseRibbons(ArrayList<RibbonNode> nodes) {
for (int i = 0; i < nodes.size(); i++) {
RibbonNode node = nodes.get(i);
if (node != null && node.getOutEdges().size() == 1) {
RibbonNode other = getNodeWithId(node.getOutEdges().get(0).getEnd(), nodes);
if (other.getInEdges().size() == 1) {
node.addStrands(other.getStrands());
for (RibbonEdge edge : other.getOutEdges()) {
edge.setStartId(node.getId());
private void collapseRibbons(ArrayList<RibbonNode> nodes, int iterations) {
int nIter = 0;
int changedThisPass = 100000;
while (nIter < iterations && nodes.size() > maxStrandsToReturn && changedThisPass > maxStrandsToReturn / 2) {
nIter++;
changedThisPass = 0;
for (int i = 0; i < nodes.size(); i++) {
RibbonNode node = nodes.get(i);
if (node != null) {
if (node.getOutEdges().size() == 1) {
RibbonNode other = getNodeWithId(node.getOutEdges().get(0).getEnd(), nodes);
if (other.getInEdges().size() == 1) {
node.addStrands(other.getStrands());
for (RibbonEdge edge : other.getOutEdges()) {
edge.setStartId(node.getId());
}
node.setOutEdges(other.getOutEdges());
nodes.remove(other);
changedThisPass++;
}
node.setOutEdges(other.getOutEdges());
nodes.remove(other);
}
}
}
return nodes;
}

}


/**
* Return a node with a certain id contained in a Ribbon Graph.
*
Expand All @@ -154,18 +173,18 @@ public RibbonNode getNodeWithId(int id, ArrayList<RibbonNode> nodes) {
* @param nodes The ribbonGraph to calculate y cooridnates for.
* @return The ribbonGraph with added y coordinates.
*/
public ArrayList<RibbonNode> calcYcoordinates(ArrayList<RibbonNode> nodes) {
private void spreadYCoordinates(ArrayList<RibbonNode> nodes) {
int currentX = 0;
ArrayList<RibbonNode> currentXNodes = new ArrayList<>();
for (int i = 0; i < nodes.size(); i++) {
RibbonNode node = nodes.get(i);
if (node.getX() == currentX) {
if (node.getX() < currentX + 1000 && node.getX() > currentX - 1000) {
currentXNodes.add(node);
} else {
if (currentXNodes.size() > 1) {
for (int j = 0; j < currentXNodes.size(); j++) {
int minY = (currentXNodes.size() / 2) * -10;
currentXNodes.get(j).setY(minY + 15 * j);
currentXNodes.get(j).setY(currentXNodes.get(j).getY() - 10 * (genomeGraph.getActiveGenomes().size() - 1));
currentXNodes.get(j).setY((int) (currentXNodes.get(j).getY() * (Math.pow(-1, j))));
}
}
currentXNodes = new ArrayList<>();
Expand All @@ -176,7 +195,7 @@ public ArrayList<RibbonNode> calcYcoordinates(ArrayList<RibbonNode> nodes) {

}

return nodes;

}


Expand All @@ -186,22 +205,23 @@ public ArrayList<RibbonNode> calcYcoordinates(ArrayList<RibbonNode> nodes) {
* @param nodes the RibbinGraph to calculate edges for.
* @return The ribbonGraph with added edges.
*/
public ArrayList<RibbonNode> addEdges(ArrayList<RibbonNode> nodes) {

public void addEdges(ArrayList<RibbonNode> nodes) {
for (Genome genome : genomeGraph.getActiveGenomes()) {
RibbonNode currentNode = findNextNodeWithGenome(nodes, genome, -1);
while (currentNode != null) {
currentNode = addEdgeReturnEnd(nodes, currentNode, genome);
}

}
return nodes;


}

/**
* Finds the next node that contains a certain genome.
* Creates an edge between the two nodes and returns the end Node of the edge.
*
* @param nodes The RibbonGraph.
* @param currentNode The start node of the edge.
* @param genome The genome to find an edge for.
Expand All @@ -217,6 +237,7 @@ public RibbonNode addEdgeReturnEnd(ArrayList<RibbonNode> nodes,
currentNode.addEdge(edge);
next.addEdge(edge);
} else {
next.setY(next.getY() + 10);
RibbonEdge edge = currentNode.getOutEdge(currentNode.getId(), next.getId());
edge.addGenomeToEdge(getColorForGenome(genome));
}
Expand Down Expand Up @@ -265,13 +286,22 @@ public Color getColorForGenome(Genome genome) {
return result;
}

/**
* Set the maximal amount of strands to return.
*
* @param maxStrandsToReturn
*/
public void setMaxStrandsToReturn(int maxStrandsToReturn) {
this.maxStrandsToReturn = maxStrandsToReturn;
}

public void addMutationLabels(ArrayList<RibbonNode> nodes) {
for (RibbonNode node : nodes) {
Strand strand = node.getStrands().get(0);
if (strand.getMutations().size() > 0) {
System.out.println("Mutation added");
node.setLabel(strand.getMutations().get(0).toString());
}
}
for (RibbonNode node : nodes) {
Strand strand = node.getStrands().get(0);
if (strand.getMutations().size() > 0) {
System.out.println("Mutation added");
node.setLabel(strand.getMutations().get(0).toString());
}
}
}
}
26 changes: 23 additions & 3 deletions backEnd/src/main/java/datatree/DataTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import java.util.concurrent.ForkJoinPool;

/**
* Tree containing all strands and genomes of the data.
*/
public class DataTree extends TreeStructure<DataNode> {

/**
* The minimal amount of strands to return.
*/
private int minStrandsToReturn=0;

/**
* Default constructor.
Expand Down Expand Up @@ -42,7 +47,7 @@ public void addStrands(ArrayList<Genome> genomes) {
}

}

ForkJoinPool pool = new ForkJoinPool();
pool.invoke(new AddStrandsFromChildren(getRoot()));
//TempReadWriteTree.writeTree((getRoot()));
Expand Down Expand Up @@ -74,15 +79,19 @@ public ArrayList<Strand> getStrands(int xMin, int xMax,
*/
public ArrayList<Strand> filterStrandsFromNodes(int xMin, int xMax, Set<DataNode> nodes) {
ArrayList<Strand> result = new ArrayList<>();
ArrayList<Integer> resultIDs = new ArrayList<>();

for (DataNode node : nodes) {
for (Strand strand : node.getStrands()) {
if (strand.getX() < xMax + 10 && strand.getX() > xMin - 10) {
if (strand.getX() > xMin - 10000 && strand.getX() < xMax + 10000) {
result.add(strand);
resultIDs.add(strand.getId());
}
}

}


return result;


Expand Down Expand Up @@ -114,17 +123,28 @@ public Set<DataNode> getDataNodesForGenomes(ArrayList<Genome> genomes, int level
public Set<DataNode> getDataNodesForGenome(Genome genome, int level) {
Set<DataNode> result = new HashSet<>();
DataNode currentNode = getRoot();
int totalStrands = 0;
while (currentNode.getLevel() <= level) {
result.add(currentNode);
totalStrands+=currentNode.getStrands().size();
currentNode = currentNode.getChildWithGenome(genome.getId());
if (currentNode == null) {
break;
}
}
if(totalStrands<minStrandsToReturn){
result=getDataNodesForGenome(genome, level+1);
}
return result;


}


/**
* Set the minimal strands to return.
* @param minStrandsToReturn The minimal strands amount to return.
*/
public void setMinStrandsToReturn(int minStrandsToReturn) {
this.minStrandsToReturn = minStrandsToReturn;
}
}
Loading