Debian Codegraph @

docs/source/report_model.rst

raw
1 Report Models
2 =============
3
4 A report is composed of model objects whose classes are defined in pbreports.model. Typically, a report object is created and then attributes, tables, or plotGroups are
5 added to the report. Lastly, the report is serialized as json to a file.
6
7 The objects that comprise a report extend BaseReportElement. `All report elements have an id`.
8
9 When the report is converted to a dictionary before serialization, each report element's id is prepended with its parent id,
10 which has also been prepended. For example, given the nested elements of report --> plotGroup --> plot, with respective ids "r", "pg", and "p",
11 the plot id would be "r.pg.p" in the dictionary.
12
13 This allows elements of a report to be looked up id, such as "mapping_stats.n_mapped_bases" for a report Attribute (i.e., metric), or a specific plot group, such as "filter_report.filtered_subreads".
14
15 .. note:: Once a report element id has been assigned, it should not change.
16
17 Report
18 ------
19
20 Report is the root class of the model hierarchy. It's instantiated with an id (should be a short string), which defines its namespace.
21 This example shows how a report is with one attribute, plotGroup, and table is created and written.
22
23 .. code-block:: python
24
25 import os
26 import logging
27
28 from pbcommand.models.report import Report, Attribute, PlotGroup, Table
29
30 log = logging.getLogger(__name__)
31
32 def make_report():
33 """Write a simple report"""
34 table = create_table() # See example below
35 attribute = create_attribute() # See example below
36 plotGroup = create_plotGroup() # See example below
37
38 # Id must match ^[a-z0-9_]+$
39 r = Report('loading', title="Loading Report",
40 attributes=[attribute],
41 plotgroups=[plotGroup],
42 tables=[table])
43
44 # Alternatively
45 r.add_table(table)
46 r.add_attribute(attribute)
47 r.add_plotGroup(plotGroup)
48
49 r.write_json('/my/file.json')
50
51
52 Attribute
53 ---------
54
55 An attribute represents a key-value pair with an optional name. The id is the key. A report contains
56 a list of attributes.
57
58 .. code-block:: python
59
60 import os
61 import logging
62
63 from pbcommand.models.report import Attribute
64
65 log = logging.getLogger(__name__)
66
67 def create_attribute():
68 """Return an attribute"""
69 a = Attribute('alpha', 1234, name='Alpha')
70 b = Attribute('beta', "value", name="Beta Display Name")
71 return a
72
73
74 Table
75 -----
76
77 A table contains a list of column objects and has an optional title and id. A report contains a list of tables.
78 In general, the paradigm for creating a table is to instantiate a table and a series of columns. Add the
79 columns to the table in the desired order. Finally, iterate over your data set and append data to the
80 columns by index.
81
82 .. code-block:: python
83
84 import os
85 import logging
86 import random
87
88 from pbcommand.models.report import Attribute, Table, Column
89
90 log = logging.getLogger(__name__)
91
92 def create_table():
93 """Return a table with 2 columns"""
94 columns = [Column( 'c1id', header='C1 header'),
95 Column('c2id', header='C2 header')]
96
97 t = Table('myid', title='My Table', columns=columns)
98
99 #Now append data to the columns
100 #Assume data is a list of tuples of len == 2
101 datum = [(c.id, random.random()) for c in columns]
102 for column_id, value in datum:
103 t.add_data_by_column_id(column_id, value)
104
105 # Alternatively
106 cx = Column("cx", header="X", values=[1,2,3,4])
107 cy = Column("cy", header="Y", values=[1,4,9,16])
108 t = Table("xy", title="X vs Y", columns=[cx, cy])
109 return t
110
111
112 PlotGroup
113 ---------
114
115 A `Plot Group` represents a logical grouping or collection of plots that convey related information, such coverage across
116 5 contigs. A plotGroup has an id, an optional thumbnail (to represent the group in SMRT Link in a
117 preview), an optional legend and a list of plots.
118
119 .. code-block:: python
120
121 import os
122 import logging
123
124 from pbcommand.model.report import PlotGroup, Plot
125
126 log = logging.getLogger(__name__)
127
128 def create_plotGroup():
129 """Return a PlotGroup with 1 plot"""
130 # Image paths must be relative to the dir where the final Report is written
131
132 plot = Plot('plot_id', image='image.png', caption='this is a plot')
133 p = PlotGroup('myid', title='some title', thumbnail='image_thumb.png', plots=[plot])
134
135 return p
136
137
138 .. note:: The image paths must be written relative to where the report JSON file will be written.
139
140 .. note:: Currently, only PNG is supported
141
142
143 Report Specs
144 ============
145
146 A parallel family of models in the same module handles specifications for
147 individual reports, i.e. enumerating the data items expected for each model
148 type, along with view metadata. The overall structure and names of objects in
149 the hierarchy is identical to the Report model. For any of the nodes in the
150 hierarchy, the following view metadata may be specified:
151
152 - a UI label, usually `title` (or `name` for Attributes, `header` for table
153 columns)
154 - a description suitable for formal documentation or mouseover text
155 - a boolean `isHidden` attribute that controls visibility
156
157 There is some redundancy between the report specifications and the actual
158 reports - for example the Report `title` and Attribute `name` occur in both
159 models. This was due to the lack of a clear model for view metadata in previous
160 versions of SMRTAnalysis; the Report model may be slimmed down in the future as
161 the view rules are deployed and utilized.
162
163 The `pbcommand` module itself does not actually define any reports; currently
164 most of these are part of the `pbreports` module.
165
166 Format strings
167 --------------
168
169 For formatting numerical attribute and column values, we are using a
170 lightweight syntax based on Python's `str.format(...)` method. If the
171 `format` attribute is set to `None` (`null` in JSON), the value should
172 simply be directly converted to string without any formatting. (In the case
173 of string and boolean values, the format should always be left unset.) More
174 complex operations values must match this regular expression::
175
176 {([GMkp]{0,1})(:)([\.,]{0,1})([0-9]*)([dfg]{1})}(.*)$
177
178 The `[GMkp]` group specifies scaling - if one of these characters is present,
179 the value should be divided by one billion (`G`), one million (`M`), or one
180 thousand (`k`) before formatting, or multiplied by 100 (`p`). The period or
181 comma after the colon modifies the display of floating-point and integer
182 values respectively. The following characters before the closing brace
183 correspond to conventional format string syntax. The format can optionally
184 include a suffix to be appended to the formatted value.
185
186 Examples of use::
187
188 format_value("{:,d}", 123456) # 123,456
189 format_value("{:.2f)", 1.23456) # 1.23
190 format_value("{G:.2f} Gb", 1234567890) # 1.23 Gb
191 format_value("{p:5g}%", 0.987654321) # 98.765%
192 format_value(None, 0.987654321) # 0.987654321