def findCenters(newickFile, switchLo, switchHi, lossLo, lossHi): """This function takes as input a .newick file in the form <filename>.newick, and low and high values for costscape for both switches and losses. It returns a list of the centroids of each region in the costscape associated with the given .newick file.""" hostTree, parasiteTree, phi = newickFormatReader(newickFile) CVlist = reconcile.reconcile(parasiteTree, hostTree, phi, switchLo, switchHi, lossLo, lossHi) coordList = plotcosts.plotcosts(CVlist, lossLo, lossHi, switchLo, switchHi, "", False, False) polygonList = getNewCoordList(newickFile, switchLo, switchHi, lossLo, lossHi) pointList = [] for i in range(len(polygonList)): point = polygonList[i] numCommas = point.count(",") if numCommas > 1: # polygon case region = load_wkt(point) pointList.append(region.centroid.wkt) elif numCommas == 1: # line case x1 = coordList[i][0][0] y1 = coordList[i][0][1] x2 = coordList[i][1][0] y2 = coordList[i][1][1] midx = (x1 + x2) * 1.0 / 2 midy = (y1 + y2) * 1.0 / 2 pointList.append("POINT ({} {})".format(str(midx), str(midy))) else: # point case pointList.append("POINT {}".format(str(coordList[i][0]))) return pointList
def newickToTreeParser(newickFile): try: outFile = tempfile.NamedTemporaryFile(delete=False) host, parasite, phi = newickFormatReader(newickFile) H = treeFormat(host) P = treeFormat(parasite) H_dict = {} # name:index P_dict = {} # name:index count = 0 for key in H: count += 1 H_dict[key] = count for key in P: count += 1 P_dict[key] = count outFile.write("HOSTTREE\n") for key in H: outFile.write(str(H_dict[key]) + "\t") if H[key] == [None, None]: outFile.write("null\tnull\n") else: outFile.write(str(H_dict[H[key][0]]) + "\t" + str(H_dict[H[key][1]]) + "\n") outFile.write("\nHOSTNAMES\n") for key in H: outFile.write(str(H_dict[key]) + "\t" + key + "\n") outFile.write("\nPARASITETREE\n") for key in P: outFile.write(str(P_dict[key]) + "\t") if P[key] == [None, None]: outFile.write("null\tnull\n") else: outFile.write(str(P_dict[P[key][0]]) + "\t" + str(P_dict[P[key][1]]) + "\n") outFile.write("\nPARASITENAMES\n") for key in P: outFile.write(str(P_dict[key]) + "\t" + key + "\n") outFile.write("\nPHI\n") for key in phi: outFile.write(str(H_dict[phi[key]]) + "\t" + str(P_dict[key]) + "\n") outFile.close() return outFile.name except CheetaError: raise except: raise CheetaError(CheetaErrorEnum.FileParse, newickFile, "Could not parse file - Check to see file formatting is correct")
def reconcile(fileName, D, T, L): """Takes as input a newick file, FileName, a dupliction cost, a transfer cost, and a loss cost. This uses newickFormatReader to extract the host tree, parasite tree and tip mapping from the file and then calls DP to return the DTL reconciliation graph of the provided newick file""" try: host, paras, phi = newickFormatReader(fileName) return DP(host, paras, phi, D, T, L) except CheetaError: raise except: raise CheetaError(CheetaErrorEnum.Alg), None, sys.exc_info()[2]
def getNewCoordList(newickFile, switchLo, switchHi, lossLo, lossHi): """Takes as input a newick file in the form <filename>.newick, and low and high values for costscape for both switches and losses. Returns a list of strings, where each string contains all the verteces of one region from costscape.""" hostTree, parasiteTree, phi = newickFormatReader(newickFile) CVlist = reconcile.reconcile(parasiteTree, hostTree, phi, switchLo, switchHi, lossLo, lossHi) coordList = plotcosts.plotcosts(CVlist, lossLo, lossHi, switchLo, switchHi, "", False, False) newCoordList = [] for vertexList in coordList: string = "POLYGON((" for vertex in vertexList: string = "{}{} {},".format(string, str(vertex[0]), str(vertex[1])) string = "{}))".format(string[:-1]) newCoordList.append(string) return newCoordList
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