1 """
2 BIANA: Biologic Interactions and Network Analysis
3 Copyright (C) 2009 Javier Garcia-Garcia, Emre Guney, Baldo Oliva
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 """
19
20 import sets
21 import sys
22 from biana.GraphUtilities import GraphUtilities
23
24 import time
25 import networkx
26 import copy
27
28
30 """
31 Class that represents a set of user entities.
32 """
33
34 - def __init__(self, id, setIdUserEntity=None, listRelations=None, listLevelSetIdUserEntity=None):
35 """
36 """
37
38 self.id = id
39
40 self.current_level = 0
41
42 self.is_network_created = False
43
44
45 self.uE_tags = {}
46 self.tag_uE = {}
47
48
49 self.uER_tags = {}
50 self.tag_uER = {}
51
52
53 self._printed_groups = sets.Set()
54
55
56
57 self.relation_groups = {}
58 self.uE_groups = {}
59 self.groups_hierarchy = {}
60 self.group_types = {}
61
62
63 if listLevelSetIdUserEntity is None:
64 self.current_level = 0
65 if setIdUserEntity is not None:
66 self.size = len(setIdUserEntity)
67 self.listLevelSetIdUserEntity = [ sets.Set(map(None,setIdUserEntity)) ]
68 else:
69 self.size = 0
70 self.listLevelSetIdUserEntity = [ sets.Set() ]
71 else:
72 self.current_level = len(listLevelSetIdUserEntity)-1
73 self.size = 0
74 for setId in listLevelSetIdUserEntity:
75 self.size += len(setId)
76 self.listLevelSetIdUserEntity = listLevelSetIdUserEntity
77
78
79 self.network = GraphUtilities.create_graph()
80
81
82 for current_level in self.listLevelSetIdUserEntity:
83 self.network.add_nodes_from(current_level)
84 if listRelations is not None:
85 self.network.add_edges_from(listRelations)
86
87
88
89
90 self.setIdUserEntitySelected = sets.Set()
91
92
93 self.setIdUserEntityRelationSelected = sets.Set()
94
95 self.userEntityClusters = None
96 self.clusteredNetwork = None
97
98 self.nodeLevelsDict = {}
99
100
101 self.userProvidedExternalIdsDict = {}
102
103
104
105 self.selectedExternalIdsDict = {}
106
107
108 self.userProvidedExtIdsLowerDict = {}
109 self.selectedExtIdsLowerDict = {}
110
111
112
113
114 for current_level_x in xrange(len(self.listLevelSetIdUserEntity)):
115 for current_node in self.listLevelSetIdUserEntity[current_level_x]:
116 self.nodeLevelsDict[current_node] = current_level_x
117
118 self._initialize_restrictions_dict()
119
120
121 self.eErIds2participants = {}
122
123
124
125
126
127
128 return
129
131 """
132 """
133
134 self.restrictions_dict = { "negative_attribute_restrictions": [],
135 "attribute_restrictions": [],
136 "externaldatabase_restrictions": [],
137 "relation_type_restrictions": [],
138 "relation_attribute_restrictions": {},
139 "use_self_relations": False,
140 "expansionlistrelationtype": [],
141 "expansionlevel": [],
142 "expansionattributeslist": [],
143 "include_relations_last_level": False,
144 "attributenetworklist" : [],
145 "group_relation_type": []}
146
148 if isinstance(self.restrictions_dict[restriction_type.lower()], list):
149 self.restrictions_dict[restriction_type.lower()].extend(restrictions)
150 elif isinstance(self.restrictions_dict[restriction_type.lower()], dict):
151
152 for attr, setValue in restrictions:
153 self.restrictions_dict[restriction_type.lower()].setdefault(attr, sets.Set()).union(setValue)
154 else:
155 print "Warning: trying to add a restriction in non-supported format"
156
157
159 """
160 """
161 self.restrictions_dict[restriction_type.lower()] = restriction
162
163
165 return self.restrictions_dict[restriction_type.lower()]
166
167
169 """
170 Adds a tag to selected user entities in this set
171 """
172
173 if not self.tag_uE.has_key(tag):
174 self.tag_uE[tag] = sets.Set()
175
176 if( len(self.setIdUserEntitySelected)==0 ):
177 print "Adding a tag to a set without any user entity selected"
178
179 self.tag_uE[tag].update(self.setIdUserEntitySelected)
180
181 [ self.uE_tags.setdefault(x, sets.Set()).add(tag) for x in self.setIdUserEntitySelected ]
182
183
185 """
186 Adds a tag to selected user entities in this set
187 """
188
189 if not self.tag_uER.has_key(tag):
190 self.tag_uER[tag] = sets.Set()
191
192 if( len(self.setIdUserEntityRelationSelected)==0 ):
193 print "Adding a tag to a set without any user entity relation selected"
194
195 self.tag_uER[tag].update(self.setIdUserEntityRelationSelected)
196
197 [ self.uER_tags.setdefault(x, sets.Set()).add(tag) for x in self.setIdUserEntityRelationSelected ]
198
199
201 """
202 Calculates the linkage degree of all nodes to "tag"
203
204 returns a dictionary node ids as keys and linkage degree as value
205 """
206
207 if not self.tag_uE.has_key(tag):
208 sys.stder.write("Tag %s is not defined" %tag)
209 return
210
211 tld = {}
212 for current_node in self.network.nodes():
213 value = 0
214 for current_neighbor in self.network.neighbors(current_node):
215 if current_neighbor!=current_node:
216 if self.has_tag( userEntityID = current_neighbor, tag = tag ):
217 value += 1
218 tld[current_node] = value
219
220 return tld
221
223 """
224 Get user entities having a linker degree equal or higher to a given cutoff.
225 """
226 result_set = sets.Set()
227 seed_set = self.listLevelSetIdUserEntity[0]
228
229 for seed in seed_set:
230 for neighbor in self.network.neighbors(seed):
231
232 if neighbor in result_set:
233 continue
234 elif len(sets.Set(self.network.neighbors(neighbor)) & seed_set) >= linker_degree_cutoff:
235 result_set.add(neighbor)
236 return result_set
237
239 """
240 Calculates a secondary linkage degree. It is defined as the mean of the linkage degree of the neighbors plus the node LD
241 """
242
243 if not self.tag_uE.has_key(tag):
244 sys.stder.write("Tag %s is not defined" %tag)
245 return 0
246 value = 0
247 for current_neighbor in self.network.neighbors(userEntityID):
248 if current_neighbor!=userEntityID:
249 value += self.getUserEntityTagLinkageDegree( userEntityID= current_neighbor, tag= tag)
250 return ((float(value)/self.get_node_degree(userEntityID))+self.getUserEntityTagLinkageDegree(userEntityID=userEntityID, tag=tag))
251
252
254 if not self.tag_uE.has_key(tag):
255 sys.stder.write("Tag %s is not defined" %tag)
256 return 0
257 value = 0
258 for current_neighbor in self.network.neighbors(userEntityID):
259 if current_neighbor!=userEntityID:
260 if self.has_tag( userEntityID = current_neighbor, tag = tag ):
261 value += 1
262 return value
263
264
266 """
267 Returns an undirected weighted networkx graph containing only nodes in listUserEntityID
268
269 Nodes are connected if exist a path between them, and weight of the edge consists in the length of shortest path
270
271 """
272
273 return GraphUtilities.get_path_network(self.network, listNodes = listUserEntityID, path_length_cutoff=10000 )
274
275
277 if not self.tag_uE.has_key(tag):
278 sys.stder.write("Tag %s is not defined" %tag)
279 raise ValueError("Tag not defined")
280
281 return [ self.getUserEntityTagConnectedMetrics(x,tag) for x in userEntityIDList ]
282
283
285
286 itime = time.time()
287 for current_userEntityID in userEntityIDList:
288 p = networkx.single_source_shortest_path(self.network, current_userEntityID)
289 for current_connected in p:
290 self.calculated_shortest_paths[(max(current_userEntityID,current_connected),min(current_userEntityID,current_connected))] = p[current_connected]
291
292
293 print "All shortest distances calculated in %s seconds" %(time.time()-itime)
294 print "Len: ",len(self.calculated_shortest_paths)
295
296
298 """
299 Test method
300 """
301
302
303
304
305
306
307
308 if self.get_node_degree(userEntityID)==0:
309 return (0,0,0,10000,0,0,0,0,0,'nan', 'nan', 'nan', 'nan', 'nan')
310
311 num_connected = 0
312 sum_path_connected = 0
313 hub_path_weight = 0
314
315 value = 0
316 min_path = 10000
317 max_path = 0
318
319 direct_path_connected = 0
320 sum_path_direct_path_connected = 0
321 max_path_direct_path_connected = 0
322 hub_path_weight_direct_path_connected = 0
323
324 for current_tagged in self.get_user_entities_for_tag( tag = tag ):
325
326 if current_tagged == userEntityID:
327 continue
328
329
330 path = self.getShortestPathBetween( idUserEntity1 = userEntityID,
331 idUserEntity2 = current_tagged )
332
333
334
335
336 if path:
337
338 num_connected += 1
339
340 sum_path_connected += len(path)-1
341
342 current_hub_value = 1
343
344 connected_directly = True
345
346 for current_path_component in path:
347 current_hub_value *= 1.0/len(self.network.neighbors(current_path_component))
348 if connected_directly == True:
349 if current_path_component != userEntityID and current_path_component != current_tagged:
350 if self.has_tag( userEntityID = current_path_component, tag = tag ):
351 connected_directly = False
352
353 if connected_directly:
354 direct_path_connected += 1
355 sum_path_direct_path_connected += len(path)-1
356 hub_path_weight_direct_path_connected += current_hub_value
357 max_path_direct_path_connected = max(len(path)-1,max_path_direct_path_connected)
358
359 hub_path_weight += current_hub_value
360
361 min_path = min(min_path,len(path)-1)
362 max_path = max(max_path,len(path)-1)
363
364
365
366 if num_connected>0:
367 if direct_path_connected>0:
368 return (num_connected, sum_path_connected, hub_path_weight, min_path, max_path, direct_path_connected, sum_path_direct_path_connected, max_path_direct_path_connected, hub_path_weight_direct_path_connected, float(sum_path_connected)/num_connected, hub_path_weight/num_connected, sum_path_direct_path_connected/direct_path_connected, hub_path_weight_direct_path_connected/direct_path_connected, direct_path_connected/num_connected )
369 else:
370 return (num_connected, sum_path_connected, hub_path_weight, min_path, max_path, direct_path_connected, sum_path_direct_path_connected, max_path_direct_path_connected, hub_path_weight_direct_path_connected, float(sum_path_connected)/num_connected, hub_path_weight/num_connected, 'nan', 'nan', direct_path_connected/num_connected)
371 else:
372 return (num_connected, sum_path_connected, hub_path_weight, min_path, max_path, direct_path_connected, sum_path_direct_path_connected, max_path_direct_path_connected, hub_path_weight_direct_path_connected, 'nan', 'nan', 'nan', 'nan', 'nan')
373
375 return self.network.has_node(user_entity_id)
376
377
378 - def has_tag(self, userEntityID, tag):
379 if self.uE_tags.has_key(userEntityID):
380 if tag in self.uE_tags[userEntityID]:
381 return True
382 return False
383
384
386 """
387 userEntityRelationID is a tuple (userEntityRelationA, userEntityRelationB) where userEntityRelationA <= userEntityRelationB
388 """
389 if self.uER_tags.has_key(userEntityRelationID):
390 if tag in self.uER_tags[userEntityRelationID]:
391 return True
392 return False
393
395 return self.tag_uE.has_key(tag)
396
398 return self.tag_uER.has_key(tag)
399
402
403
407
409 return self.tag_uE[tag]
410
411
413 return self.tag_uER[tag]
414
417
420
422 """
423 Returns the groups in which this user entity belongs
424 """
425 if self.uE_groups.has_key(userEntityID):
426 return self.uE_groups[userEntityID]
427 return sets.Set()
428
430 """
431 Returns the user entities belonging to this group
432 """
433 if self.relation_groups.has_key(group_id):
434 return self.relation_groups[group_id]
435 return sets.Set()
436
438 """
439 Returns a collection of the ids of the groups (they should be external entity ids)
440 """
441 return self.relation_groups.keys()
442
443
445 return self.relation_groups.has_key(group_id)
446
447
449 return self.current_level
450
452 return self.network.number_of_nodes()
453
455 return self.network.number_of_edges()
456
459
462
464 """
465 """
466 self.is_network_created = True
467
469 """
470 Checks if is_network_created is True or False #Checks if this user entity set contains any edge (so, if it contains any edge)
471 """
472
473
474
475
476 return self.is_network_created
477
478
487
489 """
490 """
491 for current_tag in tagList:
492 if self.relationHasTag(current_tag):
493 self.selectUserEntityRelations( idUserEntityRelationList = self.get_user_entity_relations_for_tag( tag = current_tag) )
494
496 """
497 Reverses the current selection of user entity relations
498 """
499
500
501
502
503
504
505
506
507
508 all_relations = sets.Set()
509
510 for edge in self.network.edges_iter():
511 all_relations.update(self.network.get_edge(edge[0],edge[1]))
512
513 self.setIdUserEntityRelationSelected = all_relations.difference(self.setIdUserEntityRelationSelected)
514
515
517 """
518 Selects the direct relations between the user entities belonging to the two lists
519
520 For example, an interaction between a user entity in list 1 with an user entity in list 2 will be selected
521
522 An interaction between two user entities in list 1 won't be selected
523
524 "types" is a list of the relation types to be selected
525 """
526
527 selected_relations = []
528
529 for current_1 in user_entity_ids_list1:
530 for current_2 in user_entity_ids_list2:
531 t = current_1
532 if t > current_2:
533 t = current_2
534 current_2 = current_1
535
536 for current_type in types_list:
537 rel_id = (current_1,current_2)
538 if self.network.has_edge(rel_id):
539 selected_relations.append((current_1,current_2,current_type))
540
541 self.selectUserEntityRelations(selected_relations)
542
544 """
545 Selects a user entity in this user entity set
546
547 User entities in the list must exist in the set!!!
548 """
549 for idUserEntity in user_entity_id_list:
550 if self.network.has_node(idUserEntity):
551 self.setIdUserEntitySelected.add(idUserEntity)
552 else:
553 sys.stderr.write("Trying to select an unexisting user entity (%s) in user entity set %s" %(idUserEntity,self.id))
554
555
557 """
558 Selects a user entity relation in this user entity set
559
560 "external_entity_relation_id_list": is a list of external entity relation ids to select
561 """
562
563 for current_eErID in external_entity_relation_id_list:
564 if self.eErIds2participants.has_key(current_eErID):
565 self.setIdUserEntityRelationSelected.add(current_eErID)
566 else:
567 sys.stderr.write("Trying to select an unexisting edge: %s\n" %current_eErID)
568
569
571 """
572 """
573 return ( min(node1,node2), max(node1,node2) )
574
575
577 return len(self.setIdUserEntitySelected)
578
579
581 return len(self.setIdUserEntityRelationSelected)
582
583
585 self.setIdUserEntitySelected.clear()
586
587
589 self.setIdUserEntityRelationSelected.clear()
590
591
593 """
594 Adds a user entity to this user entity set to the level "level"
595 """
596
597 if not self.network.has_node(idUserEntity):
598 self.network.add_node(idUserEntity)
599
600 if level<=self.current_level:
601 self.listLevelSetIdUserEntity[level].add(idUserEntity)
602 elif level==self.current_level+1:
603 self.current_level += 1
604 self.listLevelSetIdUserEntity.append(sets.Set([idUserEntity]))
605 else:
606 sys.stderr.write("Cannot add a node in this level")
607 return
608
609 self.size += 1
610
611 self.nodeLevelsDict[idUserEntity] = level
612
613
614
615 return
616
617
619 """
620 "list_hierarchy" is a list [(group_id1, group_id2) in which group_id1 is child of group_id2
621 """
622
623 [ self.groups_hierarchy.setdefault(current_child,current_parent) for (current_child, current_parent) in list_hierarchy ]
624
625
626 - def addUserEntitiesToGroup(self, group_id, userEntityID, representative_userEntityID, group_type, parentGroupID=None):
627 """
628 One of the two user entities must belong to the network
629 "parentGroupID" is used to store groups hierarchy
630 """
631
632 if not self.network.has_node(representative_userEntityID):
633 raise ValueError("Trying to add a user entity to a group with an unexisting representative")
634
635 if not self.network.has_node(userEntityID):
636 self.addUserEntityId(idUserEntity=userEntityID,level = self.getNodeLevel(representative_userEntityID)+1)
637
638 self.uE_groups.setdefault(userEntityID,sets.Set()).add(group_id)
639 self.relation_groups.setdefault(group_id,sets.Set()).add(userEntityID)
640 self.relation_groups.setdefault(group_id,sets.Set()).add(representative_userEntityID)
641 self.group_types.setdefault(group_id,group_type)
642 if parentGroupID is not None:
643 if self.groups_hierarchy.has_key(group_id):
644 print "Group %s has more than one parent?" %(group_id)
645 self.groups_hierarchy.setdefault(group_id,parentGroupID)
646
647
648
649
651 return self.eErIds2participants[external_entity_relation_id]
652
653
655 """
656 returns True if it has been added, or False if it existed previously a relation between idUserEntity1 and idUserEntity2
657 """
658
659
660 if not self.network.has_node(idUserEntity1) and not self.network.has_node(idUserEntity2):
661 raise ValueError("Trying to add a relation between two user entities (%s,%s) and any of them belong to this set" %(idUserEntity1,idUserEntity2))
662
663 if not self.network.has_node(idUserEntity1):
664 self.addUserEntityId(idUserEntity=idUserEntity1, level = self.getNodeLevel(idUserEntity2)+1)
665 elif not self.network.has_node(idUserEntity2):
666 self.addUserEntityId(idUserEntity=idUserEntity2, level = self.getNodeLevel(idUserEntity1)+1)
667
668 if not self.eErIds2participants.has_key(externalEntityRelationID):
669 self.eErIds2participants[externalEntityRelationID] = [idUserEntity1, idUserEntity2]
670 else:
671 lista = self.eErIds2participants[externalEntityRelationID]
672 if idUserEntity1 not in lista:
673 lista.append(idUserEntity1)
674 if idUserEntity2 not in lista:
675 lista.append(idUserEntity2)
676
677
678 if self.network.has_edge(idUserEntity1, idUserEntity2):
679 if externalEntityRelationID not in sets.Set(self.network.get_edge(idUserEntity1, idUserEntity2)):
680 self.network.get_edge(idUserEntity1, idUserEntity2).append(externalEntityRelationID)
681 return True
682 else:
683 self.network.add_edge( idUserEntity1, idUserEntity2, [externalEntityRelationID] )
684 return True
685
686 return False
687
688
690 """
691 """
692 if userEntityID1 <= userEntityID2:
693 userEntityID1, userEntityID2 = userEntityID2, userEntityID1
694
695 return (userEntityID1, userEntityID2)
696
697
699
700 for current_neighbor in self.network.neighbors(nodeID):
701 self.remove_edge(nodeID,current_neighbor)
702 self.network.delete_node(nodeID)
703 level = self.nodeLevelsDict[nodeID]
704 del self.nodeLevelsDict[nodeID]
705 self.listLevelSetIdUserEntity[level].remove(nodeID)
706 self.setIdUserEntitySelected.discard(nodeID)
707 if self.uE_tags.has_key(nodeID):
708 tags = self.uE_tags[nodeID]
709 for current_tag in tags:
710
711 self.tag_uE[current_tag].remove(nodeID)
712 del self.uE_tags[nodeID]
713 self.size -= 1
714
715
717
718 list_selected = list(self.setIdUserEntityRelationSelected)
719
720
721
722 [ self.remove_external_entity_relation(x) for x in list_selected ]
723
724 self.setIdUserEntityRelationSelected.clear()
725
726
728 return self.network.get_edge(user_entity_id1, user_entity_id2)
729
730
732
733 participants = self.eErIds2participants[externalEntityRelationID]
734
735 for current_participant1 in participants:
736 for current_participant2 in participants:
737 if self.network.has_edge(current_participant1, current_participant2):
738 self.remove_edge(current_participant1, current_participant2, externalEntityRelationID)
739
740
741 - def remove_edge(self, nodeID1, nodeID2, externalEntityRelationID=None):
742 """
743 If externalEntityRelationID is None, remove all edges between these two nodes
744 """
745
746 nodeID1, nodeID2 = self._get_user_entity_relation_id(nodeID1, nodeID2)
747
748 eErIds_list = self.network.get_edge(nodeID1, nodeID2)
749
750 if externalEntityRelationID is None:
751 for current_eErID in eErIds_list:
752 self.remove_edge( nodeID1, nodeID2, current_eErID )
753 return
754
755
756
757 number = eErIds_list.count(externalEntityRelationID)
758 if number == 0:
759 sys.stderr.write("Trying to remove an unexisting edge: %s" %externalEntityRelationID)
760 else:
761 [ eErIds_list.remove(externalEntityRelationID) for x in xrange(number) ]
762 if len(eErIds_list)==0:
763 self.network.delete_edge(nodeID1, nodeID2)
764
765 if self.uER_tags.has_key(externalEntityRelationID):
766 for current_tag in self.uER_tags[externalEntityRelationID]:
767 self.tag_uER[current_tag].remove(externalEntityRelationID)
768 del self.uER_tags[externalEntityRelationID]
769
770
771
772
773
774
775
776
777
778 self.setIdUserEntityRelationSelected.discard(externalEntityRelationID)
779
780
781
783
784 for current_node in self.network.nodes():
785 if self.get_node_degree(current_node) == 0:
786 self.remove_node(current_node)
787
788
790 return self.nodeLevelsDict
791
793
794 return self.nodeLevelsDict[nodeID]
795
797 """
798 "level" can be a number with the level or "last"
799 """
800
801 if level is not None:
802 if str(level).lower()=="last":
803 level = self.current_level
804 if level<len(self.listLevelSetIdUserEntity):
805 return list(self.listLevelSetIdUserEntity[level])
806 return None
807 else:
808 return self.network.nodes()
809
810
812 """
813 """
814 set_relations = sets.Set()
815
816 for edge in self.network.edges():
817 set_relations.update(self.network.get_edge(edge[0],edge[1]))
818
819 return set_relations
820
821
823 """
824 o cardinality_check: Boolean. Lowest userEntityId is returned as first element of the tuple; highest as second.
825 """
826
827
828 t = self.network.edges()
829
830
831 if cardinality_check is False: return t
832 arranged_t = []
833
834 for i in t:
835 if cardinality_check is True and not i[0] <= i[1]: arranged_t.append( (i[1], i[0]) )
836
837 return arranged_t
838
840 return self.setIdUserEntitySelected
841
843 return self.setIdUserEntityRelationSelected
844
847
849 """
850 Fetchs interactions between all the user entities provided in the set
851 Returns a set of tuples containing (id1, id2)
852 """
853 nUserEntity = len(setIdUserEntity)
854 listRelations = []
855
856 for edge in self.network.edges_iter(setIdUserEntity):
857 if edge[0] in setIdUserEntity and edge[1] in setIdUserEntity:
858 listRelations.append((edge[0],edge[1],self.network.get_edge(edge[0],edge[1])))
859 return listRelations
860
862 if idUserEntity1<idUserEntity2:
863 idUserEntity2,idUserEntity1 = idUserEntity1, idUserEntity2
864 return self.calculated_shortest_paths.setdefault((idUserEntity1, idUserEntity2),GraphUtilities.get_shortest_path_between(self.network, idUserEntity1, idUserEntity2))
865
868
869
871
872 union_user_entity_ids_set = sets.Set( self.get_user_entity_ids() ).union( sets.Set( objUserEntitySet.get_user_entity_ids() ) )
873
874 listLevelSetId = [ union_user_entity_ids_set ]
875
876 listInteraction = []
877
878 if flagIncludeInteractions:
879
880
881 dict_relations = {}
882 all_edges = sets.Set()
883
884 for node1, node2 in self.network.edges_iter():
885 eErIds_list = self.network.get_edge(node1,node2)
886 dict_relations[self._getRelationHash(node1,node2)] = copy.deepcopy(eErIds_list)
887
888
889 for node1, node2 in objUserEntitySet.network.edges():
890 eErIds_list = objUserEntitySet.network.get_edge(node1,node2)
891 dict_relations.setdefault(self._getRelationHash(node1,node2), []).extend(eErIds_list)
892
893 for key, value in dict_relations.iteritems():
894 listInteraction.append((key[0],key[1],list(sets.Set(value))))
895
896 return (listLevelSetId, listInteraction)
897
898
900 """
901 Interactions for intersection nodes are included if flagIncludeInteractions is selected
902 """
903
904 intersection_user_entity_ids_set = sets.Set( self.get_user_entity_ids() ).intersection( sets.Set( objUserEntitySet.get_user_entity_ids() ) )
905
906 listLevelSetId = [ intersection_user_entity_ids_set ]
907
908 listInteraction = []
909
910 if flagIncludeInteractions:
911 dict_relations = {}
912
913
914 for current_node1, current_node2 in self.network.edges_iter(intersection_user_entity_ids_set):
915 if( objUserEntitySet.network.has_node(current_node1) and objUserEntitySet.network.has_node(current_node2) ):
916 eErIdsList = self.network.get_edge(current_node1, current_node2)
917 dict_relations[self._getRelationHash(current_node1,current_node2)] = copy.deepcopy(eErIdsList)
918
919
920
921 for current_node1, current_node2 in objUserEntitySet.network.edges_iter(intersection_user_entity_ids_set):
922 if( self.network.has_node(current_node1) and self.network.has_node(current_node2) ):
923 eErIdsList = objUserEntitySet.network.get_edge(current_node1, current_node2)
924 dict_relations.setdefault(self._getRelationHash(current_node1,current_node2), []).extend(eErIdsList)
925
926
927 for key, value in dict_relations.iteritems():
928 listInteraction.append((key[0],key[1],list(sets.Set(value))))
929
930
931 return (listLevelSetId, listInteraction)
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
949
950 return self.network.degree(userEntityID)
951
953 return_set = sets.Set()
954 for current_uE in self.get_user_entity_ids(level=0):
955 if self.network.degree(current_uE)==0:
956 return_set.add(current_uE)
957
958 return return_set
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
984 return "%s / %s" % (self.listLevelSetIdUserEntity, self.network.edges())
985
987 str_list = ["User Entity Set %s" %self.id]
988 str_list.append("\tNumber of user entities: %s" %self.network.number_of_nodes())
989 str_list.append("\tNumber of relations: %s" %(self.network.number_of_edges()))
990 str_list.append("\tLevels: %s" %(len(self.listLevelSetIdUserEntity)-1))
991 str_list.append("\tNumber of root entities: %s" %(len(self.listLevelSetIdUserEntity[0])))
992
993 str_list.append("\tRestrictions:")
994 for current_restriction, restrictions in self.restrictions_dict.iteritems():
995 if isinstance(restrictions,list):
996 if len(restrictions)>0:
997 str_list.append("\t\t%s: %s" %(current_restriction, restrictions))
998 else:
999 str_list.append("\t\t%s: No applied" %current_restriction)
1000 else:
1001 str_list.append("\t\t%s: %s" %(current_restriction, restrictions))
1002
1003 return "\n".join(str_list)
1004
1006 return "<user_entity_set id=\"%s\">" %self.id
1007
1010
1015
1017 """
1018 group_identfiers is a dictionary with the id of the node as a key and a name for the node
1019 """
1020
1021 if only_not_printed is True:
1022
1023 toprint = sets.Set(self.relation_groups).difference(self._printed_groups)
1024 else:
1025 toprint = list(self.relation_groups)
1026
1027 xml_list = []
1028
1029 for current_group in toprint:
1030
1031
1032 self._printed_groups.add(current_group)
1033 if self.groups_hierarchy.has_key(current_group):
1034 parent_str = "parentGroupID=\"%s\" parentGroupName=\"%s\"" %(self.groups_hierarchy[current_group],
1035 str(group_identifiers.setdefault(self.groups_hierarchy[current_group],self.groups_hierarchy[current_group])).replace("<","\<").replace(">","\>"))
1036 else:
1037 parent_str = ""
1038
1039 xml_list.append("<add_new_group groupID=\"%s\" groupName=\"%s\" groupType=\"%s\" node_ids=\"%s\" %s/>" %(current_group,str(group_identifiers.setdefault(current_group,current_group)).replace("<","\<").replace(">","\>"),self.group_types[current_group],",".join(map(str,self.relation_groups[current_group])),parent_str))
1040
1041 return "".join(xml_list)
1042
1043
1045 """
1046 Updates self.selectedExternalIdsDict and self.selectedExtIdsLowerDict
1047 """
1048
1049 if selected_ext_id_name is None: return
1050
1051 if not self.selectedExternalIdsDict.has_key(attribute): self.selectedExternalIdsDict[attribute] = sets.Set([])
1052 if not self.selectedExtIdsLowerDict.has_key(attribute): self.selectedExtIdsLowerDict[attribute] = {}
1053 self.selectedExternalIdsDict[attribute].add(selected_ext_id_name)
1054 self.selectedExtIdsLowerDict[attribute][selected_ext_id_name.lower()] = selected_ext_id_name
1055