diff --git a/tSpace/CherryTree.java b/tSpace/CherryTree.java deleted file mode 100644 index 48fc3af..0000000 --- a/tSpace/CherryTree.java +++ /dev/null @@ -1,145 +0,0 @@ -package tTree; - -import java.util.*; - -public class CherryTree{ - ArrayList tree; - Map map; - - public CherryTree(ArrayList tree, Map map){ - this.tree = tree; - this.map = map; - } - public void setMap(Map map){ - this.map = map; - } - public Map getMap(){ - return map; - } - public void setTree(ArrayList tree){ - this.tree = tree; - } - public ArrayList getTree(){ - return tree; - - } - public String toString(){ - StringBuilder sb = new StringBuilder(); - sb.append(tree.toString()); - sb.append(map); - return sb.toString(); - } - //Simplifies both trees - public ArrayList getSimplifiedTrees(CherryTree cherryTree2){ - ArrayList listOfCherryTrees = new ArrayList(); - CherryTree cherryTree1 = this; - System.out.println("Initial CherryTree1: " + cherryTree1); - System.out.println("Initial CherryTree2: " + cherryTree2); - boolean cladeHasBeenSimplified = true; - while(cladeHasBeenSimplified){ - while(canBeSimplified(cherryTree1, cherryTree2)){ - ArrayList commonCherries = cherryTree1.getCommonCherries(cherryTree1, cherryTree2); - cherryTree1 = getSimplifiedTree(cherryTree1, commonCherries); - cherryTree2 = getSimplifiedTree(cherryTree2, commonCherries); - System.out.println(); - } - RoamingTaxaLocalisation roamer = new RoamingTaxaLocalisation(); - roamer = roamer.getRoamer(cherryTree1, cherryTree2); - System.out.println(cherryTree1); - System.out.println(cherryTree2); - cladeHasBeenSimplified = roamer.getHasRoamer(); - if(cladeHasBeenSimplified){ - System.out.println("Roaming Taxon is one of: " + roamer.getLocalisedRoamer()); - } - - } - - listOfCherryTrees.add(cherryTree1); - listOfCherryTrees.add(cherryTree2); - return listOfCherryTrees; - } - - CherryTree getSimplifiedTree(CherryTree cherryTree, ArrayList commonCherries){ - for(tNode cherry: commonCherries){ - for(tNode treeNode: cherryTree.getTree()){ - if(cherry.getTaxa().equals(treeNode.getTaxa())){ - //need some kind of holder/counter for the placeholder in the map. - ArrayList symbol = new ArrayList(); - String holder = "" + cherryTree.getMap().size(); - symbol.add(holder); - cherryTree.getMap().put(symbol.get(0), treeNode); - cherryTree.setTree(simplifyTree(cherryTree, cherry, symbol)); - //replace every instance of the cherry in cherryTree with the symbol; - } - } - } - return cherryTree; - } - - private ArrayList simplifyTree(CherryTree cherryTree, tNode cherry, ArrayList symbol){ - ArrayList cherryTaxa = cherry.getTaxa(); - ArrayList replacementTree = new ArrayList(); - for(tNode n: cherryTree.getTree()){ - ArrayList nodeTaxa = new ArrayList(); - nodeTaxa.addAll(n.getTaxa()); - if (n.getTaxa().size() == 1 /* && - (n.getTaxa().contains(cherryTaxa.get(0)) || n.getTaxa().contains(cherryTaxa.get(1)))*/){ - //don't do anything - } else if (n.getTaxa().containsAll(cherryTaxa)){ - nodeTaxa.removeAll(cherryTaxa); - nodeTaxa.addAll(symbol); - replacementTree.add(new tNode(nodeTaxa, n.getHeight())); - }else{ - replacementTree.add(n); - } - } - return replacementTree; - } - - - private ArrayList getCommonCherries(CherryTree tree1, CherryTree tree2){ - ArrayList commonCherries = new ArrayList(); - ArrayList tree1Nodes = getNTaxaNodes(tree1,2); - ArrayList tree2Nodes = getNTaxaNodes(tree2,2); - for(tNode n1: tree1Nodes){ - ArrayList taxa1 = n1.getTaxa(); - for(tNode n2: tree2Nodes){ - ArrayList taxa2 = n2.getTaxa(); - if(taxa1.equals(taxa2)){ - commonCherries.add(new tNode(taxa1, 0)); - } - } - } - return commonCherries; - } - - private boolean canBeSimplified(CherryTree tree1, CherryTree tree2){ - ArrayList tree1Nodes = getNTaxaNodes(tree1,2); - ArrayList tree2Nodes = getNTaxaNodes(tree2,2); - - boolean simple = false; - for(int i = 0; i < tree1Nodes.size(); i++){ - ArrayList taxa1 = tree1Nodes.get(i).getTaxa(); - for(int j = 0; j < tree2Nodes.size(); j++){ - ArrayList taxa2 = tree2Nodes.get(j).getTaxa(); - if(taxa1.equals(taxa2)){ - simple = true; - } - } - } - return simple; - } - ArrayList getNTaxaNodes(CherryTree cherryTree, int n){ - ArrayList treeNodes = new ArrayList(); - ArrayList tree = cherryTree.getTree(); - for(int i = 0; i < tree.size(); i++){ - if(tree.get(i).getTaxa().size() == n){ - treeNodes.add(tree.get(i)); - } - } - return treeNodes; - } - - - -} diff --git a/tSpace/RoamingTaxaLocalisation.java b/tSpace/RoamingTaxaLocalisation.java deleted file mode 100644 index 9348628..0000000 --- a/tSpace/RoamingTaxaLocalisation.java +++ /dev/null @@ -1,407 +0,0 @@ -package tTree; -import java.util.*; - -public class RoamingTaxaLocalisation { - boolean hasRoamer; - Set localisedRoamer; - - public RoamingTaxaLocalisation(){ - hasRoamer = false; - localisedRoamer = new HashSet(); - } - public RoamingTaxaLocalisation(boolean hasRoamer, Set localisedRoamer){ - this.hasRoamer = hasRoamer; - this.localisedRoamer = localisedRoamer; - } - public void setHasRoamer(boolean hasRoamer){ - this.hasRoamer = hasRoamer; - } - public boolean getHasRoamer(){ - return hasRoamer; - } - public void setLocalisedRoamer(Set localisedRoamer){ - this.localisedRoamer = localisedRoamer; - } - public Set getLocalisedRoamer(){ - return localisedRoamer; - } - - public RoamingTaxaLocalisation getRoamer(CherryTree tree1, CherryTree tree2){ - RoamingTaxaLocalisation roamer = new RoamingTaxaLocalisation(); - //go through tree and return an ArrayList of the smallest node. - ArrayList prevClades = new ArrayList(); - boolean roamerIsNotFound = true; - ArrayList commonClade = new ArrayList(); - //add a while loop until either a clade is found or we reach some terminal point (tree root) - while(roamerIsNotFound){ - - commonClade = findSmallestCommonClade(tree1, tree2, prevClades); - System.out.println(prevClades); - System.out.println(commonClade); - //with this clade: if size = 3 - if(commonClade.isEmpty()){ - - break; - } - - if(commonClade.size() == 3 ){ - //deal with a size 3 --> can definitely be simplied. Roamer is always common between the two. - roamer.setHasRoamer(true); - ArrayList t1Nodes = tree1.getNTaxaNodes(tree1, 2); - ArrayList t2Nodes = tree2.getNTaxaNodes(tree2, 2); - ArrayList t1Taxa = new ArrayList(); - ArrayList t2Taxa = new ArrayList(); - for(tNode n1: t1Nodes){ - if(commonClade.containsAll(n1.getTaxa())){ - t1Taxa.addAll(n1.getTaxa()); - } - } - for(tNode n2: t2Nodes){ - if(commonClade.containsAll(n2.getTaxa())){ - t2Taxa.addAll(n2.getTaxa()); - } - } - String roamingTaxon = ""; - for(String s1: t1Taxa){ - for(String s2: t2Taxa){ - if(s1.equals(s2)){ - roamingTaxon = s1; - } - } - } - Set roaming = new HashSet(); - roaming.add(roamingTaxon); - roamer.setLocalisedRoamer(roaming); - - }else{ - //compare subtrees/clades: down all lines where subclades>1 - //send clade and trees away, and get back a set containing the potential 3 taxa or an empty set. - roamer.setLocalisedRoamer(findRoamingTaxon(tree1.getTree(), tree2.getTree(), commonClade, commonClade)); - System.out.println(roamer.getLocalisedRoamer()); - } - if(roamer.getLocalisedRoamer() != null){ - roamerIsNotFound = false; - } - } - - - if(roamer.getLocalisedRoamer() !=null && !roamer.getLocalisedRoamer().isEmpty()){ - roamer.setHasRoamer(true); - ArrayList commonClade2 = new ArrayList(); - commonClade2.addAll(commonClade); - simplifyClade(tree1, commonClade); - simplifyClade(tree2, commonClade2); - } - return roamer; - - } - - private ArrayList findSmallestCommonClade(CherryTree tree1, CherryTree tree2, ArrayList prevClades){ - ArrayList commonClade = new ArrayList(); - int counter = 3; - while(commonClade.isEmpty()){ - ArrayList tree1CurrentClades = tree1.getNTaxaNodes(tree1, counter); - ArrayList tree2CurrentClades = tree2.getNTaxaNodes(tree2, counter); - for(tNode n1: tree1CurrentClades){ - ArrayList t1Taxa = n1.getTaxa(); - for(tNode n2: tree2CurrentClades){ - ArrayList t2Taxa = n2.getTaxa(); - if (t1Taxa.containsAll(t2Taxa) && !isContained(t1Taxa, prevClades)){ - //then lets see if this clade can be simplified :), note if this clade can't be, then anything above it can't either... - commonClade.addAll(t1Taxa); - prevClades.add(n1); - } - } - } - counter++; - if (counter >= 10){ //this needs to be modified - break; - } - } - return commonClade; - } - private boolean isContained(ArrayList taxa, ArrayList prevClades){ - boolean isContained = false; - for(tNode n: prevClades){ - if(n.getTaxa().containsAll(taxa) && n.getTaxa().size() == taxa.size()){ - isContained = true; - } - } - return isContained; - } - - private Set findRoamingTaxon(ArrayList tree1, ArrayList tree2, - ArrayList commonClade1, ArrayList commonClade2){ - - System.out.println(tree1); - System.out.println(tree2); - System.out.println(commonClade1); - System.out.println(commonClade2); - if(((commonClade1.size() == 3 && commonClade2.size() ==2) || (commonClade1.size() == 2 && commonClade2.size()==3)) - && (commonClade1.containsAll(commonClade2) || commonClade2.containsAll(commonClade1))){ - Set roamingTaxa = new HashSet(); - roamingTaxa.addAll(commonClade1); - roamingTaxa.addAll(commonClade2); - System.out.println("ROAMING TAXON/3: " + roamingTaxa); - return roamingTaxa; - } - - if(commonClade1.size() == commonClade2.size() && commonClade1.size() == 2){ - Set roamingTaxa = new HashSet(); - roamingTaxa.addAll(commonClade1); - roamingTaxa.addAll(commonClade2); - System.out.println("ROAMING TAXON: " + roamingTaxa); - return roamingTaxa; - } - - - //find subClade which contains the commonClade and below. - ArrayList clade1 = getSubClade(tree1, commonClade1); - ArrayList clade2 = getSubClade(tree2, commonClade2); - System.out.println("CLADE1: " +clade1); - System.out.println("CLADE2: " + clade2); - //find next two subclades: get largest in clade1 and if commonClade-1 > largest --> find the other, else it's just one. - - - ArrayList tree1Clades = new ArrayList(); - ArrayList tree2Clades = new ArrayList(); - if(clade1.size() != 1){ - tree1Clades = getLargestSubClades(clade1); - }else{ - tree1Clades.addAll(clade1); - } - if(clade2.size() != 1){ - tree2Clades = getLargestSubClades(clade2); - }else{ - tree2Clades.addAll(clade2); - } - System.out.println("TreeClade1: " + tree1Clades); - System.out.println("TreeClade2: " + tree2Clades); - //compare the two groups of subClades: - int minClades = Math.min(tree1Clades.size(), tree2Clades.size()); - System.out.println("minClade: " + minClades); - int matches = 0; - - - - for(tNode n1: tree1Clades){ - boolean closeTo = false; - ArrayList s1 = n1.getTaxa(); - for(tNode n2: tree2Clades){ - ArrayList s2 = n2.getTaxa(); - if((s1.containsAll(s2) || s2.containsAll(s1)) || (s1.size() == s2.size() && s1.size() > 2 && hasOneDifference(s1,s2))){ - if(closeTo){ - //problem it matches two of the subClades --> >1 roamer. - matches = 3; //can only be 1 or 2 matches ever. - break; - }else{ - closeTo = true; - matches++; - } - } - } - } - if(tree1Clades.size() == 1 && tree2Clades.size() == 1 && - tree1Clades.get(0).getTaxa().size() == 2 && tree2Clades.get(0).getTaxa().size() == 2){ - if(hasOneDifference(tree1Clades.get(0).getTaxa(), tree2Clades.get(0).getTaxa())){ - matches = 1; - } - } - - - System.out.println("matches: " + matches); - if(matches != minClades){ - return null; //theres more than one roamer; - } - //else matches == minClades which is great. - //have two subclades which we want to go down until the end of time - if(tree1Clades.size() == tree2Clades.size() && tree1Clades.get(0).getTaxa().size() == 2){ - Set roamingTaxa = new HashSet(); - roamingTaxa.addAll(tree1Clades.get(0).getTaxa()); - roamingTaxa.addAll(tree2Clades.get(0).getTaxa()); - System.out.println("ROAMING TAXON: " + roamingTaxa); - return roamingTaxa; - } - - Set roamer1 = new HashSet(); - Set roamer2 = new HashSet(); - int num = 0; - - //match up the subclades: - for(tNode n1: tree1Clades){ - ArrayList s1 = n1.getTaxa(); - for(tNode n2: tree2Clades){ - if(num > minClades){ - break; - } - ArrayList s2 = n2.getTaxa(); - if(s1.containsAll(s2) || s2.containsAll(s1)){ - roamer1 = findRoamingTaxon(tree1, tree2, s1, s2); - num++; - } else if(hasOneDifference(s1, s2) && s1.size() == s2.size() && s1.size() > 2){ - if(differenceRemovedNextGen(s1, getNextGenTaxa(s1, s1.size()-1, clade1) , s2,getNextGenTaxa(s2, s2.size()-1, clade2) )){ - roamer1 = findRoamingTaxon(tree1, tree2, s1, s2); - num++; - } - } - } - } - - Set roamers = new HashSet(); - if (roamer1 == null || roamer2 == null){ - return null; - } - roamers.addAll(roamer1); - roamers.addAll(roamer2); - System.out.println("R1: " + roamer1); - System.out.println("R2: " + roamer2); - System.out.println("R: " + roamers); - - - return roamers; - } - - private ArrayList getNextGenTaxa(ArrayList current, int n, ArrayList tree){ - ArrayList taxaList = new ArrayList(); - CherryTree treeT = new CherryTree(tree, null); - ArrayList treeNodes = treeT.getNTaxaNodes(treeT, n); - - for(tNode node: treeNodes){ - if(current.containsAll(node.getTaxa())){ - taxaList.addAll(node.getTaxa()); - } - } - - - return taxaList; - } - private boolean differenceRemovedNextGen(ArrayList t1Root, ArrayList t1Next, - ArrayList t2Root, ArrayList t2Next){ - boolean differenceRemoved = false; - ArrayList blacklist = new ArrayList(); - - for(String s1: t1Root){ - if(!t2Root.contains(s1)){ - blacklist.add(s1); - } - } - for(String s2: t2Root){ - if(!t1Root.contains(s2)){ - blacklist.add(s2); - } - } - System.out.println("BlackList: " + blacklist); - //blacklist size must be 2 otherwise we wouldn't be here. - if(blacklist.size() != 2){ - return false; - } - int count = 0; - for(String s: blacklist){ - if(t1Next.contains(s)){ - count++; - } - if(t2Next.contains(s)){ - count++; - } - } - if(count == 1){ - differenceRemoved = true; - } - - return differenceRemoved; - } - - - private ArrayList getLargestSubClades(ArrayList clade){ - ArrayList subClades = new ArrayList(); - //find greatest, then find remainder. - clade = tNode.mergeSort(clade); - tNode cladeRoot = clade.get(clade.size()-1); - - tNode largest = clade.get(clade.size()-2); - subClades.add(largest); - if(largest.getTaxa().size()+1 != cladeRoot.getTaxa().size()){ - //add other clade - ArrayList taxa = new ArrayList(); - taxa.addAll(cladeRoot.getTaxa()); - taxa.removeAll(largest.getTaxa()); - //find subclade with the same taxa as taxa; - for(tNode n: clade){ - if(taxa.containsAll(n.getTaxa()) && taxa.size() == n.getTaxa().size()){ - subClades.add(n); - } - } - } - - return subClades; - } - private ArrayList getSubClade(ArrayList tree, ArrayList commonClade){ - ArrayList subClade = new ArrayList(); - for(tNode n: tree){ - ArrayList taxa = n.getTaxa(); - if(commonClade.containsAll(taxa)){ - subClade.add(n); - } - } - return subClade; - } - - - - private void simplifyClade(CherryTree cherryTree, ArrayList cladeRoot){ - //get list of all nodes in clade; - Iterator iter = cherryTree.getTree().iterator(); - while (iter.hasNext()){ - tNode n = iter.next(); - if(cladeRoot.containsAll(n.getTaxa()) && n.getTaxa().size() != 1){ - ArrayList nodes = new ArrayList(); - nodes.add(n); - cherryTree.getSimplifiedTree(cherryTree, nodes); - cladeRoot.removeAll(n.getTaxa()); - cladeRoot.add(""+(cherryTree.getMap().size()-1)); - iter = cherryTree.getTree().iterator(); - } - } - } - //n = size of desired list - //find a taxaList in the cherryTree which is of the right length and contains taxa; - private ArrayList getNextTaxaList(CherryTree tree, ArrayList taxa, int n){ - ArrayList taxaList = new ArrayList(); - ArrayList treeNodes = tree.getNTaxaNodes(tree, n); - for(tNode node: treeNodes){ - if(node.getTaxa().containsAll(taxa)){ - taxaList.addAll(node.getTaxa()); - } - } - if(taxaList.isEmpty()){ - taxaList.addAll(taxa); - } - return taxaList; - } - - - //return true, only if there is a single difference between the arrayLists. - private boolean hasOneDifference(ArrayList taxa1, ArrayList taxa2){ - int difference = Math.abs(taxa1.size() - taxa2.size()); - int minLength = Math.min(taxa1.size(), taxa2.size()); - if (difference >= 2){ - return false; //must be >1 difference - } - int matches = 0; - for(String s1: taxa1){ - for(String s2: taxa2){ - if (s1.equals(s2)){ - matches++; - } - } - } - boolean oneDifference = false; - if(taxa1.size() == taxa2.size() && matches == taxa1.size() - 1){ - oneDifference = true; - }else if(difference == 1 && matches == minLength){ - oneDifference = true; - } - return oneDifference; - } - -} diff --git a/tSpace/TreeFromNewick b/tSpace/TreeFromNewick deleted file mode 100644 index 4b65fc3..0000000 --- a/tSpace/TreeFromNewick +++ /dev/null @@ -1,111 +0,0 @@ -package tTree; - -import java.util.ArrayList; -import java.util.Stack; -//Takes newickformat string and converts it into a tree (ArrayList) -public class TreeFromNewick { - ArrayList tree; - - public TreeFromNewick(String nf){ - tree = useAStack(nf); - } - - public ArrayList getTree(){ - return tree; - } - - private ArrayList useAStack(String nf){ - Stack s = new Stack(); - Stack holder = new Stack(); - ArrayList tree = new ArrayList(); - for(int i = 0; i < nf.length(); i++){ - String next = nf.charAt(i) + ""; - if(!next.matches("[(),:]")){ //then it's a number/string - int minIndex = findMinIndex(nf, i); - //System.out.println(minIndex); - next = nf.substring(i, minIndex); - s.push(next); - i = minIndex-1; - } - else if(next.equals(")")){ - //create a node; - int numCloseBrackets = 1; - holder.push(")"); - while(numCloseBrackets != 0){ - String piece = s.pop(); - if(piece.equals("(")){ - numCloseBrackets--; - } else if(piece.equals(")")){ - numCloseBrackets++; - } - holder.push(piece); - } - //go through holder, get all the bits which are not :,() and if you can't parse them as doubles they are the taxa. - //double heightOfThisNode = 0; - ArrayList taxa = new ArrayList(); - //int numOpenBrackets = 0; - while (!holder.isEmpty()) { - - String bit = holder.pop(); - if(bit.matches("[a-zA-Z]+")){ - //it's a taxa; - taxa.add(bit); - } - s.push(bit); - } - - boolean notComplete = true; - double totalHeight = 0; - totalHeight += Double.parseDouble(s.get(s.size()-2)); - int counter = 4; - while(notComplete){ - if(s.get(s.size()-counter).equals(")")){ - //still need to add more - totalHeight += Double.parseDouble(s.get(s.size()-(counter+1))); - counter += 3; - } else { - notComplete = false; - } - } - - tree.add(new tNode(taxa, totalHeight)); - //put holder back in the stack. - - } else { - s.push(next); - } - } - return tree; - } - - private int findMinIndex(String nf, int i){ - int minIndex = 0; - - int indexOpen = nf.indexOf('(',i); - int indexClose = nf.indexOf(')',i); - int indexComma = nf.indexOf(',',i); - int indexColon = nf.indexOf(':',i); - int indexBracket = 0; - if(indexOpen > 0 && indexClose > 0){ - indexBracket = Math.min(indexOpen,indexClose); - } else { - indexBracket = Math.max(indexOpen,indexClose); - } - int indexOther = 0; - if(indexComma > 0 && indexColon > 0){ - indexOther = Math.min(indexComma, indexColon); - } else { - indexOther = Math.max(indexComma, indexColon); - } - - if(indexBracket > 0 && indexOther > 0){ - minIndex = Math.min(indexBracket, indexOther); - } else { - minIndex = Math.max(indexBracket, indexOther); - } - return minIndex; - } - - - -} diff --git a/tSpace/caterpillar.java b/tSpace/caterpillar.java deleted file mode 100644 index 5f16305..0000000 --- a/tSpace/caterpillar.java +++ /dev/null @@ -1,65 +0,0 @@ -package tTree; -import java.util.*; -public class caterpillar { - - public static void main(String[] args) { - //Send arrays to make trees, compare trees, output tree along the path at time, time; - //nf = newickFormat - String nfTree1 = "(((A:1.0,B:1.0):3,C:4):1,((D:1,E:1):2,(F:2,G:2):1):2)"; - String nfTree2 = "((((A:1,B:1):2,C:3):1,(D:2,E:2):2):1,(F:1,G:1):4)"; - - TreeFromNewick tree1Newick = new TreeFromNewick(nfTree1); - ArrayList tree1 = tree1Newick.getTree(); - TreeFromNewick tree2Newick = new TreeFromNewick(nfTree2); - ArrayList tree2 = tree2Newick.getTree(); - - Map tree1Map = new HashMap(); - Map tree2Map = new HashMap(); - - CherryTree cherryTree1 = new CherryTree(tree1, tree1Map); - CherryTree cherryTree2 = new CherryTree(tree2, tree2Map); - - cherryTree1.getSimplifiedTrees(cherryTree2); - - System.out.println("Final CherryTree1: " + cherryTree1); - System.out.println("Final CherryTree2: " + cherryTree2); - System.out.println(); - System.out.println("___________________________________________________________"); - System.out.println(); - //The following is for cat2 - String[] t1LeftStr = {"1","2","3"}; - double[] t1LeftHeight = {2,2,4}; - String[] t1RightStr = {"4","5","6"}; - double[] t1RightHeight = {5,3,3}; - double t1Root = 7; - - String[] t2LeftStr = {"2","5","6"}; - double[] t2LeftHeight = {1,6,1}; - String[] t2RightStr = {"1","3","4"}; - double[] t2RightHeight = {3,4,3}; - double t2Root = 7; - - //currentTime is at what time one wants the tree, set to >1.0 if the entire path is desired; - double currentTime = 0.1; - //findThePath determines if a path is found or just the tree at currentTime, note: if currentTime > 1.0, the path is always returned. - boolean findThePath = false; - - //getStarted finds the path between two trees or the tree at currentTime; - System.out.println("This shows the paths between Cat2 trees"); - System.out.println("---------------------------------------"); - System.out.println(); - tNode.getStarted(t1LeftStr, t1LeftHeight, t1RightStr, t1RightHeight, t1Root, t2LeftStr, t2LeftHeight, t2RightStr, t2RightHeight, t2Root, currentTime, findThePath); - - System.out.println(); - System.out.println(); - System.out.println(); - - //for cat1: - String[] t1Str = {"1","2","3","4"}; - double[] t1Height = {4,4,7,12}; - String[] t2Str = {"1","4","3","2"}; - double[] t2Height = {1,1,3,9}; - - tNode.getTreesAndPath(t1Str, t1Height, t2Str, t2Height); - } -} diff --git a/tSpace/tNode.java b/tSpace/tNode.java deleted file mode 100644 index 6d3af3d..0000000 --- a/tSpace/tNode.java +++ /dev/null @@ -1,1158 +0,0 @@ -package tTree; -/* - * Node class, contains an arraylist of taxa included and a double height; - * - * To be added/changed: - * -- case1 need to handle a second call to case1 properly (by sorting speeds of 2+ cherry, using speedTree) - * -- perhaps make speedTree a method since same thing done in case0 and case1; - * -- -*/ -import java.util.*; -import java.io.*; -//Consider making the time of tree another part of tNode; -public class tNode { - ArrayList taxa; - double height; - - public tNode(ArrayList taxa, double height){ - this.taxa = taxa; - this.height = height; - } - - public void setHeight(double height){ - this.height = height; - } - public double getHeight(){ - return height; - } - public void setTaxa(ArrayList taxa){ - this.taxa = taxa; - } - public ArrayList getTaxa(){ - return taxa; - } - public String toString(){ - StringBuilder sb = new StringBuilder(); - sb.append(taxa); - sb.append(height); - return sb.toString(); - } - -//The following is for cat0 trees, but hopefully soon cat2 aswell. - //each taxon has it's own node so they can move more independently than before - //Also they move at constant speed so almost completely independent of end point (besides direction of movement) - //direction only changes in cat2+'s at root intersection. - //should take a double as speed - public static void getTreesAndPath(String[] t1Taxa, double[] t1Height, String[] t2Taxa, double[] t2Height){ - ArrayList tree1 = generateCat1(t1Taxa, t1Height); //trees of independent nodes --> - ArrayList tree2 = generateCat1(t2Taxa, t2Height); - //findDirections --> - ArrayList timeDistance = new ArrayList(); //distance = 1st element, rest are times - double totalDistance = 0.0; - ArrayList trees = new ArrayList(); - ArrayList treeB = tree1; - trees= getPath(tree1, tree2,treeB, timeDistance, totalDistance, trees); - System.out.println("Times: " + timeDistance); - System.out.println(trees); - String[] treesArray = new String[trees.size()]; - for(int i = 0; i generateCat1(String[] taxa, double[] height){ - ArrayList tree = new ArrayList(); - for(int i = 0; i nodeTaxa = new ArrayList(); - nodeTaxa.add(taxa[i]); - tree.add(new tNode(nodeTaxa, height[i])); - } - return tree; - } - //set everything to max speed --> either everything reaches it's destination or it intersects somewhere. - public static ArrayList findDirections(ArrayList tree1, ArrayList tree2){ - ArrayList directions = new ArrayList(); - double max = 0.0; - for(tNode n1 : tree1){ - String t1taxa = n1.getTaxa().get(0); - ArrayList taxa = new ArrayList(); - taxa.add(t1taxa); - for(tNode n2 : tree2){ - String t2taxa = n2.getTaxa().get(0); - if(t1taxa.compareTo(t2taxa) == 0){ - double height = (n2.getHeight() - n1.getHeight()); - if(Math.abs(height) > max){ - max = Math.abs(height); - } - /* - if(height != 0){ - height = height/Math.abs(height); //results in either 1 or -1. - } - */ - directions.add(new tNode(taxa, height)); - } - } - } - //multiply each direction by maxspeed; - for(tNode n : directions){ - n.setHeight(n.getHeight()); - } - - return directions; - } - //timeDistance is a map of the time and distance up to that time. As every time should be different it maps to the distance - //add directions to tree1 to get a new tree; - //check to see if there have been any topology changes --> document/restart from tree at that change; - //check to see if any nodes have stopped/should have stopped --> - public static ArrayList getPath(ArrayList tree1, ArrayList tree2, ArrayList treeB, - ArrayList timeDistance, double totalDistance, ArrayList trees){ - //will any nodes stop? --> adjust directions. every time --don't need it as a parameter - //comparing tree1 + directions to tree2 - //if 2.0 --> no stop, else --> stop - mergeSort(tree1); - System.out.println("Tree1: " + tree1); - System.out.println("TreeB: " + treeB); - System.out.println("Tree2: " + tree2); - trees.add(convertToNewick(treeB)); - //baseCase: - if(treesAreEqual(tree1, tree2)){ - System.out.println("Total Distance: " + totalDistance); - System.out.println(timeDistance); - return trees; - } - ArrayList treeNext = new ArrayList(); - ArrayList directions = findDirections(treeB, tree2); //get arrayList (sorted by tree1); - //create treeNext - for(int i = 0; i < treeB.size(); i++){ - double height = treeB.get(i).getHeight() + directions.get(i).getHeight(); - treeNext.add(new tNode(treeB.get(i).getTaxa(),height)); - } - mergeSort(treeNext); - //compare to tree2 to see if any nodes have stopped - double minTimeNodeStop = 10.0;//adjust - double nodeStop = findNodeStop(treeB, tree2, directions); - if(nodeStop != 10.0){ - minTimeNodeStop = nodeStop; - } - double minTimeTopologyAlteration = 10.0; - //check for topology changes - double topologyAlteration = findTopologyAlterations(treeB, treeNext); - if(topologyAlteration != 10.0){ - minTimeTopologyAlteration = topologyAlteration; - } - double minTime = Math.min(minTimeNodeStop, minTimeTopologyAlteration); - System.out.println("minTime: "+ minTime); - //one of minTimeNodeStop and minTimeTopologyAlteration must be less than 1.0. - //unless there are no topology alterations and each node moves the same distance. - //or it's the last step and things either move at zero or max; - if(minTime != 1.0){ - if(minTime == minTimeTopologyAlteration){ - treeNext = findTreeAtMinTime(treeB, directions, minTime); - totalDistance += findL1Distance(tree1, treeNext); - timeDistance.add(minTime); - getPath(treeNext, tree2, treeNext, timeDistance, totalDistance, trees); - } else { - treeNext = findTreeAtMinTime(treeB, directions, minTime); - getPath(tree1, tree2, treeNext, timeDistance, totalDistance, trees); - } - } else { - treeNext = findTreeAtMinTime(treeB, directions, 1.0); - totalDistance += findL1Distance(tree1, treeNext); - getPath(treeNext, tree2, treeNext, timeDistance, totalDistance, trees); - } - return trees; - } - - public static boolean treesAreEqual(ArrayList tree1, ArrayList tree2){ - boolean equal = true; //assume equal until 1 topology change or height change then return false; - for(tNode n1 : tree1){ - String taxa1 = n1.getTaxa().get(0); - for(tNode n2 : tree2){ - String taxa2 = n2.getTaxa().get(0); - if(taxa1.compareTo(taxa2) == 0){ - double height1 = n1.getHeight(); - double height2 = n2.getHeight(); - if(height1 != height2){ - return false; - } - } - } - } - return equal; - } - public static double findNodeStop(ArrayList tree1, ArrayList tree2, ArrayList directions){ - //directions is aligned with tree1, tree2 isn't. - //make a tree2 which is ordered by tree1; - ArrayList tree2Ordered = getTree2Ordered(tree1, tree2); - //using directions, tree2Ordered, and tree1 --> find out if the node will stop - double minTime = 10.0;//adjust - for(int i = 0; i < tree1.size(); i++){ - double nodeOri = tree1.get(i).getHeight(); - double actualDest = tree2Ordered.get(i).getHeight(); - double direction = directions.get(i).getHeight(); - if(direction != 0){ - double intersection = (actualDest - nodeOri)/direction; - if(intersection <= 1.0 && intersection < minTime){ - minTime = intersection; - } - } - } - return minTime; - } - - public static ArrayList getTree2Ordered(ArrayList tree1, ArrayList tree2){ - ArrayList tree2Ordered = new ArrayList(); - for(int i = 0; i < tree1.size(); i++){ - ArrayList taxa = new ArrayList(); - taxa.add(tree1.get(i).getTaxa().get(0)); - for(int j = 0; j < tree2.size(); j++){ - if(taxa.get(0).compareTo(tree2.get(j).getTaxa().get(0))==0){ - double height = tree2.get(j).getHeight(); - tree2Ordered.add(new tNode(taxa, height)); - } - } - } - return tree2Ordered; - } - - - public static double findTopologyAlterations(ArrayList tree1, ArrayList tree2){ - double minTime = 10.0; - ArrayList tree2Ordered = getTree2Ordered(tree1, tree2); - ArrayList timeSwap = timeSwapTree(tree1, tree2Ordered); - for(tNode n : timeSwap){ - if(n.getHeight() < minTime && n.getHeight() > 0.0){ - minTime = n.getHeight(); - } - } - return minTime; - } - public static ArrayList findTreeAtMinTime(ArrayList tree1, ArrayList directions, double minTime){ - ArrayList treeNext = new ArrayList(); - for(int i = 0; i taxa = new ArrayList(); - taxa.add(tree1.get(i).getTaxa().get(0)); - treeNext.add(new tNode(taxa, height)); - } - return treeNext; - } - public static double findL1Distance(ArrayList tree1, ArrayList tree2){ - double totalDistance = 0.0; - //assume ordered (as they meet at 1 or 0 topology changes; - for(int i = 0; i timeSwapTree(ArrayList tree1, ArrayList tree2){ - ArrayList timeSwapTree = new ArrayList(); - for(int i = 0; i < tree1.size() ; i++){ - double xOri = tree1.get(i).getHeight(); - double xDest = tree2.get(i).getHeight(); - for(int j = i + 1; j < tree1.size(); j++){ - double yOri = tree1.get(j).getHeight(); - double yDest = tree2.get(j).getHeight(); - double timeSwap = (yOri-xOri) / ((xDest - xOri) - (yDest - yOri)); - timeSwapTree.add(new tNode(tree1.get(i).getTaxa(), timeSwap)); - } - } - return timeSwapTree; - } - - //ConvertToNewick takes a tree of nodes and converts it to newick format; - public static String convertToNewick(ArrayList tree){ - String newickFormat = tree.get(0).getTaxa().get(0); - for(int i = 1; i sub1T1 = stringArrayToList(t1LeftTaxa); - ArrayList sub2T1 = stringArrayToList(t1RightTaxa); - ArrayList sub1T2 = stringArrayToList(t2LeftTaxa); - ArrayList sub2T2 = stringArrayToList(t2RightTaxa); - - ArrayList tree1 = generateCat2(t1LeftTaxa, t1LeftHeight, t1RightTaxa, t1RightHeight, t1RootHeight); - ArrayList tree2 = generateCat2(t2LeftTaxa, t2LeftHeight, t2RightTaxa, t2RightHeight, t2RootHeight); - - mergeSort(tree1); - mergeSort(tree2); - - System.out.println("Tree1:" + tree1); - System.out.println("Tree2: " + tree2); - - ArrayList crossedRoot = new ArrayList(); - ArrayList trees = new ArrayList(); - ArrayList times = new ArrayList(); - - //do I want a path or a tree? --> - if(findThePath || currentTime > 1.0){ - ArrayList path1Trees = null; - ArrayList path2Trees = null; - if(isTaxaEqual(t1LeftTaxa, t2LeftTaxa)){ - System.out.println(); - System.out.println("This is the path:"); - crossedRoot = findCrossedRoot(sub1T1, sub2T1, sub1T2, sub2T2); - double totalDistance = 0.0; - path1Trees = getCat2Path(tree1, tree1, tree2, trees, totalDistance, times, crossedRoot, sub1T1, sub2T1, sub1T2, sub2T2, currentTime); - - }else if(isTaxaEqual(t1LeftTaxa, t2RightTaxa)){ - System.out.println(); - System.out.println("This is the path:"); - crossedRoot = findCrossedRoot(sub1T1, sub2T1, sub2T2, sub1T2); - double totalDistance = 0.0; - path2Trees = getCat2Path(tree1,tree1, tree2, trees, totalDistance, times, crossedRoot, sub1T1, sub2T1, sub2T2, sub1T2, currentTime); - - }else { - System.out.println(); - System.out.println("PATH 1:"); - crossedRoot = findCrossedRoot(sub1T1, sub2T1, sub1T2, sub2T2); - double totalDistancePath1 = 0.0; - - path1Trees = getCat2Path(tree1, tree1, tree2, trees, totalDistancePath1, times, crossedRoot, sub1T1, sub2T1, sub1T2, sub2T2, currentTime); - - System.out.println(); - System.out.println("PATH 2:"); - tree1 = generateCat2(t1LeftTaxa, t1LeftHeight, t1RightTaxa, t1RightHeight, t1RootHeight); - trees = new ArrayList(); - times = new ArrayList(); - crossedRoot = findCrossedRoot(sub1T1, sub2T1, sub2T2, sub1T2); - double totalDistancePath2 = 0.0; - - path2Trees = getCat2Path(tree1,tree1, tree2, trees, totalDistancePath2, times, crossedRoot, sub1T1, sub2T1, sub2T2, sub1T2, currentTime); - } - - - printCat2Trees(path1Trees, "Cat2Path1.txt"); - printCat2Trees(path2Trees, "Cat2Path2.txt"); - - //we want the tree: - } else { - ArrayList endTree1 = null; - ArrayList endTree2 = null; - - if(isTaxaEqual(t1LeftTaxa, t2LeftTaxa)){ - System.out.println(); - System.out.println("This is the Tree:"); - crossedRoot = findCrossedRoot(sub1T1, sub2T1, sub1T2, sub2T2); - double totalDistance = 0.0; - endTree1 = getCat2Tree(tree1, tree1, tree2, trees, totalDistance, times, crossedRoot, sub1T1, sub2T1, sub1T2, sub2T2, currentTime); - - }else if(isTaxaEqual(t1LeftTaxa, t2RightTaxa)){ - System.out.println(); - System.out.println("This is the path:"); - crossedRoot = findCrossedRoot(sub1T1, sub2T1, sub2T2, sub1T2); - double totalDistance = 0.0; - endTree2 = getCat2Tree(tree1,tree1, tree2, trees, totalDistance, times, crossedRoot, sub1T1, sub2T1, sub2T2, sub1T2, currentTime); - - }else { - System.out.println(); - System.out.println("PATH 1:"); - crossedRoot = findCrossedRoot(sub1T1, sub2T1, sub1T2, sub2T2); - double totalDistancePath1 = 0.0; - - endTree1 = getCat2Tree(tree1, tree1, tree2, trees, totalDistancePath1, times, crossedRoot, sub1T1, sub2T1, sub1T2, sub2T2, currentTime); - System.out.println(); - System.out.println("EndTree1: " + endTree1); - - System.out.println(); - System.out.println("PATH 2:"); - tree1 = generateCat2(t1LeftTaxa, t1LeftHeight, t1RightTaxa, t1RightHeight, t1RootHeight); - trees = new ArrayList(); - times = new ArrayList(); - crossedRoot = findCrossedRoot(sub1T1, sub2T1, sub2T2, sub1T2); - double totalDistancePath2 = 0.0; - - endTree2 = getCat2Tree(tree1,tree1, tree2, trees, totalDistancePath2, times, crossedRoot, sub1T1, sub2T1, sub2T2, sub1T2, currentTime); - System.out.println(); - System.out.println("EndTree2: " + endTree2); - } - - if(endTree1 != null){ - ArrayList endTree = new ArrayList(); - endTree.add(convertCat2ToNewick(endTree1, endTree1.get(0).getTaxa().get(0))); - printCat2Trees(endTree, "Cat2Tree1.txt"); - } - if(endTree2 != null){ - ArrayList endTree = new ArrayList(); - endTree.add(convertCat2ToNewick(endTree2, endTree2.get(0).getTaxa().get(0))); - printCat2Trees(endTree, "Cat2Tree2.txt"); - } - } - - - - - } - - private static ArrayList getCat2Tree(ArrayList tree1, ArrayList treeB, ArrayList tree2, - ArrayList trees, double totalDistance, ArrayList times, ArrayList crossedRoot, - ArrayList sub1T1, ArrayList sub2T1, ArrayList sub1T2, ArrayList sub2T2, - double currentTime){ - System.out.println("............................................................................................."); - System.out.println(); - System.out.println("Initial Tree: " + tree1); - System.out.println(); - //instead of recursive make a while loop, as in while time < currentTime - times.add(0.0); - ArrayList treeA = new ArrayList(); - treeA.addAll(tree1); - while(currentTime > times.get(times.size()-1)){ - - //each time need to convert "currentTime" to modCurrentTime; - double modCurrentTime = (currentTime - times.get(times.size()-1))/(1.0-times.get(times.size()-1)); - - ArrayList directions = findCat2Directions(treeA, tree2, crossedRoot, sub1T1, sub2T1, sub1T2, sub2T2); - - //get minTime; - double minTimeNodeStop = 10.0;//adjust - double nodeStop = findCat2NodeStop(treeA, tree2, directions, crossedRoot); - if(nodeStop != 10.0 && nodeStop > 0.0){ - minTimeNodeStop = nodeStop; - } - - double minTimeIntersection = 10.0; - double intersection = findCat2Intersections(treeA, directions, crossedRoot); - if(intersection != 10.0 && intersection > 0.0){ - minTimeIntersection = intersection; - } - double minTime = Math.min(minTimeNodeStop, minTimeIntersection); - - //get the tree at minTime; - //add it to times; change destinationTree; - minTime = Math.min(minTime, modCurrentTime); - double time = minTime; - if(!times.isEmpty()){ - double prevTime = times.get(times.size()-1); - time = prevTime + (1.0-prevTime)*minTime; - } - times.add(time); - - treeB = findCat2NextTree(treeA, directions, minTime, crossedRoot); - totalDistance += findL1Cat2Distance(treeA, treeB); - treeA = new ArrayList(); - treeA.addAll(treeB); - System.out.println("TreeNext:" + treeA); - System.out.println("TOTAL: " + totalDistance); - - } - return treeA; - } - - private static ArrayList findCrossedRoot(ArrayList sub1T1, ArrayList sub2T1, - ArrayList sub1T2, ArrayList sub2T2){ - ArrayList crossedRoot = new ArrayList(); - //add each taxa which isn't crossing the root to crossedRoot; - for(String taxa: sub1T1){ - if(sub1T2.contains(taxa)){ - crossedRoot.add(taxa); - } - } - for(String taxa: sub2T1){ - if(sub2T2.contains(taxa)){ - crossedRoot.add(taxa); - } - } - return crossedRoot; - } - - private static ArrayList stringArrayToList(String[] array){ - ArrayList list = new ArrayList(); - for(int i = 0 ; i< array.length ; i++){ - list.add(array[i]); - } - return list; - } - //returns true if there are not changes over the root node; - private static boolean isTaxaEqual(String[] t1Taxa, String[] t2Taxa){ - //go through the taxa - boolean equal = false; - for(String taxa1 : t1Taxa){ - for(String taxa2 : t2Taxa){ - if(taxa1.equals(taxa2)){ - equal = true; - } - } - if(!equal){ - return false; - } - equal = false; - } - return true; - } - - private static ArrayList getCat2Path(ArrayList tree1, ArrayList treeB, ArrayList tree2, - ArrayList trees, double totalDistance, ArrayList times, ArrayList crossedRoot, - ArrayList sub1T1, ArrayList sub2T1, ArrayList sub1T2, ArrayList sub2T2, - double currentTime){ - //go through each taxa of the tree - System.out.println("............................................................................................."); - System.out.println(); - mergeSort(treeB); - - trees.add(convertCat2ToNewick(tree1, crossedRoot.get(0))); //separate to subtrees, find newick format between the two, and add a comma between them. - //baseCase: - if(areCat2TreesEqual(tree1, tree2)){ - System.out.println("*********************************************************************************************"); - System.out.println("Total Distance: " + totalDistance); - System.out.println("Times:" + times); - System.out.println("End Tree: " + tree1); - System.out.println("*********************************************************************************************"); - return trees; - } - - ArrayList directions = findCat2Directions(treeB, tree2, crossedRoot, sub1T1, sub2T1, sub1T2, sub2T2); //get arrayList (sorted by tree1); - System.out.println("Directions: " + directions); - //create treeNext - ArrayList treeNext = getTreeNext(treeB, directions); - mergeSort(treeNext); - //Now find next intersection/node stop, - double minTimeNodeStop = 10.0;//adjust - double nodeStop = findCat2NodeStop(treeB, tree2, directions, crossedRoot); - if(nodeStop != 10.0 && nodeStop > 0.0){ - minTimeNodeStop = nodeStop; - } - System.out.println("nodestop: " + nodeStop); - - double minTimeIntersection = 10.0; - double intersection = findCat2Intersections(treeB, directions,crossedRoot); - if(intersection != 10.0 && intersection > 0.0){ - minTimeIntersection = intersection; - } - System.out.println("Intersections: " + intersection); - double minTime = Math.min(minTimeNodeStop, minTimeIntersection); - - ArrayList theRealTree1 = new ArrayList(); - theRealTree1.addAll(tree1); - - double time = minTime; - if(!times.isEmpty()){ - double prevTime = times.get(times.size()-1); - time = prevTime + (1.0-prevTime)*minTime; - } - if(currentTime <= time){ - double prevTime = times.get(times.size()-1); - minTime = (time - prevTime)/(1.0 - prevTime); - treeNext = findCat2NextTree(treeB, directions, minTime, crossedRoot); - ArrayList timeTree = new ArrayList(); - timeTree.add(convertCat2ToNewick(treeNext, crossedRoot.get(0))); - return timeTree; - } - - if(minTime != 1.0){ - if(minTime == minTimeIntersection){ - treeNext = findCat2NextTree(treeB, directions, minTime, crossedRoot); - totalDistance += findL1Cat2Distance(theRealTree1, treeNext); - times.add(time); - System.out.println("TreeNext:" + treeNext); - getCat2Path(treeNext, treeNext, tree2, trees, totalDistance, times, crossedRoot, sub1T1, sub2T1, sub1T2, sub2T2, currentTime); - } else { - treeNext = findCat2NextTree(treeB, directions, minTime, crossedRoot); - System.out.println("TreeNext:" + treeNext); - getCat2Path(theRealTree1, treeNext, tree2, trees, totalDistance, times, crossedRoot, sub1T1, sub2T1, sub1T2, sub2T2, currentTime); - } - } else { - treeNext = findCat2NextTree(treeB, directions, minTime, crossedRoot); - totalDistance += findL1Cat2Distance(tree1, treeNext); - System.out.println("TreeNext, also final tree:" + treeNext); - times.add(time); - getCat2Path(treeNext, treeNext, tree2, trees, totalDistance, times, crossedRoot, sub1T1, sub2T1, sub1T2, sub2T2, currentTime); - - } - return trees; - } - //calculates the distance between two trees, this should be called after/at each topology change (intersection). - private static double findL1Cat2Distance(ArrayList tree1, ArrayList tree2){ - double distance = 0.0; - //because the two trees are only a single topology change apart, can simply find distance between tree1[i] and tree2[i]. - //these trees should also be of the same size; - for(int i = 0; i < tree1.size(); i++){ - distance += Math.abs(tree1.get(i).getHeight() - tree2.get(i).getHeight()); - } - return distance; - } - - private static ArrayList findCat2NextTree(ArrayList tree, ArrayList directions, - double time, ArrayList crossedRoot){ - //we know an intersection or a nodeStop occurs, so something is going to happen/move/intersect/just stop. - mergeSort(tree); - ArrayList endTree; - ArrayList sub1Taxa = new ArrayList(); - ArrayList sub2Taxa = new ArrayList(); - ArrayList subTree1 = new ArrayList(); - ArrayList subTree2 = new ArrayList(); - ArrayList rootTaxa = new ArrayList(); - - //determine both subTrees - rootTaxa.addAll(tree.get(tree.size()-1).getTaxa()); - sub1Taxa.addAll(tree.get(tree.size()-2).getTaxa()); - rootTaxa.removeAll(sub1Taxa); - - for(int i = tree.size()-3; i >= 0; i--){ - if(rootTaxa.containsAll(tree.get(i).getTaxa())){ - rootTaxa.removeAll(tree.get(i).getTaxa()); - sub2Taxa.addAll(tree.get(i).getTaxa()); - } - } - //sub1Taxa, sub2Taxa have the subtrees's taxa. - //covert them to the 's required and add a "R" node as the roots - subTree1 = getSubtree(sub1Taxa, tree); - subTree2 = getSubtree(sub2Taxa, tree); - //add directions to each; - subTree1 = addDirections(subTree1, directions, time); - subTree2 = addDirections(subTree2, directions, time); - - //have new trees positions : time to recreate new tree (endTree) - mergeSort(subTree1); - mergeSort(subTree2); - //go through each node in both subtrees --> see if any nodes are at the same height of the root and not in crossedRoot; - ArrayList crossingNodes = findCrossingNodes(subTree1, crossedRoot); - if(crossingNodes != null){ - subTree1.removeAll(crossingNodes); - subTree2.addAll(crossingNodes); - } - crossingNodes = findCrossingNodes(subTree2, crossedRoot); - if(crossingNodes != null){ - subTree2.removeAll(crossingNodes); - subTree1.addAll(crossingNodes); - } - //now rebuild the subtrees, combine them, and add the root; - endTree = createEndTree(subTree1, subTree2); - - mergeSort(endTree); - endTree.add(new tNode(tree.get(tree.size()-1).getTaxa(), tree.get(tree.size()-1).getHeight())); - return endTree; - } - - - private static ArrayList getSubtree(ArrayList subTaxa, ArrayList tree){ - ArrayList subTree = new ArrayList(); - for(String taxon : subTaxa){ - for(tNode n: tree){ - if(n.getTaxa().contains(taxon)){ - ArrayList taxa = new ArrayList(); - taxa.add(taxon); - subTree.add(new tNode(taxa, n.getHeight())); - break; - } - } - } - ArrayList root = new ArrayList(); - //not sure we need R. - root.add("R"); - subTree.add(new tNode(root, tree.get(tree.size()-1).getHeight())); - return subTree; - } - private static ArrayList addDirections(ArrayList subTree, ArrayList directions, double time){ - for(tNode n : subTree){ - //find n.getTaxa in directions - for(tNode d : directions){ - if(d.getTaxa().equals(n.getTaxa())){ - n.setHeight(n.getHeight() + (d.getHeight()*time)); - break; - } - } - } - return subTree; - } - - private static ArrayList findCrossingNodes(ArrayList subTree , ArrayList crossedRoot){ - ArrayList crossingNodes = new ArrayList(); - - for(int i = 0; i < subTree.size() ; i++){ - tNode n = subTree.get(i); - if(!n.getTaxa().get(0).equals("R")){ - if(n.getHeight() == subTree.get(subTree.size()-1).getHeight() && !crossedRoot.containsAll(n.getTaxa())){ - crossedRoot.addAll(n.getTaxa()); - crossingNodes.add(n); - } - } - } - return crossingNodes; - - } - - private static ArrayList createEndTree(ArrayList subTree1, ArrayList subTree2){ - ArrayList endTree = new ArrayList(); - for(int i = 0; i < subTree1.size() ; i++){ - ArrayList taxa = new ArrayList(); - if(!subTree1.get(i).getTaxa().get(0).equals("R")){ - for(int j = 0; j <= i; j++){ - if(!subTree1.get(j).getTaxa().get(0).equals("R")){ - taxa.addAll(subTree1.get(j).getTaxa()); - } - } - endTree.add(new tNode(taxa, subTree1.get(i).getHeight())); - } - } - for(int i = 0; i < subTree2.size(); i++){ - ArrayList taxa = new ArrayList(); - if(!subTree2.get(i).getTaxa().get(0).equals("R")){ - for(int j = 0; j<= i; j++){ - if(!subTree2.get(j).getTaxa().get(0).equals("R")){ - - - taxa.addAll(subTree2.get(j).getTaxa()); - } - } - endTree.add(new tNode(taxa, subTree2.get(i).getHeight())); - } - } - return endTree; - } - //great leftTree, rightTree then join them under the root - private static ArrayList generateCat2(String[] leftTaxa, double[] leftHeight, String[] rightTaxa, - double[] rightHeight, double rootHeight){ - - ArrayList left = new ArrayList(); - for(int i = 0; i < leftTaxa.length; i++){ - ArrayList taxaL = new ArrayList(); - taxaL.add(leftTaxa[i]); - left.add(new tNode(taxaL, leftHeight[i])); - } - ArrayList right = new ArrayList(); - for(int i = 0; i < rightTaxa.length; i++){ - ArrayList taxaR = new ArrayList(); - taxaR.add(rightTaxa[i]); - right.add(new tNode(taxaR, rightHeight[i])); - } - mergeSort(left); - mergeSort(right); - - //now that they are sorted, add them up into a tree - ArrayList tree = new ArrayList(); - ArrayList leftTree = new ArrayList(); - ArrayList rightTree = new ArrayList(); - - for(int i = 0; i < left.size(); i++){ - ArrayList taxaL = new ArrayList(); - for(int j = 0; j <= i; j++){ - taxaL.addAll(left.get(j).getTaxa()); - } - leftTree.add(new tNode(taxaL, left.get(i).getHeight())); - } - - for(int i = 0; i < right.size(); i++){ - ArrayList taxaR = new ArrayList(); - for(int j = 0; j <= i; j++){ - taxaR.addAll(right.get(j).getTaxa()); - } - rightTree.add(new tNode(taxaR, right.get(i).getHeight())); - } - - tree.addAll(leftTree); - tree.addAll(rightTree); - ArrayList taxaAll = new ArrayList(); - taxaAll.addAll(leftTree.get(leftTree.size()-1).getTaxa()); - taxaAll.addAll(rightTree.get(rightTree.size()-1).getTaxa()); - tree.add(new tNode(taxaAll, rootHeight)); - - return tree; - } - - private static boolean areCat2TreesEqual(ArrayList tree1, ArrayList tree2){ - //if they're sorted which they are, then each node at each height will equal one of the other nodes at that height, else --> not equal. - boolean equal = true; - for(tNode n1: tree1){ - if(n1.getTaxa().size() > 1){ - double height1 = n1.getHeight(); - boolean foundMatch = false; - for(tNode n2: tree2){ - if(n2.getHeight() == height1 && n2.getTaxa().size() == n1.getTaxa().size()){ - if(n1.getTaxa().containsAll(n2.getTaxa())){ - //then they are equal - foundMatch = true; - } - } - } - if(!foundMatch){ - equal = false; - } - } - } - return equal; - } - - //get a tNode ArrayList of taxa and their directions. - private static ArrayList findCat2Directions(ArrayList tree1, ArrayList tree2, ArrayList crossedRoot, - ArrayList sub1T1, ArrayList sub2T1, ArrayList sub1T2, ArrayList sub2T2){ - ArrayList directions = new ArrayList(); - //for each taxa find it in the subtrees, assume if it's not in the 1st its in the second. - //if they are in the same: find direction/max as usual - //else and not in crossedRoot, then they need to cross root --> findtotalDistance --> max/direction, dir == +ve - - //most of the "heights" should really be refered to as "speeds/directions" - ArrayList foundTaxa = new ArrayList(); - double max = 0.0; - for(tNode n1: tree1){ - for(String taxa : n1.getTaxa()){ - if(!foundTaxa.contains(taxa)){ - foundTaxa.add(taxa); - ArrayList nextTaxa = new ArrayList(); - nextTaxa.add(taxa); - if(sub1T1.contains(taxa)){ - if(sub2T2.contains(taxa) && !crossedRoot.contains(taxa)){ - //direction will be +ve, work out what it's max speed is - double height1 = n1.getHeight(); - double crossedRootSpeed = findCrossedRootSpeed(taxa, height1, tree2); - if(crossedRootSpeed > max){ - max = crossedRootSpeed; - } - directions.add(new tNode(nextTaxa, crossedRootSpeed));//changed from 1 to the actual speed = crossedRootSpeed - //will need to make sure this intersects with root properly! - - }else{ //it's in sub1T2 (same) - for(tNode n2: tree2){ - if(n2.getTaxa().contains(taxa)){ - double height = (n2.getHeight() - n1.getHeight()); - if(Math.abs(height) > max){ - max = Math.abs(height); - } - /* - if(height != 0){ - height = height/Math.abs(height); //results in either 1 or -1. - } - */ - directions.add(new tNode(nextTaxa, height)); - break; - } - } - } - }else { //it's in sub2T1 - if(sub1T2.contains(taxa) && !crossedRoot.contains(taxa)){ - double height1 = n1.getHeight(); - double crossedRootSpeed = findCrossedRootSpeed(taxa, height1, tree2); - if(crossedRootSpeed > max){ - max = crossedRootSpeed; - } - directions.add(new tNode(nextTaxa, crossedRootSpeed)); //speed adjusted from 1 to CRS as above - - }else{ //it's in sub2T2 (same) - for(tNode n2: tree2){ - if(n2.getTaxa().contains(taxa)){ - double height = (n2.getHeight() - n1.getHeight()); - if(Math.abs(height) > max){ - max = Math.abs(height); - } - /* standardization removed; - if(height != 0){ - height = height/Math.abs(height); //results in either 1 or -1. - } - */ - directions.add(new tNode(nextTaxa, height)); - break; - } - - } - } - } - } - } - } - //add root speed; - ArrayList root = new ArrayList(); - root.addAll(tree1.get(tree1.size()-1).getTaxa()); - directions.add(new tNode(root, 0)); - for(tNode n : directions){ - n.setHeight(n.getHeight()); //no longer multiply by max, as all roots should have their own speeds not the max speed; - } - return directions; - } - - //find treeNext; - private static ArrayList getTreeNext(ArrayList treeB, ArrayList directions){ - ArrayList treeNext = new ArrayList(); - ArrayList coveredTaxa = new ArrayList(); - for(tNode n1: treeB){ - for(String taxa : n1.getTaxa()){ - if(!coveredTaxa.contains(taxa)){ - coveredTaxa.add(taxa); - for(tNode n2: directions){ - if(n2.getTaxa().get(0).equals(taxa)){ - double height = n1.getHeight() + n2.getHeight(); - treeNext.add(new tNode(n1.getTaxa(), height)); - break; - } - } - } - } - } - //add root; - treeNext.add(new tNode(treeB.get(treeB.size()-1).getTaxa(), treeB.get(treeB.size()-1).getHeight())); - return treeNext; - } - - - //initially we'll assume the root height doesn't change; - private static double findCrossedRootSpeed(String taxa, double height1, ArrayList tree2){ - //get height1 --> root, root --> height2; - double max = 0.0; - double rootHeight = tree2.get(tree2.size()-1).getHeight(); - double toRoot = rootHeight - height1; - for(tNode n : tree2){ - if(n.getTaxa().contains(taxa)){ - double fromRoot = rootHeight - n.getHeight(); - max = toRoot + fromRoot; - break; - } - } - return max; - } - - //add check to only calculate it if it IS in crossedRoot. - private static double findCat2NodeStop(ArrayList treeB, ArrayList tree2, - ArrayList directions, ArrayList crossedRoot){ - double minTime = 10.0; - double direction = 0.0, nodeDest = 0.0, nodeOri = 0.0; - ArrayList coveredTaxa = new ArrayList(); - for(tNode n1: treeB){ - for(String taxa: n1.getTaxa()){ - if(!coveredTaxa.contains(taxa)){ - coveredTaxa.add(taxa); - if(crossedRoot.contains(taxa)){ - nodeOri = n1.getHeight(); - //find taxa in the other two - //NOTE: if two are at the same point then you're getting the same value so 1st occurence is all that's required - for(tNode n2: tree2){ - if(n2.getTaxa().contains(taxa)){ - nodeDest = n2.getHeight(); - break; - } - } - for(tNode nD: directions){ - if(nD.getTaxa().contains(taxa)){ - direction = nD.getHeight(); - break; - } - } - if(direction != 0){ - double intersection = (nodeDest - nodeOri)/direction; - if(intersection <= 1.0 && intersection < minTime){ - minTime = intersection; - } - } - } - } - } - } - return minTime; - } - - private static double findCat2Intersections(ArrayList tree1, ArrayList directions, - ArrayList crossedRoot){ - ArrayList rootTaxa = directions.get(directions.size()-1).getTaxa(); - rootTaxa.add("R"); - //find intersections for each subtree --> return least of both. - double time1 = findIntersection(tree1, directions, crossedRoot, rootTaxa); - double time2 = findIntersection(tree1, directions, crossedRoot, rootTaxa); - - return Math.min(time1, time2); - } - private static double findIntersection(ArrayList tree1, ArrayList directions, - ArrayList crossedRoot, ArrayList subtree){ - double minTime = 10.0; - double time = 10.0; - double yOri = 0.0, yDir = 0.0; - for(int i = 0; i < subtree.size()-1 ;i++){ - String taxa = subtree.get(i); - double xOri = findTaxaHeightInTree(taxa, tree1); - double xDir = findTaxaHeightInTree(taxa, directions); - for(int j = i+1; j < subtree.size(); j++){ - String taxa2 = subtree.get(j); - if(taxa2.equals("R")){ - yOri = tree1.get(tree1.size()-1).getHeight(); - yDir = 0.0; - } else { - yOri = findTaxaHeightInTree(taxa2, tree1); - yDir = findTaxaHeightInTree(taxa2, directions); - } - if((xDir - yDir) != 0){ - time = (yOri-xOri)/(xDir - yDir); - } - if(time > 0 && time < minTime){ - minTime = time; - } - } - } - - return minTime; - } - - private static double findTaxaHeightInTree(String taxa, ArrayList tree){ - double height = 0.0; - for(tNode n: tree){ - if(n.getTaxa().contains(taxa)){ - height = n.getHeight(); - break; - } - } - return height; - } - - //break into the two subtrees, then generate newick, then combine with a comma - private static String convertCat2ToNewick(ArrayList tree, String crossedRoot){ - String newickFormat = ""; - //crossedRoot should always be in sub1Taxa inorder to maintain tree viewing stability.. - ArrayList sub1Taxa = new ArrayList(); - ArrayList sub2Taxa = new ArrayList(); - ArrayList rootTaxa = new ArrayList(); - - rootTaxa.addAll(tree.get(tree.size()-1).getTaxa()); - sub1Taxa.addAll(tree.get(tree.size()-2).getTaxa()); - rootTaxa.removeAll(sub1Taxa); - for(int i = tree.size()-3; i >= 0; i--){ - if(rootTaxa.containsAll(tree.get(i).getTaxa())){ - rootTaxa.removeAll(tree.get(i).getTaxa()); - sub2Taxa.addAll(tree.get(i).getTaxa()); - } - } - ArrayList subTaxaHolder = new ArrayList(); - if(!sub1Taxa.contains(crossedRoot)){ - subTaxaHolder.addAll(sub1Taxa); - sub1Taxa = new ArrayList(); - sub1Taxa.addAll(sub2Taxa); - sub2Taxa = new ArrayList(); - sub2Taxa.addAll(subTaxaHolder); - } - - ArrayList subTree1 = getSubtree(sub1Taxa, tree); - ArrayList subTree2 = getSubtree(sub2Taxa, tree); - // with these you can call them to "covertToNewick" to get separate subtrees, and then combine. - String tree1 = convertToModNewick(subTree1); - String tree2 = convertToModNewick(subTree2); - - newickFormat = "(" + tree1 + "," + tree2 + ")"; - - return newickFormat; - } - - private static String convertToModNewick(ArrayList tree){ - String newickFormat = tree.get(0).getTaxa().get(0); - for(int i = 1; i trees, String filename){ - if(trees == null || trees.isEmpty()){ - return; - } - String[] treePath = new String[trees.size()]; - for(int i = 0; i mergeSort(ArrayList tree){ - - ArrayList left = new ArrayList(); - ArrayList right = new ArrayList(); - int center; - if(tree.size() ==1){ - return tree; - } else { - center = tree.size()/2; - for(int i = 0; i left, ArrayList right, ArrayList tree) { - int leftIndex = 0, rightIndex = 0, treeIndex = 0; - - while(leftIndex rest; - int restIndex; - if(leftIndex >= left.size()){ - rest = right; - restIndex = rightIndex; - }else{ - rest=left; - restIndex = leftIndex; - } - for(int i=restIndex; i taxa; - double height; - - public tNode(ArrayList taxa, double height){ - this.taxa = taxa; - this.height = height; - } - - public void setHeight(double height){ - this.height = height; - } - public double getHeight(){ - return height; - } - public void setTaxa(ArrayList taxa){ - this.taxa = taxa; - } - public ArrayList getTaxa(){ - return taxa; - } - public String toString(){ - StringBuilder sb = new StringBuilder(); - sb.append(taxa); - sb.append(height); - return sb.toString(); - } - - //Generate takes taxa, heights of taxa, etc. - public static double generate(String[] taxa1, double[] height1,String[] taxa2, double[] height2, double time, - boolean iterate,boolean distances,boolean absSpeed, String filename, int t1Move, int t2Move, int t3Move){ - ArrayList tree1 = new ArrayList(); - ArrayList tree2 = new ArrayList(); - //perhaps start with check if taxa and height are of equal length and also that taxa1 and taxa2 are equal length; - //also check how many taxa. - need at least 2; - for(int i = 0; i s1 = new ArrayList(); - ArrayList s2 = new ArrayList(); - s1.add(taxa1[i]); - s2.add(taxa2[i]); - tNode node1 = new tNode(s1, height1[i]); - tNode node2 = new tNode(s2, height2[i]); - tree1.add(node1); - tree2.add(node2); - } - //cat0 --> regular caterpillar path - //cherry --> path in which 2 cherries occur - if(distances){ - double cat0 = findAllTimesOfIntersection(tree1, tree2, absSpeed); - double cherry = findTheWay(tree1, tree2, t1Move, t2Move, t3Move, false); - if(cat0 > cherry){ - System.out.println("Double Cherry PathWay is quicker!"); - } else { - System.out.println("Regular Caterpillar is quicker!"); - } - } - - //otherPathway(tree1, tree2); - return findTheWay(tree1, tree2, t1Move, t2Move, t3Move, distances); - } - - -//calls modifiedSpeedTree for each topology change - //Assumes the 4 taxa tree where (1,2),(1,2,3),(1,2,3,4) --> (3,4),(3,4,1),(3,4,1,2) - //I use ints and cases for the possible speeds of nodes - //find intersection; - //get values at that point in time, and find distance between t1 and tnext; - //set tree1 to tree at that time, - //find next intersection until time:1 - public static double findAllTimesOfIntersection(ArrayList tree1, ArrayList tree2, boolean absSpeed){ - ArrayList allTimes = new ArrayList(); - double totalDistance = 0; - allTimes = findTimes(tree1, tree2, allTimes, absSpeed, totalDistance); - allTimes.add(0.0); - double totalTime = 0.0; - ArrayList allTimes2 = new ArrayList(); - ArrayList allTimes3 = new ArrayList(); - allTimes3.add(0.0); - for(int i = allTimes.size()-1; i >= 0; i--){ - allTimes2.add(allTimes.get(i)); - } - //here, calculate totalDistance which will be returned for path length comparison - ArrayList treeNext = tree1; - - for(double t : allTimes2){ - treeNext = mergeSort(treeNext); - ArrayList speed = speedTree(treeNext, tree2, false); - ArrayList treeNext2 = getTreeAtTime(treeNext, speed, t); - System.out.println("TreeNext2: " + treeNext2); - System.out.println(findTheDistance(treeNext, treeNext2)); - totalDistance += findTheDistance(treeNext, treeNext2); - treeNext = treeNext2; - } - System.out.println("This: " + totalDistance); - - System.out.println(allTimes2); - for(int i = 1; i < allTimes2.size(); i++){ - double t1 = allTimes2.get(i); - totalTime = t1 * (1.0 - totalTime) + totalTime; - System.out.println(totalTime); - allTimes3.add(totalTime); - } - return totalDistance; - } - public static ArrayList findTimes(ArrayList tree1, ArrayList tree2, - ArrayList allTimes, boolean absSpeed, double totalDistance){ - tree1 = mergeSort(tree1); - ArrayList speedTree = speedTree(tree1, tree2, absSpeed); - ArrayList tree2Ordered = new ArrayList(); - for(int i=0; i timeSwapTree = timeSwapTree(tree1, tree2Ordered); - //perhaps timeSwapTree method should just produce the minTime - since that's all we use it for. - double minTime = 3.0; - for(int i = 0 ; i 0.0){ - minTime = timeSwapTree.get(i).getHeight(); - } - } - //System.out.println("MinTime: " + minTime); - if(minTime == 1.0){ - allTimes = findTimes(tree1, tree2, allTimes,false, totalDistance); - } - else if(minTime > 1.0){ - allTimes.add(1.0); - double dist = findTheDistance(tree1, tree2); - totalDistance += dist; - //System.out.println(dist); - System.out.println("TD: " +totalDistance); - return allTimes; - } else { - ArrayList treeNext = getTreeAtTime(tree1, speedTree, minTime); - System.out.println("TreeNext: " + treeNext); - double dist = findTheDistance(tree1, treeNext); - totalDistance += dist; - ///System.out.println(dist); - //System.out.println("TD: " +totalDistance); - allTimes = findTimes(treeNext, tree2, allTimes, absSpeed, totalDistance); - allTimes.add(minTime); - } - - return allTimes; - } - public static double findTheDistance(ArrayList tree1, ArrayList tree2){ - double distance = 0; - for(int i = 1; i tree1, ArrayList tree2){ - double distance = 0; - for(int i = 0; i getTreeAtTime(ArrayList tree, ArrayList speedTree, double time){ - ArrayList treeNext = new ArrayList(); - for(int i=0; i (3,4),(3,4,1),(3,4,1,2) - //I use ints and cases for the possible speeds of nodes - //output --> determines if the path is outputed or not. - public static double findTheWay(ArrayList tree1, ArrayList tree2, int t1Move, int t2Move, int t3Move, boolean output){ - - double distance = 0.0; - double dist = 0.0; - tree1 = mergeSort(tree1); - tree2 = mergeSort(tree2); - tree1 = generateProperTree(tree1); - tree2 = generateProperTree(tree2); - tree2 = reorderTree2(tree2); //boths trees now go: t1, t2, t3 - - ArrayList speedTree = modifiedSpeedTree(tree1, tree2, t1Move, t2Move, 5, null); - ArrayList tree2Mod = tree2Mod(tree1, speedTree); - - ArrayList timeSwapTree = timeSwapTree(tree1, tree2Mod); - double minTime = findMinTime(timeSwapTree); - if(minTime == 1.0){ - return 100000; - } - ArrayList treeStage2 = modifiedGetTreeAtTime(tree1, speedTree, minTime); - dist = findDistance(tree1, treeStage2); - distance += dist; - if(output){ - System.out.println(); - System.out.println("The choice of the speeds " + t1Move + " , " + t2Move + " , " + t3Move + " result in the following: "); - System.out.println("Tree1: " + tree1); - System.out.println("Speed Tree: " + speedTree); - System.out.println("Tree to reach: " + tree2Mod); - System.out.println("Tree End stage 1:" + treeStage2); - System.out.println("Distance after stage1: " + dist); - } - - for(int i = 0; i< speedTree.size(); i++){ - double height = speedTree.get(i).getHeight(); - speedTree.get(i).setHeight(height*(1-minTime)); - } - System.out.println("That speedTree: " + speedTree); - //Stage2: - speedTree = modifiedSpeedTree(treeStage2, tree2, 10, 6, t3Move, speedTree); - tree2Mod = tree2Mod(treeStage2, speedTree); - - //tree2Ordered = tree2Ordered(treeStage2, speedTree); - timeSwapTree = timeSwapTree(treeStage2, tree2Mod); - minTime = findMinTime(timeSwapTree); - if(minTime == 1.0){ - return 100000; - } - ArrayList treeStage3 = getTreeAtTime(treeStage2, speedTree, minTime); - dist = findDistance(treeStage2, treeStage3); - distance += dist; - - - if(output){ - System.out.println("Speed Tree: " + speedTree); - System.out.println("Tree to reach: " + tree2Mod); - System.out.println("Tree End stage 2:" + treeStage3); - System.out.println("Distance after stage2: " + dist); - } - //stage3: - for(int i = 0; i< speedTree.size(); i++){ - double height = speedTree.get(i).getHeight(); - speedTree.get(i).setHeight(height*(1-minTime)); - } - System.out.println("That speedTree: " + speedTree); - - - - timeSwapTree = timeSwapTree(treeStage3, tree2Mod); - minTime = findMinTime(timeSwapTree); - if(minTime == 1.0){ - return 100000; - } - ArrayList treeStage4 = getTreeAtTime(treeStage3, speedTree, minTime); - dist = findDistance(treeStage3, treeStage4); - distance += dist; - - if(output){ - System.out.println("Tree End stage3: " + treeStage4); - System.out.println("Distance after stage3: " + dist); - } - //stage4: only need distance; - - dist = findDistance(treeStage4, tree2); - distance += dist; - - if(output){ - System.out.println("End tree: " + tree2); - System.out.println("Distance after stage4: " + dist); - System.out.println("Total Distance: " + distance); - } - return distance; - } - public static ArrayList modifiedGetTreeAtTime(ArrayList tree, ArrayList speedTree, double minTime){ - ArrayList treeAtTime = new ArrayList(); - ArrayList intermediateTree = new ArrayList(); - String currentTaxa = ""; - int speedIndex = 0; - for(tNode n : tree){ - for(String s : n.taxa){ - if(currentTaxa.indexOf(s) == -1){ - currentTaxa += s; - ArrayList taxa = new ArrayList(); - taxa.add(s); - double height = n.getHeight() + speedTree.get(speedIndex).getHeight()*minTime; - intermediateTree.add(new tNode(taxa, height)); - } - } - speedIndex++; - } - for(int i = 0; i< tree.size(); i++){ - double height = tree.get(i).getHeight() + (speedTree.get(i).getHeight() * minTime); - treeAtTime.add(new tNode(tree.get(i).getTaxa(),height )); //can sort out the taxa in the node later - } - return treeAtTime; - } - - public static ArrayList tree2Mod(ArrayList tree1, ArrayList speedTree){ - ArrayList tree2 = new ArrayList(); - for(int i = 0; i reorderTree2(ArrayList tree){ - tree.add(tree.get(1)); - tree.add(tree.get(0)); - tree.add(tree.get(2)); - tree.remove(2); - tree.remove(1); - tree.remove(0); - return tree; - } - - //generate minTime - public static double findMinTime(ArrayList timeSwapTree){ - double minTime = 1.0; - for(int i = 0 ; i 0.0000000001){ - minTime = nextTime; - } - } - return minTime; - } - - //generate tree2Ordered - public static ArrayList tree2Ordered(ArrayList tree1, ArrayList speedTree){ - ArrayList tree2Ordered = new ArrayList(); - for(int i=0; i modifiedSpeedTree(ArrayList tree1, ArrayList tree2, int t1Move, int t2Move, int t3Move, ArrayList oldSpeed){ - ArrayList speedTree = new ArrayList(); - double t1Speed = 0, t2Speed = 0, t3Speed = 0; - double t1 = tree1.get(0).getHeight(); - double t2 = tree1.get(1).getHeight(); - double t3 = tree1.get(2).getHeight(); - double t1Dash = tree2.get(0).getHeight(); - double t2Dash = tree2.get(1).getHeight(); - double t3Dash = tree2.get(2).getHeight(); - switch (t1Move) { - case 0: //t1 -> t3' - t1Speed = t3Dash - t1; - break; - case 1: //t1 -> t1' - t1Speed = t1Dash - t1; - break; - case 2: //t1 -> t3'+t1'/2 - t1Speed = ((t3Dash + t1Dash)/2.0) - t1; - break; - case 3: //t1 -> absMax(t3'-t1,t2'-t1) - if(Math.abs(t3Dash - t1) > Math.abs(t1Dash -t1)){ - t1Speed = t3Dash - t1; - } else { - t1Speed = t1Dash - t1; - } - break; - case 4: //t1 - > min - if(Math.abs(t3Dash - t1) <= Math.abs(t1Dash - t1)){ - if(Math.abs(t3Dash -t1) <= Math.abs((t3Dash + t1Dash)/2.0 - t1)){ - t1Speed = t3Dash - t1; - } else { - t1Speed = (t3Dash + t1Dash)/2.0 - t1; - } - }else { - if(Math.abs(t1Dash - t1) <= Math.abs((t3Dash + t1Dash)/2.0 - t1)){ - t1Speed = t1Dash - t1; - } else { - t1Speed = (t3Dash + t1Dash)/2.0 - t1; - } - } - break; - default : - t1Speed = oldSpeed.get(0).getHeight(); - break; - } - - switch (t2Move){ - case 0: - t2Speed = t3Dash - t2; - break; - case 1: //Stage1 - t2Speed = t1Dash - t2; - break; - case 2: - t2Speed = (t3Dash + t1Dash)/2.0 - t2; - break; - case 3: - if(Math.abs(t3Dash - t2) >= Math.abs(t1Dash - t3)){ - t2Speed = t3Dash - t2; - } else { - t2Speed = t1Dash - t2; - } - break; - case 4: - if(Math.abs(t3Dash - t2) <= Math.abs(t1Dash - t2)){ - if(Math.abs(t3Dash - t2) <= Math.abs((t3Dash+t1Dash)/2.0 - t2)){ - t2Speed = t3Dash - t2; - } else { - t2Speed = (t3Dash + t1Dash)/2.0 - t2; - } - } else { - if(Math.abs(t1Dash - t2) <= Math.abs((t1Dash+t3Dash)/2.0 - t2)){ - t2Speed = t1Dash - t2; - }else { - t2Speed = (t1Dash + t3Dash)/2.0 - t2; - } - } - break; - case 5: //t2 -> t2; Stage1 - t2Speed = 0.0; - break; - case 6: //Stage2 t2 -> t1' - t2Speed = t2Dash - t2; - break; - default: - t2Speed = oldSpeed.get(1).getHeight(); - break; - } - switch (t3Move){ - - //stage2 : -> - case 0: // t3 -> t3' - t3Speed = t3Dash - t3; - break; - case 1: //t3 -> t2' - t3Speed = t1Dash - t3; - break; - case 2: //t3 -> t3'+t2'/2 - t3Speed = ((t3Dash + t1Dash)/2.0) - t3; - break; - case 3: //t3 - > max(of abs case 0-1, I don't believe case2 can ever be greater than 0 or 1) - if(Math.abs(t3Dash - t3) >= Math.abs(t1Dash -t3)){ - t3Speed = t3Dash - t3; - } else { - t3Speed = t1Dash - t3; - } - break; - case 4: //t3 --> min( of abs case 0-2) Here case2 can be less than 0 and 1 - if(Math.abs(t3Dash - t3) <= Math.abs(t1Dash - t3)){ - if(Math.abs(t3Dash -t3) <= Math.abs((t3Dash + t1Dash)/2.0 - t3)){ - t3Speed = t3Dash - t3; - } else { - t3Speed = (t3Dash + t1Dash)/2.0 - t3; - } - }else { - if(Math.abs(t1Dash - t3) <= Math.abs((t3Dash + t1Dash)/2.0 - t3)){ - t3Speed = t1Dash - t3; - } else { - t3Speed = (t3Dash + t1Dash)/2.0 - t3; - } - } - break; - case 5: //t3 -> t1'; Stage1 - t3Speed = t2Dash - t3; - break; - default: - t3Speed = oldSpeed.get(2).getHeight(); - break; - } - - speedTree.add(new tNode(tree1.get(0).getTaxa(), t1Speed)); - speedTree.add(new tNode(tree1.get(1).getTaxa(), t2Speed)); - speedTree.add(new tNode(tree1.get(2).getTaxa(), t3Speed)); - - return speedTree; - } -} diff --git a/tSpace/tTree.java b/tSpace/tTree.java deleted file mode 100644 index e69de29..0000000