def calulateAllCombinations(comb, allowedTable, lenDrlRows): ''' Calculates the total sum of the differences in upper bound of distance restraints for the given combinations. The lower the sum, the less information is lost. ''' munk = Munkres() totalvalue = 0 table1 = [] table2 = [] count = 0 for j in range(lenDrlRows): if count < len(comb) and comb[count] == j: table1.append(allowedTable[j]) count += 1 else: table2.append(allowedTable[j]) #end if #end for indexes1 = munk.compute(table1) for row, column in indexes1: value = table1[row][column] totalvalue += value #end for indexes2 = [] for k in range(len(table2)): mini = min(table2[k]) for ii, jj in enumerate(table2[k]): #finding column index for the lowest value in the row. if jj == mini: columnind = ii #end if #end for indexes2.append((k, columnind)) #end for #indexes2=munk.compute(table2) #I used this before I realised that taking the minima will give a better result. for row, column in indexes2: value = table2[row][column] totalvalue += value #end for return(totalvalue, indexes1, indexes2, table1, table2)
def massageRestraintsForLeu(prl, proj, prlleu, projleu, threshold, deasHB): ''' This is an overall script which coordinates through all other functions. prl =project created in rotateLeucines.py proj =project you want to change prlleu and projleu are the leucine objects in cing. threshold is the threshold for the violations if deasHB=True, all HB's of the specified leucine will be deassigned. Return True on error. ''' if prlleu.resNum != projleu.resNum: nTerror('Residuenumbers %s and %s are not the same.' % (prlleu.resNum, projleu.resNum)) #end if if deasHB == True: #if HB's needs to be deassigned _deassHBaplistproj = deassignHB(proj, projleu) _deassHBaplistprl = deassignHB(prl, prlleu)#just to be able to compare the two projects later on #end if resultClassification = classifyRestraints(prl, prlleu, threshold) if not resultClassification: nTerror("Failed classifyRestraints") return True # end if n, _u, trdict, gpdict = resultClassification drlColumnsdict, drlColumns, lenDrlColumns, drlRowsdict, drlRows, lenDrlRows = renameDicts(trdict, gpdict) invdrlColumnsdict = reverseDict(drlColumnsdict) invdrlRowsdict = reverseDict(drlRowsdict) table = makeDifferenceTable(drlColumns, drlRows, lenDrlColumns, lenDrlRows) allowedTable, maxi = makeAllowedTable(table, drlColumns, drlRows, lenDrlColumns, lenDrlRows) allowedTable, table, lenDrlRows, lenDrlColumns, drlRows, drlColumns, n = checkAllowedTable(allowedTable, table, maxi, n, lenDrlColumns, lenDrlRows, drlColumns, drlRows, invdrlColumnsdict, invdrlRowsdict) if lenDrlRows < lenDrlColumns: drlColumnsdict, drlRowsdict = interChange(drlColumnsdict, drlRowsdict) lenDrlColumns, lenDrlRows = interChange(lenDrlColumns, lenDrlRows) drlColumns, drlRows = interChange(drlColumns, drlRows) table = transposeTable(table) allowedTable = transposeTable(allowedTable) #end if stringTable = tablePrint(table, 5) nTmessage('Difference table is:\n%s' % stringTable) stringAllowedTable = tablePrint(allowedTable, 15) nTmessage('Allowed table is:\n%s' % stringAllowedTable) if False: #this is the old version of the algorithm; works faster, but gives less optimal results. munk = Munkres() #algorithm to make combinations of lowest costs _indexes = munk.compute(allowedTable) #end if if allowedTable != [[]]: #checks whether there are allowed combinations. indexes, totalValueNew = calculateIndexes(allowedTable, lenDrlColumns, lenDrlRows) #Code below was only used to set the indexes of the table by hand. #if projleu.resNum==589: # indexes=[(0,0),(1,3),(2,1),(3,2),(4,4),(5,6),(6,5),(7,7),(8,7),(9,10),(10,9),(11,12),(12,11),(13,11), # (14,12),(15,8),(16,13),(17,14),(18,14),(19,15),(20,8),(21,8)] #elif projleu.resNum==596: # indexes=[(0,1),(1,0),(2,0),(3,3),(4,4),(5,5),(6,12),(7,2),(8,6),(9,8),(10,7),(11,9),(12,10),(13,11)] #elif projleu.resNum==618: # indexes=[(0,0),(1,1)] #totalValueNew='INF' n, rows, columns, _values = checkIndexes(indexes, allowedTable, table, maxi, n, invdrlColumnsdict, invdrlRowsdict, drlColumns, drlRows, lenDrlRows) if totalValueNew != 'INF': nTmessage('total sum of differences between upper bounds of combined restraints is %.2f A.' % (float(totalValueNew) / 1000)) #end if #n=checkDeasrHB(n,deassHBaplistproj) Not necessary anymore since distances are recalculated after deassignment HB's. restraintPairDict = makeRestraintPairDict(invdrlRowsdict, invdrlColumnsdict, drlRows, drlColumns, rows, columns) disRList, delList = makeDisRList(restraintPairDict, proj) proj = deleteRestraints(delList, proj) proj = appendRestraints(disRList, proj) #end if deassignRestraints(n, proj, projleu, delDeassRestr=True)
def munkIndexes(table): '''Calculate combination of lowest elements in table''' munk = Munkres() indexes = munk.compute(table) return indexes