pdf_report.py 3.9 KB
Newer Older
bow's avatar
bow committed
1
#!/usr/bin/env python
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#
# Biopet is built on top of GATK Queue for building bioinformatic
# pipelines. It is mainly intended to support LUMC SHARK cluster which is running
# SGE. But other types of HPC that are supported by GATK Queue (such as PBS)
# should also be able to execute Biopet tools and pipelines.
#
# Copyright 2014 Sequencing Analysis Support Core - Leiden University Medical Center
#
# Contact us at: sasc@lumc.nl
#
# A dual licensing mode is applied. The source code within this project that are
# not part of GATK Queue is freely available for non-commercial use under an AGPL
# license; For commercial users or users who do not want to follow the AGPL
# license, please contact us to obtain a separate license.
#
bow's avatar
bow committed
17
# pdf_report.py
18
#
bow's avatar
bow committed
19
20
# TeX template generation script for Gentrap pipeline.

21
from __future__ import print_function
bow's avatar
bow committed
22
23
24
25
26
27

import argparse
import json
import locale
import os
import re
28
29
import sys
from os import path
bow's avatar
bow committed
30
31
32
33

from jinja2 import Environment, FileSystemLoader

# set locale for digit grouping
34
locale.setlocale(locale.LC_ALL, "")
bow's avatar
bow committed
35
36
37
38
39
40
41
42

# HACK: remove this and use jinja2 only for templating
class LongTable(object):

    """Class representing a longtable in LaTeX."""

    def __init__(self, caption, label, header, aln, colnum):
        self.lines = [
43
44
45
46
47
48
49
50
51
            "\\begin{center}",
            "\\captionof{table}{%s}" % caption,
            "\\label{%s}" % label,
            "\\begin{longtable}{%s}" % aln,
            "\\hline",
            "%s" % header,
            "\\hline \\hline",
            "\\endhead",
            "\\hline \\multicolumn{%i}{c}{\\textit{Continued on next page}}\\\\" % \
bow's avatar
bow committed
52
                    colnum,
53
54
55
56
            "\\hline",
            "\\endfoot",
            "\\hline",
            "\\endlastfoot",
bow's avatar
bow committed
57
58
59
        ]

    def __str__(self):
60
        return "\n".join(self.lines)
bow's avatar
bow committed
61
62
63
64
65

    def add_row(self, row):
        self.lines.append(row)

    def end(self):
66
67
68
        self.lines.extend(["\\end{longtable}", "\\end{center}",
            "\\addtocounter{table}{-1}"])

bow's avatar
bow committed
69
70
71

# filter functions for the jinja environment
def nice_int(num):
72
    return locale.format("%i", int(num), grouping=True)
bow's avatar
bow committed
73
74


75
76
def nice_flt(num):
    return locale.format("%.2f", float(num), grouping=True)
bow's avatar
bow committed
77
78
79
80
81


# and some handy functions
def natural_sort(inlist):
    key = lambda x: [int(a) if a.isdigit() else a.lower() for a in
82
            re.split("([0-9]+)", x)]
bow's avatar
bow committed
83
84
85
86
    inlist.sort(key=key)
    return inlist


87
def write_template(summary_file, template_file):
bow's avatar
bow committed
88

89
90
    template_file = path.abspath(path.realpath(template_file))
    template_dir = path.dirname(template_file)
bow's avatar
bow committed
91
92
93
    # spawn environment and create output directory
    env = Environment(loader=FileSystemLoader(template_dir))

94
95
96
97
98
99
100
    # change delimiters since LaTeX may use "{{", "{%", or "{#"
    env.block_start_string = "((*"
    env.block_end_string = "*))"
    env.variable_start_string = "((("
    env.variable_end_string = ")))"
    env.comment_start_string = "((="
    env.comment_end_string = "=))"
bow's avatar
bow committed
101
102
103
104
105
106

    # trim all block-related whitespaces
    env.trim_blocks = True
    env.lstrip_blocks = True

    # put in out filter functions
107
108
    env.filters["nice_int"] = nice_int
    env.filters["nice_flt"] = nice_flt
bow's avatar
bow committed
109
110

    # write tex template for pdflatex
111
    jinja_template = env.get_template(path.basename(template_file))
bow's avatar
bow committed
112
    render_vars = {
113
114
115
116
        "gentrap": {
            "version": "--testing--",
            "logo": "/home/warindrarto/devel/intellij/biopet/public/gentrap/src/main/resources/nl/lumc/sasc/biopet/pipelines/gentrap/templates/img/gentrap_front.png",
        },
bow's avatar
bow committed
117
118
119
    }
    rendered = jinja_template.render(**render_vars)

120
    print(rendered, file=sys.stdout)
bow's avatar
bow committed
121
122


123
if __name__ == "__main__":
bow's avatar
bow committed
124
125

    parser = argparse.ArgumentParser()
126
127
128
129
    parser.add_argument("summary_file", type=str,
            help="Path to Gentrap summary file")
    parser.add_argument("template_file", type=str,
            help="Path to main template file")
bow's avatar
bow committed
130
131
    args = parser.parse_args()

132
    write_template(args.summary_file, args.template_file)