In [1]:
import os
import sys
import random
import time
from random import seed, randint
import argparse
import platform
from datetime import datetime
import imp
import numpy as np
import fileinput
from itertools import product
import pandas as pd
from scipy.interpolate import griddata
from scipy.interpolate import interp2d
import seaborn as sns
from os import listdir

import matplotlib.pyplot as plt
import seaborn as sns
from scipy.interpolate import griddata
import matplotlib as mpl
# sys.path.insert(0,'..')
# from notebookFunctions import *
# from .. import notebookFunctions
from Bio.PDB.Polypeptide import one_to_three
from Bio.PDB.Polypeptide import three_to_one
from Bio.PDB.PDBParser import PDBParser
from pyCodeLib import *
# from small_script.myFunctions import *
sys.path.insert(0, "/Users/weilu/openmmawsem")
from helperFunctions.myFunctions import *
from collections import defaultdict
%matplotlib inline
# plt.rcParams['figure.figsize'] = (10,6.180)    #golden ratio
# %matplotlib notebook
%load_ext autoreload
%autoreload 2

In [2]:
plt.rcParams['figure.figsize'] = np.array([16.18033, 10])    #golden ratio
plt.rcParams['figure.facecolor'] = 'w'
plt.rcParams['figure.dpi'] = 100
plt.rcParams.update({'font.size': 22})

In [3]:
def mycp(source, target):
    os.system(f"cp {source} {target}")
    
do = os.system

def getSeq(fileLocation):
    p = PDBParser()
    s = p.get_structure("test", fileLocation)
    seq = ""
    residues = list(s.get_residues())
    for residue in residues:
        res_id = residue.get_id()[0]
        if res_id==' ':
            residue_name = residue.get_resname()
            seq += three_to_one(residue_name)
    return seq

# get chains nad seq
def getChainsAndSeq(fileLocation):
    # fileLocation = "/Users/weilu/Research/examples/optimization/optimization/Structure_Ensemble/1.pdb"
    p = PDBParser()
    pdb = p.get_structure("test", fileLocation)
    residues = list(pdb.get_residues())
    seq = ""
    chains = ""
    for residue in residues:
        res_id = residue.get_id()[0]
        chain = residue.get_full_id()[2]
        if res_id==' ':
            residue_name = residue.get_resname()
            seq += three_to_one(residue_name)
            chains += chain
    return chains, seq

In [7]:
pdbFolderList = glob.glob("/Users/weilu/Dropbox/Optimization_Xfunnel/Structure_Ensemble_*")

In [34]:
pdbFolderList = [ '/Users/weilu/Downloads/Optimization_Xfunnel/Structure_Ensemble_2BNQ',
 '/Users/weilu/Downloads/Optimization_Xfunnel/Structure_Ensemble_4FTV']

In [4]:
pdbFolder = pdbFolderList[0]

In [5]:
pdbName = pdbFolder.split("_")[-1]

In [6]:
pdbName


Out[6]:
'1BD2'

In [41]:
len(pdbFolderList)


Out[41]:
33

In [8]:
# pre = "/Users/weilu/Research/server/sep_2019/peptide_optimization_specific_test"
pre = "/Users/weilu/Research/server/sep_2019/peptide_optimization_trial_6"

do(f"mkdir -p {pre}/database/dompdb")
do(f"mkdir -p {pre}/database/S20_seq")

for pdbFolder in pdbFolderList:
    pdbName = pdbFolder.split("_")[-1]
    source = pdbFolder + f"/*.pdb"
    p_list = glob.glob(source)
    if len(p_list) > 1:
        print(p_list)
        continue
    if len(p_list) == 0:
        print("no", pdbName)
    for p in p_list:
        target = f"{pre}/database/dompdb/{pdbName}.pdb"
        ## move native pdbs to dompdb
        mycp(p, target)
        ## move native seq to S20_seq
        seq = getSeq(target)
        fileLocation = f"{pre}/database/S20_seq/{pdbName}.seq"
        with open(fileLocation, "w") as out:
            out.write(seq+"\n")

In [44]:

two branch. one: set as 100 per structure.


In [28]:
pre = "/Users/weilu/Research/server/sep_2019/peptide_optimization_trial_6"

do(f"mkdir -p {pre}/database/dompdb")
do(f"mkdir -p {pre}/database/S20_seq")
do(f"mkdir -p {pre}/optimization/decoys/shuffle")
# generate decoys
protein_list = []
for pdbFolder in pdbFolderList:
    pdbName = pdbFolder.split("_")[-1]
    source = pdbFolder + f"/*.pdb"
    p_list = glob.glob(source)
    # print(p_list, source)
    assert len(p_list) == 1
    p = p_list[0]
    chain_seq, seq = getChainsAndSeq(p)
    
    # print(pdbName, i, len(seq), len(chain_seq))
    decoy_list = []
    with open(f"{pdbFolder}/peptide.txt") as f:
        for line in f:
            pep = line.strip()
            pep_len = len(pep)
            # assert len(pep) == 9
            for c in list(set(chain_seq)):
                if chain_seq.count(c) == pep_len:
                    first_c = chain_seq.find(c)
                    a = list(seq)
                    a[first_c:first_c+pep_len] = pep
                    assert len(set(chain_seq[first_c:first_c+pep_len])) == 1
                    decoy = "".join(a)
                    decoy_list.append(decoy)
                    
    # two branch. one: set as 100 per structure.
    n = int(len(decoy_list) // 100)
    for i in range(n):
        opt_pdbName = f"{pdbName}_{i}"
        fileLocation = f"{pre}/optimization/decoys/shuffle/{opt_pdbName}.decoys"
        # print("1", pdbName, len(decoy_list), pep_len)
        with open(fileLocation, "w") as out:
            for decoy in decoy_list[i*100:(i+1)*100]:
                out.write(decoy+"\n")
        protein_list.append(opt_pdbName)
        
        
        target = f"{pre}/database/dompdb/{opt_pdbName}.pdb"
        ## move native pdbs to dompdb
        mycp(p, target)
        ## move native seq to S20_seq
        seq = getSeq(target)
        fileLocation = f"{pre}/database/S20_seq/{opt_pdbName}.seq"
        with open(fileLocation, "w") as out:
            out.write(seq+"\n")
#     fileLocation = f"{pre}/optimization/decoys/shuffle/{pdbName}.decoys"
# #     if len(decoy_list) == 0:
# #         print(pdbName)
#     print("1", pdbName, len(decoy_list), pep_len)
#     with open(fileLocation, "w") as out:
#         for decoy in decoy_list:
#             out.write(decoy+"\n")

In [29]:
len(protein_list)


Out[29]:
475

In [30]:
do(f"mkdir -p {pre}/optimization")
do(f"mkdir -p {pre}/phis")

## write protein_list
fileLocation = f"{pre}/optimization/protein_list"
with open(fileLocation, "w") as out:
    for pdbName in protein_list:
        out.write(f"{pdbName}\n")

do(f"cp ~/opt/optimization/phi_list_contact.txt {pre}/optimization/phi_list.txt")


Out[30]:
0

In [ ]:


In [ ]:
# pre = "/Users/weilu/Research/server/sep_2019/peptide_optimization_specific_test"
pre = "/Users/weilu/Research/server/sep_2019/peptide_optimization_trial_6"

do(f"mkdir -p {pre}/database/dompdb")
do(f"mkdir -p {pre}/database/S20_seq")

for pdbFolder in pdbFolderList:
    pdbName = pdbFolder.split("_")[-1]
    source = pdbFolder + f"/*.pdb"
    p_list = glob.glob(source)
    if len(p_list) > 1:
        print(p_list)
        continue
    if len(p_list) == 0:
        print("no", pdbName)
    for p in p_list:
        target = f"{pre}/database/dompdb/{pdbName}.pdb"
        ## move native pdbs to dompdb
        mycp(p, target)
        ## move native seq to S20_seq
        seq = getSeq(target)
        fileLocation = f"{pre}/database/S20_seq/{pdbName}.seq"
        with open(fileLocation, "w") as out:
            out.write(seq+"\n")

In [46]:



Out[46]:
0

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [29]:
do(f"mkdir -p {pre}/optimization/decoys/shuffle")
# generate decoys
for pdbFolder in pdbFolderList:
    pdbName = pdbFolder.split("_")[-1]
    source = pdbFolder + f"/*ab.pdb"
    p_list = glob.glob(source)
    if pdbName != "3D39":
        continue
    # print(p_list, source)
    assert len(p_list) == 1
    chain_seq, seq = getChainsAndSeq(p_list[0])
    print(pdbName, i, len(seq), len(chain_seq))
    decoy_list = []
    decoy = ""
    with open(f"{pdbFolder}/peptide.txt") as f:
        for line in f:
            pep = line.strip()
            assert len(pep) == 9
            for c in list(set(chain_seq)):
                if chain_seq.count(c) == 9:
                    first_c = chain_seq.find(c)
                    a = list(seq)
                    a[first_c:first_c+9] = pep
                    decoy = "".join(a)
                    decoy_list.append(decoy)
    fileLocation = f"{pre}/optimization/decoys/shuffle/{pdbName}.decoys"
    with open(fileLocation, "w") as out:
        for decoy in decoy_list:
            out.write(decoy+"\n")


3D39 1 419 419

In [31]:
list(set(chain_seq))


Out[31]:
['A', 'X', 'B', 'Y']

In [32]:
chain_seq.count("Y")


Out[32]:
8

In [ ]:


In [ ]:


In [ ]:


In [ ]:
do(f"mkdir -p {pre}/optimization")
## write protein_list
fileLocation = f"{pre}/optimization/protein_list"
with open(fileLocation, "w") as out:
    for pdbFolder in pdbFolderList:
        pdbName = pdbFolder.split("_")[-1]
        out.write(f"{pdbName}\n")

In [205]:
## write protein_list
fileLocation = f"{pre}/optimization/protein_list"
with open(fileLocation, "w") as out:
    for pdbFolder in pdbFolderList:
        pdbName = pdbFolder.split("_")[-1]
        for i in range(1, 6):
            out.write(f"{pdbName}_{i}\n")
# generate decoys
for pdbFolder in pdbFolderList:
    pdbName = pdbFolder.split("_")[-1]
    for i in range(1, 6):
        source = pdbFolder + f"/{i}.pdb"
        source = pdbFolder + f"/*ab.pdb"
        p_list = glob.glob(source)
        assert len(p_list) == 1
        chain_seq, seq = getChainsAndSeq(source)
        print(pdbName, i, len(seq), len(chain_seq))
        decoy_list = []
        with open(f"{pdbFolder}/peptide.txt") as f:
            for line in f:
                pep = line.strip()
                assert len(pep) == 9
                for c in list(set(chain_seq)):
                    if chain_seq.count(c) == 9:
                        first_c = chain_seq.find(c)
                        a = list(seq)
                        a[first_c:first_c+9] = pep
                        decoy = "".join(a)
                        decoy_list.append(decoy)
        fileLocation = f"{pre}/optimization/decoys/shuffle/{pdbName}_{i}.decoys"
        with open(fileLocation, "w") as out:
            for decoy in decoy_list:
                out.write(decoy+"\n")

In [19]:


In [168]:
# ensure they all 9 residue peptide.
for pdbFolder in pdbFolderList:
    pdbName = pdbFolder.split("_")[-1]
    # print(pdbName)
    with open(f"{pdbFolder}/peptide.txt") as f:
        for line in f:
            pep = line.strip()
            assert len(pep) == 9

In [160]:
fileLocation


Out[160]:
'/Users/weilu/Research/server/sep_2019/peptide_optimization_trial_2/database/S20_seq/4FTV_5.seq'

In [164]:
for c in "ABCDEFG":
    if chain_seq.count(c) == 9:
        first_c = chain_seq.find(c)
        a = list(seq)
        a[first_c:first_c+9] = pep
        decoy = "".join(a)
        decoy_list.append(decoy)


Out[164]:
107

In [ ]:


In [3]:
# get seq
fileLocation = "/Users/weilu/Research/examples/optimization/optimization/Structure_Ensemble/1.pdb"
p = PDBParser()
pdb = p.get_structure("test", fileLocation)
residues = list(pdb.get_residues())
seq = ""
chains = ""
for residue in residues:
    res_id = residue.get_id()[0]
    chain = residue.get_full_id()[2]
    if res_id==' ':
        residue_name = residue.get_resname()
        seq += three_to_one(residue_name)
        chains += chain

In [5]:
# get decoy
decoy_list = []
with open("/Users/weilu/Research/examples/optimization/optimization/Structure_Ensemble/peptide.txt") as f:
    for line in f:
        pep = line.strip()
        assert len(pep) == 9
        a = list(seq)
        a[180:189] = pep
        decoy = "".join(a)
        decoy_list.append(decoy)

In [38]:
for i in range(1, 91):
    pre = "/Users/weilu/Research/server/sep_2019/peptide_optimization"
    fileLocation = f"{pre}/database/S20_seq/{i}.seq"
    with open(fileLocation, "w") as out:
        out.write(seq+"\n")

In [40]:
for i in range(1, 91):
    fileLocation = f"{pre}/optimization/decoys/shuffle/{i}.decoys"
    with open(fileLocation, "w") as out:
        for decoy in decoy_list:
            out.write(decoy+"\n")

In [42]:
with open("/Users/weilu/Research/server/sep_2019/peptide_optimization/optimization/protein_list", "w") as out:
    for i in range(1, 91):
        out.write(f"{i}\n")

In [41]:
for i in range(1, 91):
    os.system(f"cp /Users/weilu/Research/examples/optimization/optimization/Structure_Ensemble/{i}.pdb /Users/weilu/Research/server/sep_2019/peptide_optimization/database/dompdb/")

In [39]:
len(decoy_list)


Out[39]:
1000

In [6]:
seq[180:189]


Out[6]:
'SLLMWITQV'

In [49]:
a = list(seq)
a[180:189] = list('FIFLLFLTL')

In [151]:


In [26]:
all_seq = []
for i in range(1, 91):
    fileLocation = f"/Users/weilu/Research/examples/optimization/optimization/Structure_Ensemble/{i}.pdb"
    seq = getSeq(fileLocation)
    assert len(seq) == 414
    all_seq.append(seq)
#     assert preSeq == seq
#     preSeq = seq

In [27]:
all_seq[0]


Out[27]:
'GSHSMRYFFTSVSRPGRGEPRFIAVGYVDDTQFVRFDSDAASQRMEPRAPWIEQEGPEYWDGETRKVKAHSQTHRVDLGTLRGYYNQSEAGSHTVQRMYGCDVGSDWRFLRGYHQYAYDGKDYIALKEDLRSWTAADMAAQTTKHKWEAAHVAEQLRAYLEGTCVEWLRRYLENGKETLQSLLMWITQVQEVTQIPAALSVPEGENLVLNCSFTDSAIYNLQWFRQDPGKGLTSLLLIQSSQREQTSGRLNASLDKSSGRSTLYIAASQPGDSATYLCAVRPTSGGSYIPTFGRGTSLIVHPGVTQTPKFQVLKTGQSMTLQCAQDMNHEYMSWYRQDPGMGLRLIHYSVGAGITDQGEVPNGYNVSRSTTEDFPLRLLSAAPSQTSVYFCASSYVGNTGELFFGEGSRLTVLE'

In [28]:
all_seq[0] == all_seq[1]


Out[28]:
True

In [29]:
for i in range(90):
    if all_seq[i] != all_seq[0]:
        print(i)

In [9]:
def getAllFrames(movieLocation):
    # movieLocation = "/Users/weilu/Research/examples/openMM_simulation/test_2/movie.pdb"
    location = movieLocation
    with open(location) as f:
        a = f.readlines()
    n = len(a)
    # get the position of every model title
    model_title_index_list = []
    for i in range(n):
        if len(a[i]) >= 5 and a[i][:5] == "MODEL":
            model_title_index = i
            model_title_index_list.append(model_title_index)
    model_title_index_list.append(n)
    check_array = np.diff(model_title_index_list)
    if np.allclose(check_array, check_array[0]):
        size = check_array[0]
    elif np.allclose(check_array[:-1], check_array[0]) and check_array[-1] == check_array[0] + 1:
        # this is ok. with extra "END"
        size = check_array[0]
    else:  
        print("!!!! Someting is wrong  !!!!")
        print(check_array)
    return a

In [10]:
num_of_frames = int(n/size)

In [11]:
frame = 5
oneFrame = a[size*frame:size*(frame+1)]

In [60]:
frame = num_of_frames
oneFrame = a[size*frame:size*(frame+1)]

In [61]:
oneFrame


Out[61]:
[]

In [45]:
# s = p.get_structure("test", f)
# residues = list(s.get_residues())

In [24]:
import io

f = io.StringIO("".join(oneFrame))
MAX_OFFSET=4
DISTANCE_CUTOFF=9.5
s = p.get_structure("test", f)
chains = s[0].get_list()

# import pdb file
native_coords = []
for chain in chains:
    dis = []
    all_res = []
    for res in chain:
        is_regular_res = res.has_id('CA') and res.has_id('O')
        res_id = res.get_id()[0]
        if (res.get_resname()=='GLY'):
            native_coords.append(res['CA'].get_coord())
        elif (res_id==' ' or res_id=='H_MSE' or res_id=='H_M3L' or res_id=='H_CAS') and is_regular_res:
            native_coords.append(res['CB'].get_coord())
        else:
            print('ERROR: irregular residue at %s!' % res)
            exit()
native_contacts_table = compute_native_contacts(native_coords, MAX_OFFSET, DISTANCE_CUTOFF)

In [25]:
native_contacts_table


Out[25]:
array([[0, 0, 0, ..., 1, 1, 0],
       [0, 0, 0, ..., 1, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [1, 1, 0, ..., 0, 0, 0],
       [1, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]])

In [62]:
plt.imshow(native_contacts_table, origin=[0,0])


Out[62]:
<matplotlib.image.AxesImage at 0x1a23839898>

In [ ]: