Skip to content

Commit

Permalink
updates data model, importer, api, filters for first release of viral…
Browse files Browse the repository at this point in the history
… catalogues
  • Loading branch information
SandyRogers committed Jan 17, 2024
1 parent 56c34d0 commit 544c255
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 50 deletions.
7 changes: 2 additions & 5 deletions holofood/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ class Config:

class ViralFragmentSchema(ModelSchema):
cluster_representative: Optional["ViralFragmentSchema"]
host_mag: Optional[GenomeSchema]

@staticmethod
def resolve_contig_url(obj: ViralFragment):
Expand Down Expand Up @@ -238,8 +237,8 @@ class Config:
"start_within_contig",
"end_within_contig",
"metadata",
"host_mag",
"viral_type",
"taxonomy",
]


Expand Down Expand Up @@ -483,9 +482,7 @@ def get_viral_catalogue(request, catalogue_id: str):
"found in the assembly contigs of HoloFood samples."
"The Catalogue’s viral fragments are all from the same biome."
"Viral sequences are clustered by sequence identity, at a species-level."
"Both cluster representatives and cluster members are included."
"Where a viral sequence is found in a related MAG (metagenome assembly genome,"
" e.g. a bacterial species), this MAG is considered a “host MAG”.",
"Both cluster representatives and cluster members are included.",
tags=[VIRUSES],
)
def list_viral_catalogue_fragments(request, catalogue_id: str):
Expand Down
3 changes: 1 addition & 2 deletions holofood/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ class Meta:
"id": ["icontains"],
"contig_id": ["icontains"],
"viral_type": ["exact"],
"host_mag__taxonomy": ["icontains"],
"host_mag__accession": ["icontains"],
"taxonomy": ["icontains"],
}

def cluster_representative_status(self, queryset, name, value):
Expand Down
17 changes: 8 additions & 9 deletions holofood/management/commands/import_viral_catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ def handle(self, *args, **options):
"sequence_end": "end_within_contig",
"cluster_representative": "cluster_representative_id",
"mgya": "mgnify_analysis_accession",
"host_mgyg": "host_mag_id",
"viral_type": "viral_type",
"contig": "contig_id",
"viral_taxonomy": "taxonomy",
Expand Down Expand Up @@ -130,14 +129,14 @@ def handle(self, *args, **options):
for col_name, field_name in column_mapping.items()
if sequence[col_name] != ""
}

vir_data["taxonomy"] = (
vir_data["taxonomy"]
.replace(";", " > ")
.strip()
.lstrip(" >")
.rstrip(" >")
)
if "taxonomy" in vir_data:
vir_data["taxonomy"] = (
vir_data["taxonomy"]
.replace(";", " > ")
.strip()
.lstrip(" >")
.rstrip(" >")
)

contig_id = sequence["contig"]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.2 on 2024-01-17 12:57

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("holofood", "0033_remove_sample_ena_run_accessions_and_more"),
]

operations = [
migrations.AlterModelOptions(
name="animal",
options={"ordering": ("accession",)},
),
migrations.AlterModelOptions(
name="viralfragment",
options={"ordering": ("id",)},
),
migrations.RemoveField(
model_name="viralfragment",
name="host_mag",
),
]
23 changes: 23 additions & 0 deletions holofood/migrations/0035_alter_viralfragment_viral_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2 on 2024-01-17 15:12

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("holofood", "0034_alter_animal_options_alter_viralfragment_options_and_more"),
]

operations = [
migrations.AlterField(
model_name="viralfragment",
name="viral_type",
field=models.CharField(
choices=[
("viral_sequence", "viral_sequence"),
("prophage", "prophage"),
],
max_length=15,
),
),
]
14 changes: 3 additions & 11 deletions holofood/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,8 @@ class ViralFragment(models.Model):
objects = ViralFragmentClusterManager()

PROPHAGE = "prophage"
PHAGE = "phage"
VIRAL_TYPE_CHOICES = [(PHAGE, PHAGE), (PROPHAGE, PROPHAGE)]
VIRAL_SEQUENCE = "viral_sequence"
VIRAL_TYPE_CHOICES = [(VIRAL_SEQUENCE, VIRAL_SEQUENCE), (PROPHAGE, PROPHAGE)]

id = models.CharField(primary_key=True, max_length=100, verbose_name="ID")
catalogue = models.ForeignKey(
Expand All @@ -510,15 +510,7 @@ class ViralFragment(models.Model):
start_within_contig = models.IntegerField()
end_within_contig = models.IntegerField()
metadata = models.JSONField(default=dict, blank=True)
host_mag = models.ForeignKey(
Genome,
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name="viral_fragments",
verbose_name="Host MAG",
)
viral_type = models.CharField(choices=VIRAL_TYPE_CHOICES, max_length=10)
viral_type = models.CharField(choices=VIRAL_TYPE_CHOICES, max_length=15)
taxonomy = models.CharField(null=True, blank=True, max_length=100)

gff = models.TextField(blank=True, default="")
Expand Down
4 changes: 3 additions & 1 deletion holofood/templatetags/taxonomy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

@register.inclusion_tag("holofood/components/atoms/taxonomy_tooltip.html")
def taxonomy_tooltip(taxonomy: str, separator: str = ">", default: str = "—") -> dict:
if taxonomy is None or separator not in taxonomy:
if not taxonomy:
return {"short_tax": default, "long_tax": None}
if separator not in taxonomy:
return {"short_tax": taxonomy, "long_tax": None}
return {"short_tax": taxonomy.split(separator)[-1].strip(), "long_tax": taxonomy}
6 changes: 3 additions & 3 deletions holofood/tests/static_fixtures/viral_catalogue/viral_cat.tsv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
sequence_id contig viral_type viral_taxonomy host_mgyg sequence_start sequence_end cluster_representative mgya
MGYC001-start-256-end-512 MGYC001 prophage Caudovirales MGYG999 256 512 MGYA001
MGYC003-start-512-end-768 MGYC003 prophage Caudovirales MGYG999 512 768 MGYC001-start-256-end-512 MGYA002
sequence_id contig viral_type viral_taxonomy sequence_start sequence_end cluster_representative mgya
MGYC001-start-256-end-512 MGYC001 prophage Caudovirales 256 512 MGYA001
MGYC003-start-512-end-768 MGYC003 prophage Caudovirales 512 768 MGYC001-start-256-end-512 MGYA002
19 changes: 0 additions & 19 deletions templates/holofood/pages/viral_catalogue_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ <h2>{{ catalogue.title }} viral catalogue</h2>
<th class="vf-table__heading" scope="col">Parent contig</th>
<th class="vf-table__heading" scope="col">Type</th>
<th class="vf-table__heading" scope="col">Taxonomy</th>
<th class="vf-table__heading" scope="col">Host MAG</th>
<th class="vf-table__heading" scope="col">Host taxonomy</th>
<th class="vf-table__heading" scope="col">View</th>
</tr>
</thead>
Expand All @@ -141,23 +139,6 @@ <h2>{{ catalogue.title }} viral catalogue</h2>
{{ viral_fragment.viral_type }}
</td>
<td class="vf-table__cell">{% taxonomy_tooltip taxonomy=viral_fragment.taxonomy %}</td>
<td class="vf-table__cell">
{% if viral_fragment.host_mag %}
<a class="vf-button vf-button--link vf-button--sm inline"
href="{% url 'genome_catalogue' pk=catalogue.related_genome_catalogue.id %}?accession__icontains={{ viral_fragment.host_mag.accession }}">
{{ viral_fragment.host_mag.accession }}
</a>
{% else %}
{% endif %}
</td>
<td class="vf-table__cell">
{% if viral_fragment.host_mag %}
{% taxonomy_tooltip taxonomy=viral_fragment.host_mag.taxonomy %}
{% else %}
{% endif %}
</td>
<td class="vf-table__cell">
{% if viral_fragment.id == selected_viral_fragment.id %}
<b>Viewing</b>
Expand Down

0 comments on commit 544c255

Please sign in to comment.