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
예제 #2
0
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
예제 #3
0
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

    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 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)