Example #1
0
def write_bgf(atoms,description=None):

    uc = atoms.get_cell()
    a,b,c = [Vector(x) for x in uc]
    A = a.length()
    B = b.length()
    C = c.length()

    rad2deg = 360./(2*math.pi)
    alpha = b.angle(c)*rad2deg
    beta  = a.angle(c)*rad2deg
    gamma = a.angle(b)*rad2deg

    crstx = str(FortranLine(('CRYSTX',
                             A,
                             B,
                             C,
                             alpha,
                             beta,
                             gamma),
                             FortranFormat('a6,1x,6f11.5')))

    atom_lines = []
    fmt = FortranFormat('a6,1x,i5,1x,a5,1x,a3,1x,a1,1x,a5,3f10.5,1x,a5,i3,i2,1x,f8.5')
    for i,atom in enumerate(atoms):
        
        line = str(FortranLine(('HETATM',
                               i,
                               atom.symbol,
                               '',
                               '',
                               '',
                               atom.x,
                               atom.y,
                               atom.z,
                               atom.symbol,
                               1,
                               1,
                               0.0),
                               fmt))
        atom_lines.append(line)

    # energy in kcal
    energy = atoms.get_potential_energy()*23.061


    bgf = Template(bgf_template, searchList=[locals()])

    return bgf.respond()
 def _readLJParameters(self, file, ljpar_set):
     format = FortranFormat('2X,A2,6X,3F10.6')
     while 1:
         l = FortranLine(file.readline()[:-1], format)
         if l.isBlank(): break
         name = _normalizeName(l[0])
         ljpar_set.addEntry(name, l[1], l[2], l[3])
 def _readImproperParameters(self, file):
     format = FortranFormat('A2,1X,A2,1X,A2,1X,A2,I4,3F15.2')
     while 1:
         l = FortranLine(file.readline()[:-1], format)
         if l.isBlank(): break
         name1 = _normalizeName(l[0])
         name2 = _normalizeName(l[1])
         name3 = _normalizeName(l[2])
         name4 = _normalizeName(l[3])
         if name1 == 'X':
             if name2 == 'X':
                 name1 = name3
                 name2 = name4
                 name3 = 'X'
                 name4 = 'X'
             else:
                 name1 = name3
                 name2, name3 = _sort(name2, name4)
                 name4 = 'X'
         else:
             name1, name2, name3, name4 = \
                    (name3, ) + _sort3(name1, name2, name4)
         p = AmberImproperParameters(self.atom_types[name1],
                                     self.atom_types[name2],
                                     self.atom_types[name3],
                                     self.atom_types[name4], l[4], l[5],
                                     l[6], l[7])
         if name4 == 'X':
             if name3 == 'X':
                 self.impropers_2[(name1, name2)] = p
             else:
                 self.impropers_1[(name1, name2, name3)] = p
         else:
             self.impropers[(name1, name2, name3, name4)] = p
 def _readDihedralParameters(self, file):
     format = FortranFormat('A2,1X,A2,1X,A2,1X,A2,I4,3F15.2')
     append = None
     while 1:
         l = FortranLine(file.readline()[:-1], format)
         if l.isBlank(): break
         name1 = _normalizeName(l[0])
         name2 = _normalizeName(l[1])
         name3 = _normalizeName(l[2])
         name4 = _normalizeName(l[3])
         name1, name2, name3, name4 = _sort4(name1, name2, name3, name4)
         if append is not None:
             append.addTerm(l[4], l[5], l[6], l[7])
             if l[7] >= 0: append = None
         else:
             p = AmberDihedralParameters(self.atom_types[name1],
                                         self.atom_types[name2],
                                         self.atom_types[name3],
                                         self.atom_types[name4], l[4], l[5],
                                         l[6], l[7])
             if name1 == 'X' and name4 == 'X':
                 self.dihedrals_2[(name2, name3)] = p
             else:
                 self.dihedrals[(name1, name2, name3, name4)] = p
             if l[7] < 0: append = p
 def _readAtomTypes(self, file):
     format = FortranFormat('A2,1X,F10.2')
     while 1:
         l = FortranLine(file.readline()[:-1], format)
         if l.isBlank(): break
         name = _normalizeName(l[0])
         self.atom_types[name] = AmberAtomType(name, l[1])
 def _readHbondParameters(self, file):
     format = FortranFormat('2X,A2,2X,A2,2X,2F10.2')
     while 1:
         l = FortranLine(file.readline()[:-1], format)
         if l.isBlank(): break
         name1 = _normalizeName(l[0])
         name2 = _normalizeName(l[1])
         name1, name2 = _sort(name1, name2)
         self.hbonds[(name1, name2)] = \
                             AmberHbondParameters(self.atom_types[name1],
                                                  self.atom_types[name2],
                                                  l[2], l[3])
 def _readAngleParameters(self, file):
     format = FortranFormat('A2,1X,A2,1X,A2,2F10.2')
     while 1:
         l = FortranLine(file.readline()[:-1], format)
         if l.isBlank(): break
         name1 = _normalizeName(l[0])
         name2 = _normalizeName(l[1])
         name3 = _normalizeName(l[2])
         name1, name3 = _sort(name1, name3)
         self.bond_angles[(name1, name2, name3)] = \
                AmberBondAngleParameters(self.atom_types[name1],
                                         self.atom_types[name2],
                                         self.atom_types[name3],
                                         l[3], l[4])
Example #8
0
    def __init__(self,atoms,outfile=None):

        unitcell = atoms.get_cell()
        A = Vector(unitcell[0])
        B = Vector(unitcell[1])
        C = Vector(unitcell[2])

        # lengths of the vectors
        a = A.length()#*angstroms2bohr
        b = B.length()#*angstroms2bohr
        c = C.length()#*angstroms2bohr

        # angles between the vectors
        rad2deg = 360./(2.*math.pi)
        alpha = B.angle(C)*rad2deg
        beta = A.angle(C)*rad2deg
        gamma = A.angle(B)*rad2deg

        # scaledpositions = atoms.get_scaled_positions()
        # chemicalsymbols = [atom.get_symbol() for atom in atoms]

        input = ''
        input += '%1.3f %1.3f %1.3f %1.3f %1.3f %1.3f\n' % (a,b,c,
                                                            alpha,beta,gamma)
        input += '1 1 0.1 0.1\n'

        for atom in atoms:
            sym = atom.get_symbol()
            group = 1
            x,y,z = atom.get_position()
            #format(a6,i2,6f9.5)
            input += str(FortranLine((sym,
                                      group,
                                      x,y,z),
                                     FortranFormat('a6,i2,3f9.5')))+'\n'

        pin,pout = os.popen2('symmol')
        pin.writelines(input)
        pin.close()
        self.output = pout.readlines()
        pout.close()

        if outfile:
            f = open(outfile,'w')
            f.writelines(self.output)
            f.close()

        if os.path.exists('symmol.log'):
            os.remove('symmol.log')
                dy = event.y() - self.click3y
            except AttributeError:
                return
            if dy != 0:
                ratio = -dy/self.plotbox_size[1]
                self.scale = self.scale * (1.+ratio)
        self.update()


if __name__ == '__main__':

    from Scientific.IO.TextFile import TextFile
    from Scientific.IO.FortranFormat import FortranFormat, FortranLine
    import string

    generic_format = FortranFormat('A6')
    atom_format = FortranFormat('A6,I5,1X,A4,A1,A3,1X,A1,I4,A1,' +
                                '3X,3F8.3,2F6.2,7X,A4,2A2')

    # Read the PDB file and make a list of all C-alpha positions
    def readCAlphaPositions(filename):
        positions = []
        chains = [positions]
        for line in TextFile(filename):
            record_type = FortranLine(line, generic_format)[0]
            if record_type == 'ATOM  ' or record_type == 'HETATM':
                data = FortranLine(line, atom_format)
                atom_name = string.strip(data[2])
                position = N.array(data[8:11])
                if atom_name == 'CA':
                    positions.append(position)
  >>>print conf
  >>>for residue in conf.residues:
  >>>    for atom in residue:
  >>>        print atom
"""

from Scientific.IO.TextFile import TextFile
from Scientific.IO.FortranFormat import FortranFormat, FortranLine
from Scientific.Geometry import Vector, Tensor
from PDBExportFilters import export_filters
import copy, string

#
# Fortran formats for PDB entries
#
atom_format = FortranFormat('A6,I5,1X,A4,A1,A4,A1,I4,A1,3X,3F8.3,2F6.2,' +
                            '6X,A4,2A2')
anisou_format = FortranFormat('A6,I5,1X,A4,A1,A4,A1,I4,A1,1X,6I7,2X,A4,2A2')
conect_format = FortranFormat('A6,11I5')
ter_format = FortranFormat('A6,I5,6X,A4,A1,I4,A1')
model_format = FortranFormat('A6,4X,I4')
header_format = FortranFormat('A6,4X,A40,A9,3X,A4')
generic_format = FortranFormat('A6,A74')

#
# Amino acid and nucleic acid residues
#
amino_acids = [
    'ALA', 'ARG', 'ASN', 'ASP', 'CYS', 'CYX', 'GLN', 'GLU', 'GLY', 'HIS',
    'HID', 'HIE', 'HIP', 'HSD', 'HSE', 'HSP', 'ILE', 'LEU', 'LYS', 'MET',
    'PHE', 'PRO', 'SER', 'THR', 'TRP', 'TYR', 'VAL', 'ACE', 'NME', 'NHE'
]
    def __init__(self, file, modifications=[]):
        if isinstance(file, str):
            file = TextFile(file)
        title = file.readline()[:-1]

        self.atom_types = DictWithDefault(None)
        self._readAtomTypes(file)

        format = FortranFormat('20(A2,2X)')
        done = 0
        while not done:
            l = FortranLine(file.readline()[:-1], format)
            for entry in l:
                name = _normalizeName(entry)
                if len(name) == 0:
                    done = 1
                    break
                try:  # ignore errors for now
                    self.atom_types[name].hydrophylic = 1
                except:
                    pass

        self.bonds = {}
        self._readBondParameters(file)

        self.bond_angles = {}
        self._readAngleParameters(file)

        self.dihedrals = {}
        self.dihedrals_2 = {}
        self._readDihedralParameters(file)

        self.impropers = {}
        self.impropers_1 = {}
        self.impropers_2 = {}
        self._readImproperParameters(file)

        self.hbonds = {}
        self._readHbondParameters(file)

        self.lj_equivalent = {}
        format = FortranFormat('20(A2,2X)')
        while 1:
            l = FortranLine(file.readline()[:-1], format)
            if l.isBlank(): break
            name1 = _normalizeName(l[0])
            for s in l[1:]:
                name2 = _normalizeName(s)
                self.lj_equivalent[name2] = name1

        self.ljpar_sets = {}
        while 1:
            l = FortranLine(file.readline()[:-1], 'A4,6X,A2')
            if l[0] == 'END ': break
            set_name = _normalizeName(l[0])
            ljpar_set = AmberLJParameterSet(set_name, l[1])
            self.ljpar_sets[set_name] = ljpar_set
            self._readLJParameters(file, ljpar_set)

        file.close()

        for mod, ljname in modifications:
            if isinstance(mod, str):
                file = TextFile(mod)
            else:
                file = mod
            title = file.readline()[:-1]
            blank = file.readline()[:-1]
            while 1:
                keyword = file.readline()
                if not keyword: break
                keyword = string.strip(keyword)[:4]
                if keyword == 'MASS':
                    self._readAtomTypes(file)
                elif keyword == 'BOND':
                    self._readBondParameters(file)
                elif keyword == 'ANGL':
                    self._readAngleParameters(file)
                elif keyword == 'DIHE':
                    self._readDihedralParameters(file)
                elif keyword == 'IMPR':
                    self._readImproperParameters(file)
                elif keyword == 'HBON':
                    self._readHbondParameters(file)
                elif keyword == 'NONB':
                    self._readLJParameters(file, self.ljpar_sets[ljname])
Example #12
0
1) Type 'mkfifo HISTORY' in dlpoly_directory.
2) Start dlpoly_to_nc.
3) Start DLPOLY.
"""

_elements2 = [
    'cl', 'as', 'in', 'tb', 'tl', 'he', 'ar', 'se', 'sn', 'dy', 'pb', 'li',
    'br', 'sb', 'ho', 'bi', 'be', 'ca', 'kr', 'te', 'er', 'po', 'sc', 'rb',
    'tm', 'at', 'ti', 'sr', 'xe', 'yb', 'rn', 'cs', 'lu', 'fr', 'cr', 'zr',
    'ba', 'hf', 'ra', 'mn', 'nb', 'la', 'ta', 'ac', 'ne', 'fe', 'mo', 'ce',
    'th', 'na', 'co', 'tc', 'pr', 're', 'pa', 'mg', 'ni', 'ru', 'nd', 'os',
    'al', 'cu', 'rh', 'pm', 'ir', 'np', 'si', 'zn', 'pd', 'sm', 'pt', 'pu',
    'ga', 'ag', 'eu', 'au', 'am', 'ge', 'cd', 'gd', 'hg', 'cm', 'cf'
]

field_atom_line = FortranFormat('A8,2F12,3I5')
history_timestep_line = FortranFormat('A8,4I10,F12.6')
history_pbc_line = FortranFormat('3G12.4')


class DLPOLYData:
    def __init__(self, directory):
        self.directory = directory
        self.history = open(os.path.join(self.directory, 'HISTORY'))
        self.title = string.strip(self.history.readline())
        info = FortranLine(self.history.readline(), '2I10')
        if info[1] > 3:
            raise ValueError, "box shape not implemented"
        nvectors = info[0] + 1
        self.makeUniverse(info[1], self.readField())
        if nvectors > 1:
Example #13
0
def write_bgf(atoms, geofile=None):
    '''
	prepares a bgf entry from an atoms object and writes it to the geofile
	'''
    if geofile is None:
        geofile = 'geo'

    # create the geofile if it does not already exist
    if not os.path.exists(geofile):
        f = open(geofile, 'w')
        f.close()

    # get descriptions to make sure they are all unique.
    f = open(geofile, 'r')
    DESCRIPTIONS = []
    for line in f.readlines():
        if line.startswith('DESCRP'):
            DESCRIPTIONS.append(line[6:].strip())
    f.close()

    # we need a hash of something to check for uniqueness of the atoms.
    description = reax_hash(atoms)

    if description in DESCRIPTIONS:
        raise Exception, 'Non-unique description "%s" in geofile' % description

    # now prepare the unit cell information for the CRYSTX line
    uc = atoms.get_cell()
    a, b, c = [Vector(x) for x in uc]
    A = a.length()
    B = b.length()
    C = c.length()

    rad2deg = 360. / (2 * math.pi)
    alpha = b.angle(c) * rad2deg
    beta = a.angle(c) * rad2deg
    gamma = a.angle(b) * rad2deg

    crstx = str(
        FortranLine(('CRYSTX', A, B, C, alpha, beta, gamma),
                    FortranFormat('a6,1x,6f11.5')))

    # now we prepare each atom line. These are printed in the template.
    atom_lines = []
    reax_coords = reax_coordinates(atoms)
    fmt = FortranFormat(
        'a6,1x,i5,1x,a5,1x,a3,1x,a1,1x,a5,3f10.5,1x,a5,i3,i2,1x,f8.5')
    for i, atom in enumerate(atoms):
        x, y, z = reax_coords[i]
        line = FortranLine(('HETATM', i, atom.symbol, '', '', '', x, y, z,
                            atom.symbol, 1, 1, 0.0), fmt)
        atom_lines.append(line)

    # energy in kcal
    calc = atoms.get_calculator()
    if calc is not None:
        directory = os.path.abspath(calc.vaspdir)
        energy = atoms.get_potential_energy() * 23.061
    else:
        directory = 'None'
        energy = 'None'

    bgf = Template(bgf_template, searchList=[locals()])

    entry = bgf.respond()

    if geofile is None:
        geofile = 'geo'

    f = open(geofile, 'a')
    f.write(entry)
    f.close()
from Scientific import N as Num

# The MMTK distribution modules
from MMTK.PDB import PDBOutputFile
from MMTK import Units
from MMTK.Trajectory import Trajectory

# The nMOLDYN modules
from nMOLDYN.GlobalVariables import GVAR
from nMOLDYN.Preferences import PREFERENCES
from nMOLDYN.Core.Error import Error
from nMOLDYN.Core.IO import load_trajectory_file
from nMOLDYN.Core.Logger import LogMessage
from nMOLDYN.GUI.Widgets import *

atom_format = FortranFormat(
    'A6,I5,1X,A4,A1,A4,A1,I4,A1,3X,3F8.3,2F6.2,6X,A4,2A2')


class PDBSnapshotGeneratorDialog(PortableToplevel):
    """Sets up a dialog used to export one or several trajectory frames into a PDB file. 
    
    @note: if a trajectory has been previously loaded in nMOLDYN this will be the one proposed for extraction 
        by default. Otherwise, the user can still choose a trajectory to visualize from the dialog.
    """
    def __init__(self, parent, title=None):
        """The constructor.
        
        @param parent: the parent widget.
        
        @param title: a string specifying the title of the dialog.
        @type title: string