Skip to content

Commit

Permalink
add default session + general options
Browse files Browse the repository at this point in the history
  • Loading branch information
abretaud authored and fubar2 committed Jan 9, 2024
1 parent d4b4894 commit df7cb8f
Show file tree
Hide file tree
Showing 3 changed files with 463 additions and 15 deletions.
157 changes: 154 additions & 3 deletions tools/jbrowse2/jbrowse2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import logging
import os
import re
import shutil
import struct
import subprocess
Expand Down Expand Up @@ -336,7 +337,7 @@ def __init__(self, jbrowse, outdir, genomes):
self.tracksToIndex = []

# This is the id of the current assembly
self.assembly_ids = []
self.assembly_ids = {}
self.current_assembly_id = []

# If upgrading, look at the existing data
Expand Down Expand Up @@ -391,7 +392,7 @@ def check_existing(self, destination):
if 'assemblies' in conf:
for assembly in conf['assemblies']:
if 'name' in assembly:
self.assembly_ids.append(assembly['name'])
self.assembly_ids[assembly['name']] = None

def process_genomes(self):
for genome_node in self.genome_paths:
Expand Down Expand Up @@ -419,7 +420,13 @@ def add_assembly(self, path, label, default=True):
while uniq_label in self.assembly_ids:
uniq_label = label + str(lab_try)
lab_try += 1
self.assembly_ids.append(uniq_label)

# Find a default scaffold to display
# TODO this may not be necessary in the future, see https://github.com/GMOD/jbrowse-components/issues/2708
with open(path, 'r') as fa_handle:
fa_header = fa_handle.readline()[1:].strip().split(' ')[0]

self.assembly_ids[uniq_label] = fa_header
if default:
self.current_assembly_id = uniq_label

Expand Down Expand Up @@ -818,6 +825,125 @@ def process_annotations(self, track):
else:
log.warn('Do not know how to handle %s', dataset_ext)

# Return non-human label for use in other fields
yield outputTrackConfig['label']

def add_default_session(self, data):
"""
Add some default session settings: set some assemblies/tracks on/off
"""
tracks_data = []

# TODO using the default session for now, but check out session specs in the future https://github.com/GMOD/jbrowse-components/issues/2708

# We need to know the track type from the config.json generated just before
config_path = os.path.join(self.outdir, 'data', 'config.json')
track_types = {}
with open(config_path, 'r') as config_file:
config_json = json.load(config_file)

for track_conf in config_json['tracks']:
track_types[track_conf['trackId']] = track_conf['type']

# TODO Getting an error when refreshing the page, waiting for https://github.com/GMOD/jbrowse-components/issues/2708
for on_track in data['visibility']['default_on']:
tracks_data.append({
"type": track_types[on_track],
"configuration": on_track,
"displays": [
{
"type": "LinearBasicDisplay",
"height": 100
}
]
})

# The view for the assembly we're adding
view_json = {
"type": "LinearGenomeView",
"tracks": tracks_data
}

refName = None
if data.get('defaultLocation', ''):
loc_match = re.search(r'^(\w+):(\d+)\.+(\d+)$', data['defaultLocation'])
if loc_match:
refName = loc_match.group(1)
start = int(loc_match.group(2))
end = int(loc_match.group(3))
elif self.assembly_ids[self.current_assembly_id] is not None:
refName = self.assembly_ids[self.current_assembly_id]
start = 0
end = 10000 # Booh, hard coded! waiting for https://github.com/GMOD/jbrowse-components/issues/2708

if refName is not None:
view_json['displayedRegions'] = [{
"refName": refName,
"start": start,
"end": end,
"reversed": False,
"assemblyName": self.current_assembly_id
}]

session_name = data.get('session_name', "New session")
if not session_name:
session_name = "New session"

# Merge with possibly existing defaultSession (if upgrading a jbrowse instance)
session_json = {}
if 'defaultSession' in config_json:
session_json = config_json['defaultSession']

session_json["name"] = session_name

if 'views' not in session_json:
session_json['views'] = []

session_json['views'].append(view_json)

config_json['defaultSession'] = session_json

with open(config_path, 'w') as config_file:
json.dump(config_json, config_file, indent=2)

def add_general_configuration(self, data):
"""
Add some general configuration to the config.json file
"""

config_path = os.path.join(self.outdir, 'data', 'config.json')
with open(config_path, 'r') as config_file:
config_json = json.load(config_file)

config_data = {}

config_data['disableAnalytics'] = data.get('analytics', 'false') == 'true'

config_data['theme'] = {
"palette": {
"primary": {
"main": data.get('primary_color', '#0D233F')
},
"secondary": {
"main": data.get('secondary_color', '#721E63')
},
"tertiary": {
"main": data.get('tertiary_color', '#135560')
},
"quaternary": {
"main": data.get('quaternary_color', '#FFB11D')
},
},
"typography": {
"fontSize": int(data.get('font_size', 10))
},
}

config_json['configuration'].update(config_data)

with open(config_path, 'w') as config_file:
json.dump(config_json, config_file, indent=2)

def clone_jbrowse(self, jbrowse_dir, destination):
"""Clone a JBrowse directory into a destination directory.
"""
Expand Down Expand Up @@ -880,6 +1006,14 @@ def copytree(src, dst, symlinks=False, ignore=None):
]
)

default_session_data = {
'visibility': {
'default_on': [],
'default_off': [],
},
}

# TODO add metadata to tracks
for track in root.findall('tracks/track'):
track_conf = {}
track_conf['trackfiles'] = []
Expand Down Expand Up @@ -916,4 +1050,21 @@ def copytree(src, dst, symlinks=False, ignore=None):
track_conf['conf'] = etree_to_dict(track.find('options'))
keys = jc.process_annotations(track_conf)

for key in keys:
default_session_data['visibility'][track.attrib.get('visibility', 'default_off')].append(key)

default_session_data['defaultLocation'] = root.find('metadata/general/defaultLocation').text
default_session_data['session_name'] = root.find('metadata/general/session_name').text

general_data = {
'analytics': root.find('metadata/general/analytics').text,
'primary_color': root.find('metadata/general/primary_color').text,
'secondary_color': root.find('metadata/general/secondary_color').text,
'tertiary_color': root.find('metadata/general/tertiary_color').text,
'quaternary_color': root.find('metadata/general/quaternary_color').text,
'font_size': root.find('metadata/general/font_size').text,
}

jc.add_default_session(default_session_data)
jc.add_general_configuration(general_data)
jc.text_index()
30 changes: 18 additions & 12 deletions tools/jbrowse2/jbrowse2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,20 @@ cp $output.files_path/index.html $output;
</genomes>
<general>
<defaultLocation>${jbgen.defaultLocation}</defaultLocation>
<trackPadding>${jbgen.trackPadding}</trackPadding>
<shareLink>${jbgen.shareLink}</shareLink>
<aboutDescription>${jbgen.aboutDescription}</aboutDescription>
<show_tracklist>${jbgen.show_tracklist}</show_tracklist>
<show_nav>${jbgen.show_nav}</show_nav>
<show_overview>${jbgen.show_overview}</show_overview>
<show_menu>${jbgen.show_menu}</show_menu>
<hideGenomeOptions>${jbgen.hideGenomeOptions}</hideGenomeOptions>
<analytics>${jbgen.enableAnalytics}</analytics>
<primary_color>${jbgen.primary_color}</primary_color>
<secondary_color>${jbgen.secondary_color}</secondary_color>
<tertiary_color>${jbgen.tertiary_color}</tertiary_color>
<quaternary_color>${jbgen.quaternary_color}</quaternary_color>
<font_size>${jbgen.font_size}</font_size>
<session_name>${jbgen.session_name}</session_name>
</general>
<galaxyUrl>${__app__.config.galaxy_infrastructure_url}</galaxyUrl>
</metadata>
<tracks>
#for $tg in $track_groups:
#for $track in $tg.data_tracks:
<track cat="${tg.category}" format="${track.data_format.data_format_select}">
<track cat="${tg.category}" format="${track.data_format.data_format_select}" visibility="${track.data_format.track_visibility}">
#if $track.data_format.data_format_select != "sparql":
<files>
#for $dataset in $track.data_format.annotation:
Expand Down Expand Up @@ -356,11 +354,10 @@ cp $output.files_path/index.html $output;
type="data" />
</when>
</conditional>
<!-- TODO auto add GC track when implemented https://github.com/GMOD/jbrowse-components/pull/2683-->

<!-- TODO genetic code not yet supported in jbrowse2 https://github.com/GMOD/jbrowse-components/issues/1765 -->

<!-- TODO show linear view by default? -->

<conditional name="action">
<param type="select" label="JBrowse-in-Galaxy Action" name="action_select">
<option value="create">New JBrowse Instance</option>
Expand Down Expand Up @@ -425,10 +422,12 @@ cp $output.files_path/index.html $output;
token_scaling_log_select="true" />
<expand macro="track_custom_config" />
<expand macro="track_menu" />
<expand macro="track_display" />
</when>
<when value="vcf">
<expand macro="input_conditional" label="SNP Track Data" format="vcf,vcf_bgzip" />
<expand macro="track_custom_config" />
<expand macro="track_display" />
</when>
<when value="gene_calls">
<expand macro="input_conditional" label="GFF/GFF3/BED Track Data" format="gff,gff3,bed" />
Expand Down Expand Up @@ -481,6 +480,7 @@ cp $output.files_path/index.html $output;
<expand macro="color_selection" />
<expand macro="track_custom_config" />
<expand macro="track_menu" />
<expand macro="track_display" />
</when>
<when value="pileup">
<expand macro="input_conditional" label="BAM Track Data" format="bam" />
Expand All @@ -496,6 +496,7 @@ cp $output.files_path/index.html $output;
help="Maximum size in bytes of BAM chunks that the browser will try to deal with. When this is exceeded, most tracks will display 'Too much data' message."
value="5000000" />
<expand macro="track_custom_config" />
<expand macro="track_display" />
</when>
<when value="cram">
<expand macro="input_conditional" label="CRAM Track Data" format="cram" />
Expand All @@ -511,6 +512,7 @@ cp $output.files_path/index.html $output;
help="Maximum size in bytes of BAM chunks that the browser will try to deal with. When this is exceeded, most tracks will display 'Too much data' message."
value="5000000" />
<expand macro="track_custom_config" />
<expand macro="track_display" />
</when>
<when value="wiggle">
<expand macro="input_conditional" label="BigWig Track Data" format="bigwig" />
Expand Down Expand Up @@ -550,6 +552,7 @@ cp $output.files_path/index.html $output;

<expand macro="color_selection_minmax" />
<expand macro="track_custom_config" />
<expand macro="track_display" />
</when>

<when value="paf">
Expand All @@ -558,11 +561,13 @@ cp $output.files_path/index.html $output;
name="synteny_genome"
type="data" />
<expand macro="input_conditional" label="Synteny data" format="paf" />
<expand macro="track_display" />
</when>

<when value="hic">
<!-- TODO no hic datatype by default, but input for hicConvertFormat? hic_matrix datatype on .eu -->
<expand macro="input_conditional" label="HiC data" format="hic" />
<expand macro="track_display" />
</when>

<when value="sparql">
Expand Down Expand Up @@ -612,6 +617,7 @@ cp $output.files_path/index.html $output;
</valid>
</sanitizer>
</param>
<expand macro="track_display" />
</when>
</conditional>
</repeat>
Expand Down
Loading

0 comments on commit df7cb8f

Please sign in to comment.