Example #1
0
    def test_svg_annotation(component: Component, tmpdir_factory):
        if not component.atoms_ids:
            return

        json_obj = None
        wd = tmpdir_factory.mktemp('svg_json_test')
        out_file = os.path.join(wd, f'{component.id}.json')
        component.compute_2d(depictions)
        component.export_2d_annotation(out_file)

        assert os.path.isfile(out_file)
        assert os.path.getsize(out_file) > 0

        with open(out_file, 'r') as fp:
            json_obj = json.load(fp)

        assert json_obj['ccd_id'] == component.id
        assert json_obj['resolution']['x'] >= 0
        assert json_obj['resolution']['y'] >= 0

        atom_names = [atom['name'] for atom in json_obj['atoms']]
        assert len(json_obj['atoms']) == component.mol_no_h.GetNumAtoms()
        assert len(json_obj['bonds']) >= component.mol_no_h.GetNumBonds()
        assert any(
            atom['label'] for atom in
            json_obj['atoms'])  # do we have any labels (not all atoms has one)
        assert all(atom['name']
                   for atom in json_obj['atoms'])  # do we have atom names?

        assert all(bond['bgn'] in atom_names and bond['end'] in atom_names
                   for bond in json_obj['bonds'])  # are all the atoms defined?
        assert all(bond['coords']
                   for bond in json_obj['bonds'])  # do we have coordinates?
        assert all(bond['style']
                   for bond in json_obj['bonds'])  # and its stylling?
Example #2
0
    def test_svg_annotation(component: Component, tmpdir_factory):
        if not component.atoms_ids:
            return

        json_obj = None
        wd = tmpdir_factory.mktemp("svg_json_test")
        out_file = os.path.join(wd, f"{component.id}.json")
        component.compute_2d(depictions)
        component.export_2d_annotation(out_file)

        assert os.path.isfile(out_file)
        assert os.path.getsize(out_file) > 0

        with open(out_file, "r") as fp:
            json_obj = json.load(fp)

        assert json_obj["ccd_id"] == component.id
        assert json_obj["resolution"]["x"] >= 0
        assert json_obj["resolution"]["y"] >= 0

        atom_names = [atom["name"] for atom in json_obj["atoms"]]
        assert len(json_obj["atoms"]) == component.mol_no_h.GetNumAtoms()
        assert len(json_obj["bonds"]) >= component.mol_no_h.GetNumBonds()
        assert all(atom["name"] for atom in json_obj["atoms"])  # do we have atom names?

        assert all(
            bond["bgn"] in atom_names and bond["end"] in atom_names
            for bond in json_obj["bonds"]
        )  # are all the atoms defined?
        assert all(
            bond["coords"] for bond in json_obj["bonds"]
        )  # do we have coordinates?
        assert all(bond["style"] for bond in json_obj["bonds"])  # and its stylling?
    def _generate_depictions(self, component: Component):
        """Generate nice 2D depictions for the component. Presently depictions
        are generated in the following resolutions (100,200,300,400,500) with
        and without atom names.

        Args:
            component (Component): Component to be depicted.
            depictions (DepictionManager): Helper class
                to carry out depiction process.
            parent_dir (str): Where the depiction should be stored
        """
        parent_dir = os.path.join(self.output_dir, component.id[0],
                                  component.id)

        depiction_result = component.compute_2d(self.depictions)

        if depiction_result.source == DepictionSource.Failed:
            self.logger.debug('failed to generate 2D image.')
        else:
            if depiction_result.score > 0.99:
                self.logger.debug(
                    'collision free image could not be generated.')
            self.logger.debug(
                f'2D generated using {depiction_result.source.name} with score {depiction_result.score}.'
            )

        wedge_bonds = depiction_result.template_name != 'cube'

        for i in range(100, 600, 100):
            component.export_2d_svg(os.path.join(parent_dir,
                                                 f'{component.id}_{i}.svg'),
                                    width=i,
                                    wedge_bonds=wedge_bonds)
            component.export_2d_svg(os.path.join(
                parent_dir, f'{component.id}_{i}_names.svg'),
                                    width=i,
                                    names=True,
                                    wedge_bonds=wedge_bonds)

        component.export_2d_annotation(os.path.join(
            parent_dir, f'{component.id}_annotation.json'),
                                       wedge_bonds=wedge_bonds)
    def _generate_depictions(self, component: Component, out_dir: str):
        """Generate nice 2D depictions for the component and
        depiction annotations in JSON format. Presently depictions
        are generated in the following resolutions (100,200,300,400,500)
        with and without atom names.

        Args:
            component (Component): Component to be depicted.
            out_dir (str): Where the depictions should be stored.
        """
        depiction_result = component.compute_2d(self.depictions)

        if depiction_result.source == DepictionSource.Failed:
            logging.debug("failed to generate 2D image.")
        else:
            if depiction_result.score > 0.99:
                logging.debug("collision free image could not be generated.")
            logging.debug(
                f"2D generated using {depiction_result.source.name} with score {depiction_result.score}."
            )

        wedge_bonds = depiction_result.template_name != "cube"

        for i in range(100, 600, 100):
            component.export_2d_svg(
                os.path.join(out_dir, f"{component.id}_{i}.svg"),
                width=i,
                wedge_bonds=wedge_bonds,
            )
            component.export_2d_svg(
                os.path.join(out_dir, f"{component.id}_{i}_names.svg"),
                width=i,
                names=True,
                wedge_bonds=wedge_bonds,
            )

        component.export_2d_annotation(
            os.path.join(out_dir, f"{component.id}_annotation.json"),
            wedge_bonds=wedge_bonds,
        )