Exemplo n.º 1
0
def test_ired_simple_for_coverage():
    '''
    '''
    traj = pt.iterload(traj_dir, parm_dir)
    h_indices = pt.select_atoms('@H', traj.top)
    n_indices = pt.select_atoms('@H', traj.top) - 1
    nh_indices = list(zip(n_indices, h_indices))
    vecs_and_mat = pt.ired_vector_and_matrix(traj, nh_indices, dtype='tuple')
    vecs_and_mat = pt.ired_vector_and_matrix(traj, nh_indices, dtype='tuple')
    state_vecs = vecs_and_mat[0]
    mat_ired = vecs_and_mat[1]

    # get eigenvalues and eigenvectors
    modes = pt.matrix.diagonalize(mat_ired, n_vecs=len(state_vecs))
    evals, evecs = modes

    data_0 = _ired(
        state_vecs, modes=(evals, evecs), NHbond=True, tcorr=10000, tstep=1.)

    data_1 = _ired(state_vecs, modes=modes, NHbond=True, tcorr=10000, tstep=1)

    for d0, d1 in zip(data_0, data_1):
        if d0.dtype not in [
                'modes',
        ]:
            aa_eq(d0.values, d1.values)
        else:
            # modes
            # values: tuple
            aa_eq(d0.values[0], d1.values[0])
            aa_eq(d0.values[1], d1.values[1])

    # try different dtype
    out_try_new_dtype = pt.ired_vector_and_matrix(
        traj, nh_indices, dtype='cpptraj_dataset')
Exemplo n.º 2
0
    def test_ired_vector_and_matrix_pmap(self):
        traj = pt.iterload(fn('tz2.nc'), fn('tz2.parm7'))
        h = traj.top.select('@H')
        n = h - 1
        nh = list(zip(n, h))

        exptected_vecs, exptected_mat = pt.ired_vector_and_matrix(traj, nh)
        for n_cores in [2, 3]:
            vecs, mat = pt.pmap(
                pt.ired_vector_and_matrix, traj, nh, n_cores=n_cores)
            aa_eq(exptected_vecs, vecs, decimal=7)
            aa_eq(exptected_mat, mat, decimal=7)
Exemplo n.º 3
0
    def test_simple_for_coverage(self):
        '''
        '''
        traj = pt.iterload(traj_dir, parm_dir)
        h_indices = pt.select_atoms('@H', traj.top)
        n_indices = pt.select_atoms('@H', traj.top) - 1
        nh_indices = list(zip(n_indices, h_indices))
        vecs_and_mat = pt.ired_vector_and_matrix(traj, nh_indices, dtype='tuple')
        vecs_and_mat = pt.ired_vector_and_matrix(traj, nh_indices, dtype='tuple')
        state_vecs = vecs_and_mat[0]
        mat_ired = vecs_and_mat[1]

        # get eigenvalues and eigenvectors
        modes = pt.matrix.diagonalize(mat_ired, n_vecs=len(state_vecs))
        evals, evecs = modes

        data_0 = _ired(state_vecs,
                       modes=(evals, evecs),
                       NHbond=True,
                       tcorr=10000,
                       tstep=1.)

        data_1 = _ired(state_vecs,
                       modes=modes,
                       NHbond=True,
                       tcorr=10000,
                       tstep=1)

        for d0, d1 in zip(data_0, data_1):
            if d0.dtype not in ['modes', ]:
                aa_eq(d0.values, d1.values)
            else:
                # modes
                # values: tuple
                aa_eq(d0.values[0], d1.values[0])
                aa_eq(d0.values[1], d1.values[1])

        # try different dtype
        out_try_new_dtype = pt.ired_vector_and_matrix(traj, nh_indices, dtype='cpptraj_dataset')
Exemplo n.º 4
0
    def test_ired_vector_and_matrix_pmap(self):
        traj = pt.iterload("data/tz2.nc", "data/tz2.parm7")
        h = traj.top.select('@H')
        n = h - 1
        nh = list(zip(n, h))

        exptected_vecs, exptected_mat = pt.ired_vector_and_matrix(traj, nh)
        for n_cores in [2, 3]:
            vecs, mat = pt.pmap(pt.ired_vector_and_matrix,
                                traj,
                                nh,
                                n_cores=n_cores)
            aa_eq(exptected_vecs, vecs, decimal=7)
            aa_eq(exptected_mat, mat, decimal=7)
Exemplo n.º 5
0
    def test_ired_need_lapack_cpptraj(self):
        state = pt.load_cpptraj_state(txt)
        state.run()
        xyz = state.data['CRD1'].xyz
        top = state.data['CRD1'].top
        traj = pt.Trajectory(xyz=xyz, top=top)
        state_vecs = state.data[1:-3].values

        h_indices = pt.select_atoms('@H', traj.top)
        n_indices = pt.select_atoms('@H', traj.top) - 1
        nh_indices = list(zip(n_indices, h_indices))
        mat_ired = pt.ired_vector_and_matrix(traj,
                                             mask=nh_indices,
                                             order=2)[-1]
        mat_ired /= mat_ired[0, 0]

        # matired: make sure to reproduce cpptraj output
        aa_eq(mat_ired, state.data['matired'].values)

        # get modes
        modes = state.data[-2]
        cpp_eigenvalues = modes.eigenvalues
        cpp_eigenvectors = modes.eigenvectors
        evals, evecs = np.linalg.eigh(mat_ired)

        # need to sort a bit
        evals = evals[::-1]
        # cpptraj's eigvenvalues
        aa_eq(evals, cpp_eigenvalues)

        # cpptraj's eigvenvectors
        # use absolute values to avoid flipped sign
        # from Dan Roe
        # In practice, the "sign" of an eigenvector depends on the math library used to calculate it.
        # This is in fact why the modes command displacement test is disabled for cpptraj.
        # I bet if you use a different math library (e.g. use your system BLAS/LAPACK instead of the one bundled with Amber
        # or vice versa) you will get different signs.
        # Bottom line is that eigenvector sign doesn't matter.

        aa_eq(np.abs(evecs[:, ::-1].T), np.abs(cpp_eigenvectors), decimal=4)
        data = _ired(state_vecs, modes=(cpp_eigenvalues, cpp_eigenvectors))
        order_s2 = data['IRED_00127[S2]']

        # load cpptraj's output and compare to pytraj' values for S2 order paramters
        cpp_order_s2 = np.loadtxt(os.path.join(cpptraj_test_dir, 'Test_IRED',
                                               'orderparam.save')).T[-1]
        aa_eq(order_s2, cpp_order_s2, decimal=5)
Exemplo n.º 6
0
def NH_order_paramters(traj, vector_pairs, order=2, tstep=1., tcorr=10000.):
    '''the results seem ok.
    '''

    # compute N-H vectors and ired matrix
    vecs_and_mat = pt.ired_vector_and_matrix(traj, vector_pairs, order=order)
    state_vecs = vecs_and_mat[:-1].values
    mat_ired = vecs_and_mat[-1]
    mat_ired /= mat_ired[0, 0]

    # get eigenvalues and eigenvectors
    modes = pt.matrix.diagonalize(mat_ired, n_vecs=len(state_vecs))[0]
    evals, evecs = modes.eigenvalues, modes.eigenvectors

    data = pt._ired(state_vecs, modes=(evals, evecs), tcorr=tcorr, tstep=tstep)
    order = [d.values.copy() for d in data if 'S2' in d.key][0]
    return (order)
Exemplo n.º 7
0
    def test_ired_lapack_in_numpy(self):
        parmfile = parm_dir
        trajfile = traj_dir

        # load to TrajectoryIterator
        traj = pt.iterload(trajfile, parmfile)

        # create N-H vectors
        h_indices = pt.select_atoms('@H', traj.top)
        n_indices = pt.select_atoms('@H', traj.top) - 1
        nh_indices = list(zip(n_indices, h_indices))

        # compute N-H vectors and ired matrix
        vecs_and_mat = pt.ired_vector_and_matrix(traj,
                                                 mask=nh_indices,
                                                 order=2)
        state_vecs = vecs_and_mat[:-1].values
        mat_ired = vecs_and_mat[-1]
        mat_ired /= mat_ired[0, 0]

        # cpptraj
        data_cpp = pt.matrix.diagonalize(mat_ired, n_vecs=len(state_vecs))[0]
        print(data_cpp.eigenvectors)

        # numpy
        data_np = pt.matrix._diag_np(mat_ired, n_vecs=len(state_vecs))

        def order_(modes):
            data = _ired(state_vecs, modes=modes)
            order_s2_v0 = data['IRED_00127[S2]']
            # make sure the S2 values is 1st array

            # load cpptraj's output and compare to pytraj' values for S2 order paramters
            cpp_order_s2 = np.loadtxt(os.path.join(
                cpptraj_test_dir, 'Test_IRED', 'orderparam.save')).T[-1]
            aa_eq(order_s2_v0.values, cpp_order_s2, decimal=4)

        order_(data_cpp.values)

        def plot_(x, y):
            import seaborn as sb
            sb.heatmap(x - y)
            pt.show()

        print((data_cpp.values[1] - data_np[1]).shape)
Exemplo n.º 8
0
    def test_ired_vector(self):
        '''test mask as a list of strings or as a 2D array of integers
        '''
        parm_dir = os.path.join(cpptraj_test_dir, 'Test_IRED',
                                '1IEE_A_prot.prmtop')
        trajin_dir = os.path.join(cpptraj_test_dir, 'Test_IRED',
                                  '1IEE_A_test.mdcrd')
        traj = pt.iterload(trajin_dir, parm_dir)

        # get a list of mask from cpptraj input
        maskes = []
        lines = None

        n_indices_cpp = []

        with open('data/ired.in', 'r') as fh:
            lines = fh.readlines()
            for line in lines:
                if 'vector' in line and 'ired' in line:
                    # example: vector v100 @1541 ired @1542
                    sline = line.split()
                    mask = ' '.join((sline[2], sline[4]))
                    n_indices_cpp.append(int(sline[2][1:]) - 1)
                    maskes.append(mask)

        h_indices_cpp = [i + 1 for i in n_indices_cpp]

        # calcuate vector from a list of strings
        data_vec = va.vector_mask(traj, maskes)

        # calcuate vector from a 2d array of integers
        nh_indices = np.array(list(zip(n_indices_cpp, h_indices_cpp)))
        data_vec_2 = va.vector_mask(traj, nh_indices)

        # re-create cpptraj input to run cpptraj
        txt = ''.join(lines)
        # add parm and trajin lines
        txt = 'parm ' + parm_dir + '\n' + \
              'trajin ' + trajin_dir + '\n' + \
              txt

        state = pt.datafiles.load_cpptraj_output(txt, dtype='state')
        state.run()
        cpp_data = state.datasetlist
        cpp_vectors = cpp_data.grep('vector', mode='dtype').values
        cpp_matired = cpp_data.grep('matrix', mode='dtype')['matired']

        # assert between pytraj's data_vec and cpptraj's cpp_vectors
        aa_eq(data_vec, cpp_vectors)

        # from a 2D array of integers
        aa_eq(data_vec_2, cpp_vectors)

        # test ired vector with ired matrix
        # open file

        with open('data/ired_reduced.in', 'r') as fh:
            text = ''.join(fh.readlines())
        state2 = pt.load_batch(traj, text)
        state2.run()

        data = pt.ired_vector_and_matrix(traj, nh_indices, order=2)
        data_vec_3 = data[0]
        assert len(data_vec_3) == 126, 'must have 126 vectors'
        matired = data[1]
        # TODO: know why??
        matired /= matired[0, 0]
        aa_eq(data_vec_3, cpp_vectors)
        assert pt.tools.rmsd(matired.flatten(),
                             cpp_matired.values) < 1E-6, 'matired'
 def normal_(traj=traj, nh=nh):
     print('serial')
     return pt.ired_vector_and_matrix(traj, nh)
Exemplo n.º 10
0
    def test_ired_vector(self):
        '''test mask as a list of strings or as a 2D array of integers
        '''
        parm_dir = os.path.join(cpptraj_test_dir, 'Test_IRED',
                                '1IEE_A_prot.prmtop')
        trajin_dir = os.path.join(cpptraj_test_dir, 'Test_IRED',
                                  '1IEE_A_test.mdcrd')
        traj = pt.iterload(trajin_dir, parm_dir)

        # get a list of mask from cpptraj input
        maskes = []
        lines = None

        n_indices_cpp = []

        with open('data/ired.in', 'r') as fh:
            lines = fh.readlines()
            for line in lines:
                if 'vector' in line and 'ired' in line:
                    # example: vector v100 @1541 ired @1542
                    sline = line.split()
                    mask = ' '.join((sline[2], sline[4]))
                    n_indices_cpp.append(int(sline[2][1:]) - 1)
                    maskes.append(mask)

        h_indices_cpp = [i + 1 for i in n_indices_cpp]

        # calcuate vector from a list of strings
        data_vec = va.vector_mask(traj, maskes)

        # calcuate vector from a 2d array of integers
        nh_indices = np.array(list(zip(n_indices_cpp, h_indices_cpp)))
        data_vec_2 = va.vector_mask(traj, nh_indices)

        # re-create cpptraj input to run cpptraj
        txt = ''.join(lines)
        # add parm and trajin lines
        txt = 'parm ' + parm_dir + '\n' + \
              'trajin ' + trajin_dir + '\n' + \
              txt

        state = pt.datafiles.load_cpptraj_output(txt, dtype='state')
        state.run()
        cpp_data = state.datasetlist
        cpp_vectors = cpp_data.grep('vector', mode='dtype').values
        cpp_matired = cpp_data.grep('matrix', mode='dtype')['matired']

        # assert between pytraj's data_vec and cpptraj's cpp_vectors
        aa_eq(data_vec, cpp_vectors)

        # from a 2D array of integers
        aa_eq(data_vec_2, cpp_vectors)

        # test ired vector with ired matrix
        # open file

        with open('data/ired_reduced.in', 'r') as fh:
            text = ''.join(fh.readlines())
        state2 = pt.load_batch(traj, text)
        state2.run()

        data = pt.ired_vector_and_matrix(traj, nh_indices, order=2)
        data_vec_3 = data[0]
        assert len(data_vec_3) == 126, 'must have 126 vectors'
        matired = data[1]
        # TODO: know why??
        matired /= matired[0, 0]
        aa_eq(data_vec_3, cpp_vectors)
        assert pt.tools.rmsd(matired.flatten(),
                             cpp_matired.values) < 1E-6, 'matired'