Beispiel #1
0
def _CDPLgenerateConformation(cdpl_mol):
    '''
    PRIVAT METHOD
    configures a CDPL Molecule for conformation generation. \n
    Input: \n
    mol (CDPL BasicMolecule): a CDPL BasicMolecule \n
    Return: \n
    (CDPL BasicMolecule): the corresponding random conf. for the input BasicMolecule
     '''

    _CDPLconfigForConformation(
        cdpl_mol
    )  #TODO What exactly should be in the config for the cmp generation?
    cg = ConfGen.RandomConformerGenerator()
    coords = Math.Vector3DArray()
    i = 0

    cg.strictMMFF94AtomTyping = False

    ConfGen.prepareForConformerGeneration(cdpl_mol)

    coords.resize(cdpl_mol.numAtoms, Math.Vector3D())

    cg.setup(cdpl_mol)

    if cg.generate(coords) != ConfGen.RandomConformerGenerator.SUCCESS:
        log.error('! Conformer generation failed !')
        return

    Chem.set3DCoordinates(cdpl_mol, coords)

    return cdpl_mol
Beispiel #2
0
def calcMSF(positions, mean_pos):
    msf = 0.0
    tmp = Math.Vector3D()

    for pos in positions:
        tmp.assign(pos)
        tmp -= mean_pos
        msf += Math.innerProd(tmp, tmp)

    msf = msf / len(positions)
    return msf
Beispiel #3
0
def calcMaxFluct(positions, mean_pos):
    max_fluct = 0.0
    tmp = Math.Vector3D()

    for pos in positions:
        tmp.assign(pos)
        tmp -= mean_pos
        dev = Math.norm2(tmp)

        if dev > max_fluct:
            max_fluct = dev

    return max_fluct
Beispiel #4
0
def calcMeanPos(positions):
    mean_pos = Math.Vector3D()

    for pos in positions:
        mean_pos += pos

    mean_pos /= len(positions)
    return mean_pos
Beispiel #5
0
    def __init__(self, structure: Chem.BasicMolecule = None):
        self.shape: Shape.GaussianShape = Shape.GaussianShape()
        self.shapeFunc: Shape.GaussianShapeFunction = Shape.GaussianShapeFunction()
        self.coordinates: Math.Vector3DArray = Math.Vector3DArray()
        self.ligands: List[Chem.BasicMolecule] = []

        super(Protein, self).__init__()
        if structure:
            self.assign(structure)
def translate_mol_to_coords(mol, coords):
    mol_coords = Math.Vector3DArray()
    Chem.get3DCoordinates(mol, mol_coords)
    for i, row in enumerate(mol_coords):
        for j, column in enumerate(row):
            row[j] = column - coords[j]  # shift to desired coordinates

    Chem.set3DCoordinates(mol, mol_coords)
    return mol
    def makeRandomRotation(self, inplace: bool = True) -> Math.Vector3DArray:
        # TODO: maybe add boundaries for randomness
        rotMatrix = Math.Matrix3D()
        rotMatrix.assign(Rotation.random().as_matrix())
        rotatedCoords = rotate3DObject(self.getCoordinates(), rotMatrix)

        if inplace:
            Chem.set3DCoordinates(self, rotatedCoords)

        return rotatedCoords
Beispiel #8
0
def calcAtomSetCentroid(atoms, conf_idx):
    if len(atoms) == 1:
        return Chem.getConformer3DCoordinates(atoms[0], conf_idx)

    ctr = Math.Vector3D()

    for atom in atoms:
        ctr += Chem.getConformer3DCoordinates(atom, conf_idx)

    ctr /= len(atoms)
    return ctr
    def __init__(self, structure: Chem.BasicMolecule = None):
        self.shape: Shape.GaussianShape = Shape.GaussianShape()
        self.shapeFunc: Shape.GaussianShapeFunction = Shape.GaussianShapeFunction(
        )
        self.coordinates: Math.Vector3DArray = Math.Vector3DArray()
        self.ligands: List[Chem.BasicMolecule] = []
        self.surfaceAtoms: Chem.BasicMolecule = Chem.BasicMolecule()

        super(Protein, self).__init__()
        if structure:
            from MoleculeTools import sanitize_mol
            self.assign(structure)
def calcLonePairPosition(atom, mol, coords):
    lp_pos = Math.Vector3D()
    atom_pos = coords[mol.getAtomIndex(atom)]

    for nbr_atom in atom.atoms:
        lp_pos -= coords[mol.getAtomIndex(nbr_atom)]
        lp_pos -= atom_pos

    lp_pos /= atom.getNumAtoms()
    lp_pos += atom_pos

    return lp_pos
Beispiel #11
0
def process():
    if len(sys.argv) < 4:
        print >> sys.stderr, 'Usage:', sys.argv[
            0], '[input.cdf] [output directory] [frame index]'
        sys.exit(2)

    print >> sys.stderr, '> Processing grid CDF-file:', sys.argv[1], '...'

    index_frame = int(sys.argv[3])
    grid_set = Grid.DRegularGridSet()
    cdf_grid_reader = Grid.FileCDFDRegularGridSetReader(sys.argv[1])

    print >> sys.stderr, '> Writing grid data for frame', index_frame, '...\n'

    grid_set.clear()
    if not cdf_grid_reader.read(index_frame, grid_set):
        print '> No grid in file.'
        sys.exit(2)

    coord = Math.Vector3D()

    for grid in grid_set:
        int_descr = Grid.getName(grid)
        out_fname = path.splitext(
            path.basename(sys.argv[1])
        )[0] + '_' + int_descr + '_frame_no_' + sys.argv[3] + '.kont'
        out_path = path.join(sys.argv[2], out_fname)
        kont_file = open(out_path, 'w')

        index_mol = 0
        grid_values = []
        print '>', int_descr, 'grid'

        for k in range(grid.getSize3()):
            for i in range(grid.getSize1()):
                for j in range(grid.getSize2()):
                    index_mol += 1
                    grid_values.append(grid(i, j, k))
                    grid.getCoordinates(i, j, k, coord)
                    kont_file.write(
                        "{:>7}   {:>7.3f} {:>7.3f} {:>7.3f}\n".format(
                            index_mol, coord[0], coord[1], coord[2]))

        kont_file.write("{:>8}\n".format(int_descr))

        for txt in grid_values:
            kont_file.write("{:>8.3f}\n".format(txt))

        kont_file.close()
def center_mol(mol):
    coords = Math.Vector3DArray()
    Chem.get3DCoordinates(mol, coords)
    np_coords = np.array(coords)
    centroid = get_centroid(np_coords)
    centered = np_coords - centroid

    # set coordinates coordinate object
    for i, row in enumerate(coords):
        for j, column in enumerate(row):
            row[j] = centered[i, j]

    # set coordinates to molecule
    Chem.set3DCoordinates(mol, coords)
    return mol
def cdfMol_pdb(pdb, output, name):
    initial_time = time.time()
    cdf_mol = Chem.BasicMolecule()
    pdb_mol = Chem.BasicMolecule()

    pdb_str = open(pdb, 'r').read().replace('WAT', 'HOH').replace('HIE', 'HIS')
    pdb_reader = Biomol.PDBMoleculeReader(Base.StringIOStream(pdb_str))

    Biomol.setPDBApplyDictAtomBondingToNonStdResiduesParameter(
        pdb_reader, True)
    if not pdb_reader.read(pdb_mol):
        return None

    Chem.calcImplicitHydrogenCounts(pdb_mol, False)
    Chem.perceiveHybridizationStates(pdb_mol, False)
    Chem.setAtomSymbolsFromTypes(pdb_mol, False)
    Chem.perceiveSSSR(pdb_mol, False)
    Chem.setRingFlags(pdb_mol, False)
    Chem.setAromaticityFlags(pdb_mol, False)

    cdf_mol.assign(pdb_mol)
    for atom in cdf_mol.atoms:
        Chem.set3DCoordinatesArray(atom, Math.Vector3DArray())

    i = 0
    while i < cdf_mol.numAtoms:
        Chem.get3DCoordinatesArray(cdf_mol.getAtom(i)).addElement(
            Chem.get3DCoordinates(pdb_mol.getAtom(i)))
        i += 1

    tmp_output = output + name + ".cdf"
    try:
        Chem.FileCDFMolecularGraphWriter(tmp_output).write(cdf_mol)
    except:
        print('> Cdf_mol writing failure.')
        raise

    residues = Biomol.ResidueList(cdf_mol)
    tmp_output = output + name + "_residue_info.txt"
    with open(tmp_output, 'w') as txt_writer:
        txt_writer.write('residue name_resid_chain\n')
        for res in residues:
            res_id = getResidueID(res)
            txt_writer.write('{}: \n'.format(res_id))

    calc_time = time.time() - initial_time
    print('> Cdf and amino acid residue number list files generated in {}s'.
          format(int(calc_time)))
    def makeRandomTranslation(self,
                              inplace: bool = True,
                              scalingFactor: float = 10) -> Math.Vector3DArray:
        """
        
        :param inplace: 
        :param scalingFactor: Scales the randomly retrieved direction by this factor
        :return: 
        """
        direction = Math.Vector3D()
        direction.assign(np.random.rand(3) * scalingFactor)
        translatedCoords = translate3DObject(self.getCoordinates(), direction)

        if inplace:
            Chem.set3DCoordinates(self, translatedCoords)

        return translatedCoords
    def generateSurfacePoints(self,
                              scaleFactor: float = 1,
                              density: float = 1) -> Math.Vector3DArray:
        """
        Get an array of coordinates corresponding to surface exposed atoms in the protein. Points are placed on the vdw
        surface of the atoms.
        :param scaleFactor: Scales the VDW radius. Points are set at a distance proportional to this factor.
        :param density: Determines the density of points representing the atom surface per square angstrom.
        :return:
        """
        from pyvdwsurface import vdwsurface

        atomTypes = [Chem.getSymbol(a) for a in self.atoms]
        points = vdwsurface(self.getCoordinates(),
                            atomTypes,
                            scale_factor=scaleFactor,
                            double_density=density)
        surfacePointCoordinates = Math.Vector3DArray()
        surfacePointCoordinates.assign(points)
        return surfacePointCoordinates
Beispiel #16
0
import CDPL.Base as Base
import CDPL.Chem as Chem
import CDPL.Math as Math


def process():
    if len(sys.argv) < 4:
	    print('Usage:', sys.argv[0], 'training-set.sdf logP-data regression-coeff-file', file=sys.stderr)
        sys.exit(2)

	struct_is = Base.FileIOStream(sys.argv[1], 'r')
	exp_logp_is = Base.FileIOStream(sys.argv[2], 'r')
	coeff_os = Base.FileIOStream(sys.argv[3], 'w')

    mlr_model = Math.DMLRModel()
	sdf_reader = Chem.SDFMoleculeReader(struct_is)
	mol = Chem.BasicMolecule()
	xlogp_calc = Chem.XLogPCalculator()

    histo = Math.DVector()
    histo.resize(Chem.XLogPCalculator.FEATURE_VECTOR_SIZE)

    Chem.setMultiConfImportParameter(sdf_reader, False)

	while sdf_reader.read(mol):
		exp_logp = float(exp_logp_is.readline())

		Chem.perceiveComponents(mol, False)
		Chem.perceiveSSSR(mol, False)
		Chem.setRingFlags(mol, False)
def process():
    if len(sys.argv) < 3:
        print >> sys.stderr, 'Usage:', sys.argv[
            0], '[input.cdf] [output directory]'
        sys.exit(2)

    in_fname = path.splitext(path.basename(sys.argv[1]))[0]
    mol = Chem.BasicMolecule()
    cdf_reader = Chem.FileCDFMoleculeReader(sys.argv[1])
    pvd_file = open(path.join(sys.argv[2], in_fname + '.pvd'), 'w')

    Util.writePVDHeader(pvd_file)

    print >> sys.stderr, '- Processing CDF-file:', sys.argv[1], '...'

    if not cdf_reader.read(mol):
        print '!! Could not read file'
        sys.exit(2)

    backbone_atoms = []

    for atom in mol.atoms:
        if Biomol.isPDBBackboneAtom(atom) and Biomol.getResidueAtomName(
                atom) == 'C':
            backbone_atoms.append(atom)

    bond_list = []

    for bond in mol.bonds:
        if Biomol.getResidueCode(
                bond.getAtom(0)) == 'HOH' or Biomol.getResidueCode(
                    bond.getAtom(1)) == 'HOH':
            continue

        if Chem.getType(bond.getAtom(0)) == Chem.AtomType.H or Chem.getType(
                bond.getAtom(1)) == Chem.AtomType.H:
            continue

        bond_list.append(bond)

    num_confs = Chem.getNumConformations(mol)

    num_coords = len(bond_list) * 4 + (
        len(backbone_atoms) * SPLINE_POINTS_PER_BB_ATOM - 1) * 2
    bond_ctr = Math.Vector3D()
    i = 0

    while i < num_confs:
        line_x_coords = numpy.ndarray(num_coords, numpy.float32)
        line_y_coords = numpy.ndarray(num_coords, numpy.float32)
        line_z_coords = numpy.ndarray(num_coords, numpy.float32)
        atom_types = numpy.ndarray(num_coords, numpy.uint32)

        spline_ctrl_points = numpy.ndarray((len(backbone_atoms), 3),
                                           numpy.float32)
        j = 0

        for atom in backbone_atoms:
            atom_pos = Chem.getConformer3DCoordinates(atom, i)

            spline_ctrl_points[j, 0] = atom_pos(0)
            spline_ctrl_points[j, 1] = atom_pos(1)
            spline_ctrl_points[j, 2] = atom_pos(2)
            j += 1

        spline_pts = spline(spline_ctrl_points,
                            len(backbone_atoms) * SPLINE_POINTS_PER_BB_ATOM)
        j = 0
        k = 0

        while k < (len(backbone_atoms) * SPLINE_POINTS_PER_BB_ATOM - 1):
            line_x_coords[j] = spline_pts[0][k]
            line_y_coords[j] = spline_pts[1][k]
            line_z_coords[j] = spline_pts[2][k]
            atom_types[j] = 0
            j += 1

            line_x_coords[j] = spline_pts[0][k + 1]
            line_y_coords[j] = spline_pts[1][k + 1]
            line_z_coords[j] = spline_pts[2][k + 1]
            atom_types[j] = 0
            j += 1
            k += 1

        for bond in bond_list:
            atom1 = bond.getAtom(0)
            atom2 = bond.getAtom(1)

            atom1_pos = Chem.getConformer3DCoordinates(atom1, i)
            atom2_pos = Chem.getConformer3DCoordinates(atom2, i)

            atom1_type = Chem.getType(atom1)
            atom2_type = Chem.getType(atom2)

            bond_ctr.assign(atom1_pos)
            bond_ctr += atom2_pos
            bond_ctr *= 0.5

            line_x_coords[j] = atom1_pos(0)
            line_y_coords[j] = atom1_pos(1)
            line_z_coords[j] = atom1_pos(2)
            atom_types[j] = atom1_type
            j += 1

            line_x_coords[j] = bond_ctr(0)
            line_y_coords[j] = bond_ctr(1)
            line_z_coords[j] = bond_ctr(2)
            atom_types[j] = atom1_type
            j += 1

            line_x_coords[j] = bond_ctr(0)
            line_y_coords[j] = bond_ctr(1)
            line_z_coords[j] = bond_ctr(2)
            atom_types[j] = atom2_type
            j += 1

            line_x_coords[j] = atom2_pos(0)
            line_y_coords[j] = atom2_pos(1)
            line_z_coords[j] = atom2_pos(2)
            atom_types[j] = atom2_type
            j += 1

        line_x_coords.resize(j)
        line_y_coords.resize(j)
        line_z_coords.resize(j)
        atom_types.resize(j)

        out_fname = in_fname + '_frame_no_' + str(i)
        out_path = path.join(sys.argv[2], out_fname)
        line_data = {'atom_type': atom_types}

        print >> sys.stderr, '- Writing structure data for frame', i, '...'

        if not pyevtk.hl.linesToVTK(out_path,
                                    line_x_coords,
                                    line_y_coords,
                                    line_z_coords,
                                    pointData=line_data):
            print '!! Could not write output file'
            sys.exit(2)

        Util.writePVDEntry(pvd_file, i, out_fname, 'vtu')

        i += 1

    Util.writePVDFooter(pvd_file)
def process():
    if len(sys.argv) < 3:
        print >> sys.stderr, 'Usage:', sys.argv[
            0], '[input CDF-file] [output CDF-file] [[residue subset]]'
        sys.exit(2)

    print '- Processing CDF-file:', sys.argv[1], '...'

    mol = Util.loadCDFMolecule(sys.argv[1])

    if not mol:
        print '!! Could not read file'
        sys.exit(2)

    residues = Biomol.ResidueList(mol)

    print '- Num. residues:', residues.getSize()

    if len(sys.argv) > 3:
        res_subset_ids = Util.toIntegerList(Util.readLines(sys.argv[3]))

        print '- Residue subset:', res_subset_ids

        Util.filterResidues(residues, res_subset_ids)

    print '- num residues', len(residues)

    num_confs = Chem.getNumConformations(mol)

    print '- Num. frames:', num_confs
    print '- Aligning frames...'

    res_positions = []

    for res in residues:
        atoms = Util.getBackboneAtoms(res)
        positions = []
        i = 0

        while i < num_confs:
            positions.append(Util.calcAtomSetCentroid(atoms, i))
            i += 1

        res_positions.append(positions)

    alignment = Math.DKabschAlgorithm()
    al_ref_positions = Math.DMatrix(3, residues.getSize())
    al_positions = Math.DMatrix(3, residues.getSize())
    i = 0

    while i < residues.getSize():
        pos = res_positions[i][0]

        al_ref_positions.setElement(0, i, pos[0])
        al_ref_positions.setElement(1, i, pos[1])
        al_ref_positions.setElement(2, i, pos[2])
        i += 1

    i = 1
    xform = Math.Matrix4D()

    while i < num_confs:
        j = 0

        while j < residues.getSize():
            pos = res_positions[j][i]

            al_positions.setElement(0, j, pos[0])
            al_positions.setElement(1, j, pos[1])
            al_positions.setElement(2, j, pos[2])
            j += 1

        if not alignment.align(al_positions, al_ref_positions):
            print '!! Could not align frame', i

        else:
            xform.assign(alignment.getTransform())
            Chem.transformConformation(mol, i, xform)

        i += 1

    if not Util.saveCDFMolecule(sys.argv[2], mol):
        print '!! Could not write output file'
        sys.exit(2)
Beispiel #19
0
    # calculate surface area for a single carbon atom
    protein.getGaussianShape()
    shapeFunc = protein.shapeFunc
    carbonProteinIndex = None
    for a in protein.atoms:
        if Chem.getType(a) == 6:
            carbonProteinIndex = protein.getAtomIndex(a)
            break
    surfAreaCarbonProtein = shapeFunc.calcSurfaceArea(carbonProteinIndex)  # calculate the surface area contribution here?

    # create a simple carbon molecule with coordinates
    carbon = Chem.BasicMolecule()
    cAtom = carbon.addAtom()
    Chem.setType(cAtom, 6)
    coords = Math.Vector3D()
    coords.assign([1, 2, 3])
    Chem.set3DCoordinates(cAtom, coords)

    # calculate shape and surface area of carbon molecule
    carbonShape, carbonShapeFunc = getGaussianShapeOfMolecule(carbon)
    surfAreaCarbon = carbonShapeFunc.surfaceArea

    # assert that contribution of carbon atom in protein is in fact the entire surface area of a single carbon
    assert surfAreaCarbon == surfAreaCarbonProtein

    # What am I missing here?
    # Summing the surface area of all atoms in the protein yields the surface area of the protein. I find it hard to
    # believe that all the atoms are surface atoms. Am I missing the VDW radius somehow and all atoms are in fact
    # just points right now?
    # for example:
def process():
    if len(sys.argv) < 3:
        print >> sys.stderr, 'Usage:', sys.argv[
            0], '[input.cdf] [output directory]'
        sys.exit(2)

    in_fname = path.splitext(path.basename(sys.argv[1]))[0]
    grid_set = Grid.DRegularGridSet()
    cdf_reader = Grid.FileCDFDRegularGridSetReader(sys.argv[1])
    pvd_file = open(path.join(sys.argv[2], in_fname + '.pvd'), 'w')

    Util.writePVDHeader(pvd_file)

    print >> sys.stderr, '- Processing grid CDF-file:', sys.argv[1], '...'

    frame_no = 0

    while True:
        grid_set.clear()

        if not cdf_reader.read(grid_set):
            break

        output_data = {}
        grid_spacing = None
        grid_origin = None

        for grid in grid_set:
            int_descr = Grid.getName(grid)
            grid_values = numpy.ndarray(
                (grid.getSize1(), grid.getSize2(), grid.getSize3()),
                numpy.float32)
            output_data[int_descr] = grid_values

            for i in range(grid.getSize1()):
                for j in range(grid.getSize2()):
                    for k in range(grid.getSize3()):
                        grid_values[i, j, k] = grid(i, j, k)

            if not grid_spacing:
                grid_spacing = (grid.getXStepSize(), grid.getYStepSize(),
                                grid.getZStepSize())

            if not grid_origin:
                grid_origin = Math.Vector3D()
                grid.getCoordinates(0, 0, 0, grid_origin)

        out_fname = in_fname + '_frame_no_' + str(frame_no)
        out_path = path.join(sys.argv[2], out_fname)

        print >> sys.stderr, '- Writing grid data for frame', frame_no, '...'

        if not pyevtk.hl.imageToVTK(out_path,
                                    origin=grid_origin,
                                    spacing=grid_spacing,
                                    pointData=output_data):
            print '!! Could not write grid output file'
            sys.exit(2)

        Util.writePVDEntry(pvd_file, frame_no, out_fname, 'vti')

        frame_no += 1

    Util.writePVDFooter(pvd_file)
Beispiel #21
0
def process():
    if len(sys.argv) < 4:
        print >> sys.stderr, 'Usage:', sys.argv[
            0], '[input topology-file] [input coordinates-file] [output CDF-file]'
        sys.exit(2)

    print >> sys.stderr, '- Processing topology-file', sys.argv[
        1], 'and coordinates-file', sys.argv[2], '...'

    u = MDAnalysis.Universe(sys.argv[1], sys.argv[2])
    cdf_mol = Chem.BasicMolecule()

    cdf_mol.reserveMemoryForAtoms(len(u.atoms))
    cdf_mol.reserveMemoryForBonds(len(u.bonds))

    print >> sys.stderr, '- Num. atoms:', len(u.atoms)
    print >> sys.stderr, '- Num. bonds:', len(u.bonds)

    num_frames = len(u.trajectory)

    print >> sys.stderr, '- Num. frames:', num_frames

    # construct atoms

    print >> sys.stderr, '- Building atoms ...'

    waters = {}
    i = 0

    for md_atom in u.atoms:
        atom = cdf_mol.addAtom()
        sym = MDAnalysis.topology.guessers.guess_atom_element(md_atom.name)

        Chem.setSymbol(atom, sym.title())
        Chem.setImplicitHydrogenCount(atom, 0)
        Biomol.setChainID(atom, md_atom.segid)

        if md_atom.resname == 'WAT':
            Biomol.setResidueCode(atom, 'HOH')
        else:
            Biomol.setResidueCode(atom, md_atom.resname)

        if Biomol.getResidueCode(atom) == 'HOH':
            if md_atom.resid in waters:
                waters[md_atom.resid].append(i)
            else:
                waters[md_atom.resid] = [i]

        Biomol.setResidueSequenceNumber(atom, int(md_atom.resid))
        Biomol.setResidueAtomName(atom, md_atom.name)

        # fix positive charge on arginin nitrogen
        if md_atom.resname == 'ARG' and md_atom.name == 'NH2':
            Chem.setFormalCharge(atom, 1)

        coords = []
        for coord in md_atom.position:
            coords.append(float(coord))

        Chem.set3DCoordinates(atom, coords)

        coords_array = Math.Vector3DArray()
        coords_array.reserve(num_frames)

        Chem.set3DCoordinatesArray(atom, coords_array)
        Chem.setPEOECharge(atom, float(md_atom.charge))

        i += 1

    Chem.setAtomTypesFromSymbols(cdf_mol, True)

    # construct bonds

    print >> sys.stderr, '- Building bonds ...'

    for md_bond in u.bonds:
        cdf_mol.addBond(int(md_bond.atoms[0].index),
                        int(md_bond.atoms[1].index))

    print >> sys.stderr, '- Building water atom bonds ...'

    for water in waters.values():
        if len(water) < 2:
            continue

        for atom_idx in water:
            if Chem.getType(cdf_mol.atoms[atom_idx]) == Chem.AtomType.O:
                if atom.numBonds > 1:
                    break

                for atom_idx2 in water:
                    if Chem.getType(
                            cdf_mol.atoms[atom_idx2]) == Chem.AtomType.H:
                        cdf_mol.addBond(atom_idx, atom_idx2)

                break

    # make sane biomolecule

    Chem.perceiveSSSR(cdf_mol, True)
    Chem.setRingFlags(cdf_mol, True)
    Chem.perceiveBondOrders(cdf_mol, True)
    Chem.perceiveHybridizationStates(cdf_mol, True)
    Chem.setAromaticityFlags(cdf_mol, True)
    Chem.calcFormalCharges(cdf_mol, True)

    # read timsteps and write cdf

    print >> sys.stderr, '- Importing coordinates ...'

    i = 0
    traj_coords = []
    atom_coords = Math.Vector3D()

    for ts in u.trajectory:
        print >> sys.stderr, '- Processing time step', i, '...'

        for md_atom in u.atoms:
            del traj_coords[:]

            for coord in md_atom.position:
                traj_coords.append(float(coord))

            coords_array = Chem.get3DCoordinatesArray(
                cdf_mol.getAtom(int(md_atom.index)))

            atom_coords[0] = traj_coords[0]
            atom_coords[1] = traj_coords[1]
            atom_coords[2] = traj_coords[2]

            coords_array.addElement(atom_coords)

        i += 1

    print >> sys.stderr, '- Writing output file:'

    if not Chem.FileCDFMolecularGraphWriter(sys.argv[3]).write(cdf_mol):
        print >> sys.stderr, '!! Could not write output file'
        sys.exit(2)
def process():
    if len(sys.argv) < 4:
        print(
            'Usage:',
            sys.argv[0],
            '[input torsion rules.xml] [structures.sdf] [output torsion histogram library.sdf]',
            file=sys.stderr)
        sys.exit(2)

    tor_lib = ConfGen.TorsionLibrary()

    try:
        tor_lib.load(Base.FileIOStream(sys.argv[1], 'r'))
    except:
        print('Error while loading input torsion rules:',
              sys.exc_info()[0],
              file=sys.stderr)
        sys.exit(2)

    tor_matcher = ConfGen.TorsionRuleMatcher(tor_lib)

    tor_matcher.findAllRuleMappings(True)
    tor_matcher.findUniqueMappingsOnly(True)
    tor_matcher.stopAtFirstMatchingRule(True)

    mol = Chem.BasicMolecule()
    mol_reader = Chem.FileSDFMoleculeReader(sys.argv[2])

    Chem.setMultiConfImportParameter(mol_reader, False)

    print('- Analyzing input structures...', file=sys.stderr)

    i = 1
    rule_to_angle_hists = {}
    coords = Math.Vector3DArray()

    while True:
        try:
            if not mol_reader.read(mol):
                break

        except IOError as e:
            print('Error while reading input molecule',
                  i,
                  ':',
                  e,
                  file=sys.stderr)
            i += 1
            continue

        if i % 500 == 0:
            print('   ... At input molecule', i, '...', file=sys.stderr)

        Chem.initSubstructureSearchTarget(mol, False)

        try:
            Chem.get3DCoordinates(mol, coords)

        except Base.ItemNotFound:
            print('Could not get 3D-coordinates for molecule',
                  i,
                  file=sys.stderr)
            i += 1
            continue

        for bond in mol.bonds:
            if Chem.getRingFlag(bond):
                continue

            if Chem.isHydrogenBond(bond):
                continue

            if Chem.getExplicitBondCount(
                    bond.getBegin()) <= 1 or Chem.getExplicitBondCount(
                        bond.getEnd()) <= 1:
                continue

            tor_matcher.findMatches(bond, mol, False)

            for match in tor_matcher:
                processMatch(i, match, mol, coords, rule_to_angle_hists)

        i += 1

    print('- Processing torsion angle histograms...', file=sys.stderr)

    processHistograms(tor_lib, rule_to_angle_hists)

    print('- Writing output torsion library...', file=sys.stderr)

    try:
        tor_lib.save(Base.FileIOStream(sys.argv[3], 'w+'))

    except:
        print('Error while writing torsion library:',
              sys.exc_info()[0],
              file=sys.stderr)
        sys.exit(2)

    print('DONE!', file=sys.stderr)
def process():
    if len(sys.argv) < 3:
        print >> sys.stderr, 'Usage:', sys.argv[0], '[input.cdf] [output directory]'
        sys.exit(2)

    print >> sys.stderr, '> Processing grid CDF-file:', sys.argv[1], '...'

    grid_set = Grid.DRegularGridSet()
    cdf_grid_reader = Grid.FileCDFDRegularGridSetReader(sys.argv[1])

    if not cdf_grid_reader.read(grid_set):
        print '> CDF reader failure.'

    new_grids = Grid.DRegularGridSet()
    for grid in grid_set:
        g1 = grid.getSize1()
        g2 = grid.getSize2()
        g3 = grid.getSize3()
        g_type = Grid.getName(grid)

        new_grids.addElement(Grid.DRegularGrid(g1, g2, g3).assign(grid))
        Grid.setName(new_grids.getLastElement(), "Avg_" + g_type)

        new_grids.addElement(Grid.DRegularGrid(g1, g2, g3).assign(grid))
        Grid.setName(new_grids.getLastElement(), "Std_" + g_type)

        for k in range(g3):
            for i in range(g1):
                for j in range(g2):
                    new_grids.getLastElement()[i, j, k] = 0

    print >> sys.stderr, '> Calculating averaged grids...'

    index_frame = 0

    while True:
        grid_set.clear()
        if not cdf_grid_reader.read(grid_set):
            break
        n = 0
        for grid in grid_set:
            tmp_mean = Math.DGrid(grid.getSize1(), grid.getSize2(), grid.getSize3())
            g_mean = new_grids[2 * n]
            g_v = new_grids[2 * n + 1]

            tmp_mean.assign(g_mean + (grid - g_mean) / (index_frame + 1))

            g_v.assign(g_v + Math.elemProd((grid - g_mean), (grid - tmp_mean)))
            g_mean.assign(tmp_mean)

            n += 1
        index_frame += 1

    n = 0

    for grid in grid_set:
        g1 = grid.getSize1()
        g2 = grid.getSize2()
        g3 = grid.getSize3()
        g_v = new_grids[2 * n + 1]

        for k in range(g3):
            for i in range(g1):
                for j in range(g2):
                    variance = round(g_v(i, j, k) / (index_frame+1), 10) #### Due to calculations, negative values closer than e-10 from 0 are rounded to avoid warnings
                    g_v[i, j, k] = np.sqrt(variance)

        n += 1

    print >> sys.stderr, '> Writing averaged grids to', sys.argv[2]

    out_fname = path.splitext(path.basename(sys.argv[1]))[0] + '_average_grid.cdf'
    out_path = path.join(sys.argv[2], out_fname)
    grid_set_writer = Grid.FileCDFDRegularGridSetWriter(out_path)

    if not grid_set_writer.write(new_grids):
        print >> sys.stderr, '> Could not write output file'