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
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
/**
* Class for representing a node in the phylogenetic tree.
*/
public class BaseTreeNode implements JsonSerializable, TreeNode {
private String id;
public class BaseTreeNode implements TreeNode {
private static int STATIC_ID = 1;
private int id;
private String name;
private double weight;
private List<TreeNode> children;
private TreeNode parent;
Expand All @@ -22,22 +24,23 @@ public BaseTreeNode() {

/**
* Create a Tree Node Object.
* @param id id of the tree node.
* @param name name of the tree node.
* @param weight weight of the tree node.
*/
public BaseTreeNode(String id, double weight) {
this(id, weight, new ArrayList<>(), null);
public BaseTreeNode(String name, double weight) {
this(name, weight, new ArrayList<>(), null);
}

/**
* Create a BaseTreeNode object.
* @param id id of the treenode.
* @param name name of the treenode.
* @param weight weight of the tree node.
* @param children list of children tree nodes.
* @param parent parent tree node of this tree node.
*/
public BaseTreeNode(String id, double weight, List<TreeNode> children, TreeNode parent) {
this.id = id;
public BaseTreeNode(String name, double weight, List<TreeNode> children, TreeNode parent) {
this.id = STATIC_ID++;
this.name = name;
this.weight = weight;
this.children = children;
this.parent = parent;
Expand All @@ -60,11 +63,21 @@ public void setChildren(List<TreeNode> children) {
}

/**
* Set the id of the tree node.
* @param id value of the id.
* Get the id of the tree node.
*
* @return id of the tree node
*/
public void setId(String id) {
this.id = id;
@Override
public int getId() {
return id;
}

/**
* Set the name of the tree node.
* @param name value of the name.
*/
public void setName(String name) {
this.name = name;
}

/**
Expand All @@ -76,11 +89,11 @@ public void setWeight(double weight) {
}

/**
* Get id of tree node.
* @return id of tree.
* Get name of tree node.
* @return name of tree.
*/
public String getId() {
return id;
public String getName() {
return name;
}

/**
Expand Down Expand Up @@ -113,7 +126,7 @@ public void setParent(TreeNode parent) {
*/
@Override
public String toString() {
return "[" + this.getId() + " " + this.getWeight()
return "[" + this.getName() + " " + this.getWeight()
+ " {" + children.stream()
.map(Object::toString)
.collect(joining(", ")) + "}]";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
package io.github.programminglife2016.pl1_2016.parser;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
* Data is sent by the server in JSON format. Classes extending this method can convert their
* representation to JSON.
*/
public interface JsonSerializable {
/**
* Convert the representation to JSON.
*
* @return JSON representation of this object.
*/
default String toJson() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(NodeList.class, new NodeCollectionSerializer())
.registerTypeAdapter(NodeMap.class, new NodeCollectionSerializer()).create();
return gson.toJson(this);
}
String toJson();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.github.programminglife2016.pl1_2016.parser;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -62,4 +65,10 @@ public boolean containsKey(Object id) {
public Collection<Node> getNodes() {
return Arrays.asList(array).stream().filter(x -> x != null).collect(Collectors.toList());
}

@Override
public String toJson() {
Gson gson = new GsonBuilder().registerTypeAdapter(NodeList.class, new NodeCollectionSerializer()).create();
return gson.toJson(this);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.github.programminglife2016.pl1_2016.parser;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.Collection;
import java.util.HashMap;

Expand All @@ -24,4 +27,10 @@ public NodeMap(int initialCapacity) {
public Collection<Node> getNodes() {
return values();
}

@Override
public String toJson() {
Gson gson = new GsonBuilder().registerTypeAdapter(NodeMap.class, new NodeCollectionSerializer()).create();
return gson.toJson(this);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package io.github.programminglife2016.pl1_2016.parser;

import io.github.programminglife2016.pl1_2016.Launcher;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;


Expand All @@ -16,8 +12,7 @@ public class PhyloGeneticTreeParser implements Parser {
@Override
public JsonSerializable parse(InputStream inputStream) {
String s = inputStreamToString(inputStream);
Collection<TreeNode> nodes = parseTokensFromString(s);
return null;
return parseTokensFromString(s);
}

private String inputStreamToString(InputStream inputStream) {
Expand All @@ -32,9 +27,9 @@ private String inputStreamToString(InputStream inputStream) {
* @param s string representin the tree.
* @return root of tree
*/
public Collection<TreeNode> parseTokensFromString(String s) {
public TreeNodeCollection parseTokensFromString(String s) {
StringTokenizer tokenizer = new StringTokenizer(s, "(:,);", true);
Collection<TreeNode> nodes = construct(tokenizer);
TreeNodeCollection nodes = construct(tokenizer);
return nodes;
}

Expand All @@ -43,10 +38,11 @@ public Collection<TreeNode> parseTokensFromString(String s) {
* @param tokenizer tokenizer with the contents of the .nwk file.
* @return parsed Tree Node object.
*/
public Collection<TreeNode> construct(StringTokenizer tokenizer) {
Collection<TreeNode> nodes = new ArrayList<TreeNode>();
public TreeNodeCollection construct(StringTokenizer tokenizer) {
TreeNodeCollection nodes = new TreeNodeList();
TreeNode root = new BaseTreeNode();
nodes.add(root);
nodes.setRoot(root);
TreeNode current = root;
while (tokenizer.hasMoreTokens()) {
String currentToken = tokenizer.nextToken();
Expand Down Expand Up @@ -76,7 +72,7 @@ public Collection<TreeNode> construct(StringTokenizer tokenizer) {
case ";":
break;
default:
current.setId(currentToken);
current.setName(currentToken);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
* Created by ravishivam on 6-5-16.
*/
public interface TreeNode {
/**
* Get the id of the tree node.
*
* @return id of the tree node
*/
int getId();

/**
* Set the id of the tree node.
* @param id value of the id.
* Set the name of the tree node.
* @param name value of the name.
*/
void setId(String id);
void setName(String name);

/**
* Set the list of children tree nodes.
Expand All @@ -24,7 +30,7 @@ public interface TreeNode {
* Get id of tree node.
* @return id of tree.
*/
String getId();
String getName();

/**
* Return weight of this node.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.programminglife2016.pl1_2016.parser;

import java.util.Collection;

public interface TreeNodeCollection extends Collection<TreeNode>, JsonSerializable {
TreeNode getRoot();
void setRoot(TreeNode root);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.github.programminglife2016.pl1_2016.parser;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;

public class TreeNodeCollectionSerializer implements JsonSerializer<TreeNodeCollection> {
@Override
public JsonElement serialize(TreeNodeCollection treeNodeCollection, Type type, JsonSerializationContext jsonSerializationContext) {
JsonObject jsonObject = new JsonObject();
JsonArray treeNodes = new JsonArray();
for (TreeNode treeNode : treeNodeCollection) {
JsonObject treeNodeObject = new JsonObject();
treeNodeObject.add("id", new JsonPrimitive(treeNode.getId()));
treeNodeObject.add("name", new JsonPrimitive(treeNode.getName()));
JsonArray children = new JsonArray();
treeNode.getChildren().stream().map(TreeNode::getName).forEach(children::add);
treeNodeObject.add("children", children);
if (treeNode.getParent() != null) {
treeNodeObject.add("parent", new JsonPrimitive(treeNode.getParent().getId()));
} else {
treeNodeObject.add("parent", new JsonPrimitive("null"));
}
treeNodeObject.add("weight", new JsonPrimitive(treeNode.getWeight()));
treeNodes.add(treeNodeObject);
}
jsonObject.add("treeNodes", treeNodes);
jsonObject.add("root", new JsonPrimitive(treeNodeCollection.getRoot().getName()));
return jsonObject;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.programminglife2016.pl1_2016.parser;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;

public class TreeNodeList extends ArrayList<TreeNode> implements TreeNodeCollection {
private TreeNode root;

@Override
public String toJson() {
Gson gson = new GsonBuilder().registerTypeAdapter(TreeNodeList.class, new TreeNodeCollectionSerializer()).create();
return gson.toJson(this);
}

@Override
public TreeNode getRoot() {
return root;
}

@Override
public void setRoot(TreeNode root) {
this.root = root;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public void setUp() {
public void testTreeWithSingleNode() {
PhyloGeneticTreeParser parser = new PhyloGeneticTreeParser();
String s = "(B:6.0,A:5.0);";
TreeNode node = parser.parseTokensFromString(s);
assertEquals("B", node.getId());
TreeNodeCollection node = parser.parseTokensFromString(s);
assertEquals("B", node.getRoot().getChildren().get(0).getName());
}

/**
Expand All @@ -37,8 +37,8 @@ public void testTreeWithSingleNode() {
public void testTreeWithOneNestedLevel() {
PhyloGeneticTreeParser parser = new PhyloGeneticTreeParser();
String s = "(B:6.0,(A:5.0,(Z:9.0,T:10):3.0,E:4.0):5.0,D:11.0);";
TreeNode node = parser.parseTokensFromString(s);
assertEquals("B", node.getId());
TreeNodeCollection node = parser.parseTokensFromString(s);
assertEquals("B", node.getRoot().getChildren().get(0).getName());
}

/**
Expand Down