Skip to content

Commit

Permalink
Merge pull request #26 from moshi4/develop
Browse files Browse the repository at this point in the history
Bump to v0.4.4
  • Loading branch information
moshi4 authored Oct 28, 2023
2 parents 1c3560d + 9266d87 commit 9f5d6f2
Show file tree
Hide file tree
Showing 9 changed files with 822 additions and 699 deletions.
1 change: 1 addition & 0 deletions docs/api-docs/track.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
members:
- add_feature
- add_exon_feature
- add_features
- add_genbank_features
- add_gff_features
- add_subtrack
Expand Down
4 changes: 2 additions & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ markdown_extensions:
- pymdownx.highlight:
anchor_linenums: true
- pymdownx.emoji:
emoji_index: !!python/name:materialx.emoji.twemoji
emoji_generator: !!python/name:materialx.emoji.to_svg
emoji_index: !!python/name:material.extensions.emoji.twemoji
emoji_generator: !!python/name:material.extensions.emoji.to_svg
- pymdownx.inlinehilite
- pymdownx.snippets
- pymdownx.superfences
Expand Down
1,380 changes: 694 additions & 686 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pygenomeviz"
version = "0.4.3"
version = "0.4.4"
description = "A genome visualization python package for comparative genomics"
authors = ["moshi4"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion src/pygenomeviz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

warnings.filterwarnings("ignore")

__version__ = "0.4.3"
__version__ = "0.4.4"

__all__ = [
"GenomeViz",
Expand Down
14 changes: 6 additions & 8 deletions src/pygenomeviz/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def _run_mummer(self, fa_file1: Path, fa_file2: Path, idx: int) -> list[AlignCoo
# Run genome alignment using nucmer or promer
prefix = self.outdir / f"out{idx}"
delta_file = prefix.with_suffix(".delta")
cmd = f"{self._align_binary} --mum {fa_file1} {fa_file2} --prefix={prefix}"
cmd = f"{self._align_binary} --mum '{fa_file1}' '{fa_file2}' --prefix={prefix}"
_ = sp.run(cmd, shell=True, capture_output=True, text=True)

# Run delta-filter to map 'one-to-one' or 'many-to-many' relation
Expand Down Expand Up @@ -341,16 +341,14 @@ def run(self) -> list[AlignCoord]:
with tempfile.TemporaryDirectory() as tmpdir:
name1 = fa_file1.with_suffix("").name
name2 = fa_file2.with_suffix("").name
rbh_result_file = self.outdir / f"{idx+1:02d}_{name1}-{name2}_rbh.tsv"
cmd = f"mmseqs easy-rbh {fa_file1} {fa_file2} {rbh_result_file} "
rbh_file = self.outdir / f"{idx+1:02d}_{name1}-{name2}_rbh.tsv"
cmd = f"mmseqs easy-rbh '{fa_file1}' '{fa_file2}' '{rbh_file}' "
cmd += f"{tmpdir} --threads {self.process_num} -e {self.evalue} -v 0"
if not self.quiet:
print(f"# {idx+1:02d}: {name1}-{name2} RBH search\n$ {cmd}\n")
sp.run(cmd, shell=True)
_ = sp.run(cmd, shell=True, capture_output=True, text=True)

align_coords.extend(
self.parse_rbh_result(rbh_result_file, name1, name2)
)
align_coords.extend(self.parse_rbh_result(rbh_file, name1, name2))

return align_coords

Expand Down Expand Up @@ -519,7 +517,7 @@ def parse_pmauve_file(self, bbone_file: str | Path) -> list[AlignCoord]:
continue
rows.append(row)
# Sort by reference seq coordinates
rows = sorted(rows, key=lambda row: row[ref_idx])
rows = sorted(rows, key=lambda row: row[ref_idx]) # type: ignore

align_coords = []
for row in rows:
Expand Down
8 changes: 7 additions & 1 deletion src/pygenomeviz/gui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ def load_gbk_file(gbk_file: str | Path | UploadedFile) -> Genbank:
return Genbank(gbk_file)
else:
filename = Path(gbk_file.name).stem
trans_dict = {
" ": "_",
"|": "_",
"(": "[",
")": "]",
}
return Genbank(
StringIO(gbk_file.getvalue().decode("utf-8")),
name=filename.replace(" ", "_").replace("|", "_"),
name=filename.translate(str.maketrans(trans_dict)),
)


Expand Down
97 changes: 97 additions & 0 deletions src/pygenomeviz/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,103 @@ def add_exon_feature(
)
)

def add_features(
self,
features: list[SeqFeature],
label_type: str | None = None,
label_handle_func: Callable[[str], str] | None = None,
labelsize: int = 15,
labelcolor: str = "black",
plotstyle: LiteralTypes.PLOTSTYLE = "bigarrow",
facecolor: str = "orange",
edgecolor: str = "black",
linewidth: float = 0,
labelrotation: int = 45,
labelvpos: LiteralTypes.LABELVPOS = "strand",
labelhpos: LiteralTypes.LABELHPOS = "center",
labelha: LiteralTypes.LABELHA = "left",
arrow_shaft_ratio: float = 0.5,
size_ratio: float = 1.0,
patch_kws: dict[str, Any] | None = None,
) -> None:
"""Add features (BioPython's SeqFeature list)
Parameters
----------
features : str, optional
BioPython's SeqFeature list
label_type : str | None, optional
Label type (e.g. `gene`,`protein_id`,`product`,etc...)
label_handle_func : Callable[[str], str] | None, optional
User defined function to handle label.
Useful for filtering out unnecesary labels such as `hypothetical ~~~`,
omitting labels with long characters, etc.
labelsize : int, optional
Feature label size
labelcolor : str, optional
Feature label color
plotstyle : str, optional
Feature plot style (`bigarrow`|`arrow`|`bigbox`|`box`|`bigrbox`|`rbox`)
facecolor : str, optional
Feature facecolor.
If Genbank qualifiers has facecolor key (e.g. `/facecolor="red"`),
facecolor key value is applied preferentially.
edgecolor : str, optional
Feature edgecolor
linewidth : float, optional
Feature edge linewidth
labelrotation : int, optional
Feature label rotation
labelvpos : str, optional
Feature label vertical position (`top`|`center`|`bottom`|`strand`)
If 'strand' is set, 'top' or 'bottom' is auto selected by strand.
labelhpos : str, optional
Feature label horizontal position (`left`|`center`|`right`)
labelha : str, optional
Feature label horizontal alignment (`left`|`center`|`right`)
arrow_shaft_ratio : float, optional
Feature arrow shaft ratio
size_ratio : float, optional
Feature size ratio to track
patch_kws : dict[str, Any] | None, optional
Patch properties (e.g. `dict(fc="red", ec="black", lw=0.5, ...)`)
<https://matplotlib.org/stable/api/_as_gen/matplotlib.patches.Patch.html>
"""
for feature in features:
# Check if start & end positions are within appropriate track range
start = int(str(feature.location.start))
end = int(str(feature.location.end))
self._within_valid_range(start, end, raise_error_on_false=True)

# Get label value & apply handle func if exists
label = feature.qualifiers.get(label_type, [""])[0]
if label_handle_func is not None:
label = label_handle_func(label)
feat_fc = feature.qualifiers.get("facecolor", [facecolor])[0]

self.features.append(
Feature(
start,
end,
feature.strand,
label,
labelsize,
labelcolor,
plotstyle,
feat_fc,
edgecolor,
linewidth,
labelrotation,
labelvpos,
labelhpos,
labelha,
arrow_shaft_ratio,
size_ratio,
patch_kws,
feature,
)
)

def add_genbank_features(
self,
gbk: Genbank,
Expand Down
13 changes: 13 additions & 0 deletions tests/test_genomeviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,19 @@ def test_add_exon_feature(tmp_path: Path):
assert fig_outfile.exists()


def test_add_features(gbk_file: Path, tmp_path: Path):
"""Test add features (BioPython's SeqFeature list)"""
gbk = Genbank(gbk_file)

gv = GenomeViz()
track = gv.add_feature_track(gbk.name, gbk.range_size)
track.add_features(gbk.extract_features())

fig_outfile = tmp_path / "result.png"
gv.savefig(fig_outfile)
assert fig_outfile.exists()


def test_gff_genomeviz(gff_file: Path, tmp_path: Path):
"""Test add_gff_features"""
gff = Gff(gff_file)
Expand Down

0 comments on commit 9f5d6f2

Please sign in to comment.