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

Source Code for Module biana.BianaObjects.BianaSessionManager

   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  # Class to manage/handle (create, analyze, operate vs..) networks  
  21  # from userEntities and available interaction data in BianaDB (using BianaDBaccess)  
  22   
  23  import sys 
  24   
  25  from biana.BianaDB.BianaDBaccess import BianaDBaccess 
  26  import UserEntitySet 
  27  import UserEntity 
  28   
  29  from ExternalEntityAttribute import ExternalEntityAttribute 
  30   
  31  import output_utilities 
  32  from biana.GraphUtilities import GraphUtilities 
  33  from BianaReport import * 
  34  import sets 
  35  import copy 
  36  import traceback 
  37  from biana.OutBianaInterface import OutBianaInterface 
  38   
  39   
40 -class Enum(object):
41
42 - def __init__(self):
43 self._enum_type_2_letter = {} 44 self._letter_2_enum_type = {} 45 self._enum_index = -1 46 self._enum_letter = "abcdefghijklmnopqrstuvwxyz1234567890"
47
48 - def get_letter(self, enum):
49 try: 50 return self._enum_type_2_letter[enum.lower()] 51 except: 52 next = self._get_next() 53 self._letter_2_enum_type[next] = enum 54 self._enum_type_2_letter[enum.lower()] = next 55 return next
56
57 - def _get_next(self):
58 self._enum_index += 1 59 return self._enum_letter[self._enum_index]
60
61 - def get(self, letter):
62 return self._letter_2_enum_type[letter]
63 64
65 -class BianaSessionManager(object):
66 """ 67 A class to represent a BianaNetworkManager 68 69 A BianaNetworkManager is composed of one or several userEntity(s) and one BianaDBaccess object 70 """ 71 72 # attribute substitution list for outputting identifiers of nodes in network when none of the external entities in the node has a valid value for given attribute 73 substitution_list = [ "uniprotaccession", "geneid" ] 74 75 #uE_types_enum = Enum() 76 #eEr_types_enum = Enum() 77 78
79 - def __init__(self, pSessionID, unification_protocol_name, dbname, dbhost, dbuser=None, dbpassword=None, dbport=None, out_method=None):
80 """ 81 Starts a new BIANA Working session 82 ------ 83 pSessionID: identifier for current session object 84 unification_protocol_name: name of the unification protocol to be used while retrieving data from database 85 dbname: name of the mysql database to be used 86 dbhost: host where mysql database resides 87 dbuser: user for the given mysql database 88 dbpassword: password for the given mysql database and host 89 dbport: port for mysql database connection, if None default value is used 90 out_method: is where biana session manager has to inform about changes 91 """ 92 93 self.uE_types_enum = Enum() 94 self.eEr_types_enum = Enum() 95 96 self.sessionID = pSessionID 97 self.unification_protocol_name = unification_protocol_name 98 99 self.dbname = dbname 100 self.dbhost=dbhost 101 self.dbuser=dbuser 102 self.dbpassword=dbpassword 103 self.dbport=dbport 104 105 self.dbAccess = BianaDBaccess(dbname=dbname, 106 dbhost=dbhost, 107 dbuser=dbuser, 108 dbpassword=dbpassword, 109 dbport=dbport) 110 111 112 self.dictUserEntity = {} # Dictionary to store user entity objects 113 self.dictUserEntitySet = {} # dictionary to store user entity sets objects 114 115 self.dictUserEntityInfo = {} # dictionary to store user entity basic info (not objects, as they use a lot of memory) 116 self.dictExternalEntityInfo = {} # dictionary to store external entity basic info (not objects, as they use a lot of memory) 117 118 self.uE_types_dict = {} 119 self.eEr_types_dict = {} 120 121 self.idLastUserEntitySet = 0 122 123 self.selectedUserEntitySetsIds = sets.Set() 124 125 self.outmethod = out_method 126 127 self.report = None 128 129 self.outmethod("<new_session id=\"%s\" dbname=\"%s\" dbhost=\"%s\" unification_protocol=\"%s\" description=\"Session description\"/>" %(self.sessionID,dbname,dbhost,self.unification_protocol_name)) 130 131 # Checks if database is optimized for running. If not, optimize it 132 if self.dbAccess.isOptimizedForRunning()==False: 133 OutBianaInterface.send_process_message("Optimizing database... Depending on BIANA database size, this process can take from few seconds to some hours.") 134 self.dbAccess.optimize_database_for( mode="running" ) 135 OutBianaInterface.send_end_process_message() 136 return
137 138 139 # ---- 140 # methods required for using pickle with piana objects 141 # ----
142 - def __getstate__(self):
143 """ 144 Get state of the session for [c]pickle.dump() 145 """ 146 odict = self.__dict__.copy() # copy the dict since we change it 147 del odict['dbAccess'] # remove database entry 148 del odict['outmethod'] # remove static outmethod entry 149 return odict
150
151 - def __setstate__(self, dict):
152 """ 153 Get state of the session for [c]pickle.load() 154 ------ 155 dict: the dictionary of a previously created and saved BianaSessionManager object 156 """ 157 self.__dict__.update(dict) # update attributes 158 self.dbAccess = BianaDBaccess(dict["dbname"], dict["dbhost"], dict["dbuser"], dict["dbpassword"], dict["dbport"]) 159 self.outmethod = OutBianaInterface.send_data 160 161 self.outmethod("<new_session id=\"%s\" dbname=\"%s\" dbhost=\"%s\" unification_protocol=\"%s\" description=\"Session description\"/>" %(self.sessionID,self.dbname,self.dbhost,self.unification_protocol_name)) 162 163 OutBianaInterface.send_process_message("Sending data...") 164 for idUESet, user_entity_set in self.dictUserEntitySet.iteritems(): 165 self._send_complete_user_entity_set_info(user_entity_set=user_entity_set) 166 OutBianaInterface.send_end_process_message() 167 168 # Checks if database is optimized for running. If not, optimize it 169 if self.dbAccess.isOptimizedForRunning()==False: 170 OutBianaInterface.send_process_message("Optimizing database...") 171 self.dbAccess.optimize_database_for( mode="running" ) 172 OutBianaInterface.send_end_process_message() 173 174 return
175
176 - def close(self):
177 self.dbAccess.close()
178 179
180 - def reconnect(self):
181 self.dbAccess.reconnect()
182 183
184 - def _send_complete_user_entity_set_info(self, user_entity_set):
185 """ 186 Sends information of given userEntitySet in xml format through self.outmethod 187 ------ 188 user_entity_set: an instance of user entity set 189 """ 190 191 self.outmethod(self._get_xml(inner_content="<new_user_entity_set id=\"%s\"/>" %(user_entity_set.id))) 192 self.outmethod(self._get_xml(inner_content=self._get_user_entity_set_xml(user_entity_set))) 193 if( user_entity_set.isNetworkCreated() == True ): 194 self.outmethod(self._get_xml(inner_content=user_entity_set._get_xml(inner_content="<update_network_depth levels=\"%s\"/>" %user_entity_set.get_level())))
195 196
197 - def get_ontology(self, ontology_name, root_attribute_values=[]):
198 """ 199 Fetchs the ontology structure identifier by ontology_name 200 ------ 201 ontology_name: name of the ontology to be retrieved from database 202 root_attribute_values: list of values used in selecting roots of ontology by attribute 203 """ 204 205 #OutBianaInterface.send_process_message("Getting information from database...") 206 207 #print "loading ontology from database" 208 209 ontology_obj = self.dbAccess.get_ontology( ontology_name = ontology_name, root_attribute_values = root_attribute_values, load_external_entities = False ) # Changed True to False 210 211 #print "loaded" 212 213 #OutBianaInterface.send_end_process_message() 214 215 return ontology_obj
216 217 218 219
220 - def get_user_entity_set(self, user_entity_set_id):
221 """ 222 returns the user entity set object with the name "user_entity_set_id" 223 ------ 224 user_entity_set_id: identifier of user entity set to be returned 225 """ 226 227 try: 228 return self.dictUserEntitySet[user_entity_set_id] 229 except: 230 OutBianaInterface.send_error_notification( message = "ERROR", error = "Trying to get an unexisting user entity set: %s" %user_entity_set_id) 231 232 return
233 234
235 - def select_user_entity_set(self, user_entity_set_id):
236 """ 237 Method for selecting a user entity set 238 ------ 239 user_entity_set_id: identifier of user entity set to be selected 240 241 #! Not called by any other method 242 """ 243 244 if self.dictUserEntitySet.has_key(user_entity_set_id): 245 self.selectedUserEntitySetsIds.add(user_entity_set_id) 246 self.outmethod("<select_user_entity_set id=\"%s\"/>" %user_entity_set_id) 247 else: 248 OutBianaInterface.send_error_notification( message = "ERROR", error = "Trying to select an unexisting user entity set: %s" %user_entity_set_id) 249 250 return
251 252
253 - def describe_user_entity_set(self, user_entity_set_id):
254 """ 255 Method to print into the biana interface the description of the User Entity ID identified by "user_entity_set_id" 256 257 "format" can be txt or html 258 """ 259 260 if self.dictUserEntitySet.has_key(user_entity_set_id): 261 OutBianaInterface.send_info_message(self.dictUserEntitySet[user_entity_set_id].__str__()) 262 else: 263 OutBianaInterface.send_error_notification( message = "ERROR", error = "Trying to get an unexisting user entity set: %s" %user_entity_set_id) 264 265 return
266 267
269 """ 270 Returns list of identifiers of selected user entity sets 271 272 #! Not called by any other method 273 """ 274 return self.selectedUserEntitySetsIds
275 276
278 """ 279 Clears selection of user entity sets 280 281 #! Not called by any other method 282 """ 283 self.selectedUserEntitySetsIds.clear() 284 self.outmethod("<clear_user_entity_set_selection/>") 285 return
286
287 - def read_identifier_list_from_file(self, file_name, id_type=None):
288 """ 289 File either contains an identifier per line 290 or (attribute_name, identifier) couple on each line 291 292 - Designated for use from command line scripts 293 ------ 294 file_name: name of the file identifiers will be read from 295 identifier_description_list: list of identifiers (or (id_type, id_string) tuples in case id_type is "embedded") 296 id_type: type of the identifiers in the file, if "embedded" then file contains "attribute_name" "identifier" (seperated by tab) instead of just identifier at each line, if "auto" certain regular expressions will be tried to matched for automatic detection 297 """ 298 299 fileIdentifierList = open(file_name) 300 line = fileIdentifierList.readline() 301 identifier_description_list = [] 302 303 ## read input file 304 while line: 305 words = line[:-1].split("\t") 306 if len(words) == 2 and id_type=="embedded": 307 identifierType = words[0].strip() 308 identifierString = words[1].strip() #.replace("'","") 309 identifier_description_list.append((identifierType, identifierString)) 310 elif len(words) == 1: 311 identifierString = words[0].strip() #.replace("'","") 312 if id_type is None: 313 identifier_description_list.append(identifierString) 314 elif id_type == "auto": 315 detected_id_type = self.detectIdentifierType(identifierString) 316 if detected_id_type is None: 317 print "Warning: Can not auto-detect type of %s" % identifierString 318 else: 319 identifier_description_list.append((detected_id_type, identifierString)) 320 else: 321 identifier_description_list.append((id_type, identifierString)) 322 else: 323 raise Exception("Unrecognized id_type: %s" % id_type) 324 325 line = fileIdentifierList.readline() 326 327 return identifier_description_list
328 329
330 - def detectIdentifierType(self, id):
331 """ 332 Return auto-detected type of the given id 333 334 Works for ids with strict regular format (such as ENSP00123, AC1423234.1, EC0123123, RAW1_HUMAN, Q1234R, ...) 335 """ 336 import re 337 338 detected_type = None 339 re_uniprot_entry = re.compile("\w+_[A-Z]+$") 340 re_uniprot_accession = re.compile("[A-Z]\d\w{3}\d(-\d{1,3}){0,1}$") 341 re_ensembl = re.compile("ENS[A-Z]+\d{11}$") 342 re_accession_number = re.compile("[A-Z]{1,2}\d{3,7}(.\d{1,3}){0,1}$") 343 #re_ec = re.compile("") 344 #re_ordered_locus_name = re.compile("[A-Z]{1,3}\d{3,4}[W|C|](.\d{1,3}){0,1}$") 345 346 if re_uniprot_accession.search(id): 347 detected_type = "uniprotaccession" 348 elif re_uniprot_entry.search(id): 349 detected_type = "uniprotentry" 350 #elif id.startswith("ENS"): 351 elif re_ensembl.search(id): 352 detected_type = "ensembl" 353 #elif id.startswith("AC"): 354 elif re_accession_number.search(id): 355 detected_type = "accessionnumber" 356 #elif re_orf_name.search(id): 357 # detected_type = "orfname" 358 elif id.startswith("IPI"): 359 detected_type = "ipi" 360 elif id.startswith("EC"): 361 detected_type = "ec" 362 #else: 363 # print "Warning: Undetermined type: %s" % id 364 365 return detected_type
366 367
368 - def _convert_attribute_list_to_attribute_dictionary(self, identifier_description_list, attribute_name):
369 """ 370 Returns dictionary containing identifier types mapped to identifiers created from given list of identifiers 371 ------ 372 identifier_description_list: list of identifiers (or (identifier, id_type) tuples in case id_type is "embedded") 373 id_type: type of the identifiers in the file, if "embedded" then file contains (attribute_name, identifier) tuples instead of just identifiers 374 """ 375 dictTypeToName = {} 376 for identifierDescription in identifier_description_list: 377 if attribute_name == "embedded": 378 identifierType = identifierDescription[0] 379 identifierString = identifierDescription[1] 380 else: 381 identifierType = attribute_name 382 identifierString = identifierDescription 383 dictTypeToName.setdefault(identifierType, sets.Set()).add(identifierString) 384 return dictTypeToName
385
386 - def _get_next_uEs_id(self):
387 """ 388 Private method to obtain automatically the next user entity set default id 389 """ 390 self.idLastUserEntitySet += 1 391 return "uEs_%s" %self.idLastUserEntitySet
392 393
394 - def duplicate_user_entity_set(self, user_entity_set_id, new_user_entity_set_id):
395 """ 396 Returns an exact copy of user entity set object with given id 397 ------ 398 user_entity_set_id: id of user entity set to be duplicated 399 user_entity_set_id_new: id of created copy of user entity set 400 """ 401 original_uEs = self.get_user_entity_set(user_entity_set_id) 402 403 if original_uEs is not None: 404 newObj = copy.deepcopy(original_uEs) 405 newObj.id = new_user_entity_set_id 406 self.dictUserEntitySet[newObj.id] = newObj 407 self._send_complete_user_entity_set_info(user_entity_set=newObj) 408 409 return newObj
410 411
412 - def create_randomized_user_entity_set(self, user_entity_set_id, new_user_entity_set_id, type_randomization):
413 """ 414 Creates a new user entity set with randomized network from given user entity set 415 ------ 416 user_entity_set_id: id of the user entity set whose copy with random network is going to be created 417 new_user_entity_set_id: id for the created copy of user entity set 418 type_randomization: randomization type to be used in network randomization, can be one of the following: "random", "preserve_topology", "preserve_topology_and_node_degree", "preserve_degree_distribution", "preserve_degree_distribution_and_node_degree" 419 """ 420 original_uEs = self.get_user_entity_set(user_entity_set_id) 421 422 if original_uEs is not None: 423 if original_uEs.isNetworkCreated(): 424 newObj = copy.deepcopy(original_uEs) 425 newObj.id = new_user_entity_set_id 426 original_network = original_uEs.getNetwork() 427 random_network = GraphUtilities.randomize_graph(graph = original_network, randomization_type = type_randomization) 428 #print type_randomization, original_network.number_of_edges(), random_network.number_of_edges() 429 #newObj.setNetwork(GraphUtilities.randomize_graph(original_uEs.getNetwork(), type_randomization)) 430 newObj.setNetwork(random_network) 431 self.dictUserEntitySet[newObj.id] = newObj 432 self._send_complete_user_entity_set_info(user_entity_set=newObj) 433 return newObj 434 else: 435 OutBianaInterface.send_error_notification( message = "Cannot randomize without a created network", error = "Set is not created as it does not have a network" ) 436 else: 437 OutBianaInterface.send_error_notification( message = "Randomization not done!", error = "Cannot randomize with an unexisting set: %s" %user_entity_set_id) 438 439 return
440 441
442 - def randomize_user_entity_set_network(self, user_entity_set_id, type_randomization):
443 """ 444 Randomizes network of a given user entity set. 445 ------ 446 user_entity_set_id: id of the user entity set whose network will be randomized 447 type_randomization: randomization type to be used in network randomization, can be one of the following: "random", "preserve_topology", "preserve_topology_and_node_degree", "preserve_degree_distribution", "preserve_degree_distribution_and_node_degree" 448 """ 449 original_uEs = self.get_user_entity_set(user_entity_set_id) 450 if original_uEs is not None: 451 if original_uEs.isNetworkCreated(): 452 original_uEs.setNetwork(GraphUtilities.randomize_graph(original_uEs.getNetwork(), type_randomization)) 453 #self._send_complete_user_entity_set_info(user_entity_set=original_uEs) 454 #self.outmethod(self._get_xml(inner_content="<remove_user_entity_set id=\"%s\"/>" % user_entity_set_id)) 455 #self._send_user_entity_set_network_info(original_uEs) 456 self.outmethod(self._get_xml(inner_content="<remove_user_entity_set id=\"%s\"/>" % user_entity_set_id)) 457 self._send_complete_user_entity_set_info(user_entity_set=original_uEs) 458 return
459 460
461 - def create_new_user_entity_set(self, identifier_description_list, id_type="embedded", new_user_entity_set_id=None, attribute_restriction_list=[], negative_attribute_restriction_list=[], external_database_restrictions=[]):
462 """ 463 create userEntity objects from given externalEntity ids (or get if already existing) then add them to a userEntitySet 464 ------ 465 identifier_description_list: list with external identifiers of the nodes in the set provided by user 466 id_type: either list of attrubutes defined in EXTERNAL_ENTITY_TABLES or "embedded" meaning identifier_description_list is a list of (id_type, identifier) tuple in the form [(type1, id1), (type2, id2), ..., (typeN, idN)] 467 new_user_entity_set_id: identifier of the set provided by the user 468 attribute_restriction_list: list of tuples provided by the user containing external entity attribute restrictons to be applied. They will be used always in the set (network creation, etc) 469 negative_attribute_restriction_list: list of tuples provided by the user containing attributes that may neve appear in the user entity set. They will be used alwyas in the set. 470 """ 471 472 OutBianaInterface.send_process_message("Creating new user entity set. \nGetting information from database...") 473 474 try: 475 476 # Check for missing parameters and set it to default 477 if new_user_entity_set_id is None: 478 new_user_entity_set_id = self._get_next_uEs_id() 479 480 user_entity_set = UserEntitySet.UserEntitySet(new_user_entity_set_id ) 481 482 483 # Javi added: transform restrictions (for transferred attributes) 484 if id_type=="embedded": 485 identifier_description_list = self.dbAccess.transform_expanded_attribute_restrictions(identifier_description_list) 486 else: 487 identifier_description_list = self.dbAccess.transform_expanded_attribute_restrictions([ (id_type,x) for x in identifier_description_list ]) 488 489 attribute_restriction_list = self.dbAccess.transform_expanded_attribute_restrictions(attribute_restriction_list) 490 negative_attribute_restriction_list = self.dbAccess.transform_expanded_attribute_restrictions(negative_attribute_restriction_list) 491 492 493 ## prepare id_type classified sets to be used in data fetching step 494 # Gets the input to build the user entity set (root or seed user entities) 495 #dictTypeToName = self._convert_attribute_list_to_attribute_dictionary(identifier_description_list, id_type) 496 dictTypeToName = self._convert_attribute_list_to_attribute_dictionary(identifier_description_list, "embedded") 497 498 user_entity_set.userProvidedExternalIdsDict = dictTypeToName 499 500 # Fill userProvidedExtIdsLowerDict: 501 for k in user_entity_set.userProvidedExternalIdsDict: 502 user_entity_set.userProvidedExtIdsLowerDict[k] = {} 503 504 for extId in user_entity_set.userProvidedExternalIdsDict[k]: 505 user_entity_set.userProvidedExtIdsLowerDict[k][str(extId).lower()] = extId 506 507 508 # selectedExtIdsLowerDict is filled by method UserEntitySet._update_selected_external_ids_dict() 509 510 ## for each different type of id_types fetch userEntity ids associated with given externalEntity ids 511 for identifierType, setIdentifierName in dictTypeToName.iteritems(): 512 513 listIdUserEntity = [] 514 515 # javi added: 516 if( identifierType.lower()=="userentityid" ): 517 listIdUserEntity = [int(identifierName)] 518 519 else: 520 521 field_values = [] 522 for identifierName in setIdentifierName: 523 field_values.append(("value",identifierName)) 524 525 listIdUserEntity = self.dbAccess.get_list_user_entities_IDs_by_attribute( unification_protocol_name = self.unification_protocol_name, 526 attribute_identifier = identifierType, 527 field_values = field_values, 528 attribute_restrictions = attribute_restriction_list, 529 negative_attribute_restrictions = negative_attribute_restriction_list, 530 include_type = True ) 531 532 533 ## create new userEntity objects if not created (by another external entity that belongs to that userEntity) before 534 for (user_entity_id, type) in listIdUserEntity: 535 user_entity_set.addUserEntityId(idUserEntity = user_entity_id, level = 0) 536 self.uE_types_dict[user_entity_id] = self.uE_types_enum.get_letter(type) 537 self.outmethod("<user_entity id=\"%s\" type=\"%s\"/>" %(user_entity_id, type)) 538 539 user_entity_set.addRestriction("negative_attribute_restrictions", negative_attribute_restriction_list) 540 user_entity_set.addRestriction("attribute_restrictions", attribute_restriction_list) 541 user_entity_set.addRestriction("externalDatabase_restrictions", external_database_restrictions) 542 543 except: 544 OutBianaInterface.send_error_notification( message = "New set not created. BIANA ERROR:", error = traceback.format_exc() ) 545 OutBianaInterface.send_end_process_message() 546 return 547 548 OutBianaInterface.send_end_process_message() 549 550 if user_entity_set.getSize()==0: 551 OutBianaInterface.send_error_notification(message="New set not created", error = "Any element has been found with given identifiers") 552 return 553 554 self.dictUserEntitySet[user_entity_set.id] = user_entity_set 555 556 OutBianaInterface.send_process_message("Sending data...") 557 self._send_complete_user_entity_set_info( user_entity_set = user_entity_set ) 558 OutBianaInterface.send_end_process_message() 559 560 return user_entity_set
561 562 563 # def _get_different_user_entity_relation_types(self, user_entity_set, userEntity1, userEntity2 564
565 - def _get_user_entity_set_xml(self, user_entity_set_obj, level=None):
566 567 user_entity_ids_list = user_entity_set_obj.get_user_entity_ids(level=level) 568 relations = user_entity_set_obj.getRelations() 569 relations_str_list = [] 570 for (id1, id2) in user_entity_set_obj.getRelations(): 571 eEr_ids_list = user_entity_set_obj.get_external_entity_relation_ids(id1,id2) 572 types = sets.Set() 573 for current_eEr_id in eEr_ids_list: 574 types.add(self.eEr_types_dict[current_eEr_id]) 575 for current_type in types: 576 relations_str_list.append("<user_entity_relation node1=\"%s\" node2=\"%s\" type=\"%s\" relation_id=\"%s\"/>" %(id1, id2, self.eEr_types_enum.get(current_type), "0" )) #TO CHECK IF IT IS NECESSARY TO PRINT THE RELATION ID... 577 578 default_attributes_dict = dict([ (x,x) for x in user_entity_set_obj.get_groups_ids() ]) 579 default_attributes_dict.update(self.dbAccess.get_default_external_entity_ids( externalEntityIDsList=user_entity_set_obj.get_groups_ids() )) 580 581 return "%s%s%s%s%s" %(user_entity_set_obj._get_xml_header(), 582 "".join(["<user_entity id=\"%s\" type=\"%s\" />" %(id,self.uE_types_enum.get(self.uE_types_dict[id])) for id in user_entity_ids_list]), 583 "".join(relations_str_list), 584 user_entity_set_obj._get_groups_xml(only_not_printed=False, group_identifiers = default_attributes_dict), 585 user_entity_set_obj._get_xml_foot() )
586 587 588
589 - def remove_user_entity_set(self, user_entity_set_id):
590 """ 591 Remove user entity set from the session 592 ------ 593 user_entity_set_id: identifier of user entity set to be removed 594 """ 595 if self.dictUserEntitySet.has_key(user_entity_set_id): 596 del self.dictUserEntitySet[user_entity_set_id] 597 self.outmethod(self._get_xml(inner_content="<remove_user_entity_set id=\"%s\"/>" % user_entity_set_id)) 598 return
599
600 - def get_user_entity(self, user_entity_id):
601 """ 602 Fetch all externalEntity ids associated with this user entity or retrieve it from dictionary if exists 603 ------ 604 user_entity_id: identifier of user entity 605 """ 606 if not self.dictUserEntity.has_key(user_entity_id): 607 listIdExternalEntity = self.dbAccess._get_list_eE_for_uE(self.unification_protocol_name, user_entity_id) 608 objUserEntity = UserEntity.UserEntity(user_entity_id, listIdExternalEntity) 609 self.dictUserEntity[user_entity_id] = objUserEntity 610 else: 611 objUserEntity = self.dictUserEntity[user_entity_id] 612 return objUserEntity
613
614 - def expand_user_entity_set(self, user_entity_set_id, is_last_level=False):
615 """ 616 Fetchs interactions of userEntities in the last level of the userEntitySet 617 ------ 618 user_entity_set_id: identifier of the user entity set 619 """ 620 user_entity_set = self.dictUserEntitySet[user_entity_set_id] 621 622 #if( len(user_entity_set.getRestrictions("relation_type_restrictions"))==0 ): 623 if user_entity_set.isNetworkCreated() == False: 624 OutBianaInterface.send_error_notification( message = "Cannot expand network!", error = "Before expanding a network, it is mandatory to initialize a network with the create network command") 625 return 626 627 OutBianaInterface.send_process_message("Getting information from database...") 628 629 630 self.outmethod(self._get_xml_header()) 631 self.outmethod(user_entity_set._get_xml_header()) 632 633 try: 634 635 temp_inserted = 0 636 637 userEntityIds = user_entity_set.get_user_entity_ids(level="last") 638 639 # Only creates the network of relations if types are specified 640 if( len(user_entity_set.getRestrictions("relation_type_restrictions"))>0 ): 641 listTupleIdUserEntity = self.dbAccess.get_user_entity_relations(unification_protocol_name = self.unification_protocol_name, 642 userEntityID_list = userEntityIds, 643 attribute_restrictions = user_entity_set.getRestrictions("attribute_restrictions"), 644 negative_attribute_restrictions = user_entity_set.getRestrictions("negative_attribute_restrictions"), 645 listRelationType = user_entity_set.getRestrictions("relation_type_restrictions"), 646 dictRelationAttributeRestriction = user_entity_set.getRestrictions("relation_attribute_restrictions"), 647 use_self_relations = user_entity_set.getRestrictions("use_self_relations"), 648 limit_to_userEntityID_list = is_last_level) # ramon removes self. that was placed # before each user_entity_set 649 650 OutBianaInterface.send_process_message("Processing information...") 651 652 for (idUserEntity1, idUserEntity2, externalEntityRelationID, relation_type, partner_type) in listTupleIdUserEntity: 653 if not user_entity_set.has_user_entity(idUserEntity1): 654 self.outmethod("<user_entity id=\"%s\" type=\"%s\"/>" %(idUserEntity1, partner_type)) 655 elif not user_entity_set.has_user_entity(idUserEntity2): 656 self.outmethod("<user_entity id=\"%s\" type=\"%s\"/>" %(idUserEntity2, partner_type)) 657 # addUserEntityRelation adds nodes so above should be before a call to it 658 inserted = user_entity_set.addUserEntityRelation(idUserEntity1 = idUserEntity1, 659 idUserEntity2 = idUserEntity2, 660 externalEntityRelationID = externalEntityRelationID) 661 self.eEr_types_dict[externalEntityRelationID] = self.eEr_types_enum.get_letter(relation_type) 662 #self.uE_types_dict.setdefault(idUserEntity1,self.uE_types_enum.get_letter(partner_type)) 663 self.uE_types_dict.setdefault(idUserEntity2,self.uE_types_enum.get_letter(partner_type)) 664 665 if inserted: 666 self.outmethod("<user_entity_relation node1=\"%s\" node2=\"%s\" type=\"%s\" relation_id=\"%s\"/>" %(idUserEntity1, idUserEntity2, relation_type, externalEntityRelationID)) 667 temp_inserted += 1 668 669 OutBianaInterface.send_end_process_message() 670 671 # Creates the relation groups if types for groups are specified 672 relation_ids_set = sets.Set() 673 if( len(user_entity_set.getRestrictions("group_relation_type"))>0 ): 674 listTupleIdUserEntity = self.dbAccess.get_user_entity_relations(unification_protocol_name = self.unification_protocol_name, 675 userEntityID_list = userEntityIds, 676 attribute_restrictions = user_entity_set.getRestrictions("attribute_restrictions"), 677 negative_attribute_restrictions = user_entity_set.getRestrictions("negative_attribute_restrictions"), 678 listRelationType = user_entity_set.getRestrictions("group_relation_type"), 679 dictRelationAttributeRestriction = user_entity_set.getRestrictions("relation_attribute_restrictions"), 680 use_self_relations = user_entity_set.getRestrictions("use_self_relations"), 681 limit_to_userEntityID_list = is_last_level) # ramon removes self. that was placed # before each user_entity_set 682 683 OutBianaInterface.send_process_message("Processing information...") 684 685 for (idUserEntity1, idUserEntity2, externalEntityRelationID, relation_type, partner_type) in listTupleIdUserEntity: 686 self.uE_types_dict.setdefault(idUserEntity2, self.uE_types_enum.get_letter(partner_type)) 687 relation_ids_set.add(externalEntityRelationID) 688 if not user_entity_set.has_user_entity(idUserEntity1): 689 self.outmethod("<user_entity id=\"%s\" type=\"%s\"/>" %(idUserEntity1, partner_type)) 690 elif not user_entity_set.has_user_entity(idUserEntity2): 691 self.outmethod("<user_entity id=\"%s\" type=\"%s\"/>" %(idUserEntity2, partner_type)) 692 user_entity_set.addUserEntitiesToGroup(group_id = externalEntityRelationID, 693 userEntityID = idUserEntity2, 694 representative_userEntityID = idUserEntity1, 695 group_type = relation_type ) 696 697 698 699 # Get groups hierarchy information 700 if len(listTupleIdUserEntity)>0: 701 #eE_dict = self.dbAccess.get_external_entities_dict( attribute_list=["name"], externalEntityIdsList = user_entity_set.get_groups_ids() ) 702 default_attributes_dict = dict([ (x,x) for x in user_entity_set.get_groups_ids() ]) 703 default_attributes_dict.update(self.dbAccess.get_default_external_entity_ids( externalEntityIDsList=user_entity_set.get_groups_ids() )) 704 user_entity_set.setGroupsHierarchy( list_hierarchy = self.dbAccess.get_relations_hierarchy( externalEntityRelationIDs = relation_ids_set ) ) 705 self.outmethod(self._get_xml(inner_content=user_entity_set._get_xml(inner_content=user_entity_set._get_groups_xml(only_not_printed=True, group_identifiers = default_attributes_dict )))) 706 707 OutBianaInterface.send_end_process_message() 708 709 if( len(user_entity_set.getRestrictions( restriction_type = "expansionAttributesList"))>0 ): 710 711 # Get inferred relations 712 OutBianaInterface.send_process_message("Getting information from database...") 713 714 listTupleIdUserEntity = self.dbAccess.get_expanded_entity_relations(unification_protocol_name = self.unification_protocol_name, 715 userEntityID_list = userEntityIds, 716 listRelationType = user_entity_set.getRestrictions("expansionListRelationType"), 717 use_self_relations = user_entity_set.getRestrictions("use_self_relations"), 718 expansionLevel = user_entity_set.getRestrictions("expansionLevel"), 719 expansionAttributesList = user_entity_set.getRestrictions("expansionAttributesList"), 720 attribute_restrictions = user_entity_set.getRestrictions("attribute_restrictions"), 721 negative_attribute_restrictions = user_entity_set.getRestrictions("negative_attribute_restrictions"), 722 limit_to_userEntityID_list = is_last_level) 723 724 for (idUserEntity1, idUserEntity2, externalEntityRelationID, relation_type, partner_type) in listTupleIdUserEntity: 725 self.uE_types_dict.setdefault(idUserEntity2, self.uE_types_enum.get_letter(partner_type)) 726 #self.uE_types_dict.setdefault(idUserEntity1, self.uE_types_enum.get_letter(partner_type)) 727 self.eEr_types_dict.setdefault(externalEntityRelationID, self.eEr_types_enum.get_letter(relation_type)) 728 if not user_entity_set.has_user_entity(idUserEntity1): 729 self.outmethod("<user_entity id=\"%s\" type=\"%s\"/>" %(idUserEntity1, partner_type)) 730 elif not user_entity_set.has_user_entity(idUserEntity2): 731 self.outmethod("<user_entity id=\"%s\" type=\"%s\"/>" %(idUserEntity2, partner_type)) 732 inserted = user_entity_set.addUserEntityRelation(idUserEntity1=idUserEntity1, idUserEntity2=idUserEntity2, externalEntityRelationID=externalEntityRelationID) 733 if inserted: 734 self.outmethod("<user_entity_relation node1=\"%s\" node2=\"%s\" type=\"%s\" relation_id=\"%s\"/>" %(idUserEntity1, idUserEntity2, relation_type, externalEntityRelationID)) 735 OutBianaInterface.send_end_process_message() 736 737 if( len(user_entity_set.getRestrictions( restriction_type = "attributeNetworkList"))>0 ): 738 739 # Get attribute relations 740 OutBianaInterface.send_process_message("Getting attribute relations...") 741 742 listTupleIdUserEntity = self.dbAccess.get_user_entity_relations_by_sharing_attributes( unification_protocol_name = self.unification_protocol_name, 743 userEntityID_list = userEntityIds, 744 listAttributes = user_entity_set.getRestrictions("attributeNetworkList"), 745 limit_to_userEntityID_list = is_last_level, 746 attribute_restrictions = user_entity_set.getRestrictions("attribute_restrictions"), 747 negative_attribute_restrictions = user_entity_set.getRestrictions("negative_attribute_restrictions") ) 748 749 for (idUserEntity1, idUserEntity2, type) in listTupleIdUserEntity: 750 self.uE_types_dict.setdefault(idUserEntity2, self.uE_types_enum.get_letter(type)) 751 #self.uE_types_dict.setdefault(idUserEntity1, self.uE_types_enum.get_letter(type)) 752 self.eEr_types_dict[0] = self.eEr_types_enum.get_letter("common_attribute") 753 if not user_entity_set.has_user_entity(idUserEntity1): 754 self.outmethod("<user_entity id=\"%s\" type=\"%s\"/>" %(idUserEntity1, type)) 755 elif not user_entity_set.has_user_entity(idUserEntity2): 756 self.outmethod("<user_entity id=\"%s\" type=\"%s\"/>" %(idUserEntity2, type)) 757 inserted = user_entity_set.addUserEntityRelation(idUserEntity1=idUserEntity1, idUserEntity2=idUserEntity2, externalEntityRelationID=0) 758 if inserted: 759 self.outmethod("<user_entity_relation node1=\"%s\" node2=\"%s\" type=\"%s\" relation_id=\"%s\"/>" %(idUserEntity1, idUserEntity2, "common_attribute", 0)) 760 OutBianaInterface.send_end_process_message() 761 762 except: 763 OutBianaInterface.send_error_notification( message = "Network not expanded. BIANA ERROR:", error = traceback.format_exc() ) 764 765 self.outmethod("<update_network_depth levels=\"%s\"/>" %user_entity_set.get_level()) 766 self.outmethod(user_entity_set._get_xml_foot()) 767 self.outmethod(self._get_xml_foot()) 768 769 OutBianaInterface.send_end_process_message()
770 771
772 - def _get_xml(self, inner_content):
773 """ 774 Returns given information for current session in XML format. 775 ------ 776 inner_content: Information to be encapsulated with current session. 777 """ 778 return "%s%s%s" %(self._get_xml_header(), inner_content, self._get_xml_foot())
779
780 - def _get_xml_header(self):
781 return "<session id=\"%s\">" %(self.sessionID)
782
783 - def _get_xml_foot(self):
784 return "</session>"
785
786 - def create_network(self, user_entity_set_id, level=0, include_relations_last_level = True, source_database_version_list=[], relation_type_list=[], relation_attribute_restriction_list=[], use_self_relations=True, expansion_attribute_list=[], expansion_relation_type_list=[], expansion_level=2, attribute_network_attribute_list=[], group_relation_type_list=[]):
787 """ 788 Creates network of given user entity set adding relations of nodes as edges. 789 ------ 790 user_entity_set_id: identifier of user entity set for which network will be created 791 level: level of the network to be created, network will be expanded till that level 792 include_relations_last_level: include relations between the nodes residing at the last level 793 source_database_version_list: list of (biological database, version) tuples 794 relation_type_list: type of the relations to be used in expansion 795 relation_attribute_restriction_list: tuples of (attribute, value) corresponding to restrictions to be applied on attributes of relations 796 use_self_relations: include relations within the node itself 797 expansion_attribute_list: tuples of (attribute, value_dictionary) corresponding to attributes to be used in relation inference between nodes based on shared attributes - value_dictionary is empty if attribute is not parameterizable 798 expansion_relation_type_list: type of relations to be used in shared attribute based relation inference 799 expansion_level: number of relations (edges) to look further while inferring relations based on shared attributes 800 attribute_network_attribute_list: tuples of (attribute, value) corresponding to attributes to be used while associating nodes with common attributes - value_dictionary is empty if attribute is not parameterizable 801 group_relation_type_list: type of relations that are going to be treated as a group (like pathway, complex, cluster..) 802 803 """ 804 user_entity_set = self.dictUserEntitySet[user_entity_set_id] 805 806 if user_entity_set.isNetworkCreated() == True: 807 sys.stderr.write("Cannot create network on a set with created network. To expand use \"expand_user_entity_set\" command\n"); 808 OutBianaInterface.send_error_notification( message = "Network already created", error = "You cannot create a network on a set with created network, you can only expand it. In order to create a network with different parameters, create a new user entity set with selected nodes and create a network on it" ) 809 return 810 811 OutBianaInterface.send_process_message("Creating network...") 812 813 try: 814 815 if ( len(expansion_attribute_list)==0 and len(expansion_relation_type_list)>0 ) or (len(expansion_attribute_list)>0 and len(expansion_relation_type_list)==0 ): 816 raise ValueError("For expansions it is mandatory to specify attributes and relation types") 817 818 user_entity_set.setIsNetworkCreated() 819 820 dictAttributeToValues = self._convert_attribute_list_to_attribute_dictionary(relation_attribute_restriction_list, "embedded") 821 822 # Fill the restrictions dictionary 823 user_entity_set.addRestriction("relation_type_restrictions", relation_type_list) 824 user_entity_set.addRestriction("externalDatabase_restrictions", source_database_version_list) 825 user_entity_set.setRestriction("relation_attribute_restrictions", dictAttributeToValues) 826 user_entity_set.setRestriction(restriction_type="use_self_relations",restriction=use_self_relations) 827 user_entity_set.setRestriction(restriction_type="include_relations_last_level",restriction=include_relations_last_level) 828 user_entity_set.setRestriction("group_relation_type", group_relation_type_list) 829 830 #expansion related 831 user_entity_set.addRestriction("expansionAttributesList", expansion_attribute_list) 832 user_entity_set.addRestriction("expansionListRelationType", expansion_relation_type_list) 833 user_entity_set.setRestriction("expansionLevel", expansion_level) 834 835 #attribute network related 836 user_entity_set.addRestriction("attributeNetworkList", attribute_network_attribute_list) 837 838 levelStart = user_entity_set.get_level() 839 840 if levelStart == level: 841 if level == 0 and include_relations_last_level: 842 #self._getLastLevelRelations(user_entity_set) 843 self.expand_user_entity_set(user_entity_set_id = user_entity_set_id, is_last_level=True) 844 else: 845 for l in xrange(levelStart, level): 846 self.expand_user_entity_set(user_entity_set_id = user_entity_set_id) 847 848 if include_relations_last_level: 849 self.expand_user_entity_set(user_entity_set_id = user_entity_set_id, is_last_level=True) 850 #self._getLastLevelRelations(user_entity_set) 851 852 except: 853 OutBianaInterface.send_error_notification( message = "Network already created", error = "You cannot create a network on a set with created network, you can only expand it. In order to create a network with different parameters, create a new user entity set with selected nodes and create a network on it" ) 854 855 OutBianaInterface.send_end_process_message() 856 857 return
858 859
860 - def get_union_of_user_entity_set_list(self, user_entity_set_list, include_relations=False, new_user_entity_set_id=None):
861 """ 862 Does the union between of the user entities belonging to the user entities sets in the list and returns a new UserEntitySet 863 ------ 864 user_entity_set_list: list of user entity set objects/ids to be combined 865 include_relations: flag to whether or not include relations in union 866 new_user_entity_set_id: identifier of new user entity set to be created as a result of the union 867 """ 868 869 if new_user_entity_set_id is None: 870 new_user_entity_set_id = self._get_next_uEs_id() 871 872 # Check if the user_entity_set_list contains the objects or the names 873 if isinstance(user_entity_set_list[0],str) or isinstance(user_entity_set_list[0],int): 874 user_entity_set_list = [self.get_user_entity_set(x) for x in user_entity_set_list ] 875 876 if len(user_entity_set_list) <2: # TO CHECK IF THIS SHOULD BE DONE LIKE THIS... This line is here to prevent errors when only one userentityset is given 877 new_user_entity_set = copy.deepcopy(user_entity_set_list[0]) 878 new_user_entity_set.id = new_user_entity_set_id 879 self.dictUserEntitySet[new_user_entity_set.id] = new_user_entity_set 880 return 881 882 (listLevelSetId, listRelations) = user_entity_set_list[0].getUnionWithGivenUserEntitySet(user_entity_set_list[1], include_relations) 883 884 new_user_entity_set = UserEntitySet.UserEntitySet(id = new_user_entity_set_id, setIdUserEntity=None, listRelations = listRelations, listLevelSetIdUserEntity = listLevelSetId) 885 886 lenList = len(user_entity_set_list) 887 888 for i in xrange(lenList): 889 if i < 2: 890 continue 891 user_entity_set = user_entity_set_list[i] 892 (listLevelSetId, listRelations) = new_user_entity_set.getUnionWithGivenUserEntitySet(user_entity_set, include_relations) 893 new_user_entity_set = UserEntitySet.UserEntitySet(id = new_user_entity_set_id, setIdUserEntity=None, listRelations = listRelations, listLevelSetIdUserEntity = listLevelSetId) 894 895 896 self.dictUserEntitySet[new_user_entity_set.id] = new_user_entity_set 897 898 self._send_complete_user_entity_set_info(user_entity_set=new_user_entity_set) 899 900 return new_user_entity_set 901 902
903 - def get_intersection_of_user_entity_set_list(self, user_entity_set_list, include_relations=False, new_user_entity_set_id=None):
904 """ 905 Does the intersection between of the user Entities belonging to the user entities sets in the list and returns a new UserEntitySet 906 ------ 907 user_entity_set_list: list of user entity set objects to be combined 908 include_relations: flag to whether or not include relations in intersection 909 new_user_entity_set_id: identifier of new user entity set to be created as a result of the intersection 910 """ 911 912 if new_user_entity_set_id is None: 913 new_user_entity_set_id = self._get_next_uEs_id() 914 915 # Check if the user_entity_set_list contains the objects or the names 916 if isinstance(user_entity_set_list[0],str ) or isinstance(user_entity_set_list[0],int): 917 user_entity_set_list = [self.get_user_entity_set(x) for x in user_entity_set_list ] 918 919 if len(user_entity_set_list) <2: 920 sys.stderr.write("It is necessary to have at least 2 sets in order to do the intersection") 921 return 922 923 924 (listLevelSetId, listRelations) = user_entity_set_list[0].getIntersectionWithGivenUserEntitySet(user_entity_set_list[1], include_relations) 925 926 user_entity_set_new = UserEntitySet.UserEntitySet(id = new_user_entity_set_id, setIdUserEntity=None, listRelations = listRelations, listLevelSetIdUserEntity = listLevelSetId) 927 928 lenList = len(user_entity_set_list) 929 930 for i in xrange(lenList): 931 if i < 2: 932 continue 933 user_entity_set = user_entity_set_list[i] 934 (listLevelSetId, listRelations) = user_entity_set_new.getIntersectionWithGivenUserEntitySet(user_entity_set, include_relations) 935 user_entity_set_new = UserEntitySet.UserEntitySet(id = new_user_entity_set_id, setIdUserEntity=None, listRelations = listRelations, listLevelSetIdUserEntity = listLevelSetId) 936 937 self.dictUserEntitySet[user_entity_set_new.id] = user_entity_set_new 938 self._send_complete_user_entity_set_info(user_entity_set=user_entity_set_new) 939 940 return user_entity_set_new 941
942 - def remove_selected_user_entities(self, user_entity_set_id):
943 """ 944 Removes selected user entitites from given user entity set 945 ------ 946 user_entity_set_id: identifier of user entity set for which selected user entities will be removed 947 """ 948 949 user_entity_set = self.get_user_entity_set(user_entity_set_id) 950 951 selected = list(user_entity_set.getSelectedUserEntities()) # Needed to transform it to a list, because if not the selected set is modified during iteration 952 953 [ user_entity_set.remove_node(current_node) for current_node in selected ] 954 955 self.outmethod(self._get_xml(inner_content=user_entity_set._get_xml(inner_content="<remove_user_entities ids=\"%s\"/>" %",".join(map(str,selected))))) 956 957 return
958
959 - def remove_selected_relations(self, user_entity_set_id):
960 """ 961 Removes selected relations from given user entity set 962 ------ 963 user_entity_set_id: identifier of user entity set for which selected user entities will be removed 964 """ 965 966 user_entity_set = self.get_user_entity_set(user_entity_set_id) 967 selected = list(user_entity_set.getSelectedUserEntityRelations()) # Needed to transform it to a list, because if not the selected set is modified during iteration 968 969 #[ user_entity_set.remove_external_entity_relation(current_edge) for current_edge in selected ] 970 971 xml = [] 972 for current_eErID in selected: 973 participants = user_entity_set.get_external_entity_relation_participants(current_eErID) 974 for x in xrange(len(participants)): 975 for y in xrange(x): 976 xml.append( "<remove_user_entity_relations ids=\"%s,%s,%s\"/>" %(participants[x],participants[y],self.eEr_types_enum.get(self.eEr_types_dict[current_eErID])) ) 977 user_entity_set.remove_external_entity_relation(current_eErID) 978 979 980 self.outmethod(self._get_xml(inner_content=user_entity_set._get_xml(inner_content="".join(xml)))) 981 return
982 983 984 ###################### 985 ### OUTPUT METHODS ### 986 ###################### 987
988 - def output_ontology(self, ontology_object, additional_attributes = [], out_method=None):
989 """ 990 Outputs the ontology in XML Format 991 ------ 992 ontology_name: Ontology to be loaded 993 additional_attributes: Not implemented yet. TO DO!!! (Attribute "id" is the visual ID) 994 out_method: output method to be used if None overwritten by instance default output method 995 """ 996 997 #OutBianaInterface.send_process_message("Sending ontology data...") 998 999 print "outputing ontology..." 1000 1001 if out_method is None: 1002 out_method = self.outmethod 1003 1004 eEID_list = ontology_object.get_all_external_entity_ids() 1005 1006 #attribute = ontology_object.description_attribute 1007 1008 #eE_dict = self.dbAccess.get_external_entities_dict( externalEntityIdsList = eEID_list, 1009 # attribute_list = [attribute] ) 1010 1011 #attribute_field_restrictions = [(attribute,"taxid_name_type","")]) 1012 1013 # Create a dictionary with {eE: attribute} 1014 #nnd = dict( (x, "|".join([ y.value for y in eE_dict[x].get_attribute(attribute)])) for x in eE_dict ) 1015 1016 #out_method(ontology_object.get_xml(node_names_dict = nnd )) 1017 1018 #print "Outputed ontology..." 1019 1020 out_method(ontology_object.get_xml()) 1021 1022 #OutBianaInterface.send_end_process_message() 1023 1024 return
1025
1026 - def output_external_entity_relation_participant_details(self, external_entity_relation_id, out_method=None ):
1027 """ 1028 Outputs details of participants involved in a given external entry relation 1029 ------ 1030 external_entity_relation_id: relation identifier for which participant details will be outputted 1031 out_method: output method to be used if None overwritten by instance default output method 1032 1033 Called by BianaGUI/showTableDialog.java via output_external_entity_relation_details method 1034 """ 1035 # TO FINISH 1036 1037 if out_method is None: 1038 out_method = self.outmethod 1039 1040 columns = ["External Entity ID", "Source Database"] 1041 1042 values = [] 1043 1044 eEr_dict = self.dbAccess.get_external_entities_dict( externalEntityIdsList = [external_entity_relation_id] ) 1045 1046 eEr_obj = eEr_dict[external_entity_relation_id] 1047 1048 eE_dict = self.dbAccess.get_external_entities_dict( externalEntityIdsList = eEr_obj.get_participant_external_entity_ids_list() ) 1049 1050 for current_eE in eE_dict.values(): 1051 1052 current_values = [current_eE.get_id()] 1053 current_values.append( "%s" %self.dbAccess.get_external_database( database_id = current_eE.get_source_database() ) ) 1054 1055 values.append(current_values) 1056 1057 out_method(output_utilities.get_html_table(columns,values,attributes=[("title","Details for relation %s" %external_entity_relation_id)])) 1058 return
1059
1060 - def output_external_entity_relation_details(self, out_method=None, external_entity_relation_id_list=[], attributes=[], node_attributes=[], relation_attributes=[], participant_attributes=[]):
1061 """ 1062 Outputs details of external entry relations with given identifiers 1063 ------ 1064 external_entity_relation_id_list: list of relation identifiers for which details will be outputted 1065 #! attributes: Unknown 1066 node_attributes: attributes of user entities connected by these relations for which information will be fetched 1067 relation_attributes: attributes of external entity relations for which information will be fetched 1068 participant_attributes: attributes of external entity relation participants for which information will be fetched 1069 out_method: output method to be used if None overwritten by instance default output method 1070 """ 1071 1072 if out_method is None: 1073 out_method = self.outmethod 1074 1075 if not isinstance(attributes,list): 1076 attributes = attributes.split(",") 1077 1078 columns = ["External Entity Relation ID","External Database","Relation Type", "Participants"] 1079 columns.extend(attributes) 1080 1081 values = [] 1082 1083 command = "output_external_entity_relation_participant_details( external_entity_relation_id = ? )" 1084 1085 rowIDs = [] 1086 1087 eEr_dict = self.dbAccess.get_external_entities_dict( externalEntityIdsList = external_entity_relation_id_list, 1088 attribute_list = node_attributes, 1089 relation_attribute_list = relation_attributes, 1090 participant_attribute_list = participant_attributes ) 1091 1092 for current_eEr in eEr_dict.values(): 1093 1094 rowIDs.append(current_eEr.get_id()) 1095 current_values = [ current_eEr.get_id() ] 1096 current_values.append( "%s" %self.dbAccess.get_external_database( database_id = current_eEr.get_source_database()) ) 1097 current_values.append( current_eEr.get_relation_type() ) 1098 current_values.append( ",".join([ str(x) for x in current_eEr.get_participant_external_entity_ids_list()]) ) 1099 1100 values.append(current_values) 1101 1102 out_method(output_utilities.get_html_table(columns,values,rowIDs,attributes=[("title","External Entity Relation Details"),("command",command),("session",self.sessionID)])) 1103 return
1104 1105
1106 - def get_defined_node_attributes(self, user_entity_set_id, user_entity_id, attribute, output_1_value_per_attribute, return_set=False, substitute_node_attribute_if_not_exists=False):
1107 """ 1108 Gets user defined and BIANA defined node external names - adds substitution functionality to helper 1109 ------ 1110 user_entity_set_id: identifier for the user entity set information for whose user entity will be fetched 1111 user_entity_id: user_entity_id for the node for wich defined attributes are obtained 1112 attribute: attribute name corresponding to the attributes to be outputed 1113 output_1_value_per_attribute: Boolean. Defines wether 1 or multiple values are outputed per each attribute 1114 """ 1115 defined_attributes = self.get_defined_node_attributes_helper(user_entity_set_id, user_entity_id, attribute, output_1_value_per_attribute) 1116 #print defined_attributes 1117 1118 if substitute_node_attribute_if_not_exists: 1119 current_attribute = attribute 1120 copy_list = copy.deepcopy(self.substitution_list) 1121 while len(defined_attributes)==0: 1122 if current_attribute in copy_list: 1123 copy_list.remove(current_attribute) 1124 if len(copy_list) == 0: 1125 break 1126 current_attribute = copy_list[0] 1127 defined_attributes = self.get_defined_node_attributes_helper(user_entity_set_id, user_entity_id, current_attribute, output_1_value_per_attribute) 1128 defined_attributes = [ current_attribute+":"+i for i in defined_attributes ] 1129 #print defined_attributes 1130 1131 if return_set: 1132 defined_attributes = sets.Set(defined_attributes ) 1133 1134 return defined_attributes
1135 1136
1137 - def get_defined_node_attributes_helper(self, user_entity_set_id, user_entity_id, attribute, output_1_value_per_attribute):
1138 """ 1139 Gets user defined and BIANA defined user_entity_id external names 1140 ------ 1141 user_entity_set_id: identifier for the user entity set information for whose user entity will be fetched 1142 user_entity_id: user_entity_id for the user_entity_id for wich defined attributes are obtained 1143 attribute: attribute name corresponding to the attributes to be outputed 1144 output_1_value_per_attribute: Boolean. Defines wether 1 or multiple values are outputed per each attribute 1145 """ 1146 1147 ## sys.stderr.write("* Entering in get_defined_node_attributes_helper():\n") 1148 ## sys.stderr.write("*\tuser_entity_set_id : %s\n" %user_entity_set_id) 1149 ## sys.stderr.write("*\tnode : %s\n" %node) 1150 ## sys.stderr.write("*\tattribute : %s\n" %attribute) 1151 ## sys.stderr.write("*\toutput_1_value_per_attribute: %s\n" %output_1_value_per_attribute) 1152 1153 def get_more_represented(aList, preference_function=None): #lambda x: x): 1154 """ 1155 Helper method for get_defined_node_attributes_helper 1156 """ 1157 #aList.sort() 1158 aDict = {} 1159 for i in aList: 1160 if not aDict.has_key(i): aDict[i] = 0 1161 aDict[i] += 1 1162 1163 more_represented_list = [] 1164 n_more_represented = 0 1165 for k in aDict.keys(): 1166 n = aDict[k] 1167 if n > n_more_represented: 1168 more_represented_list = [k] 1169 n_more_represented = n 1170 elif n == n_more_represented: 1171 more_represented_list.append(k) 1172 1173 more_represented = None 1174 if preference_function is None and len(more_represented_list)>0: 1175 more_represented = more_represented_list[0] 1176 else: 1177 more_represented_score = -1 1178 for i in more_represented_list: 1179 score = preference_function(i) 1180 if score > more_represented_score: 1181 more_represented_score = score 1182 more_represented = i 1183 1184 return more_represented
1185 1186 attribute = attribute.lower() 1187 user_entity_set = self.dictUserEntitySet[user_entity_set_id] 1188 new_values = [] 1189 1190 attribute_values_dict = self.dbAccess.get_user_entity_attributes( unification_protocol_name = self.unification_protocol_name, 1191 listUserEntityID = [user_entity_id], 1192 attribute_identifier = attribute ) 1193 1194 attribute_values = attribute_values_dict.setdefault(user_entity_id, []) 1195 1196 #if output_1_value_per_attribute is False or ExternalEntityAttribute.isIdentifierType(attribute, self.dbAccess.biana_database) is False: # It should be done in other way... it should not access biana_database from dbAccess from here... 1197 if output_1_value_per_attribute is False: 1198 new_values.extend(attribute_values) 1199 else: 1200 user_attribute_values = [] 1201 # Check attribute node names provided by user 1202 if user_entity_set.userProvidedExternalIdsDict.has_key(attribute): 1203 temp_attribute_values = sets.Set([i.lower() for i in attribute_values]).intersection(sets.Set([i.lower() for i in user_entity_set.userProvidedExternalIdsDict[attribute]])) 1204 user_attribute_values = [user_entity_set.userProvidedExtIdsLowerDict[attribute][i] for i in temp_attribute_values] 1205 1206 # Check attribute user_entity_id names previously chosen by BianaSessionManager 1207 elif user_entity_set.selectedExternalIdsDict.has_key(attribute): 1208 temp_attribute_values = sets.Set([i.lower() for i in attribute_values]).intersection(sets.Set([i.lower() for i in user_entity_set.selectedExternalIdsDict[attribute]])) 1209 user_attribute_values = [user_entity_set.selectedExtIdsLowerDict[attribute][i] for i in temp_attribute_values] 1210 1211 if len(user_attribute_values) > 0: 1212 new_values = map(str, user_attribute_values) 1213 else: 1214 if attribute=="proteinsequence": 1215 more_represented = get_more_represented(aList = attribute_values, preference_function = len) 1216 else: 1217 more_represented = get_more_represented(attribute_values) 1218 user_entity_set._update_selected_external_ids_dict(attribute, more_represented) 1219 if more_represented is not None: 1220 new_values = [ more_represented ] 1221 1222 #print new_values 1223 return new_values 1224
1225 - def get_user_entity_set_attribute_network(self, user_entity_set_id, node_attribute):
1226 """ 1227 Gets a network of node attributes instead of user entity nodes 1228 ------ 1229 Deprecated - use get_defined_node_attributes instead 1230 """ 1231 1232 sys.stderr.write("DEPRECATED!\nMust use BianaSessionManager.get_defined_node_attributes()\n") # line added by Joan. 1233 1234 user_entity_set = self.dictUserEntitySet[user_entity_set_id] 1235 nodes = user_entity_set.get_user_entity_ids() 1236 edges = user_entity_set.getRelations() 1237 1238 added_nodes = sets.Set() 1239 1240 #print len(edges), " edges" 1241 1242 new_network = GraphUtilities.create_graph(allow_multi_edges=False) 1243 1244 new_edges_set = sets.Set() 1245 1246 for current_edge in edges: 1247 1248 1249 uEobj1 = self.get_user_entity(user_entity_id=current_edge[0]) 1250 uEobj2 = self.get_user_entity(user_entity_id=current_edge[1]) 1251 1252 added_nodes.add(current_edge[0]) 1253 added_nodes.add(current_edge[1]) 1254 1255 eE_dict1 = self.dbAccess.get_external_entities_dict( externalEntityIdsList = uEobj1.get_externalEntitiesIds_set(), 1256 attribute_list = [node_attribute] ) 1257 1258 eE_dict2 = self.dbAccess.get_external_entities_dict( externalEntityIdsList = uEobj2.get_externalEntitiesIds_set(), 1259 attribute_list = [node_attribute] ) 1260 1261 for current_eE1 in uEobj1.get_externalEntitiesIds_set(): 1262 for current_attribute1 in eE_dict1[current_eE1].get_attribute(attribute_identifier=node_attribute): 1263 for current_eE2 in uEobj2.get_externalEntitiesIds_set(): 1264 for current_attribute2 in eE_dict2[current_eE2].get_attribute(attribute_identifier=node_attribute): 1265 new_edges_set.add((current_attribute1.value.lower(),current_attribute2.value.lower())) 1266 1267 new_network.add_edges_from(new_edges_set) 1268 for current_node in nodes: 1269 if current_node not in added_nodes: 1270 uEobj = self.get_user_entity(user_entity_id=current_node) 1271 eE_dict = self.dbAccess.get_external_entities_dict( externalEntityIdsList = uEobj.get_externalEntitiesIds_set(), 1272 attribute_list = [node_attribute] ) 1273 for current_eE in uEobj.get_externalEntitiesIds_set(): 1274 for current_attribute in eE_dict[current_eE].get_attribute(attribute_identifier=node_attribute): 1275 new_network.add_node(current_attribute.value.lower()) 1276 1277 return new_network
1278 1279
1280 - def output_user_entity_set_group(self, user_entity_set_id, group_ids):
1281 """ 1282 "group_ids" must be the external entity relation that represents the group 1283 """ 1284 1285 OutBianaInterface.send_process_message("Getting information from database...") 1286 1287 1288 # First, check the parameters are correct 1289 user_entity_set = self.dictUserEntitySet[user_entity_set_id] 1290 1291 info = [] 1292 1293 for group_id in group_ids: 1294 if user_entity_set.has_group(group_id) is False: 1295 OutBianaInterface.send_error_notification("Group %s not in %s" %(group_id, user_entity_set_id)) 1296 else: 1297 external_entity_relation_id = int(group_id) 1298 1299 external_entity_relation_obj = self.dbAccess.get_external_entities_dict( externalEntityIdsList = [external_entity_relation_id], 1300 attribute_list = [], 1301 relation_attribute_list = [] )[external_entity_relation_id] 1302 1303 external_database_obj = self.dbAccess.get_external_database( database_id = external_entity_relation_obj.get_source_database() ) 1304 1305 1306 external_entity_relation_obj = self.dbAccess.get_external_entities_dict( externalEntityIdsList = [external_entity_relation_id], 1307 attribute_list = external_database_obj.get_valid_external_entity_relation_attribute_type(), 1308 relation_attribute_list = [] )[external_entity_relation_id] 1309 info.append(external_entity_relation_obj.__str__()) 1310 1311 OutBianaInterface.send_info_message("<html><br><br>".join(info)+"</html>") 1312 1313 # Then, print for the relation that is representing the group all its attributes 1314 1315 OutBianaInterface.send_end_process_message() 1316 1317 return
1318 1319
1320 - def output_user_entity_set_network_in_sif_format(self, user_entity_set_id, output_path = "./", output_prefix = "", node_attributes = [], participant_attributes = [], relation_attributes=[], include_tags=True, output_1_value_per_attribute=True, only_selected=False):
1321 """ 1322 Outputs information of the network of a given user entity set in sif format. User entity id will be taken as the node identifier. 1323 ------ 1324 output_path: directory in which Cytoscape attribute files will be generated 1325 output_prefix: prefix to be added to the begining of Cytoscape attribute files 1326 """ 1327 user_entity_set = self.dictUserEntitySet[user_entity_set_id] 1328 unconnected_nodes = user_entity_set.get_unconnected_nodes() 1329 if only_selected: 1330 nodes = user_entity_set.getSelectedUserEntities() 1331 unconnected_nodes &= nodes 1332 edges = user_entity_set.getRelationsOfSelectedUserEntities() 1333 else: 1334 edges = user_entity_set.getRelations() 1335 1336 import os 1337 if not os.path.exists(os.path.abspath(output_path)): 1338 sys.stderr.write("output_user_entity_set_network: given output path does not exist\n") 1339 return 1340 else: 1341 sif_file = open("%s/%s.sif" % (os.path.abspath(output_path), output_prefix), 'w') 1342 source_file = open("%s/%s_%s.eda" % (os.path.abspath(output_path), output_prefix, "source"), 'w') 1343 source_file.write("%s\n" % "SourceDB") 1344 node_attribute_files = {} 1345 for current_attribute in node_attributes: 1346 node_attribute_files[current_attribute] = open("%s/%s_%s.noa" % (os.path.abspath(output_path), output_prefix, current_attribute), 'w') 1347 node_attribute_files[current_attribute].write("%s\n" % current_attribute) 1348 for current_tag in user_entity_set.get_all_user_entity_tags(): 1349 node_attribute_files["tag_%s" % current_tag] = open("%s/%s_tag_%s.noa" % (os.path.abspath(output_path), output_prefix, current_tag), 'w') 1350 node_attribute_files["tag_%s" % current_tag].write("tag_%s\n" % current_tag) 1351 edge_attribute_files = {} 1352 for current_attribute in relation_attributes: 1353 edge_attribute_files[current_attribute] = open("%s/%s_%s.eda" % (os.path.abspath(output_path), output_prefix, current_attribute), 'w') 1354 edge_attribute_files[current_attribute].write("%s\n" % current_attribute) 1355 for current_tag in user_entity_set.get_all_user_entity_relation_tags(): 1356 edge_attribute_files["tag_%s" % current_tag] = open("%s/%s_tag_%s.eda" % (os.path.abspath(output_path), output_prefix, current_tag), 'w') 1357 edge_attribute_files["tag_%s" % current_tag].write("tag_%s\n" % current_tag) 1358 participant_attribute_files = {} 1359 for current_attribute in participant_attributes: 1360 participant_attribute_files[current_attribute] = open("%s/%s_%s.eda" % (os.path.abspath(output_path), output_prefix, current_attribute), 'w') 1361 participant_attribute_files[current_attribute].write("%s\n" % current_attribute) 1362 1363 ## Unconnected nodes 1364 for current_node in unconnected_nodes : 1365 sif_file.write("%s\n" %current_node) 1366 ## Node attributes 1367 for current_attribute in node_attributes: 1368 for current_value in self.get_defined_node_attributes(user_entity_set.id, current_node, current_attribute, output_1_value_per_attribute, substitute_node_attribute_if_not_exists = False, return_set = True): 1369 node_attribute_files[current_attribute].write("%s = %s\n" % (current_node, current_value)) 1370 ## Node tags 1371 if include_tags: 1372 for current_tag in user_entity_set.get_all_user_entity_tags(): 1373 node_attribute_files["tag_%s" % current_tag].write("%s = %s\n" % (current_node, user_entity_set.has_tag(current_node, current_tag))) 1374 1375 ## Edges and connected nodes 1376 eEr_dict = {} 1377 included_nodes = sets.Set() 1378 for current_edge in edges: 1379 eErIDs_list = user_entity_set.get_external_entity_relation_ids(current_edge[0], current_edge[1]) 1380 # Update dictionary 1381 for current_eErID in eErIDs_list: 1382 if not eEr_dict.has_key(current_eErID): 1383 eEr_dict.update(self.dbAccess.get_external_entities_dict( externalEntityIdsList = [current_eErID], 1384 attribute_list = node_attributes, 1385 relation_attribute_list = relation_attributes, 1386 participant_attribute_list = participant_attributes )) 1387 types = sets.Set() 1388 types2eErIDs_list = {} 1389 1390 for current_eEr_id in eErIDs_list: 1391 types.add(self.eEr_types_dict[current_eEr_id]) 1392 types2eErIDs_list.setdefault(self.eEr_types_dict[current_eEr_id], []).append(current_eEr_id) 1393 1394 ## Edges 1395 # Distinguish between relation type 1396 for current_relation_type in types: 1397 sif_file.write("%s (%s) %s\n" % (current_edge[0], self.eEr_types_enum.get(current_relation_type), current_edge[1])) 1398 inner_values = {} 1399 for current_uErID in types2eErIDs_list[current_relation_type]: 1400 if current_uErID > 0: 1401 source = eEr_dict[current_uErID].get_source_database() 1402 n = inner_values.setdefault(source, 0) 1403 inner_values[source] = n+1 1404 ## Relation attributes 1405 for current_attribute in relation_attributes: 1406 [ edge_attribute_files[current_attribute].write("%s (%s) %s = %s\n" % (current_edge[0], self.eEr_types_enum.get(current_relation_type), current_edge[1], str(y.value))) for y in eEr_dict[current_uErID].get_attribute(attribute_identifier = current_attribute) ] 1407 ## Participant attributes 1408 for current_participant in [current_edge[0],current_edge[1]]: 1409 for current_attribute in participant_attributes: 1410 [ participant_attribute_files[current_attribute].write("%s (%s) %s = %s\n" % (current_edge[0], self.eEr_types_enum.get(current_relation_type), current_edge[1], str(current_attr.value))) for current_eE in self.get_user_entity(user_entity_id = current_participant).get_externalEntitiesIds_set() for current_attr in eEr_dict[current_uErID].get_participant_attribute( participantExternalEntityID = current_eE, attribute_identifier = current_attribute ) ] 1411 ## Relation tags 1412 if include_tags: 1413 for current_tag in user_entity_set.get_all_user_entity_relation_tags(): 1414 edge_attribute_files["tag_%s" % current_tag].write("%s (%s) %s = %s\n" % (current_edge[0], self.eEr_types_enum.get(current_relation_type), current_edge[1], user_entity_set.relation_has_tag(current_uErID, current_tag) )) 1415 [ source_file.write("%s (%s) %s = %s(%s)\n" % (current_edge[0], self.eEr_types_enum.get(current_relation_type), current_edge[1], self.dbAccess.get_external_database(i).get_name(),j)) for i,j in inner_values.iteritems() ] 1416 1417 ## Connected node attributes 1418 for current_participant in [current_edge[0],current_edge[1]]: 1419 ## Node attributes 1420 if current_participant not in included_nodes: 1421 for current_attribute in node_attributes: 1422 [ node_attribute_files[current_attribute].write("%s = %s\n" % (current_participant, current_value)) for current_value in self.get_defined_node_attributes(user_entity_set.id, current_participant, current_attribute, output_1_value_per_attribute, substitute_node_attribute_if_not_exists = False, return_set = True) ] 1423 included_nodes.add(current_participant) 1424 1425 ## Node tags 1426 if include_tags: 1427 for current_tag in user_entity_set.get_all_user_entity_tags(): 1428 node_attribute_files["tag_%s" % current_tag].write("%s = %s\n" % (current_participant, user_entity_set.has_tag(current_participant, current_tag))) 1429 1430 sif_file.close() 1431 source_file.close() 1432 for current_attribute in node_attributes: 1433 node_attribute_files[current_attribute].close() 1434 for current_tag in user_entity_set.get_all_user_entity_tags(): 1435 node_attribute_files["tag_%s" % current_tag].close() 1436 for current_attribute in relation_attributes: 1437 edge_attribute_files[current_attribute].close() 1438 for current_tag in user_entity_set.get_all_user_entity_relation_tags(): 1439 edge_attribute_files["tag_%s" % current_tag].close() 1440 for current_attribute in participant_attributes: 1441 participant_attribute_files[current_attribute].close() 1442 1443 OutBianaInterface.send_end_process_message() 1444 return
1445 1446
1447 - def output_user_entity_set_network(self, user_entity_set_id, out_method=None, node_attributes = [], participant_attributes = [], relation_attributes=[], allowed_relation_types="all", include_participant_tags=True, include_relation_tags=True, include_relation_ids=True, include_participant_ids=True, include_relation_type=True, include_relation_sources=True, output_1_value_per_attribute=True, output_format="xml", value_seperator=", ", only_selected=False, include_command_in_rows=False, substitute_node_attribute_if_not_exists=False, include_unconnected_nodes=True):
1448 """ 1449 Outputs information of the network of a given user entity set 1450 ------ 1451 output_1_value_per_attribute: Boolean. Defines wether 1 or multiple values are outputed per each attribute 1452 output_format: format for the output used in case format is "table"; can be "tabulated" or "xml" 1453 include_relation_ids: Boolean to whether display or not relation identifiers 1454 include_participant_ids: Boolean to whether display or not relation participant identifiers 1455 include_relation_type: Boolean to whether display or not types of relations 1456 include_relation_sources: Boolean to whether display or not relation sources 1457 include_participant_tags: Boolean to whether display or not tags of participants 1458 include_relation_tags: Boolean to whether display or not tags of relations 1459 value_seperator: string to seperate consequitive values in the same column 1460 only_selected: Boolean to decide whether to output only selected nodes or all nodes (and their interactions) 1461 include_command_in_rows: Include the command to output individual relation information at each row 1462 substitute_node_attribute_if_not_exists: In case the node does not have a value for a given attribute (s.t. uniprotaccession) this flag make it possible to output another attribute (e.g. geneid) in the same column indicated as attribute:value (e.g. geneid:123123) 1463 include_unconnected_nodes: Boolean to whether display or not unconnected nodes 1464 """ 1465 1466 OutBianaInterface.send_process_message("Getting information from database...") 1467 user_entity_set = self.dictUserEntitySet[user_entity_set_id] 1468 1469 unconnected_nodes = user_entity_set.get_unconnected_nodes() 1470 if only_selected: 1471 nodes = user_entity_set.getSelectedUserEntities() 1472 unconnected_nodes &= nodes 1473 edges = user_entity_set.getRelationsOfSelectedUserEntities() 1474 else: 1475 edges = user_entity_set.getRelations() 1476 1477 if out_method is None: 1478 out_method = self.outmethod 1479 1480 # Check excluded relation types 1481 excluded_relation_types = {} 1482 1483 if not allowed_relation_types == "all": 1484 [ excluded_relation_types.update({relation_type:None}) for relation_type in self.dbAccess.get_valid_external_entity_relation_types() if not relation_type in allowed_relation_types ] 1485 1486 columns = [] 1487 if include_relation_ids: 1488 columns.append("Relation IDs") 1489 if include_relation_type: 1490 columns.append("Relation Types") 1491 if include_relation_tags: 1492 columns.append("Relation Tags") 1493 if include_participant_ids: 1494 columns.append("Participant 1 User Entity") 1495 columns.extend([ "Participant 1 %s" %x for x in node_attributes]) 1496 columns.extend([ "Participant 1 %s" %x for x in participant_attributes]) 1497 if include_participant_tags: 1498 columns.append("Participant 1 Tags") 1499 if include_participant_ids: 1500 columns.append("Participant 2 User Entity") 1501 columns.extend([ "Participant 2 %s" %x for x in node_attributes]) 1502 columns.extend([ "Participant 2 %s" %x for x in participant_attributes]) 1503 if include_participant_tags: 1504 columns.append("Participant 2 Tags") 1505 columns.extend([ "Relation %s" %x for x in relation_attributes]) 1506 if include_relation_sources: 1507 columns.append("Relation Source Databases") 1508 1509 if include_command_in_rows: 1510 command = "output_external_entity_relation_details(external_entity_relation_id_list=[?])" 1511 else: 1512 command = "" 1513 1514 if output_format == "xml": 1515 out_method(output_utilities.get_html_table_header( columns = columns, attributes = [ ("id", "user_entity_set_edges"), ("title","User Entity Set Network Details"),("command",command),("session",self.sessionID) ] ) ) 1516 elif output_format == "tabulated": # line added by Joan # REQUIRED TO TAG THE PRINTED COLUMNS 1517 out_method( output_utilities.get_tabulated_table(values = [columns]) ) # line added by Joan # REQUIRED TO TAG THE PRINTED COLUMNS 1518 1519 ## Unconnected node attributes 1520 if include_unconnected_nodes: 1521 for current_node in unconnected_nodes: 1522 new_values = [] 1523 if include_relation_ids: 1524 new_values.append("-") 1525 if include_relation_type: 1526 new_values.append("-") 1527 if include_relation_tags: 1528 new_values.append("-") 1529 if include_participant_ids: 1530 new_values.append(current_node) 1531 1532 current_rowID = "" # Added by Joan. 1533 for current_attribute in node_attributes: 1534 defined_attributes = self.get_defined_node_attributes(user_entity_set.id, current_node, current_attribute, output_1_value_per_attribute, substitute_node_attribute_if_not_exists = substitute_node_attribute_if_not_exists, return_set = True) 1535 temp_str = value_seperator.join(defined_attributes) 1536 if temp_str == "": 1537 new_values.append("-") 1538 else: 1539 new_values.append( temp_str ) #defined_attributes) ) 1540 [ new_values.append("-") for x in participant_attributes ] 1541 if include_participant_tags: 1542 tags = user_entity_set.get_user_entity_tags(current_node) 1543 if len(tags)==0: 1544 new_values.append("-") 1545 else: 1546 new_values.append(value_seperator.join(tags)) 1547 1548 if include_participant_ids: 1549 new_values.append("-") 1550 1551 [ new_values.append("-") for x in node_attributes ] 1552 [ new_values.append("-") for x in participant_attributes ] 1553 1554 if include_participant_tags: 1555 new_values.append("-") 1556 1557 for current_attribute in relation_attributes: 1558 new_values.append("-") 1559 1560 if include_relation_sources: 1561 new_values.append("-") 1562 1563 if output_format == "xml" : out_method(output_utilities.append_html_table_values( rowIDs = [current_rowID], values = [new_values] )) # Added by Joan. 1564 elif output_format == "tabulated": out_method( output_utilities.get_tabulated_table(values = [new_values]) ) # Added by Joan. 1565 else : raise ValueError("output_format is not valid. Valid output formats are ['xml', 'tabulated']\n") # Added by Joan. 1566 1567 # PRINT THE EDGES 1568 # Get the objects themselves 1569 eEr_dict = {} 1570 1571 for current_edge in edges: 1572 eErIDs_list = user_entity_set.get_external_entity_relation_ids(current_edge[0], current_edge[1]) 1573 # Update dictionary 1574 for current_eErID in eErIDs_list: 1575 if not eEr_dict.has_key(current_eErID): 1576 eEr_dict.update(self.dbAccess.get_external_entities_dict( externalEntityIdsList = [current_eErID], 1577 attribute_list = node_attributes, 1578 relation_attribute_list = relation_attributes, 1579 participant_attribute_list = participant_attributes )) 1580 types = sets.Set() 1581 types2eErIDs_list = {} 1582 1583 for current_eEr_id in eErIDs_list: 1584 types.add(self.eEr_types_dict[current_eEr_id]) 1585 types2eErIDs_list.setdefault(self.eEr_types_dict[current_eEr_id], []).append(current_eEr_id) 1586 1587 for current_relation_type in types: 1588 if excluded_relation_types.has_key(current_relation_type): continue 1589 new_values = [] 1590 current_rowID = "" 1591 1592 # RELATION HEADER 1593 relation_id_list = map(str,types2eErIDs_list[current_relation_type]) 1594 current_rowID = ",".join(relation_id_list) 1595 if include_relation_ids: 1596 new_values.append( value_seperator.join(relation_id_list) ) # Relation ID 1597 1598 if include_relation_type: 1599 new_values.append(self.eEr_types_enum.get(current_relation_type)) 1600 1601 if include_relation_tags: 1602 tags = [] 1603 relation_id_list = types2eErIDs_list[current_relation_type] 1604 #[ tags.extend(user_entity_set.get_user_entity_relation_tags(i)) for i in relation_id_list ] 1605 [ tags.extend(user_entity_set.get_external_entity_relation_tags(i)) for i in relation_id_list ] 1606 if len(tags)==0: 1607 new_values.append("-") 1608 else: 1609 new_values.append(value_seperator.join( tags )) 1610 1611 for current_participant in [current_edge[0],current_edge[1]]: 1612 if include_participant_ids: 1613 new_values.append(current_participant) 1614 for current_attribute in node_attributes: 1615 defined_attributes = self.get_defined_node_attributes(user_entity_set.id, current_participant, current_attribute, output_1_value_per_attribute, substitute_node_attribute_if_not_exists = substitute_node_attribute_if_not_exists, return_set = True) 1616 temp_str = value_seperator.join(defined_attributes) 1617 if temp_str == "": 1618 new_values.append("-") 1619 else: 1620 new_values.append( temp_str ) 1621 1622 for current_attribute in participant_attributes: 1623 defined_attributes = [ str(current_attr.value) for current_eE in self.get_user_entity(user_entity_id = current_participant).get_externalEntitiesIds_set() 1624 for current_eEr_id in current_edge[2] for current_attr in eEr_dict[current_eEr_id].get_participant_attribute( participantExternalEntityID = current_eE, 1625 attribute_identifier = current_attribute ) ] 1626 temp_str = value_seperator.join(defined_attributes) 1627 if temp_str == "": 1628 new_values.append("-") 1629 else: 1630 new_values.append( temp_str ) 1631 1632 if include_participant_tags: 1633 tags = user_entity_set.get_user_entity_tags(current_participant) 1634 if len(tags)==0: 1635 new_values.append("-") 1636 else: 1637 new_values.append(value_seperator.join(tags)) 1638 1639 # RELATION SPECIFIC 1640 for current_attribute in relation_attributes: 1641 inner_values = {} 1642 for current_uErID in types2eErIDs_list[current_relation_type]: 1643 if current_uErID > 0: 1644 for y in eEr_dict[current_uErID].get_attribute(attribute_identifier = current_attribute): 1645 n = inner_values.setdefault(str(y.value), 0) 1646 inner_values[str(y.value)] = n+1 1647 if len(inner_values) > 0: 1648 new_values.append(value_seperator.join([ "%s(%s)" % (i,j) for i,j in inner_values.iteritems()])) 1649 else: 1650 new_values.append("-") 1651 1652 if include_relation_sources: 1653 inner_values = {} 1654 for current_uErID in types2eErIDs_list[current_relation_type]: 1655 if current_uErID > 0: 1656 source = eEr_dict[current_uErID].get_source_database() 1657 n = inner_values.setdefault(source, 0) 1658 inner_values[source] = n+1 1659 new_values.append(value_seperator.join([ "%s(%s)" % (self.dbAccess.get_external_database(i).get_name(),j) for i,j in inner_values.iteritems()])) 1660 1661 if output_format == "xml" : out_method(output_utilities.append_html_table_values( rowIDs = [current_rowID], values = [new_values] )) 1662 elif output_format == "tabulated": out_method( output_utilities.get_tabulated_table(values = [new_values]) ) 1663 else : raise ValueError("output_format is not valid. Valid output formats are ['xml', 'tabulated']\n") 1664 1665 if output_format == "xml": 1666 out_method(output_utilities.get_html_table_foot()) 1667 OutBianaInterface.send_end_process_message() 1668 return
1669 1670
1671 - def output_user_entity_details(self, user_entity_set, user_entity_id_list, out_method = None, attributes = [], include_level_info = True, include_degree_info=True, include_tags_info=True, include_tags_linkage_degree_info=[], substitute_node_attribute_if_not_exists=False, output_1_value_per_attribute=True, output_format="tabulated", include_command_in_rows=False):
1672 """ 1673 Outputs given user entity. Called by output_user_entity_set_details. 1674 ------ 1675 user_entity_set: Instace of the user entity set, user entities of which will be outputted 1676 user_entity_id_list: list of user entities 1677 out_method: method to which output information will be directed 1678 attributes: list of attribute names corresponding to the attributes to be outputed 1679 include_level_info: Boolean. Defines inclussion of network level information in the output. 1680 include_degree_info: Boolean. Defines inclussion of node degree information in the output. 1681 inlcude_tags_info: Boolean. Defines inclussion of node tags information in the output. 1682 include_tags_linkage_degree_info: #! 1683 output_1_value_per_attribute: Boolean. Defines wether 1 or multiple values are outputed per each attribute 1684 output_format: format for the output. Either "tabulated" or "xml" 1685 include_command_in_rows: Include the command to output individual user entity information at each row 1686 """ 1687 1688 OutBianaInterface.send_process_message("Getting information from database...") 1689 1690 # Define columns 1691 columns = ["User Entity ID"] 1692 tld = {} 1693 if include_level_info: 1694 columns.append("Level") 1695 if include_degree_info: 1696 columns.append("Degree") 1697 if include_tags_info: 1698 columns.append("Tags") 1699 for current_tag_linkage_degree in include_tags_linkage_degree_info: 1700 columns.append("%s linkage degree" %current_tag_linkage_degree) 1701 tld[current_tag_linkage_degree] = user_entity_set.getTagLinkageDegree(tag = current_tag_linkage_degree) 1702 1703 # Add default identifier for the user entity 1704 # columns.append("Default Attribute") 1705 1706 1707 1708 columns.extend([ str(x) for x in attributes]) 1709 1710 if include_command_in_rows: 1711 #command = "output_external_entity_details(user_entity_id_list=[%s], attributes=[\'%s\'])" %( ",".join( [ str(uE_id) for uE_id in user_entity_id_list ] ), "\',\'".join(attributes)) 1712 #command = "output_external_entity_details(user_entity_id_list=[?], attributes=[\'%s\'])" %("\',\'".join(attributes)) 1713 command = "output_external_entity_details(user_entity_id_list=[?]" 1714 if len(attributes)>0: 1715 command += ", attributes=[\'%s\'])" %("\',\'".join(attributes)) 1716 else: 1717 command += ")" 1718 else: 1719 command = "" #"output_external_entity_details(user_entity_id_list=[...], attributes=[\'%s\'])" %(",".join(attributes)) 1720 1721 if out_method is None: 1722 out_method = self.outmethod 1723 1724 if output_format == "xml": 1725 out_method(output_utilities.get_html_table_header( columns = columns, attributes = [("id", "user_entity_set_nodes"), ("command",command),("title","User Entity Set Details"),("session",self.sessionID)] ) ) 1726 1727 elif output_format == "tabulated": # line added by Joan # REQUIRED TO TAG THE PRINTED COLUMNS 1728 out_method( output_utilities.get_tabulated_table(values = [columns]) ) # line added by Joan # REQUIRED TO TAG THE PRINTED COLUMNS 1729 1730 1731 ## 1732 ## # ---------------------------------------------- 1733 ## # Modified by Joan 1734 ## # Dict not needed any more as generalized in self.get_defined_node_attributes() 1735 ## # ---------------------------------------------- 1736 ## 1737 ## # THIS METHOD COULD BE SPEED UP IF ALL FOR EACH ATTRIBUTE WE EXECUTE ALL NODES AT THE SAME TIME, BUT IT MAY USE MORE MEMORY... 1738 ## attributes_dict = {} 1739 ## for current_attribute in attributes: 1740 ## attributes_dict[current_attribute] = self.dbAccess.get_user_entity_attributes( unification_protocol_name = self.unification_protocol_name, 1741 ## listUserEntityID = user_entity_id_list, 1742 ## attribute_identifier = current_attribute ) 1743 1744 for current_node in user_entity_id_list: 1745 1746 uEobj = self.get_user_entity(user_entity_id=current_node) 1747 1748 # TEMP COMMENTED JAVI 1749 #eE_dict = self.dbAccess.get_external_entities_dict( externalEntityIdsList = uEobj.get_externalEntitiesIds_set(), 1750 # attribute_list = attributes ) 1751 1752 new_values = [current_node] 1753 1754 if include_level_info: 1755 new_values.append(user_entity_set.getNodeLevel(current_node)) 1756 1757 if include_degree_info: 1758 new_values.append(user_entity_set.get_node_degree(current_node)) 1759 1760 if include_tags_info: 1761 tags = user_entity_set.get_user_entity_tags( user_entity_id = current_node ) 1762 if len(tags)>0: 1763 new_values.append( ", ".join(tags) ) 1764 else: 1765 new_values.append("-") 1766 1767 for current_tag_linkage_degree in include_tags_linkage_degree_info: 1768 new_values.append(user_entity_set.getUserEntityTagLinkageDegree( userEntityID = current_node, tag = current_tag_linkage_degree)) 1769 1770 1771 for current_attribute in attributes: 1772 #new_values.extend( self.get_defined_node_attributes(user_entity_set.id, current_node, current_attribute, output_1_value_per_attribute, substitute_node_attribute_if_not_exists = substitute_node_attribute_if_not_exists, return_set = True) ) 1773 attribute_values = self.get_defined_node_attributes(user_entity_set.id, current_node, current_attribute, output_1_value_per_attribute, substitute_node_attribute_if_not_exists = substitute_node_attribute_if_not_exists, return_set = True) 1774 temp_str = ", ".join(attribute_values) 1775 if temp_str != "": #len(attribute_values)>0: 1776 new_values.append( temp_str ) 1777 else: 1778 new_values.append("-") 1779 1780 # TEMP COMMENTED JAVI 1781 #current_attribute_values = [ str(y.value) for current_eE in uEobj.get_externalEntitiesIds_set() 1782 # for y in eE_dict[current_eE].get_attribute(attribute_identifier=current_attribute) ] 1783 1784 #current_attribute_values_dict = self.dbAccess.get_user_entity_attributes( unification_protocol_name = self.unification_protocol_name, 1785 # listUserEntityID = [current_node], 1786 # attribute_identifier = current_attribute ) 1787 1788 if output_format == "tabulated": 1789 out_method( output_utilities.get_tabulated_table(values = [new_values]) ) 1790 elif output_format == "xml": 1791 out_method(output_utilities.append_html_table_values( rowIDs = [current_node], values = [new_values] )) 1792 else: 1793 raise ValueError("output_format is not valid. Valid output formats are ['xml', 'tabulated']\n") 1794 1795 if output_format == "xml": 1796 out_method(output_utilities.get_html_table_foot()) 1797 1798 OutBianaInterface.send_end_process_message()
1799 1800
1801 - def output_user_entity_set_details(self, user_entity_set_id, out_method = None, attributes=[], include_level_info=True, include_degree_info=True, level=None, only_selected=False, output_format="tabulated", include_tags_info = True, include_tags_linkage_degree_info=[], substitute_node_attribute_if_not_exists=False, output_1_value_per_attribute=True, include_command_in_rows=False, output_only_native_values=False):
1802 """ 1803 Outputs given user entity set. 1804 ------ 1805 user_entity_set_id: Identifier of the user entity set, user entities of which will be outputted 1806 out_method: method to which output information will be directed 1807 attributes: list of attribute names corresponding to the attributes to be outputed 1808 include_level_info: Boolean. Defines inclussion of network level information in the output. 1809 include_degree_info: Boolean. Defines inclussion of node degree information in the output. 1810 inlcude_tags_info: Boolean. Defines inclussion of node tags information in the output. 1811 include_tags_linkage_degree_info: #! 1812 output_1_value_per_attribute: Boolean. Defines wether 1 or multiple values are outputed per each attribute 1813 output_format: format for the output. Either "tabulated" or "xml" 1814 only_selected: Boolean to decide whether to output only selected nodes or all nodes. "level" parameter is used only it is False 1815 include_command_in_rows: Include the command to output individual user entity information at each row 1816 output_only_native_values: If true, for each attribute, shows only values coming from the database providing that attribute by default 1817 """ 1818 1819 OutBianaInterface.send_process_message("Getting information from database...") 1820 1821 user_entity_set = self.dictUserEntitySet[user_entity_set_id] 1822 1823 if only_selected: 1824 nodes = user_entity_set.getSelectedUserEntities() 1825 else: 1826 nodes = user_entity_set.get_user_entity_ids(level=level) 1827 1828 if output_only_native_values: 1829 f_method = self.output_user_entity_details_with_only_defaults 1830 else: 1831 f_method = self.output_user_entity_details 1832 1833 f_method(user_entity_set = user_entity_set, 1834 user_entity_id_list=nodes, 1835 out_method = out_method, 1836 attributes = attributes, 1837 include_level_info = include_level_info, 1838 include_degree_info=include_degree_info, 1839 output_format=output_format, 1840 include_tags_info = include_tags_info, 1841 include_tags_linkage_degree_info = include_tags_linkage_degree_info, 1842 substitute_node_attribute_if_not_exists = substitute_node_attribute_if_not_exists, 1843 output_1_value_per_attribute = output_1_value_per_attribute, 1844 include_command_in_rows = include_command_in_rows) 1845 1846 OutBianaInterface.send_end_process_message()
1847 1848 1849 1850
1851 - def output_external_entity_details(self, out_method=None, attributes=[], user_entity_id_list=[]):
1852 """ 1853 Outputs a summary of the external entities given as a list 1854 ------ 1855 user_entity_id_list: list of user entities 1856 out_method: method to which output information will be directed 1857 attributes: list of attribute names corresponding to the attributes to be outputed 1858 1859 Called by BianaGUI/showTableDialog.java via output_user_entity_details method 1860 """ 1861 1862 OutBianaInterface.send_process_message("Getting information from database...") 1863 1864 if out_method is None: 1865 out_method = self.outmethod 1866 1867 if not isinstance(attributes,list): 1868 attributes = attributes.split(",") 1869 1870 columns = ["External Entity ID","User Entity ID","Type","Default attribute","External Database"] 1871 columns.extend(attributes) 1872 values = [] 1873 1874 list_eE_to_search_dict = dict([ (eE_id,x) for x in user_entity_id_list for eE_id in self.get_user_entity(x).get_externalEntitiesIds_set() ]) 1875 1876 if (len( list_eE_to_search_dict ) > 0 ): 1877 eE_dict = self.dbAccess.get_external_entities_dict( externalEntityIdsList = list_eE_to_search_dict.keys(), attribute_list = attributes ) 1878 else: 1879 raise ValueError("LIST CANNOT BE EMPTY FOR OUTPUT EXTERNAL ENTITY DETAILS...") 1880 1881 default_ids_dict = dict([ (x,"-") for x in eE_dict.keys() ]) 1882 default_ids_dict.update(self.dbAccess.get_default_external_entity_ids( externalEntityIDsList=eE_dict.keys() )) 1883 1884 for current_eE in eE_dict.values(): 1885 current_values = [ current_eE.get_id() ] 1886 current_values.append(list_eE_to_search_dict[current_eE.get_id()]) 1887 current_values.append( current_eE.get_type() ) 1888 current_values.append( default_ids_dict[current_eE.get_id()] ) 1889 1890 current_values.append("%s" %self.dbAccess.get_external_database( database_id = current_eE.get_source_database() ) ) 1891 1892 for current_attribute in attributes: 1893 attribute_values = [ str(y.value) for y in current_eE.get_attribute(attribute_identifier=current_attribute) ] 1894 temp_str = ",".join(attribute_values) 1895 #if len(attribute_values)>0: 1896 if temp_str != "": 1897 current_values.append( temp_str ) 1898 else: 1899 current_values.append("-") 1900 1901 values.append(current_values) 1902 1903 out_method(output_utilities.get_html_table(columns,values,attributes=[("title","External Entity Details")])) 1904 1905 OutBianaInterface.send_end_process_message()
1906
1907 - def output_user_entity_details_with_only_defaults(self, user_entity_set, user_entity_id_list, out_method = None, attributes = [], include_level_info = True, include_degree_info=True, include_tags_info=True, include_tags_linkage_degree_info=[], substitute_node_attribute_if_not_exists=False, output_1_value_per_attribute=True, output_format="tabulated", include_command_in_rows=False):
1908 """ 1909 Outputs given user entity (for each attribute, shows only values coming from the database providing that attribute by default). Called by output_user_entity_set_details. 1910 ------ 1911 user_entity_set: Instace of the user entity set, user entities of which will be outputted 1912 user_entity_id_list: list of user entities 1913 out_method: method to which output information will be directed 1914 attributes: list of attribute names corresponding to the attributes to be outputed 1915 include_level_info: Boolean. Defines inclussion of network level information in the output. 1916 include_degree_info: Boolean. Defines inclussion of node degree information in the output. 1917 inlcude_tags_info: Boolean. Defines inclussion of node tags information in the output. 1918 include_tags_linkage_degree_info: #! 1919 output_1_value_per_attribute: Boolean. Defines wether 1 or multiple values are outputed per each attribute 1920 output_format: format for the output. Either "tabulated" or "xml" 1921 include_command_in_rows: Include the command to output individual user entity information at each row 1922 """ 1923 1924 OutBianaInterface.send_process_message("Getting information from database...") 1925 1926 # Define columns 1927 columns = ["User Entity ID"] 1928 tld = {} 1929 if include_level_info: 1930 columns.append("Level") 1931 if include_degree_info: 1932 columns.append("Degree") 1933 if include_tags_info: 1934 columns.append("Tags") 1935 for current_tag_linkage_degree in include_tags_linkage_degree_info: 1936 columns.append("%s linkage degree" %current_tag_linkage_degree) 1937 tld[current_tag_linkage_degree] = user_entity_set.getTagLinkageDegree(tag = current_tag_linkage_degree) 1938 1939 columns.extend([ str(x) for x in attributes]) 1940 1941 if include_command_in_rows: 1942 command = "output_external_entity_details(user_entity_id_list=[?]" 1943 if len(attributes)>0: 1944 command += ", attributes=[\'%s\'])" %("\',\'".join(attributes)) 1945 else: 1946 command += ")" 1947 else: 1948 command = "" 1949 1950 if out_method is None: 1951 out_method = self.outmethod 1952 1953 if output_format == "xml": 1954 out_method(output_utilities.get_html_table_header( columns = columns, attributes = [("id", "user_entity_set_nodes"), ("command",command),("title","User Entity Set Details"),("session",self.sessionID)] ) ) 1955 1956 elif output_format == "tabulated": 1957 out_method( output_utilities.get_tabulated_table(values = [columns]) ) 1958 1959 1960 list_eE_to_search_dict = dict([ (eE_id,x) for x in user_entity_id_list for eE_id in self.get_user_entity(x).get_externalEntitiesIds_set() ]) 1961 1962 if (len( list_eE_to_search_dict ) > 0 ): 1963 eE_dict = self.dbAccess.get_external_entities_dict( externalEntityIdsList = list_eE_to_search_dict.keys(), attribute_list = attributes ) 1964 else: 1965 raise ValueError("LIST CANNOT BE EMPTY FOR OUTPUT EXTERNAL ENTITY DETAILS...") 1966 1967 default_ids_dict = dict([ (x,"-") for x in eE_dict.keys() ]) 1968 default_ids_dict.update(self.dbAccess.get_default_external_entity_ids( externalEntityIDsList=eE_dict.keys() )) 1969 1970 for current_node in user_entity_id_list: 1971 1972 uEobj = self.get_user_entity(user_entity_id=current_node) 1973 1974 # TEMP COMMENTED JAVI 1975 #eE_dict = self.dbAccess.get_external_entities_dict( externalEntityIdsList = uEobj.get_externalEntitiesIds_set(), 1976 # attribute_list = attributes ) 1977 1978 new_values = [current_node] 1979 1980 if include_level_info: 1981 new_values.append(user_entity_set.getNodeLevel(current_node)) 1982 1983 if include_degree_info: 1984 new_values.append(user_entity_set.get_node_degree(current_node)) 1985 1986 if include_tags_info: 1987 tags = user_entity_set.get_user_entity_tags( user_entity_id = current_node ) 1988 if len(tags)>0: 1989 new_values.append( ", ".join(tags) ) 1990 else: 1991 new_values.append("-") 1992 1993 for current_tag_linkage_degree in include_tags_linkage_degree_info: 1994 new_values.append(user_entity_set.getUserEntityTagLinkageDegree( userEntityID = current_node, tag = current_tag_linkage_degree)) 1995 1996 1997 for current_attribute in attributes: 1998 attribute_values = [] 1999 for current_eE in eE_dict.values(): 2000 if current_node != list_eE_to_search_dict[current_eE.get_id()]: 2001 continue 2002 default_id = default_ids_dict[current_eE.get_id()] 2003 if default_id.startswith(current_attribute): 2004 attribute_values.append(default_id[default_id.find(":")+1:]) 2005 #attribute_values.extend([ str(y.value) for y in current_eE.get_attribute(attribute_identifier=current_attribute) ]) 2006 temp_str = ", ".join(attribute_values) 2007 if temp_str != "": #len(attribute_values)>0: 2008 new_values.append( temp_str ) 2009 else: 2010 new_values.append("-") 2011 2012 # TEMP COMMENTED JAVI 2013 #current_attribute_values = [ str(y.value) for current_eE in uEobj.get_externalEntitiesIds_set() 2014 # for y in eE_dict[current_eE].get_attribute(attribute_identifier=current_attribute) ] 2015 2016 #current_attribute_values_dict = self.dbAccess.get_user_entity_attributes( unification_protocol_name = self.unification_protocol_name, 2017 # listUserEntityID = [current_node], 2018 # attribute_identifier = current_attribute ) 2019 2020 if output_format == "tabulated": 2021 out_method( output_utilities.get_tabulated_table(values = [new_values]) ) 2022 elif output_format == "xml": 2023 out_method(output_utilities.append_html_table_values( rowIDs = [current_node], values = [new_values] )) 2024 else: 2025 raise ValueError("output_format is not valid. Valid output formats are ['xml', 'tabulated']\n") 2026 2027 if output_format == "xml": 2028 out_method(output_utilities.get_html_table_foot()) 2029 2030 OutBianaInterface.send_end_process_message()
2031 2032 2033
2034 - def __str__(self):
2035 return "BIANA Session with %d User Entity Sets: \n%s\n" % ( len(self.dictUserEntitySet), "\n".join([ "%s" % user_entity_set for user_entity_set_id, user_entity_set in self.dictUserEntitySet.iteritems() ]) )
2036 2037
2038 - def deliver_error_message(self, message):
2039 OutBianaInterface.send_error_notification(error="BIANA Error!", message=message)
2040 2041
2042 - def output_user_entity_set_sequences_in_fasta(self, user_entity_set_id, only_selected = False, out_method = None, type="proteinsequence", attributes = [], include_tags_info=True, one_line_per_sequence=False, output_1_value_per_attribute=None, output_only_native_values=None):
2043 """ 2044 Outputs sequences associated with given user entity set in fasta format. 2045 ------ 2046 user_entity_set_id: Id of user entity set containing user entities whose sequence will be outputted 2047 only_selected: Includes all user entites if False, otherwise only selected ones 2048 out_method: method to which output information will be directed 2049 type: either "proteinsequence" or "nucleotidesequence" 2050 attributes: list of attribute names corresponding to the attributes to be outputed as sequence header 2051 inlcude_tags_info: Boolean. Defines inclussion of node tags information in the output. 2052 2053 Called by BianaGUI/ViewUserEntityDetails.java 2054 """ 2055 2056 if output_only_native_values is not None or output_1_value_per_attribute is not None: 2057 OutBianaInterface.send_error_notification(message="Output Error!" ,error="Displaying only single/native value feature is not available in FASTA formatted output.") 2058 return 2059 2060 user_entity_set = self.dictUserEntitySet[user_entity_set_id] 2061 2062 if only_selected: # get selected user entities of this set 2063 user_entity_id_list = user_entity_set.getSelectedUserEntities() 2064 else: # get all user entities of this set 2065 user_entity_id_list = user_entity_set.get_user_entity_ids() 2066 2067 if out_method is None: 2068 out_method = self.outmethod 2069 2070 columns = ["externalentityid","userentityid","type","defaultattribute","externaldatabase"] 2071 if include_tags_info: 2072 columns.append("tags") 2073 2074 new_attributes = [] 2075 for x in attributes: 2076 if x.lower() == "proteinsequence": 2077 pass 2078 elif x.lower() == "nucleotidesequence": 2079 pass 2080 else: 2081 columns.append(x.lower()) 2082 new_attributes.append(x) 2083 2084 if type == "proteinsequence" or "nucleotidesequence": 2085 new_attributes.append(type) 2086 else: 2087 raise Exception("Unrecognized sequence type: %s !" % type) 2088 2089 values = [] 2090 sequences = [] 2091 2092 list_eE_to_search_dict = dict([ (eE_id,x) for x in user_entity_id_list for eE_id in self.get_user_entity(x).get_externalEntitiesIds_set() ]) 2093 2094 if (len( list_eE_to_search_dict ) > 0 ): 2095 eE_dict = self.dbAccess.get_external_entities_dict( externalEntityIdsList = list_eE_to_search_dict.keys(), attribute_list = new_attributes ) 2096 else: 2097 raise Exception("LIST CANNOT BE EMPTY FOR OUTPUT EXTERNAL ENTITY DETAILS") 2098 2099 default_ids_dict = dict([ (x,"-") for x in eE_dict.keys() ]) 2100 default_ids_dict.update(self.dbAccess.get_default_external_entity_ids( externalEntityIDsList=eE_dict.keys() )) 2101 2102 for current_eE in eE_dict.values(): 2103 2104 current_values = [ current_eE.get_id() ] 2105 uE_id = list_eE_to_search_dict[current_eE.get_id()] 2106 current_values.append(uE_id) 2107 current_values.append( current_eE.get_type() ) 2108 default_id = default_ids_dict[current_eE.get_id()] 2109 if default_id == "": 2110 default_id = "-" 2111 current_values.append(default_id) 2112 current_values.append("%s" %self.dbAccess.get_external_database( database_id = current_eE.get_source_database() ) ) 2113 2114 if include_tags_info: 2115 tags = user_entity_set.get_user_entity_tags( user_entity_id = uE_id ) 2116 if len(tags)>0: 2117 current_values.append( ",".join(tags) ) 2118 else: 2119 current_values.append("-") 2120 2121 for current_attribute in new_attributes: 2122 attribute_values = [ str(y.value) for y in current_eE.get_attribute(attribute_identifier=current_attribute) ] 2123 2124 if current_attribute == "proteinsequence" or current_attribute == "nucleotidesequence": 2125 if len(attribute_values)>0: 2126 sequences.append(",".join(attribute_values)) 2127 else: 2128 sequences.append("-") 2129 else: 2130 temp_str = ",".join(attribute_values) 2131 2132 if temp_str != "": 2133 current_values.append( temp_str ) 2134 else: 2135 current_values.append("-") 2136 2137 values.append(current_values) 2138 2139 import biana.utilities.FastaWriter as FastaWriter 2140 fasta_writer = FastaWriter.FastaWriter(out_method = out_method, one_line_per_sequence=one_line_per_sequence) 2141 2142 for current_values, sequence in zip(values, sequences): 2143 2144 sequence_header = "|".join( map((lambda x, y: x+'|'+str(y)), columns, current_values) ) 2145 [ fasta_writer.output_sequence(sequence_header = sequence_header, sequence = seq) for seq in sequence.split(",")] 2146 2147 return
2148 2149 2150 # BIANA REPORT METHODS # 2151
2152 - def initReport(self, reportOutputMethod, reportTitle, reportFormat, url):
2153 """ 2154 Inits the Biana report where a description of the experiment and results will be written 2155 2156 "reportTitle" is the title that will be written on top of the report (default is 'BIANA report') 2157 2158 "reportOutputMethod" is the write method where the report will be written (e.g. file_object.write) 2159 --> if no streamOutputMethod is given, report is written to stdout 2160 2161 "reportFormat" is the formatting that will be applied to the report 2162 - 'html': report in html 2163 - 'txt': report in raw text 2164 2165 "url" is the URL where the interface to BIANA is placed 2166 """ 2167 self.report = BianaReport(title=reportTitle, streamOutputMethod=reportOutputMethod, 2168 format=reportFormat, url=url) 2169 2170 return
2171
2172 - def closeReport(self, reportOutputMethod, reportFormat):
2173 """ 2174 Closes the Biana report. This method is in charge of writing the closing HTML tags of the report 2175 2176 "reportOutputMethod" is the write method where the report will be written (e.g. file_object.write) 2177 --> if no streamOutputMethod is given, report is written to stdout 2178 2179 "reportFormat" is the formatting that will be applied to the report 2180 - 'html': report in html 2181 - 'txt': report in raw text 2182 2183 Attention!!! This method does not close the file object associated to the report. The user is responsible 2184 for closing the file (if needed). 2185 """ 2186 2187 if self.report is None: 2188 2189 raise ValueError("This BIANA session does not have an associated report. You should init one using initReport()") 2190 self.report.closeReport() 2191 2192 return
2193
2194 - def addTextToReport(self, theText=None, type="regular" ):
2195 """ 2196 This method adds text to the report. 2197 2198 Depending on the 'type' of text, it will be written one way or another 2199 2200 - 'regular' prints standard text 2201 - 'title' prints a section title 2202 - 2203 2204 """ 2205 2206 if self.report is None: 2207 2208 raise ValueError("This BIANA session does not have an associated report. You should init one using initReport()") 2209 2210 self.report.addText(theText=theText, type=type )
2211 2212
2213 - def addResultToReport(self, resultFileName=None, resultFilePath=None, associatedText=None, bulletedList=[] ):
2214 """ 2215 This method adds information to the resport about a result file. 2216 2217 'resultFileName' is the name you want to give to this result file 2218 2219 'resultFilePath' is the path (or URL) of the result file (e.g. http://localhost/this_file.html 2220 2221 'associatedText' is the text shown next to the results file name (i.e description of the file) 2222 2223 'bulletedList' is a list of strings that will be shown under the results name as a bulleted list 2224 2225 """ 2226 2227 if self.report is None: 2228 2229 raise ValueError("This BIANA session does not have an associated report. You should init one using initReport()") 2230 self.report.addResult(resultFileName=resultFileName, 2231 resultFilePath=resultFilePath, 2232 associatedText=associatedText, 2233 bulletedList=bulletedList )
2234 2235 # OTHERS #
2236 - def get_new_user_entity_set_from_user_entities(self, user_entity_id_list, new_user_entity_set_id=None, external_entity_attribute_restriction_list=[]):
2237 """ 2238 Creates a new user entity set from given user entities. 2239 ------ 2240 user_entity_id_list: user entities to be included new user entity set 2241 user_entity_set_id: identifier of user entity set to be created 2242 external_entity_attribute_restriction_list: Restrictions to not to include user entities with external entities having those restriction values. List of tuples provided by the user containing external entity attribute restrictons to be applied. They will be used always in the set (network creation, etc) 2243 2244 #! Not called by any other method 2245 """ 2246 2247 if new_user_entity_set_id is None: 2248 new_user_entity_set_id = self._get_next_uEs_id() 2249 2250 user_entity_set_new = UserEntitySet.UserEntitySet( id = new_user_entity_set_id, 2251 setIdUserEntity = user_entity_id_list ) 2252 2253 user_entity_set_new.addRestriction("attribute_restrictions", external_entity_attribute_restriction_list) 2254 2255 self.dictUserEntitySet[user_entity_set_new.id] = user_entity_set_new 2256 2257 return user_entity_set_new
2258
2259 - def get_sub_user_entity_set(self, user_entity_set_id, include_relations=False, new_user_entity_set_id=None):
2260 """ 2261 Creates a new user entity set using selected user entities 2262 ------ 2263 user_entity_set_id: identifier of user entity set from whose user entities a new one will be created 2264 new_user_entity_set_id: identifier of user entity set to be created 2265 include_relations: flag to whether or not include relations in the new user entity set 2266 """ 2267 2268 if new_user_entity_set_id is None: 2269 new_user_entity_set_id = self._get_next_uEs_id() 2270 2271 user_entity_set = self.dictUserEntitySet[user_entity_set_id] 2272 2273 #self.idLastUserEntitySet += 1 2274 selected_user_entities = user_entity_set.getSelectedUserEntities() 2275 selected_relations = user_entity_set.getRelationsOfSelectedUserEntities() 2276 if include_relations: 2277 user_entity_set_new = UserEntitySet.UserEntitySet( id = new_user_entity_set_id, 2278 setIdUserEntity = selected_user_entities, 2279 listRelations = selected_relations) 2280 else: 2281 user_entity_set_new = UserEntitySet.UserEntitySet( id = new_user_entity_set_id, 2282 setIdUserEntity = selected_user_entities) 2283 2284 user_entity_set_new.addRestriction("attribute_restrictions", user_entity_set.getRestrictions("attribute_restrictions")) 2285 2286 self.dictUserEntitySet[user_entity_set_new.id] = user_entity_set_new 2287 2288 #self.outmethod(self._get_xml(inner_content="<new_user_entity_set id=\"%s\"/>" %(new_user_entity_set_id))) 2289 #self.outmethod(self._get_xml(inner_content=user_entity_set_new._get_xml(inner_content="all"))) 2290 self._send_complete_user_entity_set_info( user_entity_set_new ) 2291 2292 return user_entity_set_new
2293 2294
2295 - def unselect_user_entities_from_user_entity_set(self, user_entity_set_id):
2296 """ 2297 Clears selection of user entities in a given user entity set 2298 ------ 2299 user_entity_set_id: identifier of user entity set 2300 """ 2301 user_entity_set = self.get_user_entity_set(user_entity_set_id) 2302 user_entity_set.clear_user_entity_selection() 2303 self.outmethod(self._get_xml(user_entity_set._get_xml(inner_content="<clear_user_entity_selection/>"))) 2304 return
2305
2306 - def unselect_user_entity_relations_from_user_entity_set(self, user_entity_set_id):
2307 """ 2308 Clears selection of relations in a given user entity set 2309 ------ 2310 user_entity_set_id: identifier of user entity set 2311 """ 2312 user_entity_set = self.get_user_entity_set(user_entity_set_id) 2313 user_entity_set.clear_user_entity_relation_selection() 2314 self.outmethod(self._get_xml(user_entity_set._get_xml(inner_content="<clear_user_entity_relation_selection/>"))) 2315 return
2316
2317 - def select_all_user_entities(self, user_entity_set_id):
2318 """ 2319 Selects all user entities in a given user entity set 2320 ------ 2321 user_entity_set_id: identifier of user entity set 2322 """ 2323 user_entity_set = self.get_user_entity_set(user_entity_set_id) 2324 user_entity_id_list = user_entity_set.get_user_entity_ids() 2325 self.select_user_entities_from_user_entity_set( user_entity_set_id = user_entity_set_id, 2326 user_entity_id_list = user_entity_id_list ) 2327 return
2328 2329
2330 - def select_all_user_entity_relations(self, user_entity_set_id):
2331 """ 2332 Selects all relations in a given user entity set 2333 ------ 2334 user_entity_set_id: identifier of user entity set 2335 """ 2336 user_entity_set = self.get_user_entity_set(user_entity_set_id) 2337 #user_entity_relation_id_list = user_entity_set.getRelations() 2338 self.select_user_entity_relations_from_user_entity_set( user_entity_set_id = user_entity_set_id, 2339 #user_entity_relation_id_list = user_entity_relation_id_list ) 2340 external_entity_relation_ids_list = user_entity_set.get_external_entity_relation_ids() ) 2341 2342 #self.select_user_entity_relations_from_user_entity_set( user_entity_set_id = user_entity_set_id, 2343 # user_entity_relation_id_list = user_entity_relation_id_list ) 2344 return
2345 2346
2347 - def select_user_entities_from_user_entity_set(self, user_entity_set_id, user_entity_id_list, clear_previous_selection = False):
2348 """ 2349 Select user entities with given id in a given user entity set 2350 ------ 2351 user_entity_set_id: identifier of user entity set 2352 user_entity_id_list: list of user entity identifiers 2353 clear_previous_selection: Boolean to clear or not previous selection 2354 """ 2355 user_entity_set = self.dictUserEntitySet[user_entity_set_id] 2356 2357 xml = [] 2358 2359 if clear_previous_selection: 2360 self.unselect_user_entities_from_user_entity_set(user_entity_set_id) 2361 2362 2363 user_entity_set.select_user_entities(user_entity_id_list) 2364 2365 # To send it one by one 2366 #xml.extend([ "<select_user_entity id=\"%s\"/>" %x for x in user_entity_id_list ]) 2367 xml.append( "<select_user_entity id=\"%s\"/>" %",".join(map(str,user_entity_id_list)) ) 2368 2369 self.outmethod(self._get_xml(inner_content=user_entity_set._get_xml(inner_content="".join(xml)))) 2370 return
2371
2372 - def select_user_entity_relations_from_user_entity_set(self, user_entity_set_id, user_entity_relation_id_list=[], clear_previous_selection = False, external_entity_relation_ids_list = []):
2373 """ 2374 Select user entities with given id in a given user entity set 2375 ------ 2376 user_entity_set_id: identifier of user entity set 2377 user_entity_relation_id_list: list of relation identifiers: (userEntityID1, userEntityID2, relation_type) 2378 clear_previous_selection: Boolean to clear or not previous selection 2379 """ 2380 2381 user_entity_set = self.dictUserEntitySet[user_entity_set_id] 2382 2383 2384 if clear_previous_selection: 2385 self.unselect_user_entity_relations_from_user_entity_set(user_entity_set_id) 2386 2387 eErID_list = [] 2388 2389 for node1, node2, relation_type in user_entity_relation_id_list: 2390 relation_type_enum = self.eEr_types_enum.get_letter(relation_type) 2391 for current_eErID in user_entity_set.get_external_entity_relation_ids(node1,node2): 2392 if self.eEr_types_dict[current_eErID]==relation_type_enum: 2393 eErID_list.append(current_eErID) 2394 2395 eErID_list.extend(external_entity_relation_ids_list) 2396 2397 user_entity_set.selectUserEntityRelations(eErID_list) 2398 2399 #user_entity_set.selectUserEntityRelations(user_entity_relation_id_list) 2400 2401 # To send it one by one 2402 #xml = [ "<select_user_entity_relation id=\"%s\"/>" %",".join([",".join(map(str,x)) for x in user_entity_relation_id_list]) ] 2403 xml = [] 2404 for current_eErID in eErID_list: 2405 participants = user_entity_set.get_external_entity_relation_participants(current_eErID) 2406 for x in xrange(len(participants)): 2407 for y in xrange(x): 2408 xml.append( "<select_user_entity_relation id=\"%s,%s,%s\"/>" %(participants[x],participants[y],self.eEr_types_enum.get(self.eEr_types_dict[current_eErID])) ) 2409 2410 self.outmethod(self._get_xml(inner_content=user_entity_set._get_xml(inner_content="".join(xml)))) 2411 2412 return
2413 2414
2415 - def tag_selected_user_entities( self, user_entity_set_id, tag):
2416 """ 2417 Tags selected user entities in a given user entity set with the given tag 2418 ------ 2419 user_entity_set_id: identifier of user entity set 2420 tag: tag string 2421 """ 2422 user_entity_set = self.dictUserEntitySet[user_entity_set_id] 2423 2424 #print user_entity_set.hasTag( tag = tag ) 2425 if user_entity_set.hasTag( tag = tag ) is False: 2426 self.outmethod(self._get_xml(inner_content=user_entity_set._get_xml(inner_content="<new_tag tag=\"%s\"/>" %tag))) 2427 2428 user_entity_set.addTagToSelectedUE(tag=tag) 2429 return
2430
2431 - def tag_selected_user_entity_relations( self, user_entity_set_id, tag):
2432 """ 2433 Tags selected relations in a given user entity set with the given tag 2434 ------ 2435 user_entity_set_id: identifier of user entity set 2436 tag: tag string 2437 """ 2438 2439 user_entity_set = self.dictUserEntitySet[user_entity_set_id] 2440 2441 #print user_entity_set.hasTag( tag = tag ) 2442 if user_entity_set.relationHasTag( tag = tag ) is False: 2443 self.outmethod(self._get_xml(inner_content=user_entity_set._get_xml(inner_content="<new_relation_tag tag=\"%s\"/>" %tag))) 2444 2445 user_entity_set.addTagToSelectedUER(tag=tag) 2446 return
2447
2448 - def select_user_entities_using_attributes(self, user_entity_set_id, identifier_description_list, id_type="embedded", external_entity_attribute_restriction_list=[], negative_external_entity_attribute_restriction_list=[], clear_previous_selection = False ):
2449 """ 2450 Selects user entities in a given user entity set by given attributes 2451 ------ 2452 user_entity_set_id: identifier of user entity set 2453 identifier_description_list: list of identifiers (or (identifier, id_type) tuples in case id_type is "embedded") 2454 id_type: type of the identifiers in the file, if "embedded" then file contains (attribute_name, identifier) tuples instead of just identifiers 2455 external_entity_attribute_restriction_list: Restrictions to not to include user entities with external entities having those restriction values. List of tuples provided by the user containing external entity attribute restrictons to be applied. 2456 clear_previous_selection: Boolean to clear or not previous selection 2457 """ 2458 user_entity_set = self.dictUserEntitySet[user_entity_set_id] 2459 2460 if clear_previous_selection: 2461 self.unselect_user_entities_from_user_entity_set(user_entity_set_id) 2462 2463 # Gets the input to build the user entity set (root or seed user entities) 2464 dictTypeToName = self._convert_attribute_list_to_attribute_dictionary(identifier_description_list, id_type) 2465 2466 OutBianaInterface.send_process_message("Getting information from database...") 2467 2468 ## for each different type of id_type's fetch userEntity ids associated with given externalEntity ids 2469 for identifierType, setIdentifierName in dictTypeToName.iteritems(): 2470 2471 # javi added: 2472 if( identifierType.lower()=="userentityid" ): 2473 user_entity_id_list = [int(identifierName)] 2474 2475 field_values = [] 2476 2477 for identifierName in setIdentifierName: 2478 field_values.append(("value",identifierName)) 2479 2480 #user_entity_id_list = self.dbAccess.get_user_entity_ids_list(unification_protocol_name = self.unification_protocol_name, 2481 # attribute_identifier = identifierType, 2482 # field_values = field_values, 2483 # attribute_restrictions = external_entity_attribute_restriction_list, 2484 # restrict_to_user_entity_ids_list = user_entity_set.get_user_entity_ids() ) 2485 2486 user_entity_id_list = self.dbAccess.get_list_user_entities_IDs_by_attribute(unification_protocol_name = self.unification_protocol_name, 2487 attribute_identifier = identifierType, 2488 field_values = field_values, 2489 attribute_restrictions = external_entity_attribute_restriction_list, 2490 negative_attribute_restrictions = negative_external_entity_attribute_restriction_list, 2491 restrict_to_user_entity_ids_list = user_entity_set.get_user_entity_ids() ) 2492 2493 #print user_entity_id_list 2494 2495 self.select_user_entities_from_user_entity_set( user_entity_set_id = user_entity_set_id, user_entity_id_list = user_entity_id_list, clear_previous_selection = False) 2496 2497 2498 OutBianaInterface.send_end_process_message() 2499 2500 return
2501 2502
2503 - def select_user_entity_relations_using_attributes(self, user_entity_set_id, identifier_description_list, id_type="embedded", relation_attribute_restriction_list=[], relation_type_list=[], clear_previous_selection = False ):
2504 """ 2505 Selects relations in a given user entity set by given attributes 2506 ------ 2507 user_entity_set_id: identifier of user entity set 2508 identifier_description_list: list of identifiers (or (identifier, id_type) tuples in case id_type is "embedded") 2509 id_type: type of the identifiers in the file, if "embedded" then file contains (attribute_name, identifier) tuples instead of just identifiers 2510 realation_attribute_restrictions: Restrictions to not to include relations with external entity relations having those restriction values. List of tuples provided by the user containing external entity attribute restrictons to be applied. 2511 relation_type_list: types of relations to be included in selection 2512 2513 #! Not called by any method 2514 """ 2515 2516 user_entity_set = self.dictUserEntitySet[user_entity_set_id] 2517 2518 if clear_previous_selection: 2519 self.unselect_user_entity_relations_from_user_entity_set(user_entity_set_id) 2520 2521 # Gets the input to build the user entity set (root or seed user entities) 2522 dictTypeToName = self._convert_attribute_list_to_attribute_dictionary(identifier_description_list, id_type ) # ASK JAVI.... is here anything to be changed???? 2523 2524 OutBianaInterface.send_process_message("Getting information from database...") 2525 2526 ## for each different type of id_type's fetch userRelatinoEntity ids associated with given externalRelationEntity ids 2527 for identifierType, setIdentifierName in dictTypeToName.iteritems(): 2528 2529 # javi added: 2530 #if( identifierType.lower()=="userentityrelationid" ): 2531 # user_entity_relation_id_list = [int(identifierName)] 2532 2533 field_values = [] 2534 2535 for identifierName in setIdentifierName: 2536 field_values.append(("value",identifierName)) 2537 2538 relation_type_list.extend(user_entity_set.getRestrictions("relation_type_restrictions")) 2539 #realation_attribute_restrictions.extend(user_entity_set.getRestrictions("relation_attribute_restrictions")) 2540 dictAttributeToValues = self._convert_attribute_list_to_attribute_dictionary(realation_attribute_restrictions, "embedded") 2541 dictAttributeToValuesExisting = user_entity_set.getRestrictions("relation_attribute_restrictions") 2542 for attr, setValue in dictAttributeToValuesExisting: 2543 dictAttributeToValues.setdefault(attr, sets.Set()).union(setValue) 2544 2545 listTupleIdUserEntity = self.dbAccess.get_user_entity_relations(unification_protocol_name = self.unification_protocol_name, 2546 userEntityID_list = user_entity_set.get_user_entity_ids(), 2547 attribute_restrictions = user_entity_set.getRestrictions("attribute_restrictions"), 2548 negative_attribute_restrictions = user_entity_set.getRestrictions("negative_attribute_restrictions"), 2549 listRelationType = relation_type_list, 2550 dictRelationAttributeRestriction = dictAttributeToValues, 2551 use_self_relations = user_entity_set.getRestrictions("use_self_relations"), 2552 limit_to_userEntityID_list = True) 2553 2554 self.select_user_entity_relations_from_user_entity_set( user_entity_set_id = user_entity_set_id, user_entity_relation_id_list = listTupleIdUserEntity, clear_previous_selection = False) 2555 2556 OutBianaInterface.send_end_process_message() 2557 2558 return
2559 2560
2561 - def cluster_user_entities(self, user_entity_set_id, attribute):
2562 """ 2563 Clusters the user Entities according to an attribute 2564 2565 #! Not called by any other method 2566 """ 2567 2568 # TO CHECK 2569 2570 eE_uE_dict = {} 2571 2572 import networkx 2573 g = networkx.Graph() 2574 2575 #for currentUE in self.dictUserEntity.values(): 2576 # eE_set = currentUE.get_externalEntitiesIds_set() 2577 # g.add_node(currentUE.id) 2578 # for current_eE_id in eE_set: 2579 # eE_uE_dict[current_eE_id] = currentUE.id 2580 2581 uEs = self.get_user_entity_set(user_entity_set_id) 2582 2583 user_entity_id_list = uEs.get_user_entity_ids() 2584 for currentUEid in user_entity_id_list: #getListUserEntityId(): 2585 currentUE = self.get_user_entity(currentUEid) 2586 eE_set = currentUE.get_externalEntitiesIds_set() 2587 g.add_node(currentUE.id) 2588 for current_eE_id in eE_set: 2589 eE_uE_dict[current_eE_id] = currentUEid 2590 2591 self.userEntityCluster = {} 2592 2593 equivalent_eE = self.dbAccess.get_equivalent_external_entities_from_list( externalEntitiesList = eE_uE_dict.keys(), attribute=attribute ) 2594 2595 for current_equivalent_list in equivalent_eE: 2596 for x in xrange(len(current_equivalent_list)-1): 2597 g.add_edge(eE_uE_dict[current_equivalent_list[x]],eE_uE_dict[current_equivalent_list[x+1]]) 2598 2599 uEs.clusterUserEntities(clusters=networkx.connected_components(g)) 2600 2601 return
2602 2603 2604 # def get_available_relation_repositories(self, relation_type_list=[]): 2605 # """ 2606 # gets available relation types from externalDatabaseExternalEntityRelationType table 2607 # relation_type_list: list of relation types with respect to inclusion of which repositories will be fetched 2608 # """ 2609 #setRelationAttributes = sets.Set( [ a.lower() for a in ExternalEntityRelationAttribute.VALID_IDENTIFIER_ATTRIBUTE_TYPES.keys() ] ) 2610 #strSQLQuery = self.dbAccess.db._get_select_sql_query(tables=[EXTERNAL_DATABASE_TABLE.get_table_name()], columns=["databaseName"], fixed_conditions=[], join_conditions=[]) 2611 #print strSQLQuery, self.dbAccess.db.select_db_content(strSQLQuery, answer_mode="raw") 2612 #setDatabases = sets.Set( [ a[0].lower() for a in self.dbAccess.db.select_db_content(strSQLQuery, answer_mode="raw") ] ) 2613 #return setRelationAttributes & setDatabases 2614 # if relation_type_list == []: 2615 # fixed_conditions = [] 2616 # else: 2617 # fixed_conditions = [ ("ET.eErType","IN","(\"%s\")" % "\",\"".join(relation_type_list), None) ] 2618 # strSQLQuery = self.dbAccess.db._get_select_sql_query(tables=["%s ET" % EXTERNAL_DATABASE_AVAILABLE_eEr_TYPES_TABLE.get_table_name(), "%s ED" % EXTERNAL_DATABASE_TABLE.get_table_name()], columns=["DISTINCT(ED.databaseName)"], fixed_conditions=fixed_conditions, join_conditions=[("ET.externalDatabaseID","=","ED.externalDatabaseID")]) 2619 #print strSQLQuery 2620 # return [i[0] for i in self.dbAccess.db.select_db_content(strSQLQuery, answer_mode="raw")] 2621 2622 # def get_available_participant_detection_methods(self): 2623 # return [ i[0] for i in self.dbAccess.db.select_db_content("SELECT DISTINCT(value) FROM %s " % EXTERNAL_ENTITY_RELATION_PARTICIPANT_ATTRIBUTE_TABLES_DICT["detection_method"].get_table_name(), answer_mode="raw") ] 2624