Package Bio :: Package Graphics :: Package GenomeDiagram :: Module _FeatureSet
[hide private]
[frames] | no frames]

Source Code for Module Bio.Graphics.GenomeDiagram._FeatureSet

  1  # Copyright 2003-2008 by Leighton Pritchard.  All rights reserved. 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5  # 
  6  # Contact:       Leighton Pritchard, Scottish Crop Research Institute, 
  7  #                Invergowrie, Dundee, Scotland, DD2 5DA, UK 
  8  #                L.Pritchard@scri.ac.uk 
  9  ################################################################################ 
 10  # 
 11  # Thanks to Peter Cock for the impetus to write the get_features() code to 
 12  # subselect Features. 
 13  # 
 14  ################################################################################ 
 15   
 16  """ FeatureSet module 
 17   
 18      Provides: 
 19   
 20      o FeatureSet - container for Feature objects 
 21   
 22      For drawing capabilities, this module uses reportlab to draw and write 
 23      the diagram: 
 24   
 25      http://www.reportlab.com 
 26   
 27      For dealing with biological information, the package expects BioPython 
 28      objects: 
 29   
 30      http://www.biopython.org 
 31  """ 
 32   
 33  #------------------------------------------------------------------------------ 
 34  # IMPORTS 
 35   
 36  # ReportLab 
 37  from reportlab.pdfbase import _fontdata 
 38  from reportlab.lib import colors 
 39   
 40  # GenomeDiagram 
 41  from _Feature import Feature 
 42   
 43  # Builtins 
 44  import re 
 45   
 46  #------------------------------------------------------------------------------ 
 47  # CLASSES 
 48   
 49  #------------------------------------------------------------ 
 50  # FeatureSet 
 51   
 52   
53 -class FeatureSet(object):
54 """ FeatureSet 55 56 Provides: 57 58 Methods: 59 60 o __init__(self, set_id=None, name=None) Called on instantiation 61 62 o add_feature(self, feature, color=colors.lightgreen) Add a Feature 63 object to the set 64 65 o del_feature(self, feature_id) Remove a feature from the set, by id 66 67 o set_all_features(self, attr, value) Set the passed attribute to the 68 passed value in all features in the set 69 70 o get_features(self) Returns a list of Features from the set 71 72 o get_ids(self) Returns a list of unique ids for features in the set 73 74 o range(self) Returns the range of bases covered by features in 75 the set 76 77 o to_string(self, verbose=0) Returns a string describing the set 78 79 o __len__(self) Returns the length of sequence covered by the set 80 81 o __getitem__(self, key) Returns a feature from the set, keyed by id 82 83 o __str__(self) Returns a string describing the set 84 85 Attributes: 86 87 o id Unique id for the set 88 89 o name String describing the set 90 """
91 - def __init__(self, set_id=None, name=None, parent=None):
92 """ __init__(self, set_id=None, name=None) 93 94 o set_id Unique id for the set 95 96 o name String identifying the feature set 97 """ 98 self.parent = parent 99 self.id = id # Unique id for the set 100 self.next_id = 0 # counter for unique feature ids 101 self.features = {} # Holds features, keyed by ID 102 self.name = name # String describing the set
103
104 - def add_feature(self, feature, **kwargs):
105 """ add_feature(self, feature, **args) 106 107 o feature Bio.SeqFeature object 108 109 o **kwargs Keyword arguments for Feature. Named attributes 110 of the Feature 111 112 Add a Bio.SeqFeature object to the diagram (will be stored 113 internally in a Feature wrapper 114 """ 115 id = self.next_id # get id number 116 f = Feature(self, id, feature) 117 self.features[id] = f # add feature 118 for key in kwargs: 119 if key == "colour" or key == "color": 120 #Deal with "colour" as a special case by also mapping to color. 121 #If Feature.py used a python property we wouldn't need to call 122 #set_color explicitly. However, this is important to make sure 123 #every color gets mapped to a colors object - for example color 124 #numbers, or strings (may not matter for PDF, but does for PNG). 125 self.features[id].set_color(kwargs[key]) 126 continue 127 setattr(self.features[id], key, kwargs[key]) 128 self.next_id += 1 # increment next id 129 return f
130
131 - def del_feature(self, feature_id):
132 """ del_feature(self, feature_id) 133 134 o feature_id Unique id of the feature to delete 135 136 Remove a feature from the set, indicated by its id 137 """ 138 del self.features[feature_id]
139
140 - def set_all_features(self, attr, value):
141 """ set_all_features(self, attr, value) 142 143 o attr An attribute of the Feature class 144 145 o value The value to set that attribute 146 147 Set the passed attribute of all features in the set to the 148 passed value 149 """ 150 changed = 0 151 for feature in self.features.values(): 152 # If the feature has the attribute, and the value should change 153 if hasattr(feature, attr): 154 if getattr(feature, attr) != value: 155 setattr(feature, attr, value) # set it to the passed value
156 157 #For backwards compatibility, we support both colour and color. 158 #As a quick hack, make "colour" set both "colour" and "color". 159 #if attr=="colour": 160 # self.set_all_feature("color",value) 161
162 - def get_features(self, attribute=None, value=None, comparator=None):
163 """ get_features(self, attribute=None, value=None, comparator=None) -> 164 [Feature, Feature, ...] 165 166 o attribute String, attribute of a Feature object 167 168 o value The value desired of the attribute 169 170 o comparator String, how to compare the Feature attribute to the 171 passed value 172 173 If no attribute or value is given, return a list of all features in the 174 feature set. If both an attribute and value are given, then depending 175 on the comparator, then a list of all features in the FeatureSet 176 matching (or not) the passed value will be returned. Allowed comparators 177 are: 'startswith', 'not', 'like'. 178 179 The user is expected to make a responsible decision about which feature 180 attributes to use with which passed values and comparator settings. 181 """ 182 # If no attribute or value specified, return all features 183 if attribute is None or value is None: 184 return self.features.values() 185 # If no comparator is specified, return all features where the attribute 186 # value matches that passed 187 if comparator is None: 188 return [feature for feature in self.features.values() if 189 getattr(feature, attribute) == value] 190 # If the comparator is 'not', return all features where the attribute 191 # value does not match that passed 192 elif comparator == 'not': 193 return [feature for feature in self.features.values() if 194 getattr(feature, attribute) != value] 195 # If the comparator is 'startswith', return all features where the attribute 196 # value does not match that passed 197 elif comparator == 'startswith': 198 return [feature for feature in self.features.values() if 199 getattr(feature, attribute).startswith(value)] 200 # If the comparator is 'like', use a regular expression search to identify 201 # features 202 elif comparator == 'like': 203 return [feature for feature in self.features.values() if 204 re.search(value, getattr(feature, attribute))] 205 # As a final option, just return an empty list 206 return []
207
208 - def get_ids(self):
209 """ get_ids(self) -> [int, int, ...] 210 211 Return a list of all ids for the feature set 212 """ 213 return self.features.keys()
214
215 - def range(self):
216 """ range(self) 217 218 Returns the lowest and highest base (or mark) numbers as a tuple 219 """ 220 lows, highs = [], [] 221 for feature in self.features.values(): 222 for start, end in feature.locations: 223 lows.append(start) 224 highs.append(end) 225 if len(lows) != 0 and len(highs) != 0: # Default in case there is 226 return (min(lows), max(highs)) # nothing in the set 227 return 0, 0
228
229 - def to_string(self, verbose=0):
230 """ to_string(self, verbose=0) -> "" 231 232 o verbose Boolean indicating whether a short or complete 233 account of the set is required 234 235 Returns a formatted string with information about the set 236 """ 237 if not verbose: # Short account only required 238 return "%s" % self 239 else: # Long account desired 240 outstr = ["\n<%s: %s>" % (self.__class__, self.name)] 241 outstr.append("%d features" % len(self.features)) 242 for key in self.features: 243 outstr.append("feature: %s" % self.features[key]) 244 return "\n".join(outstr)
245
246 - def __len__(self):
247 """ __len__(self) -> int 248 249 Return the number of features in the set 250 """ 251 return len(self.features)
252
253 - def __getitem__(self, key):
254 """ __getitem__(self, key) -> Feature 255 256 Return a feature, keyed by id 257 """ 258 return self.features[key]
259
260 - def __str__(self):
261 """ __str__(self) -> "" 262 263 Returns a formatted string with information about the feature set 264 """ 265 outstr = ["\n<%s: %s %d features>" % (self.__class__, self.name, 266 len(self.features))] 267 return "\n".join(outstr)
268 269 ################################################################################ 270 # RUN AS SCRIPT 271 ################################################################################ 272 273 if __name__ == '__main__': 274 from Bio import SeqIO 275 276 genbank_entry = SeqIO.read('/data/Genomes/Bacteria/Nanoarchaeum_equitans/NC_005213.gbk', 'gb') 277 278 # Test code 279 gdfs = FeatureSet(0, 'Nanoarchaeum equitans CDS') 280 for feature in genbank_entry.features: 281 if feature.type == 'CDS': 282 gdfs.add_feature(feature) 283 284 #print len(gdfs) 285 #print gdfs.get_ids() 286 #gdfs.del_feature(560) 287 #print gdfs.get_ids() 288 #print gdfs.get_features() 289 #for feature in gdfs.get_features(): 290 # print feature.id, feature.start, feature.end 291 #print gdfs[500] 292