Skip to content
Snippets Groups Projects
Commit 829eb44f authored by Christopher Schankula's avatar Christopher Schankula :earth_africa:
Browse files

first draft of BioTree building function (processTaxonId)

parent 8b18b933
No related branches found
No related tags found
No related merge requests found
......@@ -2,18 +2,36 @@ package biotree;
import java.io.IOException;
import org.json.simple.parser.ParseException;
import search.BST;
public class BioTree {
private static BST<Integer, TaxonNode> nodes;
private static int n;
public static void main(String[] args) {
init();
processRecord(125125);
processRecord(125125);
processRecord(125123);
processRecord(125122);
processRecord(125392);
processRecord(125391);
System.out.println(nodes.size());
System.out.println(nodes.get(123207));
Iterable<Integer> keys = nodes.keys();
for (Integer i: keys) {
System.out.println(nodes.get(i));
//System.out.println(String.format("%-26s %s", nodes.get(i).getName(), nodes.get(i).getTaxonType()));
}
printTree(nodes.get(2), 0);
}
/**
* Initialize species abstract object
*/
public static void init() {
nodes = new BST<Integer, TaxonNode>();
n = 0;
}
/**
......@@ -67,17 +85,34 @@ public class BioTree {
* @param taxonId
*/
private static void processTaxonId(int taxonId) {
TaxonNode[] newNodes = WormsAPI.idToClassification(taxonId);
for (int i = newNodes.length - 1; i > 0; i--) {
TaxonNode tx = newNodes[i];
TaxonNode[] newNodes = null;
try {
newNodes = WormsAPI.idToClassification(taxonId);
} catch (IOException | ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TaxonNode tx = nodes.get(newNodes[newNodes.length - 1].getTaxonId());
if (tx != null) {
tx.incCount();
} else {
newNodes[newNodes.length - 1].incCount();
}
for (int i = newNodes.length - 1; i >= 0; i--) {
tx = newNodes[i];
TaxonNode result = nodes.get(tx.getTaxonId());
TaxonNode parent = nodes.get(newNodes[i - 1].getTaxonId());
if (parent == null) parent = newNodes[i - 1];
TaxonNode parent = null;
if (i > 0) {
parent = nodes.get(newNodes[i - 1].getTaxonId());
if (parent == null) parent = newNodes[i - 1];
}
if (result == null) { //if node is not found, add it
nodes.put(tx.getTaxonId(), tx);
tx.setParent(parent);
parent.addChild(tx);
if (parent != null) parent.addChild(tx);
} else
break; //stop loop if this node already exists in the tree
}
}
......@@ -91,4 +126,11 @@ public class BioTree {
public static TaxonNode getTaxonRecord(int taxonId) {
return nodes.get(taxonId);
}
public static void printTree(TaxonNode tx, int level) {
String padd = new String(new char[level * 4]).replace('\0', ' ');
System.out.format(padd + "%s %d\n", tx.getName(), tx.getCount());
for (TaxonNode tx2: tx.getChildren())
printTree(tx2, level + 1);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment