Skip to content

Add Statistics plot analysis

Diogo Silva edited this page Dec 13, 2017 · 7 revisions

How to add new Statistics plots

Create analysis method

Add the new method to the AlignmentList object. Objects returned by plot methods should always be dictionaries, with the arguments for the plotter.py functions. The only required element is "data". Other elements depend on the function in plotter.py that will be used. Note that this method should not return a matplotlib object, or create a figure. That is performed by the corresponding function in plotter.py.

Add new category to Statistics exploration panel, if needed

If the new analysis does not fit in the main categories, a new category must be added. This can be done in data/screens/Statistics.kv, inside the GridLayout with id: main_stats_opts and requires the addition of two objects:

A TFStatsButton, with the desired text attribute and an on_release event calling app.toggle_stats_options(self, self.text). A OptsGrid with a unique id attribute and a grid_name attribute that must match the text of TFSTatsButton. An example follows:

TFStatsButton:
    text: "My analysis"
    on_release:
        app.toggle_stats_options(self, self.text)
OptsGrid:
    id: my_analysis
    grid_name: "My analysis" # Must match TFStatsButton.text
Add the analysis button

First, create an empty class in data/resources/custom_widgets.py that inherits from StatsBox and in trifusion.kv set the attributes for the widget. This widget should be a BoxLayout that will contain two buttons: The button issuing the analysis and a button that opens the help popup. An example follows:

<MyAnalysis>:
    TFButtonOff:
        text: "My analysis"
        on_release:
            app.stats_select_plot("My analysis gn", "My analysis sp", "My analysis avg")
    HelpButton:
        app.dialog_general_info("my_analysis_help")

Note that the on_release event of the TFButtonOff calls app.stats_select_plot() with three arguments, and this usage is encouraged for all analysis. The first argument refers to a plot for a single gene, the second refers to the per species plot, and the third refers to the average over all alignments. This provides supports for each one of these analyses, but they may not make sense for all cases. In cases where a certain analysis does not make sense, simply provide None as an argument (e.g. app.stats_select_plot(None, "My analysis sp", None)). The app will automatically disable the options that have None

Add analysis button to stats toggle

In order to your button to show when clicking the corresponding category, it must be added to the toggle routine. To achieve this, simply add a new entry to the wgts variable in the toggle_stats_optionsin TriFusion.py. The entry should have the category name as the key, and the value should be a list, with the first element being the the OptsGrid object, and the second element a list of all "Buttons" (The BoxLayout containing the actual button and the help button) classes. Following the previous example:

wgts = { ...,
        "My analysis": [self.screen.ids.my_analysis, [MyAnalysis()]]}
Add method to the Apps' method list

Add the method created in the first step to the methods variable in get_stats_data function in data/resources/background_tasks.py. An entry (the plot index) should be added for each of type of analysis plot (average, species and gene). The average analysis has no suffix, while the species and gene have ("sp" and "gene", respectively).

methods = {...,
           "My analysis": aln_obj.my_analysis,
           "My analysis sp": aln_obj.my_analysis_per_sp,
           "My analysis gn": aln_obj.my_analysis_single_gene}

Note that the methods are not called. This is only a reference for the bound method, which will be called later with the appropriate arguments.

Associate analysis with plotting method

To link the analysis with the corresponding plotting function (which can be chosen from in base/plotter.py; New plotting function may be added, if needed), simply add a new element to the self.stats_plt_method, in the stats_write_plot function, with the key being the plot index string, and the value a list with the plot bound method as first element, and figure name as second element.

self.stats_plt_method = {...,
                         "My analysis": [box_plot, "my_analysis.png"],
                         "My analysis sp": [triangular_head, "my_analysis_sp.png"],
                         "My analysis gn": [sliding_window, "my_analysis_gn.png"]}
Configure triggers between plot types

If the new analysis has multiple plot types (average, species and gene), this information should be added in the stats_compliant variable defined in data.resources.stats. An entry for each plot type must be added, to a maximum of three. Each entry should have the plot index string as key and a dictionary as value. This dictionary contains the functions for the three plot types and the active button. The contents of this dictionary are:

args1, args2 and single_gene: Contain a dictionary with a single element. The key is "plot_idx" and the value is the plot index string for that plot type:

  • args1: Species plot type
  • args2: Average plot type
  • single_gene: Single gene plot type

The plot type that is active, should have None as value, since the button will be disabled, except for the single_gene

active_bt: Identifies the active button using the corresponding strings. Average ("avg"), Species ("sp"), Single gene ("gene").

stats_compliant = {...,
                   "My analysis": {"args1": None,
                                   "args2": {"plt_idx": "My analysis sp"},
                                   "active_bt": "avg",
                                   "single_gene": {"plt_idx": "My analysis gn"}}}
Remove sliding window plots from fast-switch

Sliding window plots associated with "Single gene" analysis are usually bound to be removed at the end, instead of storing every plot with different sliding window options. To remove a plot from the fast-switch system add the plt_idx and plot file name to the gene_specific dictionary in the stats_show_plot method.

gene_specific = {...,
                 "My analysis": "my_analysis_gn.png"}

Add new plot to TriStats configuration template

Add the plot to the examples.stats_template.ini file and the generate_cfg_template function in TriStats.py. The category and option names defined in the configuration file should then be used as a key tuple for the func_map function in TriStats.py.

And should be done!