Esempio n. 1
0
def ultrametricTree(distanceMatrix):
    graph = nx.Graph()
    for nodeId in range(len(distanceMatrix)):
        graph.add_node(nodeId, {'age':0,
                                'size':1.0})
    nodeIds = range(len(distanceMatrix))
    nextNode = len(distanceMatrix)
    #print('Added nodes')
    while len(distanceMatrix)> 1:
        i,j = findLowestElement(distanceMatrix)
        nodeAge = distanceMatrix[i][j]/2.0
        sizei = graph.node[nodeIds[i]]['size']
        sizej = graph.node[nodeIds[j]]['size']
        graph.add_node(nextNode, {'age': nodeAge, 'size': sizei + sizej})
        graph.add_edge(nodeIds[i], nextNode, {'weight': nodeAge - graph.node[nodeIds[i]]['age']})
        graph.add_edge(nodeIds[j], nextNode, {'weight': nodeAge - graph.node[nodeIds[j]]['age']})
        
        for ptr in range(len(distanceMatrix)):
            distanceMatrix[ptr].append( ((distanceMatrix[ptr][i]*sizei) + (distanceMatrix[ptr][j]*sizej))/(sizei+sizej))
        newElement = [((distanceMatrix[i][ptr]*sizei) + (distanceMatrix[j][ptr]*sizej))/(sizei+sizej) for ptr in range(len(distanceMatrix))]
        newElement.append(0)
        distanceMatrix.append(newElement)
        
        nodeIds.append(nextNode)
        nextNode += 1
        
        distanceMatrix = removeEntry(removeEntry(distanceMatrix, j),i)
        nodeIds.pop(j)
        nodeIds.pop(i)
        #print('Processed item')
        #print(distanceMatrix)
    return graph
Esempio n. 2
0
def hierarchicalClustering(nodeIDs, distanceMatrix):
    """
    """
    while len(nodeIDs) > 1:
        i,j = findLowestElement(distanceMatrix)
        newNodeId = ' '.join([nodeIDs[i], nodeIDs[j]])
        
        # Calculate the Davg entry and add to the Distance Matrix
        for ptr in range(len(distanceMatrix)):
            distanceMatrix[ptr].append( ((distanceMatrix[ptr][i]) + (distanceMatrix[ptr][j]))/2.0)
        newElement = [(distanceMatrix[i][ptr] + distanceMatrix[j][ptr])/2.0 for ptr in range(len(distanceMatrix))]
        newElement.append(0)
        distanceMatrix.append(newElement)
        nodeIDs.append(newNodeId)
        del nodeIDs[j]
        del nodeIDs[i]
        distanceMatrix = removeEntry(removeEntry(distanceMatrix, j),i)
        yield newNodeId
Esempio n. 3
0
def hierarchicalClustering(nodeIDs, distanceMatrix):
    """
    """
    while len(nodeIDs) > 1:
        i, j = findLowestElement(distanceMatrix)
        newNodeId = ' '.join([nodeIDs[i], nodeIDs[j]])

        # Calculate the Davg entry and add to the Distance Matrix
        for ptr in range(len(distanceMatrix)):
            distanceMatrix[ptr].append(
                ((distanceMatrix[ptr][i]) + (distanceMatrix[ptr][j])) / 2.0)
        newElement = [(distanceMatrix[i][ptr] + distanceMatrix[j][ptr]) / 2.0
                      for ptr in range(len(distanceMatrix))]
        newElement.append(0)
        distanceMatrix.append(newElement)
        nodeIDs.append(newNodeId)
        del nodeIDs[j]
        del nodeIDs[i]
        distanceMatrix = removeEntry(removeEntry(distanceMatrix, j), i)
        yield newNodeId
Esempio n. 4
0
def neighbourJoining(nodeIds, nextNode, distanceMatrix):
    """
    """
    if len(distanceMatrix) == 2:
        graph = nx.Graph()
        graph.add_edge(nodeIds[0], nodeIds[1],
                       {'weight': distanceMatrix[0][1]})
        return graph

    neighbourMatrix = constructNearestNeighbourMatrix(distanceMatrix)
    i, j = findMinimumElement(neighbourMatrix)
    deltaLength = (sum(distanceMatrix[i]) -
                   sum(distanceMatrix[j])) / (len(distanceMatrix) - 2)
    limbLength1 = (distanceMatrix[i][j] + deltaLength) / 2.0
    limbLength2 = (distanceMatrix[i][j] - deltaLength) / 2.0
    node1 = nodeIds[i]
    node2 = nodeIds[j]

    # Add new node to distance matrix
    for m in range(len(distanceMatrix)):
        distanceMatrix[m].append((distanceMatrix[m][i] + distanceMatrix[m][j] -
                                  distanceMatrix[i][j]) / 2.0)
    newRow = [
        (distanceMatrix[m][i] + distanceMatrix[m][j] - distanceMatrix[i][j]) /
        2.0 for m in range(len(distanceMatrix))
    ]
    newRow.append(0)
    distanceMatrix.append(newRow)
    nodeIds.append(nextNode)

    # Remove i & j elements
    distanceMatrix = removeEntry(removeEntry(distanceMatrix, j), i)
    nodeIds.pop(j)
    nodeIds.pop(i)

    graph = neighbourJoining(nodeIds, nextNode + 1, distanceMatrix)

    graph.add_edge(node1, nextNode, {'weight': limbLength1})
    graph.add_edge(node2, nextNode, {'weight': limbLength2})
    return graph
Esempio n. 5
0
def ultrametricTree(distanceMatrix):
    graph = nx.Graph()
    for nodeId in range(len(distanceMatrix)):
        graph.add_node(nodeId, {'age': 0, 'size': 1.0})
    nodeIds = range(len(distanceMatrix))
    nextNode = len(distanceMatrix)
    #print('Added nodes')
    while len(distanceMatrix) > 1:
        i, j = findLowestElement(distanceMatrix)
        nodeAge = distanceMatrix[i][j] / 2.0
        sizei = graph.node[nodeIds[i]]['size']
        sizej = graph.node[nodeIds[j]]['size']
        graph.add_node(nextNode, {'age': nodeAge, 'size': sizei + sizej})
        graph.add_edge(nodeIds[i], nextNode,
                       {'weight': nodeAge - graph.node[nodeIds[i]]['age']})
        graph.add_edge(nodeIds[j], nextNode,
                       {'weight': nodeAge - graph.node[nodeIds[j]]['age']})

        for ptr in range(len(distanceMatrix)):
            distanceMatrix[ptr].append(
                ((distanceMatrix[ptr][i] * sizei) +
                 (distanceMatrix[ptr][j] * sizej)) / (sizei + sizej))
        newElement = [((distanceMatrix[i][ptr] * sizei) +
                       (distanceMatrix[j][ptr] * sizej)) / (sizei + sizej)
                      for ptr in range(len(distanceMatrix))]
        newElement.append(0)
        distanceMatrix.append(newElement)

        nodeIds.append(nextNode)
        nextNode += 1

        distanceMatrix = removeEntry(removeEntry(distanceMatrix, j), i)
        nodeIds.pop(j)
        nodeIds.pop(i)
        #print('Processed item')
        #print(distanceMatrix)
    return graph
def neighbourJoining(nodeIds, nextNode, distanceMatrix):
    """
    """
    if len(distanceMatrix) == 2:
        graph = nx.Graph()
        graph.add_edge(nodeIds[0], nodeIds[1], {"weight": distanceMatrix[0][1]})
        return graph

    neighbourMatrix = constructNearestNeighbourMatrix(distanceMatrix)
    i, j = findMinimumElement(neighbourMatrix)
    deltaLength = (sum(distanceMatrix[i]) - sum(distanceMatrix[j])) / (len(distanceMatrix) - 2)
    limbLength1 = (distanceMatrix[i][j] + deltaLength) / 2.0
    limbLength2 = (distanceMatrix[i][j] - deltaLength) / 2.0
    node1 = nodeIds[i]
    node2 = nodeIds[j]

    # Add new node to distance matrix
    for m in range(len(distanceMatrix)):
        distanceMatrix[m].append((distanceMatrix[m][i] + distanceMatrix[m][j] - distanceMatrix[i][j]) / 2.0)
    newRow = [
        (distanceMatrix[m][i] + distanceMatrix[m][j] - distanceMatrix[i][j]) / 2.0 for m in range(len(distanceMatrix))
    ]
    newRow.append(0)
    distanceMatrix.append(newRow)
    nodeIds.append(nextNode)

    # Remove i & j elements
    distanceMatrix = removeEntry(removeEntry(distanceMatrix, j), i)
    nodeIds.pop(j)
    nodeIds.pop(i)

    graph = neighbourJoining(nodeIds, nextNode + 1, distanceMatrix)

    graph.add_edge(node1, nextNode, {"weight": limbLength1})
    graph.add_edge(node2, nextNode, {"weight": limbLength2})
    return graph