Example #1
0
 def _open(self):
     self._closed = False
     self._step = 0
     self._frame = -1
     if isinstance(self.filename, chemfiles.Trajectory):
         self._file = self.filename
     else:
         self._file = chemfiles.Trajectory(self.filename, 'r', self._format)
Example #2
0
    def __init__(self, file, mode='r'):
        super().__init__()

        try:
            import chemfiles
        except:
            raise ImportError(
                'Currently mstools use chemfiles to parse XTC format. Cannot import chemfiles'
            )

        if mode not in ('r', 'w', 'a'):
            raise Exception('Invalid mode')

        self._xtc = chemfiles.Trajectory(file, mode)
Example #3
0
    def __init__(self,
                 filename,
                 n_atoms=0,
                 mode="w",
                 chemfiles_format="",
                 topology=None,
                 **kwargs):
        """
        Parameters
        ----------
        filename: str
            filename of trajectory file.
        n_atoms: int
            number of atoms in the trajectory to write. This value is not
            used and can vary during trajectory, if the underlying format
            support it
        mode : str (optional)
            file opening mode: use 'a' to append to an existing file or 'w' to
            create a new file
        chemfiles_format : str (optional)
            use the given format name instead of guessing from the extension.
            The `list of supported formats <formats>`_ and the associated names
            is available in chemfiles documentation.
        topology : Universe or AtomGroup (optional)
            use the topology from this :class:`~MDAnalysis.core.groups.AtomGroup`
            or :class:`~MDAnalysis.core.universe.Universe` to write all the
            timesteps to the file
        **kwargs : dict
            General writer arguments.


        .. _formats: http://chemfiles.org/chemfiles/latest/formats.html
        """
        if not check_chemfiles_version():
            raise RuntimeError("Please install Chemfiles > {}"
                               "".format(MIN_CHEMFILES_VERSION))
        self.filename = filename
        self.n_atoms = n_atoms
        if mode != "a" and mode != "w":
            raise IOError("Expected 'a' or 'w' as mode in ChemfilesWriter")
        self._file = chemfiles.Trajectory(self.filename, mode,
                                          chemfiles_format)
        self._closed = False
        if topology is not None:
            if isinstance(topology, string_types):
                self._file.set_topology(topology)
            else:
                topology = self._topology_to_chemfiles(topology, n_atoms)
                self._file.set_topology(topology)
Example #4
0
def replace_pos(f_data, f_traj, mol_types, out_data, translate_z=0):
    data = Data(f_data)

    traj = chemfiles.Trajectory(f_traj)
    frame = traj.read()

    mol_atomid = []
    for mol_type in mol_types:
        mol_atomid.extend(get_atoms_id(mol_type, frame))
    mol_atomid.sort() # array is 0-index

    positions = frame.positions()
    mol_pos = positions[mol_atomid]

    for loop, item in enumerate(mol_atomid):
        atom = data.Atoms[item].strip().split()
        pos = mol_pos[loop]
        pos[2] += translate_z
        data.Atoms[item] = '%s %s %s %s %.4f %.4f %.4f %s %s' % (atom[0], atom[1], atom[2], atom[3], pos[0], pos[1], pos[2], atom[7], atom[8])

    data.write(out_data)
Example #5
0
import sys
import chemfiles

from rascaline import SoapPowerSpectrum

# read structures using chemfiles
with chemfiles.Trajectory(sys.argv[1]) as trajectory:
    frames = [f for f in trajectory]

# define hyper parameters for the calculation
HYPER_PARAMETERS = {
    "cutoff": 5.0,
    "max_radial": 6,
    "max_angular": 4,
    "atomic_gaussian_width": 0.3,
    "gradients": False,
    "radial_basis": {
        "Gto": {},
    },
    "cutoff_function": {
        "ShiftedCosine": {
            "width": 0.5
        },
    },
}

calculator = SoapPowerSpectrum(**HYPER_PARAMETERS)

# run the actual calculation
descriptor = calculator.compute(frames)
Example #6
0
def bench_chemfiles(path):
    file = chemfiles.Trajectory(path)
    for step in range(file.nsteps):
        file.read()
Example #7
0
def count_eg_displaced(f_traj, eg_types=[7, 8, 9, 10], num_bins=5000, n_seg=5):

    atoms_in_eg = 10.0

    if type(f_traj) == type([]):
        traj = chemfiles.Trajectory(f_traj[0])
    elif type(f_traj) == type(''):
        traj = chemfiles.Trajectory(f_traj)

    n_frame = traj.nsteps()
    frame = traj.read()
    eg_atoms = chemfiles.Selection(
        "atoms: name {0:d} or name {1:d} or name {2:d} or name {3:d}".format(
            *eg_types)).evaluate(frame)

    box_x, box_y, box_z = frame.cell().lengths()
    box_area = box_x * box_y
    eg_z = []

    positions = frame.positions()
    eg_z.append(list(positions[eg_atoms, 2]))
    atoms_per_frame = len(eg_z[0])

    for i in range(traj.nsteps() - 1):
        frame = traj.read()
        positions = frame.positions()
        eg_z.append(list(positions[eg_atoms, 2]))

    if type(f_traj) == type([]) and len(f_traj) > 1:
        for ft in f_traj[1:]:
            traj = chemfiles.Trajectory(ft)
            n_frame += traj.nsteps()
            for i in range(traj.nsteps() - 1):
                frame = traj.read()
                positions = frame.positions()
                eg_z.append(list(positions[eg_atoms, 2]))

    eg_z = [pbc_wrap(x, box_z) for li in eg_z for x in li]

    seg_egz = np.array_split(eg_z, n_seg)
    seg_len = np.mean(list(map(len, seg_egz)))

    displaced_array = []

    with open('displaced_eg.txt', 'a') as f:
        f.write(time.strftime("%c"))
        f.write('\nNumber of segments: %d\n' % n_seg)

    for i in range(n_seg):
        count, binEdges = np.histogram(seg_egz[i], num_bins, density=False)
        dist_bin = binEdges[1] - binEdges[0]  # Unit = A
        bincenters = 0.5 * (binEdges[1:] + binEdges[:-1])
        n_frame_seg = len(seg_egz[i]) / atoms_per_frame

        density = count / (box_area * dist_bin * n_frame_seg * atoms_in_eg
                           )  # Convert to density [=] EG / A^3

        for index, val in enumerate(density):
            print('{0:d} {1:.5f}'.format(index, val))

        fig = plt.figure()
        plt.plot(bincenters, density, 'r-')
        plt.xlabel('z (nm)')
        plt.ylabel(r'$\rho_{EG}$ ($EG/A^3$)')
        fig.savefig('eg_density_%d.png' % (i + 1), dpi=fig.dpi)
        plt.show()
        plt.close()

        print('Bin with density=0.0002 contribution: %.3f' %
              (dist_bin * box_area * 0.0002))
        bin_to_cover = 4.5 / dist_bin
        print('Number of bin to cover 4.5 A: %.1f' % bin_to_cover)

        bottom_2 = int(input('Enter index of bottom peak end (> 0.00020): '))
        print('Recommended bottom peak start: %.0d' %
              (bottom_2 - round(bin_to_cover)))
        # bottom_1 = int(input('Enter index of bottom peak start: '))
        bottom_1 = int(bottom_2 - round(bin_to_cover))
        top_1 = int(input('Enter index of top peak start (> 0.00020): '))
        print('Recommended top peak end: %.0d' % (top_1 + round(bin_to_cover)))
        # top_2 = int(input('Enter index of top peak end: '))
        top_2 = int(top_1 + round(bin_to_cover))

        displaced_array.append(
            get_displaced(density, box_area, dist_bin, f_traj, bottom_1,
                          bottom_2, top_1, top_2, i, n_frame_seg))

    d = {}
    d['displaced_array'] = displaced_array
    d['displaced_mean'] = np.mean(displaced_array)
    d['displaced_sem'] = sem(displaced_array)

    print('Displaced array:')
    print('%.2f ' * len(d['displaced_array']) % tuple(d['displaced_array']))
    print('Displaced: %.2f +/- %.2f' %
          (d['displaced_mean'], d['displaced_sem']))

    with open('displaced_eg.txt', 'a') as f:
        f.write('\nSummary\n')
        f.write('Displaced array: ')
        f.write('%.2f ' * len(d['displaced_array']) %
                tuple(d['displaced_array']))
        f.write('\n')
        f.write('Displaced: %.2f +/- %.2f\n\n' %
                (d['displaced_mean'], d['displaced_sem']))

    # d['density'] = density
    # d['box_area'] = box_area
    # d['dist_bin'] = dist_bin
    # d['f_traj'] = f_traj
    # d['bottom_1'] = bottom_1
    # d['bottom_2'] = bottom_2
    # d['top_1'] = top_1
    # d['top_2'] = top_2

    return d
Example #8
0
def get_density(F_TRAJ,
                ATOM_TYPE,
                SURF_ATOMS,
                NUMBINS=100,
                ATOM_NAME='pvp',
                ON_TOP=False):

    if type(F_TRAJ) == type([]):
        traj = chemfiles.Trajectory(F_TRAJ[0])
    elif type(F_TRAJ) == type(''):
        traj = chemfiles.Trajectory(F_TRAJ)

    n_frame = traj.nsteps()
    frame = traj.read()
    ATOMS = chemfiles.Selection(
        "atoms: name {0:d}".format(ATOM_TYPE)).evaluate(frame)
    ADJ_SURF_ATOMS = [x - 1 for x in SURF_ATOMS]

    BOX_X, BOX_Y, BOX_Z = frame.cell().lengths()
    BOX_AREA = BOX_X * BOX_Y

    atom_z = []
    surf_z = []
    positions = frame.positions()
    atom_z.append(list(positions[ATOMS, 2]))
    surf_z.append(list(positions[ADJ_SURF_ATOMS, 2]))
    ATOMS_PER_FRAME = len(atom_z[0])

    for i in range(traj.nsteps() - 1):
        frame = traj.read()
        positions = frame.positions()
        atom_z.append(list(positions[ATOMS, 2]))
        surf_z.append(list(positions[ADJ_SURF_ATOMS, 2]))

    if type(F_TRAJ) == type([]) and len(F_TRAJ) > 1:
        for ft in F_TRAJ[1:]:
            traj = chemfiles.Trajectory(ft)
            n_frame += traj.nsteps()
            for i in range(traj.nsteps() - 1):
                frame = traj.read()
                positions = frame.positions()
                atom_z.append(list(positions[ATOMS, 2]))
                surf_z.append(list(positions[ADJ_SURF_ATOMS, 2]))

    atom_z = [[pbc_wrap(x, BOX_Z) for x in frame] for frame in atom_z]
    surf_z = [[pbc_wrap(x, BOX_Z) for x in frame] for frame in surf_z]
    avg_surf_z = [np.mean(frame) for frame in surf_z]

    fig = plt.figure()
    plt.hist([val for frame in surf_z for val in frame], 100)
    plt.xlabel('z of surface atoms (A)')
    plt.ylabel('counts')
    plt.title(ATOM_NAME)
    fig.savefig('ag_%s.png' % ATOM_NAME.replace(' ', '_'), dpi=fig.dpi)
    plt.show()
    plt.close()

    adj_atom_z = []
    for atom_frame, avg_surf in zip(atom_z, avg_surf_z):
        if ON_TOP:
            adj_atom_z.extend([atom - avg_surf for atom in atom_frame])
        else:
            adj_atom_z.extend([avg_surf - atom for atom in atom_frame])

    count, binEdges = np.histogram(adj_atom_z, NUMBINS, density=False)
    dist_bin = binEdges[1] - binEdges[0]  # Unit = A
    bincenters = 0.5 * (binEdges[1:] + binEdges[:-1])

    density = count / (BOX_AREA * dist_bin * n_frame
                       )  # Convert to density [=] atom / A^3

    data = {}
    data['name'] = ATOM_NAME
    data['density'] = density
    data['bincenters'] = bincenters

    return data
Example #9
0
def avg_eg_peak_width(f_traj, eg_types=[7, 8, 9, 10], num_bins=5000):

    atoms_in_eg = 10.0

    if type(f_traj) == type([]):
        traj = chemfiles.Trajectory(f_traj[0])
    elif type(f_traj) == type(''):
        traj = chemfiles.Trajectory(f_traj)

    n_frame = traj.nsteps()
    frame = traj.read()
    eg_atoms = chemfiles.Selection(
        "atoms: name {0:d} or name {1:d} or name {2:d} or name {3:d}".format(
            *eg_types)).evaluate(frame)

    box_x, box_y, box_z = frame.cell().lengths()
    box_area = box_x * box_y
    eg_z = []

    positions = frame.positions()
    eg_z.append(list(positions[eg_atoms, 2]))
    atoms_per_frame = len(eg_z[0])

    for i in range(traj.nsteps() - 1):
        frame = traj.read()
        positions = frame.positions()
        eg_z.append(list(positions[eg_atoms, 2]))

    if type(f_traj) == type([]) and len(f_traj) > 1:
        for ft in f_traj[1:]:
            traj = chemfiles.Trajectory(ft)
            n_frame += traj.nsteps()
            for i in range(traj.nsteps() - 1):
                frame = traj.read()
                positions = frame.positions()
                eg_z.append(list(positions[eg_atoms, 2]))

    eg_z = [pbc_wrap(x, box_z) for li in eg_z for x in li]

    count, binEdges = np.histogram(eg_z, num_bins, density=False)
    dist_bin = binEdges[1] - binEdges[0]  # Unit = A
    bincenters = 0.5 * (binEdges[1:] + binEdges[:-1])

    density = count / (box_area * dist_bin * n_frame * atoms_in_eg
                       )  # Convert to density [=] EG / A^3

    for index, val in enumerate(density):
        print('{0:d} {1:.5f}'.format(index, val))

    fig = plt.figure()
    plt.plot(bincenters, density, 'r-')
    plt.xlabel('z (nm)')
    plt.ylabel(r'$\rho_{EG}$ ($EG/A^3$)')
    fig.savefig('eg_density.png', dpi=fig.dpi)
    plt.show()
    plt.close()

    print('Bin with density=0.0001 contribution: %.3f' %
          (dist_bin * box_area * 0.0001))
    bin_to_cover = 4.5 / dist_bin
    print('Number of bin to cover 4.5 A: %.1f' % bin_to_cover)

    bottom_2 = int(input('Enter index of bottom peak end (> 0.00010): '))
    print('Recommended bottom peak start: %.0d' %
          (bottom_2 - round(bin_to_cover)))
    bottom_1 = int(input('Enter index of bottom peak start: '))
    top_1 = int(input('Enter index of top peak start (> 0.00010): '))
    print('Recommended top peak end: %.0d' % (top_1 + round(bin_to_cover)))
    top_2 = int(input('Enter index of top peak end: '))

    width_bottom = (bottom_2 - bottom_1) * dist_bin
    width_top = (top_2 - top_1) * dist_bin
    print('Width of bottom peak = %.2f A' % width_bottom)
    print('Width of top peak = %.2f A' % width_top)

    with open('eg_peak_width.txt', 'a') as f:
        f.write(time.strftime("%c"))
        f.write('\n')
        f.write('Width of bottom peak = %.2f A\n' % width_bottom)
        f.write('Width of top peak = %.2f A\n' % width_top)
Example #10
0
    def _sample_helper(self, frame_list, shift, is_pbc):
        """Helper function for sampling run.

        Parameters
        ----------
        frame_list :
            List of frame ids to process
        shift : list
            Distances for translating all positions in nano meter
        is_pbc : bool, optional
            True to apply periodic boundary conditions

        Returns : dictionary
            Dictionary conatining all sampled data
        """
        # Initialize
        box = self._pore_props["box"] if self._pore else self._box
        res = self._pore_props["res"] if self._pore else 0
        com_list = []
        idx_list = []
        idx_list_mc = []

        # Load trajectory
        traj = cf.Trajectory(self._traj)
        frame_form = "%"+str(len(str(self._num_frame)))+"i"

        # Create local data structures
        output = {}
        if self._is_density:
            output["density"] = self._density_data()
        if self._is_gyration:
            output["gyration"] = self._gyration_data()
        if self._is_diffusion_bin:
            output["diffusion_bin"] = self._diffusion_bin_data()
        if self._is_diffusion_mc:
            output["diffusion_mc"] = self._diffusion_mc_data()

        # Run through frames
        for frame_id in frame_list:
            # Read frame
            frame = traj.read_step(frame_id)
            positions = frame.positions

            # Add new dictionaries and remove unneeded references
            if self._is_diffusion_bin:
                len_fill = self._diff_bin_inp["len_window"]*self._diff_bin_inp["len_step"]
                if len(com_list) >= len_fill:
                    com_list.pop(0)
                    idx_list.pop(0)
                com_list.append({})
                idx_list.append({})

            # Add new dictionaries and remove unneeded references
            if self._is_diffusion_mc:
                if len(idx_list_mc) >= (max(self._diff_mc_inp["len_step"])+1):
                    idx_list_mc.pop(0)
                idx_list_mc.append({})

            # Run through residues
            for res_id in self._res_list:
                # Get position vectors
                pos = [[positions[self._res_list[res_id][atom_id]][i]/10+shift[i] for i in range(3)] for atom_id in range(len(self._atoms))]

                # Calculate centre of mass
                com_no_pbc = [sum([pos[atom_id][i]*self._masses[atom_id] for atom_id in range(len(self._atoms))])/self._sum_masses for i in range(3)]

                # Remove broken molecules
                if self._is_diffusion_bin or self._is_density:
                    is_broken = False
                    for i in range(3):
                        is_broken = abs(com_no_pbc[i]-pos[0][i])>box[i]/3
                        if is_broken:
                            break

                # Apply periodic boundary conditions
                if is_pbc:
                    com = [com_no_pbc[i]-math.floor(com_no_pbc[i]/box[i])*box[i] for i in range(3)]
                else:
                    com = com_no_pbc

                # Sample if molecule not broken near boundary
                if self._is_diffusion_bin or self._is_density:
                    if not is_broken:
                        # Calculate distance towards center axis
                        if isinstance(self._pore, pms.PoreCylinder):
                            dist = geometry.length(geometry.vector([self._pore_props["focal"][0], self._pore_props["focal"][1], com[2]], com))
                        elif isinstance(self._pore, pms.PoreSlit):
                            dist = abs(self._pore_props["focal"][1]-com[1])
                        else:
                            dist = 0

                # Set region - in-inside, ex-outside
                region = ""
                if self._pore and com[2] > res+self._entry and com[2] < box[2]-res-self._entry:
                    region = "in"
                elif not self._pore or com[2] <= res or com[2] > box[2]-res:
                    region = "ex"

                # Remove window filling instances except from first processor
                if self._is_diffusion_bin:
                    is_sample = len(com_list)==len_fill or frame_id<=len_fill
                else:
                    is_sample = True

                # Sampling routines
                if is_sample:
                    if self._is_density:
                        self._density(output["density"], region, dist, com)
                    if self._is_gyration:
                        self._gyration(output["gyration"], region, dist, com_no_pbc, pos)
                if self._is_diffusion_bin:
                    self._diffusion_bin(output["diffusion_bin"], region, dist, com_list, idx_list, res_id, com)
                if self._is_diffusion_mc:
                    self._diffusion_mc(output["diffusion_mc"], idx_list_mc, res_id, com, frame_list, frame_id)

            # Progress
            if (frame_id+1)%10==0 or frame_id==0 or frame_id==self._num_frame-1:
                sys.stdout.write("Finished frame "+frame_form%(frame_id+1)+"/"+frame_form%self._num_frame+"...\r")
                sys.stdout.flush()
        print()
        return output
Example #11
0
    def __init__(self, system, link_traj, mol, atoms=[], masses=[], entry=0.5):
        # Initialize
        self._pore = utils.load(system) if isinstance(system, str) else None
        self._box = system if isinstance(system, list) else []
        self._traj = link_traj
        self._mol = mol
        self._atoms = atoms
        self._masses = masses
        self._entry = entry

        # Set analysis routines
        self._is_density = False
        self._is_gyration = False
        self._is_diffusion_bin = False
        self._is_diffusion_mc = False

        # Get molecule ids
        self._atoms = [atom.get_name() for atom in mol.get_atom_list()] if not self._atoms else self._atoms
        self._atoms = [atom_id for atom_id in range(mol.get_num()) if mol.get_atom_list()[atom_id].get_name() in self._atoms]

        # Check masses
        if not self._masses:
            if len(self._atoms)==mol.get_num():
                self._masses = mol.get_masses()
            elif len(self._atoms) == 1:
                self._masses = [1]
        self._sum_masses = sum(self._masses)

        # Check atom mass consistency
        if self._atoms and not len(self._masses) == len(self._atoms):
            print("Length of variables *atoms* and *masses* do not match!")
            return

        # Get number of frames
        traj = cf.Trajectory(self._traj)
        self._num_frame = traj.nsteps

        # Get numer of residues
        frame = traj.read()
        num_res = len(frame.topology.atoms)/mol.get_num()

        # Check number of residues
        if abs(int(num_res)-num_res) >= 1e-5:
            print("Number of atoms is inconsistent with number of residues.")
            return

        # Create residue list with all relevant atom ids
        self._res_list = {}
        for res_id in range(int(num_res)):
            self._res_list[res_id] = [res_id*mol.get_num()+atom for atom in range(mol.get_num()) if atom in self._atoms]

        # Get pore properties
        self._pore_props = {}
        if self._pore:
            if isinstance(self._pore, pms.PoreCylinder):
                self._pore_props["type"] = "CYLINDER"
            elif isinstance(self._pore, pms.PoreSlit):
                self._pore_props["type"] = "SLIT"
            self._pore_props["res"] = self._pore.reservoir()
            self._pore_props["focal"] = self._pore.centroid()
            self._pore_props["box"] = self._pore.box()
            self._pore_props["box"][2] += 2*self._pore_props["res"]

            # Get pore diameter
            if isinstance(self._pore, pms.PoreCylinder):
                self._pore_props["diam"] = self._pore.diameter()
            elif isinstance(self._pore, pms.PoreSlit):
                self._pore_props["diam"] = self._pore.height()