def detectCycles(HostTree, ParasiteTree, reconciliation): """This function takes as input the cycle checking graph, reconGraph. It returns a new version of reconGraph, newReconGraph, from which the transfer events responsible for the cycles have been removed. It also returns a list, guiltyTransferList, of the guilty transfers.""" hostTree = ReconciliationGraph.treeFormat(HostTree) parasiteTree = ReconciliationGraph.treeFormat(ParasiteTree) guiltyTransferList = [] markingDict = {} reconGraph, transferList = buildReconciliation(HostTree, ParasiteTree, reconciliation) Hroot = ReconciliationGraph.findRoot(HostTree) markingDict[Hroot] = ["check"] cycleNode = recurseChildren(reconGraph, markingDict, Hroot, parasiteTree) markingDict = {} newReconGraph, guiltyTransfer, transferList = deleteTransfer(reconGraph, transferList, cycleNode) if guiltyTransfer != []: guiltyTransferList.append(guiltyTransfer) while cycleNode != None: markingDict = {Hroot: ["check"]} cycleNode = recurseChildren(newReconGraph, markingDict, Hroot, parasiteTree) if cycleNode == None: for node in newReconGraph: if not checked(markingDict, node): check(markingDict, node) cycleNode = recurseChildren(newReconGraph, markingDict, node, parasiteTree) newReconGraph, guiltyTransfer, transferList = deleteTransfer(newReconGraph, transferList, cycleNode) if guiltyTransfer != []: guiltyTransferList.append(guiltyTransfer) return newReconGraph, guiltyTransferList, newReconGraph
def Reconcile(argList): """Takes command-line arguments of a .newick file, duplication, transfer, and loss costs, the type of scoring desired and possible switch and loss ranges. Creates Files for the host, parasite, and reconciliations""" fileName = argList[1] #.newick file D = float(argList[2]) # Duplication cost T = float(argList[3]) # Transfer cost L = float(argList[4]) # Loss cost freqType = argList[5] # Frequency type # Optional inputs if freqType == xscape switchLo = float(argList[6]) # Switch lower boundary switchHi = float(argList[7]) # Switch upper boundary lossLo = float(argList[8]) # Loss lower boundary lossHi = float(argList[9]) # Loss upper boundary host, paras, phi = newickFormatReader.getInput(fileName) hostRoot = ReconciliationGraph.findRoot(host) hostv = ReconciliationGraph.treeFormat(host) Order = orderGraph.date(hostv) # Default scoring function (if freqtype== Frequency scoring) DTLReconGraph, numRecon = DP.DP(host, paras, phi, D, T, L) #uses xScape scoring function if freqType == "xscape": DTLReconGraph = calcCostscapeScore.newScoreWrapper(fileName, switchLo, \ switchHi, lossLo, lossHi, D, T, L) #uses Unit scoring function elif freqType == "unit": DTLReconGraph = unitScoreDTL(host, paras, phi, D, T, L) DTLGraph = copy.deepcopy(DTLReconGraph) scoresList, rec = Greedy.Greedy(DTLGraph, paras) for n in range(len(rec)): graph = ReconciliationGraph.buildReconstruction(host, paras, rec[n]) currentOrder = orderGraph.date(graph) if currentOrder == "timeTravel": rec[n], currentOrder = detectCycles.detectCyclesWrapper(host, paras, rec[n]) currentOrder = orderGraph.date(currentOrder) hostOrder = hOrder(hostv,currentOrder) hostBranchs = branch(hostv,hostOrder) if n == 0: newickToVis.convert(fileName,hostBranchs, n, 1) else: newickToVis.convert(fileName,hostBranchs, n, 0) # filename[:-7] is the file name minus the .newick ReconConversion.convert(rec[n], DTLGraph, paras, fileName[:-7], n)
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.""" parents = ReconciliationGraph.parentsDict(HostTree, ParasiteTree) H = ReconciliationGraph.treeFormat(HostTree) P = ReconciliationGraph.treeFormat(ParasiteTree) reconGraph = H reconGraph.update(P) transferList = [] for key in reconciliation: if reconciliation[key][0] == 'T': reconGraph[key[0]] = P[key[0]] + [reconciliation[key][1][1], \ reconciliation[key][2][1]] parent1 = parents[reconciliation[key][1][1]] parent2 = parents[reconciliation[key][2][1]] reconGraph[parent1] = reconGraph[parent1] + [key[0]] reconGraph[parent2] = reconGraph[parent2] + [key[0]] transferEdge1 = reconciliation[key][1][1] transferEdge2 = reconciliation[key][2][1] transferList.append([key[0], parent1, transferEdge1, parent2, \ transferEdge2]) elif reconciliation[key][0] == 'S': parent = parents[key[0]] if parent != 'Top': reconGraph[parent] = reconGraph[parent] + [key[1]] reconGraph[key[1]] = reconGraph[key[1]] + reconGraph[key[0]] elif reconciliation[key][0] == 'D': parent = parents[key[1]] if parent != 'Top': reconGraph[parent] = reconGraph[parent] + [key[0]] reconGraph[key[0]] = reconGraph[key[0]] + [key[1]] elif reconciliation[key][0] == 'C': reconGraph[key[1]] = [None] reconGraph[key[0]] = [None] for key in reconGraph: reconGraph[key] = ReconciliationGraph.uniquify(reconGraph[key]) return reconGraph, transferList
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.""" parents = ReconciliationGraph.parentsDict(HostTree, ParasiteTree) H = ReconciliationGraph.treeFormat(HostTree) P = ReconciliationGraph.treeFormat(ParasiteTree) reconGraph = H reconGraph.update(P) transferList = [] for key in reconciliation: if reconciliation[key][0] == "T": reconGraph[key[0]] = P[key[0]] + [reconciliation[key][1][1], reconciliation[key][2][1]] parent1 = parents[reconciliation[key][1][1]] parent2 = parents[reconciliation[key][2][1]] reconGraph[parent1] = reconGraph[parent1] + [key[0]] reconGraph[parent2] = reconGraph[parent2] + [key[0]] transferEdge1 = reconciliation[key][1][1] transferEdge2 = reconciliation[key][2][1] transferList.append([key[0], parent1, transferEdge1, parent2, transferEdge2]) elif reconciliation[key][0] == "S": parent = parents[key[0]] if parent != "Top": reconGraph[parent] = reconGraph[parent] + [key[1]] reconGraph[key[1]] = reconGraph[key[1]] + reconGraph[key[0]] elif reconciliation[key][0] == "D": parent = parents[key[1]] if parent != "Top": reconGraph[parent] = reconGraph[parent] + [key[0]] reconGraph[key[0]] = reconGraph[key[0]] + [key[1]] elif reconciliation[key][0] == "C": reconGraph[key[1]] = [None] reconGraph[key[0]] = [None] for key in reconGraph: reconGraph[key] = ReconciliationGraph.uniquify(reconGraph[key]) return reconGraph, transferList
def Reconcile(argList): """Takes command-line arguments of a .newick file, duplication, transfer, and loss costs, the type of scoring desired and possible switch and loss ranges. Creates Files for the host, parasite, and reconciliations""" fileName = argList[1] # .newick file D = float(argList[2]) # Duplication cost T = float(argList[3]) # Transfer cost L = float(argList[4]) # Loss cost freqType = argList[5] # Frequency type # Optional inputs if freqType == xscape switchLo = float(argList[6]) # Switch lower boundary switchHi = float(argList[7]) # Switch upper boundary lossLo = float(argList[8]) # Loss lower boundary lossHi = float(argList[9]) # Loss upper boundary try: host, paras, phi = newickFormatReader(fileName) hostRoot = ReconciliationGraph.findRoot(host) # Default scoring function (if freqtype== Frequency scoring) DTLReconGraph, numRecon, cost = DP.DP(host, paras, phi, D, T, L) # uses xScape scoring function # if freqType == "xscape": # DTLReconGraph = calcCostscapeScore.newScoreWrapper(fileName, switchLo, \ # switchHi, lossLo, lossHi, D, T, L) # uses Unit scoring function if freqType == "unit": DTLReconGraph = unitScoreDTL(host, paras, phi, D, T, L) DTLGraph = copy.deepcopy(DTLReconGraph) scoresList, recs = Greedy.Greedy(DTLGraph, paras) infeasible_recs = [] for rec in recs: if orderGraph.date(ReconciliationGraph.buildReconciliation(host, paras, rec)) == False: infeasible_recs.append(rec) except CheetaError: raise except: raise CheetaError(CheetaErrorEnum.Alg), None, sys.exc_info()[2] return infeasible_recs, recs, cost
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 = ReconciliationGraph.parentsDict(HostTree, ParasiteTree) newReconciliation = copy.deepcopy(reconciliation) for transfer in guiltyTransferList: for key in newReconciliation.keys(): if reconciliation[key][0] == "T": if transfer[0] == key[0]: newReconciliation[key] = ["GT"] + reconciliation[key][1:] return newReconciliation
def convert(fileName, HostOrder, n, writeParasite): """takes name of original .newick file and the dictionary of host tree branch lengths and creates files for the host + parasite trees. Parasite tree can be ommited if desired""" f = open(fileName, "r") contents = f.read() host, paras, phi = newickFormatReader.getInput(fileName) hostRoot = ReconciliationGraph.findRoot(host) f.close() H, P, phi = contents.split(";") P = P.strip() H = H.strip() H = H + ";" host = treelib1.parse_newick(H, HostOrder) for key in HostOrder: H = H.replace(str(key), str(key) + ":" + str(HostOrder[key])) print "thing" f = open(fileName[:-7] + str(n) + ".stree", "w") treelib1.write_newick(host, f, root_data=True) f.close() if writeParasite: f = open(fileName[:-7] + ".tree", "w") f.write(P + ";") f.close()