def generate_fixed_neighbours_constraint(self, outputPath, key="sequence_number", bondMapKey="atom_name", regressive=True): """ generates automatically RMC++ .fnc file from a pdbParser instance. RMC++ .cfg file atoms are ordered element wise. Therefore .fnc atoms indexes might not be the same as the given pdb records indexes but reordered to correspond to the .cfg file exported using convert_pdb method. In order to get a pdb file respecting the same order as .cfg file, one should convert the original pdb using convert_pdb then convert it back to pdb using convert method. :Parameters: #. outputPath (str): The RMC++ .fnc output configuration file path. #. key (str): Any pdbParser record valid key used to split records into molecules and generate bonds. #. bondMapKey (str): Any pdbParser record valid key used to generate bonds types. #. regressive (bool): Insures that a bond is calculated for both atoms of the same bond. If atom X is bonded to Y, normaly Y is in the bonds list of X, regressive insures that also X is in the bonds list of Y """ # get pdb records indexes element wise elements = self.get_pdb().elements types = set(elements) typesIndexes = [] for t in types: typesIndexes.extend( get_records_indexes_by_attribute_value(self.get_pdb().indexes, self.get_pdb(), "element_symbol", t) ) # get pdb pdb = pdbParser.pdbParser() pdb.set_name(self.get_pdb().name) pdb.records = [self.get_pdb().records[idx] for idx in typesIndexes] slicedPdb = pdbParser.pdbParser() # get molecules molecules = set(get_records_attribute_values(pdb.indexes, pdb, key)) # build bonds centralRecord = [] connectedTo = [] for mol in molecules: indexes = get_records_indexes_by_attribute_value(pdb.indexes, pdb, key, mol) # build connectivity slicedPdb.records = [pdb.records[idx] for idx in indexes] connectivity = Connectivity(slicedPdb) # calculate bonds connectivity.calculate_bonds(regressive=True) cr, ct = connectivity.get_bonds() # map records to indexes and extend centralRecord and connectedTo centralRecord.extend( [indexes[idx] for idx in cr] ) connectedTo.extend( [[indexes[idx] for idx in item] for item in ct] ) # get centralRecord and connectedTo in indexes order from 0 to len(pdb) bonds = dict(zip(centralRecord,connectedTo)) # get bondsMap bondsMapElementsKey = get_records_attribute_values(pdb.indexes, pdb, bondMapKey) bondsMap = [] for crIdx in range(len(centralRecord)): cr = centralRecord[crIdx] for ct in connectedTo[crIdx]: setted = list(set([bondsMapElementsKey[cr], bondsMapElementsKey[ct]])) bondsMap.append( str(setted[0])+"--"+str(setted[1]) ) bondsMap = OrderedDict(zip(set(bondsMap), range(1,len(bondsMap)+1))) # write fixed neighbours constraint file self.__write_fnc_file__(outputPath, pdb.name, bondsMap, bondsMapElementsKey, bonds)
""" In this test, an SDS molecule is loaded several records manipulations, translation, rotation, orientation, ... are tested """ # standard distribution imports import os # pdbParser imports from pdbParser.Utilities.Collection import get_path from pdbParser.log import Logger from pdbParser.pdbParser import pdbParser from pdbParser.Utilities.Geometry import * pdbRESULT = pdbParser() Logger.info("loading sds molecule ...") pdbSDS = pdbParser(os.path.join(get_path("pdbparser"), "Data", "SDS.pdb")) INDEXES = range(len(pdbSDS.records)) # get molecule axis sdsAxis = get_axis(INDEXES, pdbSDS) ## translate to positive quadrant atomToOriginIndex = get_closest_to_origin(INDEXES, pdbSDS) atom = pdbSDS.records[atomToOriginIndex] [minX, minY, minZ] = [atom['coordinates_x'], atom['coordinates_y'], atom['coordinates_z']] translate(INDEXES, pdbSDS, [-1.1 * minX, -1.1 * minY, -1.1 * minZ]) Logger.info("orient molecule along [1,0,0] ...") orient(axis=[1, 0, 0], indexes=INDEXES, pdb=pdbSDS, records_axis=sdsAxis) sdsAxis = [1, 0, 0] pdbRESULT.concatenate(pdbSDS)
import os from pdbParser.pdbParser import pdbParser from pdbParser.Utilities.Collection import get_path from pdbParser.Utilities.Construct import AmorphousSystem from pdbParser.Utilities.Geometry import get_satisfactory_records_indexes, translate, get_geometric_center from pdbParser.Utilities.Modify import delete_records_and_models_records, reset_records_serial_number, reset_sequence_number_per_residue from pdbParser.Utilities.Database import __WATER__ # read thf molecule and translate to the center thfNAGMA = pdbParser(os.path.join(get_path("pdbparser"), "Data/NAGMA.pdb")) center = get_geometric_center(thfNAGMA.indexes, thfNAGMA) translate(thfNAGMA.indexes, thfNAGMA, -center) # create pdbWATER pdbWATER = pdbParser() pdbWATER.records = __WATER__ pdbWATER.set_name("water") # create amorphous pdbWATER = AmorphousSystem(pdbWATER, boxSize=[40, 40, 40], density=0.75).construct().get_pdb() center = get_geometric_center(pdbWATER.indexes, pdbWATER) translate(pdbWATER.indexes, pdbWATER, -center) # make hollow hollowIndexes = get_satisfactory_records_indexes( pdbWATER.indexes, pdbWATER, "np.sqrt(x**2 + y**2 + z**2) <= 10") delete_records_and_models_records(hollowIndexes, pdbWATER) # concatenate thfNAGMA.concatenate(pdbWATER, pdbWATER.boundaryConditions)
# standard libraries imports import os # external libraries imports # fullrmc library imports from pdbParser.pdbParser import pdbParser, pdbTrajectory # visualize traj = pdbTrajectory() traj.set_structure('system.pdb') time = 0 for num in sorted( [int(item.split('.pdb')[0]) for item in os.listdir("pdbFiles")]): pdb = os.path.join("pdbFiles", str(num) + ".pdb") print pdb traj.append_configuration( pdb=pdbParser(pdb), vectors=traj.structure.boundaryConditions.get_vectors(), time=time) time = time + 1 # visualize traj.visualize()
# standard libraries imports import time import os import itertools # external libraries imports import numpy as np from pdbParser.pdbParser import pdbParser, pdbTrajectory # pdbFiles pdbFiles = [fn for fn in os.listdir("pdbFiles") if ".pdb" in fn] generated = [int(fn.split("_")[0]) for fn in pdbFiles] pdbFiles = [ os.path.join("pdbFiles", pdbFiles[idx]) for idx in np.argsort(generated) ] # create trajectory traj = pdbTrajectory() traj.set_structure(pdbParser("thf.pdb")) for idx in range(len(pdbFiles)): print "loading frame %i out of %i" % (idx, len(pdbFiles)) fname = pdbFiles[idx] pdb = pdbParser(fname) traj.append_configuration(pdb=pdb, vectors=pdb.boundaryConditions.get_vectors(), time=idx) traj.visualize()
import os from pdbParser.pdbParser import pdbParser from pdbParser.Utilities.Construct import AmorphousSystem from pdbParser.Utilities.Collection import get_path # create thf amorphous box pdb = pdbParser(os.path.join(get_path("pdbparser"), "Data/Tetrahydrofuran.pdb")) #pdb.visualize() #exit() pdb = AmorphousSystem(pdb, boxSize=[48, 48, 48], recursionLimit=1000000, insertionNumber=700, density=0.7, priorities={ "boxSize": True, "insertionNumber": False, "density": True }).construct().get_pdb() pdb.export_pdb("thf.pdb")
from pdbParser.pdbParser import pdbParser from pdbParser.Utilities.Construct import AmorphousSystem from pdbParser.Utilities.Database import __WATER__ from pdbParser.Utilities.Geometry import translate, get_center # create pdbWATER of a single molecule of water pdbWATER = pdbParser() pdbWATER.records = __WATER__ pdbWATER.set_name("water") # create amorphous water box pdbWATER = AmorphousSystem(pdbWATER, density=1, boxSize=[10, 10, 10]).construct().get_pdb() # translate water box to center translate(pdbWATER.indexes, pdbWATER, -get_center(pdbWATER.indexes, pdbWATER)) # export water box pdbWATER.export_pdb("waterBox.pdb")
def create_fixed_neighbours_constraint(self, outputPath, map, regressive=True): """ generates rmc++ .fnc file from a pdbParser instance using a user-defined bonds maping. RMC++ .cfg file atoms are ordered element wise. Therefore .fnc atoms indexes might not be the same as the given pdb records indexes but reordered to correspond to the .cfg file exported using convert_pdb method. In order to get a pdb file respecting the same order as .cfg file, one should convert the original pdb using convert_pdb then convert it back to pdb using convert method. :Parameters: #. outputPath (str): The RMC++ .fnc output configuration file path. #. map (list): list of dictionary. Every dictionary must contain mapping specific keys.Valid keys are: #. 'molecule_mapping' : Any pdbParser record valid key used to split records into molecules. #. 'molecule_key' : the molecules in the pdb. #. 'atoms_mapping' : Any pdbParser record valid key used to map records bonds. #. 'bonds' : dictionary of atom keys and list of atoms bonds value.\n :: e.g. map=[ {"molecules_mapping" : "sequence_number", "atoms_mapping" : "atom_name", "bonds" : {"O":["H1","H2"]} }, {"molecules_mapping" : "sequence_number", "atoms_mapping" : "atom_name", "bonds" : {"C":["H1","H2","H3","H4"], "H1":["H2","H3","H4"], "H2":["H3","H4"], "H3":["H4"]} } ] #. regressive (bool): Insures that a bond is calculated for both atoms of the same bond. If atom X is bonded to Y, normaly Y is in the bonds list of X, regressive insures that also X is in the bonds list of Y """ assert isinstance(regressive, bool), Logger.error("regressive must be a boolean") # check map assert isinstance(map, (list, set, tuple)), Logger.error( "map should be a list") map = list(map) for m in map: assert isinstance(m, dict), Logger.error( "map item must be python dictionary") assert m.has_key("molecules_mapping"), Logger.error( "map item must have 'molecules_mapping' key") assert m.has_key("atoms_mapping"), Logger.error( "map item must have 'atoms_mapping' key") assert m.has_key("bonds"), Logger.error( "map item must have 'bonds' key") assert isinstance(m["bonds"], dict), Logger.error( "map 'bonds' value must be a dictionary") # create regressive bonds if regressive: for m in map: for cr, values in m["bonds"].items(): for ct in values: if m["bonds"].get(ct, None) is None: m["bonds"][ct] = [cr] else: m["bonds"][ct].append(cr) # remove redundancy for ct in m["bonds"].keys(): m["bonds"][ct] = list(set(m["bonds"][ct])) # get pdb records indexes element wise elements = self.get_pdb().elements types = set(elements) typesIndexes = [] for t in types: typesIndexes.extend( get_records_indexes_by_attribute_value(self.get_pdb().indexes, self.get_pdb(), "element_symbol", t) ) pdb = pdbParser.pdbParser() pdb.set_name(self.get_pdb().name) pdb.records = [self.get_pdb().records[idx] for idx in typesIndexes] # build bonds centralRecord = [] connectedTo = [] bondsMapElementsKey = [None for idx in pdb.indexes] for m in map: # get molecules molecules = set(get_records_attribute_values(pdb.indexes, pdb, m["molecules_mapping"])) for mol in molecules: molAtomsIndexes = get_records_indexes_by_attribute_value(pdb.indexes, pdb, m["molecules_mapping"], mol) for cr, values in m["bonds"].items(): # get central atoms index crIdx = get_records_indexes_by_attribute_value(molAtomsIndexes, pdb, m["atoms_mapping"], cr) assert len(crIdx)==1, "Only one central atom in molecule map bonds must be found. '%i' where found for central atom '%s'"%(len(crIdx), str(cr)) centralRecord.append(crIdx[0]) bondsMapElementsKey[crIdx[0]] = pdb.records[crIdx[0]][m["atoms_mapping"]] # get connected atoms ctList = [] for ct in values: ctIdx = get_records_indexes_by_attribute_value(molAtomsIndexes, pdb, m["atoms_mapping"], ct) assert len(ctIdx)==1, "Only one connected atom in molecule map bonds must be found. '%i' where found for connected atom '%s' in central atom '%s' in map %s"%(len(ctIdx), str(ct), str(cr), str(m["bonds"][cr])) ctList.append(ctIdx[0]) bondsMapElementsKey[ctIdx[0]] = pdb.records[ctIdx[0]][m["atoms_mapping"]] connectedTo.append(ctList) # complete non-bonded atoms and create bonds dictionary bonds = {} for idx in pdb.indexes: bonds[idx]=[] for idx in range(len(centralRecord)): bonds[centralRecord[idx]]=connectedTo[idx] # create bondsMap bondsMap = [] for crIdx in range(len(bonds)): for ctIdx in bonds[crIdx]: cr = bondsMapElementsKey[crIdx] ct = bondsMapElementsKey[ctIdx] assert not (cr is None or ct is None), "mapping error" setted = list(set([cr, ct])) bondsMap.append( str(setted[0])+"--"+str(setted[1]) ) bondsMap = OrderedDict(zip(set(bondsMap), range(1,len(bondsMap)+1))) # write fixed neighbours constraint file self.__write_fnc_file__(outputPath, pdb.name, bondsMap, bondsMapElementsKey, bonds)