Exemple #1
0
    def add_cg_top(self, top_file):

        for line in read_between("[bonds]", "[/bonds]", top_file):
            index1 = int(line.split()[0]) - 1
            index2 = int(line.split()[1]) - 1
            self.add_cg_edge([self.beads[index1], self.beads[index2]])

        self.make_cg_graph()
Exemple #2
0
    def add_aa_top(self, top_file):

        self.top_file = top_file

        for line in read_between("[bonds]", "[/bonds]", top_file):
            if len(line.split()) >= 2:
                index1 = int(line.split()[0]) - 1
                index2 = int(line.split()[1]) - 1
                bond = self.ff.make_bond([self.atoms[index1], self.atoms[index2]])
                if bond:
                    self.add_bond(bond)

        for line in read_between("[angles]", "[/angles]", top_file):
            if len(line.split()) >= 3:
                index1 = int(line.split()[0]) - 1
                index2 = int(line.split()[1]) - 1
                index3 = int(line.split()[2]) - 1
                angle = self.ff.make_angle([self.atoms[index1], self.atoms[index2], self.atoms[index3]])
                if angle:
                    self.add_angle(angle)

        for line in read_between("[dihedrals]", "[/dihedrals]", top_file):
            if len(line.split()) >= 4:
                index1 = int(line.split()[0]) - 1
                index2 = int(line.split()[1]) - 1
                index3 = int(line.split()[2]) - 1
                index4 = int(line.split()[3]) - 1
                dih = self.ff.make_dih([self.atoms[index1], self.atoms[index2], self.atoms[index3], self.atoms[index4]])
                if dih:
                    self.add_dih(dih)

        for line in read_between("[exclusions]", "[/exclusions]", top_file):
            if len(line.split()) >= 2:
                index1 = int(line.split()[0]) - 1
                index2 = int(line.split()[1]) - 1
                self.add_excl((self.atoms[index1], self.atoms[index2]))

        for line in read_between("[pairs]", "[/pairs]", top_file):
            if len(line.split()) >= 2:
                index1 = int(line.split()[0]) - 1
                index2 = int(line.split()[1]) - 1
                self.add_pair((self.atoms[index1], self.atoms[index2]))

        self.make_aa_graph()
Exemple #3
0
    def make_preference_axis(self):

        if not self.top_file.exists():
            raise Exception('No topology file. Add topology file before calling make_preference_axis')

        align = None
        for line in read_between("[align]", "[/align]", self.top_file):
            ndx1, ndx2 = line.split()
            align = (int(ndx1)-1, int(ndx2)-1)

        if align:
            c1 = self.atoms[align[0]]
            c2 = self.atoms[align[1]]

            # compute rotation matrix to align loc env (aligns fixpoint vector with z-axis)
            v1 = np.array([0.0, 0.0, 1.0])
            v2 = self.box.diff_vec(c1.pos - c2.pos)

            # rotation axis
            v_rot = np.cross(v1, v2)
            v_rot = v_rot / np.linalg.norm(v_rot)

            # rotation angle
            cosang = np.dot(v1, v2)
            sinang = np.linalg.norm(np.cross(v1, v2))
            theta = np.arctan2(sinang, cosang)

            # rotation matrix
            a = math.cos(theta / 2.0)
            b, c, d = -v_rot * math.sin(theta / 2.0)
            aa, bb, cc, dd = a * a, b * b, c * c, d * d
            bc, ad, ac, ab, bd, cd = b * c, a * d, a * c, a * b, b * d, c * d
            rot_mat = np.array([[aa + bb - cc - dd, 2 * (bc + ad), 2 * (bd - ac)],
                                [2 * (bc - ad), aa + cc - bb - dd, 2 * (cd + ab)],
                                [2 * (bd + ac), 2 * (cd - ab), aa + dd - bb - cc]])

            self.rot_mat = rot_mat
        else:
            self.rot_mat = np.identity(3)
Exemple #4
0
    def __init__(self, file):
        self.file = file

        #load general information
        for line in read_between("[general]", "[/general]", self.file):
            name, n_excl, n_atoms = line.split()
        self.name = name
        self.n_excl = int(n_excl)
        self.n_atoms = int(n_atoms)
        #self.n_channels = int(n_channels)
        self.n_channels = 0

        #load bead types
        self.bead_types = {}
        for line in read_between("[bead_types]", "[/bead_types]", self.file):
            name, channel = line.split()
            self.n_channels = max(self.n_channels, int(channel) + 1)
            self.bead_types[name] = Bead_Type(name, channel)
        Bead_Type.index = 0
        self.n_bead_chns = len(self.bead_types)

        #load center bead types
        #self.center_bead_types = {}
        #for line in read_between("[center_beadtypes]", "[/center_beadtypes]", self.file):
        #    name, channel = line.split()
        #    self.center_bead_types[name] = Bead_Type(name, channel)

        #load atom types
        self.atom_types = {}
        for line in read_between("[atom_types]", "[/atom_types]", self.file):
            name, channel, mass, charge, sigma, epsilon = line.split()
            self.n_channels = max(self.n_channels, int(channel) + 1)
            self.atom_types[name] = Atom_Type(name, channel, mass, charge,
                                              sigma, epsilon)
        Atom_Type.index = 0
        #self.n_atom_chns = len(set([atype.channel for atype in self.atom_types.values()]))
        self.n_atom_chns = len(self.atom_types)
        #self.atom_type_index_dict = dict(zip(list(self.atom_types.values()), range(0, self.n_atom_chns)))

        #generate LJ types
        self.lj_types = {}
        for line in read_between("[lj_types]", "[/lj_types]", self.file):
            if len(line.split()) == 5:
                name1, name2, channel, exp_n, exp_m = line.split()
            else:
                name1, name2, channel = line.split()
                exp_n, exp_m = 12, 6
            self.n_channels = max(self.n_channels, int(channel) + 1)
            self.lj_types[(name1, name2)] = LJ_Type(self.atom_types[name1],
                                                    self.atom_types[name2],
                                                    channel, exp_n, exp_m)
        self.lj_index_dict = dict(
            zip(self.lj_types.values(), range(0, len(self.lj_types))))

        #load bond types
        self.bond_types = {}
        for line in read_between("[bond_types]", "[/bond_types]", self.file):
            name1, name2, channel, func, equil, force_const = line.split()
            name = (name1, name2)
            self.n_channels = max(self.n_channels, int(channel) + 1)
            self.bond_types[name] = Bond_Type(name, channel, func, equil,
                                              force_const)
        self.bond_index_dict = dict(
            zip(self.bond_types.values(), range(0, len(self.bond_types))))

        #load angle types
        self.angle_types = {}
        for line in read_between("[angle_types]", "[/angle_types]", self.file):
            name1, name2, name3, channel, func, equil, force_const = line.split(
            )
            name = (name1, name2, name3)
            self.n_channels = max(self.n_channels, int(channel) + 1)
            self.angle_types[name] = Angle_Type(name, channel, func, equil,
                                                force_const)
        self.angle_index_dict = dict(
            zip(self.angle_types.values(), range(0, len(self.angle_types))))

        #load dih types
        self.dih_types = {}
        for line in read_between("[dihedral_types]", "[/dihedral_types]",
                                 self.file):
            if len(line.split()) == 9:
                name1, name2, name3, name4, channel, func, equil, force_const, mult = line.split(
                )
                name = (name1, name2, name3, name4)
                self.n_channels = max(self.n_channels, int(channel) + 1)
                self.dih_types[name] = Dih_Type(name, channel, func, equil,
                                                force_const, mult)
            else:
                name1, name2, name3, name4, channel, func, equil, force_const = line.split(
                )
                name = (name1, name2, name3, name4)
                self.n_channels = max(self.n_channels, int(channel) + 1)
                self.dih_types[name] = Dih_Type(name, channel, func, equil,
                                                force_const)
        self.dih_index_dict = dict(
            zip(self.dih_types.values(), range(0, len(self.dih_types))))
        """
        self.align= None
        for line in read_between("[align]", "[/align]", self.file):
            ndx1, ndx2 = line.split()
            self.align = (int(ndx1)-1, int(ndx2)-1)
        """

        #print(self.bead_types)

        self.n_channels += 1
        self.chn_dict = self.make_chn_dict()
Exemple #5
0
    def __init__(self, cfg, path_dict, ff):

        start = timer()

        self.name = path_dict['file_name']

        #parameters
        self.cfg = cfg
        self.aug = int(cfg.getboolean('universe', 'aug'))
        self.align = int(cfg.getboolean('universe', 'align'))
        self.order = cfg.get('universe', 'order')
        self.cutoff_sq = cfg.getfloat('universe', 'cutoff')**2
        self.kick = cfg.getfloat('universe', 'kick')

        #forcefield
        self.ff = ff

        # use mdtraj to load xyz information
        cg = md.load(str(path_dict['cg_path']))
        if path_dict['aa_path']:
            aa = md.load(str(path_dict['aa_path']))
        # number of molecules in file
        self.n_mol = cg.topology.n_residues
        # box dimensions with periodic boundaries
        self.box = Box(path_dict['cg_path'])

        # Go through all molecules in cg file and initialize instances of mols, beads and atoms
        self.atoms, self.beads, self.mols = [], [], []
        for res in cg.topology.residues:
            self.mols.append(Mol(res.name))

            aa_top_file = path_dict['data_dir'] / "aa_top" / (res.name + ".itp")
            cg_top_file = path_dict['data_dir'] / "cg_top" / (res.name + ".itp")
            map_file = path_dict['data_dir'] / "mapping" / (res.name + ".map")
            #env_file = path_dict['dir'] / (res.name + ".env")

            beads = []
            for bead in res.atoms:
                beads.append(Bead(self.mols[-1],
                                  self.box.move_inside(cg.xyz[0, bead.index]),
                                  self.ff.bead_types[bead.element.symbol]))
                self.mols[-1].add_bead(beads[-1])

            atoms = []
            for line in read_between("[map]", "[/map]", map_file):
                type_name = line.split()[1]
                bead = beads[int(line.split()[2])-1]
                atoms.append(Atom(bead,
                                  self.mols[-1],
                                  bead.center,
                                  self.ff.atom_types[type_name]))


                if path_dict['aa_path']:
                    atoms[-1].ref_pos = self.box.diff_vec(aa.xyz[0, atoms[-1].index] - atoms[-1].center)
                bead.add_atom(atoms[-1])
                self.mols[-1].add_atom(atoms[-1])
            Atom.mol_index = 0

            self.mols[-1].add_aa_top(aa_top_file, self.ff)
            self.mols[-1].add_cg_top(cg_top_file)

            #add atoms and beads to universe
            self.beads += beads
            self.atoms += atoms

            if self.align:
                for line in read_between("[align]", "[/align]", map_file):
                    b_index, fp_index = line.split()
                    if int(b_index) > len(self.mols[-1].beads) or int(fp_index) > len(self.mols[-1].beads):
                        raise Exception('Indices in algn section do not match the molecular structure!')
                    self.mols[-1].beads[int(b_index) - 1].fp = self.mols[-1].beads[int(fp_index) - 1]

            if self.aug:
                for line in read_between("[mult]", "[/mult]", map_file):
                    b_index, m = line.split()
                    if int(b_index) > len(self.mols[-1].beads) or int(m) < 0:
                        raise Exception('Invalid number of multiples!')
                    self.mols[-1].beads[int(b_index) - 1].mult = int(m)

        Atom.index = 0
        Bead.index = 0
        Mol.index = 0
        self.n_atoms = len(self.atoms)

        # generate local envs
        self.loc_envs, self.cg_features, self.aa_seq_heavy, self.aa_seq_hydrogens = {}, {}, {}, {}
        self.tops, self.aa_features = {}, {}
        for mol in self.mols:
            cg_seq, dict_aa_seq_heavy, dict_aa_seq_hydrogens, dict_aa_predecessors = mol.aa_seq(order=self.order,
                                                                                              train=False)
            self.aa_seq_heavy = {**self.aa_seq_heavy, **dict_aa_seq_heavy}
            self.aa_seq_hydrogens = {**self.aa_seq_hydrogens, **dict_aa_seq_hydrogens}
            for bead, _ in cg_seq:
                env_beads = self.get_loc_beads(bead)
                self.loc_envs[bead] = Local_Env(bead, env_beads, self.box)
                self.cg_features[bead] = CG_Feature(self.loc_envs[bead], self.ff)
                for atom in dict_aa_seq_heavy[bead]:
                    self.tops[atom] = Top(atom, self.loc_envs[bead], dict_aa_predecessors[atom], self.ff)
                    self.aa_features[atom] = AA_Feature(self.loc_envs[bead], self.tops[atom])
                for atom in dict_aa_seq_hydrogens[bead]:
                    self.tops[atom] = Top(atom, self.loc_envs[bead], dict_aa_predecessors[atom], self.ff)
                    self.aa_features[atom] = AA_Feature(self.loc_envs[bead], self.tops[atom])

        self.energy = Energy(self.tops, self.box)

        self.kick_atoms()
Exemple #6
0
    def __init__(self, cfg, path_dict, ff):

        start = timer()

        self.name = path_dict['file_name']

        #parameters
        self.cfg = cfg
        self.aug = int(cfg.getboolean('universe', 'aug'))
        self.align = int(cfg.getboolean('universe', 'align'))
        self.cutoff_sq = cfg.getfloat('universe', 'cutoff')**2
        self.kick = cfg.getfloat('universe', 'kick')

        #forcefield
        self.ff = ff

        # use mdtraj to load xyz information
        #add element to mdtraj (otherwise some will be identified as VS - virtual sides
        for atype in list(self.ff.atom_types.values()) + list(
                self.ff.bead_types.values()):
            if atype.name not in Element._elements_by_symbol:
                _ = Element(0, atype.name, atype.name, 0.0, 0.0)
        sample = md.load(str(path_dict['path']))

        # number of molecules in file
        self.n_mol = sample.topology.n_residues
        # box dimensions with periodic boundaries
        self.box = Box(path_dict['path'], cfg.getfloat('universe', 'cutoff'))
        self.subbox_dict = self.box.empty_subbox_dict()

        # Go through all molecules in cg file and initialize instances of mols and atoms
        self.atoms, self.beads, self.mols = [], [], []
        for res in sample.topology.residues:
            self.mols.append(Mol(res.name, self.box))

            aa_top_file = path_dict['data_dir'] / "aa_top" / (res.name +
                                                              ".itp")
            cg_top_file = path_dict['data_dir'] / "cg_top" / (res.name +
                                                              ".itp")

            atoms = []
            for atom in res.atoms:
                pos = self.box.move_inside(sample.xyz[0, atom.index])
                atoms.append(
                    Atom(self.mols[-1],
                         self.ff.atom_types[atom.element.symbol],
                         self.box.subbox(pos), pos))
                self.mols[-1].add_atom(atoms[-1])
                self.subbox_dict[self.box.subbox(pos)].append(atoms[-1])

            beads = []
            for line in read_between("[atoms]", "[/atoms]", cg_top_file):
                if len(line.split()) >= 2:
                    bead_type = line.split()[1]
                    beads.append(
                        Bead(self.mols[-1], self.ff.bead_types[bead_type]))

                self.mols[-1].add_bead(beads[-1])
            Atom.mol_index = 0

            self.mols[-1].add_aa_top(aa_top_file, self.ff)
            self.mols[-1].add_cg_top(cg_top_file)
            self.mols[-1].compute_com()

            #add atoms and beads to universe
            self.beads += beads
            self.atoms += atoms

        for mol in self.mols:
            mol.intermolecular_atoms = self.intermolecular_atoms(mol)

        Atom.index = 0
        Bead.index = 0
        Mol.index = 1
        self.n_atoms = len(self.atoms)

        #self.energy = Energy(self.tops, self.box)

        self.kick_atoms()
Exemple #7
0
    def __init__(self, cfg, path_dict, ff, coms=None):

        start = timer()

        self.name = path_dict['file_name']
        print("processing ", self.name, "...")
        #forcefield
        self.ff = ff

        #parameters
        self.cfg = cfg
        self.align = int(cfg.getboolean('universe', 'align'))
        self.cutoff_sq = cfg.getfloat('universe', 'cutoff')**2
        self.kick = cfg.getfloat('universe', 'kick')
        self.n_env_mols = int(cfg.getint('universe', 'n_env_mols'))
        self.n_inter_atoms = self.n_env_mols * self.ff.n_atoms


        # use mdtraj to load xyz information
        #add element to mdtraj (otherwise some will be identified as VS - virtual sides
        for atype in  list(self.ff.atom_types.values())+list(self.ff.bead_types.values()):
            if atype.name.upper() not in Element._elements_by_symbol:
                _ = Element(0, atype.name, atype.name, 0.0, 0.0)
        sample = md.load(str(path_dict['path']))

        # number of molecules in file
        self.n_mol = sample.topology.n_residues
        # box dimensions with periodic boundaries
        self.box = Box(path_dict['path'], cfg.getfloat('universe', 'cutoff'))
        self.subbox_dict_atoms = self.box.empty_subbox_dict()
        self.subbox_dict_mols = self.box.empty_subbox_dict()

        atype_name_dict, m = {}, 0
        for line in read_between("[atoms]", "[/atoms]", path_dict['top_inp']):
            if len(line.split()) >= 2:
                atom_type = line.split()[1]
                atype_name_dict[m] = atom_type
                m += 1
        #print(atype_name_dict)

        # Go through all molecules in cg file and initialize instances of mols and atoms
        self.atoms, self.beads, self.mols = [], [], []
        for res in sample.topology.residues:
            self.mols.append(Mol(res.name, self.box, self.ff))

            #aa_top_file = path_dict['top_aa']
            #cg_top_file = path_dict['top_cg']

            atoms = []
            for atom in res.atoms:
                #print(self.ff.atom_types[atype_name_dict[atom.index % m]].name)
                #print(atom.element.symbol)
                #print(atom.element.symbol)
                pos = self.box.move_inside(sample.xyz[0, atom.index])
                atoms.append(Atom(self.mols[-1],
                                  #self.ff.atom_types[atom.element.symbol],
                                  self.ff.atom_types[atype_name_dict[atom.index % m]],
                                  self.box.subbox(pos),
                                  pos))
                self.mols[-1].add_atom(atoms[-1])
                self.subbox_dict_atoms[self.box.subbox(pos)].append(atoms[-1])
            self.atoms += atoms
            Atom.mol_index = 0



            self.mols[-1].add_aa_top(path_dict['top_inp'])
            if coms:
                self.mols[-1].com = coms[len(self.mols)-1]
            else:
                self.mols[-1].compute_com()
            self.subbox_dict_mols[self.box.subbox(self.mols[-1].com)].append(self.mols[-1])

            beads = []
            for line in read_between("[atoms]", "[/atoms]", path_dict['top_out']):
                if len(line.split()) >= 2:
                    #bead_type = line.split()[1][:1]
                    bead_type = line.split()[1]
                    beads.append(Bead(self.mols[-1],
                                      self.ff.bead_types[bead_type],
                                      pos=self.mols[-1].com))
                self.mols[-1].add_bead(beads[-1])
            self.beads += beads
            self.mols[-1].add_cg_top(path_dict['top_out'])

        for mol in self.mols:
            #intermolecular_atoms = self.intermolecular_atoms(mol)
            mol.env_mols = self.env_mols(mol)
            inter_atoms, inter_beads = [], []
            for m in mol.env_mols:
                inter_atoms += m.atoms
                inter_beads += m.beads
            mol.intermolecular_atoms = inter_atoms
            mol.intermolecular_beads = inter_beads
            mol.make_ljs()
            mol.make_preference_axis()

        Atom.index = 0
        Bead.index = 0
        Mol.index = 0
        self.n_atoms = len(self.atoms)
        self.n_beads = len(self.beads)

        #self.energy = Energy(self.tops, self.box)

        #self.kick_atoms()
        self.kick_beads()