Ejemplo n.º 1
0
def load_rigimol_inp(filename, oname, _self=cmd):
    '''
DESCRIPTION

    Load the structures from a RigiMOL "inp" file.
    '''
    import shlex
    from chempy import Atom, Bond, models

    parsestate = ''
    model = None
    state = 0

    for line in open(filename):
        lex = shlex.shlex(line, posix=True)
        lex.whitespace_split = True
        lex.quotes = '"'
        a = list(lex)

        if not a:
            continue

        if a[0] == '+':
            if a[1] == 'NAME':
                assert a[2:] == [
                    'RESI', 'RESN', 'CHAIN', 'SEGI', 'X', 'Y', 'Z', 'B'
                ]
                parsestate = 'ATOMS'
                model = models.Indexed()
            elif a[1] == 'FROM':
                assert a[2:] == ['TO']
                parsestate = 'BONDS'
            else:
                parsestate = ''
        elif a[0] == '|':
            if parsestate == 'ATOMS':
                atom = Atom()
                atom.coord = [float(x) for x in a[6:9]]
                atom.name = a[1]
                atom.resi = a[2]
                atom.resn = a[3]
                atom.chain = a[4]
                atom.segi = a[5]
                atom.b = a[9]
                atom.symbol = ''
                model.add_atom(atom)
            elif parsestate == 'BONDS':
                bnd = Bond()
                bnd.index = [int(a[1]), int(a[2])]
                model.add_bond(bnd)
        elif a[0] == 'end':
            if parsestate in ('ATOMS', 'BONDS'):
                state += 1
                _self.load_model(model, oname, state=state)
            parsestate = ''
Ejemplo n.º 2
0
 def __init__(self,
              object,
              name='MMTK_model',
              configuration=None,
              b_values=None):
     self.object = object
     self.universe = object.universe()
     self.name = name
     self.model = Indexed()
     self.index_map = {}
     self.atoms = []
     chain_id_number = 0
     in_chain = True
     for o in object.bondedUnits():
         if Utility.isSequenceObject(o):
             groups = [(g, g.name) for g in o]
             if not in_chain:
                 chain_id_number = (chain_id_number + 1) % len(
                     self.chain_ids)
             in_chain = True
         else:
             groups = [(o, o.name)]
             in_chain = False
         residue_number = 1
         for g, g_name in groups:
             for a in g.atomList():
                 atom = Atom()
                 atom.symbol = a.symbol
                 atom.name = a.name
                 atom.resi_number = residue_number
                 atom.chain = self.chain_ids[chain_id_number]
                 if b_values is not None:
                     atom.b = b_values[a]
                 atom.resn = g_name
                 self.model.atom.append(atom)
                 self.index_map[a] = len(self.atoms)
                 self.atoms.append(a)
             residue_number = residue_number + 1
         if in_chain:
             chain_id_number = (chain_id_number + 1) % len(self.chain_ids)
         try:
             bonds = o.bonds
         except AttributeError:
             bonds = []
         for b in bonds:
             bond = Bond()
             bond.index = [self.index_map[b.a1], self.index_map[b.a2]]
             self.model.bond.append(bond)
     self._setCoordinates(configuration)
Ejemplo n.º 3
0
 def __init__(self, object, name = 'MMTK_model', configuration = None,
              b_values = None):
     self.object = object
     self.universe = object.universe()
     self.name = name
     self.model = Indexed()
     self.index_map = {}
     self.atoms = []
     chain_id_number = 0
     in_chain = True
     for o in object.bondedUnits():
         if Utility.isSequenceObject(o):
             groups = [(g, g.name) for g in o]
             if not in_chain:
                 chain_id_number = (chain_id_number+1) % len(self.chain_ids)
             in_chain = True
         else:
             groups = [(o, o.name)]
             in_chain = False
         residue_number = 1
         for g, g_name in groups:
             for a in g.atomList():
                 atom = Atom()
                 atom.symbol = a.symbol
                 atom.name = a.name
                 atom.resi_number = residue_number
                 atom.chain = self.chain_ids[chain_id_number]
                 if b_values is not None:
                     atom.b = b_values[a]
                 atom.resn = g_name
                 self.model.atom.append(atom)
                 self.index_map[a] = len(self.atoms)
                 self.atoms.append(a)
             residue_number = residue_number + 1
         if in_chain:
             chain_id_number = (chain_id_number+1) % len(self.chain_ids)
         try:
             bonds = o.bonds
         except AttributeError:
             bonds = []
         for b in bonds:
             bond = Bond()
             bond.index = [self.index_map[b.a1], self.index_map[b.a2]]
             self.model.bond.append(bond)
     self._setCoordinates(configuration)
Ejemplo n.º 4
0
    def fromList(self,list):   # currently no handling of conect records

        model = Indexed()

# read atoms
        cnt = 0
        at = None
        for rec in list:
            if (rec[0:4]=='ATOM') or (rec[0:6]=='HETATM'):
                at = Atom()
                if rec[0]=='A': at.hetatm=0  # default is 1
                at.index = cnt
                at.name = string.strip(rec[12:16])
                at.alt = string.strip(rec[16:17])
                at.resn = string.strip(rec[17:20])
                at.chain = string.strip(rec[21:22])
                at.resi = string.strip(rec[22:27]) # note: insertion is part of resi
                at.resi_number = int(rec[22:26])
                at.coord = [float(rec[30:38]),
                                float(rec[38:46]),
                                float(rec[46:54])]
                try:
                    at.q = float(rec[54:60])
                except ValueError:
                    at.q = 1.0
                try:
                    at.b = float(rec[60:66])
                except ValueError:
                    at.b = 0.0
                at.segi = string.strip(rec[72:76])
                at.symbol = string.strip(rec[76:78])
                if not len(at.symbol):
                    at.symbol = at.name[0:1]
                    if at.symbol in '012345678':
                        at.symbol = at.name[1:2]
                cnt = cnt + 1
                model.add_atom(at)
            elif (rec[0:3]=='TER'):
                if at:
                    at.ter=1
        return(model)
Ejemplo n.º 5
0
Archivo: pdb.py Proyecto: Almad/pymol
    def fromList(self,list):   # currently no handling of conect records 

        model = Indexed()
        
# read atoms
        cnt = 0
        at = None
        for rec in list:
            if (rec[0:4]=='ATOM') or (rec[0:6]=='HETATM'):
                at = Atom()
                if rec[0]=='A': at.hetatm=0  # default is 1
                at.index = cnt
                at.name = string.strip(rec[12:16])
                at.alt = string.strip(rec[16:17])
                at.resn = string.strip(rec[17:20])
                at.chain = string.strip(rec[21:22])
                at.resi = string.strip(rec[22:27]) # note: insertion is part of resi
                at.resi_number = int(rec[22:26])
                at.coord = [float(rec[30:38]), 
                                float(rec[38:46]),
                                float(rec[46:54])]
                try:
                    at.q = float(rec[54:60])
                except ValueError:
                    at.q = 1.0
                try:               
                    at.b = float(rec[60:66])
                except ValueError:
                    at.b = 0.0
                at.segi = string.strip(rec[72:76])
                at.symbol = string.strip(rec[76:78])
                if not len(at.symbol):
                    at.symbol = at.name[0:1]
                    if at.symbol in '012345678':
                        at.symbol = at.name[1:2]
                cnt = cnt + 1
                model.add_atom(at)
            elif (rec[0:3]=='TER'):
                if at:
                    at.ter=1
        return(model)
Ejemplo n.º 6
0
def convert_to_chempy_model(atom_array):
    """
    Convert an :class:`AtomArray` into a :class:`chempy.models.Indexed`
    object.

    Returns
    -------
    chempy_model : Indexed
        The converted structure.
    """
    model = IndexedModel()

    annot_cat = atom_array.get_annotation_categories()

    for i in range(atom_array.array_length()):
        atom = Atom()

        atom.segi = atom_array.chain_id[i]
        atom.chain = atom_array.chain_id[i]

        atom.resi_number = atom_array.res_id[i]

        atom.ins_code = atom_array.ins_code[i]

        res_name = atom_array.res_name[i]
        atom.resn = res_name
        if len(res_name) == 1:
            atom.resn_code = res_name
        else:
            try:
                atom.resn_code = ProteinSequence.convert_letter_3to1(res_name)
            except KeyError:
                atom.resn_code = "X"

        atom.hetatm = 1 if atom_array.hetero[i] else 0

        atom.name = atom_array.atom_name[i]

        atom.symbol = atom_array.element[i]

        if "b_factor" in annot_cat:
            atom.b = atom_array.b_factor[i]

        if "occupancy" in annot_cat:
            atom.q = atom_array.occupancy[i]

        if "charge" in annot_cat:
            atom.formal_charge = atom_array.charge[i]

        atom.coord = tuple(atom_array.coord[..., i, :])

        atom.index = i + 1

        model.add_atom(atom)

    if atom_array.bonds is not None:
        for i, j, bond_type in atom_array.bonds.as_array():
            bond = Bond()
            bond.order = BOND_ORDER[bond_type]
            bond.index = [i, j]
            model.add_bond(bond)
    else:
        warnings.warn(
            "The given atom array (stack) has no associated bond information")

    return model
Ejemplo n.º 7
0
def load_pdbml(filename, object='', discrete=0, multiplex=1, zoom=-1,
        quiet=1, _self=cmd):
    '''
DESCRIPTION

    Load a PDBML formatted structure file
    '''
    from chempy import Atom, models
    from collections import defaultdict

    multiplex, discrete = int(multiplex), int(discrete)

    try:
        root = etree.fromstring(_self.file_read(filename))
        PDBxNS = root.tag.rstrip('datablock')
        atom_site_list = root.findall('.'
                '/' + PDBxNS + 'atom_siteCategory'
                '/' + PDBxNS + 'atom_site')
    except etree.XMLSyntaxError:
        raise CmdException("File doesn't look like XML")
    except etree.XPathEvalError:
        raise CmdException("XML file doesn't look like a PDBML file")

    if not atom_site_list:
        raise CmdException("no PDBx:atom_site nodes found in XML file")

    # state -> model dictionary
    model_dict = defaultdict(models.Indexed)

    # atoms
    for atom_site in atom_site_list:
        atom = Atom()
        atom.coord = [None, None, None]

        model_num = 1

        for child in atom_site:
            tag = child.tag

            if tag == PDBxNS + 'Cartn_x':
                atom.coord[0] = float(child.text)
            elif tag == PDBxNS + 'Cartn_y':
                atom.coord[1] = float(child.text)
            elif tag == PDBxNS + 'Cartn_z':
                atom.coord[2] = float(child.text)
            elif tag == PDBxNS + 'B_iso_or_equiv':
                atom.b = float(child.text)
            elif tag == PDBxNS + 'auth_asym_id':
                atom.chain = child.text or ''
            elif tag == PDBxNS + 'auth_atom_id':
                atom.name = child.text or ''
            elif tag == PDBxNS + 'auth_comp_id':
                atom.resn = child.text or ''
            elif tag == PDBxNS + 'auth_seq_id':
                atom.resi = child.text or ''
            elif tag == PDBxNS + 'label_alt_id':
                atom.resi = child.text or ''
            elif tag == PDBxNS + 'label_asym_id':
                atom.segi = child.text or ''
            elif tag == PDBxNS + 'label_atom_id':
                if not atom.name:
                    atom.name = child.text or ''
            elif tag == PDBxNS + 'label_comp_id':
                if not atom.resn:
                    atom.resn = child.text or ''
            elif tag == PDBxNS + 'label_seq_id':
                if not atom.resi:
                    atom.resi = child.text or ''
            elif tag == PDBxNS + 'label_entity_id':
                atom.custom = child.text or ''
            elif tag == PDBxNS + 'occupancy':
                atom.q = float(child.text)
            elif tag == PDBxNS + 'pdbx_PDB_model_num':
                model_num = int(child.text)
            elif tag == PDBxNS + 'type_symbol':
                atom.symbol = child.text or ''
            elif tag == PDBxNS + 'group_PDB':
                atom.hetatm = (child.text == 'HETATM')

        if None not in atom.coord:
            model_dict[model_num].add_atom(atom)

    # symmetry and cell
    try:
        node = root.findall('.'
                '/' + PDBxNS + 'cellCategory'
                '/' + PDBxNS + 'cell')[0]
        cell = [
            float(node.findall('./' + PDBxNS + a)[0].text)
            for a in [
                'length_a', 'length_b', 'length_c', 'angle_alpha',
                'angle_beta', 'angle_gamma'
            ]
        ]

        spacegroup = root.findall('.'
                '/' + PDBxNS + 'symmetryCategory'
                '/' + PDBxNS + 'symmetry'
                '/' + PDBxNS + 'space_group_name_H-M')[0].text
    except IndexError:
        cell = None
        spacegroup = ''

    # object name
    if not object:
        object = os.path.basename(filename).split('.', 1)[0]

    # only multiplex if more than one model/state
    multiplex = multiplex and len(model_dict) > 1

    # load models as objects or states
    for model_num in sorted(model_dict):
        if model_num < 1:
            print(" Error: model_num < 1 not supported")
            continue

        model = model_dict[model_num]
        model.connect_mode = 3

        if cell:
            model.cell = cell
            model.spacegroup = spacegroup

        if multiplex:
            oname = '%s_%04d' % (object, model_num)
            model_num = 1
        else:
            oname = object

        _self.load_model(model, oname,
                state=model_num, zoom=zoom, discrete=discrete)
Ejemplo n.º 8
0
def load_3d(filename, object=''):
    '''
DESCRIPTION

    Load a survex 3d cave survey as "molecule"

    http://survex.com
    http://trac.survex.com/browser/trunk/doc/3dformat.htm
    '''
    from chempy import Atom, Bond, models
    from struct import unpack

    if object == '':
        object = os.path.splitext(os.path.basename(filename))[0]

    f = open(filename, 'rb')

    line = f.readline() # File ID
    if not line.startswith('Survex 3D Image File'):
        print " Error: not a Survex 3D File"
        raise CmdException

    line = f.readline() # File format version
    assert line[0] == 'v'
    ff_version = int(line[1:])

    line = unicode(f.readline(), 'latin1') # Survex title
    line = f.readline() # Timestamp

    class Station:
        def __init__(self):
            self.labels = []
            self.adjacent = []
            self.lrud = None
            self.flag = 0
        def connect(self, other):
            self.adjacent.append(other)
        def is_surface(self):
            return self.flag & 0x01
        def is_underground(self):
            return self.flag & 0x02
        def is_entrance(self):
            return self.flag & 0x04
        def is_exported(self):
            return self.flag & 0x08
        def is_fixed(self):
            return self.flag & 0x10

    class Survey(dict):
        def __init__(self):
            self.prev = None
            self.curr_label = ''
            self.labelmap = {}
        def get(self, xyz):
            return dict.setdefault(self, tuple(xyz), Station())
        def line(self, xyz):
            s = self.get(xyz)
            self.prev.connect(s)
            self.prev = s
        def move(self, xyz):
            s = self.get(xyz)
            self.prev = s
        def label(self, xyz, flag=0):
            s = self.get(xyz)
            s.labels.append(self.curr_label)
            self.labelmap[s.labels[-1]] = s
            if flag > 0:
                s.flag = flag
        def lrud(self, lrud):
            s = self.labelmap[self.curr_label]
            s.lrud = lrud

    survey = Survey()

    def read_xyz():
        return unpack('<iii', f.read(12))

    def read_len():
        len = read_byte()
        if len == 0xfe:
            len += unpack('<H', f.read(2))[0]
        elif len == 0xff:
            len += unpack('<I', f.read(4))[0]
        return len

    def read_label():
        len = read_len()
        if len > 0:
            survey.curr_label += skip_bytes(len)

    def skip_bytes(n):
        return f.read(n)

    def read_byte():
        byte = f.read(1)
        if len(byte) != 1:
            return -1
        return ord(byte)

    while 1:
        byte = read_byte()
        if byte == -1:
            break
        
        if byte == 0x00:
            # STOP
            survey.curr_label = ''
        elif byte <= 0x0e:
            # TRIM
            # FIXME: according to doc, trim 16 bytes, but img.c does 17!
            (i,n) = (-17,0)
            while n < byte:
                i -= 1
                if survey.curr_label[i] == '.': n += 1
            survey.curr_label = survey.curr_label[:i + 1]
        elif byte <= 0x0f:
            # MOVE
            xyz = read_xyz()
            survey.move(xyz)
        elif byte <= 0x1f:
            # TRIM
            survey.curr_label = survey.curr_label[:15 - byte]
        elif byte <= 0x20:
            # DATE
            if ff_version < 7:
                skip_bytes(4)
            else:
                skip_bytes(2)
        elif byte <= 0x21:
            # DATE
            if ff_version < 7:
                skip_bytes(8)
            else:
                skip_bytes(3)
        elif byte <= 0x22:
            # Error info
            skip_bytes(5 * 4)
        elif byte <= 0x23:
            # DATE
            skip_bytes(4)
        elif byte <= 0x24:
            # DATE
            continue
        elif byte <= 0x2f:
            # Reserved
            continue
        elif byte <= 0x31:
            # XSECT
            read_label()
            lrud = unpack('<hhhh', f.read(8))
            survey.lrud(lrud)
        elif byte <= 0x33:
            # XSECT
            read_label()
            lrud = unpack('<iiii', f.read(16))
            survey.lrud(lrud)
        elif byte <= 0x3f:
            # Reserved
            continue
        elif byte <= 0x7f:
            # LABEL
            read_label()
            xyz = read_xyz()
            survey.label(xyz, byte & 0x3f)
        elif byte <= 0xbf:
            # LINE
            read_label()
            xyz = read_xyz()
            survey.line(xyz)
        elif byte <= 0xff:
            # Reserved
            continue

    model = models.Indexed()
    for (xyz,s) in survey.iteritems():
        l0, _, l1 = s.labels[0].rpartition('.')
        resi, name = l1[:5], l1[5:]
        segi, chain, resn = l0[-8:-4], l0[-4:-3], l0[-3:]
        atom = Atom()
        atom.coord = [i/100.0 for i in xyz]
        atom.segi = segi
        atom.chain = chain
        atom.resn = resn
        atom.name = name
        atom.resi = resi
        atom.b = atom.coord[2]
        atom.label = s.labels[0]
        if s.lrud is not None:
            atom.vdw = sum(s.lrud)/400.0
        model.add_atom(atom)

    s2i = dict((s,i) for (i,s) in enumerate(survey.itervalues()))
    for (s,i) in s2i.iteritems():
        for o in s.adjacent:
            bnd = Bond()
            bnd.index = [i, s2i[o]]
            model.add_bond(bnd)

    cmd.load_model(model, object, 1)
    cmd.show_as('lines', object)
    cmd.spectrum('b', 'rainbow', object)
Ejemplo n.º 9
0
    def fromList(self, MMODList):

        model = Connected()

        # get header information
        nAtom = int(MMODList[0][1:6])
        model.molecule.title = string.strip(MMODList[0][8:])
        irec = 1

        # loop through atoms
        cnt = 0
        for a in range(nAtom):
            model.bond.append([])

        for a in range(nAtom):
            at = Atom()
            at.numeric_type = int(MMODList[irec][1:4])

            # extract connectivity information
            tokens = string.splitfields(MMODList[irec][5:52])
            at.neighbor = []
            at.bondorder = []

            for i in range(6):
                if tokens[2 * i] != "0":
                    a2 = int(tokens[2 * i]) - 1
                    if (a2 > cnt):
                        b = Bond()
                        b.index = [cnt, a2]
                        b.order = int(tokens[2 * i + 1])
                        model.bond[b.index[0]].append(
                            b)  # note two refs to same object
                        model.bond[b.index[1]].append(
                            b)  # note two refs to same object
                else:
                    break

# extract other information
            at.coord = [
                float(MMODList[irec][53:64]),
                float(MMODList[irec][65:76]),
                float(MMODList[irec][77:88])
            ]
            at.resi = string.strip(MMODList[irec][89:94])
            at.resi_number = int(at.resi)
            resn_code = string.strip(MMODList[irec][94:95])
            if len(resn_code): at.resn_code = resn_code
            color_code = string.strip(MMODList[irec][96:100])
            if color_code != '':
                at.color_code = int(color_code)
            else:
                at.color_code = 0
            chain = string.strip(MMODList[irec][95:96])
            if len(chain): at.chain = chain
            at.partial_charge = float(MMODList[irec][100:109])
            at.resn = MMODList[irec][119:123]
            name = string.strip(MMODList[irec][124:128])
            if len(name): at.name = name
            model.atom.append(at)
            irec = irec + 1
            cnt = cnt + 1

# fill in remaining datatypes
        cnt = 1
        for a in model.atom:
            a.text_type = MMOD_atom_data[a.numeric_type][0]
            a.symbol = MMOD_atom_data[a.numeric_type][1]
            a.formal_charge = MMOD_atom_data[a.numeric_type][4]
            cnt = cnt + 1

        return (model.convert_to_indexed())
Ejemplo n.º 10
0
    def fromList(self,MMODList):

        model = Connected()
        
# get header information
        nAtom = int(MMODList[0][1:6])
        model.molecule.title = string.strip(MMODList[0][8:])
        irec = 1

# loop through atoms
        cnt = 0
        for a in range(nAtom):
            model.bond.append([])
            
        for a in range(nAtom):
            at = Atom()
            at.numeric_type = int(MMODList[irec][1:4])

# extract connectivity information
            tokens = string.splitfields(MMODList[irec][5:52])
            at.neighbor = []
            at.bondorder = []

            for i in range(6):
                if tokens[2*i] != "0":
                    a2 = int(tokens[2*i])-1
                    if (a2>cnt):
                        b = Bond()
                        b.index = [cnt,a2]
                        b.order = int(tokens[2*i+1])
                        model.bond[b.index[0]].append(b) # note two refs to same object
                        model.bond[b.index[1]].append(b) # note two refs to same object 
                else:
                    break
            
# extract other information
            at.coord = [float(MMODList[irec][53:64]), 
                float(MMODList[irec][65:76]), float(MMODList[irec][77:88])]
            at.resi = string.strip(MMODList[irec][89:94])
            at.resi_number = int(at.resi)
            resn_code = string.strip(MMODList[irec][94:95])
            if len(resn_code): at.resn_code = resn_code
            color_code = string.strip(MMODList[irec][96:100])
            if color_code!='':
                at.color_code = int(color_code)
            else:
                at.color_code = 0
            chain = string.strip(MMODList[irec][95:96])
            if len(chain): at.chain = chain
            at.partial_charge = float(MMODList[irec][100:109])
            at.resn = MMODList[irec][119:123]
            name = string.strip(MMODList[irec][124:128])
            if len(name): at.name = name
            model.atom.append(at)
            irec = irec + 1
            cnt = cnt + 1
            
# fill in remaining datatypes
        cnt = 1
        for a in model.atom:
            a.text_type = MMOD_atom_data[a.numeric_type][0]
            a.symbol = MMOD_atom_data[a.numeric_type][1]
            a.formal_charge = MMOD_atom_data[a.numeric_type][4]
            cnt = cnt + 1
            
        return(model.convert_to_indexed())
Ejemplo n.º 11
0
def load_3d(filename, object=''):
    '''
DESCRIPTION

    Load a survex 3d cave survey as "molecule"

    http://survex.com
    http://trac.survex.com/browser/trunk/doc/3dformat.htm
    '''
    from chempy import Atom, Bond, models
    from struct import unpack

    if object == '':
        object = os.path.splitext(os.path.basename(filename))[0]

    f = open(filename, 'rb')

    line = f.readline()  # File ID
    if not line.startswith('Survex 3D Image File'):
        print(" Error: not a Survex 3D File")
        raise CmdException

    line = f.readline()  # File format version
    assert line[0] == 'v'
    ff_version = int(line[1:])

    line = f.readline().decode('latin1')  # Survex title
    line = f.readline()  # Timestamp

    class Station:
        def __init__(self):
            self.labels = []
            self.adjacent = []
            self.lrud = None
            self.flag = 0

        def connect(self, other):
            self.adjacent.append(other)

        def is_surface(self):
            return self.flag & 0x01

        def is_underground(self):
            return self.flag & 0x02

        def is_entrance(self):
            return self.flag & 0x04

        def is_exported(self):
            return self.flag & 0x08

        def is_fixed(self):
            return self.flag & 0x10

    class Survey(dict):
        def __init__(self):
            self.prev = None
            self.curr_label = ''
            self.labelmap = {}

        def get(self, xyz):
            return dict.setdefault(self, tuple(xyz), Station())

        def line(self, xyz):
            s = self.get(xyz)
            self.prev.connect(s)
            self.prev = s

        def move(self, xyz):
            s = self.get(xyz)
            self.prev = s

        def label(self, xyz, flag=0):
            s = self.get(xyz)
            s.labels.append(self.curr_label)
            self.labelmap[s.labels[-1]] = s
            if flag > 0:
                s.flag = flag

        def lrud(self, lrud):
            s = self.labelmap[self.curr_label]
            s.lrud = lrud

    survey = Survey()

    def read_xyz():
        return unpack('<iii', f.read(12))

    def read_len():
        len = read_byte()
        if len == 0xfe:
            len += unpack('<H', f.read(2))[0]
        elif len == 0xff:
            len += unpack('<I', f.read(4))[0]
        return len

    def read_label():
        len = read_len()
        if len > 0:
            survey.curr_label += skip_bytes(len)

    def skip_bytes(n):
        return f.read(n)

    def read_byte():
        byte = f.read(1)
        if len(byte) != 1:
            return -1
        return ord(byte)

    while 1:
        byte = read_byte()
        if byte == -1:
            break

        if byte == 0x00:
            # STOP
            survey.curr_label = ''
        elif byte <= 0x0e:
            # TRIM
            # FIXME: according to doc, trim 16 bytes, but img.c does 17!
            (i, n) = (-17, 0)
            while n < byte:
                i -= 1
                if survey.curr_label[i] == '.': n += 1
            survey.curr_label = survey.curr_label[:i + 1]
        elif byte <= 0x0f:
            # MOVE
            xyz = read_xyz()
            survey.move(xyz)
        elif byte <= 0x1f:
            # TRIM
            survey.curr_label = survey.curr_label[:15 - byte]
        elif byte <= 0x20:
            # DATE
            if ff_version < 7:
                skip_bytes(4)
            else:
                skip_bytes(2)
        elif byte <= 0x21:
            # DATE
            if ff_version < 7:
                skip_bytes(8)
            else:
                skip_bytes(3)
        elif byte <= 0x22:
            # Error info
            skip_bytes(5 * 4)
        elif byte <= 0x23:
            # DATE
            skip_bytes(4)
        elif byte <= 0x24:
            # DATE
            continue
        elif byte <= 0x2f:
            # Reserved
            continue
        elif byte <= 0x31:
            # XSECT
            read_label()
            lrud = unpack('<hhhh', f.read(8))
            survey.lrud(lrud)
        elif byte <= 0x33:
            # XSECT
            read_label()
            lrud = unpack('<iiii', f.read(16))
            survey.lrud(lrud)
        elif byte <= 0x3f:
            # Reserved
            continue
        elif byte <= 0x7f:
            # LABEL
            read_label()
            xyz = read_xyz()
            survey.label(xyz, byte & 0x3f)
        elif byte <= 0xbf:
            # LINE
            read_label()
            xyz = read_xyz()
            survey.line(xyz)
        elif byte <= 0xff:
            # Reserved
            continue

    model = models.Indexed()
    for (xyz, s) in survey.items():
        l0, _, l1 = s.labels[0].rpartition('.')
        resi, name = l1[:5], l1[5:]
        segi, chain, resn = l0[-8:-4], l0[-4:-3], l0[-3:]
        atom = Atom()
        atom.coord = [i / 100.0 for i in xyz]
        atom.segi = segi
        atom.chain = chain
        atom.resn = resn
        atom.name = name
        atom.resi = resi
        atom.b = atom.coord[2]
        atom.label = s.labels[0]
        if s.lrud is not None:
            atom.vdw = sum(s.lrud) / 400.0
        model.add_atom(atom)

    s2i = dict((s, i) for (i, s) in enumerate(survey.values()))
    for (s, i) in s2i.items():
        for o in s.adjacent:
            bnd = Bond()
            bnd.index = [i, s2i[o]]
            model.add_bond(bnd)

    cmd.load_model(model, object, 1)
    cmd.show_as('lines', object)
    cmd.spectrum('b', 'rainbow', object)
def load_3d(filename, object=''):
	'''
DESCRIPTION

    Load a survex 3d cave survey as "molecule"

    http://survex.com
	'''
	from chempy import Atom, Bond, models

	if object == '':
		import os
		object = os.path.splitext(os.path.basename(filename))[0]

	f = open(filename, 'rb')

	line = f.readline() # File ID
	if not line.startswith('Survex 3D Image File'):
		print " Error: not a Survex 3D File"
		raise CmdException

	line = f.readline() # File format version
	assert line[0] == 'v'
	ff_version = int(line[1:])

	line = unicode(f.readline(), 'latin1') # Survex title
	line = f.readline() # Timestamp

	class Station(tuple):
		def __new__(cls, xyz):
			return tuple.__new__(cls, xyz)
		def __init__(self, xyz):
			self.labels = []
			self.adjacent = []
		def connect(self, other):
			self.adjacent.append(other)

	class Survey(dict):
		def __init__(self):
			self.prev = None
			self.curr_label = ''
		def get(self, xyz):
			s = Station(xyz)
			return dict.setdefault(self, s, s)
		def line(self, xyz):
			s = self.get(xyz)
			self.prev.connect(s)
			self.prev = s
		def move(self, xyz):
			s = self.get(xyz)
			self.prev = s
		def label(self, xyz):
			s = survey.get(xyz)
			s.labels.append(self.curr_label)
		def __repr__(self):
			return 'Survey(' + repr(self.keys())[1:-1] + ')'

	survey = Survey()

	def read_xyz():
		x = read_int(4, 1)
		y = read_int(4, 1)
		z = read_int(4, 1)
		return [ x, y, z ]

	def read_int(len, sign):
		int = 0
		for i in range(len):
			int |= read_byte() << (8 * i)
		if sign and (int >> (8 * len - 1)):
			int -= (1 << 8 * len)
		return int

	def read_len():
		len = read_byte()
		if len == 0xfe:
			len += read_int(2, 0)
		elif len == 0xff:
			len = read_int(4, 0)
		return len

	def read_label():
		len = read_len()
		if len > 0:
			survey.curr_label += skip_bytes(len)

	def skip_bytes(n):
		return f.read(n)

	def read_byte():
		byte = f.read(1)
		if len(byte) != 1:
			return -1
		return ord(byte)

	while 1:
		byte = read_byte()
		if byte == -1:
			break
		
		if byte == 0x00:
			# STOP
			survey.curr_label = ''
		elif byte <= 0x0e:
			# TRIM
			(i,n) = (-16,0)
			while n < byte:
				i -= 1
				if survey.curr_label[i] == '.': n += 1
			survey.curr_label = survey.curr_label[:i + 1]
		elif byte <= 0x0f:
			# MOVE
			xyz = read_xyz()
			survey.move(xyz)
		elif byte <= 0x1f:
			# TRIM
			survey.curr_label = survey.curr_label[:15 - byte]
		elif byte <= 0x20:
			# DATE
			if ff_version < 7:
				skip_bytes(4)
			else:
				skip_bytes(2)
		elif byte <= 0x21:
			# DATE
			if ff_version < 7:
				skip_bytes(8)
			else:
				skip_bytes(3)
		elif byte <= 0x22:
			# Error info
			skip_bytes(5 * 4)
		elif byte <= 0x23:
			# DATE
			skip_bytes(4)
		elif byte <= 0x24:
			# DATE
			continue
		elif byte <= 0x2f:
			# Reserved
			continue
		elif byte <= 0x31:
			# XSECT
			read_label()
			skip_bytes(4 * 2)
		elif byte <= 0x33:
			# XSECT
			read_label()
			skip_bytes(4 * 4)
		elif byte <= 0x3f:
			# Reserved
			continue
		elif byte <= 0x7f:
			# LABEL
			read_label()
			xyz = read_xyz()
			survey.label(xyz)
		elif byte <= 0xbf:
			# LINE
			read_label()
			xyz = read_xyz()
			survey.line(xyz)
		elif byte <= 0xff:
			# Reserved
			continue

	model = models.Indexed()
	for s in survey:
		l0, _, l1 = s.labels[0].rpartition('.')
		resi, name = l1[:5], l1[5:]
#		segi, chain, resn = l0[:4],l0[-4:-3], l0[-3:]
		segi, chain, resn = l0[-8:-4], l0[-4:-3], l0[-3:]
		atom = Atom()
		atom.coord = [i/100.0 for i in s]
		atom.segi = segi
		atom.chain = chain
		atom.resn = resn
		atom.name = name
		atom.resi = resi
		atom.b = atom.coord[2]
		model.add_atom(atom)

	s2i = dict((s,i) for (i,s) in enumerate(survey))
	for s in survey:
		for o in s.adjacent:
			bnd = Bond()
			bnd.index = [s2i[s], s2i[o]]
			model.add_bond(bnd)

	cmd.load_model(model, object, 1)
	cmd.show_as('lines', object)
	cmd.spectrum('b', 'rainbow', object)
Ejemplo n.º 13
0
def get_model(proc, info,number):
	vmodel=Indexed()
	atoms=info["AtomsPerMolecule"][number]
	atomsread=0
	ratoms=True
	rcoords=False
	rbfactors=False
	rss=False
	first=True
	while(True):
		v=proc.stdout.readline()
		if "Molname" in v and ratoms:
			ad=json.loads(v)
			at=Atom()
			at.name=ad["Name"]
			at.symbol=ad["Symbol"]
			at.chain=ad["Chain"]
			at.id=ad["ID"]
			at.resi_number=ad["MolID"]
			at.resn=ad["Molname"]
			vmodel.atom.append(at)
			atomsread=atomsread+1
			if atomsread==atoms:
				ratoms=False
				rcoords=True
				atomsread=0
			continue
		if "Coords" in v and not "Molname" in v and rcoords:
			coords=json.loads(v)
			vmodel.atom[atomsread].coord=coords["Coords"]
			atomsread=atomsread+1
			if atomsread==atoms:
				rcoords=False
				atomsread=0
				if  info["Bfactors"]:
					rbfactors=True
				if info["SS"]:
					rss=True
			continue
		#In many cases this part will not be needed
		if "Bfactors" in v:
			bf=json.loads(v)
			vmodel.atom[atomsread].b=bf["Bfactors"]
			atomsread=atomsread+1
			if atomsread==atoms:
				atomsread=0
				rbfactors=False
				if info["SS"]:
					rss=True
			continue
		#This one should be needed only seldomly
		if "SS" in v:
			SS=json.loads(v)
			vmodel.atom[atomsread].ss=SS["SS"]
			++atomsread
			if atomsread==atoms:
				atomsread=0
				rss=False
			continue
#		print "me fui con una deuda de 500"
		break
	return vmodel
Ejemplo n.º 14
0
def _to_chempy(data, use_auth=True):
    '''
    Construct a "chempy" model (molecule) from decoded MMTF data.
    '''
    from itertools import islice
    from chempy import models, Atom, Bond

    def add_bond(i1, i2, order, offset=0):
        bond = Bond()
        bond.order = order
        bond.index = [i1 + offset, i2 + offset]
        model.add_bond(bond)

    coord_iter = data.get_table_iter([
        'xCoordList',
        'yCoordList',
        'zCoordList',
    ])

    atom_iter = data.get_table_iter([
        'bFactorList',
        'occupancyList',
        'altLocList',
        'atomIdList',
    ], [0.0, 1.0, '', -1])

    group_iter = data.get_table_iter([
        'groupTypeList',
        'sequenceIndexList',
        'groupIdList',
        'insCodeList',
        'secStructList',
    ])

    chain_list_iter = enumerate(
        data.get_table_iter([
            'chainIdList',
            'chainNameList',
            'groupsPerChain',
        ]))

    groupList = data.get('groupList')

    symmetry = (
        data.get('unitCell', None),
        as_str(data.get('spaceGroup', '')),
    )

    model_output = []

    for n_chains in data.get_iter('chainsPerModel'):
        model = models.Indexed()
        model_output.append(model)

        if symmetry[0] is not None:
            model.cell, model.spacegroup = symmetry

        for (chain_idx, (segi, chain,
                         n_groups)) in islice(chain_list_iter, n_chains):
            for (groupType, label_seq_id, auth_seq_id, ins_code, ss_info) in \
                    islice(group_iter, n_groups):

                group = groupList[groupType]
                resn = as_str(group[b'groupName'])

                group_bond_iter = izip(
                    group[b'bondAtomList'][0::2],
                    group[b'bondAtomList'][1::2],
                    group[b'bondOrderList'],
                )

                offset = len(model.atom)
                for (i1, i2, order) in group_bond_iter:
                    add_bond(i1, i2, order, offset)

                group_atom_iter = izip(
                    group[b'atomNameList'],
                    group[b'elementList'],
                    group[b'formalChargeList'],
                )

                for (name, elem, formal_charge) in group_atom_iter:
                    atom = Atom()

                    (atom.b, atom.q, atom.alt, atom.id) = next(atom_iter)

                    atom.coord = next(coord_iter)
                    atom.symbol = as_str(elem)
                    atom.name = as_str(name)
                    atom.resn = resn
                    atom.hetatm = label_seq_id == -1
                    atom.formal_charge = formal_charge
                    atom.segi = segi
                    atom.chain = chain
                    atom.ss = ss_map.get(ss_info, '')

                    if use_auth or label_seq_id is None:
                        atom.resi = auth_seq_id
                        atom.ins_code = ins_code or ''
                    else:
                        atom.resi = label_seq_id + 1

                    model.add_atom(atom)

    model_atom_max = 0
    model_atom_min = 0
    model_iter = iter(model_output)
    bondAtomList_iter = data.get_iter('bondAtomList')

    for order in data.get_iter('bondOrderList'):
        i1 = next(bondAtomList_iter)
        i2 = next(bondAtomList_iter)
        if i1 >= model_atom_max or i2 >= model_atom_max:
            model = next(model_iter)
            model_atom_min = model_atom_max
            model_atom_max += len(model.atom)
        add_bond(i1, i2, order, -model_atom_min)

    return model_output
Ejemplo n.º 15
0
def load_pdbml(filename, object='', discrete=0, multiplex=1, zoom=-1,
        quiet=1, _self=cmd):
    '''
DESCRIPTION

    Load a PDBML formatted structure file
    '''
    from chempy import Atom, models
    from collections import defaultdict

    multiplex, discrete = int(multiplex), int(discrete)

    try:
        root = etree.fromstring(_self.file_read(filename))
        PDBxNS = root.tag.rstrip('datablock')
        atom_site_list = root.findall('.'
                '/' + PDBxNS + 'atom_siteCategory'
                '/' + PDBxNS + 'atom_site')
    except etree.XMLSyntaxError:
        raise CmdException("File doesn't look like XML")
    except etree.XPathEvalError:
        raise CmdException("XML file doesn't look like a PDBML file")

    if not atom_site_list:
        raise CmdException("no PDBx:atom_site nodes found in XML file")

    # state -> model dictionary
    model_dict = defaultdict(models.Indexed)

    # atoms
    for atom_site in atom_site_list:
        atom = Atom()
        atom.coord = [None, None, None]

        model_num = 1

        for child in atom_site:
            tag = child.tag

            if tag == PDBxNS + 'Cartn_x':
                atom.coord[0] = float(child.text)
            elif tag == PDBxNS + 'Cartn_y':
                atom.coord[1] = float(child.text)
            elif tag == PDBxNS + 'Cartn_z':
                atom.coord[2] = float(child.text)
            elif tag == PDBxNS + 'B_iso_or_equiv':
                atom.b = float(child.text)
            elif tag == PDBxNS + 'auth_asym_id':
                atom.chain = child.text or ''
            elif tag == PDBxNS + 'auth_atom_id':
                atom.name = child.text or ''
            elif tag == PDBxNS + 'auth_comp_id':
                atom.resn = child.text or ''
            elif tag == PDBxNS + 'auth_seq_id':
                atom.resi = child.text or ''
            elif tag == PDBxNS + 'label_alt_id':
                atom.resi = child.text or ''
            elif tag == PDBxNS + 'label_asym_id':
                atom.segi = child.text or ''
            elif tag == PDBxNS + 'label_atom_id':
                if not atom.name:
                    atom.name = child.text or ''
            elif tag == PDBxNS + 'label_comp_id':
                if not atom.resn:
                    atom.resn = child.text or ''
            elif tag == PDBxNS + 'label_seq_id':
                if not atom.resi:
                    atom.resi = child.text or ''
            elif tag == PDBxNS + 'label_entity_id':
                atom.custom = child.text or ''
            elif tag == PDBxNS + 'occupancy':
                atom.q = float(child.text)
            elif tag == PDBxNS + 'pdbx_PDB_model_num':
                model_num = int(child.text)
            elif tag == PDBxNS + 'type_symbol':
                atom.symbol = child.text or ''
            elif tag == PDBxNS + 'group_PDB':
                atom.hetatm = (child.text == 'HETATM')

        if None not in atom.coord:
            model_dict[model_num].add_atom(atom)

    # symmetry and cell
    try:
        node = root.findall('.'
                '/' + PDBxNS + 'cellCategory'
                '/' + PDBxNS + 'cell')[0]
        cell = [
            float(node.findall('./' + PDBxNS + a)[0].text)
            for a in [
                'length_a', 'length_b', 'length_c', 'angle_alpha',
                'angle_beta', 'angle_gamma'
            ]
        ]

        spacegroup = root.findall('.'
                '/' + PDBxNS + 'symmetryCategory'
                '/' + PDBxNS + 'symmetry'
                '/' + PDBxNS + 'space_group_name_H-M')[0].text
    except IndexError:
        cell = None
        spacegroup = ''

    # object name
    if not object:
        object = os.path.basename(filename).split('.', 1)[0]

    # only multiplex if more than one model/state
    multiplex = multiplex and len(model_dict) > 1

    # load models as objects or states
    for model_num in sorted(model_dict):
        if model_num < 1:
            print(" Error: model_num < 1 not supported")
            continue

        model = model_dict[model_num]
        model.connect_mode = 3

        if cell:
            model.cell = cell
            model.spacegroup = spacegroup

        if multiplex:
            oname = '%s_%04d' % (object, model_num)
            model_num = 1
        else:
            oname = object

        _self.load_model(model, oname,
                state=model_num, zoom=zoom, discrete=discrete)
Ejemplo n.º 16
0
def GetModel(proc, info, number):
    vmodel = Indexed()
    atoms = info["AtomsPerMolecule"][number]
    states = info["FramesPerMolecule"][number]
    statecontainer = States(atoms)
    atomsread = 0
    statesread = 0
    ratoms = True
    rcoords = False
    rbfactors = False
    rss = False
    first = True
    while (True):
        v = proc.stdout.readline().decode("utf-8")
        if "Molname" in v and ratoms:
            ad = json.loads(v)
            at = Atom()
            at.name = ad["Name"]
            at.symbol = ad["Symbol"]
            at.chain = ad["Chain"]
            at.id = ad["ID"]
            at.resi_number = ad["MolID"]
            at.resn = ad["Molname"]
            at.hetatm = 1
            if at.resn in resncodes.keys():
                at.hetatm = 0
                at.resn_code = resncodes[ad["Molname"]]
            vmodel.atom.append(at)
            atomsread = atomsread + 1
            if atomsread == atoms:
                ratoms = False
                rcoords = True
                atomsread = 0
            continue
        if "Coords" in v and not "Molname" in v and rcoords:
            coords = json.loads(v)
            if statesread == 0:
                vmodel.atom[atomsread].coord = coords["Coords"]
            else:
                statecontainer.NewCoords(coords["Coords"])
            #	print("coords added to state!!!") #############3
            atomsread = atomsread + 1
            if atomsread == atoms:
                statesread += 1
                atomsread = 0
                if statesread == states:
                    rcoords = False
                    if info["Bfactors"]:
                        rbfactors = True
                    if info["SS"]:
                        rss = True
                else:
                    statecontainer.NewState()
            #		print("NEW STATE!") ########3

            continue
        #In many cases this part will not be needed
        if "Bfactors" in v:
            bf = json.loads(v)
            vmodel.atom[atomsread].b = bf["Bfactors"]
            atomsread = atomsread + 1
            if atomsread == atoms:
                atomsread = 0
                rbfactors = False
                if info["SS"]:
                    rss = True
            continue
        #This one should be needed only seldom
        if "SS" in v:
            SS = json.loads(v)
            vmodel.atom[atomsread].ss = SS["SS"]
            ++atomsread
            if atomsread == atoms:
                atomsread = 0
                rss = False
            continue
#		print "me fui con una deuda de 500"
        break
    #i don't like this, but I don't want to break my other plugins by changing the signature of the function
    #for them. Also, most pugins will only get one state anyway.
    if statecontainer.States() < 1:
        return vmodel
    else:
        return (vmodel, statecontainer)