Ejemplo n.º 1
0
    def _sort_by_wiberg(self):
        """
        Sorts the molecules by total Wiberg bond order around the trivalent
        nitrogen.
        """
        logging.info("Sorting molecules by Wiberg bond order")

        self._mols.sort(key=lambda m: danceprops.get_dance_property(
            m, self._properties).tri_n_bond_order)
Ejemplo n.º 2
0
    def _write_bonds_to_csv(self):
        """Writes data about bonds around trivalent nitrogens to a CSV"""
        logging.info(f"Writing bond data to {self._output_tri_n_bonds}")
        with open(self._output_tri_n_bonds, 'w') as bondcsv:
            bondcsv.write("bond_order,bond_length,element\n")

            for mol in self._mols:
                prop = danceprops.get_dance_property(mol, self._properties)
                for bond in prop.tri_n_bonds:
                    bondcsv.write(f"{bond.bond_order},"
                                  f"{bond.bond_length},"
                                  f"{bond.element}\n")
Ejemplo n.º 3
0
    def _write_data_to_csv(self):
        """Writes the data about trivalent nitrogens to a CSV"""
        logging.info(f"Writing tri-n data to {self._output_tri_n_data}")

        with open(self._output_tri_n_data, 'w') as datacsv:
            datacsv.write(
                "tri_n_bond_order,tri_n_bond_angle,tri_n_bond_length\n")
            for mol in self._mols:
                prop = danceprops.get_dance_property(mol, self._properties)
                datacsv.write(f"{prop.tri_n_bond_order},"
                              f"{prop.tri_n_bond_angle},"
                              f"{prop.tri_n_bond_length}\n")
Ejemplo n.º 4
0
    def test_append_properties_list_appends_small_list(
            self, small_mols_and_props_list):
        old_mols, old_props = small_mols_and_props_list
        mols, props = old_mols.copy(), old_props.copy()

        mols2 = [oechem.OEMol() for i in range(2)]
        oechem.OESmilesToMol(mols2[0], "CN=C=O")
        oechem.OESmilesToMol(mols2[1], "[C-]#[O+]")
        danceprops.set_dance_property(mols2[0], 1)
        danceprops.set_dance_property(mols2[1], 0)
        old_mols2 = [oechem.OEMol(m) for m in mols2]
        props2 = [danceprops.DanceProperties() for i in range(2)]

        danceprops.append_properties_list(mols, props, mols2, props2)

        # The properties obtained from the molecules put in the new list should
        # be the same as the properties in the old list.
        assert danceprops.get_dance_property(
            mols[4],
            props) == danceprops.get_dance_property(old_mols2[0], props2)
        assert danceprops.get_dance_property(
            mols[5],
            props) == danceprops.get_dance_property(old_mols2[1], props2)
Ejemplo n.º 5
0
 def assert_props_equal(self, mols: [oechem.OEMol], mol_indices: [int],
                        props: [danceprops.DanceProperties],
                        correct_props: [danceprops.DanceProperties],
                        correct_prop_indices: [int]):
     """
     Assert that, for each molecule index given, the property returned with
     danceprops.get_dance_property is equal to the property at the given
     index in correct_props.
     """
     if len(mol_indices) != len(correct_prop_indices):
         raise RuntimeError("There must be an equal number of molecule and "
                            "correct property indices")
     for i in range(len(mol_indices)):
         assert danceprops.get_dance_property(
             mols[mol_indices[i]],
             props) == correct_props[correct_prop_indices[i]]
Ejemplo n.º 6
0
def write_mol_to_fingerprint_file(
        mol: oechem.OEMol,
        properties: [danceprops.DanceProperties],
        select_output_dir: str,
        select_bin_size: float,
        wiberg_precision: float,
):
    """Writes a molecule to its appropriate SMILES fingerprint file"""

    #  Some of the molecules coming in may be invalid. DanceGenerator may find
    #  there was an error in charge calculations, in which case the charged
    #  copy was not assigned to the molecule. This function checks for that.
    is_valid_molecule = \
            lambda mol: mol.HasData(danceprops.DANCE_CHARGED_COPY_KEY)

    if not is_valid_molecule(mol):
        logging.debug(f"Ignored molecule {mol.GetTitle()}")
        return

    charged_copy = mol.GetData(danceprops.DANCE_CHARGED_COPY_KEY)
    for atom in charged_copy.GetAtoms(oechem.OEIsInvertibleNitrogen()):
        tri_n = atom
        break
    fingerprint = danceprops.DanceFingerprint(tri_n, wiberg_precision)

    # Retrieve the total bond order around the trivalent nitrogen
    bond_order = danceprops.get_dance_property(mol, properties).tri_n_bond_order

    # Round the total bond order down to the lowest multiple of bin_size. For
    # instance, if bin_size is 0.02, and the bond_order is 2.028, it becomes
    # 2.02. This works because (bond_order / self._bin_size) generates a
    # multiple of the bin_size. Then floor() finds the next integer less than
    # the multiple. Finally, multiplying back by bin_size obtains the nearest
    # actual value.
    bond_order = math.floor(bond_order / select_bin_size) * select_bin_size

    filename = f"{select_output_dir}/{bond_order},{fingerprint}.smi"
    with open(filename, "a") as f:
        f.write(f"{oechem.OEMolToSmiles(mol)} {mol.GetTitle()}\n")
    logging.debug(f"Wrote {mol.GetTitle()} to {filename}")