Back to index

python-biopython  1.60
_GraphSet.py
Go to the documentation of this file.
00001 # Copyright 2003-2008 by Leighton Pritchard.  All rights reserved.
00002 # Revisions copyright 2008-2010 by Peter Cock.
00003 # This code is part of the Biopython distribution and governed by its
00004 # license.  Please see the LICENSE file that should have been included
00005 # as part of this package.
00006 #
00007 # Contact:       Leighton Pritchard, Scottish Crop Research Institute,
00008 #                Invergowrie, Dundee, Scotland, DD2 5DA, UK
00009 #                L.Pritchard@scri.ac.uk
00010 ################################################################################
00011 #
00012 # TODO: Make representation of Ymax and Ymin values at this level, so that
00013 #       calculation of graph/axis drawing is simplified
00014 
00015 """ GraphSet module
00016 
00017     Provides:
00018 
00019     o GraphSet - container for GraphData objects
00020 
00021     For drawing capabilities, this module uses reportlab to draw and write
00022     the diagram:
00023 
00024     http://www.reportlab.com
00025 
00026     For dealing with biological information, the package expects BioPython
00027     objects:
00028 
00029     http://www.biopython.org
00030 """
00031 
00032 # ReportLab imports
00033 from reportlab.lib import colors
00034 
00035 from _Graph import GraphData
00036 
00037 class GraphSet(object):
00038     """ GraphSet
00039 
00040         Provides:
00041 
00042         Methods:
00043 
00044         o __init__(self, set_id=None, name=None)    Called on instantiation
00045 
00046         o new_graph(self, data, name, style='bar', color=colors.lightgreen,
00047                   altcolor=colors.darkseagreen)    Create new graph in the set
00048                           from the passed data, with the passed parameters
00049 
00050         o del_graph(self, graph_id) Delete graph with the passed id
00051 
00052         o get_graphs(self)  Returns a list of all graphs
00053 
00054         o get_ids(self)     Returns a list of graph ids
00055 
00056         o range(self)       Returns the range covered by the graphs in the set
00057 
00058         o to_string(self, verbose=0)    Returns a string describing the set
00059 
00060         o __len__(self)     Returns the length of sequence covered by the set
00061 
00062         o __getitem__(self, key)    Returns the graph with the id of the passed key
00063 
00064         o __str__(self)     Returns a string describing the set
00065 
00066         Attributes:
00067 
00068         o id        Unique identifier for the set
00069 
00070         o name      String describing the set
00071 
00072     """
00073     def __init__(self, name=None):
00074         """ __init__(self, name=None)
00075 
00076             o name      String identifying the graph set sensibly
00077         """
00078         self.id = id            # Unique identifier for the set
00079         self._next_id = 0       # Holds unique ids for graphs
00080         self._graphs = {}       # Holds graphs, keyed by unique id
00081         self.name = name        # Holds description of graph
00082 
00083 
00084     def new_graph(self, data, name=None, style='bar', color=colors.lightgreen,
00085                   altcolor=colors.darkseagreen, linewidth=1, center=None,
00086                   colour=None, altcolour=None, centre=None):
00087         """ new_graph(self, data, name=None, style='bar', color=colors.lightgreen,
00088                   altcolor=colors.darkseagreen)
00089 
00090             o data      List of (position, value) int tuples
00091 
00092             o name      String, description of the graph
00093 
00094             o style     String ('bar', 'heat', 'line') describing how the graph
00095                         will be drawn
00096 
00097             o color    colors.Color describing the color to draw all or 'high'
00098                        (some styles) data (overridden by backwards compatible
00099                        argument with UK spelling, colour).
00100 
00101             o altcolor  colors.Color describing the color to draw 'low' (some
00102                         styles) data (overridden by backwards compatible argument
00103                         with UK spelling, colour).
00104             
00105             o linewidth     Float describing linewidth for graph
00106 
00107             o center        Float setting the value at which the x-axis
00108                             crosses the y-axis (overridden by backwards
00109                             compatible argument with UK spelling, centre)
00110 
00111             Add a GraphData object to the diagram (will be stored
00112             internally
00113         """
00114         #Let the UK spelling (colour) override the USA spelling (color)
00115         if colour is not None:
00116             color = colour
00117         if altcolour is not None:
00118             altcolor = altcolour
00119         if centre is not None:
00120             center = centre
00121 
00122         id = self._next_id                              # get id number
00123         graph = GraphData(id, data, name, style, color, altcolor, center)
00124         graph.linewidth = linewidth
00125         self._graphs[id] =  graph                       # add graph data
00126         self._next_id += 1                              # increment next id
00127         return graph
00128 
00129 
00130     def del_graph(self, graph_id):
00131         """ del_graph(self, graph_id)
00132 
00133             o graph_id        Identifying value of the graph
00134 
00135             Remove a graph from the set, indicated by its id
00136         """
00137         del self._graphs[graph_id]
00138 
00139 
00140     def get_graphs(self):
00141         """ get_graphs(self) -> [Graph, Graph, ...]
00142 
00143             Return a list of all graphs in the graph set, sorted by id (for
00144             reliable stacking...)
00145         """
00146         ids = self._graphs.keys()
00147         ids.sort()
00148         return [self._graphs[id] for id in ids]
00149 
00150 
00151     def get_ids(self):
00152         """ get_ids(self) -> [int, int, ...]
00153 
00154             Return a list of all ids for the graph set
00155         """
00156         return self._graphs.keys()
00157 
00158 
00159     def range(self):
00160         """ range(self) -> (int, int)
00161 
00162             Returns the lowest and highest base (or mark) numbers as a tuple
00163         """
00164         lows, highs = [], []
00165         for graph in self._graphs.values():
00166             low, high = graph.range()
00167             lows.append(low)
00168             highs.append(high)
00169         return (min(lows), max(highs))
00170 
00171 
00172     def data_quartiles(self):
00173         """ data_quartiles(self) -> (float, float, float, float, float)
00174 
00175             Returns the (minimum, lowerQ, medianQ, upperQ, maximum) values as
00176             a tuple
00177         """
00178         data = []
00179         for graph in self._graphs.values():
00180             data += graph.data.values()
00181         data.sort()
00182         datalen = len(data)
00183         return(data[0], data[datalen/4], data[datalen/2],
00184                data[3*datalen/4], data[-1])
00185 
00186 
00187     def to_string(self, verbose=0):
00188         """ to_string(self, verbose=0) -> ""
00189 
00190             o verbose       Flag indicating whether a short or complete account
00191                             of the set is required
00192 
00193             Returns a formatted string with information about the set
00194         """
00195         if not verbose:
00196             return "%s" % self
00197         else:
00198             outstr = ["\n<%s: %s>" % (self.__class__, self.name)]
00199             outstr.append("%d graphs" % len(self._graphs))
00200             for key in self._graphs:
00201                 outstr.append("%s" % self._graphs[key])
00202             return "\n".join(outstr)
00203 
00204 
00205     def __len__(self):
00206         """ __len__(self) -> int
00207 
00208             Return the number of graphs in the set
00209         """
00210         return len(self._graphs)
00211 
00212 
00213     def __getitem__(self, key):
00214         """ __getitem__(self, key) -> Graph
00215 
00216             Return a graph, keyed by id
00217         """
00218         return self._graphs[key]
00219 
00220 
00221     def __str__(self):
00222         """ __str__(self) -> ""
00223 
00224             Returns a formatted string with information about the feature set
00225         """
00226         outstr = ["\n<%s: %s>" % (self.__class__, self.name)]
00227         outstr.append("%d graphs" % len(self._graphs))
00228         outstr = "\n".join(outstr)
00229         return outstr
00230 
00231 
00232 ################################################################################
00233 # RUN AS SCRIPT
00234 ################################################################################
00235 
00236 if __name__ == '__main__':
00237 
00238     # Test code    
00239     gdgs = GraphSet(0, 'test data')
00240 
00241     testdata1 = [(1, 10), (5, 15), (10, 20), (20, 40)]
00242     testdata2 = [(250, .34), (251, .7), (252, .7), (253, .54), (254, .65)]
00243 
00244     gdgs.add_graph(testdata1, 'TestData 1')
00245     gdgs.add_graph(testdata2, 'TestData 2')
00246 
00247     print gdgs