tooba_gmx.py 6.75 KB
Newer Older
1 2 3 4 5 6
import os
import subprocess
import numpy as np
from scipy.signal import find_peaks
from scipy.signal import savgol_filter
import hashlib
7 8
from progress.bar import Bar

9 10 11 12 13 14 15 16 17 18 19 20 21
#cmd="gmx covar -s test/eq_run.tpr -f test/eq_traj.trr -n test/gmx_ndx/test_\(1\,\ 0\,\ 3\).ndx"
#os.system(cmd)

#cmd="gmx anaeig -v test/eigenvec.trr -s test/eq_run.tpr -f test/eq_traj.trr -n test/gmx_ndx/test_\(1\,\ 0\,\ 3\).ndx -proj -first 1 -last 20"
#os.system(cmd)

#cmd="gmx_clusterByFeatures cluster -s test/eq_run.tpr -f test/eq_traj.trr -n test/gmx_ndx/test_\(1\,\ 0\,\ 3\).ndx \
#                                             -feat proj.xvg -method kmeans -nfeature 20 -cmetric ssr-sst -ncluster 5 \
#                                             -fit2central -sort features -cpdb test/central.pdb \
#                                             -fout test/cluster.xtc -plot test/pca_cluster.png"


#os.system(cmd)
22
def ndx_index(SYSTEM_NAME, pth):
23
    uniq_id={}
24
    path='./'+SYSTEM_NAME+'/'+pth+'/'
25
    for f in os.listdir(path):
26 27 28 29
        dom=f.split('(')[1].split(')')[0]
        dom='('+dom+')'
        #dom=f.split('_')[2].split('.')[0]
        text=SYSTEM_NAME+'_'+dom
30
        iidd=hashlib.md5(text.encode('utf-8')).hexdigest()
31
        #dom=f.split('_')[2].split('.')[0]
32
        uniq_id[iidd]={}
33
        uniq_id[iidd]={'system':SYSTEM_NAME,'fld':pth,'ndx_file':f,'domain':dom}
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    return uniq_id

def read_xvg(XVG):
    x=[]
    y=[]
    with open(XVG) as f:
        for line in f:
            if line.startswith(("#","@")):
                continue
            else:
                cols = line.split()
                x.append(cols[0])
                y.append(cols[1])
    x = np.asarray(x)
    y = np.asarray(y)
    return x,y

51
def density_peaks(TRR,TPR,IND,SLC,ST,EN,normal,fld,arg,dist_pk=1):
52 53 54 55 56 57 58 59 60 61

    cmd=str(arg)+'\n'+str(arg)+'\n'
    if EN==-1:
        p = subprocess.Popen(['gmx', 'density', '-symm', '-b', str(ST), '-sl',str(SLC), '-d', normal,
                            '-f', TRR,
                            '-s',TPR,
                            '-n',IND,
                            '-o', fld+'/tmp.xvg'],
                        stdin=subprocess.PIPE)
    else:
62
        p = subprocess.Popen(['gmx', 'density', '-symm', '-b', str(ST), '-e', str(EN), '-sl',str(SLC), '-d', normal,
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
                            '-f', TRR,
                            '-s',TPR,
                            '-n',IND,
                            '-o', fld+'/tmp.xvg'],
                        stdin=subprocess.PIPE)
    p.communicate(cmd.encode('UTF-8'))
    p.wait()

    x,y=read_xvg(XVG=fld+'/tmp.xvg')
    yhat = savgol_filter(y, 15, 4) # window size 15, polynomial order 4
    peaks, _ = find_peaks(yhat, distance=dist_pk)

    pathname = os.path.abspath(os.path.join(fld, 'tmp.xvg'))
    os.remove(pathname)

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    return peaks

def rdf_peaks(TRR,TPR,IND,ST,EN,fld,arg1,arg2,dist_pk):

    cmd=str(arg1)+'\n'+str(arg2)+'\n'
    if EN==-1:
        p = subprocess.Popen(['gmx', 'rdf', '-b', str(ST), '-selrpos', 'mol_com',
                            '-f', TRR,
                            '-s',TPR,
                            '-n',IND,
                            '-o', fld+'/tmp.xvg'],
                        stdin=subprocess.PIPE)
    else:
        p = subprocess.Popen(['gmx', 'rdf', '-b', str(ST), '-e', str(EN), '-selrpos', 'mol_com',
                            '-f', TRR,
                            '-s',TPR,
                            '-n',IND,
                            '-o', fld+'/tmp.xvg'],
                        stdin=subprocess.PIPE)
    p.communicate(cmd.encode('UTF-8'))
    p.send_signal('<Ctrl>-D')
    p.wait()

    x,y=read_xvg(XVG=fld+'/tmp.xvg')
    yhat = savgol_filter(y, 15, 4) # window size 15, polynomial order 4
    peaks, _ = find_peaks(yhat, distance=dist_pk)

    pathname = os.path.abspath(os.path.join(fld, 'tmp.xvg'))
    os.remove(pathname)

    return peaks

def order_ndx(box_res,fld,atoms,sv_name):
#def togmxndx(box_res, fld, sv_name):
    print(' ')
    print('Save to order_ndx progress:')
    print('==============================')
    cnt=0
    fl_save=fld+'/'
    if not os.path.exists(fl_save):
        os.makedirs(fl_save)
    else:
        print('WARNING: .ndx files exists. Nothing to do!')
        return
 
    ndx={}
    for step in box_res.keys():
        if cnt==1:
            break
        for resid in box_res[step].keys():
            for res in box_res[step][resid].keys():
                for subdomain in box_res[step][resid][res].keys():
                    ndx[subdomain]={}

        rs=[]
        bar = Bar('Create format: ', max=len(box_res[step].keys()))
        for resid in box_res[step].keys():
            for res in box_res[step][resid].keys():
                for subdomain in box_res[step][resid][res]:
                    posl=-1
                    for at in atoms:
                        posl=posl+1
                        if (at,subdomain) not in rs:
                            rs.append((at,subdomain))
                            ndx[subdomain][at]=[]
                        posf=-1
                        for atomnum in box_res[step][resid][res][subdomain]:    
                            posf=posf+1
                            if posl==posf:
                                ndx[subdomain][at].append(atomnum)
            bar.next()
        bar.finish()                 
        cnt=cnt+1

    bar = Bar('Save file: ', max=len(ndx.keys()))
    for subdomain in ndx.keys():
        save=fl_save+sv_name+'_'+str(subdomain)+'.ndx'
        with open(save, "a") as myfile:
            for res in ndx[subdomain].keys():
                myfile.write("["+res+"]\n")
                for atomnum in ndx[subdomain][res]:
                    myfile.write(atomnum+'\n')

        bar.next()
    bar.finish()

def order(TRR,TPR,IND,ST,EN,normal,fld,dist_pk=1):

    if EN==-1:
        p = subprocess.Popen(['gmx', 'order', '-b', str(ST), '-d', normal,
                            '-f', TRR,
                            '-s',TPR,
                            '-n',IND,
                            '-o', fld+'/tmp1.xvg',
                            '-od', fld+'/tmp2.xvg'],
                        stdin=subprocess.PIPE)
    else:
        p = subprocess.Popen(['gmx', 'order', '-b', str(ST), '-e', str(EN), '-d', normal,
                            '-f', TRR,
                            '-s',TPR,
                            '-n',IND,
                            '-o', fld+'/tmp1.xvg',
                            '-od', fld+'/tmp2.xvg'],
                        stdin=subprocess.PIPE)
    p.wait()

    x,y=read_xvg(XVG=fld+'/tmp1.xvg')
    #yhat = savgol_filter(y, 15, 4) # window size 15, polynomial order 4
    #peaks, _ = find_peaks(yhat, distance=dist_pk)
    #exit()
    pathname = os.path.abspath(os.path.join(fld, 'tmp1.xvg'))
    os.remove(pathname)
    pathname = os.path.abspath(os.path.join(fld, 'tmp2.xvg'))
    os.remove(pathname)

    return y