Package biana :: Package BianaObjects :: Module UserEntitySet
[hide private]
[frames] | no frames]

Source Code for Module biana.BianaObjects.UserEntitySet

   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   
29 -class UserEntitySet(object):
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 # can only be set create_network command of BianaSessionManager showing whether this command has been called or not 43 44 # Tags related 45 self.uE_tags = {} 46 self.tag_uE = {} 47 48 # Relation Tags related 49 self.uER_tags = {} 50 self.tag_uER = {} 51 52 # For sending only interactions fetched with the last expansion to Cytoscape 53 self._printed_groups = sets.Set() 54 55 # Dictionary to store relation groups (relations taken as groups, not as edges) 56 # As key stores the external entity relation ID and as a value a set of user entities ids 57 self.relation_groups = {} 58 self.uE_groups = {} # Stores the groups to which a user entity belongs 59 self.groups_hierarchy = {} 60 self.group_types = {} 61 62 # List that contains the user entities ids at different levels 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 ## Network of relations between the elements of this UserEntitySet 79 self.network = GraphUtilities.create_graph() 80 81 # Add initial nodes and edges 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 #self.network.add_edges_from(listRelations) 87 #self.is_network_created = True 88 89 # Selected user entity ids 90 self.setIdUserEntitySelected = sets.Set() 91 92 # Selected user entity relation ids 93 self.setIdUserEntityRelationSelected = sets.Set() 94 95 self.userEntityClusters = None # Used to cluster user entities according to some criteria. Key: UserEntityID. Cluster: ClusterID 96 self.clusteredNetwork = None 97 98 self.nodeLevelsDict = {} # Dictionary to store the level to which belongs node 99 100 # Output related 101 self.userProvidedExternalIdsDict = {} # Dictionary to store external Ids provided by the user. 102 # Corresponds to dictTypeToName in BianaSessionManager.create_new_user_entity_set() 103 104 105 self.selectedExternalIdsDict = {} # Dictionary to store external Ids selected previously by BianaSessionManager. 106 # It is updated in BianaSessionManager.outputUserEntityObjectsDetails 107 108 self.userProvidedExtIdsLowerDict = {} # Stores correspondence between lower case and user provided codes in self.userProvidedExternalIdsDict 109 self.selectedExtIdsLowerDict = {} # Stores correspondence between lower case and selected codes in self.selectedExtneralIdsDict 110 111 112 # Start the nodeLevelsDict in the case it is not empty 113 # Start the tags dict in the case it is not empty 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 = {} # Stores the participants in each eErID 122 123 124 # TEMP 125 #self.all_shortest_paths = None 126 #self.calculated_shortest_paths = {} 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
147 - def addRestriction(self, restriction_type, restrictions):
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 # assuming that dictionary contains set as values 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
158 - def setRestriction(self, restriction_type, restriction):
159 """ 160 """ 161 self.restrictions_dict[restriction_type.lower()] = restriction
162 163
164 - def getRestrictions(self, restriction_type):
165 return self.restrictions_dict[restriction_type.lower()]
166 167
168 - def addTagToSelectedUE(self, tag):
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
184 - def addTagToSelectedUER(self, tag):
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
200 - def getTagLinkageDegree(self, tag):
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
222 - def get_user_entity_ids_by_linker_degree_cutoff(self, linker_degree_cutoff):
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 #print seed_set 229 for seed in seed_set: 230 for neighbor in self.network.neighbors(seed): 231 #print seed, neighbor, sets.Set(self.network.neighbors(neighbor)) 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
238 - def getUserEntitySecondaryTagLinkageDegree(self, userEntityID, tag):
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
253 - def getUserEntityTagLinkageDegree(self, userEntityID, tag):
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
265 - def getPathLengthNetwork(self, listUserEntityID, path_length_cutoff=10000):
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
276 - def testfast_getUserEntityTagConnectedMetrics(self, userEntityIDList, tag):
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
284 - def _get_single_source_shortest_path(self, userEntityIDList):
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
297 - def getUserEntityTagConnectedMetrics(self, userEntityID, tag):
298 """ 299 Test method 300 """ 301 302 #if not self.tag_uE.has_key(tag): 303 # sys.stder.write("Tag %s is not defined" %tag) 304 # raise ValueError("Tag not defined") 305 306 #itime = time.time() 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 # Number of connected nodes with "tag", but without going though another node with the "tag" 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 #ptime = time.time() 330 path = self.getShortestPathBetween( idUserEntity1 = userEntityID, 331 idUserEntity2 = current_tagged ) 332 #print "shortest_path_time: ",time.time()-ptime 333 334 #ltime = time.time() 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 #print "Loop time: ",time.time()-ltime 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
374 - def has_user_entity(self, user_entity_id):
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
385 - def relation_has_tag(self, userEntityRelationID, tag):
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
394 - def hasTag(self, tag):
395 return self.tag_uE.has_key(tag)
396
397 - def relationHasTag(self, tag):
398 return self.tag_uER.has_key(tag)
399
400 - def get_user_entity_tags(self, user_entity_id):
401 return self.uE_tags.setdefault(user_entity_id, sets.Set())
402 403 #def get_user_entity_relation_tags(self, user_entity_relation_id):
404 - def get_external_entity_relation_tags(self, external_entity_relation_id):
405 #return self.uER_tags.setdefault(user_entity_relation_id, sets.Set()) 406 return self.uER_tags.setdefault(external_entity_relation_id, sets.Set() ) ### ALERT: Possible performance.... it creates unnecessary sets
407
408 - def get_user_entities_for_tag(self, tag):
409 return self.tag_uE[tag]
410 411 #def get_user_entity_relations_for_tag(self, tag):
413 return self.tag_uER[tag]
414
415 - def get_all_user_entity_tags(self):
416 return self.tag_uE.keys()
417
419 return self.tag_uER.keys()
420
421 - def getUserEntityGroups(self, userEntityID):
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
429 - def get_group_user_entities(self, group_id):
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
437 - def get_groups_ids(self):
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
444 - def has_group(self, group_id):
445 return self.relation_groups.has_key(group_id)
446 447
448 - def get_level(self):
449 return self.current_level
450
451 - def getSize(self):
452 return self.network.number_of_nodes()
453
454 - def getNumberEdges(self):
455 return self.network.number_of_edges()
456
457 - def getNetwork(self):
458 return self.network
459
460 - def setNetwork(self, netw):
461 self.network = netw
462
463 - def setIsNetworkCreated(self):
464 """ 465 """ 466 self.is_network_created = True
467
468 - def isNetworkCreated(self):
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 #if self.network.number_of_edges()==0: 473 # return False 474 #else: 475 # return True 476 return self.is_network_created
477 478
479 - def selectUserEntitiesByTag(self, tagList):
480 """ 481 """ 482 for current_tag in tagList: 483 if self.hasTag(current_tag): 484 self.select_user_entities( idUserEntityList = self.get_user_entities_for_tag( tag = current_tag ) ) 485 else: 486 print "%s tag does not exist" %current_tag
487
488 - def selectUserEntityRelationsByTag(self, tagList):
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 #temp_all_relations = self.network.edges() # returns a list of [(interactor1, interactor2)] 501 502 #all_relations = sets.Set() 503 504 #for in temp_all_relations: 505 # for current_type in dictio.keys(): 506 # all_relations.add((int1,int2,current_type)) 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
516 - def select_user_entity_relations_between_two_subsets(self, user_entity_ids_list1, user_entity_ids_list2, types_list):
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
543 - def select_user_entities(self, user_entity_id_list):
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
556 - def selectUserEntityRelations(self, external_entity_relation_id_list):
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
570 - def _get_user_entity_relation_id(self, node1, node2):
571 """ 572 """ 573 return ( min(node1,node2), max(node1,node2) )
574 575
576 - def getSelectionSize(self):
577 return len(self.setIdUserEntitySelected)
578 579
580 - def getRelationSelectionSize(self):
581 return len(self.setIdUserEntityRelationSelected)
582 583
585 self.setIdUserEntitySelected.clear()
586 587
589 self.setIdUserEntityRelationSelected.clear()
590 591
592 - def addUserEntityId(self, idUserEntity, level=0):
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 #else: 614 # print "Warning. Trying to add an existing node: %s" %idUserEntity 615 return
616 617
618 - def setGroupsHierarchy(self, list_hierarchy):
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 #self._not_printed_groups.add(group_id) 648 649
650 - def get_external_entity_relation_participants(self, external_entity_relation_id):
651 return self.eErIds2participants[external_entity_relation_id]
652 653
654 - def addUserEntityRelation(self, idUserEntity1, idUserEntity2, externalEntityRelationID):
655 """ 656 returns True if it has been added, or False if it existed previously a relation between idUserEntity1 and idUserEntity2 657 """ 658 659 # First, it is necessary to add the user entity ids to the correct levels 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
689 - def _getRelationHash(self, userEntityID1, userEntityID2):
690 """ 691 """ 692 if userEntityID1 <= userEntityID2: 693 userEntityID1, userEntityID2 = userEntityID2, userEntityID1 694 695 return (userEntityID1, userEntityID2)
696 697
698 - def remove_node(self, nodeID):
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 #self.tag_uE.remove(current_tag) 711 self.tag_uE[current_tag].remove(nodeID) 712 del self.uE_tags[nodeID] 713 self.size -= 1
714 715
716 - def remove_selected_relations(self):
717 718 list_selected = list(self.setIdUserEntityRelationSelected) # makes a copy of the content because it is modified in the following loop 719 720 #[ self.remove_edge(current_edge[0],current_edge[1],current_edge[2]) for current_edge in list_selected ] 721 722 [ self.remove_external_entity_relation(x) for x in list_selected ] 723 724 self.setIdUserEntityRelationSelected.clear()
725 726
727 - def get_external_entity_relation_ids(self, user_entity_id1, user_entity_id2):
728 return self.network.get_edge(user_entity_id1, user_entity_id2)
729 730
731 - def remove_external_entity_relation(self, externalEntityRelationID):
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: # Possible performance... 751 for current_eErID in eErIds_list: 752 self.remove_edge( nodeID1, nodeID2, current_eErID ) 753 return 754 755 756 # Possible performance problem... we are running a list 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 #self.tag_uER[current_tag].remove(externalEntityRelationID) 771 #if self.uER_tags.has_key((nodeID1,nodeID2,relation_type)): 772 # for current_tag in self.uER_tags[(nodeID1,nodeID2,relation_type)]: 773 # self.tag_uER[current_tag].remove((nodeID1,nodeID2,relation_type)) 774 # del self.uER_tags[(nodeID1,nodeID2,relation_type)] 775 776 777 #self.setIdUserEntityRelationSelected.discard((nodeID1,nodeID2)) 778 self.setIdUserEntityRelationSelected.discard(externalEntityRelationID)
779 780 781
782 - def remove_unconnected_nodes(self):
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
789 - def getNodeLevelDict(self):
790 return self.nodeLevelsDict
791
792 - def getNodeLevel(self, nodeID):
793 794 return self.nodeLevelsDict[nodeID]
795
796 - def get_user_entity_ids(self, level=None):
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
811 - def getRelationIds(self):
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
822 - def getRelations(self, cardinality_check=False):
823 """ 824 o cardinality_check: Boolean. Lowest userEntityId is returned as first element of the tuple; highest as second. 825 """ 826 827 #t = [ (x[0], x[1]) for x in self.network.edges() ] 828 t = self.network.edges() 829 830 # cardinality_check added by Joan 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
839 - def getSelectedUserEntities(self):
840 return self.setIdUserEntitySelected
841
843 return self.setIdUserEntityRelationSelected
844
846 return self.getRelationsBetweenGivenUserEntities(self.setIdUserEntitySelected)
847
848 - def getRelationsBetweenGivenUserEntities(self, setIdUserEntity):
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
861 - def getShortestPathBetween(self, idUserEntity1, idUserEntity2):
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
866 - def getAllPathsFrom(self, idUserEntity):
867 return GraphUtilities.get_all_paths_from(self.network, idUserEntity)
868 869
870 - def getUnionWithGivenUserEntitySet(self, objUserEntitySet, flagIncludeInteractions):
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 # Merge relation ids 881 dict_relations = {} 882 all_edges = sets.Set() 883 #for node1, node2, eErIds_list in self.network.edges(): 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 #for node1, node2, eErIds_list in objUserEntitySet.network.edges(): 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
899 - def getIntersectionWithGivenUserEntitySet(self, objUserEntitySet, flagIncludeInteractions):
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 #for current_node1, current_node2, eErIdsList in self.network.edges_iter(intersection_user_entity_ids_set): 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 #listInteraction.append(current_edge) 919 920 #for current_node1, current_node2, eErIdsList in objUserEntitySet.network.edges_iter(intersection_user_entity_ids_set): 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 #listInteraction.append(current_edge) 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 # def outputUserEntitySet(self, fileName, format): 935 # if format=="sif": 936 # fileOut = open(fileName, 'w') 937 # for actual_node in self.network.nodes(): 938 # if( len(self.network.neighbors(actual_node))==0 ): 939 # fileOut.write("%s\n" %actual_node) 940 # for id1, id2, x in self.network.edges(): 941 # fileOut.write("%d %s %d\n" % (id1, x, id2)) 942 # 943 # elif format=="dot": 944 # networkx.write_dot(self.network, fileName) 945 # return 946 947
948 - def get_node_degree(self, userEntityID):
949 950 return self.network.degree(userEntityID)
951
952 - def get_unconnected_nodes(self):
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 # def outputNetwork(self, outmethod, format): 962 # if format == "sif": 963 # for actual_node in self.network.nodes(): 964 # if( len(self.network.neighbors(actual_node))==0 ): 965 # outmethod("%s\n" %actual_node) 966 # for id1, id2, x in self.network.edges(): 967 # outmethod("%d %s %d\n" % (id1, x, id2)) 968 969 # elif format == "table": 970 # columns = ["interactor1","interactor2","interaction_type"] 971 # values = [] 972 # for actual_node in self.network.nodes(): 973 # if( len(self.network.neighbors(actual_node))==0 ): 974 # values.append((str(actual_node),"-","-")) 975 # for id1, id2, x in self.network.edges(): 976 # values.append((str(id1), str(x), str(id2))) 977 978 # th_str = "<tr>%s</tr>" %"".join([ "<th>%s</th>" %x for x in columns ]) 979 # data_str = "".join([ "<tr>%s</tr>" %"".join([ "<td>%s</td>" %current_column for current_column in current_row ]) for current_row in values ]) 980 # 981 # outmethod("<table>%s%s</table>" %(th_str,data_str)) 982
983 - def __repr__(self):
984 return "%s / %s" % (self.listLevelSetIdUserEntity, self.network.edges())
985
986 - def __str__(self):
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
1005 - def _get_xml_header(self):
1006 return "<user_entity_set id=\"%s\">" %self.id
1007
1008 - def _get_xml_foot(self):
1009 return "</user_entity_set>"
1010
1011 - def _get_xml(self, inner_content=""):
1012 return "%s%s%s" %(self._get_xml_header(), 1013 inner_content, 1014 self._get_xml_foot())
1015
1016 - def _get_groups_xml(self, only_not_printed=False, group_identifiers={}):
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 #toprint = list(self._not_printed_groups) 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 #self._not_printed_groups.discard(current_group) 1031 #print self._not_printed_groups 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
1044 - def _update_selected_external_ids_dict(self, attribute, selected_ext_id_name):
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