IMP logo
IMP Reference Guide  2.21.0
The Integrative Modeling Platform
graphNode.py
1 """@namespace IMP.spatiotemporal.graphNode
2  Defines the graphNode class. Each node corresponds to a snapshot model.
3  Nodes can be connected to create spatiotemporal models.
4 """
5 import numpy as np
6 import os
7 
8 
9 class graphNode:
10  """A class to represent a node in a spatiotemporal process.
11 
12  Each graphNode contains a list of it's component subcoplexes,
13  an array of scores, a time index and a list of pointers to nodes
14  to which edges coming from this node point.
15  """
16 
17  def __init__(self):
18  """Initialize a node.
19  """
20 
21  self._edges = set()
22  self._scores = []
23  self._time = None
24  self._index = None
25  self._label = None
26  self._components = []
27  self._expected_subcomplexes = []
28 
29  def __repr__(self):
30  """String representation of a graphNode
31  """
32  return ("graphNode(" + ",".join([str(self._time), str(self._label)])
33  + ")")
34 
35  def init_graphNode(self, time, label, scorestr, subcomplexstr,
36  expected_subcomplexes):
37  """Function that initiates a graph node with specific time, label,
38  and expected_subcomplexes. Scores and components are extracted from
39  files named scorestr and subcomplexstr respectively. Returns a single
40  graphNode object.
41 
42  @param time: string, time point in the stepwise temporal process
43  @param label: string, a number label for the graphNode
44  @param scorestr: string, trailing characters at the end of the file
45  with scores for each stage of the spatiotemporal model
46  @param subcomplexstr: string, trailing characters after the subcomplex
47  file, which is a list of subcomplexes included in the given
48  label/time
49  @param expected_subcomplexes: list of all possible subcomplex strings
50  in the model
51  """
52 
53  # Set values input from the function call
54  self.set_time(time)
55  self.set_label(label)
56  self.set_expected_subcomplexes(expected_subcomplexes)
57 
58  # scores is a list of energies, where each entry corresponds to the
59  # score at a given timepoint. It is averaged over all simulations
60  scores_fn = label + '_' + time + scorestr
61  if os.path.exists(scores_fn):
62  scores = np.loadtxt(scores_fn)
63  else:
64  raise Exception("Error!!! Unable to find scores file: "
65  + scores_fn + '\nClosing...')
66  self.add_score(scores.mean())
67 
68  if len(expected_subcomplexes) > 0:
69  with open(label + '_' + time + subcomplexstr, "r") as fh:
70  included_prots = fh.read().splitlines()
71 
73  included_prots, label + '_' + time + subcomplexstr)
74 
75  # Index indexes over all nodes
76  def set_index(self, index):
77  """Set an index to label node's identity.
78  """
79  self._index = index
80 
81  def get_index(self):
82  """Return the node's index.
83  """
84  return self._index
85 
86  # Labels are different states at the same time point
87  def set_label(self, label):
88  """Set an index to label node's identity.
89  """
90  self._label = label
91 
92  def get_label(self):
93  """Return the node's index.
94  """
95  return self._label
96 
97  def get_weight(self):
98  """Return the weight of the node. A weight refers in this case to the
99  sum score of the scores list.
100  """
101  return sum(self._scores)
102 
104  """Return a list of subcomplexes in this node's representation.
105  """
106  return self._components
107 
108  def set_subcomplex_components(self, scs, fn):
109  """Set the list of subcomplex components.
110 
111  Should be one of the standard used components.
112  """
113  for sc in scs:
114  if sc not in self._expected_subcomplexes:
115  raise Exception("Error!!! Did not recognize the subcomplex "
116  "name " + sc + " from config file: " + fn)
117 
118  self._components = scs
119 
120  def set_expected_subcomplexes(self, expected_subcomplexes):
121  """Set the list of possible subcomplex components.
122  Should include all possible components across the entire
123  spatiotemporal model
124  """
125  self._expected_subcomplexes = expected_subcomplexes
126 
127  def add_edge(self, edge):
128  """Add a directed edge to the node.
129 
130  Expects a graphNode object.
131  """
132 
133  if not isinstance(edge, graphNode):
134  raise TypeError("Object " + str(edge) + " is not a graphNode")
135 
136  # add if not already present
137  self._edges.add(edge)
138 
139  def get_edges(self):
140  """Return the list of edges for this node.
141  """
142  return self._edges
143 
144  def set_time(self, time):
145  """Set the time.
146  """
147  self._time = time
148 
149  def get_time(self):
150  """Return the time associated with this node.
151  """
152  return self._time
153 
154  def set_scores(self, scores):
155  """Set the score data for this node.
156 
157  Expects an list of floats which represent the total score array.
158  """
159  self._scores = scores
160 
161  def add_score(self, score):
162  """Update the score list by appending score.
163  """
164  # assert we're getting a float
165  if not isinstance(score, float):
166  raise TypeError("add_score expects a float but got a "
167  + str(type(score)))
168 
169  self._scores.append(score)
170 
171  def get_scores(self):
172  """Return the scores array.
173  """
174  return self._scores
175 
176 
177 def draw_edge(nodeA, nodeB, spatio_temporal_rule):
178  """
179  Draws an edge between graphNode objects nodeA and nodeB. If
180  spatio_temporal_rule, node will only be drawn if the components of nodeA
181  are a subset of the components of nodeB.
182  If spatio_temporal_rule: determines if an edge should be drawn if Node A
183  comes immediately before node B in time and if node B contains node A's
184  components as a subset.
185  Else: draws an edge between nodeA and nodeB regardless.
186 
187  @param nodeA: first graphNode object
188  @param nodeB: second graphNode object
189  @param spatio_temporal_rule: Boolean, whether to consider composition
190  when drawing edges
191  """
192 
193  # assert both are nodes
194  assert isinstance(nodeA, graphNode), str(nodeA) + " is not a graphNode."
195  assert isinstance(nodeB, graphNode), str(nodeB) + " is not a graphNode."
196 
197  # check subcomponents are subsets. All nodes in timestep t are also in t+1
198  if spatio_temporal_rule:
199  if frozenset(nodeA.get_subcomplex_components()).issubset(
200  set(nodeB.get_subcomplex_components())):
201  nodeA.add_edge(nodeB)
202  else:
203  nodeA.add_edge(nodeB)
def set_scores
Set the score data for this node.
Definition: graphNode.py:154
def get_edges
Return the list of edges for this node.
Definition: graphNode.py:139
def get_index
Return the node's index.
Definition: graphNode.py:81
def init_graphNode
Function that initiates a graph node with specific time, label, and expected_subcomplexes.
Definition: graphNode.py:45
def set_subcomplex_components
Set the list of subcomplex components.
Definition: graphNode.py:108
def __repr__
String representation of a graphNode.
Definition: graphNode.py:29
def get_subcomplex_components
Return a list of subcomplexes in this node's representation.
Definition: graphNode.py:103
def set_label
Set an index to label node's identity.
Definition: graphNode.py:87
def add_score
Update the score list by appending score.
Definition: graphNode.py:161
def set_expected_subcomplexes
Set the list of possible subcomplex components.
Definition: graphNode.py:120
def get_scores
Return the scores array.
Definition: graphNode.py:171
def add_edge
Add a directed edge to the node.
Definition: graphNode.py:127
def set_index
Set an index to label node's identity.
Definition: graphNode.py:76
def get_weight
Return the weight of the node.
Definition: graphNode.py:97
def get_label
Return the node's index.
Definition: graphNode.py:92
def get_time
Return the time associated with this node.
Definition: graphNode.py:149
The general base class for IMP exceptions.
Definition: exception.h:48
def draw_edge
Draws an edge between graphNode objects nodeA and nodeB.
Definition: graphNode.py:187
def __init__
Initialize a node.
Definition: graphNode.py:17