File size: 6,134 Bytes
9f39a5d | 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 165 166 167 168 | """
.. codeauthor:: Zander Harteveld <zandermilanh@gmail.com>
.. codeauthor:: Jaume Bonet <jaume.bonet@gmail.com>
.. affiliation::
Laboratory of Protein Design and Immunoengineering <lpdi.epfl.ch>
Bruno Correia <bruno.correia@epfl.ch>
.. class:: FakeForm
"""
# Standard Libraries
import copy
# External Libraries
import numpy as np
import pandas as pd
# This Library
class FakeForm( object ):
"""docstring for FakeForm"""
def __init__(self, sslist):
self.sslist = copy.deepcopy(sslist)
self.id = "_".join(x.desc for x in self.sslist)
self.turn = [0 for x in sslist]
self.do = 0
self.edges = 0
self.direct = 0
self.inter = 0
def evaluate(self):
self.edges = 1 if self._expected_edges() else 0
self.direct = 1 if self._expected_directions() else 0
if self.direct == 0:
self._alternate_regardless()
self.inter = 1 if self._expected_intersection() else 0
evals = [self.edges, self.direct, self.inter]
self.do = sum(evals) == len(evals)
def not_evaluate(self):
self.edges = 1 if self._expected_edges() else 0
self.direct = 1 if self._expected_directions() else 0
if self.direct == 0:
self._alternate_regardless()
self.inter = 1 if self._expected_intersection() else 0
evals = [self.edges, self.direct, self.inter]
self.do = True
def to_json(self):
return { "id": self.id, "do": self.do, "up": self.turn,
"obeys": {"edges": self.edges,
"directions": self.direct,
"intersections": self.inter
} }
def get_ss_by_id(self, name):
for x in self.sslist:
if x.desc == name:
return x
def _alternate_regardless(self):
direct = [x.struc.up_is_1() for x in self.sslist]
directpre = direct[0]
for x in range(1, len(direct)):
if direct[x] == directpre:
self.sslist[x].struc.invert_direction()
direct[x] = self.sslist[x].struc.up_is_1()
directpre = direct[x]
self.turn = direct
def _expected_directions(self):
fixed = [x.static for x in self.sslist]
direct = [x.struc.up_is_1() for x in self.sslist]
try: # start shift at ss 2
directpre = direct[0]
fixpre = fixed[0]
for x in range(1, len(fixed)):
if direct[x] == directpre:
# 2 consecutive fix in same direction won't work
if fixpre and fixed[x]: return False
if fixed[x]: raise ValueError
self.sslist[x].struc.invert_direction()
direct[x] = self.sslist[x].struc.up_is_1()
directpre = direct[x]
fixpre = fixed[x]
except ValueError: # start shift at ss 1
if fixed[0]: return False
fixpre = fixed[0]
self.sslist[0].struc.invert_direction()
direct[0] = self.sslist[0].struc.up_is_1()
directpre = direct[0]
for x in range(1, len(fixed)):
if direct[x] == directpre:
if fixed[x]: return False
self.sslist[x].struc.invert_direction()
direct[x] = self.sslist[x].struc.up_is_1()
directpre = direct[x]
fixpre = fixed[x]
self.turn = direct
return True
def _expected_edges(self):
edges = 0
if self.sslist[0].edge == -1 or self.sslist[-1].edge == -1: return False
for x in self.sslist:
if x.edge == 1: edges += 1
if edges > 2: raise AttributeError("More than two edge structures is not possible")
if edges != 0 and self.sslist[0].edge + self.sslist[-1].edge != edges: return False
return True
def _expected_intersection(self):
dU = []
dD = []
up = 1 if self.sslist[0].struc.goes_up() else -1
for x in range(len(self.sslist) - 1):
if up == 1: dU.append((self.sslist[x], self.sslist[x + 1]))
if up == -1: dD.append((self.sslist[x], self.sslist[x + 1]))
up *= -1
if len(dU) > 1:
for x in dU:
for y in dU:
if x < y and self._intersection(x, y, "up"): return False
if len(dD) > 1:
for x in dD:
for y in dD:
if x < y and self._intersection(x, y, "down"): return False
return True
def _intersection(self, s1, s2, key):
# http://stackoverflow.com/a/20679579/2806632 (first half of function)
def line(p1, p2):
A = (p1[1] - p2[1])
B = (p2[0] - p1[0])
C = (p1[0] * p2[1] - p2[0] * p1[1])
return A, B, -C
def intersection(L1, L2):
D = L1[0] * L2[1] - L1[1] * L2[0]
Dx = L1[2] * L2[1] - L1[1] * L2[2]
Dy = L1[0] * L2[2] - L1[2] * L2[0]
if D != 0:
x = Dx / D
y = Dy / D
return x, y
else:
return False
# http://stackoverflow.com/a/328193/2806632 (second half)
def distance(a, b):
return np.sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2)
def is_between(a, c, b):
return np.isclose(distance(a, c) + distance(c, b), distance(a, b))
L1 = line([s1[0].get_x(key), s1[0].get_z(key)], [s1[1].get_x(key), s1[1].get_z(key)])
L2 = line([s2[0].get_x(key), s2[0].get_z(key)], [s2[1].get_x(key), s2[1].get_z(key)])
R = intersection(L1, L2)
if not R: return False
between1 = is_between([s1[0].get_x(key), s1[0].get_z(key)], R, [s1[1].get_x(key), s1[1].get_z(key)])
between2 = is_between([s2[0].get_x(key), s2[0].get_z(key)], R, [s2[1].get_x(key), s2[1].get_z(key)])
if between1 and between2: return True
else: return False
|