<commit_before>#!/usr/bin/env python

import logging
import os
import subprocess
import pysam
import preprocess_and_align
import breakseq_core
import breakseq_post
import compute_zygosity
import gen_vcf
import breakseq_index
from _version import __version__


def add_options(main_parser):
    preprocess_and_align.add_options(main_parser)
    breakseq_core.add_options(main_parser)
    breakseq_post.add_options(main_parser)
    compute_zygosity.add_options(main_parser)
    gen_vcf.add_options(main_parser)
    breakseq_index.add_options(main_parser)

    main_parser.add_argument("--nthreads", help="Number of processes to use for parallelism", type=int, default=1)
    main_parser.add_argument("--bams", help="Alignment BAMs", nargs="+", required=True, default=[])
    main_parser.add_argument("--work", help="Working directory", default="work")
    main_parser.add_argument("--chromosomes", nargs="+", help="List of chromosomes to process", default=[])
    main_parser.add_argument("--reference", help="Reference FASTA", required=True)
    main_parser.add_argument("--sample", help="Sample name. Leave unspecified to infer sample name from BAMs.")
    main_parser.add_argument("--keep_temp", help="Keep temporary files", action="store_true")
    main_parser.add_argument('--version', action='version', version='%(prog)s ' + __version__)


def infer_sample(bam):
    samfile = pysam.Samfile(bam, "rb")
    if "RG" not in samfile.header:
        raise Exception("Unable to infer sample name from %s since RG is missing" % bam)
    samples = list(set([item["SM"] for item in samfile.header["RG"]]))
    if len(samples) > 1:
        raise Exception("Multiple samples found: %s" % (", ".join(samples)))
    samfile.close()
    return samples[0]


def get_reference_contigs(reference):
    func_logger = logging.getLogger(get_reference_contigs.__name__)
    fai = os.path.join(reference, ".fai")
    contigs = []
    func_logger.info("Extracting chromosome names from %s" % fai)
    with open(fai, "w") as fai_fd:
        contigs = [line.split()[0] for line in fai_fd]
    return contigs


def breakseq2_workflow(sample=None, bplib=None, bplib_gff=None, bwa=None, samtools=None, bams=[], work="work", chromosomes=[],
                       nthreads=1, min_span=breakseq_core.DEFAULT_MIN_SPAN,
                       min_overlap=compute_zygosity.DEFAULT_MIN_OVERLAP, reference=None, keep_temp=False, window=compute_zygosity.DEFAULT_WINDOW, junction_length=breakseq_index.DEFAULT_JUNCTION_LENGTH):
    func_logger = logging.getLogger(breakseq2_workflow.__name__)

    bams = [os.path.abspath(bam) for bam in bams]

    if not bams:
        func_logger.error("No BAMs specified so nothing to do")
        return 1

    if not sample:
        sample = infer_sample(bams[0])

    if not os.path.isdir(work):
        func_logger.info("Created working directory %s" % work)
        os.makedirs(work)

    if not bplib and not bplib_gff:
        func_logger.error("Atleast one of the breakpoint FASTA or GFF must be specified")
        return 1<commit_msg>Remove incorrect join<commit_after>#!/usr/bin/env python

import logging
import os
import subprocess
import pysam
import preprocess_and_align
import breakseq_core
import breakseq_post
import compute_zygosity
import gen_vcf
import breakseq_index
from _version import __version__


def add_options(main_parser):
    preprocess_and_align.add_options(main_parser)
    breakseq_core.add_options(main_parser)
    breakseq_post.add_options(main_parser)
    compute_zygosity.add_options(main_parser)
    gen_vcf.add_options(main_parser)
    breakseq_index.add_options(main_parser)

    main_parser.add_argument("--nthreads", help="Number of processes to use for parallelism", type=int, default=1)
    main_parser.add_argument("--bams", help="Alignment BAMs", nargs="+", required=True, default=[])
    main_parser.add_argument("--work", help="Working directory", default="work")
    main_parser.add_argument("--chromosomes", nargs="+", help="List of chromosomes to process", default=[])
    main_parser.add_argument("--reference", help="Reference FASTA", required=True)
    main_parser.add_argument("--sample", help="Sample name. Leave unspecified to infer sample name from BAMs.")
    main_parser.add_argument("--keep_temp", help="Keep temporary files", action="store_true")
    main_parser.add_argument('--version', action='version', version='%(prog)s ' + __version__)


def infer_sample(bam):
    samfile = pysam.Samfile(bam, "rb")
    if "RG" not in samfile.header:
        raise Exception("Unable to infer sample name from %s since RG is missing" % bam)
    samples = list(set([item["SM"] for item in samfile.header["RG"]]))
    if len(samples) > 1:
        raise Exception("Multiple samples found: %s" % (", ".join(samples)))
    samfile.close()
    return samples[0]


def get_reference_contigs(reference):
    func_logger = logging.getLogger(get_reference_contigs.__name__)
    fai = "%s.fai" % reference
    contigs = []
    func_logger.info("Extracting chromosome names from %s" % fai)
    with open(fai, "w") as fai_fd:
        contigs = [line.split()[0] for line in fai_fd]
    return contigs


def breakseq2_workflow(sample=None, bplib=None, bplib_gff=None, bwa=None, samtools=None, bams=[], work="work", chromosomes=[],
                       nthreads=1, min_span=breakseq_core.DEFAULT_MIN_SPAN,
                       min_overlap=compute_zygosity.DEFAULT_MIN_OVERLAP, reference=None, keep_temp=False, window=compute_zygosity.DEFAULT_WINDOW, junction_length=breakseq_index.DEFAULT_JUNCTION_LENGTH):
    func_logger = logging.getLogger(breakseq2_workflow.__name__)

    bams = [os.path.abspath(bam) for bam in bams]

    if not bams:
        func_logger.error("No BAMs specified so nothing to do")
        return 1

    if not sample:
        sample = infer_sample(bams[0])

    if not os.path.isdir(work):
        func_logger.info("Created working directory %s" % work)
        os.makedirs(work)

    if not bplib and not bplib_gff:
        func_logger.error("Atleast one of the breakpoint FASTA or GFF must be specified")
        return 1
{'fraction_non_alphanumeric': 0.11410277458545395, 'fraction_numerical': 0.003283533081595797, 'mean_word_length': 4.334500875656743, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4118295', 'n_tokens_mistral': 1995, 'n_tokens_neox': 1930, 'n_words': 447}