flowcraft/templates/trimmomatic.py
def main(sample_id, fastq_pair, trim_range, trim_opts, phred, adapters_file,
         clear):
    """ Main executor of the trimmomatic template.

    Parameters
    ----------
    sample_id : str
        Sample Identification string.
    fastq_pair : list
        Two element list containing the paired FastQ files.
    trim_range : list
        Two element list containing the trimming range.
    trim_opts : list
        Four element list containing several trimmomatic options:
        [*SLIDINGWINDOW*; *LEADING*; *TRAILING*; *MINLEN*]
    phred : int
        Guessed phred score for the sample. The phred score is a generated
        output from :py:class:`templates.integrity_coverage`.
    adapters_file : str
        Path to adapters file. If not provided, or the path is not available,
        it will use the default adapters from Trimmomatic will be used
    clear : str
        Can be either 'true' or 'false'. If 'true', the input fastq files will
        be removed at the end of the run, IF they are in the working directory
    """

    logger.info("Starting trimmomatic")

    # Create base CLI
    cli = [
        "java",
        "-Xmx{}".format("$task.memory"[:-1].lower().replace(" ", "")),
        "-jar",
        TRIM_PATH.strip(),
        "PE",
        "-threads",
        "$task.cpus"
    ]

    # If the phred encoding was detected, provide it
    try:
        # Check if the provided PHRED can be converted to int
        phred = int(phred)
        phred_flag = "-phred{}".format(str(phred))
        cli += [phred_flag]
    # Could not detect phred encoding. Do not add explicit encoding to
    # trimmomatic and let it guess
    except ValueError:
        pass

    # Add input samples to CLI
    cli += fastq_pair

    # Add output file names
    output_names = []
    for i in range(len(fastq_pair)):
        output_names.append("{}_{}_trim.fastq.gz".format(
            SAMPLE_ID, str(i + 1)))
        output_names.append("{}_{}_U.fastq.gz".format(
            SAMPLE_ID, str(i + 1)))
    cli += output_names

    if trim_range != ["None"]:
        cli += [
            "CROP:{}".format(trim_range[1]),
            "HEADCROP:{}".format(trim_range[0]),
        ]

    if os.path.exists(adapters_file):
        logger.debug("Using the provided adapters file '{}'".format(
            adapters_file))
    else:
        logger.debug("Adapters file '{}' not provided or does not exist. Using"
                     " default adapters".format(adapters_file))
        adapters_file = merge_default_adapters()

    cli += [
        "ILLUMINACLIP:{}:3:30:10:6:true".format(adapters_file)
    ]

    #create log file im temporary dir to avoid issues when running on a docker container in macOS
    logfile = os.path.join(tempfile.mkdtemp(prefix='tmp'), "{}_trimlog.txt".format(sample_id))

    # Add trimmomatic options
    cli += [
        "SLIDINGWINDOW:{}".format(trim_opts[0]),
        "LEADING:{}".format(trim_opts[1]),
        "TRAILING:{}".format(trim_opts[2]),
        "MINLEN:{}".format(trim_opts[3]),
        "TOPHRED33",
        "-trimlog",
        logfile
    ]

    logger.debug("Running trimmomatic subprocess with command: {}".format(cli))

    p = subprocess.Popen(cli, stdout=PIPE, stderr=PIPE)
    stdout, stderr = p.communicate()

    # Attempt to decode STDERR output from bytes. If unsuccessful, coerce to
    # string
    try:
        stderr = stderr.decode("utf8")
    except (UnicodeDecodeError, AttributeError):
        stderr = str(stderr)

    logger.info("Finished trimmomatic subprocess with STDOUT:\\n"
                "======================================\\n{}".format(stdout))
    logger.info("Finished trimmomatic subprocesswith STDERR:\\n"
                "======================================\\n{}".format(stderr))
    logger.info("Finished trimmomatic with return code: {}".format(
        p.returncode))

    trimmomatic_log(logfile, sample_id)

    if p.returncode == 0 and os.path.exists("{}_1_trim.fastq.gz".format(
            SAMPLE_ID)):
        clean_up(fastq_pair, clear)

    # Check if trimmomatic ran successfully. If not, write the error message
    # to the status channel and exit.
    with open(".status", "w") as status_fh:
        if p.returncode != 0:
            status_fh.write("fail")
            return
        else:
            status_fh.write("pass")
def main(sample_id, fastq_pair, trim_range, trim_opts, phred, adapters_file,
         clear):
    logger.info("Starting trimmomatic")

    # Create base CLI
    cli = [
        "java",
        "-Xmx{}".format("$task.memory"[:-1].lower().replace(" ", "")),
        "-jar",
        TRIM_PATH.strip(),
        "PE",
        "-threads",
        "$task.cpus"
    ]

    # If the phred encoding was detected, provide it
    try:
        # Check if the provided PHRED can be converted to int
        phred = int(phred)
        phred_flag = "-phred{}".format(str(phred))
        cli += [phred_flag]
    # Could not detect phred encoding. Do not add explicit encoding to
    # trimmomatic and let it guess
    except ValueError:
        pass

    # Add input samples to CLI
    cli += fastq_pair

    # Add output file names
    output_names = []
    for i in range(len(fastq_pair)):
        output_names.append("{}_{}_trim.fastq.gz".format(
            SAMPLE_ID, str(i + 1)))
        output_names.append("{}_{}_U.fastq.gz".format(
            SAMPLE_ID, str(i + 1)))
    cli += output_names

    if trim_range != ["None"]:
        cli += [
            "CROP:{}".format(trim_range[1]),
            "HEADCROP:{}".format(trim_range[0]),
        ]

    if os.path.exists(adapters_file):
        logger.debug("Using the provided adapters file '{}'".format(
            adapters_file))
    else:
        logger.debug("Adapters file '{}' not provided or does not exist. Using"
                     " default adapters".format(adapters_file))
        adapters_file = merge_default_adapters()

    cli += [
        "ILLUMINACLIP:{}:3:30:10:6:true".format(adapters_file)
    ]

    #create log file im temporary dir to avoid issues when running on a docker container in macOS
    logfile = os.path.join(tempfile.mkdtemp(prefix='tmp'), "{}_trimlog.txt".format(sample_id))

    # Add trimmomatic options
    cli += [
        "SLIDINGWINDOW:{}".format(trim_opts[0]),
        "LEADING:{}".format(trim_opts[1]),
        "TRAILING:{}".format(trim_opts[2]),
        "MINLEN:{}".format(trim_opts[3]),
        "TOPHRED33",
        "-trimlog",
        logfile
    ]

    logger.debug("Running trimmomatic subprocess with command: {}".format(cli))

    p = subprocess.Popen(cli, stdout=PIPE, stderr=PIPE)
    stdout, stderr = p.communicate()

    # Attempt to decode STDERR output from bytes. If unsuccessful, coerce to
    # string
    try:
        stderr = stderr.decode("utf8")
    except (UnicodeDecodeError, AttributeError):
        stderr = str(stderr)

    logger.info("Finished trimmomatic subprocess with STDOUT:\\n"
                "======================================\\n{}".format(stdout))
    logger.info("Finished trimmomatic subprocesswith STDERR:\\n"
                "======================================\\n{}".format(stderr))
    logger.info("Finished trimmomatic with return code: {}".format(
        p.returncode))

    trimmomatic_log(logfile, sample_id)

    if p.returncode == 0 and os.path.exists("{}_1_trim.fastq.gz".format(
            SAMPLE_ID)):
        clean_up(fastq_pair, clear)

    # Check if trimmomatic ran successfully. If not, write the error message
    # to the status channel and exit.
    with open(".status", "w") as status_fh:
        if p.returncode != 0:
            status_fh.write("fail")
            return
        else:
            status_fh.write("pass")
def main ( sample_id , fastq_pair , trim_range , trim_opts , phred , adapters_file , clear ) : logger . info ( "Starting trimmomatic" ) # Create base CLI cli = [ "java" , "-Xmx{}" . format ( "$task.memory" [ : - 1 ] . lower ( ) . replace ( " " , "" ) ) , "-jar" , TRIM_PATH . strip ( ) , "PE" , "-threads" , "$task.cpus" ] # If the phred encoding was detected, provide it try : # Check if the provided PHRED can be converted to int phred = int ( phred ) phred_flag = "-phred{}" . format ( str ( phred ) ) cli += [ phred_flag ] # Could not detect phred encoding. Do not add explicit encoding to # trimmomatic and let it guess except ValueError : pass # Add input samples to CLI cli += fastq_pair # Add output file names output_names = [ ] for i in range ( len ( fastq_pair ) ) : output_names . append ( "{}_{}_trim.fastq.gz" . format ( SAMPLE_ID , str ( i + 1 ) ) ) output_names . append ( "{}_{}_U.fastq.gz" . format ( SAMPLE_ID , str ( i + 1 ) ) ) cli += output_names if trim_range != [ "None" ] : cli += [ "CROP:{}" . format ( trim_range [ 1 ] ) , "HEADCROP:{}" . format ( trim_range [ 0 ] ) , ] if os . path . exists ( adapters_file ) : logger . debug ( "Using the provided adapters file '{}'" . format ( adapters_file ) ) else : logger . debug ( "Adapters file '{}' not provided or does not exist. Using" " default adapters" . format ( adapters_file ) ) adapters_file = merge_default_adapters ( ) cli += [ "ILLUMINACLIP:{}:3:30:10:6:true" . format ( adapters_file ) ] #create log file im temporary dir to avoid issues when running on a docker container in macOS logfile = os . path . join ( tempfile . mkdtemp ( prefix = 'tmp' ) , "{}_trimlog.txt" . format ( sample_id ) ) # Add trimmomatic options cli += [ "SLIDINGWINDOW:{}" . format ( trim_opts [ 0 ] ) , "LEADING:{}" . format ( trim_opts [ 1 ] ) , "TRAILING:{}" . format ( trim_opts [ 2 ] ) , "MINLEN:{}" . format ( trim_opts [ 3 ] ) , "TOPHRED33" , "-trimlog" , logfile ] logger . debug ( "Running trimmomatic subprocess with command: {}" . format ( cli ) ) p = subprocess . Popen ( cli , stdout = PIPE , stderr = PIPE ) stdout , stderr = p . communicate ( ) # Attempt to decode STDERR output from bytes. If unsuccessful, coerce to # string try : stderr = stderr . decode ( "utf8" ) except ( UnicodeDecodeError , AttributeError ) : stderr = str ( stderr ) logger . info ( "Finished trimmomatic subprocess with STDOUT:\\n" "======================================\\n{}" . format ( stdout ) ) logger . info ( "Finished trimmomatic subprocesswith STDERR:\\n" "======================================\\n{}" . format ( stderr ) ) logger . info ( "Finished trimmomatic with return code: {}" . format ( p . returncode ) ) trimmomatic_log ( logfile , sample_id ) if p . returncode == 0 and os . path . exists ( "{}_1_trim.fastq.gz" . format ( SAMPLE_ID ) ) : clean_up ( fastq_pair , clear ) # Check if trimmomatic ran successfully. If not, write the error message # to the status channel and exit. with open ( ".status" , "w" ) as status_fh : if p . returncode != 0 : status_fh . write ( "fail" ) return else : status_fh . write ( "pass" )
Main executor of the trimmomatic template.

    Parameters
    ----------
    sample_id : str
        Sample Identification string.
    fastq_pair : list
        Two element list containing the paired FastQ files.
    trim_range : list
        Two element list containing the trimming range.
    trim_opts : list
        Four element list containing several trimmomatic options:
        [*SLIDINGWINDOW*; *LEADING*; *TRAILING*; *MINLEN*]
    phred : int
        Guessed phred score for the sample. The phred score is a generated
        output from :py:class:`templates.integrity_coverage`.
    adapters_file : str
        Path to adapters file. If not provided, or the path is not available,
        it will use the default adapters from Trimmomatic will be used
    clear : str
        Can be either 'true' or 'false'. If 'true', the input fastq files will
        be removed at the end of the run, IF they are in the working directory
Main executor of the trimmomatic template .