File: __init__.py

package info (click to toggle)
openstructure 2.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 206,240 kB
  • sloc: cpp: 188,571; python: 36,686; ansic: 34,298; fortran: 3,275; sh: 312; xml: 146; makefile: 29
file content (601 lines) | stat: -rw-r--r-- 25,335 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
#------------------------------------------------------------------------------
# This file is part of the OpenStructure project <www.openstructure.org>
#
# Copyright (C) 2008-2020 by the OpenStructure authors
#
# This library is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3.0 of the License, or (at your option)
# any later version.
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#------------------------------------------------------------------------------
import os, tempfile, ftplib, http.client

from ._ost_io import *
from ost import mol, geom, conop, seq

class IOProfiles:
  def __init__(self):
    self._dict={}

    if conop.GetDefaultLib():
      processor = conop.RuleBasedProcessor(conop.GetDefaultLib())
    else:
      processor = conop.HeuristicProcessor()
    self['STRICT'] = IOProfile(dialect='PDB', fault_tolerant=False,
                               processor=processor.Copy())
    self['SLOPPY'] = IOProfile(dialect='PDB', fault_tolerant=True,
                               processor=processor.Copy())
    self['CHARMM'] = IOProfile(dialect='CHARMM', fault_tolerant=True,
                               processor=processor.Copy())
    self['DEFAULT'] = 'STRICT'

  def __getitem__(self, key):
    return IOProfileRegistry.Instance().Get(key)

  def __setitem__(self, key, value):
    if isinstance(value, str):
      value=self[value].Copy()
    IOProfileRegistry.Instance().Set(key, value)
    self._dict[key]=value

  def Get(self, key):
    """ Getter which keeps compound library up to date

    Keeps compound library for default profiles up to date. Reason for that is
    that conop.SetDefaultLib() after importing io has no effect. Processors
    (and the associated compound library) are set at import. Custom profiles,
    i.e. profiles that are not defined in constructor of this class, are
    returned as is without any update.
    """
    if key not in ['STRICT', 'SLOPPY', 'CHARMM', 'DEFAULT']:
      return self[key].Copy()
    prof = self[key].Copy()
    if conop.GetDefaultLib():
      processor = conop.RuleBasedProcessor(conop.GetDefaultLib())
    else:
      processor = conop.HeuristicProcessor()
    prof.processor = processor
    return prof

  def __len__(self):
    return len(self._dict)

  def __iter__(self):
    return self._dict.__iter__()

profiles = IOProfiles()

def _override(val1, val2):
  if val2!=None:
    return val2
  else:
    return val1

def LoadPDB(filename, restrict_chains="", no_hetatms=None,
            fault_tolerant=None, load_multi=False,
            join_spread_atom_records=None, calpha_only=None,
            profile='DEFAULT', remote=False, remote_repo='pdb',
            dialect=None, seqres=False, bond_feasibility_check=None,
            read_conect=False):
  """
  Load PDB file from disk and return one or more entities. Several options 
  allow to customize the exact behaviour of the PDB import. For more information 
  on these options, see :doc:`profile`.
  
  Residues are flagged as ligand if they are mentioned in a HET record.

  :param filename: File to be loaded
  :type filename: :class:`str`

  :param restrict_chains: If not an empty string, only chains listed in the
                          string will be imported.
  :type restrict_chains: :class:`str`

  :param no_hetatms: If set to True, HETATM records will be ignored. Overrides 
                     the value of :attr:`IOProfile.no_hetatms`
  :type no_hetatms: :class:`bool`

  :param fault_tolerant: Enable/disable fault-tolerant import. If set, overrides 
                         the value of :attr:`IOProfile.fault_tolerant`.
  :type fault_tolerant: :class:`bool`

  :param load_multi: If set to True, a list of entities will be returned instead
                     of only the first. This is useful when dealing with
                     multi-PDB files.
  :type load_multi: :class:`bool`

  :param join_spread_atom_records: If set, overrides the value of 
                                   :attr:`IOProfile.join_spread_atom_records`.
  :type join_spread_atom_records: :class:`bool`

  :param calpha_only: When set to true, forces the importer to only load atoms
                      named CA. If set, overrides the value of
                      :attr:`IOProfile.calpha_only`.
  :type calpha_only: :class:`bool`

  :param profile: Aggregation of flags and algorithms to control import and
                  processing of molecular structures. Can either be a
                  :class:`str` specifying one of the default profiles
                  ['DEFAULT', 'SLOPPY', 'CHARMM', 'STRICT'] or an actual object
                  of type :class:`ost.io.IOProfile`. If a :class:`str` defines
                  a default profile, :attr:`IOProfile.processor` is set to
                  :class:`ost.conop.RuleBasedProcessor` with the currently
                  set :class:`ost.conop.CompoundLib` available as
                  :func:`ost.conop.GetDefaultLib()`. If no
                  :class:`ost.conop.CompoundLib` is available,
                  :class:`ost.conop.HeuristicProcessor` is used instead. See
                  :doc:`profile` for more info.
  :type profile: :class:`str`/:class:`ost.io.IOProfile`
  
  :param remote: If set to True, the method tries to load the pdb from the remote
                 repository given as *remote_repo*. The filename is then
                 interpreted as the entry id as further specified for the
                 *remote_repo* parameter.
  :type remote: :class:`bool`

  :param remote_repo: Remote repository to fetch structure if *remote* is True. 
                      Must be one in ['pdb', 'smtl', 'pdb_redo']. In case of 
                      'pdb' and 'pdb_redo', the entry must be given as lower 
                      case pdb id, which loads the deposited assymetric unit 
                      (e.g. '1ake'). In case of 'smtl', the entry must also 
                      specify the desired biounit (e.g. '1ake.1').
  :type remote_repo: :class:`str`
     
  :param dialect: Specifies the particular dialect to use. If set, overrides 
                  the value of :attr:`IOProfile.dialect`
  :type dialect: :class:`str`

  :param seqres: Whether to read SEQRES records. If set to True, the loaded 
                 entity and seqres entry will be returned as a tuple.
                 If file doesnt contain SEQRES records, the returned
                 :class:`ost.seq.SequenceList` will be invalid.
  :type seqres: :class:`bool`

  :param bond_feasibility_check: Flag for :attr:`IOProfile.processor`. If
                                 turned on, atoms are only connected by
                                 bonds if they are within a reasonable distance
                                 (as defined by
                                 :func:`ost.conop.IsBondFeasible`).
                                 If set, overrides the value of
                                 :attr:`ost.conop.Processor.check_bond_feasibility`
  :type bond_feasibility_check: :class:`bool`
  :param read_conect: By default, OpenStructure doesn't read CONECT statements in
                      a pdb file. Reason is that they're often missing and we prefer
                      to rely on the chemical component dictionary from the PDB.
                      However, there may be cases where you really want these CONECT
                      statements. For example novel compounds with no entry in
                      the chemical component dictionary. Setting this to True has
                      two effects: 1) CONECT statements are read and blindly applied
                      2) The processor does not connect any pair of HETATOM atoms in
                      order to not interfer with the CONECT statements.
  :type read_conect: :class:`bool`

  :rtype: :class:`~ost.mol.EntityHandle` or a list thereof if `load_multi` is 
      True.  

  :raises: :exc:`~ost.io.IOException` if the import fails due to an erroneous or 
      inexistent file
  """
  def _override(val1, val2):
    if val2!=None:
      return val2
    else:
      return val1
  if isinstance(profile, str):
    prof=profiles.Get(profile)
  elif isinstance(profile, IOProfile):
    prof=profile.Copy()
  else:
    raise TypeError('profile must be of type string or IOProfile, '+\
                    'instead of %s'%type(profile))
  if dialect not in (None, 'PDB', 'CHARMM',):
    raise ValueError('dialect must be PDB or CHARMM')
  prof.calpha_only=_override(prof.calpha_only, calpha_only)
  prof.no_hetatms=_override(prof.no_hetatms, no_hetatms)
  prof.dialect=_override(prof.dialect, dialect)
  prof.read_conect=_override(prof.read_conect, read_conect)
  if prof.processor:
    prof.processor.check_bond_feasibility=_override(prof.processor.check_bond_feasibility, 
                                                    bond_feasibility_check)
    prof.processor.connect_hetatm=_override(prof.processor.connect_hetatm,
                                            not read_conect)
  prof.fault_tolerant=_override(prof.fault_tolerant, fault_tolerant)
  prof.join_spread_atom_records=_override(prof.join_spread_atom_records,
                                          join_spread_atom_records)

  tmp_file = None # avoid getting out of scope
  if remote:
    if remote_repo not in ['pdb', 'smtl', 'pdb_redo']:
      raise IOError("remote_repo must be in ['pdb', 'smtl', 'pdb_redo']")
    from ost.io.remote import RemoteGet
    tmp_file =RemoteGet(filename, from_repo=remote_repo)
    filename = tmp_file.name
  
  conop_inst=conop.Conopology.Instance()
  if prof.processor:
    if prof.dialect=='PDB':
      prof.processor.dialect=conop.PDB_DIALECT
    elif prof.dialect=='CHARMM':
      prof.processor.dialect=conop.CHARMM_DIALECT
  reader=PDBReader(filename, prof)
  reader.read_seqres=seqres
  try:
    if load_multi:
      ent_list=[]
      while reader.HasNext():
        ent=mol.CreateEntity()
        reader.Import(ent, restrict_chains)
        if prof.processor:
          prof.processor.Process(ent)
        ent_list.append(ent)
      if len(ent_list)==0:
        raise IOError("File '%s' doesn't contain any entities" % filename)
      return ent_list
    else:
      ent=mol.CreateEntity()
      if reader.HasNext():
        reader.Import(ent, restrict_chains)
        if prof.processor:
          prof.processor.Process(ent)
      else:
        raise IOError("File '%s' doesn't contain any entities" % filename)
      if seqres:
        return ent, reader.seqres
      return ent
  except:
    raise

def SavePDB(models, filename, dialect=None,  pqr=False, profile='DEFAULT'):
  """
  Save entity or list of entities to disk. If a list of entities is supplied
  the PDB file will be saved as a multi PDB file. Each of the entities is
  wrapped into a MODEL/ENDMDL pair.

  If the atom number exceeds 99999, '*****' is used.

  :param models: The entity or list of entities (handles or views) to be saved
  :param filename: The filename
  :type  filename: string
  :raises: IOException if the restrictions of the PDB format are not satisfied
           (with the exception of atom numbers, see above):

             * Chain names with more than one character
             * Atom positions with coordinates outside range [-999.99, 9999.99]
             * Residue names longer than three characters
             * Atom names longer than four characters
             * Numeric part of :class:`ost.mol.ResNum` outside range [-999, 9999] 
             * Alternative atom indicators longer than one character
  """
  if not getattr(models, '__len__', None):
    models=[models]
  if isinstance(profile, str):
    profile=profiles.Get(profile)
  elif isinstance(profile, IOProfile):
    profile = profile.Copy()
  else:
    raise TypeError('profile must be of type string or IOProfile, '+\
                    'instead of %s'%type(profile))
  profile.dialect=_override(profile.dialect, dialect)
  writer=PDBWriter(filename, profile)
  writer.SetIsPQR(pqr)
  if len(models)>1:
    writer.write_multi_model=True
  for model in models:
    writer.Write(model)

try:
  from ost import img
  LoadMap = LoadImage
  SaveMap = SaveImage
except ImportError:
  pass

 ## loads several images and puts them in an ImageList
 # \sa \example fft_li.py "View Fourier Transform Example"
def LoadImageList (files):
  image_list=img.ImageList()
  for file in files:
    image=LoadImage(file)
    image_list.append(image)
  return image_list

LoadMapList=LoadImageList

def LoadCHARMMTraj(crd, dcd_file=None, profile='CHARMM',
                   lazy_load=False, stride=1, 
                   dialect=None, detect_swap=True,swap_bytes=False):
  """
  Load CHARMM trajectory file.
  
  :param crd: EntityHandle or filename of the (PDB) file containing the
      structure. The structure must have the same number of atoms as the 
      trajectory
  :param dcd_file: The filename of the DCD file. If not set, and crd is a 
      string, the filename is set to the <crd>.dcd
  :param layz_load: Whether the trajectory should be loaded on demand. Instead 
      of loading the complete trajectory into memory, the trajectory frames are 
      loaded from disk when requested.
  :param stride: The spacing of the frames to load. When set to 2, for example, 
      every second frame is loaded from the trajectory. By default, every frame 
      is loaded.
  :param dialect: The dialect for the PDB file to use. See :func:`LoadPDB`. If 
      set, overrides the value of the profile
  :param profile: The IO profile to use for loading the PDB file. See 
      :doc:`profile`.
  :param detect_swap: if True (the default), then automatic detection of endianess
      is attempted, otherwise the swap_bytes parameter is used
  :param swap_bytes: is detect_swap is False, this flag determines whether bytes
      are swapped upon loading or not
  """
  if not isinstance(crd, mol.EntityHandle):
    if dcd_file==None:
      dcd_file='%s.dcd' % os.path.splitext(crd)[0]    
    crd=LoadPDB(crd, profile=profile, dialect=dialect)

  else:
    if not dcd_file:
      raise ValueError("No DCD filename given")
  return LoadCHARMMTraj_(crd, dcd_file, stride, lazy_load, detect_swap, swap_bytes)

def LoadMMCIF(filename, fault_tolerant=None, calpha_only=None,
              profile='DEFAULT', remote=False, seqres=False, info=False):
  """
  Load an mmCIF file and return the first model as an entity.

  Several options allow to customize the exact behaviour of the mmCIF import.
  For more information on these options, see :doc:`profile`.
  
  Residues are flagged as ligand if they are not waters nor covered by an
  ``entity_poly`` record (ie. they are non-polymer entities in
  ``pdbx_entity_nonpoly``). Note that all residues except waters will be
  flagged as ligands if ``seqres=False`` (the default).

  :param filename: File to be loaded
  :type filename: :class:`str`

  :param fault_tolerant: Enable/disable fault-tolerant import. If set, overrides
                         the value of :attr:`IOProfile.fault_tolerant`.
  :type fault_tolerant: :class:`bool`

  :param calpha_only: When set to true, forces the importer to only load atoms
                      named CA. If set, overrides the value of
                      :attr:`IOProfile.calpha_only`.
  :type calpha_only: :class:`bool`

  :param profile: Aggregation of flags and algorithms to control import and
                  processing of molecular structures. Can either be a
                  :class:`str` specifying one of the default profiles
                  ['DEFAULT', 'SLOPPY', 'CHARMM', 'STRICT'] or an actual object
                  of type :class:`ost.io.IOProfile`. If a :class:`str` defines
                  a default profile, :attr:`IOProfile.processor` is set to
                  :class:`ost.conop.RuleBasedProcessor` with the currently
                  set :class:`ost.conop.CompoundLib` available as
                  :func:`ost.conop.GetDefaultLib()`. If no
                  :class:`ost.conop.CompoundLib` is available,
                  :class:`ost.conop.HeuristicProcessor` is used instead. See
                  :doc:`profile` for more info.
  :type profile: :class:`str`/:class:`ost.io.IOProfile`

  :param remote: If set to True, the method tries to load the pdb from the 
                 remote pdb repository www.pdb.org. The filename is then
                 interpreted as the pdb id.
  :type remote: :class:`bool`

  :param seqres: Whether to return SEQRES records. If True, a
                 :class:`~ost.seq.SequenceList` object is returned as the second
                 item. The sequences in the list are named according to the
                 mmCIF chain name.
                 This feature requires a default
                 :class:`compound library <ost.conop.CompoundLib>`
                 to be defined and accessible via
                 :func:`~ost.conop.GetDefaultLib`. One letter codes of non
                 standard compounds are set to X otherwise.
  :type seqres: :class:`bool`

  :param info: Whether to return an info container with the other output.
               If True, a :class:`MMCifInfo` object is returned as last item.
  :type info: :class:`bool`

  :rtype: :class:`~ost.mol.EntityHandle` (or tuple if *seqres* or *info* are
          True).

  :raises: :exc:`~ost.io.IOException` if the import fails due to an erroneous
           or non-existent file.
  """
  def _override(val1, val2):
    if val2!=None:
      return val2
    else:
      return val1
  if isinstance(profile, str):
    prof = profiles.Get(profile)
  else:
    prof = profile.Copy()

  prof.calpha_only=_override(prof.calpha_only, calpha_only)
  prof.fault_tolerant=_override(prof.fault_tolerant, fault_tolerant)

  if remote:
    from ost.io.remote import RemoteGet
    tmp_file = RemoteGet(filename, from_repo='cif')
    filename = tmp_file.name
  
  try:
    ent = mol.CreateEntity()
    reader = MMCifReader(filename, ent, prof)

    # NOTE: to speed up things, we could introduce a restrict_chains parameter
    #       similar to the one in LoadPDB. Here, it would have to be a list/set
    #       of chain-name-strings.

    #if reader.HasNext():
    reader.Parse() # branch links are connected in here
    if prof.processor:
      prof.processor.Process(ent)
    #else:
    #  raise IOError("File doesn't contain any entities")
    if seqres and info:
      return ent, reader.seqres, reader.info
    if seqres:
      return ent, reader.seqres
    if info:
      return ent, reader.info
    return ent
  except:
    raise


def SaveMMCIF(ent, filename, compound_lib = conop.GetDefaultLib(),
              data_name="OST_structure", mmcif_conform = True,
              entity_info = MMCifWriterEntityList()):
  """
  Save OpenStructure entity in mmCIF format

  :param ent:           OpenStructure Entity to be saved
  :param filename:      Filename - .gz suffix triggers gzip compression
  :param compound_lib:  Compound library required when writing, uses
                        :func:`ost.conop.GetDefaultLib` if not given
  :param data_name:     Name of data block that will be written to
                        mmCIF file. Typically, thats the PDB ID or some
                        identifier.
  :param mmcif_conform: Controls processing of structure, i.e. identification
                        of mmCIF entities etc. before writing. Detailed
                        description in :ref:`MMCif writing`. In short:
                        If *mmcif_conform* is set to True, Chains in *ent* are
                        expected to be valid mmCIF entities with residue numbers
                        set according to underlying SEQRES. That should be the
                        case when *ent* has been loaded with :func:`LoadMMCIF`.
                        If *mmcif_conform* is set to False, heuristics kick in
                        to identify and separate mmCIF entities based on
                        :class:`ost.mol.ChemClass` of the residues in a chain.
  :type ent: :class:`ost.mol.EntityHandle`/:class:`ost.mol.EntityView`/:class:`ost.io.OMF`
  :param entity_info: Advanced usage - description in :ref:`MMCif writing`
  :type filename: :class:`str`
  :type compound_lib: :class:`ost.conop.CompoundLib`
  :type data_name: :class:`str`
  :type mmcif_conform: :class:`bool`
  :type entity_info: :class:`MMCifWriterEntityList`
  """
  if compound_lib is None:
    raise RuntimeError("Require valid compound library to write mmCIF format")
  writer = MMCifWriter()
  writer.SetStructure(ent, compound_lib, mmcif_conform = mmcif_conform,
                      entity_info = entity_info)
  writer.Write(data_name, filename)



# this function uses a dirty trick: should be a member of MMCifInfoBioUnit
# which is totally C++, but we want the method in Python... so we define it
# here (__init__) and add it as a member to the class. With this, the first
# arguement is the usual 'self'.
# documentation for this function was moved to mmcif.rst,
# MMCifInfoBioUnit.PDBize, since this function is not included in SPHINX.
def _PDBize(biounit, asu, seqres=None, min_polymer_size=None,
            transformation=False, peptide_min_size=10, nucleicacid_min_size=10,
            saccharide_min_size=10):
  if min_polymer_size is not None:
    pdbizer = mol.alg.PDBize(min_polymer_size=min_polymer_size)
  else:
    pdbizer = mol.alg.PDBize(peptide_min_size=peptide_min_size,
                             nucleicacid_min_size=nucleicacid_min_size,
                             saccharide_min_size=saccharide_min_size)

  chains = biounit.GetChainList()
  c_intvls = biounit.GetChainIntervalList()
  o_intvls = biounit.GetOperationsIntervalList()
  ss = seqres
  if not ss:
    ss = seq.CreateSequenceList()
  # create list of operations
  # for cartesian products, operations are stored in a list, multiplied with
  # the next list of operations and re-stored... until all lists of operations
  # are multiplied in an all-against-all manner.
  operations = biounit.GetOperations()
  for i in range(0,len(c_intvls)):
    trans_matrices = geom.Mat4List()
    l_operations = operations[o_intvls[i][0]:o_intvls[i][1]]
    if len(l_operations) > 0:
      for op in l_operations[0]:
        rot = geom.Mat4()
        rot.PasteRotation(op.rotation)
        trans = geom.Mat4()
        trans.PasteTranslation(op.translation)
        tr = geom.Mat4()
        tr = trans * rot
        trans_matrices.append(tr)
      for op_n in range(1, len(l_operations)):
        tmp_ops = geom.Mat4List()
        for o in l_operations[op_n]:
          rot = geom.Mat4()
          rot.PasteRotation(o.rotation)
          trans = geom.Mat4()
          trans.PasteTranslation(o.translation)
          tr = geom.Mat4()
          tr = trans * rot
          for t_o in trans_matrices:
            tp = t_o * tr
            tmp_ops.append(tp)
        trans_matrices = tmp_ops
    # select chains into a view as basis for each transformation
    assu = asu.Select('cname='+','.join(mol.QueryQuoteName(name) \
                                        for name in \
                                        chains[c_intvls[i][0]:c_intvls[i][1]]))
    pdbizer.Add(assu, trans_matrices, ss)
  pdb_bu = pdbizer.Finish(transformation)
  if transformation:
    return pdb_bu, pdb_bu.GetTransform().GetMatrix()
  return pdb_bu

MMCifInfoBioUnit.PDBize = _PDBize


def LoadSDF(filename, fault_tolerant=None, profile='DEFAULT'):
  """
  Load SDF file from disk and return an entity.

  :param filename: File to be loaded
  :type filename: :class:`str`

  :param fault_tolerant: Enable/disable fault-tolerant import. If set, overrides
                         the value of :attr:`IOProfile.fault_tolerant`.
  :type fault_tolerant: :class:`bool`

  :param profile: Aggregation of flags and algorithms to control import and
                  processing of molecular structures. Can either be a
                  :class:`str` specifying one of the default profiles
                  ['DEFAULT', 'SLOPPY', 'CHARMM', 'STRICT'] or an actual object
                  of type :class:`ost.io.IOProfile`.
                  See :doc:`profile` for more info.
  :type profile: :class:`str`/:class:`ost.io.IOProfile`

  :raises: :exc:`~ost.io.IOException` if the import fails due to an erroneous or
      inexistent file
  """
  def _override(val1, val2):
    if val2 != None:
      return val2
    else:
      return val1

  if isinstance(profile, str):
    prof = profiles.Get(profile)
  elif isinstance(profile, IOProfile):
    prof = profile.Copy()
  else:
    raise TypeError('profile must be of type string or IOProfile, ' + \
                    'instead of %s' % type(profile))
  prof.fault_tolerant = _override(prof.fault_tolerant, fault_tolerant)

  reader = SDFReader(filename, prof)
  ent = mol.CreateEntity()
  reader.Import(ent)
  return ent