-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_scores.py
More file actions
164 lines (138 loc) · 5.4 KB
/
process_scores.py
File metadata and controls
164 lines (138 loc) · 5.4 KB
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
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
#!/home/kmm5/anaconda2/envs/py36/bin/python
"""
Name: process_scores.py
Author: Kareem Mehrabiani
Date: 12th February 2020
Creates a contact map from DCA raw scoring matrix. Optional: Maps DCA residues
to PDB.
Usage: process_scores.py -s [dca scoring matrix]
If mapping pfam to pdb indices add:
--hmmscan [hmmscan file]
optional arguments:
-h, --help show this help message and exit
-s S Raw DCA scores output.
--hmmscan HMMSCAN hmmscan file (if mapping)
Outputs:
- three-column contact map (in same dir as input)
- mapping reference (optional)
- mapped three-column contact map (in current dir)
"""
import argparse
import os
import pandas as pd
import numpy as np
import sys
import re
def process_scan(scan_in):
"""Separates hmmscan results into Domain A and B."""
f = open(scan_in, 'r')
data = f.read()
raw_data = (re.findall(r'== domain (.*?)Internal', data, re.DOTALL))
len(raw_data)
scan_A = raw_data[0].split('\n')
if len(raw_data) > 1:
scan_B = raw_data[1].split('\n')
domain_A = scan_A[1].split()
pdb_A = scan_A[3].split()
domain_B = scan_B[1].split()
pdb_B = scan_B[3].split()
domain_seq = domain_A[2] + domain_B[2]
pdb_seq = pdb_A[2] + pdb_B[2]
return [domain_A, domain_B], [pdb_A, pdb_B], domain_seq, pdb_seq
###############################################################################################
def map_loop(scan_in):
"""Creates a dictionary object with DCA-to-PDB map."""
print("\tMapping using %s file\n" % scan_in)
# Initialize
file_header = 'Domain\td_seq\tp_seq\tPDB'
domain, pdb, domain_seq, pdb_seq = process_scan(scan_in)
domain_A, domain_B = domain[0], domain[1]
pdb_A, pdb_B = pdb[0], pdb[1]
domain_begin_A = int(domain_A[1])
pdb_begin_A = int(pdb_A[1])
length_domain = len(domain_seq)
length_pdb = len(pdb_seq)
domain_num = np.zeros(length_domain, dtype=int)
pdb_num = np.zeros(length_pdb, dtype=int)
resid_domain = []
resid_pdb = []
insert_count = 0
gap_count = 0
# Loop through domain and pdb sequence, counting every non-insert
for i, res in enumerate(domain_seq, domain_begin_A):
resid_domain.append(res)
if res == '.':
insert_count = insert_count + 1
if res != '.':
domain_num[i - int(domain_A[1])] = i - insert_count
for i, res in enumerate(pdb_seq, pdb_begin_A):
resid_pdb.append(res)
if i > int(pdb_A[3]):
if res == '-':
gap_count = gap_count + 1
if res != '-':
pdb_num[i - int(pdb_A[1])] = (i + 4) - gap_count
else:
if res == '-':
gap_count = gap_count + 1
if res != '-':
pdb_num[i - int(pdb_A[1])] = i - gap_count
name = os.path.split(scan_in)[-1]
outfile_ref = "map_ref_" + name
map_array = np.transpose([domain_num, resid_domain, resid_pdb, pdb_num])
np.savetxt(outfile_ref, map_array, fmt='%s', delimiter='\t', header=file_header)
map_dict = dict(zip(domain_num[domain_num != 0] - (domain_begin_A - 2),
pdb_num[pdb_num != 0] - (domain_begin_A - 2)))
print("Output (current dir):\n\t%s" % outfile_ref)
return map_dict
###############################################################################################
def map_dca(scan_in, dcafile):
"""Converts DCA pairs into PDB coordinates and outputs a file."""
names = ['i', 'j', 'score']
dca_data = pd.read_csv(dcafile, names=names, delim_whitespace=True, usecols=(0, 1, 2))
name = os.path.split(scan_in)[-1]
outfile_map = "map_" + name
map_dict = map_loop(scan_in)
N = len(dca_data)
mapped_dca = []
for i in range(N):
dca_resi = dca_data['i'][i]
dca_resj = dca_data['j'][i]
fn = dca_data['score'][i]
if dca_resi in map_dict and dca_resj in map_dict:
mapped_dca.append([map_dict[dca_resi], map_dict[dca_resj], fn, dca_resi, dca_resj])
header = 'i\tj\tfn\ti_pfam\tj_pfam'
np.savetxt(outfile_map, mapped_dca, fmt='%d\t%d\t%f\t%d\t%d', header=header)
print("\t%s" % outfile_map)
###############################################################################################
def cm_make(scores):
# x is the output FN matrix
filename = scores.strip(".txt") + "_CM.txt"
x = np.loadtxt(scores)
x_output = []
N = x.shape[0]
for i in range(N - 1):
for j in range(i + 1, N):
x_output.append([i + 1, j + 1, x[i, j]])
header = ['i', 'j', 'score']
df_x = pd.DataFrame(x_output, columns=header)
df_x = df_x.sort_values(ascending=False, by=['score'])
np.savetxt(filename, df_x, fmt='%d\t%d\t%f')
print("\tWrote contact map to: %s" % filename)
return filename
###############################################################################################
def main():
print("Starting")
parser = argparse.ArgumentParser(description='Creates a contact map from \
DCA raw scoring matrix. Optional: Maps DCA residues to PDB.')
parser.add_argument('-s', required=True, help="Raw DCA scores output.")
parser.add_argument('--hmmscan', required=False, help='hmmscan file \
(if mapping)')
args = parser.parse_args()
scores = args.s
dcafile = cm_make(scores)
if args.hmmscan:
scan_in = args.hmmscan
map_dca(scan_in, dcafile)
if __name__ == '__main__':
main()