ragtag_utilities/ScaffoldGraph.py
def write_agp(self, agp_fn, ref_fn, add_suffix_to_unplaced=False):
        """
        Write the AGP file implied by the scaffold graph
        :param agp_fn: AGP file name
        :param ref_fn: reference FASTA file name
        :param add_suffix_to_unplaced: add "_RagTag" to unscaffolded sequences
        """
        used_components = set()
        used_edges = set()
        obj_header_idx = -1

        agp = AGPFile(agp_fn, "w")
        agp.add_pragma()
        agp.add_comment("# AGP created by RagTag {}".format(get_ragtag_version()))

        while True:
            # Find a starting node
            from_node = None
            to_node = None
            cur_ref = None
            for u, v in sorted(self.edges):
                if (u, v) not in used_edges:
                    u_base = u[:-2]

                    u_degree = 0
                    if u_base + "_b" in self.nodes:
                        u_degree += self.graph.degree[u_base + "_b"]
                    if u_base + "_e" in self.nodes:
                        u_degree += self.graph.degree[u_base + "_e"]

                    assert u_degree in {2, 4}

                    # Check if we have found a starting target sequence
                    if u_degree == 2:
                        cur_ref = u_base
                        from_node = u
                        to_node = v
                        used_edges.add((u, v))
                        used_edges.add((v, u))
                        break

            # If we haven't found a new starting target sequence, we are done
            if from_node is None:
                break

            # Initialize this object
            obj_header_idx += 1
            obj_header = "scf" + "{0:08}".format(obj_header_idx)
            obj_pos = 0
            obj_pid = 1

            # Process the first target sequence
            cur_ref_len = self.component_lens[cur_ref]
            cur_ref_strand = "+"
            if from_node.endswith("_b"):
                cur_ref_strand = "-"
            agp.add_seq_line(obj_header, obj_pos+1, obj_pos+cur_ref_len, obj_pid, "W", cur_ref, 1, cur_ref_len, cur_ref_strand)
            obj_pos += cur_ref_len
            obj_pid += 1
            used_components.add(cur_ref)

            # Process the remaining sequences.
            next_edge_exists = True
            while next_edge_exists:
                # Process the patch
                patch_aln = self.graph[from_node][to_node]["alignment"]
                patch_query = patch_aln.query
                patch_strand = "+"
                if patch_aln.strand:
                    patch_strand = "-"

                patch_len = patch_aln.their_query_start - patch_aln.my_query_end
                if patch_len > 0:
                    if patch_aln.is_gap:
                        agp.add_gap_line(obj_header, obj_pos+1, obj_pos+patch_len, obj_pid, "N", patch_len, "scaffold", "yes", "align_genus")
                    else:
                        agp.add_seq_line(obj_header, obj_pos+1, obj_pos+patch_len, obj_pid, "W", patch_query, patch_aln.my_query_end+1, patch_aln.their_query_start, patch_strand)
                        used_components.add(patch_query)
                    obj_pos += patch_len
                    obj_pid += 1

                # Next, process the reference sequence
                comp_start = min(0, patch_len)
                cur_ref = to_node[:-2]
                cur_ref_len = self.component_lens[cur_ref]
                cur_ref_strand = "+"
                if to_node.endswith("_e"):
                    cur_ref_strand = "-"
                agp.add_seq_line(obj_header, obj_pos+1, obj_pos+(cur_ref_len + comp_start), obj_pid, "W", cur_ref, 1+(-1*comp_start), cur_ref_len, cur_ref_strand)
                obj_pos += cur_ref_len + comp_start
                obj_pid += 1
                used_components.add(cur_ref)

                # Look for the next edge
                from_node = to_node[:-2] + "_b"
                if to_node.endswith("_b"):
                    from_node = to_node[:-2] + "_e"

                if from_node in self.graph.nodes:
                    next_nodes = set(self.graph[from_node])
                    assert len(next_nodes) == 1
                    to_node = next_nodes.pop()
                    used_edges.add((from_node, to_node))
                    used_edges.add((to_node, from_node))
                else:
                    next_edge_exists = False

        # Write unplaced reference sequences
        fai = pysam.FastaFile(ref_fn)
        all_ref_seqs = set(fai.references)
        fai.close()
        remaining_components = all_ref_seqs - used_components
        for c in sorted(remaining_components):
            agp.add_seq_line(
                c + "_RagTag" * add_suffix_to_unplaced,
                "1",
                str(self.component_lens[c]),
                "1",
                "W",
                c,
                "1",
                str(self.component_lens[c]),
                "+"
            )

        agp.write()

        Write the AGP file implied by the scaffold graph
        :param agp_fn: AGP file name
        :param ref_fn: reference FASTA file name
        :param add_suffix_to_unplaced: add "_RagTag" to unscaffolded sequences
        
Write the AGP file implied by the scaffold graph
Write the AGP file implied by the scaffold graph
def write_agp(self, agp_fn, ref_fn, add_suffix_to_unplaced=False):
        used_components = set()
        used_edges = set()
        obj_header_idx = -1
        agp = AGPFile(agp_fn, "w")
        agp.add_pragma()
        agp.add_comment("# AGP created by RagTag {}".format(get_ragtag_version()))
        while True:
            from_node = None
            to_node = None
            cur_ref = None
            for u, v in sorted(self.edges):
                if (u, v) not in used_edges:
                    u_base = u[:-2]
                    u_degree = 0
                    if u_base + "_b" in self.nodes:
                        u_degree += self.graph.degree[u_base + "_b"]
                    if u_base + "_e" in self.nodes:
                        u_degree += self.graph.degree[u_base + "_e"]
                    assert u_degree in {2, 4}
                    if u_degree == 2:
                        cur_ref = u_base
                        from_node = u
                        to_node = v
                        used_edges.add((u, v))
                        used_edges.add((v, u))
                        break
            if from_node is None:
                break
            obj_header_idx += 1
            obj_header = "scf" + "{0:08}".format(obj_header_idx)
            obj_pos = 0
            obj_pid = 1
            cur_ref_len = self.component_lens[cur_ref]
            cur_ref_strand = "+"
            if from_node.endswith("_b"):
                cur_ref_strand = "-"
            agp.add_seq_line(obj_header, obj_pos+1, obj_pos+cur_ref_len, obj_pid, "W", cur_ref, 1, cur_ref_len, cur_ref_strand)
            obj_pos += cur_ref_len
            obj_pid += 1
            used_components.add(cur_ref)
            next_edge_exists = True
            while next_edge_exists:
                patch_aln = self.graph[from_node][to_node]["alignment"]
                patch_query = patch_aln.query
                patch_strand = "+"
                if patch_aln.strand:
                    patch_strand = "-"
                patch_len = patch_aln.their_query_start - patch_aln.my_query_end
                if patch_len > 0:
                    if patch_aln.is_gap:
                        agp.add_gap_line(obj_header, obj_pos+1, obj_pos+patch_len, obj_pid, "N", patch_len, "scaffold", "yes", "align_genus")
                    else:
                        agp.add_seq_line(obj_header, obj_pos+1, obj_pos+patch_len, obj_pid, "W", patch_query, patch_aln.my_query_end+1, patch_aln.their_query_start, patch_strand)
                        used_components.add(patch_query)
                    obj_pos += patch_len
                    obj_pid += 1
                comp_start = min(0, patch_len)
                cur_ref = to_node[:-2]
                cur_ref_len = self.component_lens[cur_ref]
                cur_ref_strand = "+"
                if to_node.endswith("_e"):
                    cur_ref_strand = "-"
                agp.add_seq_line(obj_header, obj_pos+1, obj_pos+(cur_ref_len + comp_start), obj_pid, "W", cur_ref, 1+(-1*comp_start), cur_ref_len, cur_ref_strand)
                obj_pos += cur_ref_len + comp_start
                obj_pid += 1
                used_components.add(cur_ref)
                from_node = to_node[:-2] + "_b"
                if to_node.endswith("_b"):
                    from_node = to_node[:-2] + "_e"
                if from_node in self.graph.nodes:
                    next_nodes = set(self.graph[from_node])
                    assert len(next_nodes) == 1
                    to_node = next_nodes.pop()
                    used_edges.add((from_node, to_node))
                    used_edges.add((to_node, from_node))
                else:
                    next_edge_exists = False
        fai = pysam.FastaFile(ref_fn)
        all_ref_seqs = set(fai.references)
        fai.close()
        remaining_components = all_ref_seqs - used_components
        for c in sorted(remaining_components):
            agp.add_seq_line(
                c + "_RagTag" * add_suffix_to_unplaced,
                "1",
                str(self.component_lens[c]),
                "1",
                "W",
                c,
                "1",
                str(self.component_lens[c]),
                "+"
            )
        agp.write()
def write_agp ( self , agp_fn , ref_fn , add_suffix_to_unplaced = False ) : used_components = set ( ) used_edges = set ( ) obj_header_idx = - 1 agp = AGPFile ( agp_fn , "w" ) agp . add_pragma ( ) agp . add_comment ( "# AGP created by RagTag {}" . format ( get_ragtag_version ( ) ) ) while True : from_node = None to_node = None cur_ref = None for u , v in sorted ( self . edges ) : if ( u , v ) not in used_edges : u_base = u [ : - 2 ] u_degree = 0 if u_base + "_b" in self . nodes : u_degree += self . graph . degree [ u_base + "_b" ] if u_base + "_e" in self . nodes : u_degree += self . graph . degree [ u_base + "_e" ] assert u_degree in { 2 , 4 } if u_degree == 2 : cur_ref = u_base from_node = u to_node = v used_edges . add ( ( u , v ) ) used_edges . add ( ( v , u ) ) break if from_node is None : break obj_header_idx += 1 obj_header = "scf" + "{0:08}" . format ( obj_header_idx ) obj_pos = 0 obj_pid = 1 cur_ref_len = self . component_lens [ cur_ref ] cur_ref_strand = "+" if from_node . endswith ( "_b" ) : cur_ref_strand = "-" agp . add_seq_line ( obj_header , obj_pos + 1 , obj_pos + cur_ref_len , obj_pid , "W" , cur_ref , 1 , cur_ref_len , cur_ref_strand ) obj_pos += cur_ref_len obj_pid += 1 used_components . add ( cur_ref ) next_edge_exists = True while next_edge_exists : patch_aln = self . graph [ from_node ] [ to_node ] [ "alignment" ] patch_query = patch_aln . query patch_strand = "+" if patch_aln . strand : patch_strand = "-" patch_len = patch_aln . their_query_start - patch_aln . my_query_end if patch_len > 0 : if patch_aln . is_gap : agp . add_gap_line ( obj_header , obj_pos + 1 , obj_pos + patch_len , obj_pid , "N" , patch_len , "scaffold" , "yes" , "align_genus" ) else : agp . add_seq_line ( obj_header , obj_pos + 1 , obj_pos + patch_len , obj_pid , "W" , patch_query , patch_aln . my_query_end + 1 , patch_aln . their_query_start , patch_strand ) used_components . add ( patch_query ) obj_pos += patch_len obj_pid += 1 comp_start = min ( 0 , patch_len ) cur_ref = to_node [ : - 2 ] cur_ref_len = self . component_lens [ cur_ref ] cur_ref_strand = "+" if to_node . endswith ( "_e" ) : cur_ref_strand = "-" agp . add_seq_line ( obj_header , obj_pos + 1 , obj_pos + ( cur_ref_len + comp_start ) , obj_pid , "W" , cur_ref , 1 + ( - 1 * comp_start ) , cur_ref_len , cur_ref_strand ) obj_pos += cur_ref_len + comp_start obj_pid += 1 used_components . add ( cur_ref ) from_node = to_node [ : - 2 ] + "_b" if to_node . endswith ( "_b" ) : from_node = to_node [ : - 2 ] + "_e" if from_node in self . graph . nodes : next_nodes = set ( self . graph [ from_node ] ) assert len ( next_nodes ) == 1 to_node = next_nodes . pop ( ) used_edges . add ( ( from_node , to_node ) ) used_edges . add ( ( to_node , from_node ) ) else : next_edge_exists = False fai = pysam . FastaFile ( ref_fn ) all_ref_seqs = set ( fai . references ) fai . close ( ) remaining_components = all_ref_seqs - used_components for c in sorted ( remaining_components ) : agp . add_seq_line ( c + "_RagTag" * add_suffix_to_unplaced , "1" , str ( self . component_lens [ c ] ) , "1" , "W" , c , "1" , str ( self . component_lens [ c ] ) , "+" ) agp . write ( )
Write the AGP file implied by the scaffold graph
Write the AGP file implied by the scaffold graph
"""
        Write the AGP file implied by the scaffold graph
        :param agp_fn: AGP file name
        :param ref_fn: reference FASTA file name
        :param add_suffix_to_unplaced: add "_RagTag" to unscaffolded sequences
        """ # Find a starting node # Check if we have found a starting target sequence # If we haven't found a new starting target sequence, we are done # Initialize this object # Process the first target sequence # Process the remaining sequences. # Process the patch # Next, process the reference sequence # Look for the next edge # Write unplaced reference sequences
self
agp_fn
AGP file name
AGP file name
ref_fn
reference FASTA file name
reference FASTA file name
add_suffix_to_unplaced
add "_RagTag" to unscaffolded sequences
add " _RagTag " to unscaffolded sequences