vanallenlab / comut

CoMut is a Python library for visualizing genomic and phenotypic information via comutation plots
MIT License
88 stars 28 forks source link

Issues with re-ordering legends #7

Closed erikagedvilaite closed 4 years ago

erikagedvilaite commented 4 years ago

Hello, can someone help me out with this error:

Code:

# plot
toy_comut = comut.CoMut()
toy_comut.add_categorical_data(cnv_list, name = 'FACETS calls')
toy_comut.add_categorical_data(variant_col_df, name = 'Mutation type')
toy_comut.add_categorical_data(clin_set_MSI, name = 'MSI Status')
toy_comut.add_categorical_data(clin_set_MiMSI, name = 'MiMSI Status')
toy_comut.add_categorical_data(assay, name = 'Assay')
toy_comut.add_bar_data(mutcounts, name = 'Mutation burden', stacked = True, ylabel = '# Mut')
#toy_comut.add_unified_legend()

toy_comut.add_axis_legend(name = 'Mutation burden', bbox_to_anchor = (1, 1), title = 'Mutation burden', ncol = 3)

toy_comut.plot_comut(figsize = (15,10))
#toy_comut.add_unified_legend()
toy_comut.figure.savefig('tutorial_comut.pdf', bbox_inches = 'tight', dpi = 300)

Error:

Traceback (most recent call last):
  File "Facets_plot.py", line 61, in <module>
    toy_comut.add_axis_legend(name = 'Mutation burden', bbox_to_anchor = (1, 1), title = 'Mutation burden', ncol = 3)
  File "/opt/anaconda2/envs/py36/lib/python3.6/site-packages/comut/comut.py", line 1612, in add_axis_legend
    axis = self.axes[name]
KeyError: 'Mutation burden'
jett-crowdis commented 4 years ago

Axis legends must be added after the plot is created. This is because the axes objects are only created when the comut is created with plot_comut. Try putting toy_comut.add_axis_legend after toy_comut.plot_comut(figsize = (15,10)).

erikagedvilaite commented 4 years ago

Thank you!

Another question - how do you order mutations based on their prevalence (ex. TP53 on top since it has most mutations) as seen in oncoplots/oncoprints

jett-crowdis commented 4 years ago

This isn't something that is natively built into CoMut (yet), but it isn't too hard to implement by the user. You can use pandas functions to extract the most frequent mutations and pass it to category_order. In your case, you're passing mutation data as variant_col_df, so you could use the following to define the order of mutations:

mut_order = variant_col_df.groupby('category').size().sort_values().index toy_comut.add_categorical_data(variant_col_df, name = 'Mutation type', category_order = mut_order)

Note that CoMut uses category_order to determine both which mutations should be shown and their order - this means unless you want CoMut to show ALL the mutations in variant_col_df (which may not be desirable if you parsed it from a MAF of all your mutations), you will need to subset mut_order. For example, if you want to show just the 10 most frequent mutations, you should use category_order = mut_order[:10].

If you want to show a subset of genes but order them by frequency, you can do something like the following:

interesting_genes = ['TP53', 'BRCA2', 'MYC', 'BRAF'...] # define interesting genes interesting_df = variant_col_df[variant_col_df['category'].isin(interesting_genes)] # subset df to genes mut_order = interesting_df.groupby('category').size().sort_values().index # determine order toy_comut.add_categorical_data(variant_col_df, name = 'Mutation type', category_order = mut_order)

Hope that helps!