Example #1
0
def clinker(
    files,
    identity=0.3,
    delimiter=None,
    decimals=2,
    plot=None,
    output=None,
    force=False,
    hide_link_headers=False,
    hide_alignment_headers=False,
):
    """Entry point for running the script."""
    LOG.info("Starting clinker")

    # Check output file before doing anything else
    if output and Path(output).exists() and not force:
        LOG.error(
            f"File {output} already exists but --force not specified, exiting")
        return

    # Parse files, generate objects
    paths = find_files(files)
    LOG.info("Parsing GenBank files: %s", paths)
    clusters = parse_files(paths)

    # Align all clusters
    if len(clusters) == 1:
        globaligner = align.align_clusters(clusters[0])
    else:
        LOG.info("Starting cluster alignments")
        globaligner = align.align_clusters(*clusters, cutoff=identity)

        LOG.info("Generating results summary...")
        summary = globaligner.format(
            delimiter=delimiter,
            decimals=decimals,
            link_headers=not hide_link_headers,
            alignment_headers=not hide_alignment_headers,
        )
        if output:
            LOG.info(f"Writing alignments to {output}")
            with open(output, "w") as fp:
                fp.write(summary)
        else:
            print(summary)

    # Generate the SVG
    if plot:
        LOG.info("Building clustermap.js visualisation")
        if isinstance(plot, str):
            LOG.info("Writing to: %s", plot)
        plot_clusters(globaligner, output=None if plot is True else plot)

    LOG.info("Done!")
    return globaligner
Example #2
0
def clinker(
    files,
    session=None,
    identity=0.3,
    delimiter=None,
    decimals=2,
    plot=None,
    output=None,
    force=False,
    no_align=False,
    hide_link_headers=False,
    hide_alignment_headers=False,
    use_file_order=False,
    json_indent=None,
    jobs=None,
):
    """Entry point for running the script."""
    LOG.info("Starting clinker")

    load_session = session and Path(session).exists()

    if load_session:
        LOG.info("Loading session from: %s", session)
        with open(session) as fp:
            globaligner = align.Globaligner.from_json(fp)
        if files:
            paths = find_files(files)
            if not paths:
                LOG.error("No files found")
                raise SystemExit
            LOG.info("Parsing GenBank files: %s", paths)
            clusters = parse_files(paths)

            LOG.info("Adding clusters to loaded session and aligning")
            globaligner.add_clusters(*clusters)
            globaligner.align_stored_clusters(cutoff=identity, jobs=jobs)
            load_session = False
    else:
        # Parse files, generate objects
        paths = find_files(files)
        if not paths:
            LOG.error("No files found")
            raise SystemExit
        LOG.info("Parsing GenBank files: %s", paths)
        clusters = parse_files(paths)

        # Align all clusters
        if no_align:
            globaligner = align.Globaligner()
            globaligner.add_clusters(*clusters)
        elif len(clusters) == 1:
            globaligner = align.align_clusters(clusters[0], jobs=1)
        else:
            LOG.info("Starting cluster alignments")
            globaligner = align.align_clusters(*clusters, cutoff=identity, jobs=jobs)

    LOG.info("Generating results summary...")
    summary = globaligner.format(
        delimiter=delimiter,
        decimals=decimals,
        link_headers=not hide_link_headers,
        alignment_headers=not hide_alignment_headers,
    )

    if output:
        if (output and Path(output).exists() and not force):
            print(summary)
            LOG.warn("File %s already exists but --force was not specified", output)
        else:
            LOG.info("Writing alignments to: %s", output)
            with open(output, "w") as fp:
                fp.write(summary)
    else:
        print(summary)

    if session and not load_session:
        LOG.info("Saving session to: %s", session)
        with open(session, "w") as fp:
            globaligner.to_json(fp, indent=json_indent)

    # Generate the SVG
    if plot:
        LOG.info("Building clustermap.js visualisation")
        if isinstance(plot, str):
            LOG.info("Writing to: %s", plot)
        plot_clusters(
            globaligner,
            output=None if plot is True else plot,
            use_file_order=use_file_order,
        )

    LOG.info("Done!")
    return globaligner
Example #3
0
def clinker(
    files,
    session=None,
    identity=0.3,
    delimiter=None,
    decimals=2,
    plot=None,
    output=None,
    force=False,
    no_align=False,
    hide_link_headers=False,
    hide_alignment_headers=False,
    use_file_order=False,
    json_indent=None,
    jobs=None,
    ranges=None,
    matrix_out=None,
    gene_functions=None,
):
    """Entry point for running the script."""
    LOG.info("Starting clinker")

    load_session = session and Path(session).exists()

    # Allow no files, so that user can generate a blank clinker web app
    # and load in previously saved figure data
    if not files:
        LOG.info("No files provided!")
        if plot:
            LOG.info("Opening empty clinker web app...")
            plot_data(
                dict(clusters=[], links=[], groups=[]),
                output=None if plot is True else plot
            )
        return

    # Parse range strings, if any specified
    if ranges:
        ranges = parse_ranges(ranges)

    # Parse any gene functions for grouping, if specified
    if gene_functions:
        gene_functions = parse_gene_functions(gene_functions)

    if load_session:
        LOG.info("Loading session from: %s", session)
        with open(session) as fp:
            globaligner = align.Globaligner.from_json(fp)
        if files:
            paths = find_files(files)
            if not paths:
                LOG.error("No files found")
                raise SystemExit
            LOG.info("Parsing files:")
            clusters = parse_files(paths, ranges=ranges)

            LOG.info("Adding clusters to loaded session and aligning")
            globaligner.add_clusters(*clusters)
            globaligner.align_stored_clusters(cutoff=identity, jobs=jobs)
            globaligner.build_gene_groups(functions=gene_functions)
            load_session = False
    else:
        # Parse files, generate objects
        paths = find_files(files)
        if not paths:
            LOG.error("No files found")
            raise SystemExit
        LOG.info("Parsing files:")
        clusters = parse_files(paths, ranges=ranges)

        # Align all clusters
        if no_align:
            globaligner = align.Globaligner()
            globaligner.add_clusters(*clusters)
            globaligner.build_gene_groups(functions=gene_functions)
        elif len(clusters) == 1:
            globaligner = align.align_clusters(clusters[0], jobs=1)
        else:
            LOG.info("Starting cluster alignments")
            globaligner = align.align_clusters(*clusters, cutoff=identity, jobs=jobs)
            globaligner.build_gene_groups(functions=gene_functions)

    if globaligner.alignments:
        LOG.info("Generating results summary...")
        summary = globaligner.format(
            delimiter=delimiter,
            decimals=decimals,
            link_headers=not hide_link_headers,
            alignment_headers=not hide_alignment_headers,
        )
        if output:
            if (output and Path(output).exists() and not force):
                print(summary)
                LOG.warn("File %s already exists but --force was not specified", output)
            else:
                LOG.info("Writing alignments to: %s", output)
                with open(output, "w") as fp:
                    fp.write(summary)
        else:
            print(summary)
        if matrix_out:
            LOG.info("Writing synteny matrix to: %s", matrix_out)
            matrix = globaligner.format_matrix(normalise=True, as_distance=True)
            with open(matrix_out, "w") as fp:
                fp.write(matrix)

    else:
        LOG.info("No alignments were generated")

    if session and not load_session:
        LOG.info("Saving session to: %s", session)
        with open(session, "w") as fp:
            globaligner.to_json(fp, indent=json_indent)

    # Generate the SVG
    if plot:
        LOG.info("Building clustermap.js visualisation")
        if isinstance(plot, str):
            LOG.info("Writing to: %s", plot)
        plot_clusters(
            globaligner,
            output=None if plot is True else plot,
            use_file_order=use_file_order,
        )

    LOG.info("Done!")
    return globaligner