19 import os, tempfile, ftplib, httplib
22 from ost
import mol, conop
31 return IOProfileRegistry.Instance().Get(key)
34 if isinstance(value, str):
36 IOProfileRegistry.Instance().Set(key, value)
40 return len(self.
_dict)
43 return self._dict.__iter__()
47 profiles[
'STRICT']=
IOProfile(dialect=
'PDB', fault_tolerant=
False,
48 strict_hydrogens=
False, quack_mode=
False)
49 profiles[
'SLOPPY']=
IOProfile(dialect=
'PDB', fault_tolerant=
True,
50 strict_hydrogens=
False, quack_mode=
True)
51 profiles[
'CHARMM']=
IOProfile(dialect=
'CHARMM', fault_tolerant=
True,
52 strict_hydrogens=
False, quack_mode=
False)
53 profiles[
'DEFAULT']=
'STRICT'
55 def _override(val1, val2):
61 def __GetModelFromPDB(model_id, output_dir, file_pattern='pdb%s.ent.gz'):
62 file_name = file_pattern % model_id
63 file_path = os.path.join(output_dir,file_name)
65 server=
"ftp.wwpdb.org"
66 ftp=ftplib.FTP(server,
"anonymous",
"user@")
67 ftp.cwd(
"pub/pdb/data/structures/all/pdb")
68 ftp_retrfile=open(file_path,
"wb")
69 ftp.retrbinary(
"RETR "+file_name,ftp_retrfile.write)
72 conn=httplib.HTTPConnection(
'www.pdb.org')
73 conn.request(
'GET',
'/pdb/files/%s.pdb.gz' % model_id )
74 response=conn.getresponse()
75 if response.status==200:
77 f=open(os.path.join(output_dir, file_pattern % model_id),
'w+')
83 return os.path.getsize(file_path) > 0
85 def LoadPDB(filename, restrict_chains="", no_hetatms=None,
86 fault_tolerant=
None, load_multi=
False, quack_mode=
None,
87 join_spread_atom_records=
None, calpha_only=
None,
88 profile=
'DEFAULT', remote=
False, dialect=
None,
89 strict_hydrogens=
None):
91 Load PDB file from disk and returns one or more entities. Several options
92 allow to customize the exact behaviour of the PDB import. For more information
93 on these options, see :doc:`profile`.
95 Residues are flagged as ligand if they are mentioned in a HET record.
97 :param restrict_chains: If not an empty string, only chains listed in the
98 string will be imported.
100 :param fault_tolerant: Enable/disable fault-tolerant import. If set, overrides
101 the value of :attr:`IOProfile.fault_tolerant`.
103 :param no_hetatms: If set to True, HETATM records will be ignored. Overrides
104 the value of :attr:`IOProfile.no_hetatms`
106 :param load_multi: If set to True, a list of entities will be returned instead
107 of only the first. This is useful when dealing with multi-PDB files.
109 :param join_spread_atom_records: If set, overrides the value of
110 :attr:`IOProfile.join_spread_atom_records`.
112 :param remote: If set to true, the method tries to load the pdb from the
113 remote pdb repository www.pdb.org. The filename is then interpreted as the
116 :rtype: :class:`~ost.mol.EntityHandle` or a list thereof if `load_multi` is
119 :param dialect: Specifies the particular dialect to use. If set, overrides
120 the value of :attr:`IOProfile.dialect`
122 :type dialect: :class:`str`
124 :param strict_hydrogens: If set, overrides the value of
125 :attr:`IOProfile.strict_hydrogens`.
127 :raises: :exc:`~ost.io.IOException` if the import fails due to an erroneous or
130 def _override(val1, val2):
135 if isinstance(profile, str):
136 prof=profiles[profile].Copy()
139 if dialect
not in (
None,
'PDB',
'CHARMM',):
140 raise ValueError(
'dialect must be PDB or CHARMM')
141 prof.calpha_only=_override(prof.calpha_only, calpha_only)
142 prof.no_hetatms=_override(prof.no_hetatms, no_hetatms)
143 prof.dialect=_override(prof.dialect, dialect)
144 prof.quack_mode=_override(prof.quack_mode, quack_mode)
145 prof.strict_hydrogens=_override(prof.strict_hydrogens, strict_hydrogens)
146 prof.fault_tolerant=_override(prof.fault_tolerant, fault_tolerant)
147 prof.join_spread_atom_records=_override(prof.join_spread_atom_records,
148 join_spread_atom_records)
151 output_dir = tempfile.gettempdir()
152 if __GetModelFromPDB(filename, output_dir):
153 filename = os.path.join(output_dir,
'pdb%s.ent.gz' % filename)
155 raise IOError(
'Can not load PDB %s from www.pdb.org'%filename)
157 conop_inst=conop.Conopology.Instance()
158 builder=conop_inst.GetBuilder(
"DEFAULT")
159 if prof.dialect==
'PDB':
160 builder.dialect=conop.PDB_DIALECT
161 elif prof.dialect==
'CHARMM':
162 builder.dialect=conop.CHARMM_DIALECT
163 builder.strict_hydrogens=prof.strict_hydrogens
168 while reader.HasNext():
169 ent=mol.CreateEntity()
170 reader.Import(ent, restrict_chains)
171 conop_inst.ConnectAll(builder, ent, 0)
174 raise IOError(
"File doesn't contain any entities")
177 ent=mol.CreateEntity()
179 reader.Import(ent, restrict_chains)
180 conop_inst.ConnectAll(builder, ent, 0)
182 raise IOError(
"File doesn't contain any entities")
187 def SavePDB(models, filename, dialect=None, pqr=False, profile='DEFAULT'):
189 Save entity or list of entities to disk. If a list of entities is supplied the
190 PDB file will be saved as a multi PDB file. Each of the entities is wrapped
191 into a MODEL/ENDMDL pair.
193 :param models: The entity or list of entities (handles or views) to be saved
194 :param filename: The filename
195 :type filename: string
197 if not getattr(models,
'__len__',
None):
199 if isinstance(profile, str):
200 profile=profiles[profile].Copy()
203 profile.dialect=_override(profile.dialect, dialect)
207 writer.write_multi_model=
True
221 image_list=img.ImageList()
224 image_list.append(image)
227 LoadMapList=LoadImageList
230 lazy_load=
False, stride=1,
233 Load CHARMM trajectory file.
235 :param crd: EntityHandle or filename of the (PDB) file containing the
236 structure. The structure must have the same number of atoms as the
238 :param dcd_file: The filename of the DCD file. If not set, and crd is a
239 string, the filename is set to the <crd>.dcd
240 :param layz_load: Whether the trajectory should be loaded on demand. Instead
241 of loading the complete trajectory into memory, the trajectory frames are
242 loaded from disk when requested.
243 :param stride: The spacing of the frames to load. When set to 2, for example,
244 every second frame is loaded from the trajectory. By default, every frame
246 :param dialect: The dialect for the PDB file to use. See :func:`LoadPDB`. If
247 set, overrides the value of the profile
248 :param profile: The IO profile to use for loading the PDB file. See
253 dcd_file=
'%s.dcd' % os.path.splitext(crd)[0]
254 crd=
LoadPDB(crd, profile=profile, dialect=dialect)
258 raise ValueError(
"No DCD filename given")
259 return LoadCHARMMTraj_(crd, dcd_file, stride, lazy_load)