Skip to content

Commit a237237

Browse files
committed
Add Auspice JSON datatype
1 parent df51b26 commit a237237

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-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="vitesscejson" type="galaxy.datatypes.text:VitessceJson" mimetype="application/json" display_in_upload="True">
149149
<visualization plugin="vitessce" />
150150
</datatype>
151+
<dataatype extension="auspicejson" 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"/>
@@ -1364,6 +1365,7 @@
13641365
<sniffer type="galaxy.datatypes.text:CytoscapeJson"/>
13651366
<sniffer type="galaxy.datatypes.text:GeoJson"/>
13661367
<sniffer type="galaxy.datatypes.text:VitessceJson"/>
1368+
<snipper type="galaxy.datatypes.text:AuspiceJson"/>
13671369
<sniffer type="galaxy.datatypes.text:PithyaResult"/>
13681370
<sniffer type="galaxy.datatypes.text:BCSLts"/>
13691371
<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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,55 @@ def _looks_like_is_vitesscejson(self, file_prefix: FilePrefix, load_size: int =
700700
return False
701701

702702

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

0 commit comments

Comments
 (0)