Skip to content

Commit

Permalink
Modularize
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Dec 11, 2023
1 parent 68a3d57 commit 49e25fb
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 140 deletions.
3 changes: 2 additions & 1 deletion jbrowse_jupyter/dev_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def send_head(self):
last = file_len - 1
response_length = last - first + 1

self.send_header("Content-Range", "bytes %s-%s/%s" % (first, last, file_len))
self.send_header("Content-Range", "bytes %s-%s/%s" %
(first, last, file_len))
self.send_header("Content-Length", str(response_length))
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
self.end_headers()
Expand Down
33 changes: 22 additions & 11 deletions jbrowse_jupyter/jbrowse_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ def __init__(self, view="LGV", conf=None):
"tracks": [],
}
if view != "LGV" and view == "CGV":
view_default = {"id": "circularView", "type": "CircularView", "tracks": []}
view_default = {"id": "circularView",
"type": "CircularView", "tracks": []}
default = {
"assembly": {},
"tracks": [],
Expand Down Expand Up @@ -237,7 +238,8 @@ def set_assembly(self, assembly_data, **kwargs):
"data. You can check out our local "
"file support docs for more information"
)
assembly_adapter = guess_adapter_type(assembly_data, "uri", indx, **kwargs)
assembly_adapter = guess_adapter_type(
assembly_data, "uri", indx, **kwargs)
name = kwargs.get("name", get_name(assembly_data))
if assembly_adapter["type"] == "UNKNOWN":
raise TypeError("Adapter type is not recognized")
Expand Down Expand Up @@ -335,7 +337,8 @@ def get_track_display(self, track):

def get_track(self, track_name):
# Return the list of track configurations with that name
tracks = [track for track in self.get_tracks() if track["name"] == track_name]
tracks = [track for track in self.get_tracks() if track["name"]
== track_name]
return tracks

def get_tracks(self):
Expand Down Expand Up @@ -398,7 +401,8 @@ def add_df_track(self, track_data, name, **kwargs):
raise TypeError(err)
if track_id in self.tracks_ids_map.keys() and overwrite:
# delete track and overwrite it
current_tracks = [t for t in current_tracks if t["trackId"] != track_id]
current_tracks = [
t for t in current_tracks if t["trackId"] != track_id]

current_tracks.append(df_track_config)
self.config["tracks"] = current_tracks
Expand Down Expand Up @@ -472,7 +476,8 @@ def add_track(self, data, **kwargs):
if adapter["type"] == "CramAdapter":
extra_config = self.get_assembly()["sequence"]["adapter"]
adapter["sequenceAdapter"] = extra_config
t_type = kwargs.get("track_type", guess_track_type(adapter["type"]))
t_type = kwargs.get(
"track_type", guess_track_type(adapter["type"]))
supported_track_types = set(
{
"AlignmentsTrack",
Expand Down Expand Up @@ -501,7 +506,8 @@ def add_track(self, data, **kwargs):
)
)
if track_id in self.tracks_ids_map.keys() and overwrite:
current_tracks = [t for t in current_tracks if t["trackId"] != track_id]
current_tracks = [
t for t in current_tracks if t["trackId"] != track_id]

current_tracks.append(track_config)
self.config["tracks"] = current_tracks
Expand Down Expand Up @@ -543,7 +549,8 @@ def add_track(self, data, **kwargs):
if adapter["type"] == "CramAdapter":
extra_config = self.get_assembly()["sequence"]["adapter"]
adapter["sequenceAdapter"] = extra_config
t_type = kwargs.get("track_type", guess_track_type(adapter["type"]))
t_type = kwargs.get(
"track_type", guess_track_type(adapter["type"]))
supported_track_types = set(
{
"AlignmentsTrack",
Expand Down Expand Up @@ -572,7 +579,8 @@ def add_track(self, data, **kwargs):
)
)
if track_id in self.tracks_ids_map.keys() and overwrite:
current_tracks = [t for t in current_tracks if t["trackId"] != track_id]
current_tracks = [
t for t in current_tracks if t["trackId"] != track_id]

current_tracks.append(track_config)
self.config["tracks"] = current_tracks
Expand All @@ -598,12 +606,14 @@ def delete_track(self, track_id):
(f'track with trackId: "{track_id}" does not exist in' f"config.")
)
else:
new_tracks = [t for t in current_tracks if t["trackId"] != track_id]
new_tracks = [
t for t in current_tracks if t["trackId"] != track_id]
self.config["tracks"] = new_tracks
# clear from default session
default_sess = self.get_default_session()
tracks_sess = default_sess["view"]["tracks"]
new_tracks_sess = [t for t in tracks_sess if t["configuration"] != track_id]
new_tracks_sess = [
t for t in tracks_sess if t["configuration"] != track_id]
if self.view == "CGV":
self.config["defaultSession"] = {
"name": "my session",
Expand Down Expand Up @@ -660,7 +670,8 @@ def set_default_session(self, tracks_ids, display_assembly=True):
if display_assembly:
reference_track = self.get_reference_track()
tracks_configs.append(reference_track)
tracks_to_display = [t for t in self.get_tracks() if t["trackId"] in tracks_ids]
tracks_to_display = [
t for t in self.get_tracks() if t["trackId"] in tracks_ids]
# guess the display type
for t in tracks_to_display:
tracks_configs.append(self.get_track_display(t))
Expand Down
187 changes: 63 additions & 124 deletions jbrowse_jupyter/util.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
import re
import copy
import os

import dash_jbrowse as jb
from dash import html, Dash
from urllib.parse import urlparse


hg38_lgv = {
"assembly": {
"name": "GRCh38",
"sequence": {
"type": "ReferenceSequenceTrack",
"trackId": "GRCh38-ReferenceSequenceTrack",
"adapter": {
"type": "BgzipFastaAdapter",
"fastaLocation": {
"uri": "https://s3.amazonaws.com/jbrowse.org/genomes/GRCh38/fasta/GRCh38.fa.gz"
},
"faiLocation": {
"uri": "https://s3.amazonaws.com/jbrowse.org/genomes/GRCh38/fasta/GRCh38.fa.gz.fai"
},
"gziLocation": {
"uri": "https://s3.amazonaws.com/jbrowse.org/genomes/GRCh38/fasta/GRCh38.fa.gz.gzi"
},
hg38_assembly = {
"name": "GRCh38",
"sequence": {
"type": "ReferenceSequenceTrack",
"trackId": "GRCh38-ReferenceSequenceTrack",
"adapter": {
"type": "BgzipFastaAdapter",
"fastaLocation": {
"uri": "https://s3.amazonaws.com/jbrowse.org/genomes/GRCh38/fasta/GRCh38.fa.gz"
},
"faiLocation": {
"uri": "https://s3.amazonaws.com/jbrowse.org/genomes/GRCh38/fasta/GRCh38.fa.gz.fai"
},
"gziLocation": {
"uri": "https://s3.amazonaws.com/jbrowse.org/genomes/GRCh38/fasta/GRCh38.fa.gz.gzi"
},
},
"aliases": ["hg38"],
"refNameAliases": {
"adapter": {
"type": "RefNameAliasAdapter",
"location": {
"uri": "https://s3.amazonaws.com/jbrowse.org/genomes/GRCh38/hg38_aliases.txt"
},
}
},
},
"aliases": ["hg38"],
"refNameAliases": {
"adapter": {
"type": "RefNameAliasAdapter",
"location": {
"uri": "https://s3.amazonaws.com/jbrowse.org/genomes/GRCh38/hg38_aliases.txt"
},
}
},
}
hg38_lgv = {
"assembly": hg38_assembly,
"tracks": [],
"location": "10:29,838,737..29,838,819",
"defaultSession": {
Expand All @@ -49,38 +50,7 @@


hg38_cgv = {
"assembly": {
"name": "hg38",
"sequence": {
"type": "ReferenceSequenceTrack",
"trackId": "GRCh38-ReferenceSequenceTrack",
"adapter": {
"type": "BgzipFastaAdapter",
"fastaLocation": {
"uri": "https://jbrowse.org/genomes/GRCh38/fasta/hg38.prefix.fa.gz",
"locationType": "UriLocation",
},
"faiLocation": {
"uri": "https://jbrowse.org/genomes/GRCh38/fasta/hg38.prefix.fa.gz.fai",
"locationType": "UriLocation",
},
"gziLocation": {
"uri": "https://jbrowse.org/genomes/GRCh38/fasta/hg38.prefix.fa.gz.gzi",
"locationType": "UriLocation",
},
},
},
"aliases": ["GRCh38"],
"refNameAliases": {
"adapter": {
"type": "RefNameAliasAdapter",
"location": {
"uri": "https://s3.amazonaws.com/jbrowse.org/genomes/GRCh38/hg38_aliases.txt",
"locationType": "UriLocation",
},
}
},
},
"assembly": hg38_assembly,
"tracks": [],
"defaultSession": {
"name": "My session",
Expand All @@ -92,36 +62,36 @@
},
},
}

hg19_lgv = {
"assembly": {
"name": "hg19",
"aliases": ["GRCh37"],
"sequence": {
"type": "ReferenceSequenceTrack",
"trackId": "hg19-ReferenceSequenceTrack",
"adapter": {
"type": "BgzipFastaAdapter",
"fastaLocation": {
"uri": "https://jbrowse.org/genomes/hg19/fasta/hg19.fa.gz"
},
"faiLocation": {
"uri": "https://jbrowse.org/genomes/hg19/fasta/hg19.fa.gz.fai"
},
"gziLocation": {
"uri": "https://jbrowse.org/genomes/hg19/fasta/hg19.fa.gz.gzi"
},
hg19_assembly = {
"name": "hg19",
"aliases": ["GRCh37"],
"sequence": {
"type": "ReferenceSequenceTrack",
"trackId": "hg19-ReferenceSequenceTrack",
"adapter": {
"type": "BgzipFastaAdapter",
"fastaLocation": {
"uri": "https://jbrowse.org/genomes/hg19/fasta/hg19.fa.gz"
},
"faiLocation": {
"uri": "https://jbrowse.org/genomes/hg19/fasta/hg19.fa.gz.fai"
},
"gziLocation": {
"uri": "https://jbrowse.org/genomes/hg19/fasta/hg19.fa.gz.gzi"
},
},
"refNameAliases": {
"adapter": {
"type": "RefNameAliasAdapter",
"location": {
"uri": "https://s3.amazonaws.com/jbrowse.org/genomes/hg19/hg19_aliases.txt"
},
}
},
},
"refNameAliases": {
"adapter": {
"type": "RefNameAliasAdapter",
"location": {
"uri": "https://s3.amazonaws.com/jbrowse.org/genomes/hg19/hg19_aliases.txt"
},
}
},
}
hg19_lgv = {
"assembly": hg19_assembly,
"tracks": [],
"defaultSession": {
"name": "test",
Expand All @@ -135,38 +105,7 @@
}

hg19_cgv = {
"assembly": {
"name": "hg19",
"aliases": ["GRCh37"],
"sequence": {
"type": "ReferenceSequenceTrack",
"trackId": "Pd8Wh30ei9R",
"adapter": {
"type": "BgzipFastaAdapter",
"fastaLocation": {
"uri": "https://jbrowse.org/genomes/hg19/fasta/hg19.fa.gz",
"locationType": "UriLocation",
},
"faiLocation": {
"uri": "https://jbrowse.org/genomes/hg19/fasta/hg19.fa.gz.fai",
"locationType": "UriLocation",
},
"gziLocation": {
"uri": "https://jbrowse.org/genomes/hg19/fasta/hg19.fa.gz.gzi",
"locationType": "UriLocation",
},
},
},
"refNameAliases": {
"adapter": {
"type": "RefNameAliasAdapter",
"location": {
"uri": "https://s3.amazonaws.com/jbrowse.org/genomes/hg19/hg19_aliases.txt",
"locationType": "UriLocation",
},
}
},
},
"assembly": hg19_assembly,
"tracks": [],
"defaultSession": {
"name": "My session",
Expand Down Expand Up @@ -210,11 +149,11 @@ def get_name(assembly_file):
name_end = 0
name_start = 0
for i in range(0, len(assembly_file)):
if assembly_file[len(assembly_file) - i - 1 : len(assembly_file) - i] == "/":
if assembly_file[len(assembly_file) - i - 1: len(assembly_file) - i] == "/":
name_start = len(assembly_file) - i
break
for i in range(name_start, len(assembly_file)):
if assembly_file[i : i + 1] == ".":
if assembly_file[i: i + 1] == ".":
name_end = i
break

Expand All @@ -230,14 +169,14 @@ def get_default(name, view_type="LGV"):
"""Returns the configuration object given a genome name."""
if name == "hg38":
if view_type == "CGV":
return hg38_cgv
return copy.deepcopy(hg38_cgv)
else:
return hg38_lgv
return copy.deepcopy(hg38_lgv)
elif name == "hg19":
if view_type == "CGV":
return hg19_cgv
return copy.deepcopy(hg19_cgv)
else:
return hg19_lgv
return copy.deepcopy(hg19_lgv)


def create_component(conf, **kwargs):
Expand Down
3 changes: 0 additions & 3 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@

# Same as Black.
line-length = 88
indent-width = 4
2 changes: 1 addition & 1 deletion tests/test_tracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"http://hgdownload.cse.ucsc.edu/goldenpath/hg38/phyloP100way/hg38.phyloP100way.bw"
)
vcf = "https://ftp.ncbi.nlm.nih.gov/pub/clinvar/vcf_GRCh37/clinvar.vcf"
vcfGz = "https://ftp.ncbi.nlm.nih.gov/pub/clinvar/vcf_GRCh37/clinvar.vcf.tbi"
vcfGz = "https://ftp.ncbi.nlm.nih.gov/pub/clinvar/vcf_GRCh37/clinvar.vcf.gz"


def test_make_location():
Expand Down

0 comments on commit 49e25fb

Please sign in to comment.