Esempio n. 1
0
 /**
  * Initialise a CTMain before usage with a first tree, going through and setting up the hash table
  * etc.
  *
  * @param root Root of the initial tree to initialise with
  * @param noOfSamples Current number of samples taken
  */
 public void initialize(TreeNode root, int noOfSamples) {
   // Parameter initialisation
   this.noOfSamples = noOfSamples;
   noOfTrees = 0;
   // TaxaMap initialisation
   List<TreeNode> leaves = root.getLeaves();
   noOfTaxa = leaves.size();
   // Hash initialisation
   hashUtils = new HashUtils();
   hashUtils.initialize(noOfTaxa, noOfSamples, C, seed);
   hashTable = new HashTable(hashUtils.m1);
   // Taxamap initialisation
   taxa = new TaxaMap(noOfTaxa);
   for (int i = 0; i < leaves.size(); i++) {
     taxa.put(leaves.get(i).name, i);
   }
   leafEdgeLengths = new double[noOfTaxa];
   // Adds a single star partition, once and for all.
   BitSet star = new BitSet(noOfTaxa);
   star.flip(0, noOfTaxa);
   HashEntry entry = new HashEntry(-1, star, 0.0d);
   entry.count = noOfSamples + 1;
   partitions.add(entry);
   // Majority threshold initialisation
   updateInterestThreshold();
 }
Esempio n. 2
0
 /**
  * Find the taxon node in the network.
  *
  * @param network The network to find the taxon in
  * @param taxonRef Integer representation of taxon in the TaxaMap
  */
 public CNetworkNode TaxonRefToNode(int taxonRef, CNetwork network) {
   // function to return the node that is of the taxon
   for (CNetworkNode currentNode : network.nodes) {
     if (currentNode.Taxaname == taxa.getName(taxonRef)) {
       return currentNode;
     }
   }
   // Notify that method failed... return null...
   System.out.println("Error - taxon reference not found.  Maybe duplicate names?");
   return null;
 }
Esempio n. 3
0
 /**
  * Create partitions from an input tree - recursive so will be called by many nodes, beginning
  * with root but with calculations actually beginning on leaves.
  *
  * @param partitions Partitions in the form of entries in the hash table
  * @param curInterestPercentage Current percentage of interest that we want splits to occur above
  *     to view in the network later
  */
 private ArrayList<Cluster> constructClusters(
     LinkedList<HashEntry> partitions, double curInterestPercentage) {
   ArrayList<Cluster> clusters = new ArrayList<Cluster>();
   // Thresholds for the current run of the cluster builder (c.f. the threshold for the partitions
   // list).
   double curInterestThreshold = (double) (noOfTrees * (curInterestPercentage / 100.0d));
   int majInterestThreshold = (int) (noOfTrees * (50.0 / 100.0d));
   for (Iterator<HashEntry> it = partitions.iterator(); it.hasNext(); ) {
     HashEntry entry = it.next();
     // Checks if this partition is still above threshold... partition.
     // If not: remove it - O(1).
     if ((double) entry.count <= curInterestThreshold) {
       // correct the isMajority flag to now refer to majority not if of interest or not..
       if (entry.count <= majInterestThreshold) {
         entry.isMajority = false;
       }
       it.remove();
       continue;
     }
     // Constructs clusters (list of TreeNode's for each set bit) from each partition.
     if ((double) entry.count > curInterestThreshold) {
       Cluster cluster = new Cluster();
       if (entry.count > majInterestThreshold) {
         cluster.isMajority = true;
       }
       cluster.aboveSplit = entry.partition;
       cluster.noOfOccurrences = entry.count;
       cluster.edgeLength = entry.edgeLengthsSum / entry.count;
       for (int i = 0; i < entry.partition.size(); i++) {
         if (entry.partition.get(i)) {
           TreeNode node = new TreeNode(taxa.getName(i));
           node.edgeLength = leafEdgeLengths[i] / noOfTrees;
           cluster.add(node);
         }
       }
       clusters.add(cluster);
     }
   }
   // Sort by number of taxa.
   // TODO: This might obviously be optimized a bit, e.g. with a PriorityQueue. - Eiriksson
   Collections.sort(clusters);
   return clusters;
 }
Esempio n. 4
0
  /**
   * FOR TESTING ONLY Function called to initialise the Network Tester hash table & rest of CTMain
   *
   * @param noOfTestTaxa Number of taxa
   * @param noOfTestSamples number of trees input
   */
  public void InitialiseNetworkTester(int noOfTestTaxa, int noOfTestSamples) {
    // TaxaMap initialisation
    noOfTrees = 0;
    noOfTaxa = noOfTestTaxa;
    noOfSamples = noOfTestSamples;
    // Hash initialisation
    hashUtils = new HashUtils();
    hashUtils.initialize(noOfTaxa, noOfSamples, C, seed);
    hashTable = new HashTable(hashUtils.m1);
    leafEdgeLengths = new double[noOfTaxa];
    taxa = new TaxaMap(noOfTaxa);
    for (int i = 0; i < noOfTaxa; i++) {
      taxa.put("" + i, i);
      leafEdgeLengths[i] = 1;
    }

    // Adds a single star partition, once and for all.
    BitSet star = new BitSet(noOfTaxa);
    star.flip(0, noOfTaxa);
    HashEntry entry = new HashEntry(-1, star, 0.0d);
    entry.count = noOfSamples + 1;
    partitions.add(entry);
    updateInterestThreshold();
  }
Esempio n. 5
0
  /**
   * FOR TESTING ONLY Print details from the network - call after it has been constructed!
   *
   * @param tree Standard consensus tree
   * @param network The network
   */
  public void printDetails(CTree tree, CNetwork network) {
    // Print out the clusters:
    System.out.println("ALL CLUSTERS:");
    for (int i = 0; i < tree.clusters.size(); i++) {
      System.out.println(
          i
              + " "
              + tree.clusters.get(i).aboveSplit.toString()
              + " "
              + tree.clusters.get(i).noOfOccurrences);
    }
    // Print out the tree copied into network format:
    System.out.println("TREE IN NETWORK, splits:");
    for (int i = 0; i < network.splits.size(); i++) {
      System.out.println(
          i
              + " "
              + network.splits.get(i).split.toString()
              + " "
              + network.splits.get(i).noOfOccurences);
      for (int j = 0; j < network.splits.get(i).edges.size(); j++) {
        System.out.println(
            j
                + " "
                + network.splits.get(i).edges.get(j).networkNodeA.Taxaname
                + " to "
                + network.splits.get(i).edges.get(j).networkNodeB.Taxaname);
      }
    }
    System.out.println("The following comes from the network format...");
    System.out.println("NODES:");
    for (int i = 0; i < network.nodes.size(); i++) {
      System.out.println(
          "" + network.nodes.get(i).hashCode() + " " + network.nodes.get(i).Taxaname);
    }

    System.out.println("EDGES:");
    for (int i = 0; i < network.splits.size(); i++) {
      for (int j = 0; j < network.splits.get(i).edges.size(); j++) {
        System.out.println(
            network.splits.get(i).split.toString()
                + ": "
                + network.splits.get(i).edges.get(j).networkNodeA.Taxaname
                + " TO "
                + network.splits.get(i).edges.get(j).networkNodeB.Taxaname);
      }
    }
    System.out.println("The following comes from the original tree...(!)");
    for (int i = 1; i < tree.clusters.size(); i++) {
      Cluster cluster2 = tree.clusters.get(i);
      // if(cluster2.isMajority == false){
      network.outputString = "1 ";
      for (TreeNode node : cluster2) {
        network.outputString += " " + (taxa.get(node.name) + 1);
      }
      network.outputString += ",";
      System.out.println(network.outputString);
      // }
    }
    // printing to a file
    try {
      network.PrintOut(1, noOfTaxa, taxa);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    // network in nex format
    try {
      network.PrintOut(2, noOfTaxa, taxa);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return;
  }
Esempio n. 6
0
 /**
  * Create partitions from an input tree - recursive so will be called by many nodes, beginning
  * with root but with calculations actually beginning on leaves.
  *
  * @param node Node in tree with above split to add to the hash table
  */
 private BitSet createPartitions(TreeNode node) {
   if (node.isLeaf()) { // Leaf node.
     int index = taxa.get(node.name);
     // leaf simply has it's keys stored in the hash utilities
     node.addProperty("tableHashKey", hashUtils.a1[index]);
     node.addProperty("bucketHashKey", hashUtils.a2[index]);
     // Updates the edge length array.
     leafEdgeLengths[index] += node.edgeLength;
     // Create a new partition from scratch to represent it
     BitSet partition = new BitSet(noOfTaxa);
     partition.set(index);
     assert partition.cardinality() == 1 : "There should be exactly a single bit set.";
     return partition;
   } else { // An internal node: Traverses the tree in post order.
     BitSet partition = new BitSet(noOfTaxa);
     // Get the node's partition representation from its children
     List<TreeNode> children = node.children;
     for (TreeNode child : children) {
       partition.or(createPartitions(child));
     }
     // if this node is NOT to the right side of the root then add it... (Avoid adding splits twice
     // for CNetworks)
     if (node.parent != root || root.getRight() != node) {
       noOfPartitions++;
       long tableKey = 0;
       long bucketKey = 0;
       long tableKey2 = 0;
       long bucketKey2 = 0;
       // Calculate the hash keys for this partition
       for (TreeNode child : children) {
         tableKey += child.getIntProperty("tableHashKey");
         bucketKey += child.getIntProperty("bucketHashKey");
       }
       // if first is one then we need to store the flipped version so we store each split in one
       // representation only.
       if (partition.get(0) == true) {
         // copy to a new partition that is the flipped version
         BitSet partitionF = new BitSet(noOfTaxa);
         for (int l = 0; l < noOfTaxa; l++) {
           if (partition.get(l) == false) partitionF.set(l);
         }
         // calculate the hash keys for the flipped partition...
         for (int k = 0; k < noOfTaxa; k++) {
           if (partitionF.get(k) == true) {
             tableKey2 += hashUtils.a1[k];
             bucketKey2 += hashUtils.a2[k];
           }
         }
         // store the properties in the node
         node.addProperty("tableHashKey", (int) (tableKey2 % hashUtils.m1));
         node.addProperty("bucketHashKey", (int) (bucketKey2 % hashUtils.m2));
         if (noOfPartitions < noOfTaxa - 2) { // Avoids the addition of the star partition
           hashTable.put(
               partitionF,
               node.edgeLength,
               node.getIntProperty("tableHashKey"),
               node.getIntProperty("bucketHashKey"),
               interestThreshold,
               partitions);
         }
         // remember to still return the original partition with its appropriate keys...
         node.addProperty("tableHashKey", (int) (tableKey % hashUtils.m1));
         node.addProperty("bucketHashKey", (int) (bucketKey % hashUtils.m2));
         return partition;
       }
       // if first is zero then simply add the partition, recursively calculating the hash
       else {
         node.addProperty("tableHashKey", (int) (tableKey % hashUtils.m1));
         node.addProperty("bucketHashKey", (int) (bucketKey % hashUtils.m2));
         if (noOfPartitions < noOfTaxa - 2) { // Avoids the addition of the star partition
           hashTable.put(
               partition,
               node.edgeLength,
               node.getIntProperty("tableHashKey"),
               node.getIntProperty("bucketHashKey"),
               interestThreshold,
               partitions);
         }
         return partition;
       }
     }
     // still return the partition even if node was not added as was right of root...
     return partition;
   }
 }