-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualize_proteins.py
More file actions
163 lines (140 loc) · 6.39 KB
/
visualize_proteins.py
File metadata and controls
163 lines (140 loc) · 6.39 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
#! /usr/bin/env python3
import os
import argparse
import matplotlib.pyplot as plt
from dna_features_viewer import BiopythonTranslator
from Bio import SeqIO
import numpy as np
##########################################################################################
## METHODS
##########################################################################################
class MyCustomTranslator(BiopythonTranslator):
graphic_record_parameters = {
"labels_spacing": 4,
}
def compute_feature_color(self, feature):
if feature.type == "CHAIN":
return "green"
elif feature.type == "DOMAIN":
return "red"
elif feature.type == "TOPO_DOM":
return "gold"
elif feature.type == "TRANSMEM":
return "gray"
else:
return "blue"
def compute_feature_label(self, feature):
if feature.type == "BINDING":
if BiopythonTranslator.compute_feature_label(self, feature) != feature.type:
return f"BINDING: {BiopythonTranslator.compute_feature_label(self, feature)}"
else:
return feature.type
elif feature.type == "ACT_SITE":
if BiopythonTranslator.compute_feature_label(self, feature) != feature.type:
return f"ACT_SITE: {BiopythonTranslator.compute_feature_label(self, feature)}"
else:
return feature.type
#elif feature.type == "MUTAGEN":
# if BiopythonTranslator.compute_feature_label(self, feature) != feature.type:
# return f"MUTAGEN: {BiopythonTranslator.compute_feature_label(self, feature)}"
# else:
# return feature.type
#elif feature.type == "TOPO_DOM":
# if BiopythonTranslator.compute_feature_label(self, feature) != feature.type:
# return f"{BiopythonTranslator.compute_feature_label(self, feature)}"
# else:
# return feature.type
#elif feature.type == "SITE":
# if BiopythonTranslator.compute_feature_label(self, feature) != feature.type:
# return f"SITE: {BiopythonTranslator.compute_feature_label(self, feature)}"
# else:
# return feature.type
#elif feature.type == "REGION":
# if BiopythonTranslator.compute_feature_label(self, feature) != feature.type:
# return f"REGION: {BiopythonTranslator.compute_feature_label(self, feature)}"
# else:
# return feature.type
#elif feature.type == "MOTIF":
# if BiopythonTranslator.compute_feature_label(self, feature) != feature.type:
# return f"MOTIF: {BiopythonTranslator.compute_feature_label(self, feature)}"
# else:
# return feature.type
elif feature.type == "CHAIN":
if BiopythonTranslator.compute_feature_label(self, feature) != feature.type:
return f"{BiopythonTranslator.compute_feature_label(self, feature)}"
else:
return feature.type
else:
return feature.type
#return feature.type
def compute_filtered_features(self, features):
return[
feature for feature in features
if (feature.type != "MUTAGEN" and feature.type != "VARIANT" and feature.type != "BINDING" and feature.type != "LIPID" and feature.type != "CONFLICT")
]
def load_tabular_file(file):
fields = []
with open(file, 'rt') as f:
records = f.read().splitlines()
for record in records:
fields.append(record.split("\t"))
return fields
def get_protein_list(codon2fts, proteins):
prot_list = []
for protein in proteins:
prot_list.append([x for x in codon2fts if x[0] == protein])
return prot_list
def visualize(protein_data, output_path, smoothing_window):
for protein in protein_data:
protein_name = protein[0][0]
seq = [x[2] for x in protein if x[4] == 'TRUE']
pos = [float(x[3]) for x in protein]
pos_label = [float(x[3])-1 for x in protein if x[4] == 'TRUE']
RSCU = [float(x[5]) for x in protein]
prot_features = [x[-1].split(',') if "," in x[-1] else x[-1] for x in protein]
if smoothing_window:
cumsum_vec = np.cumsum(np.insert(RSCU, 0, 0))
smoothing_vec = (cumsum_vec[smoothing_window:] - cumsum_vec[:-smoothing_window]) / smoothing_window
smoothing_positions = [x for x in range(smoothing_window//2, len(smoothing_vec)+smoothing_window//2)]
fig, (ax1, ax2) = plt.subplots(
2, 1, figsize=(16, 7), sharex=True, gridspec_kw={'height_ratios': [2, 2]})
record = SeqIO.read(args.protein_records, "swiss")
graphic_record = MyCustomTranslator().translate_record(record)
graphic_record.plot(ax=ax1, with_ruler=False, strand_in_label_threshold=1)
plt.sca(ax2)
ax2.plot(smoothing_positions, smoothing_vec)
plt.xticks(pos_label, seq, fontsize = 5)
ax2.tick_params(axis='x', colors='red', width=3, direction= 'in', labelbottom=False)
plt.xlabel("AA sequence", labelpad=10)
plt.ylabel("RSCU Values", labelpad=15)
fig.suptitle(f'{protein_name} Features and RSCU values')
fig.savefig(f'{output_path}/{protein_name}_fts_RSCU.svg')
######################################################################################
## ARGPARSE
######################################################################################
parser = argparse.ArgumentParser()
parser.add_argument("codon2fts_file", type=str, help="codon2fts file to be processed")
parser.add_argument("protein_records", type=str, help="parsed protein records file")
parser.add_argument('output_path', nargs='?', type=str, help="desired path for writting output file", default=os.getcwd())
parser.add_argument('-p', '--protein', type=str, help='protein AC to be specificially returned')
#parser.add_argument('-d', '--data_type', type=str, help='type of profile data to display')
parser.add_argument('-w', '--window', type=int, help='smoothing window size (must be an even number)')
args = parser.parse_args()
######################################################################################
## MAIN
######################################################################################
codon2fts = load_tabular_file(args.codon2fts_file)
proteins = args.protein.split(',')
protein_data = get_protein_list(codon2fts, proteins)
graphs_path = os.path.join(args.output_path, "Protein examples graphs")
isExist = os.path.exists(graphs_path)
if not isExist:
os.mkdir(graphs_path)
visualize(protein_data, graphs_path, args.window)
for protein in protein_data:
print(f'-------------------------------------{protein[0][0]}-----------------------------------------')
for codon in protein:
line = '\t'.join(codon)
print(line)
print('\n')
print(f"################ SVG graph saved at {graphs_path}/{protein[0][0]}_fts_RSCU.svg ################")