File: _cgi.py

package info (click to toggle)
python-weblogo 3.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,700 kB
  • sloc: xml: 14,455; python: 10,384; sh: 140; makefile: 58
file content (592 lines) | stat: -rwxr-xr-x 19,619 bytes parent folder | download | duplicates (2)
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
59
60
61
62
63
64
65
66
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#  Copyright (c) 2003-2004 The Regents of the University of California.
#  Copyright (c) 2005 Gavin E. Crooks
#  Copyright (c) 2006-2015, The Regents of the University of California, through
#  Lawrence Berkeley National Laboratory (subject to receipt of any required
#  approvals from the U.S. Dept. of Energy).  All rights reserved.

#  This software is distributed under the new BSD Open Source License.
#  <http://www.opensource.org/licenses/bsd-license.html>
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
#
#  (1) Redistributions of source code must retain the above copyright notice,
#  this list of conditions and the following disclaimer.
#
#  (2) Redistributions in binary form must reproduce the above copyright
#  notice, this list of conditions and the following disclaimer in the
#  documentation and or other materials provided with the distribution.
#
#  (3) Neither the name of the University of California, Lawrence Berkeley
#  National Laboratory, U.S. Dept. of Energy nor the names of its contributors
#  may be used to endorse or promote products derived from this software
#  without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGE.

import cgi as cgilib
import cgitb
import os.path
import shutil
import sys
from io import BytesIO, StringIO, TextIOWrapper
from string import Template
from typing import Any, Callable, List, Optional, Union

import weblogo
from weblogo.colorscheme import ColorScheme, SymbolColor

cgitb.enable()


# TODO: Check units

# TODO: In WebLogo2: why slash create.cgi? I think this was a workaround
# for some browser quirk
# <form method="post" action="/create.cgi" enctype="multipart/form-data">


# Should replace with weblogo.utils?
def resource_string(resource: str, basefilename: str) -> str:
    import os

    fn = os.path.join(os.path.dirname(basefilename), resource)
    return open(fn).read()


mime_type = {
    "eps": "application/postscript",
    "pdf": "application/pdf",
    "svg": "image/svg+xml",
    "png": "image/png",
    "png_print": "image/png",
    "logodata": "text/plain",
    "csv": "text/plain",
    "jpeg": "image/jpeg",
}

extension = {
    "eps": "eps",
    "pdf": "pdf",
    "png": "png",
    "svg": "svg",
    "png_print": "png",
    "logodata": "txt",
    "csv": "csv",
    "jpeg": "jpeg",
}

alphabets = {
    "alphabet_auto": None,
    "alphabet_protein": weblogo.unambiguous_protein_alphabet,
    "alphabet_rna": weblogo.unambiguous_rna_alphabet,
    "alphabet_dna": weblogo.unambiguous_dna_alphabet,
}

color_schemes = {}
for k in weblogo.std_color_schemes.keys():
    color_schemes["color_" + k.replace(" ", "_")] = weblogo.std_color_schemes[k]

composition = {
    "comp_none": "none",
    "comp_auto": "auto",
    "comp_equiprobable": "equiprobable",
    "comp_CG": "percentCG",
    "comp_Celegans": "C. elegans",
    "comp_Dmelanogaster": "D. melanogaster",
    "comp_Ecoli": "E. coli",
    "comp_Hsapiens": "H. sapiens",
    "comp_Mmusculus": "M. musculus",
    "comp_Scerevisiae": "S. cerevisiae",
}


class Field:
    """A representation of an HTML form field."""

    def __init__(
        self,
        name: str,
        default: Optional[Any] = None,
        conversion: Optional[Callable] = None,
        options: Optional[List[Any]] = None,
        errmsg: str = "Illegal value.",
    ) -> None:
        self.name = name
        self.default = default
        self.value = default
        self.conversion = conversion
        self.options = options
        self.errmsg = errmsg

    def get_value(self) -> Any:
        if self.options:
            if self.value not in self.options:
                raise ValueError(str((self.name, self.errmsg)))

        if self.conversion:
            try:
                return self.conversion(self.value)
            except ValueError:
                raise ValueError(str((self.name, self.errmsg)))
        else:
            return self.value


def string_or_none(value: Any) -> Optional[str]:
    if value is None or value == "auto":
        return None
    return str(value)


def truth(value: Any) -> bool:
    if value == "true":
        return True
    return bool(value)


def int_or_none(value: Any) -> Optional[int]:
    if value == "" or value is None or value == "auto":
        return None
    return int(value)


def float_or_none(value: Any) -> Optional[float]:
    if value == "" or value is None or value == "auto":
        return None
    return float(value)


def main(htdocs_directory: Optional[str] = None) -> None:
    logooptions = weblogo.LogoOptions()

    # A list of form fields.
    # The default for checkbox values must be False (irrespective of
    # the default in logooptions) since a checked checkbox returns 'true'
    # but an unchecked checkbox returns nothing.
    controls = [
        Field("sequences", ""),
        Field("sequences_url", ""),
        Field(
            "format",
            "png",
            weblogo.formatters.get,
            options=[
                "png_print",
                "png",
                "jpeg",
                "eps",
                "pdf",
                "svg",
                "logodata",
                "csv",
            ],
            # TODO: Should copy list from __init__.formatters
            errmsg="Unknown format option.",
        ),
        Field(
            "stacks_per_line",
            logooptions.stacks_per_line,
            int,
            errmsg="Invalid number of stacks per line.",
        ),
        Field(
            "stack_width",
            "medium",
            weblogo.std_sizes.get,
            options=["small", "medium", "large"],
            errmsg="Invalid logo size.",
        ),
        Field(
            "alphabet",
            "alphabet_auto",
            alphabets.get,
            options=[
                "alphabet_auto",
                "alphabet_protein",
                "alphabet_dna",
                "alphabet_rna",
            ],
            errmsg="Unknown sequence type.",
        ),
        Field(
            "unit_name",
            "bits",
            options=["probability", "bits", "nats", "kT", "kJ/mol", "kcal/mol"],
        ),
        Field("first_index", 1, int_or_none),
        Field("logo_start", "", int_or_none),
        Field("logo_end", "", int_or_none),
        Field(
            "composition",
            "comp_auto",
            composition.get,
            options=[
                "comp_none",
                "comp_auto",
                "comp_equiprobable",
                "comp_CG",
                "comp_Celegans",
                "comp_Dmelanogaster",
                "comp_Ecoli",
                "comp_Hsapiens",
                "comp_Mmusculus",
                "comp_Scerevisiae",
            ],
            errmsg="Illegal sequence composition.",
        ),
        Field("percentCG", "", float_or_none, errmsg="Invalid CG percentage."),
        Field("show_errorbars", False, truth),
        Field("logo_title", logooptions.logo_title),
        Field("logo_label", logooptions.logo_label),
        Field("show_xaxis", False, truth),
        Field("xaxis_label", logooptions.xaxis_label),
        Field("show_yaxis", False, truth),
        Field("yaxis_label", logooptions.yaxis_label, string_or_none),
        Field(
            "yaxis_scale",
            logooptions.yaxis_scale,
            float_or_none,
            errmsg="The yaxis scale must be a positive number.",
        ),
        Field("yaxis_tic_interval", logooptions.yaxis_tic_interval, float_or_none),
        Field("show_ends", False, truth),
        Field("show_fineprint", False, truth),
        Field(
            "color_scheme",
            "color_auto",
            color_schemes.get,
            options=list(color_schemes.keys()),
            errmsg="Unknown color scheme",
        ),
        Field("color0", ""),
        Field("symbols0", ""),
        Field("desc0", ""),
        Field("color1", ""),
        Field("symbols1", ""),
        Field("desc1", ""),
        Field("color2", ""),
        Field("symbols2", ""),
        Field("desc2", ""),
        Field("color3", ""),
        Field("symbols3", ""),
        Field("desc3", ""),
        Field("color4", ""),
        Field("symbols4", ""),
        Field("desc4", ""),
        Field("ignore_lower_case", False, truth),
        Field("scale_width", False, truth),
    ]

    form = {}
    for c in controls:
        form[c.name] = c

    form_values = cgilib.FieldStorage()

    # Send default form?
    if len(form_values) == 0 or "cmd_reset" in form_values:
        # Load default truth values now.
        form["show_errorbars"].value = logooptions.show_errorbars
        form["show_xaxis"].value = logooptions.show_xaxis
        form["show_yaxis"].value = logooptions.show_yaxis
        form["show_ends"].value = logooptions.show_ends
        form["show_fineprint"].value = logooptions.show_fineprint
        form["scale_width"].value = logooptions.scale_width

        send_form(controls, htdocs_directory=htdocs_directory)
        return

    # Get form content
    for c in controls:
        c.value = form_values.getfirst(c.name, c.default)

    options_from_form = [
        "format",
        "stacks_per_line",
        "stack_width",
        "alphabet",
        "unit_name",
        "first_index",
        "logo_start",
        "logo_end",
        "composition",
        "show_errorbars",
        "logo_title",
        "logo_label",
        "show_xaxis",
        "xaxis_label",
        "show_yaxis",
        "yaxis_label",
        "yaxis_scale",
        "yaxis_tic_interval",
        "show_ends",
        "show_fineprint",
        "scale_width",
    ]

    errors = []
    for optname in options_from_form:
        try:
            value = form[optname].get_value()
            if value is not None:
                setattr(logooptions, optname, value)
        except ValueError as err:
            errors.append(err.args)

    # Construct custom color scheme
    custom = ColorScheme()
    for i in range(0, 5):
        color = form["color%d" % i].get_value()
        symbols = form["symbols%d" % i].get_value()
        desc = form["desc%d" % i].get_value()

        if color:
            try:
                custom.rules.append(SymbolColor(symbols, color, desc))
            except ValueError:
                errors.append(("color%d" % i, "Invalid color: %s" % color))

    if form["color_scheme"].value == "color_custom":
        logooptions.color_scheme = custom
    else:
        try:
            logooptions.color_scheme = form["color_scheme"].get_value()
        except ValueError as err:
            errors.append(err.args)

    # FIXME: Ugly fix: Must check that sequence_file key exists
    # FIXME: Sending malformed or missing form keys should not cause a crash
    # sequences_file = form["sequences_file"]
    sequences_from_file = None
    if "sequences_file" in form_values:
        sequences_from_file = form_values.getvalue("sequences_file")

    sequences_from_textfield = form["sequences"].get_value()
    sequences_url = form["sequences_url"].get_value()

    sequences = None
    seq_file: Union[StringIO, TextIOWrapper, None] = None

    if sequences_from_file:
        if sequences_from_textfield or sequences_url:
            errors.append(("sequences_file", "Cannot upload, sequence source conflict"))
        else:
            sequences = sequences_from_file
            seq_file = TextIOWrapper(BytesIO(sequences), encoding="utf-8")
    elif sequences_from_textfield:
        if sequences_url:
            errors.append(("sequences", "Cannot upload, sequence source conflict"))
        else:
            # check SEQUENCES_MAXLENGT
            # If a user tries to paste a very large file into sequence textarea,
            # then WebLogo runs very slow for no apparently good reason. (Might be client side bug?)
            # So we limit the maximum sequence size.
            # Form field also limits size, but not necessarly respected. Also can truncate data
            # without warning, so we'll set textarea maximum to be larger than MAX_SEQUENCE_SIZE
            SEQUENCES_MAXLENGTH = 100000
            if len(sequences_from_textfield) > SEQUENCES_MAXLENGTH:
                errors.append(
                    (
                        "sequences",
                        "Sequence data too large for text input. Use file upload instead.",
                    )
                )
                controls[0] = Field("sequences", "")
            else:
                sequences = sequences_from_textfield
                seq_file = StringIO(sequences)

    elif sequences_url:
        from .logo import _from_URL_fileopen

        try:
            seq_file = _from_URL_fileopen(sequences_url)
        except ValueError:
            errors.append(("sequences_url", "Cannot parse URL"))
        except IOError:
            errors.append(("sequences_url", "Cannot load sequences from URL"))

    else:
        errors.append(
            (
                "sequences",
                "Please enter a multiple-sequence alignment in the box above, or select a "
                "file to upload.",
            )
        )

    # If we have uncovered errors or we want the chance to edit the logo
    # ("cmd_edit" command from examples page) then we return the form now.
    # We do not proceed to the time consuming logo creation step unless
    # required by a 'create' or 'validate' command, and no errors have been
    # found yet.
    if errors or "cmd_edit" in form_values:
        send_form(controls, errors, htdocs_directory)
        return

    try:
        comp = form["composition"].get_value()
        percentCG = form["percentCG"].get_value()
        ignore_lower_case = "ignore_lower_case" in form_values
        if comp == "percentCG":
            comp = str(percentCG / 100)

        from .matrix import Motif

        try:
            # Try reading data in transfac format first.
            # TODO Refactor this code
            motif = Motif.read_transfac(seq_file, alphabet=logooptions.alphabet)  # type: ignore  # FIXME
            prior = weblogo.parse_prior(comp, motif.alphabet)
            data = weblogo.LogoData.from_counts(motif.alphabet, motif.array, prior)
        except ValueError:
            seqs = weblogo.read_seq_data(
                seq_file,
                alphabet=logooptions.alphabet,
                ignore_lower_case=ignore_lower_case,
            )
            alpha = seqs.alphabet
            assert alpha is not None
            prior = weblogo.parse_prior(comp, alpha)
            data = weblogo.LogoData.from_seqs(seqs, prior)

        logoformat = weblogo.LogoFormat(data, logooptions)
        format = str(form["format"].value)
        logo = weblogo.formatters[format](data, logoformat)
    except ValueError as err:
        errors.append(err.args)
    except IOError as err:
        errors.append(err.args)
    except RuntimeError as err:
        errors.append(err.args)

    if errors or "cmd_validate" in form_values:
        send_form(controls, errors, htdocs_directory)
        return

    #
    #  RETURN LOGO OVER HTTP
    #

    print("Content-Type:", mime_type[format])
    # Content-Disposition: inline       Open logo in browser window
    # Content-Disposition: attachment   Download logo
    if "download" in form_values:
        print(
            "Content-Disposition: attachment; " 'filename="logo.%s"' % extension[format]
        )
    else:
        print("Content-Disposition: inline; " 'filename="logo.%s"' % extension[format])
    # Separate header from data
    print()
    sys.stdout.flush()

    # Finally, and at last, send the logo.
    sys.stdout.buffer.write(logo)


def send_form(
    controls: List[Field],
    errors: Optional[List[tuple]] = None,
    htdocs_directory: Optional[str] = None,
) -> None:
    if errors is None:
        errors = []

    if htdocs_directory is None:
        htdocs_directory = os.path.join(os.path.dirname(__file__), "htdocs")

    substitutions = {}
    substitutions["version"] = weblogo.release_description
    # Bug fix. Not sure why this default substitution isn't added automatically like everything else
    substitutions["color_custom"] = ""
    for c in controls:
        if c.options:
            for opt in c.options:
                substitutions[opt.replace("/", "_")] = ""
            substitutions[str(c.value).replace("/", "_")] = "selected"
        else:
            value = c.value
            if value is None:
                value = "auto"
            if value == "true":
                substitutions[c.name] = "checked"
            elif type(value) is bool:
                if value:
                    substitutions[c.name] = "checked"
                else:
                    substitutions[c.name] = ""
            else:
                substitutions[c.name] = str(value)
        substitutions[c.name + "_err"] = ""
    substitutions["logo_range_err"] = ""

    # Disable graphics options if necessary auxiliary programs are not installed.
    if shutil.which("gs") is None and shutil.which("gswin32c.exe") is None:
        substitutions["png_print"] = 'disabled="disabled"'
        substitutions["png"] = 'disabled="disabled"'
        substitutions["jpeg"] = 'disabled="disabled"'
        substitutions["pdf"] = 'disabled="disabled"'
        substitutions["svg"] = 'disabled="disabled"'
        substitutions["eps"] = 'selected="selected"'

    if shutil.which("pdf2svg") is None:
        substitutions["svg"] = 'disabled="disabled"'

    if errors:
        print(errors, file=sys.stderr)
        error_message: List[str] = []
        for e in errors:
            if type(e) is str:
                msg = e
            elif len(e) == 2:
                substitutions[e[0] + "_err"] = "class='error'"
                msg = e[1]
            else:
                msg = e[0]

            error_message += "ERROR: "
            error_message += msg
            error_message += " <br />"

        error_message += (
            "<input style='float:right; font-size:small' type='submit' "
            "name='cmd_validate' value='Clear Error' /> "
        )
        substitutions["error_message"] = "".join(error_message)
    else:
        substitutions["error_message"] = ""

    template = resource_string("create_html_template.html", htdocs_directory)
    html = Template(template).safe_substitute(substitutions)  # FIXME

    print("Content-Type: text/html\n\n")
    print(html)

    # DEBUG
    # keys = substitutions.keys()
    # keys.sort()
    # for k in keys :
    #    print(k, "=", substitutions[k], " <br />")

    # print(" <br />")
    # print(" <br />")
    # for k in controls :
    #    print(k.name, "=", k.get_value(), " <br />")


if __name__ == "__main__":
    from . import _cli

    _cli.main()