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   - FeatureSet - container for Feature objects 
 20   
 21  For drawing capabilities, this module uses reportlab to draw and write 
 22  the diagram: http://www.reportlab.com 
 23  """ 
 24   
 25  # ----------------------------------------------------------------------------- 
 26  # IMPORTS 
 27   
 28  # ReportLab 
 29  from __future__ import print_function 
 30   
 31  from reportlab.pdfbase import _fontdata 
 32  from reportlab.lib import colors 
 33   
 34  # GenomeDiagram 
 35  from ._Feature import Feature 
 36   
 37  # Builtins 
 38  import re 
 39   
 40  __docformat__ = "restructuredtext en" 
 41   
 42  # ------------------------------------------------------------------------------ 
 43  # CLASSES 
 44   
 45  # ------------------------------------------------------------ 
 46  # FeatureSet 
 47   
 48   
49 -class FeatureSet(object):
50 """FeatureSet object.""" 51
52 - def __init__(self, set_id=None, name=None, parent=None):
53 """Create the object. 54 55 Arguments: 56 - set_id: Unique id for the set 57 - name: String identifying the feature set 58 """ 59 self.parent = parent 60 self.id = id # Unique id for the set 61 self.next_id = 0 # counter for unique feature ids 62 self.features = {} # Holds features, keyed by ID 63 self.name = name # String describing the set
64
65 - def add_feature(self, feature, **kwargs):
66 """Add a new feature. 67 68 Arguments: 69 - feature: Bio.SeqFeature object 70 - kwargs: Keyword arguments for Feature. Named attributes 71 of the Feature 72 73 Add a Bio.SeqFeature object to the diagram (will be stored 74 internally in a Feature wrapper). 75 """ 76 id = self.next_id # get id number 77 f = Feature(self, id, feature) 78 self.features[id] = f # add feature 79 for key in kwargs: 80 if key == "colour" or key == "color": 81 # Deal with "colour" as a special case by also mapping to color. 82 # If Feature.py used a python property we wouldn't need to call 83 # set_color explicitly. However, this is important to make sure 84 # every color gets mapped to a colors object - for example color 85 # numbers, or strings (may not matter for PDF, but does for PNG). 86 self.features[id].set_color(kwargs[key]) 87 continue 88 setattr(self.features[id], key, kwargs[key]) 89 self.next_id += 1 # increment next id 90 return f
91
92 - def del_feature(self, feature_id):
93 """Delete a feature. 94 95 Arguments: 96 - feature_id: Unique id of the feature to delete 97 98 Remove a feature from the set, indicated by its id. 99 """ 100 del self.features[feature_id]
101
102 - def set_all_features(self, attr, value):
103 """Set an attribute of all the features. 104 105 Arguments: 106 - attr: An attribute of the Feature class 107 - value: The value to set that attribute to 108 109 Set the passed attribute of all features in the set to the 110 passed value. 111 """ 112 changed = 0 113 for feature in self.features.values(): 114 # If the feature has the attribute, and the value should change 115 if hasattr(feature, attr): 116 if getattr(feature, attr) != value: 117 setattr(feature, attr, value) # set it to the passed value
118 119 # For backwards compatibility, we support both colour and color. 120 # As a quick hack, make "colour" set both "colour" and "color". 121 # if attr=="colour": 122 # self.set_all_feature("color",value) 123
124 - def get_features(self, attribute=None, value=None, comparator=None):
125 """Retreive features. 126 127 Arguments: 128 - attribute: String, attribute of a Feature object 129 - value: The value desired of the attribute 130 - comparator: String, how to compare the Feature attribute to the 131 passed value 132 133 If no attribute or value is given, return a list of all features in the 134 feature set. If both an attribute and value are given, then depending 135 on the comparator, then a list of all features in the FeatureSet 136 matching (or not) the passed value will be returned. Allowed comparators 137 are: 'startswith', 'not', 'like'. 138 139 The user is expected to make a responsible decision about which feature 140 attributes to use with which passed values and comparator settings. 141 """ 142 # If no attribute or value specified, return all features 143 if attribute is None or value is None: 144 return list(self.features.values()) 145 # If no comparator is specified, return all features where the attribute 146 # value matches that passed 147 if comparator is None: 148 return [feature for feature in self.features.values() if 149 getattr(feature, attribute) == value] 150 # If the comparator is 'not', return all features where the attribute 151 # value does not match that passed 152 elif comparator == 'not': 153 return [feature for feature in self.features.values() if 154 getattr(feature, attribute) != value] 155 # If the comparator is 'startswith', return all features where the attribute 156 # value does not match that passed 157 elif comparator == 'startswith': 158 return [feature for feature in self.features.values() if 159 getattr(feature, attribute).startswith(value)] 160 # If the comparator is 'like', use a regular expression search to identify 161 # features 162 elif comparator == 'like': 163 return [feature for feature in self.features.values() if 164 re.search(value, getattr(feature, attribute))] 165 # As a final option, just return an empty list 166 return []
167
168 - def get_ids(self):
169 """Return a list of all ids for the feature set.""" 170 return list(self.features.keys())
171
172 - def range(self):
173 """Returns the lowest and highest base (or mark) numbers as a tuple.""" 174 lows, highs = [], [] 175 for feature in self.features.values(): 176 for start, end in feature.locations: 177 lows.append(start) 178 highs.append(end) 179 if len(lows) != 0 and len(highs) != 0: # Default in case there is 180 return (min(lows), max(highs)) # nothing in the set 181 return 0, 0
182
183 - def to_string(self, verbose=0):
184 """Returns a formatted string with information about the set 185 186 Arguments: 187 - verbose: Boolean indicating whether a short (default) or 188 complete account of the set is required 189 """ 190 if not verbose: # Short account only required 191 return "%s" % self 192 else: # Long account desired 193 outstr = ["\n<%s: %s>" % (self.__class__, self.name)] 194 outstr.append("%d features" % len(self.features)) 195 for key in self.features: 196 outstr.append("feature: %s" % self.features[key]) 197 return "\n".join(outstr)
198
199 - def __len__(self):
200 """Return the number of features in the set.""" 201 return len(self.features)
202
203 - def __getitem__(self, key):
204 """Return a feature, keyed by id.""" 205 return self.features[key]
206
207 - def __str__(self):
208 """Returns a formatted string with information about the feature set.""" 209 outstr = ["\n<%s: %s %d features>" % (self.__class__, self.name, 210 len(self.features))] 211 return "\n".join(outstr)
212 213 ################################################################################ 214 # RUN AS SCRIPT 215 ################################################################################ 216 217 if __name__ == '__main__': 218 from Bio import SeqIO 219 220 genbank_entry = SeqIO.read('/data/Genomes/Bacteria/Nanoarchaeum_equitans/NC_005213.gbk', 'gb') 221 222 # Test code 223 gdfs = FeatureSet(0, 'Nanoarchaeum equitans CDS') 224 for feature in genbank_entry.features: 225 if feature.type == 'CDS': 226 gdfs.add_feature(feature) 227 228 # print len(gdfs) 229 # print gdfs.get_ids() 230 # gdfs.del_feature(560) 231 # print gdfs.get_ids() 232 # print gdfs.get_features() 233 # for feature in gdfs.get_features(): 234 # print feature.id, feature.start, feature.end 235 # print gdfs[500] 236