Skip to content

Commit f33eb42

Browse files
authored
Merge pull request #20466 from pvanheus/add_auspicejson_datatype
[25.0] Add Auspice JSON datatype
2 parents 0aa5616 + 182f6c2 commit f33eb42

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

lib/galaxy/config/sample/datatypes_conf.xml.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
<datatype extension="vitessce.json" type="galaxy.datatypes.text:VitessceJson" mimetype="application/json" display_in_upload="True">
149149
<visualization plugin="vitessce" />
150150
</datatype>
151+
<datatype extension="auspice.json" type="galaxy.datatypes.text:AuspiceJson" mimetype="application/json" display_in_upload="True" />
151152
<datatype extension="data_manager_json" type="galaxy.datatypes.text:DataManagerJson" mimetype="application/json" subclass="true" display_in_upload="false"/>
152153
<datatype extension="dbn" type="galaxy.datatypes.sequence:DotBracket" display_in_upload="true" description="Dot-Bracket format is a text-based format for storing both an RNA sequence and its corresponding 2D structure." description_url="https://wiki.galaxyproject.org/Learn/Datatypes#Dbn"/>
153154
<datatype extension="fai" type="galaxy.datatypes.tabular:Tabular" display_in_upload="true" subclass="true" description="A Fasta Index File is a text file consisting of lines each with five TAB-delimited columns : Name, Length, offset, linebases, Linewidth" description_url="http://www.htslib.org/doc/faidx.html"/>
@@ -1387,6 +1388,7 @@
13871388
<sniffer type="galaxy.datatypes.text:CytoscapeJson"/>
13881389
<sniffer type="galaxy.datatypes.text:GeoJson"/>
13891390
<sniffer type="galaxy.datatypes.text:VitessceJson"/>
1391+
<snipper type="galaxy.datatypes.text:AuspiceJson"/>
13901392
<sniffer type="galaxy.datatypes.text:PithyaResult"/>
13911393
<sniffer type="galaxy.datatypes.text:BCSLts"/>
13921394
<sniffer type="galaxy.datatypes.text:Json"/>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": "v2",
3+
"meta": {
4+
"title": "Minimal AuspiceJSON",
5+
"updated": "2025-02-05",
6+
"panels": ["tree"]
7+
},
8+
"tree": {
9+
"name": "1",
10+
"node_attrs": {
11+
"div": 1
12+
}
13+
}
14+
}

lib/galaxy/datatypes/text.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,57 @@ def _looks_like_is_vitesscejson(self, file_prefix: FilePrefix, load_size: int =
695695
return False
696696

697697

698+
@build_sniff_from_prefix
699+
class AuspiceJson(Json):
700+
"""
701+
Auspice is a visualization tool for phylogenetic trees and associated data.
702+
It uses JSON format to represent the tree structure and metadata.
703+
"""
704+
705+
file_ext = "auspice.json"
706+
707+
def set_peek(self, dataset: DatasetProtocol, **kwd) -> None:
708+
super().set_peek(dataset)
709+
if not dataset.dataset.purged:
710+
dataset.blurb = "AuspiceJSON"
711+
712+
def sniff_prefix(self, file_prefix: FilePrefix) -> bool:
713+
"""
714+
Determines whether the file is in Auspice v2 JSON by looking for keys
715+
like "version", "meta" and "updated" that are both required by the
716+
https://docs.nextstrain.org/projects/auspice/en/stable/releases/v2.html format
717+
and also will be in the first part of the file
718+
719+
>>> from galaxy.datatypes.sniff import get_test_fname
720+
>>> fname = get_test_fname( '1.json' )
721+
>>> AuspiceJson().sniff( fname )
722+
False
723+
>>> fname = get_test_fname( '1.auspicejson' )
724+
>>> AuspiceJson().sniff( fname )
725+
True
726+
"""
727+
is_auspicejson = False
728+
if self._looks_like_json(file_prefix):
729+
is_auspicejson = self._looks_like_is_auspicejson(file_prefix)
730+
return is_auspicejson
731+
732+
def _looks_like_is_auspicejson(self, file_prefix: FilePrefix, load_size: int = 20000) -> bool:
733+
"""
734+
Expects JSON to start with { and 'meta', 'tree', 'updated' and 'nodes' to be present as keys in the JSON structure.
735+
"""
736+
try:
737+
with open(file_prefix.filename) as fh:
738+
segment_str = fh.read(load_size)
739+
740+
if segment_str.startswith("{") and all(
741+
x in segment_str for x in ["version", "meta", "updated", "panels"]
742+
):
743+
return True
744+
except Exception:
745+
pass
746+
return False
747+
748+
698749
@build_sniff_from_prefix
699750
class Obo(Text):
700751
"""

0 commit comments

Comments
 (0)