forked from jczech/mcellRules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
small_structures.py
800 lines (665 loc) · 29.6 KB
/
small_structures.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
# -*- coding: utf-8 -*-
"""
Created on Wed May 30 11:44:17 2012
@author: proto
"""
from copy import deepcopy
import re
from random import randint
from pyparsing import Word, Suppress, Optional, alphanums, Group, ZeroOrMore
from collections import Counter, defaultdict
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
def parseReactions(reaction):
components = (Word(alphanums + "_") + Optional(Group('~' + Word(alphanums+"_")))
+ Optional(Group('!' + Word(alphanums+'+?'))))
molecule = (Word(alphanums + "_")
+ Optional(Suppress('(') + Group(components) + ZeroOrMore(Suppress(',') + Group(components))
+Suppress(')')))
species = Group(molecule) + ZeroOrMore(Suppress('.') + Group(molecule))
result = species.parseString(reaction).asList()
return result
def readFromString(string):
sp = Species()
reactionList = parseReactions(string)
for molecule in reactionList:
mol = Molecule(molecule[0], '')
if len(molecule) > 1:
for idx in range(1, len(molecule)):
comp = Component(molecule[idx][0], '')
if len(molecule[idx]) > 1:
for idx2 in range(1, len(molecule[idx])):
if molecule[idx][idx2][0] == '~':
comp.addState(molecule[idx][idx2][1])
elif molecule[idx][idx2][0] == '!':
comp.addBond(molecule[idx][idx2][1])
mol.addComponent(comp)
sp.addMolecule(mol)
return sp
class Rule:
def __init__(self, label=''):
self.label = label
self.reactants = []
self.products = []
self.rates = []
self.bidirectional = False
self.actions = []
self.mapping = []
def addReactant(self, reactant):
self.reactants.append(reactant)
def addProduct(self, product):
self.products.append(product)
def addReactantList(self, reactants):
self.reactants.extend(reactants)
def addProductList(self, products):
self.products.extend(products)
def addRate(self, rate):
self.rates.append(rate)
def addMapping(self, mapping):
self.mapping.add(mapping)
def addMappingList(self, mappingList):
self.mapping.extend(mappingList)
def addActionList(self, actionList):
self.actions.extend(actionList)
def __str__(self):
finalStr = ''
if self.label != '':
finalStr += '{0}: '.format(self.label)
arrow = ' <-> ' if self.bidirectional or len(self.rates) > 1 else ' -> '
finalStr += ' + '.join([str(x) for x in self.reactants]) + arrow + ' + '.join([str(x) for x in self.products]) + ' ' + ','.join(self.rates)
return finalStr
class Species:
def __init__(self):
self.molecules = []
self.bondNumbers = []
self.bonds = []
self.identifier = randint(0, 100000)
self.idx = ''
self.compartment = ''
def getBondNumbers(self):
bondNumbers = [0]
for element in self.molecules:
bondNumbers.extend(element.getBondNumbers())
return bondNumbers
def copy(self):
species = Species()
species.identifier = randint(0, 1000000)
for molecule in self.molecules:
species.molecules.append(molecule.copy())
return species
def getMoleculeById(self, idx):
for molecule in self.molecules:
if molecule.idx == idx:
return molecule
def addMolecule(self, molecule, concatenate=False, iteration=1):
if not concatenate:
self.molecules.append(molecule)
else:
counter = 1
for element in self.molecules:
if element.name == molecule.name:
if iteration == counter:
element.extend(molecule)
return
else:
counter +=1
self.molecules.append(molecule)
#self.molecules.append(molecule)
#for element in self.molecules:
# if element.name == molecule.name:
def addCompartment(self, tags):
for molecule in self.molecules:
molecule.setCompartment(tags)
def deleteMolecule(self, moleculeName):
deadMolecule = None
for element in self.molecules:
if element.name == moleculeName:
deadMolecule = element
if deadMolecule == None:
return
bondNumbers = deadMolecule.getBondNumbers()
self.molecules.remove(deadMolecule)
for element in self.molecules:
for component in element.components:
for number in bondNumbers:
if str(number) in component.bonds:
component.bonds.remove(str(number))
def getMolecule(self, moleculeName):
for molecule in self.molecules:
if moleculeName == molecule.name:
return molecule
return None
def getSize(self):
return len(self.molecules)
def getMoleculeNames(self):
return [x.name for x in self.molecules]
def contains(self, moleculeName):
for molecule in self.molecules:
if moleculeName == molecule.name:
return True
return False
def addChunk(self, tags, moleculesComponents, precursors):
'''
temporary transitional method
'''
for (tag, components) in zip (tags, moleculesComponents):
if self.contains(tag):
tmp = self.getMolecule(tag)
else:
tmp = Molecule(tag)
#for element in precursors:
# if element.getMolecule(tag) != None:
# tmp = element.getMolecule(tag)
for component in components:
if tmp.contains(component[0][0]):
tmpCompo = tmp.getComponent(component[0][0])
#continue
else:
tmpCompo = Component(component[0][0])
for index in range(1, len(component[0])):
tmpCompo.addState(component[0][index])
if len(component) > 1:
tmpCompo.addBond(component[1])
if not tmp.contains(component[0][0]):
tmp.addComponent(tmpCompo)
if not self.contains(tag):
self.molecules.append(tmp)
def extend(self, species, update=True):
if(len(self.molecules) == len(species.molecules)):
for (selement, oelement) in zip(self.molecules, species.molecules):
for component in oelement.components:
if component.name not in [x.name for x in selement.components]:
selement.components.append(component)
else:
for element in selement.components:
if element.name == component.name:
element.addStates(component.states, update)
else:
for element in species.molecules:
if element.name not in [x.name for x in self.molecules]:
self.addMolecule(deepcopy(element), update)
else:
for molecule in self.molecules:
if molecule.name == element.name:
for component in element.components:
if component.name not in [x.name for x in molecule.components]:
molecule.addComponent(deepcopy(component), update)
else:
comp = molecule.getComponent(component.name)
for state in component.states:
comp.addState(state, update)
def updateBonds(self, bondNumbers):
newBondNumbers = deepcopy(bondNumbers)
correspondence = {}
intersection = [int(x) for x in newBondNumbers if x in self.getBondNumbers()]
for element in self.molecules:
for component in element.components:
for index in range(0, len(component.bonds)):
if int(component.bonds[index]) in intersection:
if component.bonds[index] in correspondence:
component.bonds[index] = correspondence[component.bonds[index]]
else:
correspondence[component.bonds[index]] = max(intersection) + 1
component.bonds[index] = max(intersection) + 1
#intersection = [int(x) for x in newBondNumbers if x in self.getBondNumbers()]
def append(self, species):
newSpecies = (deepcopy(species))
newSpecies.updateBonds(self.getBondNumbers())
for element in newSpecies.molecules:
self.molecules.append(deepcopy(element))
def sort(self):
"""
Sort molecules by number of components, then number of bonded components, then the negative sum of the bond index, then number
of active states, then string length
"""
self.molecules.sort(key=lambda molecule: (len(molecule.components),
-min([int(y) if y not in ['?', '+'] else 999 for x in molecule.components for y in x.bonds] + [999]),
-len([x for x in molecule.components if len(x.bonds) > 0]),
-len([x for x in molecule.components if x.activeState not in [0, '0']]),
len(str(molecule)),
str(molecule)),
reverse=True)
def __str__(self):
self.sort()
name = ''
moleculeStrings = [x.toString() for x in self.molecules]
if self.compartment != '':
name += '@{0}::'.format(self.compartment)
#name += '.'.join(moleculeStrings)
moleculeStrings = [x if '@{0}'.format(self.compartment) not in x else x[:-len('@{0}'.format(self.compartment))] for x in moleculeStrings ]
name += '.'.join(moleculeStrings)
else:
name += '.'.join(moleculeStrings)
'''
name = name.replace('~', '')
name = name.replace('~', '')
name = name.replace(',', '')
name = name.replace('.', '')
name = name.replace('(', '')
name = name.replace(')', '')
name = name.replace('!', '')
name = name.replace('_', '')
'''
return name
def str2(self):
return '.'.join([x.str2() for x in self.molecules])
def getBondDict(self):
bondDict = defaultdict(list)
bondIdx = 1
for idx, molecule in enumerate(self.molecules):
for idx2, component in enumerate(molecule.components):
for bond in component.bonds:
bondDict[bond].append('M{0}_C{1}'.format(idx, idx2))
return bondDict
def toXML(self, identifier, tab=''):
xmlStr = StringIO.StringIO()
xmlStr.write('{0}<ListOfMolecules>\n'.format(tab))
for idx, molecule in enumerate(self.molecules):
xmlStr.write(molecule.toXML('{0}_M{1}'.format(identifier, idx), tab + '\t'))
xmlStr.write('{0}</ListOfMolecules>\n'.format(tab))
bondDict = self.getBondDict()
xmlStr.write('{0}<ListOfBonds>\n'.format(tab))
for key in bondDict:
xmlStr.write('{0}\t<Bond id="{1}_B{2}" site1="{1}_{3}" site2="{1}_{4}"/>\n'.format(tab, identifier,
key, bondDict[key][0],
bondDict[key][1]))
xmlStr.write('{0}</ListOfBonds>\n'.format(tab))
return xmlStr.getvalue()
def reset(self):
for element in self.molecules:
element.reset()
def toString(self):
return self.__str__()
def extractAtomicPatterns(self, action, site1, site2, differentiateDimers=False):
atomicPatterns = {}
bondedPatterns = {}
reactionCenter = []
context = []
#one atomic pattern for the state, one for the bond
nameCounter = Counter([x.name for x in self.molecules])
nameCounterCopy = Counter([x.name for x in self.molecules])
self.sort()
for molecule in self.molecules:
moleculeCounter = nameCounter[molecule.name] - nameCounterCopy[molecule.name]
nameCounterCopy[molecule.name] -= 1
for component in molecule.components:
if component.activeState != '':
speciesStructure = Species()
# one atomic pattern for the states
speciesStructure.bonds = self.bonds
if differentiateDimers:
moleculeStructure = Molecule(molecule.name + '%{0}'.format(moleculeCounter), molecule.idx)
else:
moleculeStructure = Molecule(molecule.name, molecule.idx)
componentStructure = Component(component.name, component.idx)
componentStructure.addState(component.activeState)
componentStructure.activeState = component.activeState
moleculeStructure.addComponent(componentStructure)
speciesStructure.addMolecule(moleculeStructure)
if componentStructure.idx in [site1, site2] and action == 'StateChange':
reactionCenter.append((speciesStructure))
else:
context.append((speciesStructure))
atomicPatterns[str(speciesStructure)] = speciesStructure
speciesStructure = Species()
#one atomic pattern for the bonds
speciesStructure.bonds = self.bonds
if differentiateDimers:
moleculeStructure = Molecule(molecule.name + '%{0}'.format(moleculeCounter), molecule.idx)
else:
moleculeStructure = Molecule(molecule.name, molecule.idx)
componentStructure = Component(component.name, component.idx)
moleculeStructure.addComponent(componentStructure)
speciesStructure.addMolecule(moleculeStructure)
#atomicPatterns[str(speciesStructure)] = speciesStructure
if len(component.bonds) == 0:
#if component.activeState == '':
atomicPatterns[str(speciesStructure)] = speciesStructure
else:
if component.bonds[0] != '+':
componentStructure.addBond(1)
else:
componentStructure.addBond('+')
if component.bonds[0] not in bondedPatterns:
bondedPatterns[component.bonds[0]] = speciesStructure
elif '+' not in component.bonds[0] or \
len(bondedPatterns[component.bonds[0]].molecules) == 0:
bondedPatterns[component.bonds[0]].addMolecule(moleculeStructure)
if componentStructure.idx in [site1, site2] and action != 'StateChange':
reactionCenter.append((speciesStructure))
elif len(component.bonds) > 0 or component.activeState == '':
context.append((speciesStructure))
for element in bondedPatterns:
atomicPatterns[str(bondedPatterns[element])] = bondedPatterns[element]
reactionCenter = [str(x) for x in reactionCenter
if str(x) in atomicPatterns]
context = [str(x) for x in context if str(x) in atomicPatterns]
return atomicPatterns, reactionCenter, context
def graphVizGraph(self, graph, identifier, layout='LR', options={}):
speciesDictionary = {}
graphName = "%s_%s" % (identifier, str(self))
for idx, molecule in enumerate(self.molecules):
ident = "%s_m%i" %(graphName, idx)
speciesDictionary[molecule.idx] = ident
if len(self.molecules) == 1:
compDictionary = molecule.graphVizGraph(graph, ident, flag=False, options=options)
else:
#s1 = graph.subgraph(name = graphName, label=' ')
compDictionary = molecule.graphVizGraph(graph, ident, flag=False, options=options)
speciesDictionary.update(compDictionary)
for bond in self.bonds:
if bond[0] in speciesDictionary and bond[1] in speciesDictionary:
if layout == 'RL':
graph.add_edge(speciesDictionary[bond[1]], speciesDictionary[bond[0]], dir='none', len=0.1, weight=100)
else:
graph.add_edge(speciesDictionary[bond[0]], speciesDictionary[bond[1]], dir='none', len=0.1, weight=100)
return speciesDictionary
def containsComponentIdx(self, idx, dictionary):
for molecule in self.molecules:
for component in molecule.components:
if component.idx == idx:
return dictionary[idx]
return None
def notContainsComponentIdx(self, idx):
context = []
for molecule in self.molecules:
for component in molecule.components:
if component.idx not in idx:
context.append(component)
return context
def hasWildCardBonds(self):
for molecule in self.molecules:
if molecule.hasWildcardBonds():
return True
return False
def listOfBonds(self, nameDict):
listofbonds = {}
for bond in self.bonds:
mol1 = re.sub('_C[^_]*$', '', bond[0])
mol2 = re.sub('_C[^_]*$', '', bond[1])
if nameDict[mol1] not in listofbonds:
listofbonds[nameDict[mol1]] = {}
listofbonds[nameDict[mol1]][nameDict[bond[0]]] = [(nameDict[mol2], nameDict[bond[1]])]
if nameDict[mol2] not in listofbonds:
listofbonds[nameDict[mol2]] = {}
listofbonds[nameDict[mol2]][nameDict[bond[1]]] = [(nameDict[mol1], nameDict[bond[0]])]
return listofbonds
class Molecule:
def __init__(self, name, idx):
self.components = []
self.name, self.idx = name, idx
self.compartment = ''
self.uniqueIdentifier = randint(0, 100000)
def copy(self):
molecule = Molecule(self.name, self.idx)
for element in self.components:
molecule.components.append(element.copy())
return molecule
def addChunk(self, chunk):
component = Component(chunk[0][0][0][0])
component.addState(chunk[0][0][0][1])
self.addComponent(component)
def addComponent(self, component, overlap=0):
if not overlap:
self.components.append(component)
else:
if not component.name in [x.name for x in self.components]:
self.components.append(component)
else:
compo = self.getComponent(component.name)
for state in component.states:
compo.addState(state)
def setCompartment(self, compartment):
self.compartment = compartment
def getComponentById(self, idx):
for component in self.components:
if component.idx == idx:
return component
def getBondNumbers(self):
bondNumbers = []
for element in self.components:
bondNumbers.extend([int(x) for x in element.bonds if x != '+'])
return bondNumbers
def getComponent(self, componentName):
for component in self.components:
if componentName == component.getName():
return component
def removeComponent(self, componentName):
x = [x for x in self.components if x.name == componentName]
if x != []:
self.components.remove(x[0])
def removeComponents(self, components):
for element in components:
if element in self.components:
self.components.remove(element)
def addBond(self, componentName, bondName):
bondNumbers = self.getBondNumbers()
while bondName in bondNumbers:
bondName += 1
component = self.getComponent(componentName)
component.addBond(bondName)
def getComponentWithBonds(self):
return [x for x in self.components if x.bonds != []]
def contains(self, componentName):
return componentName in [x.name for x in self.components]
def __str__(self):
self.components = sorted(self.components, key = lambda st:st.name)
finalStr = self.name
if len(self.components) > 0:
finalStr += '(' + ','.join([str(x) for x in self.components]) + ')'
if self.compartment != '':
finalStr += '@' + self.compartment
return finalStr
def toXML(self, identifier, tab):
buffer = StringIO.StringIO()
buffer.write('{2}<Molecule id="{0}" name="{1}">\n'.format(identifier, self.name, tab))
buffer.write('{0}<ListOfComponents>\n'.format(tab))
for idx, component in enumerate(self.components):
buffer.write(component.toXML('{0}_C{1}'.format(identifier, idx), tab + '\t'))
buffer.write('{0}</ListOfComponents>\n'.format(tab))
return buffer.getvalue()
def toString(self):
return self.__str__()
def str2(self):
self.components.sort()
return self.name + '(' + ','.join([x.str2() for x in self.components]) + ')'
def str3(self):
return self.name + '(' + self.components[0].name + ')'
def extend(self, molecule):
for element in molecule.components:
comp = [x for x in self.components if x.name == element.name]
if len(comp) == 0:
self.components.append(deepcopy(element))
else:
for bond in element.bonds:
comp[0].addBond(bond)
for state in element.states:
comp[0].addState(state)
def reset(self):
for element in self.components:
element.reset()
def update(self, molecule):
for comp in molecule.components:
if comp.name not in [x.name for x in self.components]:
self.components.append(deepcopy(comp))
def graphVizGraph(self, graph, identifier, components=None, flag=False, options={}):
moleculeDictionary = {}
flag = False
'''
if flag:
graph.add_node(identifier, label=self.__str__(), name="cluster%s_%s" % (identifier, self.idx))
print "cluster%s_%s" % (identifier, self.idx)
else:
print identifier
graph.add_node(identifier, label=self.__str__(), name=identifier)
moleculeDictionary[self.idx] = identifier
return moleculeDictionary
'''
if len(self.components) == 0:
graph.add_node(identifier, label=self.name)
moleculeDictionary[self.idx] = identifier
else:
if not flag:
s1 = graph.subgraph(name = "cluster%s_%s" % (identifier, self.idx), label=self.name, **options)
else:
s1 = graph.subgraph(name = identifier, label=self.name, **options)
s1.add_node('cluster%s_%s_dummy' % (identifier, self.idx), shape='point', style='invis')
if components == None:
tmpComponents = self.components
else:
tmpComponents = components
for idx, component in enumerate(tmpComponents):
ident = "%s_c%i" %(identifier, idx)
moleculeDictionary[component.idx] = ident
compDictionary = component.graphVizGraph(s1, ident)
moleculeDictionary.update(compDictionary)
return moleculeDictionary
def hasWildcardBonds(self):
for component in self.components:
if component.hasWilcardBonds():
return True
return False
def distance(self, cMolecule):
distance = 0
distance += 10000 if self.name != cMolecule.name else 0
for component1, component2 in zip(self.components, cMolecule.components):
distance += not component1.bonds == component2.bonds
distance += not component1.activeState == component2.activeState
return distance
def compare(self, cMolecule):
self.components = sorted(self.components, key=lambda st:st.name)
cMolecule.components = sorted(cMolecule.components, key=lambda st:st.name)
for c1, c2 in zip(self.components, cMolecule.components):
if c1.activeState != c2.activeState:
c1.activeState = ''
if c1.bonds != c2.bonds:
'''
if len(c1.bonds) != len(c2.bonds) or '?' in c1.bonds or '?' in c2.bonds:
c1.bonds = ['?']
else:
c1.bonds = ['+']
'''
class Component:
def __init__(self, name, idx, bonds = [], states=[]):
self.name, self.idx = name, idx
self.states = []
self.bonds = []
self.activeState = ''
def copy(self):
component = Component(self.name, self.idx, deepcopy(self.bonds), deepcopy(self.states))
component.activeState = deepcopy(self.activeState)
return component
def addState(self, state, update=True):
if not state in self.states:
self.states.append(state)
if update:
self.setActiveState(state)
def addStates(self, states, update=True):
for state in states:
if state not in self.states:
self.addState(state, update)
def addBond(self, bondName):
if not bondName in self.bonds:
self.bonds.append(bondName)
def setActiveState(self, state):
if state not in self.states:
return False
self.activeState = state
return True
def getRuleStr(self):
tmp = self.name
if self.activeState != '':
tmp += '~' + self.activeState
if len(self.bonds) > 0:
tmp += '!' + '!'.join([str(x) for x in self.bonds])
return tmp
def getTotalStr(self):
return self.name + '~'.join(self.states)
def getName(self):
return self.name
def __str__(self):
return self.getRuleStr()
def str2(self):
tmp = self.name
if len(self.bonds) > 0:
tmp += '!' + '!'.join([str(x) for x in self.bonds])
if len(self.states) > 0:
tmp += '~' + '~'.join([str(x) for x in self.states])
return tmp
def toXML(self, identifier, tab):
if self.activeState != '':
return '{0}<Component id="{1}" name="{2}" state="{3}" numberOfBonds="{4}"/>\n'.format(tab, identifier,
self.name, self.activeState, len(self.bonds))
else:
return '{0}<Component id="{1}" name="{2}" state="{3}" numberOfBonds="{4}"/>\n'.format(tab, identifier,
self.name, self.activeState, len(self.bonds))
def __hash__(self):
return self.name
def reset(self):
self.bonds = []
if '0' in self.states:
self.activeState = '0'
def hasWilcardBonds(self):
if '+' in self.bonds:
return True
return False
def graphVizGraph(self, graph, identifier):
compDictionary = {}
if len(self.states) == 0:
graph.add_node(identifier, label=self.name)
else:
s1 = graph.subgraph(name="cluster%s_%s" % (identifier, self.idx), label=self.name)
if self.activeState != '':
ident = "%s" % (identifier)
compDictionary[self.activeState] = ident
s1.add_node(ident, label = self.activeState)
else:
for idx, state in enumerate(self.state):
ident = "%s_s%i" % (identifier, idx)
s1.add_node(ident, label = state)
compDictionary[state] = ident
return compDictionary
# def createGraph(self, identifier):
# return component, compDictionary
def __lt__(self, other):
return str(self) < str(other)
class States:
def __init__(self, name='', idx=''):
self.name = name
self.idx = idx
class Action:
def __init__(self):
self.action = ''
self.site1 = ''
self.site2 = ''
def setAction(self, action, site1, site2=''):
self.action = action
self.site1 = site1
self.site2 = site2
def __str__(self):
return '%s, %s, %s' % (self.action, self.site1, self.site2)
class Databases:
def __init__(self):
self.translator = {}
self.synthesisDatabase = {}
self.catalysisDatabase = {}
self.rawDatabase = {}
self.labelDictionary = {}
self.synthesisDatabase2 = {}
def getRawDatabase(self):
return self.rawDatabase
def getLabelDictionary(self):
return self.labelDictionary
def add2LabelDictionary(self, key, value):
temp = tuple(key)
temp = temp.sort()
self.labelDictionary[temp] = value
def add2RawDatabase(self, rawDatabase):
pass
def getTranslator(self):
return self.translator
if __name__ == "__main__":
sp = readFromString('A(b!1,p~P).B(a!1)')
print(sp.toXML('S1', '\t'))