def calc_ensembles(structure_orig):
    structure_ca = structure_orig.select('ca or name P')
    structure_anm = pr.ANM('structure ca')
    structure_anm.buildHessian(structure_ca)
    structure_anm.calcModes(n_modes=5)
    structure_anm_ext, structure_all = pr.extendModel(structure_anm,
                                                      structure_ca,
                                                      structure_orig,
                                                      norm=True)
    ens = pr.sampleModes(structure_anm_ext,
                         atoms=structure_all.all,
                         n_confs=1,
                         rmsd=1.5)
    return ens, structure_all
Example #2
0
def calculate_nmodes(pdb_file_name, n_modes, molecule):
    """Calculates Normal modes for a given molecule"""
    prody_molecule = parsePDB(pdb_file_name)
    # Try first for proteins
    backbone_atoms = prody_molecule.select('name CA')
    if not backbone_atoms:
        # If previous step has failed, maybe we're dealing with DNA
        backbone_atoms = prody_molecule.select("nucleic and name C4'")
    if not backbone_atoms:
        raise NormalModesCalculationError("Error selecting backbone atoms (protein or DNA)")
    molecule_anm = ANM('molecule backbone')
    molecule_anm.buildHessian(backbone_atoms)
    molecule_anm.calcModes(n_modes=n_modes)

    num_atoms_prody = prody_molecule.numAtoms()

    if num_atoms_prody != molecule.num_atoms:
        log.error("Number of atoms in ProDy (%d) vs LightDock (%d)" % (num_atoms_prody, molecule.num_atoms))
        raise NormalModesCalculationError("Number of atoms is different")

    # Check for sanity in atoms from both structures just in case
    for lightdock_atom, prody_atom in zip (molecule.atoms, prody_molecule.iterAtoms()):
        if lightdock_atom.name != prody_atom.getName():
            raise NormalModesCalculationError("Atoms differ: %s - %s" % (str(lightdock_atom),
                                                                         str(prody_atom)))

    molecule_anm_ext, molecule_all = extendModel(molecule_anm, backbone_atoms, prody_molecule, norm=True)
    modes = []
    calculated_n_modes = (molecule_anm_ext.getEigvecs()).shape[1]
    try:
        for i in range(calculated_n_modes):
            nm = molecule_anm_ext.getEigvecs()[:, i].reshape((num_atoms_prody, 3))
            modes.append(nm)
    except (ValueError, IndexError) as e:
        log.info("Number of atoms of the ANM model: %s" % str(molecule_anm_ext.numAtoms()))
        log.info("Number of nodes in the model: %s" % str((molecule_anm_ext.getEigvecs()).shape))
        raise NormalModesCalculationError("Number of atoms and ANM model differ. Please, check there are no missing "
                                          "nucleotides nor residues.")
    if calculated_n_modes < n_modes:
        log.warning("Number of non-trivial calculated modes is %d (asked for %d)" % (calculated_n_modes, n_modes))
        # Padding
        for i in range(n_modes - calculated_n_modes):
            modes.append(np.zeros((num_atoms_prody, 3)))

    return np.array(modes)
Example #3
0
def prody_modes(molecule, max_modes, algorithm=None, **options):
    """
    Parameters
    ----------
    molecule : prody.AtomGroup
    nax_modes : int
        number of modes to calculate
    algorithm : callable, optional, default=None
        coarseGrain(prm) wich make molecule.select().setBetas(i) where i
        is the index Coarse Grain group
        Where prm is prody AtomGroup
    options : dict, optional
        Parameters for algorithm callable

    Returns
    -------
    modes : ProDy modes ANM or RTB
    """
    modes = None
    if algorithm in ['residues', 'mass']:
        title = 'normal modes for {}'.format(molecule.getTitle())
        molecule = algorithm(molecule, **options)
        modes = prody.RTB(title)
        modes.buildHessian(molecule.getCoords(), molecule.getBetas())
        modes.calcModes(n_modes=max_modes)
    elif algorithm == 'calpha':
        calphas_modes = prody.ANM('normal modes for {}'.format(
            molecule.getTitle()))
        calphas = molecule = molecule.select(algorithm)
        calphas_modes.buildHessian(calphas)
        calphas_modes.calcModes(n_modes=max_modes)
        modes = prody.extendModel(calphas_modes, calphas, molecule,
                                  norm=True)[0]
    else:
        modes = prody.ANM('normal modes for {}'.format(molecule.getTitle()))
        modes.buildHessian(molecule)
        modes.calcModes(n_modes=max_modes)
    return modes
Example #4
0
    protein_anm.buildHessian(ca_atoms)
    protein_anm.calcModes(n_modes=n_modes)
    print('Normal modes calculated')

    atoms, residues, chains = parse_complex_from_file(pdb_structure)
    lightdock_structures = [{'atoms': atoms, 'residues': residues, 'chains': chains, 'file_name': pdb_structure}]
    lightdock_structure = Complex.from_structures(lightdock_structures)
    print('Structure read by lightdock')

    num_atoms_prody = len(protein.protein)
    num_atoms_lightdock = len(atoms)

    if num_atoms_prody != num_atoms_lightdock:
        raise SystemExit("Number of atoms is different")

    protein_anm_ext, protein_all = extendModel(protein_anm, ca_atoms, protein, norm=True)

    modes = []
    for i in range(n_modes):
        nm = protein_anm_ext.getEigvecs()[:, i].reshape((num_atoms_lightdock, 3))
        modes.append(nm)

    coordinates = lightdock_structure.atom_coordinates[0].coordinates
    for i in range(n_modes):
        lightdock_structure.atom_coordinates[0].coordinates += modes[i] * factor

    output_file = 'anm_' + pdb_structure
    write_pdb_to_file(lightdock_structure, output_file, lightdock_structure[0])

    print('Structure written to %s' % output_file)
    print('Done.')
Example #5
0
def prody_pca(coords, **kwargs):
    """Perform PCA calculations for PDB or DCD format *coords* file.

    """

    for key in DEFAULTS:
        if not key in kwargs:
            kwargs[key] = DEFAULTS[key]

    from os.path import isdir, splitext, join
    outdir = kwargs.get('outdir')
    if not isdir(outdir):
        raise IOError('{0} is not a valid path'.format(repr(outdir)))

    import prody
    LOGGER = prody.LOGGER

    prefix = kwargs.get('prefix')
    nmodes = kwargs.get('nmodes')
    selstr = kwargs.get('select')

    ext = splitext(coords)[1].lower()
    if ext == '.gz':
        ext = splitext(coords[:-3])[1].lower()

    if ext == '.dcd':
        pdb = kwargs.get('psf') or kwargs.get('pdb')
        if pdb:
            if splitext(pdb)[1].lower() == '.psf':
                pdb = prody.parsePSF(pdb)
            else:
                pdb = prody.parsePDB(pdb)
        dcd = prody.DCDFile(coords)
        if prefix == '_pca' or prefix == '_eda':
            prefix = dcd.getTitle() + prefix

        if len(dcd) < 2:
            raise ValueError('DCD file must have multiple frames')
        if pdb:
            if pdb.numAtoms() == dcd.numAtoms():
                select = pdb.select(selstr)
                dcd.setAtoms(select)
                LOGGER.info('{0} atoms are selected for calculations.'
                            .format(len(select)))
            else:
                select = pdb.select(selstr)
                if select.numAtoms() != dcd.numAtoms():
                    raise ValueError('number of selected atoms ({0}) does '
                                     'not match number of atoms in the DCD '
                                     'file ({1})'.format(select.numAtoms(),
                                                           dcd.numAtoms()))
                if pdb.numCoordsets():
                    dcd.setCoords(select.getCoords())

        else:
            select = prody.AtomGroup()
            select.setCoords(dcd.getCoords())
        pca = prody.PCA(dcd.getTitle())
        if len(dcd) > 1000:
            pca.buildCovariance(dcd, aligned=kwargs.get('aligned'))
            pca.calcModes(nmodes)
            ensemble = dcd
        else:
            ensemble = dcd[:]
            if not kwargs.get('aligned'):
                ensemble.iterpose()
            pca.performSVD(ensemble)

    else:
        pdb = prody.parsePDB(coords)
        if pdb.numCoordsets() < 2:
            raise ValueError('PDB file must contain multiple models')

        if prefix == '_pca' or prefix == '_eda':
            prefix = pdb.getTitle() + prefix

        select = pdb.select(selstr)
        LOGGER.info('{0} atoms are selected for calculations.'
                    .format(len(select)))
        if select is None:
            raise ValueError('selection {0} do not match any atoms'
                                .format(repr(selstr)))
        LOGGER.info('{0} atoms will be used for PCA calculations.'
                    .format(len(select)))
        ensemble = prody.Ensemble(select)
        pca = prody.PCA(pdb.getTitle())
        if not kwargs.get('aligned'):
            ensemble.iterpose()
        pca.performSVD(ensemble)


    LOGGER.info('Writing numerical output.')
    if kwargs.get('outnpz'):
        prody.saveModel(pca, join(outdir, prefix))

    prody.writeNMD(join(outdir, prefix + '.nmd'), pca[:nmodes], select)

    extend = kwargs.get('extend')
    if extend:
        if pdb:
            if extend == 'all':
                extended = prody.extendModel(pca[:nmodes], select, pdb)
            else:
                extended = prody.extendModel(pca[:nmodes], select,
                                             select | pdb.bb)
            prody.writeNMD(join(outdir, prefix + '_extended_' +
                           extend + '.nmd'), *extended)
        else:
            prody.LOGGER.warn('Model could not be extended, provide a PDB or '
                              'PSF file.')
    outall = kwargs.get('outall')
    delim = kwargs.get('numdelim')
    ext = kwargs.get('numext')
    format = kwargs.get('numformat')

    if outall or kwargs.get('outeig'):
        prody.writeArray(join(outdir, prefix + '_evectors'+ext),
                         pca.getArray(), delimiter=delim, format=format)
        prody.writeArray(join(outdir, prefix + '_evalues'+ext),
                         pca.getEigvals(), delimiter=delim, format=format)
    if outall or kwargs.get('outcov'):
        prody.writeArray(join(outdir, prefix + '_covariance'+ext),
                         pca.getCovariance(), delimiter=delim, format=format)
    if outall or kwargs.get('outcc') or kwargs.get('outhm'):
        cc = prody.calcCrossCorr(pca)
        if outall or kwargs.get('outcc'):
            prody.writeArray(join(outdir, prefix + '_cross-correlations' +
                             ext), cc, delimiter=delim, format=format)
        if outall or kwargs.get('outhm'):
            resnums = select.getResnums()
            hmargs = {} if resnums is None else {'resnums': resnums}
            prody.writeHeatmap(join(outdir, prefix + '_cross-correlations.hm'),
                               cc, xlabel='Residue', ylabel='Residue',
                               title=pca.getTitle() + ' cross-correlations',
                               **hmargs)

    if outall or kwargs.get('outsf'):
        prody.writeArray(join(outdir, prefix + '_sqfluct'+ext),
                         prody.calcSqFlucts(pca), delimiter=delim,
                         format=format)
    if outall or kwargs.get('outproj'):
        prody.writeArray(join(outdir, prefix + '_proj'+ext),
                         prody.calcProjection(ensemble, pca), delimiter=delim,
                         format=format)

    figall = kwargs.get('figall')
    cc = kwargs.get('figcc')
    sf = kwargs.get('figsf')
    sp = kwargs.get('figproj')

    if figall or cc or sf or sp:
        try:
            import matplotlib.pyplot as plt
        except ImportError:
            LOGGER.warning('Matplotlib could not be imported. '
                           'Figures are not saved.')
        else:
            prody.SETTINGS['auto_show'] = False
            LOGGER.info('Saving graphical output.')
            format = kwargs.get('figformat')
            width = kwargs.get('figwidth')
            height = kwargs.get('figheight')
            dpi = kwargs.get('figdpi')

            format = format.lower()
            if figall or cc:
                plt.figure(figsize=(width, height))
                prody.showCrossCorr(pca)
                plt.savefig(join(outdir, prefix + '_cc.'+format),
                    dpi=dpi, format=format)
                plt.close('all')
            if figall or sf:
                plt.figure(figsize=(width, height))
                prody.showSqFlucts(pca)
                plt.savefig(join(outdir, prefix + '_sf.'+format),
                    dpi=dpi, format=format)
                plt.close('all')
            if figall or sp:
                indices = []
                for item in sp.split():
                    try:
                        if '-' in item:
                            item = item.split('-')
                            if len(item) == 2:
                                indices.append(list(range(int(item[0])-1,
                                                          int(item[1]))))
                        elif ',' in item:
                            indices.append([int(i)-1 for i in item.split(',')])
                        else:
                            indices.append(int(item)-1)
                    except:
                        pass
                for index in indices:
                        plt.figure(figsize=(width, height))
                        prody.showProjection(ensemble, pca[index])
                        if isinstance(index, int):
                            index = [index]
                        index = [str(i+1) for i in index]
                        plt.savefig(join(outdir, prefix + '_proj_' +
                            '_'.join(index) + '.' + format),
                            dpi=dpi, format=format)
                        plt.close('all')
Example #6
0
def prody_anm(pdb, **kwargs):
    """Perform ANM calculations for *pdb*.

    """

    for key in DEFAULTS:
        if not key in kwargs:
            kwargs[key] = DEFAULTS[key]

    from os.path import isdir, join
    outdir = kwargs.get('outdir')
    if not isdir(outdir):
        raise IOError('{0} is not a valid path'.format(repr(outdir)))

    import numpy as np
    import prody
    LOGGER = prody.LOGGER

    selstr = kwargs.get('select')
    prefix = kwargs.get('prefix')
    cutoff = kwargs.get('cutoff')
    gamma = kwargs.get('gamma')
    nmodes = kwargs.get('nmodes')
    selstr = kwargs.get('select')
    model = kwargs.get('model')

    pdb = prody.parsePDB(pdb, model=model)
    if prefix == '_anm':
        prefix = pdb.getTitle() + '_anm'

    select = pdb.select(selstr)
    if select is None:
        LOGGER.warn('Selection {0} did not match any atoms.'
                    .format(repr(selstr)))
        return
    LOGGER.info('{0} atoms will be used for ANM calculations.'
                .format(len(select)))

    anm = prody.ANM(pdb.getTitle())
    anm.buildHessian(select, cutoff, gamma)
    anm.calcModes(nmodes)
    LOGGER.info('Writing numerical output.')
    if kwargs.get('outnpz'):
        prody.saveModel(anm, join(outdir, prefix))
    prody.writeNMD(join(outdir, prefix + '.nmd'), anm, select)

    extend = kwargs.get('extend')
    if extend:
        if extend == 'all':
            extended = prody.extendModel(anm, select, pdb)
        else:
            extended = prody.extendModel(anm, select, select | pdb.bb)
        prody.writeNMD(join(outdir, prefix + '_extended_' +
                       extend + '.nmd'), *extended)

    outall = kwargs.get('outall')
    delim = kwargs.get('numdelim')
    ext = kwargs.get('numext')
    format = kwargs.get('numformat')


    if outall or kwargs.get('outeig'):
        prody.writeArray(join(outdir, prefix + '_evectors'+ext),
                         anm.getArray(), delimiter=delim, format=format)
        prody.writeArray(join(outdir, prefix + '_evalues'+ext),
                         anm.getEigvals(), delimiter=delim, format=format)

    if outall or kwargs.get('outbeta'):
        from prody.utilities import openFile
        fout = openFile(prefix + '_beta.txt', 'w', folder=outdir)
        fout.write('{0[0]:1s} {0[1]:4s} {0[2]:4s} {0[3]:5s} {0[4]:5s}\n'
                       .format(['C', 'RES', '####', 'Exp.', 'The.']))
        for data in zip(select.getChids(), select.getResnames(),
                        select.getResnums(), select.getBetas(),
                        prody.calcTempFactors(anm, select)):
            fout.write('{0[0]:1s} {0[1]:4s} {0[2]:4d} {0[3]:5.2f} {0[4]:5.2f}\n'
                       .format(data))
        fout.close()

    if outall or kwargs.get('outcov'):
        prody.writeArray(join(outdir, prefix + '_covariance' + ext),
                         anm.getCovariance(), delimiter=delim, format=format)

    if outall or kwargs.get('outcc') or kwargs.get('outhm'):
        cc = prody.calcCrossCorr(anm)
        if outall or kwargs.get('outcc'):
            prody.writeArray(join(outdir, prefix +
                             '_cross-correlations' + ext),
                             cc, delimiter=delim,  format=format)
        if outall or kwargs.get('outhm'):
            prody.writeHeatmap(join(outdir, prefix + '_cross-correlations.hm'),
                               cc, resnum=select.getResnums(),
                               xlabel='Residue', ylabel='Residue',
                               title=anm.getTitle() + ' cross-correlations')

    if outall or kwargs.get('hessian'):
        prody.writeArray(join(outdir, prefix + '_hessian'+ext),
                         anm.getHessian(), delimiter=delim, format=format)

    if outall or kwargs.get('kirchhoff'):
        prody.writeArray(join(outdir, prefix + '_kirchhoff'+ext),
                         anm.getKirchhoff(), delimiter=delim, format=format)

    if outall or kwargs.get('outsf'):
        prody.writeArray(join(outdir, prefix + '_sqflucts'+ext),
                         prody.calcSqFlucts(anm), delimiter=delim,
                         format=format)

    figall = kwargs.get('figall')
    cc = kwargs.get('figcc')
    sf = kwargs.get('figsf')
    bf = kwargs.get('figbeta')
    cm = kwargs.get('figcmap')


    if figall or cc or sf or bf or cm:
        try:
            import matplotlib.pyplot as plt
        except ImportError:
            LOGGER.warning('Matplotlib could not be imported. '
                           'Figures are not saved.')
        else:
            prody.SETTINGS['auto_show'] = False
            LOGGER.info('Saving graphical output.')
            format = kwargs.get('figformat')
            width = kwargs.get('figwidth')
            height = kwargs.get('figheight')
            dpi = kwargs.get('figdpi')
            format = format.lower()

            if figall or cc:
                plt.figure(figsize=(width, height))
                prody.showCrossCorr(anm)
                plt.savefig(join(outdir, prefix + '_cc.'+format),
                    dpi=dpi, format=format)
                plt.close('all')

            if figall or cm:
                plt.figure(figsize=(width, height))
                prody.showContactMap(anm)
                plt.savefig(join(outdir, prefix + '_cm.'+format),
                    dpi=dpi, format=format)
                plt.close('all')

            if figall or sf:
                plt.figure(figsize=(width, height))
                prody.showSqFlucts(anm)
                plt.savefig(join(outdir, prefix + '_sf.'+format),
                    dpi=dpi, format=format)
                plt.close('all')

            if figall or bf:
                plt.figure(figsize=(width, height))
                bexp = select.getBetas()
                bcal = prody.calcTempFactors(anm, select)
                plt.plot(bexp, label='Experimental')
                plt.plot(bcal, label=('Theoretical (R={0:.2f})'
                                        .format(np.corrcoef(bcal, bexp)[0,1])))
                plt.legend(prop={'size': 10})
                plt.xlabel('Node index')
                plt.ylabel('Experimental B-factors')
                plt.title(pdb.getTitle() + ' B-factors')
                plt.savefig(join(outdir, prefix + '_bf.'+format),
                    dpi=dpi, format=format)
                plt.close('all')
Example #7
0
    protein_anm.buildHessian(ca_atoms)
    protein_anm.calcModes(n_modes=n_modes)
    print 'Normal modes calculated'

    atoms, residues, chains = parse_complex_from_file(pdb_structure)
    lightdock_structures = [{'atoms': atoms, 'residues': residues, 'chains': chains, 'file_name': pdb_structure}]
    lightdock_structure = Complex.from_structures(lightdock_structures)
    print 'Structure read by lightdock'

    num_atoms_prody = len(protein.protein)
    num_atoms_lightdock = len(atoms)

    if num_atoms_prody != num_atoms_lightdock:
        raise SystemExit("Number of atoms is different")

    protein_anm_ext, protein_all = extendModel(protein_anm, ca_atoms, protein, norm=True)

    modes = []
    for i in range(n_modes):
        nm = protein_anm_ext.getEigvecs()[:, i].reshape((num_atoms_lightdock, 3))
        modes.append(nm)

    coordinates = lightdock_structure.atom_coordinates[0].coordinates
    for i in range(n_modes):
        lightdock_structure.atom_coordinates[0].coordinates += modes[i] * factor

    output_file = 'anm_' + pdb_structure
    write_pdb_to_file(lightdock_structure, output_file, lightdock_structure[0])

    print 'Structure written to %s' % output_file
    print 'Done.'
Example #8
0
def prody_pca(coords, **kwargs):
    """Perform PCA calculations for PDB or DCD format *coords* file.

    """

    for key in DEFAULTS:
        if not key in kwargs:
            kwargs[key] = DEFAULTS[key]

    from os.path import isdir, splitext, join
    outdir = kwargs.get('outdir')
    if not isdir(outdir):
        raise IOError('{0} is not a valid path'.format(repr(outdir)))

    import prody
    LOGGER = prody.LOGGER

    prefix = kwargs.get('prefix')
    nmodes = kwargs.get('nmodes')
    selstr = kwargs.get('select')
    quiet = kwargs.pop('quiet', False)
    altloc = kwargs.get('altloc')

    ext = splitext(coords)[1].lower()
    if ext == '.gz':
        ext = splitext(coords[:-3])[1].lower()

    if ext == '.dcd':
        pdb = kwargs.get('psf') or kwargs.get('pdb')
        if pdb:
            if splitext(pdb)[1].lower() == '.psf':
                pdb = prody.parsePSF(pdb)
            else:
                pdb = prody.parsePDB(pdb, altlocs=altlocs)
        dcd = prody.DCDFile(coords)
        if prefix == '_pca' or prefix == '_eda':
            prefix = dcd.getTitle() + prefix

        if len(dcd) < 2:
            raise ValueError('DCD file must have multiple frames')
        if pdb:
            if pdb.numAtoms() == dcd.numAtoms():
                select = pdb.select(selstr)
                dcd.setAtoms(select)
                LOGGER.info('{0} atoms are selected for calculations.'.format(
                    len(select)))
            else:
                select = pdb.select(selstr)
                if select.numAtoms() != dcd.numAtoms():
                    raise ValueError('number of selected atoms ({0}) does '
                                     'not match number of atoms in the DCD '
                                     'file ({1})'.format(
                                         select.numAtoms(), dcd.numAtoms()))
                if pdb.numCoordsets():
                    dcd.setCoords(select.getCoords())

        else:
            select = prody.AtomGroup()
            select.setCoords(dcd.getCoords())
        pca = prody.PCA(dcd.getTitle())

        nproc = kwargs.get('nproc')
        if nproc:
            try:
                from threadpoolctl import threadpool_limits
            except ImportError:
                raise ImportError(
                    'Please install threadpoolctl to control threads')

            with threadpool_limits(limits=nproc, user_api="blas"):
                if len(dcd) > 1000:
                    pca.buildCovariance(dcd,
                                        aligned=kwargs.get('aligned'),
                                        quiet=quiet)
                    pca.calcModes(nmodes)
                    ensemble = dcd
                else:
                    ensemble = dcd[:]
                    if not kwargs.get('aligned'):
                        ensemble.iterpose(quiet=quiet)
                    pca.performSVD(ensemble)
                nmodes = pca.numModes()
        else:
            if len(dcd) > 1000:
                pca.buildCovariance(dcd,
                                    aligned=kwargs.get('aligned'),
                                    quiet=quiet)
                pca.calcModes(nmodes)
                ensemble = dcd
            else:
                ensemble = dcd[:]
                if not kwargs.get('aligned'):
                    ensemble.iterpose(quiet=quiet)
                pca.performSVD(ensemble)
            nmodes = pca.numModes()

    else:
        pdb = prody.parsePDB(coords)
        if pdb.numCoordsets() < 2:
            raise ValueError('PDB file must contain multiple models')

        if prefix == '_pca' or prefix == '_eda':
            prefix = pdb.getTitle() + prefix

        select = pdb.select(selstr)
        LOGGER.info('{0} atoms are selected for calculations.'.format(
            len(select)))
        if select is None:
            raise ValueError('selection {0} do not match any atoms'.format(
                repr(selstr)))
        LOGGER.info('{0} atoms will be used for PCA calculations.'.format(
            len(select)))
        ensemble = prody.Ensemble(select)
        pca = prody.PCA(pdb.getTitle())
        if not kwargs.get('aligned'):
            ensemble.iterpose()

        nproc = kwargs.get('nproc')
        if nproc:
            try:
                from threadpoolctl import threadpool_limits
            except ImportError:
                raise ImportError(
                    'Please install threadpoolctl to control threads')

            with threadpool_limits(limits=nproc, user_api="blas"):
                pca.performSVD(ensemble)
        else:
            pca.performSVD(ensemble)

    LOGGER.info('Writing numerical output.')
    if kwargs.get('outnpz'):
        prody.saveModel(pca, join(outdir, prefix))

    if kwargs.get('outscipion'):
        prody.writeScipionModes(outdir, pca)

    prody.writeNMD(join(outdir, prefix + '.nmd'), pca[:nmodes], select)

    extend = kwargs.get('extend')
    if extend:
        if pdb:
            if extend == 'all':
                extended = prody.extendModel(pca[:nmodes], select, pdb)
            else:
                extended = prody.extendModel(pca[:nmodes], select,
                                             select | pdb.bb)
            prody.writeNMD(
                join(outdir, prefix + '_extended_' + extend + '.nmd'),
                *extended)
        else:
            prody.LOGGER.warn('Model could not be extended, provide a PDB or '
                              'PSF file.')
    outall = kwargs.get('outall')
    delim = kwargs.get('numdelim')
    ext = kwargs.get('numext')
    format = kwargs.get('numformat')

    if outall or kwargs.get('outeig'):
        prody.writeArray(join(outdir, prefix + '_evectors' + ext),
                         pca.getArray(),
                         delimiter=delim,
                         format=format)
        prody.writeArray(join(outdir, prefix + '_evalues' + ext),
                         pca.getEigvals(),
                         delimiter=delim,
                         format=format)
    if outall or kwargs.get('outcov'):
        prody.writeArray(join(outdir, prefix + '_covariance' + ext),
                         pca.getCovariance(),
                         delimiter=delim,
                         format=format)
    if outall or kwargs.get('outcc') or kwargs.get('outhm'):
        cc = prody.calcCrossCorr(pca)
        if outall or kwargs.get('outcc'):
            prody.writeArray(join(outdir,
                                  prefix + '_cross-correlations' + ext),
                             cc,
                             delimiter=delim,
                             format=format)
        if outall or kwargs.get('outhm'):
            resnums = select.getResnums()
            hmargs = {} if resnums is None else {'resnums': resnums}
            prody.writeHeatmap(join(outdir, prefix + '_cross-correlations.hm'),
                               cc,
                               xlabel='Residue',
                               ylabel='Residue',
                               title=pca.getTitle() + ' cross-correlations',
                               **hmargs)

    if outall or kwargs.get('outsf'):
        prody.writeArray(join(outdir, prefix + '_sqfluct' + ext),
                         prody.calcSqFlucts(pca),
                         delimiter=delim,
                         format=format)
    if outall or kwargs.get('outproj'):
        prody.writeArray(join(outdir, prefix + '_proj' + ext),
                         prody.calcProjection(ensemble, pca),
                         delimiter=delim,
                         format=format)

    figall = kwargs.get('figall')
    cc = kwargs.get('figcc')
    sf = kwargs.get('figsf')
    sp = kwargs.get('figproj')

    if figall or cc or sf or sp:
        try:
            import matplotlib.pyplot as plt
        except ImportError:
            LOGGER.warning('Matplotlib could not be imported. '
                           'Figures are not saved.')
        else:
            prody.SETTINGS['auto_show'] = False
            LOGGER.info('Saving graphical output.')
            format = kwargs.get('figformat')
            width = kwargs.get('figwidth')
            height = kwargs.get('figheight')
            dpi = kwargs.get('figdpi')

            format = format.lower()
            if figall or cc:
                plt.figure(figsize=(width, height))
                prody.showCrossCorr(pca)
                plt.savefig(join(outdir, prefix + '_cc.' + format),
                            dpi=dpi,
                            format=format)
                plt.close('all')
            if figall or sf:
                plt.figure(figsize=(width, height))
                prody.showSqFlucts(pca)
                plt.savefig(join(outdir, prefix + '_sf.' + format),
                            dpi=dpi,
                            format=format)
                plt.close('all')
            if figall or sp:
                indices = []
                for item in sp.split():
                    try:
                        if '-' in item:
                            item = item.split('-')
                            if len(item) == 2:
                                indices.append(
                                    list(range(int(item[0]) - 1,
                                               int(item[1]))))
                        elif ',' in item:
                            indices.append(
                                [int(i) - 1 for i in item.split(',')])
                        else:
                            indices.append(int(item) - 1)
                    except:
                        pass
                for index in indices:
                    plt.figure(figsize=(width, height))
                    prody.showProjection(ensemble, pca[index])
                    if isinstance(index, Integral):
                        index = [index]
                    index = [str(i + 1) for i in index]
                    plt.savefig(join(
                        outdir,
                        prefix + '_proj_' + '_'.join(index) + '.' + format),
                                dpi=dpi,
                                format=format)
                    plt.close('all')
Example #9
0
def prody_anm(pdb, **kwargs):
    """Perform ANM calculations for *pdb*.

    """

    for key in DEFAULTS:
        if not key in kwargs:
            kwargs[key] = DEFAULTS[key]

    from os.path import isdir, join
    outdir = kwargs.get('outdir')
    if not isdir(outdir):
        raise IOError('{0} is not a valid path'.format(repr(outdir)))

    import numpy as np
    import prody
    LOGGER = prody.LOGGER

    selstr = kwargs.get('select')
    prefix = kwargs.get('prefix')
    cutoff = kwargs.get('cutoff')
    gamma = kwargs.get('gamma')
    nmodes = kwargs.get('nmodes')
    selstr = kwargs.get('select')
    model = kwargs.get('model')

    pdb = prody.parsePDB(pdb, model=model)
    if prefix == '_anm':
        prefix = pdb.getTitle() + '_anm'

    select = pdb.select(selstr)
    if select is None:
        LOGGER.warn('Selection {0} did not match any atoms.'.format(
            repr(selstr)))
        return
    LOGGER.info('{0} atoms will be used for ANM calculations.'.format(
        len(select)))

    anm = prody.ANM(pdb.getTitle())
    anm.buildHessian(select, cutoff, gamma)
    anm.calcModes(nmodes)
    LOGGER.info('Writing numerical output.')
    if kwargs.get('outnpz'):
        prody.saveModel(anm, join(outdir, prefix))
    prody.writeNMD(join(outdir, prefix + '.nmd'), anm, select)

    extend = kwargs.get('extend')
    if extend:
        if extend == 'all':
            extended = prody.extendModel(anm, select, pdb)
        else:
            extended = prody.extendModel(anm, select, select | pdb.bb)
        prody.writeNMD(join(outdir, prefix + '_extended_' + extend + '.nmd'),
                       *extended)

    outall = kwargs.get('outall')
    delim = kwargs.get('numdelim')
    ext = kwargs.get('numext')
    format = kwargs.get('numformat')

    if outall or kwargs.get('outeig'):
        prody.writeArray(join(outdir, prefix + '_evectors' + ext),
                         anm.getArray(),
                         delimiter=delim,
                         format=format)
        prody.writeArray(join(outdir, prefix + '_evalues' + ext),
                         anm.getEigvals(),
                         delimiter=delim,
                         format=format)

    if outall or kwargs.get('outbeta'):
        from prody.utilities import openFile
        fout = openFile(prefix + '_beta.txt', 'w', folder=outdir)
        fout.write(
            '{0[0]:1s} {0[1]:4s} {0[2]:4s} {0[3]:5s} {0[4]:5s}\n'.format(
                ['C', 'RES', '####', 'Exp.', 'The.']))
        for data in zip(select.getChids(), select.getResnames(),
                        select.getResnums(), select.getBetas(),
                        prody.calcTempFactors(anm, select)):
            fout.write(
                '{0[0]:1s} {0[1]:4s} {0[2]:4d} {0[3]:5.2f} {0[4]:5.2f}\n'.
                format(data))
        fout.close()

    if outall or kwargs.get('outcov'):
        prody.writeArray(join(outdir, prefix + '_covariance' + ext),
                         anm.getCovariance(),
                         delimiter=delim,
                         format=format)

    if outall or kwargs.get('outcc') or kwargs.get('outhm'):
        cc = prody.calcCrossCorr(anm)
        if outall or kwargs.get('outcc'):
            prody.writeArray(join(outdir,
                                  prefix + '_cross-correlations' + ext),
                             cc,
                             delimiter=delim,
                             format=format)
        if outall or kwargs.get('outhm'):
            prody.writeHeatmap(join(outdir, prefix + '_cross-correlations.hm'),
                               cc,
                               resnum=select.getResnums(),
                               xlabel='Residue',
                               ylabel='Residue',
                               title=anm.getTitle() + ' cross-correlations')

    if outall or kwargs.get('hessian'):
        prody.writeArray(join(outdir, prefix + '_hessian' + ext),
                         anm.getHessian(),
                         delimiter=delim,
                         format=format)

    if outall or kwargs.get('kirchhoff'):
        prody.writeArray(join(outdir, prefix + '_kirchhoff' + ext),
                         anm.getKirchhoff(),
                         delimiter=delim,
                         format=format)

    if outall or kwargs.get('outsf'):
        prody.writeArray(join(outdir, prefix + '_sqflucts' + ext),
                         prody.calcSqFlucts(anm),
                         delimiter=delim,
                         format=format)

    figall = kwargs.get('figall')
    cc = kwargs.get('figcc')
    sf = kwargs.get('figsf')
    bf = kwargs.get('figbeta')
    cm = kwargs.get('figcmap')

    if figall or cc or sf or bf or cm:
        try:
            import matplotlib.pyplot as plt
        except ImportError:
            LOGGER.warning('Matplotlib could not be imported. '
                           'Figures are not saved.')
        else:
            prody.SETTINGS['auto_show'] = False
            LOGGER.info('Saving graphical output.')
            format = kwargs.get('figformat')
            width = kwargs.get('figwidth')
            height = kwargs.get('figheight')
            dpi = kwargs.get('figdpi')
            format = format.lower()

            if figall or cc:
                plt.figure(figsize=(width, height))
                prody.showCrossCorr(anm)
                plt.savefig(join(outdir, prefix + '_cc.' + format),
                            dpi=dpi,
                            format=format)
                plt.close('all')

            if figall or cm:
                plt.figure(figsize=(width, height))
                prody.showContactMap(anm)
                plt.savefig(join(outdir, prefix + '_cm.' + format),
                            dpi=dpi,
                            format=format)
                plt.close('all')

            if figall or sf:
                plt.figure(figsize=(width, height))
                prody.showSqFlucts(anm)
                plt.savefig(join(outdir, prefix + '_sf.' + format),
                            dpi=dpi,
                            format=format)
                plt.close('all')

            if figall or bf:
                plt.figure(figsize=(width, height))
                bexp = select.getBetas()
                bcal = prody.calcTempFactors(anm, select)
                plt.plot(bexp, label='Experimental')
                plt.plot(bcal,
                         label=('Theoretical (R={0:.2f})'.format(
                             np.corrcoef(bcal, bexp)[0, 1])))
                plt.legend(prop={'size': 10})
                plt.xlabel('Node index')
                plt.ylabel('Experimental B-factors')
                plt.title(pdb.getTitle() + ' B-factors')
                plt.savefig(join(outdir, prefix + '_bf.' + format),
                            dpi=dpi,
                            format=format)
                plt.close('all')
Example #10
0
def prody_gnm(pdb, **kwargs):
    """Perform GNM calculations for *pdb*.
    
    """

    for key in DEFAULTS:
        if not key in kwargs:
            kwargs[key] = DEFAULTS[key]

    from os.path import isdir, splitext, join

    outdir = kwargs.get("outdir")
    if not isdir(outdir):
        raise IOError("{0} is not a valid path".format(repr(outdir)))

    import numpy as np
    import prody

    LOGGER = prody.LOGGER

    selstr = kwargs.get("select")
    prefix = kwargs.get("prefix")
    cutoff = kwargs.get("cutoff")
    gamma = kwargs.get("gamma")
    nmodes = kwargs.get("nmodes")
    selstr = kwargs.get("select")
    model = kwargs.get("model")

    pdb = prody.parsePDB(pdb, model=model)
    if prefix == "_gnm":
        prefix = pdb.getTitle() + "_gnm"

    select = pdb.select(selstr)
    if select is None:
        raise ValueError("selection {0} do not match any atoms".format(repr(selstr)))
    LOGGER.info("{0} atoms will be used for GNM calculations.".format(len(select)))

    gnm = prody.GNM(pdb.getTitle())
    gnm.buildKirchhoff(select, cutoff, gamma)
    gnm.calcModes(nmodes)

    LOGGER.info("Writing numerical output.")

    if kwargs.get("outnpz"):
        prody.saveModel(gnm, join(outdir, prefix))

    prody.writeNMD(join(outdir, prefix + ".nmd"), gnm, select)

    extend = kwargs.get("extend")
    if extend:
        if extend == "all":
            extended = prody.extendModel(gnm, select, pdb)
        else:
            extended = prody.extendModel(gnm, select, select | pdb.bb)
        prody.writeNMD(join(outdir, prefix + "_extended_" + extend + ".nmd"), *extended)

    outall = kwargs.get("outall")
    delim = kwargs.get("numdelim")
    ext = kwargs.get("numext")
    format = kwargs.get("numformat")

    if outall or kwargs.get("outeig"):
        prody.writeArray(join(outdir, prefix + "_evectors" + ext), gnm.getArray(), delimiter=delim, format=format)
        prody.writeArray(join(outdir, prefix + "_evalues" + ext), gnm.getEigvals(), delimiter=delim, format=format)

    if outall or kwargs.get("outbeta"):
        from prody.utilities import openFile

        fout = openFile(prefix + "_beta.txt", "w", folder=outdir)
        fout.write("{0[0]:1s} {0[1]:4s} {0[2]:4s} {0[3]:5s} {0[4]:5s}\n".format(["C", "RES", "####", "Exp.", "The."]))
        for data in zip(
            select.getChids(),
            select.getResnames(),
            select.getResnums(),
            select.getBetas(),
            prody.calcTempFactors(gnm, select),
        ):
            fout.write("{0[0]:1s} {0[1]:4s} {0[2]:4d} {0[3]:5.2f} {0[4]:5.2f}\n".format(data))
        fout.close()

    if outall or kwargs.get("outcov"):
        prody.writeArray(
            join(outdir, prefix + "_covariance" + ext), gnm.getCovariance(), delimiter=delim, format=format
        )

    if outall or kwargs.get("outcc") or kwargs.get("outhm"):
        cc = prody.calcCrossCorr(gnm)
        if outall or kwargs.get("outcc"):
            prody.writeArray(join(outdir, prefix + "_cross-correlations" + ext), cc, delimiter=delim, format=format)
        if outall or kwargs.get("outhm"):
            prody.writeHeatmap(
                join(outdir, prefix + "_cross-correlations.hm"),
                cc,
                resnum=select.getResnums(),
                xlabel="Residue",
                ylabel="Residue",
                title=gnm.getTitle() + " cross-correlations",
            )

    if outall or kwargs.get("kirchhoff"):
        prody.writeArray(join(outdir, prefix + "_kirchhoff" + ext), gnm.getKirchhoff(), delimiter=delim, format=format)

    if outall or kwargs.get("outsf"):
        prody.writeArray(
            join(outdir, prefix + "_sqfluct" + ext), prody.calcSqFlucts(gnm), delimiter=delim, format=format
        )

    figall = kwargs.get("figall")
    cc = kwargs.get("figcc")
    sf = kwargs.get("figsf")
    bf = kwargs.get("figbeta")
    cm = kwargs.get("figcmap")
    modes = kwargs.get("figmode")

    if figall or cc or sf or bf or cm or modes:
        try:
            import matplotlib.pyplot as plt
        except ImportError:
            LOGGER.warning("Matplotlib could not be imported. " "Figures are not saved.")
        else:
            prody.SETTINGS["auto_show"] = False
            LOGGER.info("Saving graphical output.")
            format = kwargs.get("figformat")
            width = kwargs.get("figwidth")
            height = kwargs.get("figheight")
            dpi = kwargs.get("figdpi")
            format = format.lower()

            if figall or cc:
                plt.figure(figsize=(width, height))
                prody.showCrossCorr(gnm)
                plt.savefig(join(outdir, prefix + "_cc." + format), dpi=dpi, format=format)
                plt.close("all")

            if figall or cm:
                plt.figure(figsize=(width, height))
                prody.showContactMap(gnm)
                plt.savefig(join(outdir, prefix + "_cm." + format), dpi=dpi, format=format)
                plt.close("all")

            if figall or sf:
                plt.figure(figsize=(width, height))
                prody.showSqFlucts(gnm)
                plt.savefig(join(outdir, prefix + "_sf." + format), dpi=dpi, format=format)
                plt.close("all")

            if figall or bf:
                plt.figure(figsize=(width, height))
                bexp = select.getBetas()
                bcal = prody.calcTempFactors(gnm, select)
                plt.plot(bexp, label="Experimental")
                plt.plot(bcal, label=("Theoretical (corr coef = {0:.2f})".format(np.corrcoef(bcal, bexp)[0, 1])))
                plt.legend(prop={"size": 10})
                plt.xlabel("Node index")
                plt.ylabel("Experimental B-factors")
                plt.title(pdb.getTitle() + " B-factors")
                plt.savefig(join(outdir, prefix + "_bf." + format), dpi=dpi, format=format)
                plt.close("all")

            if modes:
                indices = []
                items = modes.split()
                items = sum([item.split(",") for item in items], [])
                for item in items:
                    try:
                        item = item.split("-")
                        if len(item) == 1:
                            indices.append(int(item[0]) - 1)
                        elif len(item) == 2:
                            indices.extend(range(int(item[0]) - 1, int(item[1])))
                    except:
                        pass
                for index in indices:
                    try:
                        mode = gnm[index]
                    except:
                        pass
                    else:
                        plt.figure(figsize=(width, height))
                        prody.showMode(mode)
                        plt.grid()
                        plt.savefig(
                            join(outdir, prefix + "_mode_" + str(mode.getIndex() + 1) + "." + format),
                            dpi=dpi,
                            format=format,
                        )
                        plt.close("all")
Example #11
0
    def prody_anm(self, variables, txtOutput):
        '''
        PRODY DRIVER is the function to read in variables from GUI input and
        used to run a prody normal mode calculation using the anisotropic network model
        (ANM) on a structure provide in a pdb file.

        INPUT:  variable descriptions:

        pdbfile:          input pdb file (reference)

        OUTPUT:
                        model_anm_extended_bb.nmd
                        model_traverse.dcd
                        model_samp.dcd
                        model_samp.pdb
                        model_anm_sqflucts.txt
                        model_anm_kirchhoff.txt
                        model_anm_hessian.txt
                        model_anm_cross-correlations.hm
                        model_anm_cross-correlations.txt
                        model_anm_covariance.txt
                        model_anm_beta.txt
                        model_anm_evalues.txt
                        model_anm_evectors.txt
                        model_anm_extended_all.nmd
                        model_anm.nmd

        txtOutput:        TK handler for output to GUI textbox

        files stored in ~/runname/prody directory:
        outfile:          output filename

        '''
        log = self.log
        pgui = self.run_utils.print_gui

        # start gui output
        pgui("\n%s \n" % ('=' * 60))
        pgui("DATA FROM RUN: %s \n\n" % time.asctime( time.gmtime( time.time() ) ))

        mvars = self.mvars
        #path = os.path.join(os.getcwd(),mvars.runname, 'prody')
        path = os.path.join(mvars.runname, 'prody')
        direxist = os.path.exists(path)
        if(direxist == 0):
            try:
                result = os.system('mkdir -p ' + path)
            except:
                message = 'can not create project directory: ' + path
                message += '\nstopping here\n'
                print_failure(message, txtOutput)
            if(result != 0):
                message = 'can not create project directory: ' + path
                message += '\nstopping here\n'
                print_failure(message, txtOutput)
        if mvars.advanced_usage == 1:
            run_cmd = prody_exe + mvars.advanced_usage_cmd
            os.system(run_cmd)
            run_cmd = 'mv *.nmd *.txt *.hm prody'
            os.system(run_cmd)
            exit()

        # display progress
        fraction_done = (0 + 1) * 1.0 / 10.0
        report_string = 'STATUS\t%f' % fraction_done
        pgui(report_string)

        prody.confProDy(verbosity='none')  #option to set silent verbosity
        model = mvars.pdbfile[0:len(mvars.pdbfile) - 4]
        run_cmd = prody_exe + ' anm ' + \
            mvars.pdbfile + ' -t all -n ' + str(mvars.number_modes) + ' -a'
        log.info('staring prody_exe %s' % run_cmd)
        prody_run = subprocess.Popen(run_cmd,shell=True,executable='/bin/bash')
        prody_run.wait() 
        #prody.confProDy(verbosity='none')  #option to set silent verbosity
        file_anm = model + '_anm_extended_all.nmd'

        # display progress
        fraction_done = (1 + 1) * 1.0 / 10.0
        report_string = 'STATUS\t%f' % fraction_done
        pgui(report_string)

        # parse nmd file with resuts extended to all atoms
        log.info('staring prody.parseNMD %s' % file_anm)
        mod, ag = prody.parseNMD(file_anm, type=None)
        allatoms = ag.copy()
        # set up to randomly sample number_conformations_samp modes
        log.info('staring prody.sampleModes')
        ensemble = prody.sampleModes(mod[:mvars.number_modes],
                                     ag,
                                     n_confs=mvars.number_conformations_samp,
                                     rmsd=mvars.rmsd_conformations_samp)
        ensemble
        log.info('staring prody ensemble and writing pdb/dcd files')
        allatoms.addCoordset(ensemble)
        prody.writePDB('model_samp.pdb', allatoms)
        prody.writeDCD('model_samp.dcd', allatoms)
        trajectory_names = []
        
        # display progress
        fraction_done = (1 + 2) * 1.0 / 10.0
        report_string = 'STATUS\t%f' % fraction_done
        pgui(report_string)

        log.info('starting prody traverse')
        for i in xrange(0, mvars.number_modes):
            #print i
            # setup to tranverse slowest mode
            traverse = prody.traverseMode(
                mod[i],
                allatoms,
                n_steps=mvars.number_steps_traverse,
                rmsd=mvars.rmsd_traverse)
            traverse
            prody.writeDCD('traverse.dcd', traverse)
            this_dcd = str(os.path.join(path, 'traverse_' + str(i) + '.dcd'))
            cmd = 'mv traverse.dcd ' + this_dcd
            os.system(cmd)
            trajectory_names.append(this_dcd)

        # display progress
        fraction_done = (1 + 7) * 1.0 / 10.0
        report_string = 'STATUS\t%f' % fraction_done
        pgui(report_string)

        m1 = sasmol.SasMol(0)
        m2 = sasmol.SasMol(0)
        m1.read_pdb(mvars.pdbfile)
        m2.read_pdb(mvars.pdbfile,fastread=True)

        mvars.dcdfile = mvars.runname + '.dcd'
        log.info('opening new dcd file to store trajectory: %s' %
                 os.path.join(self.runpath, mvars.dcdfile))

        outfile_name = str(os.path.join(path, mvars.dcdfile))
        dcdoutfile = m2.open_dcd_write(outfile_name)
        count = 0
        coor = numpy.zeros((1,m2.natoms(),3),numpy.float32)
        for this_trajectory_name in trajectory_names:

            dcdfile = m1.open_dcd_read(this_trajectory_name)
            number_of_frames = dcdfile[2]

            for j in xrange(number_of_frames):
                m1.read_dcd_step(dcdfile,j)
                coor[0,:,:] = m1.coor()[0]
                m2.setCoor(coor)
                m2.write_dcd_step(dcdoutfile,0, count + 1)
                count += 1

        m2.close_dcd_write(dcdoutfile)

        log.info('moving files to runname / prody')

        file_anm = model + '_anm.nmd'
        mod, ag = prody.parseNMD(file_anm, type=None)
        mod1 = prody.parsePDB(mvars.pdbfile)
        calphas = mod1.select('calpha')
        bb_anm, bb_atoms = prody.extendModel(mod, calphas, mod1.select(
            'backbone'))  # extend model to backbone atoms
        prody.writeNMD('model_anm_extended_bb.nmd', bb_anm, bb_atoms)

        cmd = 'mv model_samp.pdb ' + path + os.sep + os.path.basename(model) + '_samp.pdb'
        os.system(cmd)

        cmd = 'mv model_samp.dcd ' + path + os.sep + os.path.basename(model) + '_samp.dcd'
        os.system(cmd)

        cmd = 'mv model_anm_extended_bb.nmd ' + \
            model + '_anm_extended_bb.nmd'
        os.system(cmd)

        cmd = 'mv *.hm *.nmd *.txt ' + path + os.sep
        os.system(cmd)
        
        # display progress
        fraction_done = (1 + 9) * 1.0 / 10.0
        report_string = 'STATUS\t%f' % fraction_done
        pgui(report_string)

        return
Example #12
0
def prody_gnm(pdb, **kwargs):
    """Perform GNM calculations for *pdb*.

    """

    for key in DEFAULTS:
        if not key in kwargs:
            kwargs[key] = DEFAULTS[key]

    from os.path import isdir, splitext, join
    outdir = kwargs.get('outdir')
    if not isdir(outdir):
        raise IOError('{0} is not a valid path'.format(repr(outdir)))

    import numpy as np
    import prody
    LOGGER = prody.LOGGER

    selstr = kwargs.get('select')
    prefix = kwargs.get('prefix')
    cutoff = kwargs.get('cutoff')
    gamma = kwargs.get('gamma')
    nmodes = kwargs.get('nmodes')
    selstr = kwargs.get('select')
    model = kwargs.get('model')
    altloc = kwargs.get('altloc')
    zeros = kwargs.get('zeros')

    pdb = prody.parsePDB(pdb, model=model, altloc=altloc)
    if prefix == '_gnm':
        prefix = pdb.getTitle() + '_gnm'

    select = pdb.select(selstr)
    if select is None:
        raise ValueError('selection {0} do not match any atoms'.format(
            repr(selstr)))
    LOGGER.info('{0} atoms will be used for GNM calculations.'.format(
        len(select)))

    gnm = prody.GNM(pdb.getTitle())

    nproc = kwargs.get('nproc')
    if nproc:
        try:
            from threadpoolctl import threadpool_limits
        except ImportError:
            raise ImportError(
                'Please install threadpoolctl to control threads')

        with threadpool_limits(limits=nproc, user_api="blas"):
            gnm.buildKirchhoff(select, cutoff, gamma)
            gnm.calcModes(nmodes, zeros=zeros)
    else:
        gnm.buildKirchhoff(select, cutoff, gamma)
        gnm.calcModes(nmodes, zeros=zeros)

    LOGGER.info('Writing numerical output.')

    if kwargs.get('outnpz'):
        prody.saveModel(gnm, join(outdir, prefix))

    if kwargs.get('outscipion'):
        prody.writeScipionModes(outdir, gnm)

    prody.writeNMD(join(outdir, prefix + '.nmd'), gnm, select)

    extend = kwargs.get('extend')
    if extend:
        if extend == 'all':
            extended = prody.extendModel(gnm, select, pdb)
        else:
            extended = prody.extendModel(gnm, select, select | pdb.bb)
        prody.writeNMD(join(outdir, prefix + '_extended_' + extend + '.nmd'),
                       *extended)

    outall = kwargs.get('outall')
    delim = kwargs.get('numdelim')
    ext = kwargs.get('numext')
    format = kwargs.get('numformat')

    if outall or kwargs.get('outeig'):
        prody.writeArray(join(outdir, prefix + '_evectors' + ext),
                         gnm.getArray(),
                         delimiter=delim,
                         format=format)
        prody.writeArray(join(outdir, prefix + '_evalues' + ext),
                         gnm.getEigvals(),
                         delimiter=delim,
                         format=format)

    if outall or kwargs.get('outbeta'):
        from prody.utilities import openFile
        fout = openFile(prefix + '_beta' + ext, 'w', folder=outdir)
        fout.write(
            '{0[0]:1s} {0[1]:4s} {0[2]:4s} {0[3]:5s} {0[4]:5s}\n'.format(
                ['C', 'RES', '####', 'Exp.', 'The.']))
        for data in zip(select.getChids(), select.getResnames(),
                        select.getResnums(), select.getBetas(),
                        prody.calcTempFactors(gnm, select)):
            fout.write(
                '{0[0]:1s} {0[1]:4s} {0[2]:4d} {0[3]:5.2f} {0[4]:5.2f}\n'.
                format(data))
        fout.close()

    if outall or kwargs.get('outcov'):
        prody.writeArray(join(outdir, prefix + '_covariance' + ext),
                         gnm.getCovariance(),
                         delimiter=delim,
                         format=format)

    if outall or kwargs.get('outcc') or kwargs.get('outhm'):
        cc = prody.calcCrossCorr(gnm)
        if outall or kwargs.get('outcc'):
            prody.writeArray(join(outdir,
                                  prefix + '_cross-correlations' + ext),
                             cc,
                             delimiter=delim,
                             format=format)
        if outall or kwargs.get('outhm'):
            prody.writeHeatmap(join(outdir, prefix + '_cross-correlations.hm'),
                               cc,
                               resnum=select.getResnums(),
                               xlabel='Residue',
                               ylabel='Residue',
                               title=gnm.getTitle() + ' cross-correlations')

    if outall or kwargs.get('kirchhoff'):
        prody.writeArray(join(outdir, prefix + '_kirchhoff' + ext),
                         gnm.getKirchhoff(),
                         delimiter=delim,
                         format=format)

    if outall or kwargs.get('outsf'):
        prody.writeArray(join(outdir, prefix + '_sqfluct' + ext),
                         prody.calcSqFlucts(gnm),
                         delimiter=delim,
                         format=format)

    figall = kwargs.get('figall')
    cc = kwargs.get('figcc')
    sf = kwargs.get('figsf')
    bf = kwargs.get('figbeta')
    cm = kwargs.get('figcmap')
    modes = kwargs.get('figmode')

    if figall or cc or sf or bf or cm or modes:
        try:
            import matplotlib.pyplot as plt
        except ImportError:
            LOGGER.warning('Matplotlib could not be imported. '
                           'Figures are not saved.')
        else:
            prody.SETTINGS['auto_show'] = False
            LOGGER.info('Saving graphical output.')
            format = kwargs.get('figformat')
            width = kwargs.get('figwidth')
            height = kwargs.get('figheight')
            dpi = kwargs.get('figdpi')
            format = format.lower()

            if figall or cc:
                plt.figure(figsize=(width, height))
                prody.showCrossCorr(gnm)
                plt.savefig(join(outdir, prefix + '_cc.' + format),
                            dpi=dpi,
                            format=format)
                plt.close('all')

            if figall or cm:
                plt.figure(figsize=(width, height))
                prody.showContactMap(gnm)
                plt.savefig(join(outdir, prefix + '_cm.' + format),
                            dpi=dpi,
                            format=format)
                plt.close('all')

            if figall or sf:
                plt.figure(figsize=(width, height))
                prody.showSqFlucts(gnm)
                plt.savefig(join(outdir, prefix + '_sf.' + format),
                            dpi=dpi,
                            format=format)
                plt.close('all')

            if figall or bf:
                plt.figure(figsize=(width, height))
                bexp = select.getBetas()
                bcal = prody.calcTempFactors(gnm, select)
                plt.plot(bexp, label='Experimental')
                plt.plot(bcal,
                         label=('Theoretical (corr coef = {0:.2f})'.format(
                             np.corrcoef(bcal, bexp)[0, 1])))
                plt.legend(prop={'size': 10})
                plt.xlabel('Node index')
                plt.ylabel('Experimental B-factors')
                plt.title(pdb.getTitle() + ' B-factors')
                plt.savefig(join(outdir, prefix + '_bf.' + format),
                            dpi=dpi,
                            format=format)
                plt.close('all')

            if modes:
                indices = []
                items = modes.split()
                items = sum([item.split(',') for item in items], [])
                for item in items:
                    try:
                        item = item.split('-')
                        if len(item) == 1:
                            indices.append(int(item[0]) - 1)
                        elif len(item) == 2:
                            indices.extend(
                                list(range(int(item[0]) - 1, int(item[1]))))
                    except:
                        pass
                for index in indices:
                    try:
                        mode = gnm[index]
                    except:
                        pass
                    else:
                        plt.figure(figsize=(width, height))
                        prody.showMode(mode)
                        plt.grid()
                        plt.savefig(join(
                            outdir, prefix + '_mode_' +
                            str(mode.getIndex() + 1) + '.' + format),
                                    dpi=dpi,
                                    format=format)
                        plt.close('all')
Example #13
0
def calculate_vibrations(molecule,
                         max_modes=20,
                         algorithm='calpha',
                         queue=None,
                         mass_weighted=False,
                         cutoff=15.0,
                         gamma=1.0,
                         **options):
    """
    Parameters
    ----------
    molecule : prody.AtomGroup
    nax_modes : int
        number of modes to calculate
    algorithm : callable, optional, default=None
        coarseGrain(prm) which make molecule.select().setBetas(i) where i
        is the index Coarse Grain group
        and prm is prody AtomGroup
    options : dict, optional
        Parameters for algorithm callable

    Returns
    -------
    modes : ProDy modes ANM or RTB
    """
    if queue is None:
        queue = Queue()
    modes = None
    hessian_kwargs = dict(cutoff=cutoff, gamma=gamma)
    if algorithm in ('residues', 'mass', 'graph'):
        queue.put('Building model...')
        title = 'normal modes for {}'.format(molecule.getTitle())
        molecule = GROUPERS[algorithm](molecule, **options)
        modes = prody.RTB(title)
        queue.put('Building hessian...')
        modes.buildHessian(molecule.getCoords(), molecule.getBetas(),
                           **hessian_kwargs)
        if mass_weighted:
            queue.put('Mass weighting...')
            _mass_weighted_hessian(molecule, modes)
        queue.put('Calculating {} modes...'.format(max_modes))
        modes.calcModes(n_modes=max_modes)
    elif algorithm == 'calpha':
        queue.put('Building model...')
        calphas_modes = prody.ANM('normal modes for {}'.format(
            molecule.getTitle()))
        calphas = molecule = molecule.select(algorithm)
        queue.put('Building hessian...')
        calphas_modes.buildHessian(calphas, **hessian_kwargs)
        if mass_weighted:
            queue.put('Mass weighting...')
            _mass_weighted_hessian(molecule, modes)
        queue.put('Calculating {} modes...'.format(max_modes))
        calphas_modes.calcModes(n_modes=max_modes)
        queue.put('Extending model...')
        modes = prody.extendModel(calphas_modes, calphas, molecule,
                                  norm=True)[0]
    else:
        queue.put('Building model...')
        modes = prody.ANM('normal modes for {}'.format(molecule.getTitle()))
        queue.put('Building hessian...')
        modes.buildHessian(molecule, **hessian_kwargs)
        if mass_weighted:
            queue.put('Mass weighting...')
            _mass_weighted_hessian(molecule, modes)
        queue.put('Calculating {} modes...'.format(max_modes))
        modes.calcModes(n_modes=max_modes)
    print(modes)
    return modes