Skip to content

Commit

Permalink
Add VF tabular comparison views.
Browse files Browse the repository at this point in the history
  • Loading branch information
njohner committed Feb 16, 2024
1 parent afbb6e1 commit 737466b
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 19 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ to [Common Changelog](https://common-changelog.org)

### Added

- Add VF tabular comparison views. ([#58](https://github.com/metagenlab/zDB/pull/58)) (Niklaus Johner)
- Add VF Venn diagram view. ([#58](https://github.com/metagenlab/zDB/pull/58)) (Niklaus Johner)
- Add VF hit extraction view. ([#58](https://github.com/metagenlab/zDB/pull/58)) (Niklaus Johner)
- Add VF comparison index view and link it in the menu and home page. ([#58](https://github.com/metagenlab/zDB/pull/58)) (Niklaus Johner)
Expand Down
10 changes: 4 additions & 6 deletions webapp/chlamdb/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def get_genomes(self):
return PlotForm


def make_metabo_from(db, add_box=False, add_amr_choices=False):
def make_metabo_from(db, add_box=False, type_choices=None):

accession_choices, rev_index = get_accessions(db)

Expand All @@ -132,11 +132,9 @@ class MetaboForm(forms.Form):
input_box = forms.CharField(
widget=forms.Textarea(attrs={'cols': 10, 'rows': 10}))

if add_amr_choices:
if type_choices:
comp_type = forms.ChoiceField(
choices=(("gene", "Gene"),
("class", "Class"),
("subclass", "Subclass")),
choices=type_choices,
required=True
)

Expand All @@ -149,7 +147,7 @@ def __init__(self, *args, **kwargs):
rows = [Row('targets')]
if add_box:
rows.append(Row('input_box'))
if add_amr_choices:
if type_choices:
rows.append(Row('comp_type'))

self.helper.layout = Layout(
Expand Down
1 change: 1 addition & 0 deletions webapp/chlamdb/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


urlpatterns = [
re_path(r'^vf_comparison', tabular_comparison.VfComparisonView.as_view(), name="vf_comparison"), # noqa
re_path(r'^venn_vf/$', venn.VennVfView.as_view(), name="venn_vf"),
re_path(r'^venn_pfam/$', venn.VennPfamView.as_view(), name="venn_pfam"),
re_path(r'^venn_orthogroup/$', venn.VennOrthogroupView.as_view(), name="venn_orthogroup"), # noqa
Expand Down
22 changes: 22 additions & 0 deletions webapp/lib/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ def gen_where_clause(self, search_on, entries):
raise RuntimeError(f"Searching on {search_on} is not supported")
return where_clause

def get_hits(self, taxids):
where_clause = self.gen_where_clause("taxid", taxids)
columns = ["bioentry.taxon_id",
f"{self.hit_table}.{self.id_col}",
"evalue",
"prot_name",
"vfid",
"category",
]
query = (
f"SELECT {', '.join(columns)} "
f"FROM {self.hit_table} "
f"INNER JOIN {self.decription_table} ON {self.decription_table}.{self.id_col} = {self.hit_table}.{self.id_col} "
f"INNER JOIN sequence_hash_dictionnary AS hsh ON hsh.hsh = {self.hit_table}.hsh "
"INNER JOIN seqfeature ON hsh.seqid = seqfeature.seqfeature_id "
"INNER JOIN bioentry ON seqfeature.bioentry_id = bioentry.bioentry_id "
f"WHERE {where_clause} "
)
results = self.server.adaptor.execute_and_fetchall(query, taxids)
df = self.to_pandas_frame(results, [col.split(".")[-1] for col in columns])
return df

def get_hit_counts(self, ids, indexing="taxid", search_on="taxid",
keep_taxid=False, plasmids=None):
if indexing == "seqid":
Expand Down
4 changes: 4 additions & 0 deletions webapp/views/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ class VfViewMixin(BaseViewMixin):
def get_hit_counts(self):
return self.db.vf.get_hit_counts

@property
def get_hits(self):
return self.db.vf.get_hits

def get_hit_descriptions(self, ids, transformed=True):
descriptions = self.db.vf.get_hit_descriptions(ids)
descriptions = descriptions.set_index("vf_gene_id", drop=False)
Expand Down
74 changes: 62 additions & 12 deletions webapp/views/tabular_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.views import View

from views.mixins import (AmrViewMixin, CogViewMixin, KoViewMixin,
OrthogroupViewMixin, PfamViewMixin)
OrthogroupViewMixin, PfamViewMixin, VfViewMixin)
from views.utils import (format_cog, format_ko, format_lst_to_html,
format_orthogroup, my_locals)

Expand All @@ -13,6 +13,7 @@ class TabularComparisonViewBase(View):
template = 'chlamdb/tabular_comparison.html'
hist_colour_index_shift = 0
tab_name = "comp"
table_headers = None

def dispatch(self, request, *args, **kwargs):
self.comp_metabo_form = self.make_metabo_from()
Expand Down Expand Up @@ -111,6 +112,11 @@ def form_help(self):
def first_coloured_row(self):
return len(self.base_info_headers)

@property
def base_info_headers(self):
return [self.colname_to_header(colname)
for colname in self.base_info_accessors]


class PfamComparisonView(TabularComparisonViewBase, PfamViewMixin):

Expand All @@ -125,11 +131,6 @@ class PfamComparisonView(TabularComparisonViewBase, PfamViewMixin):
corresponding Pfam entry.
"""

@property
def base_info_headers(self):
return [self.colname_to_header(colname)
for colname in self.base_info_accessors]

def get_table_rows(self):
pfam_hits = self.get_hit_counts(ids=self.targets)
pfam_defs = self.get_hit_descriptions(pfam_hits.index.tolist(),
Expand Down Expand Up @@ -280,8 +281,12 @@ class AmrComparisonView(TabularComparisonViewBase, AmrViewMixin):
"plus" proteins are included with a less stringent criteria.<br>
"""

type_choices = (("gene", "Gene"),
("class", "Class"),
("subclass", "Subclass"))

def make_metabo_from(self):
return make_metabo_from(self.db, add_amr_choices=True)
return make_metabo_from(self.db, type_choices=self.type_choices)

@property
def base_info_accessors(self):
Expand All @@ -292,11 +297,6 @@ def base_info_accessors(self):
elif self.comp_type == "class":
return ["Class"]

@property
def base_info_headers(self):
return [self.colname_to_header(colname)
for colname in self.base_info_accessors]

@property
def table_help(self):
if self.comp_type == "gene":
Expand Down Expand Up @@ -326,3 +326,53 @@ def get_table_rows(self):
@property
def hist_colour_index_shift(self):
return len(self.targets)


class VfComparisonView(TabularComparisonViewBase, VfViewMixin):

_table_help = """
The ouput table contains the number of times a given {} appears
in the selected genomes, color coded according to the e-value
of the best hit for that genome.<br>
<br> Counts can be reordrered by clicking on column headers.<br>
"""

type_choices = (("vf_gene_id", "VF Gene"),
("vfid", "VF"),
("category", "VF Category"))

def make_metabo_from(self):
return make_metabo_from(self.db, type_choices=self.type_choices)

@property
def base_info_accessors(self):
if self.comp_type == "vf_gene_id":
return ["vf_gene_id", "prot_name", "vfid", "category"]
elif self.comp_type == "vfid":
return ["vfid", "category"]
elif self.comp_type == "category":
return ["category"]

@property
def table_help(self):
return self._table_help.format(self.comp_type)

def get_table_rows(self):
hits = self.get_hits(self.targets)
hits = hits.where(hits.notna(), "-")
table_rows = []
for groupid, data in hits.groupby(self.comp_type):
row = data.iloc[0][self.base_info_accessors].tolist()
taxonids = data["taxon_id"]
values = [len(taxonids[taxonids == target_id])
for target_id in self.targets]
colours = [data[taxonids == target_id]["evalue"].max() if value
else 0 for value, target_id in zip(values, self.targets)]
row.extend(values)
row.extend(colours)
table_rows.append(row)
return table_rows

@property
def hist_colour_index_shift(self):
return len(self.targets)
2 changes: 1 addition & 1 deletion webapp/views/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def safe_replace(string, search_string, replace_string):
'orthogroup_comparison', 'pan_genome_orthogroup', 'plot_heatmap_orthogroup',
'venn_orthogroup'],
'Comparisons: Virulence Factors': [
'entry_list_vf', 'index_comp_vf', 'extract_vf', 'venn_vf'],
'entry_list_vf', 'index_comp_vf', 'extract_vf', 'venn_vf', 'vf_comparison'],
'Genome alignments: Circos plot': ['circos'],
'Genome alignments: Plot region': ['plot_region'],
'Genomes: table of contents': ['extract_contigs', 'genomes_intro'],
Expand Down

0 comments on commit 737466b

Please # to comment.