Exemple #1
0
def combine_nodes(graph: GraphContainer):
    """
    Combine adjacent nodes with the same sequence labels
    """
    for n1 in list(graph.nodes.values()):
        if len(list(graph.outEdges(n1))) != 1:
            continue  # Pair of nodes with no other in/out edges
        n2 = graph.nodes[next(graph.outEdges(n1))["to"]]
        if len(list(graph.inEdges(n2))) != 1:
            continue
        if not (n1["chrom"] == n2["chrom"] and n1["end"] + 1 == n2["start"]):
            continue  # nodes must be adjacent
        haplos = n1["sequences"]
        if n2["sequences"] != haplos:
            continue  # only collapse nodes with same haplotypes
        if "reference" in n1:
            if "reference" not in n2:
                continue  # nodes must be of same type
            node = graph.add_refNode(n1["chrom"], n1["start"], n2["end"], haplos)
        else:
            if "reference" in n2:
                continue  # nodes must be of same type
            node = graph.add_altNode(
                n1["chrom"], n1["start"], n2["end"], n1["sequence"] + n2["sequence"], haplos)
        logging.info("Combinding %s and %s", n1['name'], n2['name'])
        for e in list(graph.inEdges(n1)):
            graph.add_edge(graph.nodes[e["from"]], node, e["sequences"])
        for e in list(graph.outEdges(n2)):
            graph.add_edge(node, graph.nodes[e["to"]], e["sequences"])
        graph.del_node(n1)
        graph.del_node(n2)
Exemple #2
0
def add_source_sink(graph: GraphContainer,
                    source_name="source",
                    sink_name="sink"):
    """
    add source and sink if necessary and link to exising nodes without in/out edges
    :param graph: graph to work on
    :param source_name: name of source node
    :param sink_name: name of sink node
    """
    if source_name not in graph.nodes:
        graph.nodes[source_name] = {
            "name": source_name,
            "sequence": "N" * 10
        }
    if sink_name not in graph.nodes:
        graph.nodes[sink_name] = {
            "name": sink_name,
            "sequence": "N" * 10
        }
    # Link nodes without incoming/outgoing edges to source/sink
    for node in graph.nodes.values():
        if node["name"] in [source_name, sink_name]:
            continue
        if not any(graph.inEdges(node)):
            logging.info("Linking %s from source", node['name'])
            graph.add_edge(graph.nodes[source_name], node)
        if not any(graph.outEdges(node)):
            logging.info("Linking %s to sink", node['name'])
            graph.add_edge(node, graph.nodes[sink_name])
Exemple #3
0
def split_alt_nodes(graph: GraphContainer, max_len=300, padding_len=150):
    """
    Split long alternate nodes
    :param graph: graph to work on
    :param max_len: max length of reference node with no sequences
    :param padding_len: length of sequence to keep
    """
    assert max_len >= 2 * padding_len
    for node in list(graph.altNodes()):
        if len(node["sequence"]) <= max_len:
            continue
        logging.info(f"Splitting long ALT node: {node['name']}")

        n1 = graph.add_altNode(node["chrom"], node["start"], node["end"],
                               node["sequence"][:padding_len],
                               node["sequences"])
        n2 = graph.add_altNode(node["chrom"], node["start"], node["end"],
                               node["sequence"][-padding_len:],
                               node["sequences"])

        for e in list(graph.inEdges(node)):
            graph.add_edge(graph.nodes[e["from"]], n1, e["sequences"])
        for e in list(graph.outEdges(node)):
            graph.add_edge(n2, graph.nodes[e["to"]], e["sequences"])
        graph.del_node(node)
Exemple #4
0
def remove_empty_nodes(graph: GraphContainer):
    """
    Remove nodes without sequence (from deletions / skipped insertions or split ref nodes)
    Merge in & out edge pairs to keep connections
    """
    for node in list(graph.nodes.values()):
        if (("reference" in node and node["start"] <= node["end"]) or
                node.get("sequence", "") != ""):
            continue
        logging.info("Removing empty node %s", node['name'])
        inSeqs = [s for e in graph.inEdges(node) for s in e["sequences"]]
        outSeqs = [s for e in graph.outEdges(node) for s in e["sequences"]]
        for e1 in list(graph.inEdges(node)):
            for e2 in list(graph.outEdges(node)):
                # Label the new edges with sequence labels either observed
                # on both merged in- and out-edge or on an in (out) -edge only
                # if the label is undetermined goung out (in)
                haplos = e1["sequences"].intersection(e2["sequences"]).union(
                    e1["sequences"].difference(outSeqs).union(
                        e2["sequences"].difference(inSeqs)))
                graph.add_edge(graph.nodes[e1["from"]], graph.nodes[e2["to"]], haplos)
        graph.del_node(node)
Exemple #5
0
def split_node(graph: GraphContainer, node, breakpoints):
    """
    Split a node at a set of breakpoints and link new (sub-)nodes
    Used to link to new variant nodes later
    Modifies graph and deletes node after splitting
    :returns Created sub-nodes
    """
    if not breakpoints:
        return node
    breakpoints = sorted(set(breakpoints))
    logging.debug(f"Splitting {node['name']} at {breakpoints}")
    nodes = []
    lEnd = 0
    for p in breakpoints:
        assert 0 <= p <= node["end"] - node["start"] + 1
        nStart = node["start"] + lEnd
        nEnd = node["start"] + p - 1
        if "reference" in node:
            nodes.append(
                graph.add_refNode(node["chrom"], nStart, nEnd,
                                  node["sequences"]))
        else:
            seq = node["sequence"][lEnd:p]
            nodes.append(
                graph.add_altNode(node["chrom"], nStart, nEnd, seq,
                                  node["sequences"]))
        lEnd = p
    # Add last node
    lStart = node["start"] + breakpoints[-1]
    if "reference" in node:
        nodes.append(
            graph.add_refNode(node["chrom"], lStart, node["end"],
                              node["sequences"]))
    else:
        seq = node["sequence"][breakpoints[-1]:]
        nodes.append(
            graph.add_altNode(node["chrom"], lStart, node["end"], seq,
                              node["sequences"]))
    # Connect nodes
    for e in graph.inEdges(node):
        graph.add_edge(graph.nodes[e["from"]], nodes[0], e["sequences"])
    for e in graph.outEdges(node):
        graph.add_edge(nodes[-1], graph.nodes[e["to"]], e["sequences"])
    for (n1, n2) in zip(nodes[:-1], nodes[1:]):
        graph.add_edge(n1, n2)
    # Delete original node, unless identical to new node (no split)
    if node['name'] not in [n['name'] for n in nodes]:
        graph.del_node(node)
    return nodes
Exemple #6
0
def split_ref_nodes(graph: GraphContainer, max_len=300, padding_len=150):
    """
    Split long reference nodes
    :param graph: graph to work on
    :param max_len: max length of reference node with no sequences
    :param padding_len: length of sequence to keep
    """
    assert max_len >= 2 * padding_len
    for node in list(graph.refNodes()):
        if node["end"] - node["start"] + 1 <= max_len:
            continue
        logging.info("Splitting long REF node: %s", node['name'])
        firstEnd = node["start"] + padding_len - 1
        n1 = graph.add_refNode(node["chrom"], node["start"], firstEnd, node["sequences"])
        sndStart = node["end"] - padding_len + 1
        n2 = graph.add_refNode(node["chrom"], sndStart, node["end"], node["sequences"])

        for e in list(graph.inEdges(node)):
            graph.add_edge(graph.nodes[e["from"]], n1, e["sequences"])
        for e in list(graph.outEdges(node)):
            graph.add_edge(n2, graph.nodes[e["to"]], e["sequences"])
        graph.del_node(node)
Exemple #7
0
    def get_graph(self, allele_graph=False):
        """ Create the paragraph representation of nodes and edges for this graph
        :param alleleGraph: create edges between any compatible allele pair (rather
                            than just following reference and given haplotypes)
        :return GraphContainer object
        """
        logging.info("Creating output graph")
        graph = GraphContainer()
        # create ref nodes
        pnode = None
        for ref in self.get_ref_alleles():
            node = graph.add_refNode(self.chrom, ref.begin, ref.end - 1,
                                     ref.data.haplotypes)
            if pnode:
                assert pnode["end"] + 1 == node["start"]
                graph.add_edge(pnode, node)
            pnode = node
        # Create alt nodes
        for alt in self.alts.values():
            graph.add_altNode(self.chrom, alt.start, alt.end, alt.sequence,
                              alt.haplotypes)

        # Create edges connecting nodes along a haplotype (or allele in alleleGraph mode)
        for haplo in self.get_haplotypes():
            nodes = graph.nodes_by_haplo(haplo)
            logging.info(
                f"Linking nodes in sequence {haplo}:\t{', '.join(n['name'] for n in nodes)}"
            )
            pnode = None
            for node in nodes:
                if pnode:
                    if pnode["end"] == node["start"] - 1:
                        graph.add_edge(pnode, node, [haplo])
                    pnode_is_ref_dummy = pnode[
                        "end"] == pnode["start"] - 1 and not pnode["sequence"]
                    pnode_ends_before_node = pnode["end"] < node[
                        "start"] and pnode["start"] < node["start"]
                    if not pnode_is_ref_dummy and not pnode_ends_before_node:
                        raise Exception(
                            f"Inconsistent nodes for haplotype {haplo}: {pnode['name']}, {node['name']}"
                        )
                pnode = node

        # In alleleGraph mode link each alt node to all neighboring nodes
        # In haplotype mode link nodes without in/out edges to reference
        for node in graph.altNodes():
            if allele_graph or not any(graph.inEdges(node)):
                graph.add_edge(
                    graph.refNode_ending_at[node["chrom"], node["start"] - 1],
                    node)
            if not any(graph.outEdges(node)):
                graph.add_edge(
                    node, graph.refNode_starting_at[node["chrom"],
                                                    node["end"] + 1])
            if allele_graph:
                isInsertion = node["end"] < node["start"]
                for n in graph.nodes_starting_at[node["end"] + 1]:
                    # Don't loop by connecting multiple insertions at the same position
                    if not (isInsertion and n["end"] < n["start"]):
                        graph.add_edge(node, n)

        # For nodes that do not have determined in/out edges for a given haplotype
        # label all in/out edges as compatible with that haplotype
        # excluding edges that connect to another allele at the same vcfVariant (e.g. insertions)
        for haplo in self.get_haplotypes():
            for node in graph.nodes_by_haplo(haplo):
                if not any(graph.inEdges(node, haplo)):
                    for e in graph.inEdges(node):
                        graph.add_edge(graph.nodes[e["from"]], node, [haplo])
                assert any(graph.inEdges(node, haplo))
                if not any(graph.outEdges(node, haplo)):
                    for e in graph.outEdges(node):
                        graph.add_edge(node, graph.nodes[e["to"]], [haplo])
        return graph