### Install pygenomeviz and Dependencies Source: https://github.com/moshi4/pygenomeviz/blob/main/docs/getting_started.ipynb Installs the pygenomeviz library and necessary command-line tools like BLAST, MUMmer, and MMseqs2 using pip and apt-get. ```python # !pip install pygenomeviz # !apt install ncbi-blast+ mummer mmseqs2 ``` -------------------------------- ### progressiveMauve CLI Workflow for Genome Visualization Source: https://github.com/moshi4/pygenomeviz/blob/main/README.md This command-line example shows how to visualize genome alignments using the `pgv-pmauve` tool. It involves downloading an example dataset and then executing the progressiveMauve workflow with an option to display a scale bar. ```shell # Download example dataset pgv-download escherichia_coli # Run progressiveMauve CLI workflow pgv-pmauve NC_000913.gbk.gz NC_002695.gbk.gz NC_011751.gbk.gz NC_011750.gbk.gz \ -o pgv-pmauve_example --show_scale_bar ``` -------------------------------- ### Install pygenomeviz and NCBI BLAST+ Source: https://github.com/moshi4/pygenomeviz/blob/main/notebooks/example.ipynb Installs the pygenomeviz library and the ncbi-blast+ package using pip and apt-get respectively. These are common prerequisites for genomic visualization tasks. ```shell # !pip install pygenomeviz # !apt install ncbi-blast+ ``` -------------------------------- ### Compare Genbank Files using BLAST Alignment with PyGenomeViz Source: https://github.com/moshi4/pygenomeviz/blob/main/notebooks/example.ipynb This example demonstrates comparing multiple Genbank files by running a BLAST alignment and visualizing the results. It loads example Genbank datasets, plots CDS features for each, performs BLAST alignment using `pygenomeviz.align.Blast`, filters results by length and identity thresholds, and prepares for visualization. Dependencies include `pygenomeviz.parser.Genbank`, `pygenomeviz.utils.load_example_genbank_dataset`, and `pygenomeviz.align.Blast`. ```python from pygenomeviz import GenomeViz from pygenomeviz.parser import Genbank from pygenomeviz.utils import load_example_genbank_dataset from pygenomeviz.align import Blast, AlignCoord gbk_files = load_example_genbank_dataset("yersinia_phage") gbk_list = list(map(Genbank, gbk_files)) gv = GenomeViz(track_align_type="center") gv.set_scale_bar() # Plot CDS features for gbk in gbk_list: track = gv.add_feature_track(gbk.name, gbk.get_seqid2size(), align_label=False) for seqid, features in gbk.get_seqid2features("CDS").items(): segment = track.get_segment(seqid) segment.add_features(features, plotstyle="bigarrow", fc="limegreen", lw=0.5) # Run BLAST alignment & filter by user-defined threshold align_coords = Blast(gbk_list, seqtype="protein").run() align_coords = AlignCoord.filter(align_coords, length_thr=100, identity_thr=30) ``` -------------------------------- ### BLAST CLI Workflow for Genome Visualization Source: https://github.com/moshi4/pygenomeviz/blob/main/README.md This command-line example demonstrates how to visualize genome alignments using the `pgv-blast` tool. It first downloads an example dataset and then runs the alignment visualization with specified parameters for sequence type, output file, and filtering thresholds. ```shell # Download example dataset pgv-download yersinia_phage # Run BLAST CLI workflow pgv-blast NC_070914.gbk NC_070915.gbk NC_070916.gbk NC_070918.gbk \ -o pgv-blast_example --seqtype protein --show_scale_bar --curve \ --feature_linewidth 0.3 --length_thr 100 --identity_thr 30 ``` -------------------------------- ### MMseqs CLI Workflow for Genome Visualization Source: https://github.com/moshi4/pygenomeviz/blob/main/README.md This command-line example demonstrates genome alignment visualization using the `pgv-mmseqs` tool. It includes downloading an example dataset and running the MMseqs workflow with options for scale bar, curves, feature line width, and custom colors for features and links. ```shell # Download example dataset pgv-download enterobacteria_phage # Run MMseqs CLI workflow pgv-mmseqs NC_013600.gbk NC_016566.gbk NC_019724.gbk NC_024783.gbk NC_028901.gbk NC_031081.gbk \ -o pgv-mmseqs_example --show_scale_bar --curve --feature_linewidth 0.3 \ --feature_type2color CDS:skyblue --normal_link_color chocolate --inverted_link_color limegreen ``` -------------------------------- ### Visualize Genbank Features with PyGenomeViz Source: https://github.com/moshi4/pygenomeviz/blob/main/notebooks/example.ipynb Shows how to load a Genbank file and visualize its features using GenomeViz. It utilizes `pygenomeviz.parser.Genbank` to parse the file and `pygenomeviz.utils.load_example_genbank_dataset` for example data. The code adds a feature track and plots extracted features. ```python from pygenomeviz import GenomeViz from pygenomeviz.parser import Genbank from pygenomeviz.utils import load_example_genbank_dataset gbk_files = load_example_genbank_dataset("yersinia_phage") gbk = Genbank(gbk_files[0]) gv = GenomeViz() gv.set_scale_bar(ymargin=0.5) track = gv.add_feature_track(gbk.name, gbk.genome_length) track.add_sublabel() features = gbk.extract_features() track.add_features(features) gv.savefig("genbank_features.png") ``` -------------------------------- ### Plot Contigs per Track with CDS and rRNA Features Source: https://github.com/moshi4/pygenomeviz/blob/main/docs/getting_started.ipynb This example shows how to create a separate track for each contig and plot CDS and rRNA features within them. It differentiates between regular and pseudo CDS features by color. Dependencies include pygenomeviz, Gff, load_example_gff_file, and is_pseudo_feature. ```python from pygenomeviz import GenomeViz from pygenomeviz.parser import Gff from pygenomeviz.utils import load_example_gff_file, is_pseudo_feature gff_file = load_example_gff_file("mycoplasma_mycoides.gff") gff = Gff(gff_file) gv = GenomeViz(fig_track_height=0.5, feature_track_ratio=0.5) gv.set_scale_xticks(labelsize=10) # Plot CDS, rRNA features for each contig to tracks for seqid, size in gff.get_seqid2size().items(): track = gv.add_feature_track(seqid, size, labelsize=15) track.add_sublabel(f"{size:,} bp", size=10, color="grey") cds_features = gff.get_seqid2features(feature_type="CDS")[seqid] # CDS: blue, CDS(pseudo): grey for cds_feature in cds_features: color = "grey" if is_pseudo_feature(cds_feature) else "blue" track.add_features(cds_feature, color=color) # rRNA: lime rrna_features = gff.get_seqid2features(feature_type="rRNA")[seqid] track.add_features(rrna_features, color="lime") fig = gv.plotfig() ``` -------------------------------- ### MUMmer CLI Workflow for Genome Visualization Source: https://github.com/moshi4/pygenomeviz/blob/main/README.md This command-line example shows how to visualize genome alignments using the `pgv-mummer` tool. It involves downloading an example dataset and then executing the MUMmer workflow with options to display a scale bar, add curves, and specify colors for different feature types. ```shell # Download example dataset pgv-download mycoplasma_mycoides # Run MUMmer CLI workflow pgv-mummer GCF_000023685.1.gbff GCF_000800785.1.gbff GCF_000959055.1.gbff GCF_000959065.1.gbff \ -o pgv-mummer_example --show_scale_bar --curve \ --feature_type2color CDS:blue rRNA:lime tRNA:magenta ``` -------------------------------- ### GenomeViz with Multiple Genomes and Features Source: https://github.com/moshi4/pygenomeviz/blob/main/docs/getting_started.ipynb Shows how to initialize GenomeViz with multiple genome configurations, each having a name, size, and a list of features. It also customizes the x-axis ticks. Dependencies include pygenomeviz. ```python from pygenomeviz import GenomeViz genome_list = [ dict(name="genome 01", size=1000, features=((150, 300, 1), (500, 700, -1), (750, 950, 1))), dict(name="genome 02", size=1300, features=((50, 200, 1), (350, 450, 1), (700, 900, -1), (950, 1150, -1))), dict(name="genome 03", size=1200, features=((150, 300, 1), (350, 450, -1), (500, 700, -1), (700, 900, -1))), ] gv = GenomeViz() gv.set_scale_xticks() # Further visualization code would follow here to plot these genomes. ``` -------------------------------- ### Visualize Chromosomes with Features and Links in pygenomeviz Source: https://github.com/moshi4/pygenomeviz/blob/main/docs/getting_started.ipynb This example demonstrates visualizing multiple genomes (chimp, human, mouse) by treating their chromosomes as segments within tracks. Each segment is then populated with features styled as 'bigrbox' with distinct colors. Links are added between homologous regions of different genomes, showcasing synteny. This is useful for comparative genomics. ```python from pygenomeviz import GenomeViz from pygenomeviz.utils import ColorCycler ColorCycler.set_cmap("tab10") name2chr_segments = dict( chimp=dict(chr1=224244399, chr2A=108022953, chr2B=128755405, chr3=196562556, chr4=189151597, chr5=159319378, chr6=168369391, chr7=156046543, chr8=143338810, chr9=110513671, chr10=129809613, chr11=130782606, chr12=130995916, chr13=95599650, chr14=87716528, chr15=80519282, chr16=75912362, chr17=76554115, chr18=74774469, chr19=56733099, chr20=64035432, chr21=33232379, chr22=33698415, chrX=151576176, chrY=26350515), human=dict(chr1=248956422, chr2=242193529, chr3=198295559, chr4=190214555, chr5=181538259, chr6=170805979, chr7=159345973, chr8=145138636, chr9=138394717, chr10=133797422, chr11=135086622, chr12=133275309, chr13=114364328, chr14=107043718, chr15=101991189, chr16=90338345, chr17=83257441, chr18=80373285, chr19=58617616, chr20=64444167, chr21=46709983, chr22=50818468, chrX=156040895, chrY=57227415), mouse=dict(chr1=195471971, chr2=182113224, chr3=160039680, chr4=156508116, chr5=151834684, chr6=149736546, chr7=145441459, chr8=129401213, chr9=124595110, chr10=130694993, chr11=122082543, chr12=120129022, chr13=120421639, chr14=124902244, chr15=104043685, chr16=98207768, chr17=94987271, chr18=90702639, chr19=61431566, chrX=171031299, chrY=91744698), ) gv = GenomeViz(fig_track_height=0.7, feature_track_ratio=0.15, track_align_type="center") gv.set_scale_bar(ymargin=2) for name, chr_segments in name2chr_segments.items(): color = ColorCycler() # Set chromosomes as segments track = gv.add_feature_track( name, chr_segments, space=0.01, align_label=False, label_kws=dict(color=color), ) for seg in track.segments: seg.add_feature( seg.start, seg.end, plotstyle="bigrbox", fc=color, ec="black", lw=0.5, label=seg.name.replace("chr", ""), text_kws=dict(rotation=0, size=8, color="white", vpos="center", hpos="center"), ) gv.add_link(("human", "chr1", 0, 100000000), ("chimp", "chr1", 0, 100000000)) gv.add_link(("human", "chr2", 0, 120000000), ("chimp", "chr2A", 0, 108022953), color="lime", curve=True) gv.add_link(("human", "chr2", 120000000, 242193529), ("chimp", "chr2B", 0, 128755405), color="lime", curve=True) fig = gv.plotfig() ``` -------------------------------- ### Create and Save Basic Genome Features Source: https://github.com/moshi4/pygenomeviz/blob/main/notebooks/example.ipynb Demonstrates creating a basic genome visualization with feature tracks. It adds several features with different start and end points and strand orientations, then saves the figure as 'features.png'. ```python from pygenomeviz import GenomeViz gv = GenomeViz() gv.set_scale_xticks(ymargin=0.5) track = gv.add_feature_track("tutorial", 1000) track.add_sublabel() track.add_feature(50, 200, 1) track.add_feature(250, 460, -1, fc="blue") track.add_feature(500, 710, 1, fc="lime") track.add_feature(750, 960, 1, fc="magenta", lw=1.0) gv.savefig("features.png") ``` -------------------------------- ### Visualize Fasta Genomes and MUMmer Alignments with pygenomeviz Source: https://github.com/moshi4/pygenomeviz/blob/main/docs/getting_started.ipynb This Python snippet visualizes complete/contig genomes from Fasta files and plots MUMmer alignment links. It loads example Fasta datasets, initializes GenomeViz with a dark theme, and adds feature tracks for each genome. It then runs MUMmer alignment on the Fasta list and plots the resulting alignment links with a color gradient. This is suitable for visualizing and comparing unannotated genome sequences. ```python from pygenomeviz import GenomeViz from pygenomeviz.parser import Fasta from pygenomeviz.utils import load_example_fasta_dataset, ColorCycler from pygenomeviz.align import MUMmer ColorCycler.set_cmap("Set3") fasta_files = load_example_fasta_dataset("mycoplasma_mycoides") fasta_list = list(map(Fasta, fasta_files)) gv = GenomeViz(feature_track_ratio=0.10, track_align_type="center", theme="dark") gv.set_scale_bar() # Plot complete/contig genomes for fasta in fasta_list: color = ColorCycler() track = gv.add_feature_track(fasta.name, fasta.get_seqid2size(), label_kws=dict(color=color)) for segment in track.segments: segment.add_feature(segment.start, segment.end, plotstyle="bigrbox", fc=color, lw=0.5) # Run MUMmer alignment align_coords = MUMmer(fasta_list).run() # Plot MUMmer alignment links if len(align_coords) > 0: min_ident = int(min([ac.identity for ac in align_coords if ac.identity])) color, inverted_color = "grey", "red" for ac in align_coords: gv.add_link(ac.query_link, ac.ref_link, color=color, inverted_color=inverted_color, v=ac.identity, vmin=min_ident, curve=True) gv.set_colorbar([color, inverted_color], vmin=min_ident) fig = gv.plotfig() ``` -------------------------------- ### Visualize GFF Contig Features with PyGenomeViz Source: https://github.com/moshi4/pygenomeviz/blob/main/notebooks/example.ipynb This example shows how to parse a GFF file and visualize features (CDS and rRNA) for each contig into separate tracks using GenomeViz. It differentiates between normal and pseudo CDS features by color. Dependencies include `pygenomeviz.parser.Gff`, `pygenomeviz.utils.load_example_gff_file`, and `pygenomeviz.utils.is_pseudo_feature`. ```python from pygenomeviz import GenomeViz from pygenomeviz.parser import Gff from pygenomeviz.utils import load_example_gff_file, is_pseudo_feature gff_file = load_example_gff_file("mycoplasma_mycoides.gff") gff = Gff(gff_file) gv = GenomeViz(fig_track_height=0.5, feature_track_ratio=0.5) gv.set_scale_xticks(labelsize=10) # Plot CDS, rRNA features for each contig to tracks for seqid, size in gff.get_seqid2size().items(): track = gv.add_feature_track(seqid, size, labelsize=15) track.add_sublabel(size=10, color="grey") cds_features = gff.get_seqid2features(feature_type="CDS")[seqid] # CDS: blue, CDS(pseudo): grey for cds_feature in cds_features: color = "grey" if is_pseudo_feature(cds_feature) else "blue" track.add_features(cds_feature, color=color) # rRNA: lime rrna_features = gff.get_seqid2features(feature_type="rRNA")[seqid] track.add_features(rrna_features, color="lime") gv.savefig("gff_contigs.png") ``` -------------------------------- ### Launch Web GUI Application Source: https://context7.com/moshi4/pygenomeviz/llms.txt Launches an interactive web-based GUI for genome visualization. Installation requires the `gui` extra for `pygenomeviz`. The application can be run directly or via Docker, providing an accessible interface for genome visualization tasks. ```bash # Install with GUI support pip install pygenomeviz[gui] # Launch GUI application on localhost:8501 pgv-gui # Or run with Docker docker run -it --rm -p 8501:8501 ghcr.io/moshi4/pygenomeviz:latest pgv-gui ``` -------------------------------- ### Visualize Genbank Features and MUMmer Alignments with pygenomeviz Source: https://github.com/moshi4/pygenomeviz/blob/main/docs/getting_started.ipynb This Python snippet visualizes features (CDS, rRNA) from Genbank files and plots MUMmer alignment links. It loads example Genbank datasets, initializes GenomeViz, and adds feature tracks for each contig. It then runs MUMmer alignment on the Genbank list and plots the resulting alignment links with a color gradient representing identity. This is useful for comparative genomics analysis of annotated genomes. ```python from pygenomeviz import GenomeViz from pygenomeviz.parser import Genbank from pygenomeviz.utils import load_example_genbank_dataset, is_pseudo_feature from pygenomeviz.align import MUMmer gbk_files = load_example_genbank_dataset("mycoplasma_mycoides") gbk_list = list(map(Genbank, gbk_files)) gv = GenomeViz() gv.set_scale_bar() # Plot CDS, rRNA features for each contig to tracks for gbk in gbk_list: track = gv.add_feature_track(gbk.name, gbk.get_seqid2size(), align_label=False) for seqid, features in gbk.get_seqid2features(None).items(): segment = track.get_segment(seqid) for feature in features: if feature.type == "CDS": # CDS: blue, CDS(pseudo): grey color = "grey" if is_pseudo_feature(feature) else "blue" segment.add_features(feature, fc=color) elif feature.type == "rRNA": # rRNA: lime segment.add_features(feature, fc="lime") # Run MUMmer alignment align_coords = MUMmer(gbk_list).run() # Plot MUMmer alignment links if len(align_coords) > 0: min_ident = int(min([ac.identity for ac in align_coords if ac.identity])) color, inverted_color = "grey", "red" for ac in align_coords: gv.add_link(ac.query_link, ac.ref_link, color=color, inverted_color=inverted_color, v=ac.identity, vmin=min_ident) gv.set_colorbar([color, inverted_color], vmin=min_ident) fig = gv.plotfig() ``` -------------------------------- ### Color Segments with pyGenomeViz ColorCycler Source: https://github.com/moshi4/pygenomeviz/blob/main/docs/getting_started.ipynb This example illustrates how to use pyGenomeViz's ColorCycler to assign distinct colors to segments and features from multiple Genbank files. It initializes GenomeViz with a specific track ratio, sets a scale bar, and then iterates through each Genbank file, assigning a unique color to each track segment and drawing a box representing the segment. Dependencies include pygenomeviz and its utility functions. ```python from pygenomeviz import GenomeViz from pygenomeviz.parser import Genbank from pygenomeviz.utils import load_example_genbank_dataset, ColorCycler ColorCycler.set_cmap("tab10") gbk_files = load_example_genbank_dataset("saccharomyces") gbk_list = list(map(Genbank, gbk_files)) gv = GenomeViz(fig_track_height=0.7, feature_track_ratio=0.15) gv.set_scale_bar(ymargin=2.0) for gbk in gbk_list: color = ColorCycler() track = gv.add_feature_track(gbk.name, gbk.get_seqid2size(), space=0.01, label_kws=dict(color=color)) track.set_label(track.name.replace("_", "\n")) for segment in track.segments: segment.add_feature(segment.start, segment.end, plotstyle="bigrbox", fc=color, lw=0.5) fig = gv.plotfig() ``` -------------------------------- ### Basic GenomeViz Plotting with a Feature Track Source: https://github.com/moshi4/pygenomeviz/blob/main/docs/getting_started.ipynb Demonstrates the basic usage of GenomeViz to create a plot with a single feature track. It initializes GenomeViz, adds a track, and generates a plot figure. Dependencies include the pygenomeviz library. ```python from pygenomeviz import GenomeViz gv = GenomeViz() track = gv.add_feature_track("tutorial", 1000) track.add_sublabel() fig = gv.plotfig() # gv.savefig("example.png") # gv.savefig_html("example.html") ``` -------------------------------- ### GenomeViz with Multi-Segment Tracks and Scale Bar Source: https://github.com/moshi4/pygenomeviz/blob/main/docs/getting_started.ipynb Demonstrates creating a GenomeViz plot with a track spanning multiple segments defined by a list of genome sizes. Each segment's boundaries are labeled. Dependencies include pygenomeviz. ```python from pygenomeviz import GenomeViz gv = GenomeViz() gv.set_scale_bar(ymargin=0.5) # Set multi segments on track genome_size_list = (100000, 180000, 150000) track = gv.add_feature_track("tutorial", genome_size_list) for segment in track.segments: segment.add_sublabel(f"{segment.name}: {segment.start:,} - {segment.end:,} bp") fig = gv.plotfig() ``` -------------------------------- ### Align Genomes using BLAST and Plot Links Source: https://github.com/moshi4/pygenomeviz/blob/main/docs/getting_started.ipynb This advanced snippet demonstrates genome alignment between Genbank files using BLAST and visualizing the alignment links. It filters alignments based on length and identity thresholds and plots them with a colorbar. Dependencies include pygenomeviz, Genbank, load_example_genbank_dataset, Blast, and AlignCoord. ```python from pygenomeviz import GenomeViz from pygenomeviz.parser import Genbank from pygenomeviz.utils import load_example_genbank_dataset from pygenomeviz.align import Blast, AlignCoord gbk_files = load_example_genbank_dataset("yersinia_phage") gbk_list = list(map(Genbank, gbk_files)) gv = GenomeViz(track_align_type="center") gv.set_scale_bar() # Plot CDS features for gbk in gbk_list: track = gv.add_feature_track(gbk.name, gbk.get_seqid2size(), align_label=False) for seqid, features in gbk.get_seqid2features("CDS").items(): segment = track.get_segment(seqid) segment.add_features(features, plotstyle="bigarrow", fc="limegreen", lw=0.5) # Run BLAST alignment & filter by user-defined threshold align_coords = Blast(gbk_list, seqtype="protein").run() align_coords = AlignCoord.filter(align_coords, length_thr=100, identity_thr=30) # Plot BLAST alignment links if len(align_coords) > 0: min_ident = int(min([ac.identity for ac in align_coords if ac.identity])) color, inverted_color = "grey", "red" for ac in align_coords: gv.add_link(ac.query_link, ac.ref_link, color=color, inverted_color=inverted_color, v=ac.identity, vmin=min_ident) gv.set_colorbar([color, inverted_color], vmin=min_ident) fig = gv.plotfig() ``` -------------------------------- ### GenomeViz Plotting with Features and X-axis Ticks Source: https://github.com/moshi4/pygenomeviz/blob/main/docs/getting_started.ipynb Illustrates adding multiple features with different strand orientations and colors to a GenomeViz track, along with customizing x-axis tick margins. Dependencies include pygenomeviz. ```python from pygenomeviz import GenomeViz gv = GenomeViz() gv.set_scale_xticks(ymargin=0.5) track = gv.add_feature_track("tutorial", 1000) track.add_sublabel() # Add features to track track.add_feature(50, 200, 1) track.add_feature(250, 460, -1, fc="blue") track.add_feature(500, 710, 1, fc="lime") track.add_feature(750, 960, 1, fc="magenta", lw=1.0) fig = gv.plotfig() ``` -------------------------------- ### GenomeViz with Styled Features and Scale Bar Source: https://github.com/moshi4/pygenomeviz/blob/main/docs/getting_started.ipynb Shows how to add various styled features (arrows, boxes, etc.) to a GenomeViz track with a scale bar, including customization of labels, colors, and line widths. Dependencies include pygenomeviz. ```python from pygenomeviz import GenomeViz gv = GenomeViz() gv.set_scale_bar(ymargin=0.5) track = gv.add_feature_track("tutorial", (1000, 2000)) track.add_sublabel() # Add styled features track.add_feature(1050, 1150, 1, label="arrow") track.add_feature(1200, 1300, -1, plotstyle="bigarrow", label="bigarrow", fc="red", lw=1) track.add_feature(1330, 1400, 1, plotstyle="bigbox", label="bigbox", fc="blue", text_kws=dict(rotation=0, hpos="center")) track.add_feature(1420, 1500, 1, plotstyle="box", label="box", fc="limegreen", text_kws=dict(size=10, color="blue")) track.add_feature(1550, 1600, 1, plotstyle="bigrbox", label="bigrbox", fc="magenta", ec="blue", lw=1, text_kws=dict(rotation=0, vpos="bottom", hpos="center")) track.add_feature(1650, 1750, -1, plotstyle="rbox", label="rbox", fc="grey", text_kws=dict(rotation=-45, vpos="bottom")) track.add_feature(1780, 1880, 1, fc="lime", hatch="o", arrow_shaft_ratio=0.2, label="arrow shaft\n0.2", text_kws=dict(rotation=0, hpos="center")) track.add_feature(1890, 1990, 1, fc="lime", hatch="/", arrow_shaft_ratio=1.0, label="arrow shaft\n1.0", text_kws=dict(rotation=0, hpos="center")) fig = gv.plotfig() ``` -------------------------------- ### GenomeViz with Multi-Segment Tracks, Regions, and Color Cycling Source: https://github.com/moshi4/pygenomeviz/blob/main/docs/getting_started.ipynb Illustrates using pygenomeviz to visualize features across multiple defined regions within a track. It utilizes a ColorCycler for feature colors and sets segment separators. Dependencies include pygenomeviz and ColorCycler. ```python from pygenomeviz import GenomeViz from pygenomeviz.utils import ColorCycler ColorCycler.set_cmap("Set3") features = ((1200, 1500, 1), (1600, 1960, -1), (4200, 4800, 1), (4900, 5180, -1), (5200, 5500, 1), (7000, 7600, -1), (7800, 8200, 1)) gv = GenomeViz() gv.set_scale_bar(ymargin=0.5) # Set multi segments on track target_regions = dict(region1=(1000, 2000), region2=(4000, 5500), region3=(7000, 8200)) track = gv.add_feature_track("tutorial", target_regions) track.set_segment_sep() for segment in track.segments: segment.add_sublabel(f"{segment.name}: {segment.start:,} - {segment.end:,} bp") # Add feature to segment for feature in features: start, end, strand = feature if segment.start <= start <= end <= segment.end: segment.add_feature(start, end, strand, fc=ColorCycler(), lw=1.0) fig = gv.plotfig() ``` -------------------------------- ### Create and Save Styled Genome Features Source: https://github.com/moshi4/pygenomeviz/blob/main/notebooks/example.ipynb Illustrates creating a genome visualization with various feature styling options, including different plot styles, labels, colors, and text properties. The output is saved as 'styled_features.png'. ```python from pygenomeviz import GenomeViz gv = GenomeViz() gv.set_scale_bar(ymargin=0.5) track = gv.add_feature_track("tutorial", (1000, 2000)) track.add_sublabel() track.add_feature(1050, 1150, 1, label="arrow") track.add_feature(1200, 1300, -1, plotstyle="bigarrow", label="bigarrow", fc="red", lw=1) track.add_feature(1330, 1400, 1, plotstyle="bigbox", label="bigbox", fc="blue", text_kws=dict(rotation=0, hpos="center")) track.add_feature(1420, 1500, 1, plotstyle="box", label="box", fc="limegreen", text_kws=dict(size=10, color="blue")) track.add_feature(1550, 1600, 1, plotstyle="bigrbox", label="bigrbox", fc="magenta", ec="blue", lw=1, text_kws=dict(rotation=0, vpos="bottom", hpos="center")) track.add_feature(1650, 1750, -1, plotstyle="rbox", label="rbox", fc="grey", text_kws=dict(rotation=-45, vpos="bottom")) track.add_feature(1780, 1880, 1, fc="lime", hatch="o", arrow_shaft_ratio=0.2, label="arrow shaft\n0.2", text_kws=dict(rotation=0, hpos="center")) track.add_feature(1890, 1990, 1, fc="lime", hatch="/", arrow_shaft_ratio=1.0, label="arrow shaft\n1.0", text_kws=dict(rotation=0, hpos="center")) gv.savefig("styled_features.png") ``` -------------------------------- ### Plot Genbank Features with pyGenomeViz Source: https://github.com/moshi4/pygenomeviz/blob/main/docs/getting_started.ipynb This snippet shows how to load a Genbank file, initialize GenomeViz, add a feature track, and plot the extracted features. It sets a scale bar and adds sublabels to the track. Dependencies include pygenomeviz and its utility functions. ```python from pygenomeviz import GenomeViz from pygenomeviz.parser import Genbank from pygenomeviz.utils import load_example_genbank_dataset gbk_files = load_example_genbank_dataset("yersinia_phage") gbk = Genbank(gbk_files[0]) gv = GenomeViz(fig_track_height=0.7) gv.set_scale_bar(ymargin=0.5) track = gv.add_feature_track(gbk.name, gbk.genome_length) track.add_sublabel() # Plot genbank features features = gbk.extract_features() track.add_features(features, lw=0.5) fig = gv.plotfig() ``` -------------------------------- ### GenomeViz with Exon Features and Styling Source: https://github.com/moshi4/pygenomeviz/blob/main/docs/getting_started.ipynb Demonstrates adding exon features to a GenomeViz track using different plot styles (box, arrow, bigarrow) and applying various styling options like colors, line widths, and text properties. Dependencies include pygenomeviz. ```python from pygenomeviz import GenomeViz exon_regions1 = [(0, 210), (300, 480), (590, 800), (850, 1000), (1030, 1300)] exon_regions2 = [(1500, 1710), (2000, 2480), (2590, 2800)] exon_regions3 = [(3000, 3300), (3400, 3690), (3800, 4100), (4200, 4620)] gv = GenomeViz() track = gv.add_feature_track("Exon Features", 5000) # Add exon features track.add_exon_feature(exon_regions1, strand=1, plotstyle="box", label="box", text_kws=dict(rotation=0, hpos="center")) track.add_exon_feature(exon_regions2, strand=-1, plotstyle="arrow", label="arrow", text_kws=dict(rotation=0, vpos="bottom", hpos="center"), patch_kws=dict(fc="darkgrey"), intron_patch_kws=dict(ec="red")) track.add_exon_feature(exon_regions3, strand=1, plotstyle="bigarrow", label="bigarrow", text_kws=dict(rotation=0, hpos="center"), patch_kws=dict(fc="lime", lw=1)) fig = gv.plotfig() ``` -------------------------------- ### Visualize Genome Features and MUMmer Alignments with pygenomeviz Source: https://github.com/moshi4/pygenomeviz/blob/main/docs/example_gallery.ipynb This snippet visualizes CDS and rRNA features from Genbank files and initiates a MUMmer alignment. It plots different feature types with distinct colors, handling pseudo-features for CDS. Requires pygenomeviz and mummer. ```python from pygenomeviz import GenomeViz from pygenomeviz.parser import Genbank from pygenomeviz.utils import load_example_genbank_dataset, is_pseudo_feature from pygenomeviz.align import MUMmer gbk_files = load_example_genbank_dataset("mycoplasma_mycoides") gbk_list = list(map(Genbank, gbk_files)) gv = GenomeViz() gv.set_scale_bar() # Plot CDS, rRNA features for each contig to tracks for gbk in gbk_list: track = gv.add_feature_track(gbk.name, gbk.get_seqid2size(), align_label=False) for seqid, features in gbk.get_seqid2features(None).items(): segment = track.get_segment(seqid) for feature in features: if feature.type == "CDS": # CDS: blue, CDS(pseudo): grey color = "grey" if is_pseudo_feature(feature) else "blue" segment.add_features(feature, fc=color) elif feature.type == "rRNA": # rRNA: lime segment.add_features(feature, fc="lime") # Run MUMmer alignment align_coords = MUMmer(gbk_list).run() ``` -------------------------------- ### Generate Interactive HTML Viewer Source: https://context7.com/moshi4/pygenomeviz/llms.txt Creates an interactive HTML output for genome visualization, supporting features like pan, zoom, and tooltips. This example demonstrates setting up tracks, adding features, and saving the visualization as an HTML file instead of a static image. ```python from pygenomeviz import GenomeViz from pygenomeviz.parser import Genbank from pygenomeviz.utils import load_example_genbank_dataset # Create visualization as usual gbk_files = load_example_genbank_dataset("yersinia_phage") gbk_list = list(map(Genbank, gbk_files)) gv = GenomeViz(track_align_type="center") gv.set_scale_bar() for gbk in gbk_list: track = gv.add_feature_track(gbk.name, gbk.get_seqid2size()) for seqid, features in gbk.get_seqid2features("CDS").items(): segment = track.get_segment(seqid) segment.add_features(features, plotstyle="bigarrow", fc="skyblue", lw=0.5) # Save as interactive HTML instead of static image gv.savefig_html("interactive_viewer.html") # HTML viewer supports: pan/zoom, tooltips, color editing, text editing ``` -------------------------------- ### Add Multi Tracks and Features in pygenomeviz Source: https://github.com/moshi4/pygenomeviz/blob/main/docs/getting_started.ipynb This snippet demonstrates how to add multiple genome tracks and populate them with features. It iterates through a list of genomes, creating a track for each and then adding individual features with specified start, end, and strand. The 'bigarrow' plot style and line width are applied to the features. This functionality is useful for visualizing different chromosomes or contigs. ```python from pygenomeviz import GenomeViz genome_list = [ dict(name="genome 01", size=1000, features=((150, 300, 1), (500, 700, -1), (750, 950, 1))), dict(name="genome 02", size=1300, features=((50, 200, 1), (350, 450, 1), (700, 900, -1), (950, 1150, -1))), dict(name="genome 03", size=1200, features=((150, 300, 1), (350, 450, -1), (500, 700, -1), (700, 900, -1))), ] gv = GenomeViz(track_align_type="center") gv.set_scale_bar() # Set multi tracks & features for genome in genome_list: name, size, features = genome["name"], genome["size"], genome["features"] track = gv.add_feature_track(name, size) track.add_sublabel() for idx, feature in enumerate(features, 1): start, end, strand = feature track.add_feature(start, end, strand, plotstyle="bigarrow", lw=1, label=f"gene{idx:02d}", text_kws=dict(rotation=0, vpos="top", hpos="center")) # Add links between "genome 01" and "genome 02" gv.add_link(("genome 01", 150, 300), ("genome 02", 50, 200)) gv.add_link(("genome 01", 700, 500), ("genome 02", 900, 700)) gv.add_link(("genome 01", 750, 950), ("genome 02", 1150, 950)) # Add links between "genome 02" and "genome 03" gv.add_link(("genome 02", 50, 200), ("genome 03", 150, 300), color="skyblue", inverted_color="lime", curve=True) gv.add_link(("genome 02", 350, 450), ("genome 03", 450, 350), color="skyblue", inverted_color="lime", curve=True) gv.add_link(("genome 02", 900, 700), ("genome 03", 700, 500), color="skyblue", inverted_color="lime", curve=True) gv.add_link(("genome 03", 900, 700), ("genome 02", 1150, 950), color="skyblue", inverted_color="lime", curve=True) fig = gv.plotfig() ``` -------------------------------- ### Visualize Genome Features and MMseqs Alignments with pygenomeviz (Different Colors) Source: https://github.com/moshi4/pygenomeviz/blob/main/docs/example_gallery.ipynb Similar to the previous example, this snippet visualizes CDS features and performs MMseqs RBH search, but uses different color schemes for the alignment links. Requires pygenomeviz and mmseqs2. ```python from pygenomeviz import GenomeViz from pygenomeviz.parser import Genbank from pygenomeviz.utils import load_example_genbank_dataset from pygenomeviz.align import MMseqs gbk_files = load_example_genbank_dataset("acinetobacter_phage") gbk_list = list(map(Genbank, gbk_files)) gv = GenomeViz(fig_track_height=0.8, feature_track_ratio=0.4) gv.set_scale_xticks() # Plot CDS features for gbk in gbk_list: track = gv.add_feature_track(gbk.name, gbk.get_seqid2size(), align_label=False) for seqid, features in gbk.get_seqid2features("CDS").items(): segment = track.get_segment(seqid) segment.add_features(features, fc="lightgreen", lw=0.2) # Run MMseqs RBH search align_coords = MMseqs(gbk_list).run() # Plot MMseqs RBH search links if len(align_coords) > 0: min_ident = int(min([ac.identity for ac in align_coords if ac.identity])) color, inverted_color = "skyblue", "salmon" for ac in align_coords: gv.add_link(ac.query_link, ac.ref_link, color=color, inverted_color=inverted_color, v=ac.identity, vmin=min_ident, curve=True) gv.set_colorbar([color, inverted_color], vmin=min_ident) fig = gv.plotfig() ``` -------------------------------- ### Visualize GFF Contigs with pygenomeviz Source: https://github.com/moshi4/pygenomeviz/blob/main/README.md This example initializes GenomeViz for visualizing GFF contigs with specific track height and ratio settings. It also configures the scale bar tick labels. The code prepares for further processing and plotting of features from a GFF file, likely focusing on contig-level visualization. Note: This is a partial snippet as the original text was truncated. ```python from pygenomeviz import GenomeViz from pygenomeviz.parser import Gff from pygenomeviz.utils import load_example_gff_file, is_pseudo_feature gff_file = load_example_gff_file("mycoplasma_mycoides.gff") gff = Gff(gff_file) gv = GenomeViz(fig_track_height=0.5, feature_track_ratio=0.5) gv.set_scale_xticks(labelsize=10) ``` -------------------------------- ### Configure Custom Theme for Visualization Source: https://context7.com/moshi4/pygenomeviz/llms.txt Allows customization of the visualization's appearance using predefined themes. The example shows how to create `GenomeViz` instances with either a 'light' theme (default) or a 'dark' theme, adjusting background and text colors accordingly. Tracks and features can then be added to these themed visualizations. ```python from pygenomeviz import GenomeViz # Light theme (default): white background with black text/edges gv_light = GenomeViz(theme="light", fig_width=15, fig_track_height=1.0) # Dark theme: black background with white text/edges gv_dark = GenomeViz(theme="dark", fig_width=15, fig_track_height=1.0) # Add tracks and features as usual track = gv_dark.add_feature_track("genome", 10000) track.add_feature(1000, 3000, 1, fc="lime", label="gene1") track.add_feature(5000, 8000, -1, fc="magenta", label="gene2") gv_dark.savefig("dark_theme_example.png") ``` -------------------------------- ### Create Linked Genome Tracks Source: https://github.com/moshi4/pygenomeviz/blob/main/notebooks/example.ipynb Generates a visualization with multiple genome tracks and adds links between features of different genomes. This demonstrates how to represent relationships or synteny between genomic regions. ```python from pygenomeviz import GenomeViz genome_list = [ dict(name="genome 01", size=1000, features=((150, 300, 1), (500, 700, -1), (750, 950, 1))), dict(name="genome 02", size=1300, features=((50, 200, 1), (350, 450, 1), (700, 900, -1), (950, 1150, -1))), dict(name="genome 03", size=1200, features=((150, 300, 1), (350, 450, -1), (500, 700, -1), (700, 900, -1))), ] gv = GenomeViz(track_align_type="center") gv.set_scale_bar() for genome in genome_list: name, size, features = genome["name"], genome["size"], genome["features"] track = gv.add_feature_track(name, size) track.add_sublabel() for idx, feature in enumerate(features, 1): start, end, strand = feature track.add_feature(start, end, strand, plotstyle="bigarrow", lw=1, label=f"gene{idx:02d}", text_kws=dict(rotation=0, vpos="top", hpos="center")) # Add links between "genome 01" and "genome 02" gv.add_link(("genome 01", 150, 300), ("genome 02", 50, 200)) gv.add_link(("genome 01", 700, 500), ("genome 02", 900, 700)) gv.add_link(("genome 01", 750, 950), ("genome 02", 1150, 950)) ``` -------------------------------- ### Initialize GenomeViz and Set Save Option Source: https://github.com/moshi4/pygenomeviz/blob/main/notebooks/example.ipynb Initializes the GenomeViz object and configures the save behavior. `GenomeViz.clear_savefig = False` prevents clearing previous save figures when saving a new one. ```python from pygenomeviz import GenomeViz GenomeViz.clear_savefig = False ``` -------------------------------- ### progressiveMauve CLI Workflow for Genome Comparison Source: https://context7.com/moshi4/pygenomeviz/llms.txt Executes genome comparison using the progressiveMauve command-line workflow. This involves downloading a dataset and running the progressiveMauve comparison, with an option to display a scale bar on the resulting visualization. It's designed for comparative genomics analyses. ```bash # Download example dataset pgv-download escherichia_coli # Run progressiveMauve comparison workflow pgv-pmauve NC_000913.gbk.gz NC_002695.gbk.gz NC_011751.gbk.gz NC_011750.gbk.gz \ -o pgv-pmauve_example \ --show_scale_bar # Output: pgv-pmauve_example.png ```