def addEdge(self, gene1, gene2): """Adds an edge between the two gene families that the two given genes belong to. Genes object are expected, and they are also expected to have a family assigned :param gene1: The first gene :type gene1: :class:`ppanggolin.genome.Gene` :param gene2: The second gene :type gene2: :class:`ppanggolin.genome.Gene` :return: the created Edge :rtype: :class:`ppanggolin.pangenome.Edge` """ key = frozenset([gene1.family, gene2.family]) edge = self._edgeGetter.get(key) if edge is None: edge = Edge(gene1, gene2) self._edgeGetter[key] = edge else: edge.addGenes(gene1, gene2) return edge
def test_cstr_error(): o_src = Gene('source') o_tgt = Gene('target') # genes should have a family with pytest.raises(Exception): _ = Edge(o_src, o_tgt) o_family = GeneFamily(None, None) o_family.addGene(o_src) # both genes sould have a family with pytest.raises(Exception): _ = Edge(o_src, o_tgt) # gene should belong to the same organism o_family.addGene(o_tgt) o_src.fill_parents("", None) o_tgt.fill_parents(None, None) with pytest.raises(Exception): _ = Edge(o_src, o_tgt)
def test_cstr(): o_src = Gene('source') o_tgt = Gene('target') # set organism and contig to None. o_src.fill_parents(None, None) o_tgt.fill_parents(None, None) # define the None GeneFamily, and add the 2 genes to it. o_family = GeneFamily(None, None) o_family.addGene(o_src) o_family.addGene(o_tgt) o_edge = Edge(o_src, o_tgt) assert isinstance(o_edge, Edge) assert o_edge.source == o_src.family assert o_edge.target == o_tgt.family assert dict(o_edge.organisms) == {None: [(o_src, o_tgt)]}
def filled_edge(make_gene_pair): # Note that the same edge here links 4 families. p1 = make_gene_pair("org1", "s1", "t1") p2 = make_gene_pair("org1", "s2", "t1") p3 = make_gene_pair("org2", "s1", "t2") p4 = make_gene_pair("org2", "s1", "s2") # org1: s1,s2 -- t1 # org2: s1 -- t2,s2 o_edge = Edge(*p1) o_edge.addGenes(*p2) o_edge.addGenes(*p3) o_edge.addGenes(*p4) return o_edge
def test_addGenes(make_gene_pair): p1 = make_gene_pair("org1", "s1", "t1") p2 = make_gene_pair("org1", "s2", "t1") p3 = make_gene_pair("org2", "s1", "t2") p4 = make_gene_pair("org2", "s1", "s2") # org1: s1,s2 -- t1 # org2: s1 -- t2,s2 o_edge = Edge(*p1) o_edge.addGenes(*p2) o_edge.addGenes(*p3) o_edge.addGenes(*p4) assert set(o_edge.organisms.keys()) == set(["org1", "org2"]) assert o_edge.organisms["org1"] == [p1, p2] assert o_edge.organisms["org2"] == [p3, p4]
def test_genePairs(make_gene_pair): # cannot use filled_edge because i need access to pairs. p1 = make_gene_pair("org1", "s1", "t1") p2 = make_gene_pair("org1", "s2", "t1") p3 = make_gene_pair("org2", "s1", "t2") p4 = make_gene_pair("org2", "s1", "s2") # org1: s1,s2 -- t1 # org2: s1 -- t2,s2 o_edge = Edge(*p1) o_edge.addGenes(*p2) o_edge.addGenes(*p3) o_edge.addGenes(*p4) # 'set' because the order is not guaranted due to '.values()'. l_pairs = o_edge.genePairs assert set(l_pairs) == set([p1, p2, p3, p4])
def o_edge(make_gene_pair): p = make_gene_pair("org", "src", "tgt") return Edge(*p)