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

Source Code for Module biana.BianaObjects.UnificationProtocol'

  1  """ 
  2      BIANA: Biologic Interactions and Network Analysis 
  3      Copyright (C) 2009  Javier Garcia-Garcia, Emre Guney, Baldo Oliva 
  4   
  5      This program is free software: you can redistribute it and/or modify 
  6      it under the terms of the GNU General Public License as published by 
  7      the Free Software Foundation, either version 3 of the License, or 
  8      (at your option) any later version. 
  9   
 10      This program is distributed in the hope that it will be useful, 
 11      but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13      GNU General Public License for more details. 
 14   
 15      You should have received a copy of the GNU General Public License 
 16      along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 17   
 18  """ 
 19   
 20  import sets 
 21   
 22   
23 -class UnificationAtomElement(object):
24 """ 25 Class to represent a condition of unification 26 """ 27
28 - def __init__(self, externalDatabaseID_A, externalDatabaseID_B, externalAttribute, field_conditions_A=[], field_conditions_B=[], 29 field_cross_references = ["value"]):
30 """ 31 "externalDatabaseID_A" and "externalDatabaseID_B" are the databaseIntegers that identify the databases we want to crossreference 32 externalDatabaseID_A must be lower or equal to externalDatabaseID_B. If not, they will be flipped 33 34 "externalAttribute" is the attribute that we want to cross-reference 35 It can be a list of attributes if they have to be taken into account together (for example, ["sequence","taxID"] 36 37 "fields_A" and "fields_B" is a list with the fields we want to cross-reference (for example, ["value","type"] 38 39 "field_conditions_A" and "field_conditions_B" is optional, and they must be a dictionary with restricted fields { "field": "restricted_value" } 40 41 "field_cross_references" is a list of the fields that have to be cross-referenced. By default, it takes the value [ "value" ] 42 """ 43 44 # I DON'T KNOW IF DOING THIS IN THIS WAY... 45 if externalDatabaseID_A > externalDatabaseID_B: 46 self.externalDatabaseID_A = externalDatabaseID_B 47 self.externalDatabaseID_B = externalDatabaseID_A 48 self.field_conditions_A = field_conditions_B 49 self.field_conditions_B = field_conditions_A 50 else: 51 self.externalDatabaseID_A = externalDatabaseID_A 52 self.externalDatabaseID_B = externalDatabaseID_B 53 self.field_conditions_A = field_conditions_A 54 self.field_conditions_B = field_conditions_B 55 56 self.field_cross_references = field_cross_references 57 58 if (isinstance(externalAttribute,list) ): 59 self.externalAttribute = [ x.lower() for x in externalAttribute ] 60 else: 61 self.externalAttribute = [externalAttribute.lower()] 62 63 #for actual_external_attribute in self.externalAttribute: 64 # if NOT_ALLOWED_CROSS_REFERENCES.has_key(actual_external_attribute): 65 # raise ValueError("%s cannot be used for integration purposes" %externalAttribute) 66 67 # We sort them to be able to compare later distinct unification atom elements 68 self.field_cross_references.sort() 69 self.field_conditions_A.sort() 70 self.field_conditions_B.sort()
71 72
73 - def __str__(self):
74 """ 75 String representation of the class 76 """ 77 78 return_list = ["Unification atom"] 79 return_list.append("Cross databases: %s - %s" %(self.get_externalDatabaseID_A(),self.get_externalDatabaseID_B())) 80 return_list.append("Using external attribute: %s" %self.get_external_attribute_list()) 81 return_list.append("Field cross_references: %s" %self.field_cross_references) 82 83 return "%s" %("\n\t".join(return_list))
84 85
86 - def get_externalDatabaseIDs(self):
87 """ 88 returns a tuple with both database identifiers 89 """ 90 return (self.externalDatabaseID_A, self.externalDatabaseID_B)
91
92 - def get_externalDatabaseID_A(self):
93 return self.externalDatabaseID_A
94
95 - def get_externalDatabaseID_B(self):
96 return self.externalDatabaseID_B
97
99 return self.externalAttribute
100
101 - def get_field_conditions_A(self):
102 """ 103 gets a list of tuples with the following format: (field,value) 104 """ 105 return [ (x,self.field_conditions_A[x]) for x in self.field_conditions_A ]
106 107
108 - def get_field_conditions_B(self):
109 """ 110 gets a list of tuples with the following format: (field,value) 111 """ 112 return [ (x,self.field_conditions_B[x]) for x in self.field_conditions_B ]
113 114
116 117 return self.field_cross_references
118 119
120 - def is_comparable(self, unificationAtomElement2 ):
121 """ 122 Checks if two atom elements are comparable, by checking the equivalence of all its attributes except the source databases 123 """ 124 125 return ( self.field_cross_references == unificationAtomElement2.get_field_cross_references() and 126 ( (self.field_conditions_A == unificationAtomElement2.get_field_conditions_A() and 127 self.field_conditions_B == unificationAtomElement2.get_field_conditions_B() ) or 128 (self.field_conditions_A == unificationAtomElement2.get_field_conditions_B() and 129 self.field_conditions_B == unificationAtomElement2.get_field_conditions_A() ) ) and 130 self.externalAttribute == unificationAtomElement2.get_external_attribute_list() )
131 132 133
134 -class UnificationProtocol(object):
135 """ 136 Class to represent 137 """ 138 139
140 - def __init__(self, description, BianaDatabaseVersion, id=None):
141 """ 142 """ 143 144 self.unification_atom_elements = [] 145 146 self.description = description 147 self.databaseVersion = BianaDatabaseVersion 148 149 self.id = id # Only has an ID if it has been inserted to the database 150 151 self.use_databases = sets.Set()
152 153
154 - def add_database(self, externalDatabaseID):
155 156 self.use_databases.add(externalDatabaseID)
157
158 - def get_database_ids(self):
159 """ 160 Returns a set with the database ids it contains 161 """ 162 163 return self.use_databases
164 165
166 - def add_unification_atom_elements(self, unificationAtomElement):
167 """ 168 Adds a crossreference condition 169 """ 170 self.unification_atom_elements.append(unificationAtomElement) 171 self.use_databases.add(unificationAtomElement.externalDatabaseID_A) 172 self.use_databases.add(unificationAtomElement.externalDatabaseID_B)
173
175 return self.unification_atom_elements
176 177
178 - def get_description(self):
179 return self.description
180 181
182 - def get_id(self):
183 return self.id
184 185
186 - def get_summary_str_list(self):
187 """ 188 """
189