PeakViewerApp

The PeakViewerApp class is the top level of the GUI. When the class is instantiated, it creates all the components (buttons, sliders, labels, etc) and lays them out in the browser window.

Layout

The app is based on a Panel template named BootstrapTemplate. Applications that use this template have a title at the top of the window, a sidebar on the left side, and a main content area.

In our application, we put the filter widgets in the sidebar. The main content is a "tab" widget with two tabs, one to show a single chromosome and the other to show summary plots. Each tab also needs its own layout:

  • the chromosome tab has controls to navigate to different chromosomes and a column that has graphics and tables for the blocks in the current chromosome
  • the summary tab has a text widget for entering a chromosome name pattern and a set of buttons to make plots

The initialization code has a series of assignment statements that create objects and save them as instance variables, e.g.

    self.back_button = pn.widgets.Button(name='◀︎', stylesheets=[button_style_sheet])
    self.forward_button = pn.widgets.Button(name='▶︎', stylesheets=[button_style_sheet])

After widgets are created they need to be placed in a layout. This statement lays out the structure of the chromosome tab:

    chr_tab = pn.Column(
        pn.pane.HTML('<h3>Chromosome</h3>'),
        pn.Row(self.back_button, self.chromosome_id, self.forward_button),
        ...
    )

Eventually all the layouts are saved in either the sidebar or the main section.

Callbacks

Another important role for the initialization function is to connect widgets to code that will be executed when the widget is activated, i.e. when the button on a scrollbar is moved, or a button is clicked.

This is accomplished by telling the application to "watch" the value of a widget. When the value changes we want to call a function to respond to the change. This example shows how we tell the application to call a method named chromosome_edited_cb whenver the text in the chromosome name widget is updated:

    self.chromosome_id.param.watch(self.chromosome_edited_cb, ['value'])  

Initialization Method

Bases: BootstrapTemplate

Source code in src/xo/gui.py
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
def __init__(self, **params):

    super(PeakViewerApp, self).__init__(**params)

    self.peaks = None               # groups of SNPs found by peak finder
    # self._filtered = None           # filtered blocks
    # self._ncos = None               # NCOs

    self.snp_filter = SNPFilter({})
    self.block_widgets = FilterBox(self.snp_filter)

    self.nco_filter = NCOFilter({})
    self.nco_widgets = NCOFilterBox(self.nco_filter)
    self.nco_widgets.visible = False

    self.nco_switch = pn.widgets.Switch(name='nco_switch')

    button_style_sheet = ''':host(.solid) .bk-btn {
        --color: white;
    }
    '''

    self.back_button = pn.widgets.Button(name='◀︎', stylesheets=[button_style_sheet])
    self.forward_button = pn.widgets.Button(name='▶︎', stylesheets=[button_style_sheet])
    self.chromosome_id = pn.widgets.TextInput(name="", value="")
    self.open_blocks = set()

    # self.chromosome_pattern = pn.widgets.TextInput(name="Chromosomes", value="BSP.*")
    # self.size_graph_button = pn.widgets.Button(name='Block Size', stylesheets=[button_style_sheet])
    # self.length_graph_button = pn.widgets.Button(name='Block Length', stylesheets=[button_style_sheet])
    # self.location_graph_button = pn.widgets.Button(name='Block Location', stylesheets=[button_style_sheet])

    # self.download_button = pn.widgets.FileDownload(callback=self.download_cb, filename='summary.csv', align='center', icon='download')
    # self.download_pane = pn.GridBox(self.download_button, height=200, width=SIDEBAR_WIDTH)
    # self.download_button.visible = False

    self.attach_callbacks()

    self.chromosome_panel = pn.Column(
        pn.pane.HTML('<h3>Chromosome Viewer</h3>'),
        pn.Row(self.back_button, self.chromosome_id, self.forward_button),
        pn.pane.HTML('<p>Placeholder</p>'),
        height=800,
    )

    self.sidebar.append(pn.pane.HTML("<h3>Block Parameters</h3>"))
    self.sidebar.append(self.block_widgets)
    self.sidebar.append(pn.layout.Divider())
    self.sidebar.append(pn.Row(
        pn.pane.HTML("<b>Find NCOs</b>"),
        self.nco_switch,
    ))
    self.sidebar.append(self.nco_widgets)

    self.main.append(self.chromosome_panel)

attach_callbacks()

This method is called after all of the widgets have been created. It connects various widgets to functions that will be called when the widgets are activated.

Source code in src/xo/gui.py
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
def attach_callbacks(self):
    '''
    This method is called after all of the widgets have been created.
    It connects various widgets to functions that will be called when
    the widgets are activated.
    '''
    for w in self.block_widgets.widgets():
        w.param.watch(self.filter_cb, ['value'])

    for w in self.nco_widgets.widgets():
        w.param.watch(self.filter_cb, ['value'])

    for w in [self.back_button, self.forward_button]:
        w.param.watch(self.change_chromosome_cb, ['value'])

    # for w in [self.size_graph_button, self.length_graph_button, self.location_graph_button]:
    #     w.param.watch(self.summary_plot_cb, ['value'])

    self.chromosome_id.param.watch(self.chromosome_edited_cb, ['value'])      

    self.nco_switch.param.watch(self.find_ncos_cb, ['value'])

load_data(args)

This method is called from the top level application after the GUI has been initialized. It reads the three data files needed by the application: * the interval data file has the names of all the chromosomes (regardless of whether any SNPs were found); it's used to initialize the list of chromosome names.
* the peaks data file (loaded by the filter object) has blocks of SNPs to visualize * the crossover data file has locations of main crossover points

Chromosome names are saved in two instance vars
  • clist is a list of all chromosome names; the forward and backward buttons move to the next or previous chromosome in this list
  • cmap is a dictionary that maps chromosome names to locations in clist; when the user types a new name in the chromosome name box the app uses the map to find the new list location
Parameters:
  • args –

    command line arguments

Source code in src/xo/gui.py
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
def load_data(self, args):
    '''
    This method is called from the top level application after the GUI has
    been initialized. It reads the three data files needed by the application:
      * the interval data file has the names of all the chromosomes 
        (regardless of whether any SNPs were found); it's used to initialize the 
        list of chromosome names.  
      * the peaks data file (loaded by the filter object) has blocks of SNPs to
        visualize
      * the crossover data file has locations of main crossover points

    Chromosome names are saved in two instance vars:
      * clist is a list of all chromosome names; the forward and backward buttons
        move to the next or previous chromosome in this list
      * cmap is a dictionary that maps chromosome names to locations in clist; when
        the user types a new name in the chromosome name box the app uses the map
        to find the new list location

    Arguments:
      args:  command line arguments 
    '''
    c = Config()

    logging.info('Loading intervals')
    self.intervals = pd.read_pickle(c.intervals_default, compression='gzip').groupby('chrom_id')
    self.clist = list(self.intervals.groups.keys())
    self.cmap = { name: i for i, name in enumerate(self.clist)}
    logging.info(f'  read {len(self.intervals)} intervals')

    logging.info('Loading crossovers')
    self.crossovers = pd.read_pickle(c.crossovers_default, compression='gzip').groupby('chrom_id')
    logging.info(f'  read {len(self.crossovers)} crossovers')

    logging.info('Loading peaks')
    df = pd.read_csv(args.peaks)
    df['chr_length'] = df.chromosome.map(lambda n: chr_length[n])
    df['location'] = df.position / df.chr_length
    df['homozygosity'] = df.ref_reads / (df.ref_reads + df.var_reads)
    self.peaks = df.groupby('chrom_id')

    # self._chromosome_names = self.peaks.groups.keys()

    # p = Path(args.filtered)
    # if p.is_file():
    #     logging.info('Loading filtered')
    #     self._filtered = pd.read_csv(p)
    #     logging.info(f'  read {len(self._filtered)} filtered blocks from {args.filtered}')

    # p = Path(args.ncos)
    # if p.is_file():
    #     logging.info('Loading NCOs')
    #     self._ncos = pd.read_csv(p)
    #     logging.info(f'  read {len(self._ncos)} NCO blocks from {args.ncos}')

    # setting a value in the chromosome name widget triggers an update
    # to the graphic to display the first chromosome
    self.chr_index = 0
    self.chromosome_id.value = self.clist[self.chr_index]