Spaces:
Sleeping
Sleeping
Commit
·
8b0bef9
1
Parent(s):
cf1af18
Create add_sasa.py
Browse files- code/add_sasa.py +119 -0
code/add_sasa.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import glob
|
| 2 |
+
import ssbio.utils
|
| 3 |
+
import subprocess
|
| 4 |
+
import ssbio
|
| 5 |
+
import os.path as op
|
| 6 |
+
from add_3Dalignment import *
|
| 7 |
+
import os
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
import gzip
|
| 10 |
+
import shutil
|
| 11 |
+
import streamlit as st
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def run_freesasa(infile, outfile, include_hetatms=True, outdir=None, force_rerun=False, file_type = 'gzip'):
|
| 16 |
+
if not outdir:
|
| 17 |
+
outdir = ''
|
| 18 |
+
outfile = op.join(outdir, outfile)
|
| 19 |
+
if file_type == 'pdb':
|
| 20 |
+
if ssbio.utils.force_rerun(flag=force_rerun, outfile=outfile):
|
| 21 |
+
if include_hetatms:
|
| 22 |
+
shell_command = 'freesasa --format=rsa --hetatm {} -o {}'.format(infile, outfile)
|
| 23 |
+
else:
|
| 24 |
+
shell_command = 'freesasa --format=rsa {} -o {}'.format(infile, outfile)
|
| 25 |
+
command = subprocess.Popen(shell_command,
|
| 26 |
+
stdout=subprocess.PIPE,
|
| 27 |
+
stderr=subprocess.PIPE,
|
| 28 |
+
shell=True)
|
| 29 |
+
out, err = command.communicate()
|
| 30 |
+
elif file_type == 'gzip':
|
| 31 |
+
with gzip.open(infile, 'rb') as f_in:
|
| 32 |
+
with open('file_temp.pdb', 'wb') as f_out:
|
| 33 |
+
shutil.copyfileobj(f_in, f_out)
|
| 34 |
+
|
| 35 |
+
infile = 'file_temp.pdb'
|
| 36 |
+
|
| 37 |
+
if ssbio.utils.force_rerun(flag=force_rerun, outfile=outfile):
|
| 38 |
+
if include_hetatms:
|
| 39 |
+
shell_command = 'freesasa --format=rsa --hetatm {} -o {}'.format(infile, outfile)
|
| 40 |
+
else:
|
| 41 |
+
shell_command = 'freesasa --format=rsa {} -o {}'.format(infile, outfile)
|
| 42 |
+
command = subprocess.Popen(shell_command,
|
| 43 |
+
stdout=subprocess.PIPE,
|
| 44 |
+
stderr=subprocess.PIPE,
|
| 45 |
+
shell=True)
|
| 46 |
+
out, err = command.communicate()
|
| 47 |
+
return outfile
|
| 48 |
+
|
| 49 |
+
def calculate_freesasa(ID, model_num, existing_free_sasa, path_to_input,path_to_output_files, file_type = 'gzip'):
|
| 50 |
+
print('Calculating surface area...\n')
|
| 51 |
+
file_base = str(Path(path_to_input / '*'))
|
| 52 |
+
file_str = glob.glob(file_base)[0].split('-')[-1].split('.')[0]
|
| 53 |
+
if file_type == 'gzip':
|
| 54 |
+
if ID not in existing_free_sasa:
|
| 55 |
+
fullID = f'AF-{ID}-F{model_num}-{file_str }.pdb.gz'
|
| 56 |
+
run_freesasa(Path(path_to_input / fullID),
|
| 57 |
+
Path(path_to_output_files / f'freesasa_files/{fullID}.txt'), include_hetatms=True,
|
| 58 |
+
outdir=None, force_rerun=False)
|
| 59 |
+
elif file_type == 'pdb':
|
| 60 |
+
if ID not in existing_free_sasa:
|
| 61 |
+
fullID = f'AF-{ID}-F{model_num}-model_v1.pdb'
|
| 62 |
+
run_freesasa(Path(path_to_input / fullID),
|
| 63 |
+
Path(path_to_output_files / f'freesasa_files/{fullID}.txt'), include_hetatms=True,
|
| 64 |
+
outdir=None, force_rerun=False)
|
| 65 |
+
|
| 66 |
+
def sasa(uniprotID, sasa_pos, wt, mode, filename, path_to_output_files, file_type = 'gzip'):
|
| 67 |
+
if mode == 1:
|
| 68 |
+
files = open(filename, 'r')
|
| 69 |
+
file = files.readlines()
|
| 70 |
+
for k in file:
|
| 71 |
+
if str(k.strip()[10:13].strip()) == str(sasa_pos):
|
| 72 |
+
residue = str(k[4:7].strip())
|
| 73 |
+
if wt == threeToOne(residue):
|
| 74 |
+
sasa = str(k[22:28]).strip('\n')
|
| 75 |
+
return (sasa)
|
| 76 |
+
elif wt != threeToOne(residue):
|
| 77 |
+
sasa = str(k[22:28]).strip('\n') + '*'
|
| 78 |
+
return (sasa)
|
| 79 |
+
else:
|
| 80 |
+
return 'nan'
|
| 81 |
+
if mode == 2:
|
| 82 |
+
if sasa_pos != np.NaN:
|
| 83 |
+
sasa = 'nan'
|
| 84 |
+
if file_type == 'pdb':
|
| 85 |
+
for filename in list(Path(path_to_output_files / 'freesasa_files').glob("*")):
|
| 86 |
+
fname = list(filter(None, filename.split('.'))).split('/')[-1].upper()
|
| 87 |
+
if uniprotID == fname:
|
| 88 |
+
files = open(filename, 'r')
|
| 89 |
+
file = files.readlines()
|
| 90 |
+
for k in file:
|
| 91 |
+
if k.strip()[10:13] == sasa_pos:
|
| 92 |
+
residue = str(k[4:7].strip())
|
| 93 |
+
if wt == threeToOne(residue):
|
| 94 |
+
sasa = str(k[22:28]).strip('\n')
|
| 95 |
+
elif wt != threeToOne(residue):
|
| 96 |
+
sasa = str(k[22:28]).strip('\n') + '*'
|
| 97 |
+
|
| 98 |
+
return sasa
|
| 99 |
+
elif file_type == 'gzip':
|
| 100 |
+
for filename in list(Path(path_to_output_files / 'freesasa_files').glob("*")):
|
| 101 |
+
fname = list(filter(None, str(filename).split('.')))[0].split('/')[-1].split('-')[1].upper()
|
| 102 |
+
|
| 103 |
+
if uniprotID == fname:
|
| 104 |
+
files = open(filename, 'r')
|
| 105 |
+
file = files.readlines()
|
| 106 |
+
for k in file:
|
| 107 |
+
if str(k.strip()[10:13]).strip() == str(sasa_pos):
|
| 108 |
+
residue = str(k[4:7].strip())
|
| 109 |
+
if wt == threeToOne(residue):
|
| 110 |
+
sasa = str(k[22:28]).strip('\n')
|
| 111 |
+
elif wt != threeToOne(residue):
|
| 112 |
+
sasa = str(k[22:28]).strip('\n') + '*'
|
| 113 |
+
else:
|
| 114 |
+
sasa = 'nan'
|
| 115 |
+
|
| 116 |
+
return sasa
|
| 117 |
+
else:
|
| 118 |
+
sasa = 'nan'
|
| 119 |
+
return sasa
|