コード例 #1
0
ファイル: xyz.py プロジェクト: molmod/zeobuilder
    def __call__(self, f):
        try:
            xyz_reader = XYZReader(f)
            molecule = xyz_reader.get_first_molecule()
        except IOError:
            raise FilterError("Could not read the first frame from the XYZ file. Incorrect file format.")

        Universe = context.application.plugins.get_node("Universe")
        universe = Universe()
        Folder = context.application.plugins.get_node("Folder")
        folder = Folder()

        title = molecule.title.strip()
        if len(title) > 0:
            universe.name = title

        Atom = context.application.plugins.get_node("Atom")
        Point = context.application.plugins.get_node("Point")

        for index, number, symbol, coordinate in zip(xrange(molecule.size), molecule.numbers, xyz_reader.symbols, molecule.coordinates):
            extra = {"index": index}
            transl = Translation(coordinate)
            if number == 0:
                atom = Point(name=symbol, extra=extra, transformation=transl)
            else:
                atom = Atom(name=symbol, number=number, extra=extra, transformation=transl)
            universe.add(atom)

        geometries = []
        for title, coordinates in xyz_reader:
            geometries.append(coordinates)

        return [universe, folder]
コード例 #2
0
    def __call__(self, f):
        try:
            xyz_reader = XYZReader(f)
            molecule = xyz_reader.get_first_molecule()
        except IOError:
            raise FilterError(
                "Could not read the first frame from the XYZ file. Incorrect file format."
            )

        Universe = context.application.plugins.get_node("Universe")
        universe = Universe()
        Folder = context.application.plugins.get_node("Folder")
        folder = Folder()

        title = molecule.title.strip()
        if len(title) > 0:
            universe.name = title

        Atom = context.application.plugins.get_node("Atom")
        Point = context.application.plugins.get_node("Point")

        for index, number, symbol, coordinate in zip(xrange(molecule.size),
                                                     molecule.numbers,
                                                     xyz_reader.symbols,
                                                     molecule.coordinates):
            extra = {"index": index}
            transl = Translation(coordinate)
            if number == 0:
                atom = Point(name=symbol, extra=extra, transformation=transl)
            else:
                atom = Atom(name=symbol,
                            number=number,
                            extra=extra,
                            transformation=transl)
            universe.add(atom)

        geometries = []
        for title, coordinates in xyz_reader:
            geometries.append(coordinates)

        return [universe, folder]
コード例 #3
0
    def from_file(cls, filename):
        """Construct a molecule object read from the given file.

           The file format is inferred from the extensions. Currently supported
           formats are: ``*.cml``, ``*.fchk``, ``*.pdb``, ``*.sdf``, ``*.xyz``

           If a file contains more than one molecule, only the first one is
           read.

           Argument:
            | ``filename``  --  the name of the file containing the molecule

           Example usage::

             >>> mol = Molecule.from_file("foo.xyz")
        """
        # TODO: many different API's to load files. brrr...
        if filename.endswith(".cml"):
            from molmod.io import load_cml
            return load_cml(filename)[0]
        elif filename.endswith(".fchk"):
            from molmod.io import FCHKFile
            fchk = FCHKFile(filename, field_labels=[])
            return fchk.molecule
        elif filename.endswith(".pdb"):
            from molmod.io import load_pdb
            return load_pdb(filename)
        elif filename.endswith(".sdf"):
            from molmod.io import SDFReader
            return SDFReader(filename).next()
        elif filename.endswith(".xyz"):
            from molmod.io import XYZReader
            xyz_reader = XYZReader(filename)
            title, coordinates = xyz_reader.next()
            return Molecule(xyz_reader.numbers,
                            coordinates,
                            title,
                            symbols=xyz_reader.symbols)
        else:
            raise ValueError("Could not determine file format for %s." %
                             filename)
コード例 #4
0
ファイル: molecules.py プロジェクト: Clyde-fare/molmod
    def from_file(cls, filename):
        """Construct a molecule object read from the given file.

           The file format is inferred from the extensions. Currently supported
           formats are: ``*.cml``, ``*.fchk``, ``*.pdb``, ``*.sdf``, ``*.xyz``

           If a file contains more than one molecule, only the first one is
           read.

           Argument:
            | ``filename``  --  the name of the file containing the molecule

           Example usage::

             >>> mol = Molecule.from_file("foo.xyz")
        """
        # TODO: many different API's to load files. brrr...
        if filename.endswith(".cml"):
            from molmod.io import load_cml
            return load_cml(filename)[0]
        elif filename.endswith(".fchk"):
            from molmod.io import FCHKFile
            fchk = FCHKFile(filename, field_labels=[])
            return fchk.molecule
        elif filename.endswith(".pdb"):
            from molmod.io import load_pdb
            return load_pdb(filename)
        elif filename.endswith(".sdf"):
            from molmod.io import SDFReader
            return SDFReader(filename).next()
        elif filename.endswith(".xyz"):
            from molmod.io import XYZReader
            xyz_reader = XYZReader(filename)
            title, coordinates = xyz_reader.next()
            return Molecule(xyz_reader.numbers, coordinates, title, symbols=xyz_reader.symbols)
        else:
            raise ValueError("Could not determine file format for %s." % filename)
コード例 #5
0
ファイル: xyz.py プロジェクト: boegel/yaff
def xyz_to_hdf5(f, fn_xyz, sub=slice(None), file_unit=angstrom, name='pos'):
    """Convert XYZ trajectory file to Yaff HDF5 format.

       **Arguments:**

       f
            An open and writable HDF5 file.

       fn_xyz
            The filename of the XYZ trajectory file.

       **Optional arguments:**

       sub
            The sub argument for the XYZReader. This must be a slice object that
            defines the subsampling of the XYZ file reader. By default all
            frames are read.

       file_unit
            The unit of the data in the XYZ file. [default=angstrom]

       name
            The name of the HDF5 dataset where the trajectory is stored. This
            array is stored in the 'trajectory' group.

       This routine will also test the consistency of the row attribute of the
       trajectory group. If some trajectory data is already present, it will be
       replaced by the new data. It is highly recommended to first initialize
       the HDF5 file with the ``to_hdf5`` method of the System class.
    """
    with log.section('XYZH5'):
        if log.do_medium:
            log('Loading XYZ file \'%s\' into \'trajectory/%s\' of HDF5 file \'%s\''
                % (fn_xyz, name, f.filename))

        # First make sure the HDF5 file has a system description that is consistent
        # with the XYZ file.
        if 'system' not in f:
            raise ValueError('The HDF5 file must contain a system group.')
        if 'numbers' not in f['system']:
            raise ValueError(
                'The HDF5 file must have a system group with atomic numbers.')

        xyz_reader = XYZReader(fn_xyz, sub=sub, file_unit=file_unit)
        if len(xyz_reader.numbers) != len(f['system/numbers']):
            raise ValueError(
                'The number of atoms in the HDF5 and the XYZ files does not match.'
            )
        if (xyz_reader.numbers != f['system/numbers']).any():
            log.warn(
                'The atomic numbers of the HDF5 and XYZ file do not match.')

        # Take care of the trajectory group
        tgrp = get_trajectory_group(f)

        # Take care of the dataset
        ds, = get_trajectory_datasets(tgrp,
                                      (name, (len(xyz_reader.numbers), 3)))

        # Fill the dataset with data.
        row = get_last_trajectory_row([ds])
        for title, coordinates in xyz_reader:
            write_to_dataset(ds, coordinates, row)
            row += 1

        # Check number of rows
        check_trajectory_rows(tgrp, [ds], row)