tooba_gmx.py 2.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
import os
import subprocess
import numpy as np
from scipy.signal import find_peaks
from scipy.signal import savgol_filter
import hashlib
#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)
def ndx_index(SYSTEM_NAME):
    uniq_id={}
    path='./'+SYSTEM_NAME+'/gmx_ndx/'
    for f in os.listdir(path):
        text=SYSTEM_NAME+'_'+f
        iidd=hashlib.md5(text.encode('utf-8')).hexdigest()
26
        dom=f.split('_')[2].split('.')[0]
27
        uniq_id[iidd]={}
28
        uniq_id[iidd]={'system':SYSTEM_NAME,'ndx_file':f,'domain':dom}
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    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

def density_picks(TRR,TPR,IND,SLC,ST,EN,normal,fld,arg,dist_pk=1):

    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:
        p = subprocess.Popen(['gmx', 'density', '-symm', '-b', str(ST), '-e', str(EN),'-sl',str(SLC), '-d', normal,
                            '-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)

    return peaks #res