Exemplo n.º 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
Exemplo n.º 2
0
def calcMeanPos(positions):
    mean_pos = Math.Vector3D()

    for pos in positions:
        mean_pos += pos

    mean_pos /= len(positions)
    return mean_pos
Exemplo n.º 3
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
Exemplo n.º 4
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 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
Exemplo n.º 6
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
Exemplo n.º 7
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()
Exemplo n.º 8
0
    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 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)
Exemplo n.º 10
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)
Exemplo n.º 12
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)