Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -85,7 +85,7 @@ public Collection<GraphNode> getRootNodes() {
* @param graphNodes a list of graphnodes for which the edges need to be set.
* @param newBubbles a list of bubbles which already have correct edges.
*/
public void pruneNodes(List<GraphNode> graphNodes, ArrayList<Bubble> newBubbles) {
public void pruneNodes(HashSet<GraphNode> graphNodes, ArrayList<Bubble> newBubbles) {
pruneBubbles(newBubbles);

Iterator<GraphNode> iterator = graphNodes.iterator();
Expand Down Expand Up @@ -120,7 +120,7 @@ private void pruneBubbles(ArrayList<Bubble> newBubbles) {
}

private Collection<GraphNode> pruneLinks(Collection<GraphNode> links,
List<GraphNode> graphNodes) {
HashSet<GraphNode> graphNodes) {
Collection<GraphNode> prunedLinks = new ArrayList<>();
links.forEach(link -> {
if (graphNodes.contains(link)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,67 @@

import nl.tudelft.pl2016gr2.model.graph.nodes.GraphNode;

import java.util.Collections;
import java.util.List;
import java.util.Queue;
import java.util.Set;

/**
* Class with helper methods for the filter bubbles algorithm.
*
*
* @author Casper
*
*/
public class FilterHelpers {
private FilterHelpers(){

private FilterHelpers() {
}

/**
* Adds an int to a tovisit queue if it hasn't been visited yet and is not already in
* the queue.
* Adds an int to a tovisit queue if it hasn't been visited yet and is not already in the queue.
*
* @param visitor the int to visit
* @param toVisit the current queue
* @param visited the set of visited ints
*/
public static void addToVisit(GraphNode visitor, Queue<GraphNode> toVisit,
public static void addToVisit(GraphNode visitor, Queue<GraphNode> toVisit,
Set<GraphNode> visited) {
if (!visited.contains(visitor) && !toVisit.contains(visitor)) {
toVisit.add(visitor);
}
}

/**
* This method checks if a node is shared among the leaves of a phylo genetic tree node.
* What that means is that the node either contains all the genomes of the leaves of the
* phylo node, or that it has some genomes which are in other branches of the phylo tree.
*
* @param node the node to check
* This method checks if a node is shared among the leaves of a phylo genetic tree node. What that
* means is that the nodes either contains all the genomes of the leaves of the phylo node, or
* that it has some genomes which are in other branches of the phylo tree.
*
* @param node the node to check
* @param leaves the labels of the leaves of the phylo node
* @return true if the node is shared
*/
public static boolean isShared(GraphNode node, List<Integer> leaves) {
if (node.getGenomes().containsAll(leaves)) {
sortIfUnsorted(leaves);
if (node.containsAllGenomes(leaves)) {
return true;
}

for (Integer genome : node.getGenomes()) {
if (genome != 0 && !leaves.contains(genome)) {
if (genome != 0 && Collections.binarySearch(leaves, genome, null) < 0) {
return true;
}
}
return false;
}

private static void sortIfUnsorted(List<Integer> genomes) {
int previous = Integer.MIN_VALUE;
for (int i = 0; i < genomes.size(); i++) {
if (genomes.get(i) < previous) {
genomes.sort(null);
return;
}
previous = genomes.get(i);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private void mapAnnotation(GraphNode firstNode, Annotation annotation, int nodeS
public GraphNode findBase(Integer genome, int baseOffset) {
Iterator<GraphNode> it = orderedGraph.iterator();
GraphNode firstNode = it.next();
while (it.hasNext() && !firstNode.getGenomes().contains(genome)) {
while (it.hasNext() && !firstNode.containsGenome(genome)) {
firstNode = it.next();
}
return findBase(genome, baseOffset, 0, firstNode).right;
Expand All @@ -134,7 +134,7 @@ private GraphNode getNextNode(GraphNode curGraphNode, Integer genome) {
GraphNode nextNode = null;
int nextLevel = Integer.MAX_VALUE;
for (GraphNode outEdge : curGraphNode.getOutEdges()) {
if (outEdge.getLevel() < nextLevel && outEdge.getGenomes().contains(genome)) {
if (outEdge.getLevel() < nextLevel && outEdge.containsGenome(genome)) {
nextLevel = outEdge.getLevel();
nextNode = outEdge;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ public ArrayList<GraphNode> filter() {
}

ArrayList<Bubble> newBubbles = new ArrayList<>();
ArrayList<GraphNode> graphNodes = new ArrayList<>(findBubbles(startNodes, null, newBubbles));
HashSet<GraphNode> graphNodes = findBubbles(startNodes, null, newBubbles);
graphNodes.addAll(rootNodes);
pruneNodes(graphNodes, newBubbles);

Collections.sort(graphNodes, (GraphNode first, GraphNode second) -> {
ArrayList<GraphNode> sortedNodes = new ArrayList<>(graphNodes);
Collections.sort(sortedNodes, (GraphNode first, GraphNode second) -> {
return first.getLevel() - second.getLevel();
});
return graphNodes;
return sortedNodes;
}

@Override
Expand All @@ -75,9 +76,9 @@ public List<GraphNode> zoomIn(Bubble bubble) {
* @param newBubbles a list which contains all the newly made bubbles.
* @return A list of new graphnodes in the specified interval after they have been bubbled.
*/
protected Collection<GraphNode> findBubbles(Collection<GraphNode> startNodes, GraphNode end,
protected HashSet<GraphNode> findBubbles(Collection<GraphNode> startNodes, GraphNode end,
ArrayList<Bubble> newBubbles) {
Set<GraphNode> poppedNodes = new HashSet<>();
HashSet<GraphNode> poppedNodes = new HashSet<>();
Set<GraphNode> visited = new HashSet<>();
Queue<GraphNode> toVisit = new LinkedList<>();

Expand All @@ -99,8 +100,8 @@ protected Collection<GraphNode> findBubbles(Collection<GraphNode> startNodes, Gr
* @param newBubbles : list of new bubbles.
* @return a list of new nodes.
*/
private Collection<GraphNode> visitNodes(Queue<GraphNode> toVisit, Set<GraphNode> visited,
GraphNode end, Set<GraphNode> poppedNodes, ArrayList<Bubble> newBubbles) {
private HashSet<GraphNode> visitNodes(Queue<GraphNode> toVisit, Set<GraphNode> visited,
GraphNode end, HashSet<GraphNode> poppedNodes, ArrayList<Bubble> newBubbles) {
while (!toVisit.isEmpty()) {
GraphNode start = toVisit.poll();
if (end != null && start.getLevel() >= end.getLevel()) {
Expand Down Expand Up @@ -134,13 +135,12 @@ private Collection<GraphNode> visitNodes(Queue<GraphNode> toVisit, Set<GraphNode
* @return : a bubble (or null).
*/
private Bubble createBubble(GraphNode start, GraphNode currentEnd) {
Set<Integer> genomes = new HashSet<>(start.getGenomes());
Set<GraphNode> visited = new HashSet<>();
Queue<GraphNode> toVisit = new LinkedList<>();
toVisit.addAll(getOriginalOutEdges().get(start.getId()));
Set<GraphNode> nestedNodes = new HashSet<>();

Bubble bubble = makeBubble(toVisit, visited, start, currentEnd, nestedNodes, genomes);
Bubble bubble = makeBubble(toVisit, visited, start, currentEnd, nestedNodes);
if (bubble != null) {
for (GraphNode node : nestedNodes) {
bubble.addChild(node);
Expand All @@ -158,25 +158,23 @@ private Bubble createBubble(GraphNode start, GraphNode currentEnd) {
* @param start : start of bubble.
* @param endNode : end of the bubble should be before this node.
* @param nestedNodes : nestednodes of the bubble.
* @param genomes : genomes of the bubble.
* @return : a bubble (or null).
*/
private Bubble makeBubble(Queue<GraphNode> toVisit, Set<GraphNode> visited, GraphNode start,
GraphNode endNode, Set<GraphNode> nestedNodes, Set<Integer> genomes) {
GraphNode endNode, Set<GraphNode> nestedNodes) {
GraphBubble bubble = null;
while (!toVisit.isEmpty()) {
GraphNode next = toVisit.poll();
visited.add(next);
if (endNode != null && next.getLevel() >= endNode.getLevel()) {
continue;
}
Set<Integer> nextGenomes = new HashSet<>(next.getGenomes());
if (nextGenomes.size() == genomes.size() && nextGenomes.containsAll(genomes)) {
if (next.hasSameGenomes(start.getGenomes())) {
endNode = next;
bubble = new GraphBubble(mutationId, this,
Collections.singletonList(start), Collections.singletonList(endNode));
mutationId--;
} else if (nextGenomes.size() < genomes.size()) {
} else if (next.getGenomeSize() < start.getGenomeSize()) {
nestedNodes.add(next);
for (GraphNode out : getOriginalOutEdges().get(next.getId())) {
if (endNode == null || out.getLevel() < endNode.getLevel()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,18 @@ public List<GraphNode> zoom(Bubble bubble) {
end.setInEdges(new HashSet<>());

ArrayList<Bubble> newBubbles = new ArrayList<>();
ArrayList<GraphNode> graphNodes = new ArrayList<>(
filter.findBubbles(Collections.singletonList(startNode), end, newBubbles));
HashSet<GraphNode> graphNodes =
filter.findBubbles(Collections.singletonList(startNode), end, newBubbles);
graphNodes.add(end);
pruneStart(bubble.getInEdges(), originalInEdges, originalOutEdges, bubble.getId());
pruneEnd(bubble.getOutEdges(), originalOutEdges, originalInEdges, bubble.getId());
filter.pruneNodes(graphNodes, newBubbles);

return alignNodes(graphNodes, bubble);
ArrayList<GraphNode> sortedNodes = new ArrayList<>(graphNodes);
Collections.sort(sortedNodes, (GraphNode first, GraphNode second) -> {
return first.getLevel() - second.getLevel();
});

return alignNodes(sortedNodes, bubble);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
public class MutationBubbleAlgorithms {

private static int bubbleCount = 0;
private static int bubbleCount = Integer.MIN_VALUE;

private MutationBubbleAlgorithms() {
}
Expand All @@ -36,11 +36,13 @@ private MutationBubbleAlgorithms() {
* graph.
*/
public static ArrayList<GraphNode> makeBubbels(ArrayList<GraphNode> orderedNodes) {
if (bubbleCount > Integer.MIN_VALUE / 4 * 3) {
bubbleCount = Integer.MIN_VALUE;
}
return initStraightInDelPoint(orderedNodes);
}

private static ArrayList<GraphNode> initStraightInDelPoint(ArrayList<GraphNode> orderedGraph) {
bubbleCount = Integer.MIN_VALUE;
Settings settings = Settings.getInstance();
List<Settings.BubbleAlgorithms> algorithms = settings.getAlgorithms();

Expand Down Expand Up @@ -113,7 +115,7 @@ private static PointMutationBubble createPointMutationBubble(Collection<GraphNod
*/
private static PointMutationBubble createPointMutationBubble(GraphNode firstChild,
GraphNode secondChild) {
ArrayList<GraphNode> nestedNodes = new ArrayList<>();
HashSet<GraphNode> nestedNodes = new HashSet<>();
nestedNodes.add(firstChild);
nestedNodes.add(secondChild);

Expand Down Expand Up @@ -202,8 +204,9 @@ private static IndelBubble createIndelBubble(List<GraphNode> nestedNodes, GraphN
ArrayList<GraphNode> outEdges = new ArrayList<>();
outEdges.add(endNode);

IndelBubble bubble = new IndelBubble(bubbleCount++, inEdges, outEdges, nestedNodes,
VerticalAligner.INDEL_ALIGNER);
IndelBubble bubble = new IndelBubble(bubbleCount++, inEdges, outEdges,
new HashSet<>(nestedNodes), VerticalAligner.INDEL_ALIGNER,
startNode.getOutEdges().iterator().next(), endNode.getInEdges().iterator().next());

startNode.removeOutEdge(nestedNodes.get(0));
startNode.removeOutEdge(endNode);
Expand Down Expand Up @@ -257,10 +260,11 @@ private static ArrayList<GraphNode> filterStraightSequence(List<GraphNode> order
* @param visited The visited list to avoid iterating over nodes multiple times
* @return The <code>startNode</code> if no sequence was made, or the Bubble for the sequence
*/
@SuppressWarnings("checkstyle:MethodLength")
private static Collection<GraphNode> detectStraightSequence(GraphNode startNode,
Set<GraphNode> visited) {
boolean overlap = startNode.getGuiData().overlapping;
List<GraphNode> nestedNodes = new ArrayList<>();
HashSet<GraphNode> nestedNodes = new HashSet<>();
nestedNodes.add(startNode);
GraphNode current = startNode;
while (current.getOutEdges().size() == 1) {
Expand All @@ -280,7 +284,8 @@ private static Collection<GraphNode> detectStraightSequence(GraphNode startNode,
nestedNodes.remove(current);
StraightSequenceBubble bubble = new StraightSequenceBubble(bubbleCount++,
Collections.singletonList(startNode), Collections.singletonList(current), nestedNodes,
VerticalAligner.STRAIGHT_SEQUENCE_ALIGNER);
VerticalAligner.STRAIGHT_SEQUENCE_ALIGNER, startNode.getOutEdges().iterator().next(),
current.getInEdges().iterator().next());
return getNewNodes(startNode, current, bubble);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/
public class PhyloBubbleFilter extends AbstractBubbleFilter {

private int mutationId = -1;
private static int mutationId = -1;

/**
* Creates an instance of this class.
Expand All @@ -36,6 +36,9 @@ public class PhyloBubbleFilter extends AbstractBubbleFilter {
*/
public PhyloBubbleFilter(Collection<GraphNode> orderedNodes) {
super(orderedNodes);
if (mutationId < Integer.MIN_VALUE / 4) {
mutationId = -1;
}
}

/**
Expand Down Expand Up @@ -63,16 +66,16 @@ public List<GraphNode> zoomIn(Bubble bubble) {
public ArrayList<GraphNode> filter(IPhylogeneticTreeRoot<?> treeRoot,
Collection<Integer> genomes) {
IPhylogeneticTreeRoot<?> newRoot = new TreeBuilder(treeRoot, genomes).getTree();
Set<GraphNode> graphNodes = new HashSet<>();
HashSet<GraphNode> graphNodes = new HashSet<>();
ArrayList<Bubble> newBubbles = new ArrayList<>();
debubble(graphNodes, newRoot, newBubbles);
ArrayList<GraphNode> poppedNodes = new ArrayList<>(graphNodes);
pruneNodes(poppedNodes, newBubbles);
pruneNodes(graphNodes, newBubbles);

Collections.sort(poppedNodes, (GraphNode first, GraphNode second) -> {
ArrayList<GraphNode> sortedNodes = new ArrayList<>(graphNodes);
Collections.sort(sortedNodes, (GraphNode first, GraphNode second) -> {
return first.getLevel() - second.getLevel();
});
return poppedNodes;
return sortedNodes;
}

private List<Bubble> debubble(Set<GraphNode> graphNodes, IPhylogeneticTreeNode<?> treeNode,
Expand Down Expand Up @@ -204,10 +207,9 @@ private List<GraphNode> calcNodeOutlinks(Collection<GraphNode> outEdges,
if (bubble != null && !bubble.hasChild(outlink)) {
continue;
}
ArrayList<Integer> genomes = new ArrayList<>(outlink.getGenomes());

for (Integer leaf : leaves) {
if (genomes.contains(leaf)) {
if (outlink.containsGenome(leaf)) {
curNodeOutlinks.add(outlink);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -45,15 +46,19 @@ public List<GraphNode> zoom(Bubble bubble) {
IPhylogeneticTreeNode<?> curTreeNode = getTreeNode(bubble);
IPhylogeneticTreeNode<?> childOne = curTreeNode.getChild(0);
IPhylogeneticTreeNode<?> childTwo = curTreeNode.getChild(1);
Set<GraphNode> poppedNodes = new HashSet<>();
HashSet<GraphNode> poppedNodes = new HashSet<>();
ArrayList<Bubble> newBubbles = new ArrayList<>();
debubble(poppedNodes, childOne, bubble, newBubbles);
debubble(poppedNodes, childTwo, bubble, newBubbles);
pruneStart(bubble.getInEdges(), originalInEdges, originalOutEdges, bubble.getId());
pruneEnd(bubble.getOutEdges(), originalOutEdges, originalInEdges, bubble.getId());
ArrayList<GraphNode> graphNodes = new ArrayList<>(poppedNodes);
filter.pruneNodes(graphNodes, newBubbles);
return alignNodes(graphNodes, bubble);
filter.pruneNodes(poppedNodes, newBubbles);

ArrayList<GraphNode> sortedNodes = new ArrayList<>(poppedNodes);
Collections.sort(sortedNodes, (GraphNode first, GraphNode second) -> {
return first.getLevel() - second.getLevel();
});
return alignNodes(sortedNodes, bubble);
}

private IPhylogeneticTreeNode<?> getTreeNode(Bubble bubble) {
Expand Down
Loading