File size: 5,889 Bytes
c2a02c6
 
 
 
 
 
 
 
 
 
d6d6c3a
c451111
c2a02c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c451111
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
26
27
28
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
74
75
76
77
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
import glob
import ssbio.utils
import subprocess
import ssbio
import os.path as op
from add_3Dalignment import *
import os
from pathlib import Path
import gzip
import shutil
import streamlit as st


def run_freesasa(infile, outfile, include_hetatms=True, outdir=None, force_rerun=False, file_type = 'gzip'):
    if not outdir:
        outdir = ''
    outfile = op.join(outdir, outfile)
    if file_type == 'pdb':
        if ssbio.utils.force_rerun(flag=force_rerun, outfile=outfile):
            if include_hetatms:
                shell_command = 'freesasa --format=rsa --hetatm {} -o {}'.format(infile, outfile)
            else:
                shell_command = 'freesasa --format=rsa {} -o {}'.format(infile, outfile)
            command = subprocess.Popen(shell_command,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE,
                                       shell=True)
            out, err = command.communicate()
    elif file_type == 'gzip':
        with gzip.open(infile, 'rb') as f_in:
            with open('file_temp.pdb', 'wb') as f_out:
                shutil.copyfileobj(f_in, f_out)

        infile = 'file_temp.pdb'

        if ssbio.utils.force_rerun(flag=force_rerun, outfile=outfile):
            if include_hetatms:
                shell_command = 'freesasa --format=rsa --hetatm {} -o {}'.format(infile, outfile)
            else:
                shell_command = 'freesasa --format=rsa {} -o {}'.format(infile, outfile)
            command = subprocess.Popen(shell_command,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE,
                                       shell=True)
            out, err = command.communicate()
    return outfile

def calculate_freesasa(ID, model_num, existing_free_sasa, path_to_input,path_to_output_files, file_type = 'gzip'):
    print('Calculating surface area...\n')
    file_base = str(Path(path_to_input / '*'))
    file_str = glob.glob(file_base)[0].split('-')[-1].split('.')[0]
    if file_type == 'gzip':
        if ID not in existing_free_sasa:
            fullID = f'AF-{ID}-F{model_num}-{file_str }.pdb.gz'
            run_freesasa(Path(path_to_input / fullID),
                         Path(path_to_output_files / f'freesasa_files/{fullID}.txt'), include_hetatms=True,
                         outdir=None, force_rerun=False)
    elif file_type == 'pdb':
        if ID not in existing_free_sasa:
            fullID = f'AF-{ID}-F{model_num}-model_v1.pdb'
            run_freesasa(Path(path_to_input / fullID),
                         Path(path_to_output_files / f'freesasa_files/{fullID}.txt'), include_hetatms=True,
                         outdir=None, force_rerun=False)

def sasa(source, pdbID, uniprotID, sasa_pos, wt, mode, path_to_output_files,file_type = 'gzip'):
    if mode == 1:
        sasa = 'nan'
        for filename in list(Path(path_to_output_files / 'freesasa_files').glob("*")):
            if source == 'PDB':
                fname = str(filename).split('.')[0].split('/')[-1].upper()
            elif source == 'MODBASE':
                fname = str(filename).split('.')[0].split('/')[-1]
            elif source == 'SWISSSMODEL':
                fname = str(filename).split('_')[2]
            if pdbID == fname:
                files = open(filename, 'r')
                file = files.readlines()
                for k in file:

                    if k.strip()[10:13] == sasa_pos:
                        residue = str(k[4:7].strip())
                        if wt == threeToOne(residue):
                            sasa = str(k[22:28]).strip('\n')
                            return (sasa)
                        elif wt != threeToOne(residue):
                            sasa = str(k[22:28]).strip('\n') + '*'
                            return (sasa)
                        else:
                            return 'nan'  #######

    if mode == 2:
        if sasa_pos != np.NaN:
            sasa = 'nan'
            if file_type == 'pdb':
                for filename in list(Path(path_to_output_files / 'freesasa_files').glob("*")):
                    fname = list(filter(None, filename.split('.'))).split('/')[-1].upper()
                    if uniprotID == fname:
                        files = open(filename, 'r')
                        file = files.readlines()
                        for k in file:
                            if k.strip()[10:13] == sasa_pos:
                                residue = str(k[4:7].strip())
                                if wt == threeToOne(residue):
                                    sasa = str(k[22:28]).strip('\n')
                                elif wt != threeToOne(residue):
                                    sasa = str(k[22:28]).strip('\n') + '*'

                return sasa
            elif file_type == 'gzip':
                for filename in  list(Path(path_to_output_files / 'freesasa_files').glob("*")):
                    fname = list(filter(None, str(filename).split('.')))[0].split('/')[-1].split('-')[1].upper()

                    if uniprotID == fname:
                        files = open(filename, 'r')
                        file = files.readlines()
                        for k in file:
                            if str(k.strip()[10:13]) == str(sasa_pos):
                                residue = str(k[4:7].strip())
                                if wt == threeToOne(residue):
                                    sasa = str(k[22:28]).strip('\n')
                                elif wt != threeToOne(residue):
                                    sasa = str(k[22:28]).strip('\n') + '*'
                                else:
                                    sasa = 'nan'

                return sasa
        else:
            sasa = 'nan'
            return sasa