flowcraft/templates/process_viral_assembly.py
def main(sample_id, assembly_file, minsize):
    """Main executor of the process_mapping template.

    Parameters
    ----------
    sample_id : str
        Sample Identification string.
    assembly: str
        Path to the fatsa file generated by the assembler.
    minsize: str
        Min contig size to be considered a complete ORF

    """

    logger.info("Starting assembly file processing")
    warnings = []
    fails = ""

    # Parse the spades assembly file and perform the first filtering.
    logger.info("Starting assembly parsing")
    assembly_obj = Assembly(assembly_file, 0, 0,
                            sample_id, minsize)

    if 'spades' in assembly_file:
        assembler = "SPAdes"
    else:
        assembler = "MEGAHIT"

    with open(".warnings", "w") as warn_fh:

        t_80 = int(minsize) * 0.8
        t_150 = int(minsize) * 1.5
        # Check if assembly size of the first assembly is lower than 80% of the
        # estimated genome size - DENV ORF has min 10k nt. If True, redo the filtering without the
        # k-mer coverage filter
        assembly_len = assembly_obj.get_assembly_length()
        logger.debug("Checking assembly length: {}".format(assembly_len))

        if assembly_obj.nORFs < 1:
            warn_msg = "No complete ORFs found."
            warn_fh.write(warn_msg)
            fails = warn_msg

        if assembly_len < t_80:

            logger.warning("Assembly size ({}) smaller than the minimum "
                           "threshold of 80% of expected genome size. "
                           "Applying contig filters without the k-mer "
                           "coverage filter".format(assembly_len))

            assembly_len = assembly_obj.get_assembly_length()
            logger.debug("Checking updated assembly length: "
                         "{}".format(assembly_len))
            if assembly_len < t_80:

                warn_msg = "Assembly size smaller than the minimum" \
                           " threshold of 80% of expected genome size: {}".format(
                                assembly_len)
                logger.warning(warn_msg)
                warn_fh.write(warn_msg)
                fails = warn_msg

        if assembly_len > t_150:

            warn_msg = "Assembly size ({}) larger than the maximum" \
                       " threshold of 150% of expected genome size.".format(
                            assembly_len)
            logger.warning(warn_msg)
            warn_fh.write(warn_msg)
            fails = warn_msg


    # Write json report
    with open(".report.json", "w") as json_report:
        json_dic = {
            "tableRow": [{
                "sample": sample_id,
                "data": [
                    {"header": "Contigs ({})".format(assembler),
                     "value": len(assembly_obj.contigs),
                     "table": "assembly",
                     "columnBar": True},
                    {"header": "Assembled BP ({})".format(assembler),
                     "value": assembly_len,
                     "table": "assembly",
                     "columnBar": True},
                    {"header": "ORFs",
                     "value": assembly_obj.nORFs,
                     "table": "assembly",
                     "columnBar":False}
                ]
            }],
        }

        if warnings:
            json_dic["warnings"] = [{
                "sample": sample_id,
                "table": "assembly",
                "value": warnings
            }]

        if fails:
            json_dic["fail"] = [{
                "sample": sample_id,
                "table": "assembly",
                "value": [fails]
            }]

        json_report.write(json.dumps(json_dic, separators=(",", ":")))

    with open(".status", "w") as status_fh:
        status_fh.write("pass")
def main(sample_id, assembly_file, minsize):
    """Main executor of the process_mapping template.

    Parameters
    ----------
    sample_id : str
        Sample Identification string.
    assembly: str
        Path to the fatsa file generated by the assembler.
    minsize: str
        Min contig size to be considered a complete ORF

    """

    logger.info("Starting assembly file processing")
    warnings = []
    fails = ""

    # Parse the spades assembly file and perform the first filtering.
    logger.info("Starting assembly parsing")
    assembly_obj = Assembly(assembly_file, 0, 0,
                            sample_id, minsize)

    if 'spades' in assembly_file:
        assembler = "SPAdes"
    else:
        assembler = "MEGAHIT"

    with open(".warnings", "w") as warn_fh:

        t_80 = int(minsize) * 0.8
        t_150 = int(minsize) * 1.5
        # Check if assembly size of the first assembly is lower than 80% of the
        # estimated genome size - DENV ORF has min 10k nt. If True, redo the filtering without the
        # k-mer coverage filter
        assembly_len = assembly_obj.get_assembly_length()
        logger.debug("Checking assembly length: {}".format(assembly_len))

        if assembly_obj.nORFs < 1:
            warn_msg = "No complete ORFs found."
            warn_fh.write(warn_msg)
            fails = warn_msg

        if assembly_len < t_80:

            logger.warning("Assembly size ({}) smaller than the minimum "
                           "threshold of 80% of expected genome size. "
                           "Applying contig filters without the k-mer "
                           "coverage filter".format(assembly_len))

            assembly_len = assembly_obj.get_assembly_length()
            logger.debug("Checking updated assembly length: "
                         "{}".format(assembly_len))
            if assembly_len < t_80:

                warn_msg = "Assembly size smaller than the minimum" \
                           " threshold of 80% of expected genome size: {}".format(
                                assembly_len)
                logger.warning(warn_msg)
                warn_fh.write(warn_msg)
                fails = warn_msg

        if assembly_len > t_150:

            warn_msg = "Assembly size ({}) larger than the maximum" \
                       " threshold of 150% of expected genome size.".format(
                            assembly_len)
            logger.warning(warn_msg)
            warn_fh.write(warn_msg)
            fails = warn_msg


    # Write json report
    with open(".report.json", "w") as json_report:
        json_dic = {
            "tableRow": [{
                "sample": sample_id,
                "data": [
                    {"header": "Contigs ({})".format(assembler),
                     "value": len(assembly_obj.contigs),
                     "table": "assembly",
                     "columnBar": True},
                    {"header": "Assembled BP ({})".format(assembler),
                     "value": assembly_len,
                     "table": "assembly",
                     "columnBar": True},
                    {"header": "ORFs",
                     "value": assembly_obj.nORFs,
                     "table": "assembly",
                     "columnBar":False}
                ]
            }],
        }

        if warnings:
            json_dic["warnings"] = [{
                "sample": sample_id,
                "table": "assembly",
                "value": warnings
            }]

        if fails:
            json_dic["fail"] = [{
                "sample": sample_id,
                "table": "assembly",
                "value": [fails]
            }]

        json_report.write(json.dumps(json_dic, separators=(",", ":")))

    with open(".status", "w") as status_fh:
        status_fh.write("pass")
Main executor of the process_mapping template.

    Parameters
    ----------
    sample_id : str
        Sample Identification string.
    assembly: str
        Path to the fatsa file generated by the assembler.
    minsize: str
        Min contig size to be considered a complete ORF