def updateReconciliation(guiltyTransferList, HostTree, ParasiteTree, reconciliation): """This function takes as input a list guiltyTransferList of transfers that are responsible for cycles, a host tree and parasite tree, and the original reconciliation of host and parasite. It returns a new reconciliation in which all of the guilty transfers have been marked as a new event 'GT'.""" parents = createParentsDict(HostTree, ParasiteTree) newReconciliation = copy.deepcopy(reconciliation) for transfer in guiltyTransferList: for key in newReconciliation: if reconciliation[key][0] == 'T': # check if transfer and key are the same event if transfer[0] == key[0] and (transfer[2] == key[1] or transfer[4] == key[1]): # create a new event 'GT' instead of 'T' newValue = ['GT'] + newReconciliation[key][1:] # replace the old event with newValue in the reconciliation newReconciliation[key] = newValue return newReconciliation
def buildReconciliation(HostTree, ParasiteTree, reconciliation): """Takes as input a host tree, a parasite tree, and a reconciliation, and returns a graph where the keys are host or parasite nodes, and the values are a list of the children of a particular node. The graph represents temporal relationships between events. The function also returns a list transferList containing all the transfers in the reconciliation in the form """ # create a dictionary with a list of parents of each host and parasite node parents = createParentsDict(HostTree, ParasiteTree) H = treeFormat(HostTree) P = treeFormat(ParasiteTree) cycleCheckingGraph = H cycleCheckingGraph.update(P) transferList = [] for key in reconciliation: # deal with transfer case: if reconciliation[key][0] == 'T': # add the children of the parasite node to the list of children # of the host node in cycleCheckingGraph cycleCheckingGraph[key[0]] = P[key[0]] + \ [reconciliation[key][1][1], reconciliation[key][2][1]] # find the parents of the take-off and landing host nodes parent1 = parents[reconciliation[key][1][1]] parent2 = parents[reconciliation[key][2][1]] # add the parasite node as a child of parent1 and parent2 cycleCheckingGraph[parent1] = cycleCheckingGraph[parent1] + \ [key[0]] cycleCheckingGraph[parent2] = cycleCheckingGraph[parent2] + \ [key[0]] transferEdge1 = reconciliation[key][1][1] transferEdge2 = reconciliation[key][2][1] transferList.append([key[0], parent1, transferEdge1, parent2, \ transferEdge2]) # deal with speciation case: elif reconciliation[key][0] == 'S': parent = parents[key[0]] if parent != 'Top': cycleCheckingGraph[parent] = cycleCheckingGraph[parent] + \ [key[1]] cycleCheckingGraph[key[1]] = cycleCheckingGraph[key[1]] + \ cycleCheckingGraph[key[0]] # deal with duplication case: elif reconciliation[key][0] == 'D': parent = parents[key[1]] if parent != 'Top': cycleCheckingGraph[parent] = cycleCheckingGraph[parent] + \ [key[0]] cycleCheckingGraph[key[0]] = cycleCheckingGraph[key[0]] + [key[1]] # deal with contemporary case: elif reconciliation[key][0] == 'C': cycleCheckingGraph[key[1]] = [None] cycleCheckingGraph[key[0]] = [None] for key in cycleCheckingGraph: cycleCheckingGraph[key] = list(set(cycleCheckingGraph[key])) return cycleCheckingGraph, transferList