def __init__(self, hdf5_group_or_filename, universe=None, author_name='unknown', author_email=None, file_mode=None, float_type=np.float32, mosaic_data = {}): """ :param hdf5_group_or_filename: a group in an open HDF5 file, or a string interpreted as a file name :type hdf5_group_or_filename: str or ``h5py.Group`` :param file_mode: ``'r'`` or ``'w'``, used only with a filename argument :type file_mode: str :param universe: the universe associated with the trajectory :type universe: :class:`mosaic.api.MosaicUniverse` :param mosaic_data: additional MOSAIC data items to be stored """ if isstring(hdf5_group_or_filename): if file_mode is None: file_mode = 'r' self.root = h5py.File(hdf5_group_or_filename, file_mode) self._close = True else: api.validate_value(file_mode, [None], "file_mode") self.root = hdf5_group_or_filename self._close = False self.float_type = float_type self._mosaic_group(universe, mosaic_data) self._create_h5md_group(author_name, author_email) self._create_particles_group()
def _property(self, el_shape, dtype, text): data = IN.array([dtype(s) for s in text.split()]) if isstring(el_shape): el_shape = tuple(int(s) for s in el_shape.split()) n_els = N.product(el_shape) assert len(data) % n_els == 0 n_entries = len(data) // n_els return data.reshape((n_entries,)+el_shape)
def create_dataset(group, path, data): if isstring(data): ds = group.require_dataset(path, shape=(), dtype=h5py.special_dtype(vlen=bytes)) ds[...] = data else: ds = group.require_dataset(path, shape=data.shape, dtype=data.dtype) if 0 not in data.shape: ds[...] = data return ds
def __init__(self, xml_file): """ :param xml_file: a file object, or a string interpreted as a file name :type xml_file: str or file-like """ XMLStore.__init__(self) if isstring(xml_file): # file name given: open file and close it at the end self.file = open(xml_file, 'r') self._close_file = True else: # assume it is a file-like object, don't close at the end self.file = xml_file self._close_file = False
def __init__(self, hdf5_group_or_filename, file_mode=None): """ :param hdf5_group_or_filename: a group in an open HDF5 file, or a string interpreted as a file name :type hdf5_group_or_filename: str or ``h5py.Group`` :param file_mode: ``'r'`` or ``'w'``, used only with a filename argument :type file_mode: str """ if isstring(hdf5_group_or_filename): if file_mode is None: file_mode = 'r' self.root = h5py.File(hdf5_group_or_filename, file_mode) self._close = True else: api.validate_value(file_mode, [None], "file_mode") self.root = hdf5_group_or_filename self._close = False self._path_map = weakref.WeakKeyDictionary() self._data_map = weakref.WeakValueDictionary() self._factory = mosaic.array_model.Factory()
def __init__(self, xml_file): """ :param xml_file: a writeable file object, or a string interpreted as a file name :type xml_file: str or file-like """ XMLStore.__init__(self) if isstring(xml_file): self.file = open(xml_file, 'w') self._close_file = True else: # assume it is a file-like object self.file = xml_file self._close_file = False # Keep a set of already used ids in order to check they remain unique self._xml_ids = set() # Start with the XML declaration self.file.write('<?xml version="1.0" encoding="utf-8"?>') # Open the top-level element self.file.write('<mosaic version="%d.%d">' % api.MOSAIC_VERSION)
def _array(self, shape, dtype, text): if isstring(shape): shape = tuple(int(s) for s in shape.split()) return IN.array([dtype(s) for s in text.split()]).reshape(shape)
def __init__(self, structure_file=None, pdb_code=None, with_auth_spec=False): """ Specify the data to be loaded. The following combinations are valid: - pdb_code only: the data is taken from a public or local PDB repository - structure_file only: the data is taken from the structure file @param structure_file: the name of the structure mmCIF file @type structure_file: C{str} @param pdb_code: a four-letter PDB code @type pdb_code: C{str} @param with_auth_spec: flag for reading and storing the author's alternative atom site specifications @type with_auth_spec: C{bool} """ if pdb_code is not None: self.pdb_code = pdb_code assert structure_file is None self.structure_file = None parser = MMCIFParser(pdb_code=self.pdb_code) elif structure_file is not None: self.pdb_code = None if isstring(structure_file): structure_file = open(structure_file) self.structure_file = structure_file parser = MMCIFParser(file_object=self.structure_file) else: raise MMCIFError("No input structure given") self.cell_parameters = {} self.symmetry = {} self.entities = OrderedDict() self.components = OrderedDict() self.molecules = OrderedDict() self.ss_bridges = [] self.extra_bonds = [] self.models = [] self.u = {} if with_auth_spec: self.auth_spec = {} else: self.auth_spec = None self.modified = False parser.parseToObjects(cell=self.cell_parameters, symmetry=self.symmetry, exptl=self.getPDBCodeFromExptl, entity=self.addEntity, entity_name_com=self.addEntityName, entity_poly=self.addPolymerInfo, entity_poly_seq=self.buildSequence, chem_comp=self.addComponent, struct_conn=self.addConnection, struct_asym=self.addMolecule, atom_site=self.addSite, atom_sites_alt=self.recordAltLabel, atom_site_anisotrop=self.addAnisoU) if self.structure_file is not None: self.structure_file.close() if len(self.cell_parameters) == 0 \ or (self.cell_parameters['length_a'] == self.cell_parameters['length_b'] == self.cell_parameters['length_a'] == '1' and self.cell_parameters['angle_alpha'] == self.cell_parameters['angle_beta'] == self.cell_parameters['angle_gamma'] == '90') \ or (self.cell_parameters['length_a'] == self.cell_parameters['length_b'] == self.cell_parameters['length_a'] == '?' and self.cell_parameters['angle_alpha'] == self.cell_parameters['angle_beta'] == self.cell_parameters['angle_gamma'] == '?'): # NMR or other non-crystal structure self.cell = None else: self.cell = \ UnitCell( float(self.cell_parameters['length_a']) * Units.Ang, float(self.cell_parameters['length_b']) * Units.Ang, float(self.cell_parameters['length_c']) * Units.Ang, float(self.cell_parameters['angle_alpha']) * Units.deg, float(self.cell_parameters['angle_beta']) * Units.deg, float(self.cell_parameters['angle_gamma']) * Units.deg) del self.cell_parameters self.space_group = None sg = self.symmetry.get('Int_Tables_number', None) if sg not in [None, '?', '.']: self.space_group = space_groups[int(sg)] else: for field in ['pdbx_full_space_group_name_H-M', 'space_group_name_H-M']: sg = self.symmetry.get(field, None) if sg not in [None, '?', '.']: self.space_group = space_groups[sg] break