Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 48 additions & 19 deletions ppanggolin/graph/makeGraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from tqdm import tqdm

# local libraries
from ppanggolin.genome import Contig, Gene
from ppanggolin.pangenome import Pangenome
from ppanggolin.formats import read_pangenome, write_pangenome, erase_pangenome

Expand Down Expand Up @@ -84,6 +85,52 @@ def remove_high_copy_number(pangenome, number):
fam.removed = True


def _is_fragment_self_link(gene1: Gene, gene2: Gene) -> bool:
"""Tell whether two genes are pieces of the same fragmented gene.

Two neighboring genes of the same family, one of them flagged as a fragment, are not
linked in the graph: they likely come from a gene split in pieces, not from two neighbor genes.

:param gene1: First gene
:param gene2: Second gene
:return: True if the two genes must not be linked
"""
return gene1.family == gene2.family and (gene1.is_fragment or gene2.is_fragment)


def add_contig_edges(pangenome: Pangenome, contig: Contig):
"""Add to the neighbors graph the edges between the consecutive genes of a contig.

Genes of removed families are skipped, so their neighbors are linked together, and
two genes of a fragmented gene are not linked. On a circular contig, the last and
the first kept genes are linked under the same rules.

:param pangenome: Pangenome receiving the edges
:param contig: Contig whose genes are linked
"""
first = prev = None
for gene in contig.genes:
if gene.family is None:
raise AttributeError("a Gene does not have a GeneFamily object associated")
if gene.family.removed:
continue
if prev is None:
first = gene
elif not _is_fragment_self_link(prev, gene):
pangenome.add_edge(gene, prev)
prev = gene

# first is None when the contig only has genes of removed families,
# and first is prev when a single gene is kept: there is nothing to close then.
if (
contig.is_circular
and first is not None
and first is not prev
and not _is_fragment_self_link(prev, first)
):
pangenome.add_edge(first, prev)


def compute_neighbors_graph(
pangenome: Pangenome,
remove_copy_number: int = 0,
Expand Down Expand Up @@ -115,25 +162,7 @@ def compute_neighbors_graph(
bar.set_description(f"Processing {org.name}")
bar.refresh()
for contig in org.contigs:
prev = None
for gene in contig.genes:
try:
if not gene.family.removed:
if prev is not None and not (
prev.family == gene.family
and (prev.is_fragment or gene.is_fragment)
):
pangenome.add_edge(gene, prev)
prev = gene
except AttributeError:
raise AttributeError(
"a Gene does not have a GeneFamily object associated"
)
except Exception:
raise Exception("Unexpected error. Please report on our github.")
if prev is not None and contig.is_circular and contig.number_of_genes > 0:
# if prev is None, the contig is entirely made of duplicated genes, so no edges are added
pangenome.add_edge(contig[0], prev)
add_contig_edges(pangenome, contig)
logging.getLogger("PPanGGOLiN").info("Done making the neighbors graph.")
pangenome.status["neighborsGraph"] = "Computed"

Expand Down
119 changes: 119 additions & 0 deletions tests/unit_tests/graph/test_make_graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#! /usr/bin/env python3

from typing import Dict, List, Optional, Set, Tuple

import pytest

from ppanggolin.genome import Contig, Gene, Organism
from ppanggolin.geneFamily import GeneFamily
from ppanggolin.graph.makeGraph import add_contig_edges
from ppanggolin.pangenome import Pangenome


def build_contig(
families: List[str],
fragments: Optional[Set[int]] = None,
removed: Optional[Set[str]] = None,
is_circular: bool = False,
) -> Tuple[Pangenome, Contig]:
"""Build a pangenome with one contig whose genes belong to the given families.

:param families: Family name of each gene, in contig order
:param fragments: Positions of the genes flagged as fragments
:param removed: Names of the families removed from the graph
:param is_circular: Whether the contig is circular
:return: The pangenome and its contig
"""
fragments = fragments or set()
removed = removed or set()
pangenome = Pangenome()
organism = Organism("organism")
contig = Contig(0, "contig", is_circular=is_circular)
organism.add(contig)
fams: Dict[str, GeneFamily] = {}
for position, fam_name in enumerate(families):
if fam_name not in fams:
fams[fam_name] = GeneFamily(len(fams), fam_name)
fams[fam_name].removed = fam_name in removed
pangenome.add_gene_family(fams[fam_name])
gene = Gene(f"gene_{position}")
gene.fill_annotations(
start=10 * position + 1,
stop=10 * position + 9,
strand="+",
position=position,
)
gene.is_fragment = position in fragments
contig.add(gene)
gene.fill_parents(organism, contig)
fams[fam_name].add(gene)
return pangenome, contig


def gene_pairs(pangenome: Pangenome) -> Set[frozenset]:
"""Unordered pairs of gene identifiers linked in the graph."""
return {
frozenset((gene1.ID, gene2.ID))
for edge in pangenome.edges
for gene1, gene2 in edge.gene_pairs
}


def pair(position1: int, position2: int) -> frozenset:
return frozenset((f"gene_{position1}", f"gene_{position2}"))


@pytest.mark.parametrize("is_circular", [False, True])
def test_consecutive_genes_are_linked(is_circular):
pangenome, contig = build_contig(["A", "B", "C"], is_circular=is_circular)
add_contig_edges(pangenome, contig)
expected = {pair(0, 1), pair(1, 2)} | ({pair(2, 0)} if is_circular else set())
assert gene_pairs(pangenome) == expected


def test_fragments_of_a_same_family_are_not_linked():
pangenome, contig = build_contig(["A", "B", "B", "C"], fragments={2})
add_contig_edges(pangenome, contig)
assert gene_pairs(pangenome) == {pair(0, 1), pair(2, 3)}


def test_tandem_copies_without_fragment_are_linked():
pangenome, contig = build_contig(["A", "B", "B", "C"])
add_contig_edges(pangenome, contig)
assert pair(1, 2) in gene_pairs(pangenome)


def test_circular_closure_skips_fragments_of_a_same_family():
# first and last genes are pieces of the same fragmented gene, split by the contig origin
pangenome, contig = build_contig(
["A", "B", "C", "A"], fragments={0, 3}, is_circular=True
)
add_contig_edges(pangenome, contig)
assert gene_pairs(pangenome) == {pair(0, 1), pair(1, 2), pair(2, 3)}


def test_circular_closure_skips_removed_families():
pangenome, contig = build_contig(
["R", "A", "B", "C"], removed={"R"}, is_circular=True
)
add_contig_edges(pangenome, contig)
assert gene_pairs(pangenome) == {pair(1, 2), pair(2, 3), pair(3, 1)}


def test_removed_families_are_skipped():
pangenome, contig = build_contig(["A", "R", "B"], removed={"R"})
add_contig_edges(pangenome, contig)
assert gene_pairs(pangenome) == {pair(0, 2)}


def test_circular_contig_with_a_single_gene_has_no_edge():
pangenome, contig = build_contig(["A"], is_circular=True)
add_contig_edges(pangenome, contig)
assert gene_pairs(pangenome) == set()


def test_circular_contig_with_two_genes_is_linked_twice():
pangenome, contig = build_contig(["A", "B"], is_circular=True)
add_contig_edges(pangenome, contig)
(edge,) = pangenome.edges
assert len(edge.gene_pairs) == 2
Loading