1 from STTools import *
2 import Validate
3 thisPath = os.path.dirname(os.path.abspath(__file__))
4 sys.path.append(os.path.abspath(os.path.join(thisPath,"../..")))
5 import Utils.Libraries.combine as combine
6 import copy
7
9 """
10 Resolves equivalences in place
11 """
12 numOldEvents = 0
13 numNewEvents = 0
14 for doc in documents:
15
16
17 if debug:
18 print "Document:", doc.id
19 numOldEvents += len(doc.events)
20
21 rootEvents = getRoots(doc)
22
23 newEvents = []
24
25
26
27 duplDict = {}
28 for rootEvent in rootEvents:
29 newEvents.extend(duplicateEquiv(rootEvent, duplDict, debug))
30
31 doc.events = rebuildEventList(newEvents)
32 doc.events.sort(key = lambda x: (x.id[0], int(x.id[1:].split(".")[0]), x.id[1:].split(".")[-1]) )
33 numNewEvents += len(doc.events)
34 print >> sys.stderr, "Duplication created", numNewEvents - numOldEvents, "new events (new total", numNewEvents, "events)"
35
37 """
38 Returns topmost events in event hierarchy
39 """
40 eventDict = {}
41 for event in document.events:
42 eventDict[event.id] = event
43 for event in document.events:
44 for arg in event.arguments:
45 if arg[1].id[0] == "E":
46 if arg[1].id in eventDict:
47 del eventDict[arg[1].id]
48 rootEvents = []
49 for key in sorted(eventDict.keys()):
50 rootEvents.append(eventDict[key])
51 return rootEvents
52
54 """
55 Return the arguments of a nested event tree in a depth-first order.
56 Each argument is in a list, and has multiple items if that argument
57 is a protein with Equivs.
58 """
59 hasEquiv = False
60 for arg in event.arguments:
61 if len(arg[1].equiv) == 0:
62 argList.append([arg[1]])
63 else:
64 hasEquiv = True
65 argList.append([arg[1]] + arg[1].equiv)
66 assert arg[1] not in arg[1].equiv
67 if arg[1].id[0] == "E":
68 assert len(arg[1].equiv) == 0
69 if hasNestedEquivs(arg[1]):
70 rv = getArgs(arg[1], argList)
71 hasEquiv = rv or hasEquiv
72 else:
73 pass
74 return hasEquiv
75
77 rv = False
78 for arg in event.arguments:
79 if len(arg[1].equiv) != 0:
80 rv = True
81 elif arg[1].id[0] == "E":
82 rv = rv or hasNestedEquivs(arg[1])
83 return rv
84
85 -def makeEvent(model, argCombination, count, newEvent = None, finished=False, duplDict=None, debug=False, level=0):
86 """
87 Given an argument list in depth-first order (argCombination), make
88 a copy of "model" event tree.
89 """
90 createdEvents = []
91 if newEvent == None:
92 if debug:
93 print "Arg.Comb.:", argCombination
94
95 newEvent = Annotation()
96 newEvent.trigger = model.trigger
97 newEvent.type = model.type
98 newEvent.id = model.id + ".d" + str(count)
99 newEvent.speculation = model.speculation
100 newEvent.negation = model.negation
101 createdEvents.append(newEvent)
102 for arg in model.arguments:
103
104 if arg[1].id[0] != "E":
105
106 if not finished:
107 newEvent.arguments.append([arg[0], argCombination[0], arg[2]])
108 argCombination.pop(0)
109 if debug: print level * " ", "SIMP", model.id, [x[1].id for x in model.arguments], "/", arg[1].id, argCombination, "/", newEvent, newEvent.arguments
110 else:
111 assert arg[2] == None, (model.id, arg)
112 if not finished:
113 if hasNestedEquivs(arg[1]):
114
115 duplId = arg[1].id + ".d" + str(count)
116
117 if duplId not in duplDict:
118 newArg = [arg[0], copy.copy(argCombination[0]), None]
119 createdEvents.append(newArg[1])
120 argCombination.pop(0)
121 newArg[1].arguments = []
122 newArg[1].id = duplId
123 newEvent.arguments.append(newArg)
124 duplDict[duplId] = newArg[1]
125 if debug: print level * " ", "NEST(new)", model.id, [x[1].id for x in model.arguments], "/", arg[1].id, argCombination, "/", newEvent, newEvent.arguments
126 createdEvents += makeEvent(arg[1], argCombination, count, newArg[1], finished, duplDict, level=level+1, debug=debug)
127 else:
128 newArg = [arg[0], duplDict[duplId], None]
129 argCombination.pop(0)
130
131 newEvent.arguments.append(newArg)
132 if debug: print level * " ", "NEST(old)", model.id, [x[1].id for x in model.arguments], "/", arg[1].id, argCombination, "/", newEvent, newEvent.arguments
133
134 createdEvents += makeEvent(arg[1], argCombination, count, duplDict[duplId], True, duplDict, level=level+1, debug=debug)
135 else:
136 newArg = [arg[0], argCombination[0], None]
137 argCombination.pop(0)
138 newEvent.arguments.append(newArg)
139 if debug: print level * " ", "STOP", model.id, [x[1].id for x in model.arguments], "/", arg[1].id, argCombination, "/", newEvent, newEvent.arguments
140
141
142 return createdEvents
143
145 """
146 If the event (event tree) has arguments which have Equiv-statements, create a new event
147 for each combination. Otherwise, return just the existing event.
148 """
149 argList = []
150 hasEquiv = getArgs(event, argList)
151 if not hasEquiv:
152 return [event]
153 if debug:
154 print "----------------------------------------------"
155 print "Event:", event.id, event.type, event.arguments
156 print " Orig. Duplicates:", argList
157 combinations = combine.combine(*argList)
158 if debug:
159 print " Dup. Combinations:", combinations
160 newEvents = []
161 count = 0
162 for combination in combinations:
163 createdEvents = makeEvent(event, combination, count, duplDict=duplDict, debug=debug)
164 newEvent = createdEvents[0]
165 if debug:
166 for createdEvent in createdEvents:
167 if createdEvent == newEvent:
168 print " New Event (root):", createdEvent.id, createdEvent.type, createdEvent.arguments
169 else:
170 print " New Event:", createdEvent.id, createdEvent.type, createdEvent.arguments
171 Validate.validate([createdEvent], simulation=True)
172 newEvents.append(newEvent)
173 count += 1
174 return newEvents
175
177 """
178 Add all events (top and nested) from event trees to a list.
179 """
180 if eventList == None:
181 eventList = []
182 for event in events:
183 if event not in eventList:
184 eventList.append(event)
185 for arg in event.arguments:
186 if arg[1].id[0] == "E":
187 rebuildEventList([arg[1]], eventList)
188 return eventList
189
190 if __name__=="__main__":
191 import sys
192 from optparse import OptionParser
193
194 try:
195 import psyco
196 psyco.full()
197 print >> sys.stderr, "Found Psyco, using"
198 except ImportError:
199 print >> sys.stderr, "Psyco not installed"
200
201 optparser = OptionParser(description="Resolve annotated equivalences")
202 optparser.add_option("-i", "--input", default=None, dest="input", help="", metavar="FILE")
203 optparser.add_option("-o", "--output", default=None, dest="output", help="")
204 optparser.add_option("-d", "--debug", default=False, action="store_true", dest="debug", help="")
205 (options, args) = optparser.parse_args()
206
207 print >> sys.stderr, "Loading documents from", options.input
208 documents = loadSet(options.input)
209 print >> sys.stderr, "Resolving equivalences"
210 process(documents, debug=options.debug)
211 print >> sys.stderr, "Writing documents to", options.output
212 writeSet(documents, options.output, validate=False)
213