Beispiel #1
0
    def setupReflectionSet(self):
        from CDTK.Crystal import UnitCell
        from CDTK.SpaceGroups import space_groups
        from CDTK.Reflections import ReflectionSet
        from CDTK.ReflectionData import ExperimentalAmplitudes, \
                                        ExperimentalIntensities
        from CDTK import Units

        if len(self.cell) == 0 or len(self.symmetry) == 0:
            self.parseStructureFile()
        if len(self.cell) == 0:
            raise MMCIFError("cell parameters missing")
        if len(self.symmetry) == 0:
            raise MMCIFError("symmetry parameters missing")

        cell = UnitCell(
            float(self.cell['length_a']) * Units.Ang,
            float(self.cell['length_b']) * Units.Ang,
            float(self.cell['length_c']) * Units.Ang,
            float(self.cell['angle_alpha']) * Units.deg,
            float(self.cell['angle_beta']) * Units.deg,
            float(self.cell['angle_gamma']) * Units.deg)
        try:
            sg_key = int(self.symmetry['Int_Tables_number'])
        except (KeyError, ValueError):
            sg_key = self.symmetry['space_group_name_H-M'].strip()
        space_group = space_groups[sg_key]
        self.reflections = ReflectionSet(cell, space_group)
Beispiel #2
0
    def test_hdf5(self):
        cell = UnitCell(3., 3., 4., 90. * Units.deg, 90. * Units.deg,
                        120. * Units.deg)
        res_max = 0.5
        res_min = 10.
        reflections = ReflectionSet(cell,
                                    space_groups['P 31'],
                                    res_max,
                                    res_min,
                                    compact=self.compact)
        frozen = reflections.freeze()

        with HDF5Store('test.h5', 'w') as store:
            store.store('reflections', reflections)
            retrieved = store.retrieve('reflections')
            store.store('frozen_reflections', frozen)

        self.assert_(retrieved is reflections)

        with HDF5Store('test.h5', 'r') as store:
            retrieved = store.retrieve('reflections')
            retrieved_again = store.retrieve('reflections')
            retrieved_frozen = store.retrieve('frozen_reflections')

        self.assert_(retrieved_again is retrieved)
        self.assert_(isinstance(retrieved, ReflectionSet))
        self.assert_(isinstance(retrieved_frozen, ReflectionSet))
        self.assert_(isinstance(retrieved, FrozenReflectionSet))
        self.assert_(isinstance(retrieved_frozen, FrozenReflectionSet))

        self._compare(reflections, retrieved, False)
        self._compare(reflections, retrieved_frozen, False)
Beispiel #3
0
    def test_pickle(self):
        cell = UnitCell(3., 3., 4., 90. * Units.deg, 90. * Units.deg,
                        120. * Units.deg)
        res_max = 0.5
        res_min = 10.
        reflections = ReflectionSet(cell,
                                    space_groups['P 31'],
                                    res_max,
                                    res_min,
                                    compact=self.compact)
        self.assert_(reflections.isComplete())

        string = cPickle.dumps(reflections)
        unpickled = cPickle.loads(string)
        self.assert_(unpickled.isComplete())
        self.assertEqual(len(reflections.reflection_map),
                         len(unpickled.reflection_map))
        self._compare(reflections, unpickled)

        frozen = reflections.freeze()
        self.assert_(frozen.isComplete())
        self._compare(reflections, frozen)

        string = cPickle.dumps(frozen)
        unpickled = cPickle.loads(string)
        self.assert_(unpickled.isComplete())
        self.assert_((frozen._reflections == unpickled._reflections).any())
        self.assert_((frozen._absences == unpickled._absences).any())
        self._compare(frozen, unpickled)
Beispiel #4
0
    def test_P1(self):
        cell = UnitCell(Vector(1., 0., 0.), Vector(0., 1., 0.),
                        Vector(0., 0., 1.))
        res_max = 0.5
        res_min = 10.
        reflections = ReflectionSet(cell,
                                    space_groups['P 1'],
                                    res_max,
                                    res_min,
                                    compact=self.compact)
        self.assert_(reflections.isComplete())
        nr = sum([r.n_symmetry_equivalents for r in reflections]) + \
             sum([r.n_symmetry_equivalents
                  for r in reflections.systematic_absences])
        self.assertEqual(reflections.totalReflectionCount(), nr)
        self.assert_(reflections.totalReflectionCount() == 2 *
                     len(reflections.minimal_reflection_list))
        for r in reflections:
            self.assert_(len(r.symmetryEquivalents()) == 2)
            self.assert_(res_max <= r.resolution() <= res_min)
            self.assert_(not r.isCentric())
            self.assert_(r.symmetryFactor() == 1)
        self._shellTest(reflections)
        self._subsetTest(reflections)
        self._symmetryTest(reflections)
        self._intersectionTest(reflections)
        self._equalityTest(reflections)

        frozen = reflections.freeze()
        self.assert_(frozen is reflections.freeze())
        self.assert_(frozen is frozen.freeze())
        self.assert_(frozen.isComplete())
        self.assert_(frozen.totalReflectionCount() == 2 *
                     len(frozen._reflections))
        for r in frozen:
            self.assert_(reflections.hasReflection(r.h, r.k, r.l))
            self.assert_(len(r.symmetryEquivalents()) == 2)
            self.assert_(res_max <= r.resolution() <= res_min)
            self.assert_(not r.isCentric())
            self.assert_(r.symmetryFactor() == 1)
        for r in reflections:
            self.assert_(frozen.hasReflection(r.h, r.k, r.l))
            self.assert_(r.index == frozen[(r.h, r.k, r.l)].index)
        self._shellTest(frozen)
        self._subsetTest(frozen)
        self._symmetryTest(frozen)
        self._intersectionTest(frozen)
        self._equalityTest(frozen)
Beispiel #5
0
 def test_P31(self):
     cell = UnitCell(3., 3., 4.,
                     90.*Units.deg, 90.*Units.deg, 120.*Units.deg)
     res_max = 0.5
     res_min = 10.
     reflections = ReflectionSet(cell, space_groups['P 31'],
                                 res_max, res_min)
     sf = StructureFactor(reflections)
     value = 1.1-0.8j
     for r in reflections:
         for re in r.symmetryEquivalents():
             sf[re] = value
             self.assert_(N.absolute(sf[re]-value) < 1.e-14)
             ri = reflections[(-re.h, -re.k, -re.l)]
             self.assert_(N.absolute(sf[ri]-N.conjugate(value)) < 1.e-13)
Beispiel #6
0
    def test_P31(self):
        cell = UnitCell(3., 3., 4., 90. * Units.deg, 90. * Units.deg,
                        120. * Units.deg)
        res_max = 0.5
        res_min = 10.
        sg = space_groups['P 31']
        reflections = ReflectionSet(cell,
                                    sg,
                                    res_max,
                                    res_min,
                                    compact=self.compact)
        self.assert_(reflections.isComplete())
        nr = sum([r.n_symmetry_equivalents for r in reflections]) + \
             sum([r.n_symmetry_equivalents
                  for r in reflections.systematic_absences])
        self.assertEqual(reflections.totalReflectionCount(), nr)
        for r in reflections:
            self.assert_(res_max <= r.resolution() <= res_min)
        for r in reflections.systematic_absences:
            self.assertEqual(r.h, 0)
            self.assertEqual(r.k, 0)
            self.assertNotEqual(r.l % 3, 0)
        self._shellTest(reflections)
        self._subsetTest(reflections)
        self._symmetryTest(reflections)
        self._intersectionTest(reflections)
        self._equalityTest(reflections)

        frozen = reflections.freeze()
        self.assert_(frozen is reflections.freeze())
        self.assert_(frozen is frozen.freeze())
        self.assert_(frozen.isComplete())
        for r in frozen:
            self.assert_(reflections.hasReflection(r.h, r.k, r.l))
            self.assert_(res_max <= r.resolution() <= res_min)
        for r in reflections:
            self.assert_(frozen.hasReflection(r.h, r.k, r.l))
            self.assert_(r.index == frozen[(r.h, r.k, r.l)].index)
        self._shellTest(frozen)
        self._subsetTest(frozen)
        self._symmetryTest(frozen)
        self._intersectionTest(frozen)
        self._equalityTest(frozen)
Beispiel #7
0
class MMCIFStructureFactorData(object):
    """
    Structure factor data from an mmCIF file

    Loads reflections with structure factor amplitudes or intensities
    from the mmCIF structure factor file in the PDB. Since many
    structure factor files in the PDB do not completely respect the
    mmCIF format and dictionary, this class is generous about common
    mistakes. Nevertheless, it fails for some PDB entries.

    After successful initialization, the following attributes of
    a MMCIFStructureFactorData object contain the data:

      - reflections: a CDTK.Reflections.ReflectionSet object

      - data: the experimental data stored in a
        CDTK.ReflectionData.ExperimentalAmplitudes or
        CDTK.ReflectionData.ExperimentalIntensities object.

      - model: the calculated structure factor stored in a
        CDTK.ReflectionData.StructureFactor object. If no calculated
        data is contained in the file, the value is None.
    """
    def __init__(self,
                 sf_file=None,
                 structure_file=None,
                 pdb_code=None,
                 fill=False,
                 load_model_sf=True,
                 require_sigma=True):
        """
        Specify the data to be loaded. The following combinations
        are valid:

         - pdb_code only: the data is taken from a public or local
           PDB repository

         - sf_file only: the data is taken from the structure
           factor file

         - sf_file and structure_file: cell and/or symmetry information
           is read from structure_file if it is missing in sf_file

        :param sf_file: the name of the structure factor mmCIF file
        :type sf_file: str
        :param structure_file: the name of the structure mmCIF file
        :type structure_file: str
        :param pdb_code: a four-letter PDB code
        :type pdb_code: str
        :param fill: True if the reflection set should contain all
               reflections in the resolution sphere. With the default value
               of False, only the reflections listed in the mmCIF file
               will be present in the reflection set.
        :type fill: bool
        :param load_model_sf: True if model structure factors (F_calc)
                              should be loaded if present in the file
        :type load_model_sf: bool
        :param require_sigma: if True, ignore experimental data points
                              without sigma. If False, set sigma to zero
                              if it is not given.
        :type require_sigma: bool
        """
        if pdb_code is not None:
            self.pdb_code = pdb_code
            assert sf_file is None
            assert structure_file is None
        elif sf_file is not None:
            self.pdb_code = None
            if isinstance(sf_file, str):
                sf_file = TextFile(sf_file)
            self.sf_file = sf_file
            if structure_file is not None:
                if isinstance(structure_file, str):
                    structure_file = TextFile(structure_file)
            self.structure_file = structure_file
        else:
            raise MMCIFError("No structure factor data given")
        self.load_model_sf = load_model_sf
        self.require_sigma = require_sigma
        self.cell = {}
        self.symmetry = {}
        self.reflections = None
        self.parseSFFile()
        self.finalize(fill)

    def parseSFFile(self):
        if self.pdb_code is not None:
            parser = MMCIFParser(pdb_sf_code=self.pdb_code)
        else:
            parser = MMCIFParser(file_object=self.sf_file)
        parser.parseToObjects(cell=self.cell,
                              symmetry=self.symmetry,
                              refln=self.addReflection)
        if self.reflections is None:
            raise MMCIFError("reflection data missing")

    def parseStructureFile(self):
        if self.pdb_code is not None:
            parser = MMCIFParser(pdb_code=self.pdb_code)
        else:
            if self.structure_file is None:
                return
            parser = MMCIFParser(file_object=self.structure_file)
        parser.parseToObjects(cell=self.cell, symmetry=self.symmetry)

    def setupReflectionSet(self):
        from CDTK.Crystal import UnitCell
        from CDTK.SpaceGroups import space_groups
        from CDTK.Reflections import ReflectionSet
        from CDTK.ReflectionData import ExperimentalAmplitudes, \
                                        ExperimentalIntensities
        from CDTK import Units

        if len(self.cell) == 0 or len(self.symmetry) == 0:
            self.parseStructureFile()
        if len(self.cell) == 0:
            raise MMCIFError("cell parameters missing")
        if len(self.symmetry) == 0:
            raise MMCIFError("symmetry parameters missing")

        cell = UnitCell(
            float(self.cell['length_a']) * Units.Ang,
            float(self.cell['length_b']) * Units.Ang,
            float(self.cell['length_c']) * Units.Ang,
            float(self.cell['angle_alpha']) * Units.deg,
            float(self.cell['angle_beta']) * Units.deg,
            float(self.cell['angle_gamma']) * Units.deg)
        try:
            sg_key = int(self.symmetry['Int_Tables_number'])
        except (KeyError, ValueError):
            sg_key = self.symmetry['space_group_name_H-M'].strip()
        space_group = space_groups[sg_key]
        self.reflections = ReflectionSet(cell, space_group)

    def addReflection(self, indices, data):
        from Scientific import N
        if self.reflections is None:
            # First call
            from CDTK.ReflectionData import ExperimentalAmplitudes, \
                                            ExperimentalIntensities, \
                                            StructureFactor
            self.setupReflectionSet()
            self.data = None
            for label1, label2 in [('F_meas', 'F_meas_sigma'),
                                   ('F_meas_au', 'F_meas_sigma_au')]:
                if label1 in indices and label2 in indices:
                    self.data_index = indices[label1]
                    self.sigma_index = indices[label2]
                    self.data = ExperimentalAmplitudes
            for label1, label2 in [('intensity_meas', 'intensity_sigma'),
                                   ('intensity_meas_au', 'intensity_sigma_au'),
                                   ('F_squared_meas', 'F_squared_sigma')]:
                if label1 in indices and label2 in indices:
                    self.data_index = indices[label1]
                    self.sigma_index = indices[label2]
                    self.data = ExperimentalIntensities
            if self.data is None:
                if self.require_sigma:
                    raise MMCIFError("no experimental data with sigma"
                                     " found in %s" % str(indices.keys()))
                else:
                    for label1 in ['F_meas', 'F_meas_au']:
                        if label1 in indices:
                            self.data_index = indices[label1]
                            self.sigma_index = None
                            self.data = ExperimentalAmplitudes
                    for label1 in [
                            'intensity_meas', 'intensity_meas_au',
                            'F_squared_meas'
                    ]:
                        if label1 in indices:
                            self.data_index = indices[label1]
                            self.sigma_index = None
                            self.data = ExperimentalIntensities
                    if self.data is None:
                        raise MMCIFError("no experimental data found in %s" %
                                         str(indices.keys()))
            self.model = None
            if self.load_model_sf and \
                   'F_calc' in indices and 'phase_calc' in indices:
                self.model = StructureFactor
            self.reflection_data = []

        h = int(data[indices['index_h']])
        k = int(data[indices['index_k']])
        l = int(data[indices['index_l']])
        if h * h + k * k + l * l > 0:
            ri = self.reflections.getReflection(h, k, l)
            # The status field is often used incorrectly.
            # Accept a 0/1 flag (presumably meant as "r_free flag")
            # in addition to what mmCIF defines.
            try:
                status = data[indices['status']]
            except KeyError:
                status = 'o'
            value = data[self.data_index]
            if self.sigma_index is None:
                sigma = '0.'
            else:
                sigma = data[self.sigma_index]
            observed = status in 'ofhl01' and value != '?'
            if self.require_sigma:
                observed = observed and sigma != '?'
            if observed:
                value = float(value)
                sigma = float(sigma)
            else:
                value = sigma = 0.
            if self.model is not None:
                model_amplitude = float(data[indices['F_calc']])
                model_phase = float(data[indices['phase_calc']])
            else:
                model_amplitude = model_phase = 0.
            self.reflection_data.append((h, k, l, value, sigma, observed,
                                         model_amplitude, model_phase))

    def finalize(self, fill):
        if fill:
            self.reflections.fillResolutionSphere()
        self.data = self.data(self.reflections)
        if self.model is not None:
            self.model = self.model(self.reflections)
        for h, k, l, value, sigma, observed, model_amplitude, model_phase \
            in self.reflection_data:
            ri = self.reflections[(h, k, l)]
            if observed:
                try:
                    self.data[ri] = N.array([value, sigma])
                except ValueError:
                    # Systematically absent reflections that is not marked
                    # as such in the status field. This is too common to
                    # raise an exception. Continue to the next reflection,
                    # as setting the model sf is likely to cause the same
                    # exception again.
                    continue
            if self.model is not None:
                self.model[ri] = model_amplitude * N.exp(
                    1j * model_phase * Units.deg)
Beispiel #8
0
    def test_P43212(self):
        cell = UnitCell(Vector(1., 0., 0.), Vector(0., 1., 0.),
                        Vector(0., 0., 1.5))
        res_max = 0.5
        res_min = 10.
        sg = space_groups['P 43 21 2']
        reflections = ReflectionSet(cell,
                                    sg,
                                    res_max,
                                    res_min,
                                    compact=self.compact)
        self.assert_(reflections.isComplete())
        nr = sum([r.n_symmetry_equivalents for r in reflections]) + \
             sum([r.n_symmetry_equivalents
                  for r in reflections.systematic_absences])
        self.assertEqual(reflections.totalReflectionCount(), nr)
        for r in reflections:
            self.assert_(res_max <= r.resolution() <= res_min)
            is_centric = r.isCentric()
            if r.h == 0 or r.k == 0 or r.l == 0:
                self.assert_(is_centric)
            elif r.h == r.k:
                self.assert_(is_centric)
            else:
                self.assert_(not is_centric)
            eps = r.symmetryFactor()
            if r.l == 0 and (r.h == r.k or r.h == -r.k):
                self.assert_(eps == 2)
            elif int(r.h == 0) + int(r.k == 0) + int(r.l == 0) == 2:
                if r.l != 0:
                    self.assert_(eps == 4)
                else:
                    self.assert_(eps == 2)
            else:
                self.assert_(eps == 1)
        for r in reflections.systematic_absences:
            self.assertEqual(int(r.h == 0) + int(r.k == 0) + int(r.l == 0), 2)
            if r.h != 0:
                self.assertEqual(r.h % 2, 1)
            if r.k != 0:
                self.assertEqual(r.k % 2, 1)
            if r.l != 0:
                self.assertNotEqual(r.l % 4, 0)
        self._shellTest(reflections)
        self._subsetTest(reflections)
        self._symmetryTest(reflections)
        self._intersectionTest(reflections)
        self._equalityTest(reflections)

        frozen = reflections.freeze()
        self.assert_(frozen is reflections.freeze())
        self.assert_(frozen is frozen.freeze())
        self.assert_(frozen.isComplete())
        for r in frozen:
            self.assert_(reflections.hasReflection(r.h, r.k, r.l))
            self.assert_(res_max <= r.resolution() <= res_min)
        for r in reflections:
            self.assert_(frozen.hasReflection(r.h, r.k, r.l))
            self.assert_(r.index == frozen[(r.h, r.k, r.l)].index)
        self._shellTest(frozen)
        self._subsetTest(frozen)
        self._symmetryTest(frozen)
        self._intersectionTest(frozen)
        self._equalityTest(frozen)