vis.py 10.8 KB
Newer Older
jhoogenboom's avatar
jhoogenboom committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
"""
Create a data visualisation web page or Vega graph specification.

With no optional arguments specified, a self-contained web page (HTML
file) is produced.  You can open this file in a web browser to view
interactive visualisations of your data.  The web page contains a file
selection element which can be used to select the data to be visualised.

Visualisations make use of the Vega JavaScript library
(https://vega.github.io).  The required JavaScript libraries (Vega and
D3) are embedded in the generated HTML file.  With the -O/--online
option specified, the HTML file will instead link to the latest version
of these libraries on the Internet.

Vega also supports generating visualisations on the command line, which
is useful if you wish to generate graphs automatically in your analysis
pipeline.  Specify the -V/--vega option to obtain a bare Vega graph
specification (a JSON file) instead of the full-featured HTML file.  You
can pass this file through Vega to generate a PNG or SVG image file.

If an input file is specified, the visualisation will be set up
specifically to visualise the contents of this file.  To this end, the
entire file contents are embedded in the generated visualisation.
"""
import argparse
import sys
import json
import re
import os
import cgi

from pkg_resources import resource_stream, resource_string

from ..lib import pos_int_arg

__version__ = "0.1dev"


# Default values for parameters are specified below.

# Default minimum number of reads to require.
# This value can be overridden by the -n command line option.
_DEF_THRESHOLD_ABS = 15

# Default minimum amount of reads to require, as a percentage of the
# highest allele of each marker.
# This value can be overridden by the -m command line option.
_DEF_THRESHOLD_PCT = 0.5

# Default width of bars in bar graphs, in pixels.
# This value can be overridden by the -b command line option.
_DEF_BAR_WIDTH = 15

# Default amount of padding between subgraphs, in pixels.
# This value can be overridden by the -p command line option.
_DEF_SUBGRAPH_PADDING = 70

59
60
61
62
63
64
65
66
# Default graph width in pixels.
# This value can be overridden by the -w command line option.
_DEF_WIDTH = 600

# Default marker name matching regular expression.
# This value can be overridden by the -M command line option.
_DEF_MARKER_REGEX = ".*"

jhoogenboom's avatar
jhoogenboom committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Default data file that Vega will read when -V/--vega is specified
# without providing data to embed in the file.
# It is currently impossible to override this value.
_DEF_DATA_FILENAME = "data.csv"



_PAT_LOAD_SCRIPT = re.compile("<!--\s*BEGIN_LOAD_SCRIPT\s*-->\s*(.*?)\s*"
                              "<!--\s*END_LOAD_SCRIPT\s*-->", flags=re.DOTALL)
_PAT_LIBRARIES = re.compile("<!--\s*BEGIN_LIBRARIES\s*-->\s*(.*?)\s*"
                            "<!--\s*END_LIBRARIES\s*-->", flags=re.DOTALL)

_SCRIPT_BEGIN = '<script type="text/javascript">'
_SCRIPT_END = '</script>'

_EXTERNAL_LIBRARIES = ("vis/d3.min.js", "vis/vega.min.js")



def set_data_formula_transform_value(spec, dataname, fieldname, value):
    for data in spec["data"]:
        if data["name"] != dataname:
            continue
        if "transform" not in data:
            return False
        for transform in data["transform"]:
            if (transform["type"] == "formula" and
                    transform["field"] == fieldname):
                transform["expr"] = str(value)
                return True
    return False
#set_data_formula_transform_value


def create_visualisation(vistype, infile, outfile, vega, online, tidy,
102
                         min_abs, min_pct, bar_width, padding, marker, width):
jhoogenboom's avatar
jhoogenboom committed
103
104
105
106
107
108
109
110
111
112
113
114
    # Get graph spec.
    spec = json.load(resource_stream(
        "fdstools", "vis/%svis/%svis.json" % (vistype, vistype)))
    if infile is not None:
        # Embed the given data file.
        spec["data"][0]["values"] = infile.read()
    elif vega:
        # Vega should load data from somewhere in headless mode.
        del spec["data"][0]["values"]
        spec["data"][0]["url"] = _DEF_DATA_FILENAME

    # Apply settings.
115
116
117
118
119
    spec["width"] = width
    set_data_formula_transform_value(spec, "yscale", "barwidth", bar_width)
    set_data_formula_transform_value(spec, "yscale", "subgraphoffset", padding)
    set_data_formula_transform_value(
        spec, "table", "filter_marker", "'" + marker + "'")
jhoogenboom's avatar
jhoogenboom committed
120
    if vistype == "sample" or vistype == "bgraw":
jhoogenboom's avatar
jhoogenboom committed
121
122
123
124
        set_data_formula_transform_value(
            spec, "table", "amplitude_threshold", min_abs)
        set_data_formula_transform_value(
            spec, "table", "amplitude_pct_threshold", min_pct)
125
126
127
    elif vistype == "profile":
        set_data_formula_transform_value(
            spec, "table", "filter_threshold", min_pct)
jhoogenboom's avatar
jhoogenboom committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

    # Stringify spec.
    if tidy:
        spec = json.dumps(spec, indent=2, separators=(",", ": "))
    else:
        spec = json.dumps(spec, separators=(",", ":"))

    if vega:
        # Vega graph spec is all that we were asked for.
        outfile.write(spec)
        return

    # Creating a fully self-contained HTML visualisation instead.
    html = resource_string("fdstools", "vis/%svis/index.html" % vistype)
    match = _PAT_LOAD_SCRIPT.search(html)
    if match:
        html = "".join([html[:match.start(1)],
                        _SCRIPT_BEGIN,
                        "var graph_spec=",
                        spec,
                        ";onLoadSpec(",
                        "true" if infile is not None else "false",
                        ");",
                        _SCRIPT_END,
                        html[match.end(1):]])

    if not online:
        # Replace external libraries with inline libraries.
        match = _PAT_LIBRARIES.search(html)
        if match:
            parts = [html[:match.start(1)]]
            for library in _EXTERNAL_LIBRARIES:
                parts += [_SCRIPT_BEGIN,
                          resource_string("fdstools", library),
                          _SCRIPT_END]
            parts.append(html[match.end(1):])
            html = "".join(parts)

    outfile.write(html)    
#create_visualisation


def add_arguments(parser):
jhoogenboom's avatar
jhoogenboom committed
171
172
    parser.add_argument('type', metavar="TYPE",
        choices=("sample", "profile", "bgraw"),
173
174
175
        help="the type of data to visualise; use 'sample' to visualise "
             "sample data files and BGCorrect output; use 'profile' to "
             "visualise background noise profiles obtained with BGEstimate, "
jhoogenboom's avatar
jhoogenboom committed
176
177
             "BGHomStats, and BGPredict; use 'bgraw' to visualise raw "
             "background noise data obtained with BGHomRaw")
jhoogenboom's avatar
jhoogenboom committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
    parser.add_argument('infile', metavar="IN", nargs="?",
        help="file containing the data to embed in the visualisation file; if "
             "not specified, HTML visualisation files will contain a file "
             "selection control, and Vega visualisation files will load data "
             "from a file called '%s'" % _DEF_DATA_FILENAME)
    parser.add_argument('outfile', metavar="OUT", nargs="?",
        type=argparse.FileType('w'),
        default=sys.stdout,
        help="file to write output to (default: write to stdout)")
    parser.add_argument('-V', '--vega', action="store_true",
        help="an HTML file containing an interactive visualisation is created "
             "by default; if this option is specified, a Vega graph "
             "specification (JSON file) is produced instead")
    parser.add_argument('-O', '--online', action="store_true",
        help="when generating an HTML visualisation file, required JavaScript "
             "libraries (D3 and Vega) are embedded in the file; if this "
             "option is specified, the HTML file will instead link to these "
             "libraries on the Internet, thereby always using the latest "
             "versions of D3 and Vega")
    parser.add_argument('-t', '--tidy', action="store_true",
        help="tidily indent the generated JSON")

200
201
202
203
    visgroup = parser.add_argument_group("visualisation options",
        description="words in [brackets] indicate applicable visualisation "
                    "types")
    visgroup.add_argument('-n', '--min-abs', metavar="N", type=pos_int_arg,
jhoogenboom's avatar
jhoogenboom committed
204
        default=_DEF_THRESHOLD_ABS,
jhoogenboom's avatar
jhoogenboom committed
205
206
        help="[sample, bgraw] only show sequences with this minimum number of "
             "reads (default: %(default)s)")
207
    visgroup.add_argument('-m', '--min-pct', metavar="PCT", type=float,
jhoogenboom's avatar
jhoogenboom committed
208
        default=_DEF_THRESHOLD_PCT,
jhoogenboom's avatar
jhoogenboom committed
209
210
211
212
        help="[sample, profile, bgraw] for sample: only show sequences with "
             "at least this percentage of the number of reads of the highest "
             "allele of a marker; for profile and bgraw: at least this "
             "percentage of the true allele (default: %(default)s)")
213
214
    visgroup.add_argument('-M', '--marker', metavar="REGEX",
        default=_DEF_MARKER_REGEX,
jhoogenboom's avatar
jhoogenboom committed
215
216
217
        help="[sample, profile, bgraw] only show graphs for the markers that "
             "match the given regular expression; the default value "
             "'%(default)s' matches any marker name")
218
    visgroup.add_argument('-b', '--bar-width', metavar="N", type=pos_int_arg,
jhoogenboom's avatar
jhoogenboom committed
219
        default=_DEF_BAR_WIDTH,
jhoogenboom's avatar
jhoogenboom committed
220
        help="[sample, profile, bgraw] width of the bars in pixels (default: "
221
222
             "%(default)s)")
    visgroup.add_argument('-p', '--padding', metavar="N", type=pos_int_arg,
jhoogenboom's avatar
jhoogenboom committed
223
        default=_DEF_SUBGRAPH_PADDING,
jhoogenboom's avatar
jhoogenboom committed
224
225
        help="[sample, profile, bgraw] amount of padding (in pixels) between "
             "graphs of different markers/alleles (default: %(default)s)")
226
227
    visgroup.add_argument('-w', '--width', metavar="N", type=pos_int_arg,
        default=_DEF_WIDTH,
jhoogenboom's avatar
jhoogenboom committed
228
229
        help="[sample, profile, bgraw] width of the graph area in pixels "
             "(default: %(default)s)")
jhoogenboom's avatar
jhoogenboom committed
230
231
232
233
234
235
236
237
238
239
240
241
242
#add_arguments


def run(args):
    if args.infile == "-" and not sys.stdin.isatty():
        # User appears to want to pipe data in.
        args.infile = sys.stdin
    if (args.infile is not None and args.outfile == sys.stdout
            and not os.path.exists(args.infile)):
        # One filename given, and it does not exist.  Assume outfile.
        args.outfile = open(args.infile, 'w')
        args.infile = None

243
244
245
246
247
    if args.outfile.isatty():
        raise ValueError("Please specify a file name to write the %s to." %
                         ("Vega graph specification (JSON format)" if args.vega
                          else "HTML document"))

jhoogenboom's avatar
jhoogenboom committed
248
249
250
251
252
253
    if args.infile is not None and args.infile != sys.stdin:
        # Open the specified input file.
        args.infile = open(args.infile, 'r')

    create_visualisation(args.type, args.infile, args.outfile, args.vega,
                         args.online, args.tidy, args.min_abs, args.min_pct,
254
                         args.bar_width, args.padding, args.marker, args.width)
jhoogenboom's avatar
jhoogenboom committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#run


def main():
    """
    Main entry point.
    """
    parser = argparse.ArgumentParser(
        description=__doc__)
    try:
        add_arguments(parser)
        run(parser.parse_args())
    except OSError as error:
        parser.error(error)
#main


if __name__ == "__main__":
    main()