def addCoulombBasePair(object1, object2, value): """ Adding the Coulomb interaction (a base pair): the lower pKa is lowered """ label1 = object1.label label2 = object2.label if object1.pKa_mod < object2.pKa_mod: newDeterminant = Determinant(label2, -value) object1.determinants[2].append(newDeterminant) else: newDeterminant = Determinant(label1, -value) object2.determinants[2].append(newDeterminant)
def addCoulombAcidPair(object1, object2, value): """ Adding the Coulomb interaction (an acid pair): the higher pKa is raised """ label1 = object1.label label2 = object2.label if object1.pKa_mod > object2.pKa_mod: newDeterminant = Determinant(label2, value) object1.determinants[2].append(newDeterminant) else: newDeterminant = Determinant(label1, value) object2.determinants[2].append(newDeterminant)
def setBackBoneBaseDeterminants(data_clump, version=None): """ adding back-bone determinants/perturbations for bases: Angle: atom1 -- atom2-atom3, i.e. C=O -- H-N(HIS) data_clump = [bases, CO] """ residues, interactions = data_clump for residue in residues: if residue.location != "BONDED": dpKa_max, cutoff = version.BackBoneParameters[residue.resType] for interaction in interactions: distance = 999. atom1 = interaction[1] atoms = residue.makeDeterminantAtomList("back-bone", version=version) for atom in atoms: current_distance = calculate.InterAtomDistance(atom1, atom) if current_distance < distance: atom2 = atom distance = current_distance if distance < cutoff[1]: if residue.resType in version.angularDependentSideChainInteractions: atom3 = residue.getThirdAtomInAngle(atom2) distance, f_angle, nada = calculate.AngleFactorX(atom1, atom2, atom3) else: f_angle = 1.0 if f_angle > 0.001: # add determinant label = "%s%4d%2s" % (atom2.resName, atom2.resNumb, atom2.chainID) value = residue.Q * calculate.HydrogenBondEnergy(distance, dpKa_max, cutoff, f_angle) newDeterminant = Determinant(label, value) residue.determinants[1].append(newDeterminant)
def setBackBoneAcidDeterminants(data_clump, version=None): """ adding back-bone determinants/perturbations for acids: Angle: atom1 -- atom2-atom3, i.e. COO -- H-N data_clump = [acids, NH] """ residues, interactions = data_clump for residue in residues: if residue.location != "BONDED": dpKa_max, cutoff = version.BackBoneParameters[residue.resType] for interaction in interactions: atom2 = interaction[1] atom3 = interaction[0] atoms = residue.makeDeterminantAtomList("back-bone", version=version) shortest_distance = 999. for atom in atoms: distance = calculate.InterAtomDistance(atom, atom2) if distance < shortest_distance: shortest_distance = distance atom1 = atom distance, f_angle, nada = calculate.AngleFactorX(atom1, atom2, atom3) if distance < cutoff[1] and f_angle > 0.001: label = "%s%4d%2s" % (atom2.resName, atom2.resNumb, atom2.chainID) value = residue.Q * calculate.HydrogenBondEnergy(distance, dpKa_max, cutoff, f_angle) newDeterminant = Determinant(label, value) residue.determinants[1].append(newDeterminant)
def addCoulombIonPair(object1, object2, value): """ Adding the Coulomb interaction (an acid-base pair): the pKa of the acid is lowered & the pKa of the base is raised """ label1 = object1.label label2 = object2.label # residue1 Q1 = object1.Q newDeterminant = Determinant(label2, Q1*value) object1.determinants[2].append(newDeterminant) # residue2 Q2 = object2.Q newDeterminant = Determinant(label1, Q2*value) object2.determinants[2].append(newDeterminant)
def simplex_distance(self, support, sample): '''Calculate cosine distance among all k examples with respect to m sample Args: support: [way*shot, D] sample: [quiry, D] Returns: dists: [way, shot, quiry] same for examples in a same category ''' support = support.view(self.way, self.shot, 1600) dists = [] for s in range(self.way): matrix_A = support[s, 1:].sub(support[s, 0].repeat(self.shot - 1, 1)) # shot-1, D matrix_A_trans = matrix_A.permute(1, 0) # D, shot-1 matrix_A = matrix_A.unsqueeze(0).bmm(matrix_A_trans.unsqueeze( 0)).squeeze() # self.shot-1, self.shot-1 Volume_A = Determinant(matrix_A) # 0 # print (Volume_A, 'Volume_A') for q in range(self.quiry): matrix_B = support[s].sub(sample[q].repeat(self.shot, 1)) # print (matrix) matrix_B_trans = matrix_B.permute(1, 0) # D, way*shot matrix_B = matrix_B.unsqueeze(0).bmm( matrix_B_trans.unsqueeze(0)).squeeze() # print (matrix) # print (Determinant(matrix)) Volume_B = Determinant(matrix_B) dists.append(Volume_B / Volume_A) dists = torch.stack(dists).squeeze().view( self.way, self.quiry).unsqueeze(1).repeat(1, self.shot, 1) # way, shot, quiry # print (dists) # print (torch.max(dists)) '''numpy''' # max_values = torch.max(dists).data.cpu().numpy()[0] # print (max_values) '''repeat tensor''' # max_values = torch.max(dists).unsqueeze(0).unsqueeze(0).repeat(self.way, self.shot, self.quiry) # dists = dists / (max_values.repeat(1, self.way*self.shot)) # dists = max_values / dists dists = dists.view(-1, self.quiry) return dists
def addSidechainDeterminants(residue1, residue2, version=None): """ adding side-chain determinants/perturbations Note, resNumb1 > resNumb2 """ distance = 999.0 closest_atom1 = None closest_atom2 = None atoms1 = residue1.makeDeterminantAtomList(residue2.resName, version=version) atoms2 = residue2.makeDeterminantAtomList(residue1.resName, version=version) for atom1 in atoms1: for atom2 in atoms2: # select the smallest inter-atom distance current_distance = calculate.InterAtomDistance(atom1, atom2) if current_distance < distance: closest_atom1 = atom1 closest_atom2 = atom2 distance = current_distance dpka_max, cutoff = version.SideChainParameters[residue1.resType][ residue2.resType] if distance < cutoff[1]: if residue2.resType in version.angularDependentSideChainInteractions: atom3 = residue2.getThirdAtomInAngle(closest_atom2) distance, f_angle, nada = calculate.AngleFactorX( closest_atom1, closest_atom2, atom3) elif residue1.resType in version.angularDependentSideChainInteractions: atom3 = residue1.getThirdAtomInAngle(closest_atom1) distance, f_angle, nada = calculate.AngleFactorX( closest_atom2, closest_atom1, atom3) else: # i.e. no angular dependence f_angle = 1.0 weight = version.calculatePairWeight(residue1.Nmass, residue2.Nmass) exception, value = version.checkExceptions(residue1, residue2) #exception = False # circumventing exception if exception == True: """ do nothing, value should have been assigned """ #pka_print(" exception for %s %s %6.2lf" % (residue1.label, residue2.label, value)) else: value = version.calculateSideChainEnergy(distance, dpka_max, cutoff, weight, f_angle) if residue1.Q == residue2.Q: # acid pair or base pair if residue1.pKa_mod < residue2.pKa_mod: newDeterminant1 = Determinant(residue2.label, -value) newDeterminant2 = Determinant(residue1.label, value) else: newDeterminant1 = Determinant(residue2.label, value) newDeterminant2 = Determinant(residue1.label, -value) else: newDeterminant1 = Determinant(residue2.label, value * residue1.Q) newDeterminant2 = Determinant(residue1.label, value * residue2.Q) if residue1.resName not in version.exclude_sidechain_interactions: residue1.determinants[0].append(newDeterminant1) if residue2.resName not in version.exclude_sidechain_interactions: residue2.determinants[0].append(newDeterminant2)
def setIonDeterminants(protein, version=None): """ adding ion determinants/perturbations """ ionizable_residues = lib.residueList("propka1") for residue in protein.propka_residues: if residue.resName in ionizable_residues: for ion in protein.residue_dictionary["ION"]: distance = calculate.InterResidueDistance(residue, ion) if distance < version.coulomb_cutoff[1]: label = "%s%4d%2s" % (ion.resName, ion.resNumb, ion.chainID) weight = version.calculatePairWeight(residue.Nmass, ion.Nmass) # the pKa of both acids and bases are shifted up by negative ions (and vice versa) value = (-ion.Q) * version.calculateCoulombEnergy(distance, weight) newDeterminant = Determinant(label, value) residue.determinants[2].append(newDeterminant)
def addDeterminants(iterative_interactions, version, options=None): """ The iterative pKa scheme. Later it is all added in 'calculateTotalPKA' """ # --- setup --- iteratives = [] done_residue = [] #debug.printIterativeDeterminants(iterative_interactions) # creating iterative objects with references to their real residue counterparts for interaction in iterative_interactions: pair = interaction[0] for residue in pair: if residue in done_residue: #print "done already" """ do nothing - already have an iterative object for this residue """ else: newIterative = Iterative(residue) iteratives.append(newIterative) done_residue.append(residue) # Initialize iterative scheme if options.print_iterations == True: pka_print("\n --- pKa iterations (%d residues, %d interactions) ---" % ( len(iteratives), len(iterative_interactions) )) converged = False iteration = 0 for itres in iteratives: itres.pKa_iter.append(itres.pKa_NonIterative) # --- starting pKa iterations --- while converged == False: # initialize pKa_new iteration += 1 for itres in iteratives: itres.determinants = [[], [], []] itres.pKa_new = itres.pKa_NonIterative # Adding interactions to temporary determinant container for interaction in iterative_interactions: pair = interaction[0] values = interaction[1] annihilation = interaction[2] #print "len(interaction) = %d" % (len(interaction)) object1, object2 = findIterative(pair, iteratives) Q1 = object1.Q Q2 = object2.Q if Q1 < 0.0 and Q2 < 0.0: """ both are acids """ addIterativeAcidPair(object1, object2, interaction) elif Q1 > 0.0 and Q2 > 0.0: """ both are bases """ addIterativeBasePair(object1, object2, interaction) else: """ one of each """ addIterativeIonPair(object1, object2, interaction, version) # Calculating pKa_new values for itres in iteratives: for type in range(0,3): for determinant in itres.determinants[type]: itres.pKa_new += determinant[1] # Check convergence converged = True for itres in iteratives: if itres.pKa_new == itres.pKa_old: itres.converged = True else: itres.converged = False converged = False # reset pKa_old & storing pKa_new in pKa_iter for itres in iteratives: itres.pKa_old = itres.pKa_new itres.pKa_iter.append(itres.pKa_new) if iteration == 10: pka_print("did not converge in %d iterations" % (iteration)) break # --- Iterations finished --- # printing pKa iterations if options.print_iterations == True: str = "%12s" % (" ") for index in range(0, iteration+1 ): str += "%8d" % (index) pka_print(str) for itres in iteratives: str = "%s " % (itres.label) for pKa in itres.pKa_iter: str += "%8.2lf" % (pKa) if itres.converged == False: str += " *" pka_print(str) # creating real determinants and adding them to residue object for itres in iteratives: for type in range(0,3): for interaction in itres.determinants[type]: value = interaction[1] if value > 0.005 or value < -0.005: label = interaction[0] newDeterminant = Determinant(label, value) itres.residue.determinants[type].append(newDeterminant)