pytest_wdl/data_types/bam.py
def bam_to_sam(
    input_bam: Path,
    output_sam: Path,
    headers: Optional[Iterable[str]] = ("HD", "SQ", "RG"),
    min_mapq: Optional[int] = None,
    sorting: Sorting = Sorting.NONE
):
    """
    Use PySAM to convert bam to sam.
    """
    opts = []
    if headers:
        opts.append("-h")
        headers = set(headers)
    if min_mapq:
        opts.extend(["-q", str(min_mapq)])
    sam = pysam.view(*opts, str(input_bam)).rstrip()
    # Replace any randomly assigned readgroups with a common placeholder
    sam = re.sub(r"UNSET-\w*\b", "UNSET-placeholder", sam)

    lines = sam.splitlines(keepends=True)
    header_lines = []
    start = 0
    if headers:
        for i, line in enumerate(lines):
            if not line.startswith("@"):
                start = i
                break
            elif line[1:3] in headers:
                header_lines.append(line)

    body_lines = lines[start:]
    if sorting is not Sorting.NONE:
        with tempdir() as temp:
            temp_sam = temp / f"output_{str(output_sam.stem)}.sam"
            with open(temp_sam, "w") as out:
                out.write("".join(body_lines))
            if sorting is Sorting.COORDINATE:
                sort_cols = "-k3,3 -k4,4n -k2,2n"
            else:
                sort_cols = "-k1,1 -k2,2n"
            sorted_sam = subby.sub(f"cat {str(temp_sam)} | sort {sort_cols}")
            body_lines = [sorted_sam]

    with open(output_sam, "w") as out:
        out.write("".join(header_lines + body_lines))

    Use PySAM to convert bam to sam.
    
Use PySAM to convert bam to sam.
Use PySAM to convert bam to sam .
def bam_to_sam(
    input_bam: Path,
    output_sam: Path,
    headers: Optional[Iterable[str]] = ("HD", "SQ", "RG"),
    min_mapq: Optional[int] = None,
    sorting: Sorting = Sorting.NONE
):
    opts = []
    if headers:
        opts.append("-h")
        headers = set(headers)
    if min_mapq:
        opts.extend(["-q", str(min_mapq)])
    sam = pysam.view(*opts, str(input_bam)).rstrip()
    sam = re.sub(r"UNSET-\w*\b", "UNSET-placeholder", sam)
    lines = sam.splitlines(keepends=True)
    header_lines = []
    start = 0
    if headers:
        for i, line in enumerate(lines):
            if not line.startswith("@"):
                start = i
                break
            elif line[1:3] in headers:
                header_lines.append(line)
    body_lines = lines[start:]
    if sorting is not Sorting.NONE:
        with tempdir() as temp:
            temp_sam = temp / f"output_{str(output_sam.stem)}.sam"
            with open(temp_sam, "w") as out:
                out.write("".join(body_lines))
            if sorting is Sorting.COORDINATE:
                sort_cols = "-k3,3 -k4,4n -k2,2n"
            else:
                sort_cols = "-k1,1 -k2,2n"
            sorted_sam = subby.sub(f"cat {str(temp_sam)} | sort {sort_cols}")
            body_lines = [sorted_sam]
    with open(output_sam, "w") as out:
        out.write("".join(header_lines + body_lines))
def bam_to_sam ( input_bam : Path , output_sam : Path , headers : Optional [ Iterable [ str ] ] = ( "HD" , "SQ" , "RG" ) , min_mapq : Optional [ int ] = None , sorting : Sorting = Sorting . NONE ) : opts = [ ] if headers : opts . append ( "-h" ) headers = set ( headers ) if min_mapq : opts . extend ( [ "-q" , str ( min_mapq ) ] ) sam = pysam . view ( * opts , str ( input_bam ) ) . rstrip ( ) sam = re . sub ( r"UNSET-\w*\b" , "UNSET-placeholder" , sam ) lines = sam . splitlines ( keepends = True ) header_lines = [ ] start = 0 if headers : for i , line in enumerate ( lines ) : if not line . startswith ( "@" ) : start = i break elif line [ 1 : 3 ] in headers : header_lines . append ( line ) body_lines = lines [ start : ] if sorting is not Sorting . NONE : with tempdir ( ) as temp : temp_sam = temp / f"output_{str(output_sam.stem)}.sam" with open ( temp_sam , "w" ) as out : out . write ( "" . join ( body_lines ) ) if sorting is Sorting . COORDINATE : sort_cols = "-k3,3 -k4,4n -k2,2n" else : sort_cols = "-k1,1 -k2,2n" sorted_sam = subby . sub ( f"cat {str(temp_sam)} | sort {sort_cols}" ) body_lines = [ sorted_sam ] with open ( output_sam , "w" ) as out : out . write ( "" . join ( header_lines + body_lines ) )
Use PySAM to convert bam to sam.
Use PySAM to convert bam to sam .
"""
    Use PySAM to convert bam to sam.
    """ # Replace any randomly assigned readgroups with a common placeholder
input_bam
Path
output_sam
Path
headers
Optional[Iterable[str]]
min_mapq
Optional[int]
sorting
Sorting