ragtag_patch.py
def build_aln_scaffold_graph(ctg_alns, components_fn, max_term_dist):
    """
    Build a directed scaffold graph from filtered alignments
    :param ctg_alns: query sequence -> ContigAlignment object
    :param components_fn: name of FASTA file with all relevant sequences
    :param max_term_dist: maximum alignment distance from a sequence terminus
    :return: PatchScaffoldGraph
    """
    sg = PatchScaffoldGraph(components_fn)

    for query_seq in ctg_alns:
        als = ctg_alns[query_seq]
        als.sort_by_query()
        last = None
        last_reversed = False

        # Iterate over each alignment for this query sequence
        for i in range(als.num_alns):
            cur_reversed = False

            # For each reference/query alignment terminus, determine if it is close to the sequence terminus
            ref_left_end, ref_right_end = als.ref_start_end(i, max_term_dist)
            query_left_end, query_right_end = als.query_start_end(i, max_term_dist)

            # Determine if we are reversing the reference sequence of this alignment
            if ref_left_end and ref_right_end:
                # Entire reference aligns - have to look at strand in alignment
                if als.strands[i] == '-':
                    cur_reversed = True
            elif ref_left_end:
                # Beginning of reference - we would expect suffix of read if same strand
                if query_left_end and query_right_end:
                    if als.strands[i] == '-':
                        cur_reversed = True
                elif query_left_end:
                    cur_reversed = True
            else:
                # End of contig - we would expect prefix of read if same strand
                if query_left_end and query_right_end:
                    if als.strands[i] == '-':
                        cur_reversed = True
                elif query_right_end:
                    cur_reversed = True

            if last is not None:
                if als.ref_headers[last] != als.ref_headers[i]:
                    my_query_end_offset = als.ref_lens[last] - als.ref_ends[last]
                    if last_reversed:
                        my_query_end_offset = als.ref_starts[last]

                    their_query_start_offset = als.ref_starts[i]
                    if cur_reversed:
                        their_query_start_offset = als.ref_lens[i] - als.ref_ends[i]

                    my_query_end = als.query_ends[last] + my_query_end_offset
                    their_query_start = als.query_starts[i] - their_query_start_offset
                    overlap = my_query_end - their_query_start

                    if overlap <= als.ref_lens[last] and overlap <= als.ref_lens[i]:

                        # Determine the scaffold graph nodes
                        u = als.ref_headers[last] + "_e"
                        v = als.ref_headers[i] + "_b"
                        if last_reversed:
                            u = als.ref_headers[last] + "_b"
                        if cur_reversed:
                            v = als.ref_headers[i] + "_e"

                        alignment = Alignment(
                            u,
                            v,
                            query_seq,
                            als.query_len,
                            my_query_end,
                            their_query_start,
                            0,  # Always on the query's forward strand
                            is_gap=False
                        )
                        sg.add_edge(u, v, alignment)

            last = i
            last_reversed = cur_reversed

    sg.remove_heavier_than(1)
    return sg

    Build a directed scaffold graph from filtered alignments
    :param ctg_alns: query sequence -> ContigAlignment object
    :param components_fn: name of FASTA file with all relevant sequences
    :param max_term_dist: maximum alignment distance from a sequence terminus
    :return: PatchScaffoldGraph
    
Build a directed scaffold graph from filtered alignments
Build a directed scaffold graph from filtered alignments
def build_aln_scaffold_graph(ctg_alns, components_fn, max_term_dist):
    sg = PatchScaffoldGraph(components_fn)
    for query_seq in ctg_alns:
        als = ctg_alns[query_seq]
        als.sort_by_query()
        last = None
        last_reversed = False
        for i in range(als.num_alns):
            cur_reversed = False
            ref_left_end, ref_right_end = als.ref_start_end(i, max_term_dist)
            query_left_end, query_right_end = als.query_start_end(i, max_term_dist)
            if ref_left_end and ref_right_end:
                if als.strands[i] == '-':
                    cur_reversed = True
            elif ref_left_end:
                if query_left_end and query_right_end:
                    if als.strands[i] == '-':
                        cur_reversed = True
                elif query_left_end:
                    cur_reversed = True
            else:
                if query_left_end and query_right_end:
                    if als.strands[i] == '-':
                        cur_reversed = True
                elif query_right_end:
                    cur_reversed = True
            if last is not None:
                if als.ref_headers[last] != als.ref_headers[i]:
                    my_query_end_offset = als.ref_lens[last] - als.ref_ends[last]
                    if last_reversed:
                        my_query_end_offset = als.ref_starts[last]
                    their_query_start_offset = als.ref_starts[i]
                    if cur_reversed:
                        their_query_start_offset = als.ref_lens[i] - als.ref_ends[i]
                    my_query_end = als.query_ends[last] + my_query_end_offset
                    their_query_start = als.query_starts[i] - their_query_start_offset
                    overlap = my_query_end - their_query_start
                    if overlap <= als.ref_lens[last] and overlap <= als.ref_lens[i]:
                        u = als.ref_headers[last] + "_e"
                        v = als.ref_headers[i] + "_b"
                        if last_reversed:
                            u = als.ref_headers[last] + "_b"
                        if cur_reversed:
                            v = als.ref_headers[i] + "_e"
                        alignment = Alignment(
                            u,
                            v,
                            query_seq,
                            als.query_len,
                            my_query_end,
                            their_query_start,
                            0,  
                            is_gap=False
                        )
                        sg.add_edge(u, v, alignment)
            last = i
            last_reversed = cur_reversed
    sg.remove_heavier_than(1)
    return sg
def build_aln_scaffold_graph ( ctg_alns , components_fn , max_term_dist ) : sg = PatchScaffoldGraph ( components_fn ) for query_seq in ctg_alns : als = ctg_alns [ query_seq ] als . sort_by_query ( ) last = None last_reversed = False for i in range ( als . num_alns ) : cur_reversed = False ref_left_end , ref_right_end = als . ref_start_end ( i , max_term_dist ) query_left_end , query_right_end = als . query_start_end ( i , max_term_dist ) if ref_left_end and ref_right_end : if als . strands [ i ] == '-' : cur_reversed = True elif ref_left_end : if query_left_end and query_right_end : if als . strands [ i ] == '-' : cur_reversed = True elif query_left_end : cur_reversed = True else : if query_left_end and query_right_end : if als . strands [ i ] == '-' : cur_reversed = True elif query_right_end : cur_reversed = True if last is not None : if als . ref_headers [ last ] != als . ref_headers [ i ] : my_query_end_offset = als . ref_lens [ last ] - als . ref_ends [ last ] if last_reversed : my_query_end_offset = als . ref_starts [ last ] their_query_start_offset = als . ref_starts [ i ] if cur_reversed : their_query_start_offset = als . ref_lens [ i ] - als . ref_ends [ i ] my_query_end = als . query_ends [ last ] + my_query_end_offset their_query_start = als . query_starts [ i ] - their_query_start_offset overlap = my_query_end - their_query_start if overlap <= als . ref_lens [ last ] and overlap <= als . ref_lens [ i ] : u = als . ref_headers [ last ] + "_e" v = als . ref_headers [ i ] + "_b" if last_reversed : u = als . ref_headers [ last ] + "_b" if cur_reversed : v = als . ref_headers [ i ] + "_e" alignment = Alignment ( u , v , query_seq , als . query_len , my_query_end , their_query_start , 0 , is_gap = False ) sg . add_edge ( u , v , alignment ) last = i last_reversed = cur_reversed sg . remove_heavier_than ( 1 ) return sg
Build a directed scaffold graph from filtered alignments
Build a directed scaffold graph from filtered alignments
"""
    Build a directed scaffold graph from filtered alignments
    :param ctg_alns: query sequence -> ContigAlignment object
    :param components_fn: name of FASTA file with all relevant sequences
    :param max_term_dist: maximum alignment distance from a sequence terminus
    :return: PatchScaffoldGraph
    """ # Iterate over each alignment for this query sequence # For each reference/query alignment terminus, determine if it is close to the sequence terminus # Determine if we are reversing the reference sequence of this alignment # Entire reference aligns - have to look at strand in alignment # Beginning of reference - we would expect suffix of read if same strand # End of contig - we would expect prefix of read if same strand # Determine the scaffold graph nodes # Always on the query's forward strand
None
ctg_alns
query sequence -> ContigAlignment object
query sequence - > ContigAlignment object
components_fn
name of FASTA file with all relevant sequences
name of FASTA file with all relevant sequences
max_term_dist
maximum alignment distance from a sequence terminus
maximum alignment distance from a sequence terminus