forked from KevinMenden/TMHProjectFinal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModuleMethods.py
91 lines (77 loc) · 2.23 KB
/
ModuleMethods.py
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
import math
import numpy as np
from scipy.spatial import distance
__date__ = '01.06.2016'
"""
Methods
"""
from sklearn.decomposition import PCA
from sklearn import svm
from sklearn.externals import joblib
hydrophobic_residues = ["F", "G", "I", "L", "M", "V", "W", "Y"]
def calculate_normal_vector(helices):
"""
Calculate the mean vector of all helix vectors, i.e. the
membrane normal vector
:param helices: all membrane helices
:return: the normal vector
"""
helix_set = []
for h in helices:
helix_set.append(h.vector)
helix_set.append(h.neg_vector)
pca = PCA(n_components=1)
pca.fit_transform(helix_set)
return pca.components_[0]
def add_quiver(ax, helix):
"""
Add a quiver to the plot
:param ax:
:param helix:
:return:
"""
vec = helix.vector
start = helix.start_point
end = helix.end_point
dist = end - start
length = math.sqrt(dist[0]**2 + dist[1]**2 + dist[2]**2)
ax.quiver(start[0], start[1], start[2], vec[0], vec[1], vec[2], length=length, arrow_length_ratio=0.1, pivot='tail',
cmap='Accent', lw=3)
def calculate_hydrophobicity(seq):
"""
Calculate the hydrophobicity factor for an amino acid sequence
:param seq: the input amino acid sequence
:return: the hydrophobicity factor
"""
phobic = 0
seq = seq.upper()
for elem in seq:
if elem in hydrophobic_residues:
phobic += 1
return phobic
def calculate_features(sequence):
"""
Calculates all features of a given amino acid sequence
:param sequence: amino acid sequence
:return: array of features
"""
hp_factor = calculate_hydrophobicity(sequence)
length_factor = len(sequence)
return [hp_factor, length_factor]
def is_transmembrane_helix(helix):
"""
Predicts if a helix is transmembrane or not
:param helix: the helix of interest (sequence)
:return: True or False
"""
clf = joblib.load("tmh_predictor_final.pkl")
feats = calculate_features(helix.sequence)
return clf.predict([feats])
def normalize(vector):
"""
normalizes a vector
:param vector:
:return:
"""
length = np.sqrt(vector[0] ** 2 + vector[1] ** 2 + vector[2] ** 2)
return vector / length