-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuildSomeTrees.py
154 lines (130 loc) · 4.96 KB
/
buildSomeTrees.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
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
import numpy as np
from numpy import *
import decimal
import functions as f
from testing import *
import numpy
# Source : kdtreeCOPY.py ( equivalent to scipy.spatial.kdtree )
# Use: Help build KD_Tree
class KD_Tree(object):
"""
Binary Heap
"""
def __init__(self, observer, resN, atoms, distLimit="", K=1):
# Construct tree
self.tree = self._construct(observer, resN, distLimit, atoms, K)
self.children = 0
class Node(object):
def __init__(self, value, dist, LEFT=None, RIGHT=None):
self.data = value
self.left = LEFT
self.right = RIGHT
self.distance = dist
def _insert(self, new, top):
if top == None:
self.children += 1
return new
elif top.left == None and top.right == None:
if new.distance <= top.distance:
self.children += 1
top.left = new
else:
self.children += 1
top.right = new
else:
if new.distance <= top.distance:
return self._insert(new, top.left)
else:
return self._insert(new, top.right)
# def _remove(self, top):
# # assume (for now) tree is balanced
# if self.children <= 1 or top.right == None:
# if top == None: # tree = 0
# raise Warning
# elif top.left == None and top.right == None: # tree = 1
# return None
# else: # tree has no right
# return top.left
# else:
# grandparent = top
# while grandparent.right.right != None:
# grandparent = grandparent.right
# grandparent.right = None
# return top
def _update(self, new, K, top):
# assume (for now) tree is balanced
if self.children < K:
raise Warning
else:
grandparent = top
while grandparent.right.right != None:
grandparent = grandparent.right
if new.distance < grandparent.right.distance:
grandparent.right = new
return top
def _construct(self, observer, Res, distLimit, atoms, K):
self.Root = None
for atom in atoms:
if distLimit != "":
if atom.resName == Res:
dist = f.euclideanDistance([observer.x, observer.y, observer.z], [atom.x, atom.y, atom.z])
if dist <= distLimit:
curr = self.Node(atom, observer)
if self.children < K:
self.Root = self._insert(curr, self.Root)
else:
self.Root = self._update(curr, K, self.Root)
return self.Root
# kNN_resNames here to compare code structure
"""
def kNN_resNames(Atoms, K, distance, observer, resname):
# initialize root
neighbors = []
walking = []
# same
for atom in Atoms:
# same
if atom.resName.strip() == resname:
# same
d = euclideanDistance((atom.x, atom.y, atom.z), (observer.x, observer.y, observer.z))
# same
if d <= distance:
# compare children to K
if len(neighbors) < K:
# _insert
neighbors.append(atom.name)
walking.append(d)
# compare children to K
elif len(neighbors) == K:
# _update:
# temp: top
max = d
marker = K+1
# while loop moving only to right of tree structure until temp's right is None
for PO in range(len(walking)):
if max < walking[PO]:
marker = PO
max = walking[PO]
if marker < K+1:
neighbors[marker] = atom.name
walking[marker] = d
# return tree
return neighbors
"""
def treeTesting():
pdbID = raw_input("Enter a pdb code: ")
path = urllib.urlretrieve('http://files.rcsb.org/download/%s.pdb' % pdbID,
'C:/Users/Brianna/Downloads/proteinDATA/%s.pdb' % pdbID)
try:
file = 'C:/Users/blm7643/Downloads/proteinDATA/%s.pdb' % pdbID
pdbData = readFile(file)
except IOError:
file = '%s.pdb' % pdbID
pdbData = readFile(file)
Atoms = grabStandardAtomsFromPDB(pdbData)
desiredAtoms = getSpecificAtom("CB", "GLU", Atoms)
distance = 11.14
constraint = ["SG", "CB", "CA", "C", "N", "O"]
constraint.sort()
K = len(constraint)
treeTesting()