Create the HTML for the TsTv by quality linegraph plot.
354373
def parse_tstv_by_qual(self):
        

        self.vcftools_tstv_by_qual = dict()
        for f in self.find_log_files('vcftools/tstv_by_qual', filehandles=True):
            d = {}
            for line in f['f'].readlines()[1:]: # don't add the header line (first row)
                key = float(line.split()[0]) # taking the first column (QUAL_THRESHOLD) as key
                val = float(line.split()[6]) # taking Ts/Tv_GT_QUAL_THRESHOLD as value
                if (val == float('inf')) or (val == float('-inf')):
                    val = float('nan')
                d[key] = val
            self.vcftools_tstv_by_qual[f['s_name']] = d

        # Filter out ignored sample names
        self.vcftools_tstv_by_qual = self.ignore_samples(self.vcftools_tstv_by_qual)

        if len(self.vcftools_tstv_by_qual) == 0:
            return 0

        pconfig = {
            'id': 'vcftools_tstv_by_qual',
            'title': 'VCFTools: TsTv by Qual',
            'ylab': 'TsTv Ratio',
            'xlab': 'SNP Quality Threshold',
            'xmin': 0,
            'ymin': 0,
            'smooth_points': 400, # this limits huge filesizes and prevents browser crashing
            'smooth_points_sumcounts': False
        }

        helptext = '''
        `Transition` is a purine-to-purine or pyrimidine-to-pyrimidine point mutations.
        `Transversion` is a purine-to-pyrimidine or pyrimidine-to-purine point mutation.
        `Quality` here is the Phred-scaled quality score as given in the QUAL column of VCF.
        Note: only bi-allelic SNPs are used (multi-allelic sites and INDELs are skipped.)
        Refer to Vcftools's manual (https://vcftools.github.io/man_latest.html) on `--TsTv-by-qual`
        '''

        self.add_section(
            name = 'TsTv by Qual',
            anchor = 'vcftools-tstv-by-qual',
            description = "Plot of `TSTV-BY-QUAL` - the transition to transversion ratio as a function of SNP quality from the output of vcftools TsTv-by-qual.",
            helptext = helptext,
            plot = linegraph.plot(self.vcftools_tstv_by_qual,pconfig)
        )

        return len(self.vcftools_tstv_by_qual)
907618
def to_html(self, show_mean=None, sortable=None, colorize=True, *args,
                **kwargs):
        """"""
        if show_mean is None:
            show_mean = self.show_mean
        if sortable is None:
            sortable = self.sortable
        df = self.copy()
        if show_mean:
            df.insert(0, 'Mean', None)
            df.loc[:, 'Mean'] = ['%.3f' % self[m].mean() for m in self.models]
        html = df.to_html(*args, **kwargs)  # Pandas method
        html, table_id = self.annotate(df, html, show_mean, colorize)
        if sortable:
            self.dynamify(table_id)
        return html
984239
def parse_tstv_summary(self):
        

        self.vcftools_tstv_summary = dict()
        for f in self.find_log_files('vcftools/tstv_summary', filehandles=True):
            d = {}
            for line in f['f'].readlines()[1:]: # don't add the header line (first row)
                key = line.split()[0] # taking the first column (MODEL) as key
                val = int(line.split()[1]) # taking the second column (COUNT) as value
                d[key] = val
            self.vcftools_tstv_summary[f['s_name']] = d

        # Filter out ignored sample names
        self.vcftools_tstv_summary = self.ignore_samples(self.vcftools_tstv_summary)

        if len(self.vcftools_tstv_summary) == 0:
            return 0

        # Specifying the categories of the bargraph
        keys = OrderedDict()
        keys = ['AC', 'AG', 'AT', 'CG', 'CT', 'GT', 'Ts', 'Tv']

        pconfig = {
            'id': 'vcftools_tstv_summary',
            'title': 'VCFTools: TsTv Summary',
            'ylab': 'Counts',
        }

        self.add_section(
            name = 'TsTv Summary',
            anchor = 'vcftools-tstv-summary',
            description = "Plot of `TSTV-SUMMARY` - count of different types of transition and transversion SNPs.",
            plot = bargraph.plot(self.vcftools_tstv_summary,keys,pconfig)
        )

        return len(self.vcftools_tstv_summary)
49966
def _repr_html_(self, **kwargs):
        """"""
        if self._parent is None:
            self.add_to(Figure())
            out = self._parent._repr_html_(**kwargs)
            self._parent = None
        else:
            out = self._parent._repr_html_(**kwargs)
        return out
869443
def as_html(self, max_rows=0):
        """"""
        if not max_rows or max_rows > self.num_rows:
            max_rows = self.num_rows
        omitted = max(0, self.num_rows - max_rows)
        labels = self.labels
        lines = [
            (0, '<table border="1" class="dataframe">'),
            (1, '<thead>'),
            (2, '<tr>'),
            (3, ' '.join('<th>' + label + '</th>' for label in labels)),
            (2, '</tr>'),
            (1, '</thead>'),
            (1, '<tbody>'),
        ]
        fmts = self._get_column_formatters(max_rows, True)
        for row in itertools.islice(self.rows, max_rows):
            lines += [
                (2, '<tr>'),
                (3, ' '.join('<td>' + fmt(v, label=False) + '</td>' for
                    v, fmt in zip(row, fmts))),
                (2, '</tr>'),
            ]
        lines.append((1, '</tbody>'))
        lines.append((0, '</table>'))
        if omitted:
            lines.append((0, '<p>... ({} rows omitted)</p>'.format(omitted)))
        return '\n'.join(4 * indent * ' ' + text for indent, text in lines)
230699
def _repr_html_(self):
        
        row1 = '<tr><th style="text-align:center;" '
        row1 += 'colspan="2">{}<br><small>{{}}</small></th></tr>'
        rows = row1.format(getattr(self.header, 'name', ''))
        rows = rows.format(getattr(self.header, 'uwi', ''))
        s = '<tr><td><strong>{k}</strong></td><td>{v}</td></tr>'

        if getattr(self, 'location', None) is not None:
            for k, v in self.location.__dict__.items():
                if k in ['deviation', 'position']:
                    continue
                if k == 'crs':
                    v = v.__repr__()
                rows += s.format(k=k, v=v)

        if getattr(self, 'data', None) is not None:
            v = ', '.join(sorted(list(self.data.keys())))
            rows += s.format(k="data", v=v)

        html = '<table>{}</table>'.format(rows)
        return html
385955
def _generate_html(data, out):
    '''
    
    '''
    print('<html>', file=out)
    print('<body>', file=out)
    _generate_html_table(data, out, 0)
    print('</body>', file=out)
    print('</html>', file=out)
972973
def toplot(ts,
           filename=None,
           grid=True,
           legend=True,
           pargs=(),
           **kwargs):
    ''''''
    fig = plt.figure()
    ax = fig.add_subplot(111)
    dates = list(ts.dates())
    ax.plot(dates, ts.values(), *pargs)
    ax.grid(grid)

    # rotates and right aligns the x labels, and moves the bottom of the
    # axes up to make room for them
    fig.autofmt_xdate()

    # add legend or title
    names = ts.name.split('__')
    if len(names) == 1:
        title = names[0]
        fontweight = kwargs.get('title_fontweight', 'bold')
        ax.set_title(title, fontweight=fontweight)#,fontsize=fontsize,
    elif legend:
        ##add legend
        loc = kwargs.get('legend_location','best')
        ncol = kwargs.get('legend_ncol', 2)
        ax.legend(names, loc=loc, ncol=ncol)

    return plt
659437
def generate_graph(self):
        
        logger.debug('Generating graph for %s', self._graph_id)
        # tools to use
        tools = [
            PanTool(),
            BoxZoomTool(),
            WheelZoomTool(),
            SaveTool(),
            ResetTool(),
            ResizeTool()
        ]

        # generate the stacked area graph
        try:
            g = Area(
                self._data, x='Date', y=self._y_series_names,
                title=self._title, stack=True, xlabel='Date',
                ylabel='Downloads', tools=tools,
                # note the width and height will be set by JavaScript
                plot_height=400, plot_width=800,
                toolbar_location='above', legend=False
            )
        except Exception as ex:
            logger.error("Error generating %s graph", self._graph_id)
            logger.error("Data: %s", self._data)
            logger.error("y=%s", self._y_series_names)
            raise ex

        lines = []
        legend_parts = []
        # add a line at the top of each Patch (stacked area) for hovertool
        for renderer in g.select(GlyphRenderer):
            if not isinstance(renderer.glyph, Patches):
                continue
            series_name = renderer.data_source.data['series'][0]
            logger.debug('Adding line for Patches %s (series: %s)', renderer,
                         series_name)
            line = self._line_for_patches(self._data, g, renderer, series_name)
            if line is not None:
                lines.append(line)
                legend_parts.append((series_name, [line]))

        # add the Hovertool, specifying only our line glyphs
        g.add_tools(
            HoverTool(
                tooltips=[
                    (self._y_name, '@SeriesName'),
                    ('Date', '@FmtDate'),
                    ('Downloads', '@Downloads'),
                ],
                renderers=lines,
                line_policy='nearest'
            )
        )

        # legend outside chart area
        legend = Legend(legends=legend_parts, location=(0, 0))
        g.add_layout(legend, 'right')
        return components(g)
52422
def as_html(self, table_class='code-difftable', line_class='line',
                new_lineno_class='lineno old', old_lineno_class='lineno new',
                code_class='code'):
        
        def _link_to_if(condition, label, url):
            """
            Generates a link if condition is meet or just the label if not.
            """

            if condition:
                return '''<a href="%(url)s">%(label)s</a>''' % {'url': url,
                                                                'label': label}
            else:
                return label
        diff_lines = self.prepare()
        _html_empty = True
        _html = []
        _html.append('''<table class="%(table_class)s">\n''' \
                                            % {'table_class': table_class})
        for diff in diff_lines:
            for line in diff['chunks']:
                _html_empty = False
                for change in line:
                    _html.append('''<tr class="%(line_class)s %(action)s">\n''' \
                        % {'line_class': line_class,
                           'action': change['action']})
                    anchor_old_id = ''
                    anchor_new_id = ''
                    anchor_old = "%(filename)s_o%(oldline_no)s" % \
                                {'filename': self._safe_id(diff['filename']),
                                 'oldline_no': change['old_lineno']}
                    anchor_new = "%(filename)s_n%(oldline_no)s" % \
                                {'filename': self._safe_id(diff['filename']),
                                 'oldline_no': change['new_lineno']}
                    cond_old = change['old_lineno'] != '...' and \
                                                        change['old_lineno']
                    cond_new = change['new_lineno'] != '...' and \
                                                        change['new_lineno']
                    if cond_old:
                        anchor_old_id = 'id="%s"' % anchor_old
                    if cond_new:
                        anchor_new_id = 'id="%s"' % anchor_new
                    ###########################################################
                    # OLD LINE NUMBER
                    ###########################################################
                    _html.append('''\t<td %(a_id)s class="%(old_lineno_cls)s">''' \
                                    % {'a_id': anchor_old_id,
                                       'old_lineno_cls': old_lineno_class})

                    _html.append('''<pre>%(link)s</pre>''' \
                        % {'link':
                        _link_to_if(cond_old, change['old_lineno'], '#%s' \
                                                                % anchor_old)})
                    _html.append('''</td>\n''')
                    ###########################################################
                    # NEW LINE NUMBER
                    ###########################################################

                    _html.append('''\t<td %(a_id)s class="%(new_lineno_cls)s">''' \
                                    % {'a_id': anchor_new_id,
                                       'new_lineno_cls': new_lineno_class})

                    _html.append('''<pre>%(link)s</pre>''' \
                        % {'link':
                        _link_to_if(cond_new, change['new_lineno'], '#%s' \
                                                                % anchor_new)})
                    _html.append('''</td>\n''')
                    ###########################################################
                    # CODE
                    ###########################################################
                    _html.append('''\t<td class="%(code_class)s">''' \
                                                % {'code_class': code_class})
                    _html.append('''\n\t\t<pre>%(code)s</pre>\n''' \
                                                % {'code': change['line']})
                    _html.append('''\t</td>''')
                    _html.append('''\n</tr>\n''')
        _html.append('''</table>''')
        if _html_empty:
            return None
        return ''.join(_html)
1001067
def save(self,
             data,
             chart_type,
             options=None,
             filename='chart',
             w=800,
             h=420,
             overwrite=True):
        """"""
        html = self.render(
            data=data,
            chart_type=chart_type,
            options=options,
            div_id=filename,
            head=self.head,
            w=w,
            h=h)

        if overwrite:
            with open(filename.replace(" ", "_") + '.html', 'w') as f:
                f.write(html)
        else:
            if not os.path.exists(filename.replace(" ", "_") + '.html'):
                with open(filename.replace(" ", "_") + '.html', 'w') as f:
                    f.write(html)
            else:
                raise IOError('File Already Exists!')
1147253
def _generate_html(self):
        
        logger.debug('Generating templated HTML')
        env = Environment(
            loader=PackageLoader('pypi_download_stats', 'templates'),
            extensions=['jinja2.ext.loopcontrols'])
        env.filters['format_date_long'] = filter_format_date_long
        env.filters['format_date_ymd'] = filter_format_date_ymd
        env.filters['data_columns'] = filter_data_columns
        template = env.get_template('base.html')

        logger.debug('Rendering template')
        html = template.render(
            project=self.project_name,
            cache_date=self._stats.as_of_datetime,
            user=getuser(),
            host=platform_node(),
            version=VERSION,
            proj_url=PROJECT_URL,
            graphs=self._graphs,
            graph_keys=self.GRAPH_KEYS,
            resources=Resources(mode='inline').render(),
            badges=self._badges
        )
        logger.debug('Template rendered')
        return html
1290945
def make_report(plots, path):
    '''
    
    '''
    logging.info("Writing html report.")
    html_head = """<!DOCTYPE html>
    <html>
        <head>
        <meta charset="UTF-8">
            <style>
            table, th, td {
                text-align: left;
                padding: 2px;
                /* border: 1px solid black;
                border-collapse: collapse; */
            }
            h2 {
                line-height: 0pt;
            }
            </style>
            <title>NanoComp Report</title>
        </head>"""
    html_content = ["\n<body>\n<h1>NanoComp report</h1>"]
    html_content.append("<h2>Summary statistics</h2>")
    html_content.append(utils.stats2html(path + "NanoStats.txt"))
    html_content.append('\n<br>\n<br>\n<br>\n<br>')
    html_content.append("<h2>Plots</h2>")
    for plot in plots:
        html_content.append("\n<h3>" + plot.title + "</h3>\n" + plot.encode())
        html_content.append('\n<br>\n<br>\n<br>\n<br>')
    html_body = '\n'.join(html_content) + "</body></html>"
    html_str = html_head + html_body
    with open(path + "NanoComp-report.html", "w") as html_file:
        html_file.write(html_str)
    return path + "NanoComp-report.html"
242622
def _make_graph(self):
        """"""
        self.nodes['graph'] = self.svg.node(
            class_='graph %s-graph %s' % (
                self.__class__.__name__.lower(),
                'horizontal' if self.horizontal else 'vertical'
            )
        )
        self.svg.node(
            self.nodes['graph'],
            'rect',
            class_='background',
            x=0,
            y=0,
            width=self.width,
            height=self.height
        )
        self.nodes['plot'] = self.svg.node(
            self.nodes['graph'],
            class_="plot",
            transform="translate(%d, %d)" %
            (self.margin_box.left, self.margin_box.top)
        )
        self.svg.node(
            self.nodes['plot'],
            'rect',
            class_='background',
            x=0,
            y=0,
            width=self.view.width,
            height=self.view.height
        )
        self.nodes['title'] = self.svg.node(
            self.nodes['graph'], class_="titles"
        )
        self.nodes['overlay'] = self.svg.node(
            self.nodes['graph'],
            class_="plot overlay",
            transform="translate(%d, %d)" %
            (self.margin_box.left, self.margin_box.top)
        )
        self.nodes['text_overlay'] = self.svg.node(
            self.nodes['graph'],
            class_="plot text-overlay",
            transform="translate(%d, %d)" %
            (self.margin_box.left, self.margin_box.top)
        )
        self.nodes['tooltip_overlay'] = self.svg.node(
            self.nodes['graph'],
            class_="plot tooltip-overlay",
            transform="translate(%d, %d)" %
            (self.margin_box.left, self.margin_box.top)
        )
        self.nodes['tooltip'] = self.svg.node(
            self.nodes['tooltip_overlay'],
            transform='translate(0 0)',
            style="opacity: 0",
            **{'class': 'tooltip'}
        )

        self.svg.node(
            self.nodes['tooltip'],
            'rect',
            rx=self.tooltip_border_radius,
            ry=self.tooltip_border_radius,
            width=0,
            height=0,
            **{'class': 'tooltip-box'}
        )
        self.svg.node(self.nodes['tooltip'], 'g', class_='text')
1239275
def render(self, **kwargs):
        
        # Calling parent function
        super(VisCurve2D, self).render(**kwargs)

        # Initialize variables
        plot_data = []

        for plot in self._plots:
            pts = np.array(plot['ptsarr'], dtype=self.vconf.dtype)

            # Plot control points
            if plot['type'] == 'ctrlpts' and self.vconf.display_ctrlpts:
                figure = graph_objs.Scatter(
                    x=pts[:, 0],
                    y=pts[:, 1],
                    name=plot['name'],
                    mode='lines+markers',
                    line=dict(
                        color=plot['color'],
                        width=self.vconf.line_width,
                        dash='dash'
                    )
                )
                plot_data.append(figure)

            # Plot evaluated points
            if plot['type'] == 'evalpts' and self.vconf.display_evalpts:
                figure = graph_objs.Scatter(
                    x=pts[:, 0],
                    y=pts[:, 1],
                    name=plot['name'],
                    mode='lines',
                    line=dict(
                        color=plot['color'],
                        width=self.vconf.line_width
                    )
                )
                plot_data.append(figure)

            # Plot bounding box
            if plot['type'] == 'bbox' and self.vconf.display_bbox:
                figure = graph_objs.Scatter(
                    x=pts[:, 0],
                    y=pts[:, 1],
                    name=plot['name'],
                    line=dict(
                        color=plot['color'],
                        width=self.vconf.line_width,
                        dash='dashdot',
                    )
                )
                plot_data.append(figure)

            # Plot extras
            if plot['type'] == 'extras':
                figure = graph_objs.Scatter(
                    x=pts[:, 0],
                    y=pts[:, 1],
                    name=plot['name'],
                    mode='markers',
                    marker=dict(
                        color=plot['color'][0],
                        size=plot['color'][1],
                        line=dict(
                            width=self.vconf.line_width
                        )
                    )
                )
                plot_data.append(figure)

        plot_layout = dict(
            width=self.vconf.figure_size[0],
            height=self.vconf.figure_size[1],
            autosize=False,
            showlegend=self.vconf.display_legend,
            yaxis=dict(
                scaleanchor="x",
                showgrid=self.vconf.display_axes,
                showline=self.vconf.display_axes,
                zeroline=self.vconf.display_axes,
                showticklabels=self.vconf.display_axes,
            ),
            xaxis=dict(
                showgrid=self.vconf.display_axes,
                showline=self.vconf.display_axes,
                zeroline=self.vconf.display_axes,
                showticklabels=self.vconf.display_axes,
            )
        )

        # Generate the figure
        fig = graph_objs.Figure(data=plot_data, layout=plot_layout)

        # Process keyword arguments
        fig_filename = kwargs.get('fig_save_as', None)
        fig_display = kwargs.get('display_plot', True)

        # Prepare plot configuration
        plotfn_dict = {
            'show_link': False,
            'filename': self.vconf.figure_filename,
            'image': None if fig_display else self.vconf.figure_image_format,
        }
        if self.vconf.no_ipython:
            plotfn_dict_extra = {
                'image_filename': self.vconf.figure_image_filename if fig_filename is None else fig_filename,
                'auto_open': fig_display,
            }
            # Python < 3.5 does not support starred expressions inside dicts
            plotfn_dict.update(plotfn_dict_extra)

        # Display the plot
        self.vconf.plotfn(fig, **plotfn_dict)
771923
def plotGraph(self,mode="plain",nodes=None,filename="tgraph.png"):
        
        if nodes==None:
            nodes=self.nodes
        else:
            nodes=[i for i in self.nodes if i in nodes]
        for node in self.nodes:
            n_=self.A.get_node(node)
            if mode=="plain":
                nmode=1
            else:
                nmode=-1
            pos="{},{}".format(self.xi[::nmode][self.nm.nodes_.index(node)],self.yi[::nmode][self.nm.nodes_.index(node)])
            n_.attr["pos"]=pos
            n_.attr["pin"]=True
            color='#%02x%02x%02x' % tuple([255*i for i in self.cm[int(self.clustering[n_]*255)][:-1]])
            n_.attr['fillcolor']= color
            n_.attr['fixedsize']=True
            n_.attr['width']=  abs(.1*(self.nm.degrees[n_]+  .5))
            n_.attr['height']= abs(.1*(self.nm.degrees[n_]+.5))
            n_.attr["label"]=""
            if node not in nodes:
                n_.attr["style"]="invis"
            else:
                n_.attr["style"]="filled"
        for e in self.edges:
            e.attr['penwidth']=3.4
            e.attr["arrowsize"]=1.5
            e.attr["arrowhead"]="lteeoldiamond"
            e.attr["style"]=""
            if sum([i in nodes for i in (e[0],e[1])])==2:
                e.attr["style"]=""
            else:
                e.attr["style"]="invis"
        tname="{}{}".format(self.basedir,filename)
        print(tname)
        self.A.draw(tname,prog="neato")
998465
def create_html(self):
        """"""

        html_table = ""
        columns = [panel.get_html_column() for panel in self.panels]
        trs = len(columns[0])
        html_table += os.linesep.join(
            [
                "<tr>{}</tr>".format("".join(["<td>{}</td>".format(columns[col][row]) for col in range(len(columns))]))
                for row in range(trs)
            ]
        )

        with open(self._html_fn, "w+") as f:
            css_src = textwrap.dedent(
                """\
					.main_table                       {border-collapse:collapse;margin-top:15px;}
					td                                {border: solid #aaaaff 1px;padding:4px;vertical-alignment:top;}
					colgroup, thead                   {border: solid black 2px;padding 2px;}
					.configuration                    {font-size:85%;}
					.configuration, .configuration *  {margin:0;padding:0;}
					.formats                          {text-align:center;margin:20px 0px;}
					img                               {min-width:640px}
			"""
            )

            html_src = """<!DOCTYPE html>
			<html>
			<head>
				<meta charset="UTF-8" />
				<title>{title}</title>
				<style type="text/css">
				{css}
				</style>
			</head>
			<body>
				<h1>{title}</h1>
				<strong>{description}</strong>

				<table class="main_table">
				{html_table}
				</table>

			</body>
			""".format(
                html_table=html_table,
                css=css_src,
                title=self.title,
                description=self.description,
            )

            tidy_html_src = bs4.BeautifulSoup(html_src).prettify()
            f.write(tidy_html_src)