Example #1
0
def open_output_files(options, default_outfile, interleaved):
	"""
	Return an OutputFiles instance. If demultiplex is True, the untrimmed, untrimmed2, out and out2
	attributes are not opened files, but paths (out and out2 with the '{name}' template).
	"""
	rest_file = info_file = wildcard = None
	if options.rest_file is not None:
		rest_file = xopen(options.rest_file, 'w')
	if options.info_file is not None:
		info_file = xopen(options.info_file, 'w')
	if options.wildcard_file is not None:
		wildcard = xopen(options.wildcard_file, 'w')

	def open2(path1, path2):
		file1 = file2 = None
		if path1 is not None:
			file1 = xopen(path1, 'w')
			if path2 is not None:
				file2 = xopen(path2, 'w')
		return file1, file2

	too_short = too_short2 = None
	if options.minimum_length is not None:
		too_short, too_short2 = open2(options.too_short_output, options.too_short_paired_output)

	too_long = too_long2 = None
	if options.maximum_length is not None:
		too_long, too_long2 = open2(options.too_long_output, options.too_long_paired_output)

	if int(options.discard_trimmed) + int(options.discard_untrimmed) + int(
					options.untrimmed_output is not None) > 1:
		raise CommandLineError("Only one of the --discard-trimmed, --discard-untrimmed "
			"and --untrimmed-output options can be used at the same time.")

	demultiplex = options.output is not None and '{name}' in options.output
	if options.paired_output is not None and (demultiplex != ('{name}' in options.paired_output)):
		raise CommandLineError('When demultiplexing paired-end data, "{name}" must appear in '
			'both output file names (-o and -p)')

	if demultiplex:
		if options.discard_trimmed:
			raise CommandLineError("Do not use --discard-trimmed when demultiplexing.")

		out = options.output
		untrimmed = options.output.replace('{name}', 'unknown')
		if options.untrimmed_output:
			untrimmed = options.untrimmed_output
		if options.discard_untrimmed:
			untrimmed = None

		if options.paired_output is not None:
			out2 = options.paired_output
			untrimmed2 = options.paired_output.replace('{name}', 'unknown')
			if options.untrimmed_paired_output:
				untrimmed2 = options.untrimmed_paired_output
			if options.discard_untrimmed:
				untrimmed2 = None

		else:
			untrimmed2 = out2 = None
	else:
		untrimmed, untrimmed2 = open2(options.untrimmed_output, options.untrimmed_paired_output)
		out, out2 = open2(options.output, options.paired_output)
		if out is None:
			out = default_outfile

	if demultiplex:
		assert out is not None and '{name}' in out and (out2 is None or '{name}' in out2)
	return OutputFiles(
		rest=rest_file,
		info=info_file,
		wildcard=wildcard,
		too_short=too_short,
		too_short2=too_short2,
		too_long=too_long,
		too_long2=too_long2,
		untrimmed=untrimmed,
		untrimmed2=untrimmed2,
		out=out,
		out2=out2,
		demultiplex=demultiplex,
		interleaved=interleaved,
	)
Example #2
0
def open_output_files(args, default_outfile,
                      file_opener: FileOpener) -> OutputFiles:
    """
    Return an OutputFiles instance. If demultiplex is True, the untrimmed, untrimmed2, out and out2
    attributes are not opened files, but paths (out and out2 with the '{name}' template).
    """

    rest_file = file_opener.xopen_or_none(args.rest_file, "wb")
    info_file = file_opener.xopen_or_none(args.info_file, "wb")
    wildcard = file_opener.xopen_or_none(args.wildcard_file, "wb")

    too_short = too_short2 = None
    if args.minimum_length is not None:
        too_short, too_short2 = file_opener.xopen_pair(
            args.too_short_output, args.too_short_paired_output, "wb")

    too_long = too_long2 = None
    if args.maximum_length is not None:
        too_long, too_long2 = file_opener.xopen_pair(
            args.too_long_output, args.too_long_paired_output, "wb")

    if int(args.discard_trimmed) + int(args.discard_untrimmed) + int(
            args.untrimmed_output is not None) > 1:
        raise CommandLineError(
            "Only one of the --discard-trimmed, --discard-untrimmed "
            "and --untrimmed-output options can be used at the same time.")

    demultiplex_mode = determine_demultiplex_mode(args)
    if demultiplex_mode and args.discard_trimmed:
        raise CommandLineError(
            "Do not use --discard-trimmed when demultiplexing.")

    if demultiplex_mode == "normal":
        out = args.output
        untrimmed = args.output.replace('{name}', 'unknown')
        if args.untrimmed_output:
            untrimmed = args.untrimmed_output
        if args.discard_untrimmed:
            untrimmed = None

        if args.paired_output is not None:
            out2 = args.paired_output
            untrimmed2 = args.paired_output.replace('{name}', 'unknown')
            if args.untrimmed_paired_output:
                untrimmed2 = args.untrimmed_paired_output
            if args.discard_untrimmed:
                untrimmed2 = None
        else:
            untrimmed2 = out2 = None

        assert out is not None and '{name}' in out and (out2 is None
                                                        or '{name}' in out2)
    elif demultiplex_mode == "combinatorial":
        out = args.output
        out2 = args.paired_output
        if args.untrimmed_output or args.untrimmed_paired_output:
            raise CommandLineError(
                "Combinatorial demultiplexing (with {name1} and {name2})"
                " cannot be combined with --untrimmed-output or --untrimmed-paired-output"
            )
        if args.discard_untrimmed:
            untrimmed = untrimmed2 = None
        else:
            untrimmed = untrimmed2 = 'unknown'
    else:
        untrimmed, untrimmed2 = file_opener.xopen_pair(
            args.untrimmed_output, args.untrimmed_paired_output, "wb")
        out, out2 = file_opener.xopen_pair(args.output, args.paired_output,
                                           "wb")
        if out is None:
            out = default_outfile

    return OutputFiles(
        rest=rest_file,
        info=info_file,
        wildcard=wildcard,
        too_short=too_short,
        too_short2=too_short2,
        too_long=too_long,
        too_long2=too_long2,
        untrimmed=untrimmed,
        untrimmed2=untrimmed2,
        out=out,
        out2=out2,
        demultiplex=bool(demultiplex_mode),
        force_fasta=args.fasta,
    )
Example #3
0
def open_output_files(
    args,
    default_outfile,
    file_opener: FileOpener,
    adapter_names: Sequence[str],
    adapter_names2: Sequence[str],
) -> OutputFiles:
    """
    Return an OutputFiles instance. If demultiplex is True, the untrimmed, untrimmed2, out and out2
    attributes are not opened files, but paths (out and out2 with the '{name}' template).
    """

    rest_file = file_opener.xopen_or_none(args.rest_file, "wb")
    info_file = file_opener.xopen_or_none(args.info_file, "wb")
    wildcard = file_opener.xopen_or_none(args.wildcard_file, "wb")

    too_short = too_short2 = None
    if args.minimum_length is not None:
        too_short, too_short2 = file_opener.xopen_pair(
            args.too_short_output, args.too_short_paired_output, "wb")

    too_long = too_long2 = None
    if args.maximum_length is not None:
        too_long, too_long2 = file_opener.xopen_pair(
            args.too_long_output, args.too_long_paired_output, "wb")

    if int(args.discard_trimmed) + int(args.discard_untrimmed) + int(
            args.untrimmed_output is not None) > 1:
        raise CommandLineError(
            "Only one of the --discard-trimmed, --discard-untrimmed "
            "and --untrimmed-output options can be used at the same time.")

    demultiplex_mode = determine_demultiplex_mode(args)
    if demultiplex_mode and args.discard_trimmed:
        raise CommandLineError(
            "Do not use --discard-trimmed when demultiplexing.")

    if demultiplex_mode == "normal":
        out = out2 = None
        combinatorial_out = combinatorial_out2 = None
        demultiplex_out, demultiplex_out2, untrimmed, untrimmed2 = open_demultiplex_out(
            adapter_names, args, file_opener)
    elif demultiplex_mode == "combinatorial":
        assert '{name1}' in args.output and '{name2}' in args.output
        assert '{name1}' in args.paired_output and '{name2}' in args.paired_output
        out = out2 = None
        demultiplex_out = demultiplex_out2 = None
        combinatorial_out, combinatorial_out2, untrimmed, untrimmed2 = open_combinatorial_out(
            adapter_names, adapter_names2, args, file_opener)
    else:
        combinatorial_out = combinatorial_out2 = None
        demultiplex_out = demultiplex_out2 = None
        untrimmed, untrimmed2 = file_opener.xopen_pair(
            args.untrimmed_output, args.untrimmed_paired_output, "wb")
        out, out2 = file_opener.xopen_pair(args.output, args.paired_output,
                                           "wb")
        if out is None:
            out = default_outfile

    return OutputFiles(
        rest=rest_file,
        info=info_file,
        wildcard=wildcard,
        too_short=too_short,
        too_short2=too_short2,
        too_long=too_long,
        too_long2=too_long2,
        untrimmed=untrimmed,
        untrimmed2=untrimmed2,
        out=out,
        out2=out2,
        demultiplex_out=demultiplex_out,
        demultiplex_out2=demultiplex_out2,
        combinatorial_out=combinatorial_out,
        combinatorial_out2=combinatorial_out2,
        force_fasta=args.fasta,
    )
Example #4
0
def open_output_files(args, default_outfile, interleaved):
    """
    Return an OutputFiles instance. If demultiplex is True, the untrimmed, untrimmed2, out and out2
    attributes are not opened files, but paths (out and out2 with the '{name}' template).
    """
    compression_level = args.compression_level
    rest_file = info_file = wildcard = None
    if args.rest_file is not None:
        rest_file = xopen(args.rest_file, 'w', compresslevel=compression_level)
    if args.info_file is not None:
        info_file = xopen(args.info_file, 'w', compresslevel=compression_level)
    if args.wildcard_file is not None:
        wildcard = xopen(args.wildcard_file,
                         'w',
                         compresslevel=compression_level)

    def open2(path1, path2):
        file1 = file2 = None
        if path1 is not None:
            file1 = xopen(path1, 'wb', compresslevel=compression_level)
            if path2 is not None:
                file2 = xopen(path2, 'wb', compresslevel=compression_level)
        return file1, file2

    too_short = too_short2 = None
    if args.minimum_length is not None:
        too_short, too_short2 = open2(args.too_short_output,
                                      args.too_short_paired_output)

    too_long = too_long2 = None
    if args.maximum_length is not None:
        too_long, too_long2 = open2(args.too_long_output,
                                    args.too_long_paired_output)

    if int(args.discard_trimmed) + int(args.discard_untrimmed) + int(
            args.untrimmed_output is not None) > 1:
        raise CommandLineError(
            "Only one of the --discard-trimmed, --discard-untrimmed "
            "and --untrimmed-output options can be used at the same time.")

    demultiplex = args.output is not None and '{name}' in args.output

    if args.paired_output is not None and (demultiplex !=
                                           ('{name}' in args.paired_output)):
        raise CommandLineError(
            'When demultiplexing paired-end data, "{name}" must appear in '
            'both output file names (-o and -p)')

    demultiplex_combinatorial = (args.output is not None
                                 and args.paired_output is not None
                                 and '{name1}' in args.output
                                 and '{name2}' in args.output
                                 and '{name1}' in args.paired_output
                                 and '{name2}' in args.paired_output)
    if (demultiplex or demultiplex_combinatorial) and args.discard_trimmed:
        raise CommandLineError(
            "Do not use --discard-trimmed when demultiplexing.")

    if demultiplex:
        if demultiplex_combinatorial:
            raise CommandLineError(
                "You cannot combine {name} with {name1} and {name2}")

        out = args.output
        untrimmed = args.output.replace('{name}', 'unknown')
        if args.untrimmed_output:
            untrimmed = args.untrimmed_output
        if args.discard_untrimmed:
            untrimmed = None

        if args.paired_output is not None:
            out2 = args.paired_output
            untrimmed2 = args.paired_output.replace('{name}', 'unknown')
            if args.untrimmed_paired_output:
                untrimmed2 = args.untrimmed_paired_output
            if args.discard_untrimmed:
                untrimmed2 = None

        else:
            untrimmed2 = out2 = None

        assert out is not None and '{name}' in out and (out2 is None
                                                        or '{name}' in out2)
    elif demultiplex_combinatorial:
        out = args.output
        out2 = args.paired_output
        if args.untrimmed_output or args.untrimmed_paired_output:
            raise CommandLineError(
                "Combinatorial demultiplexing (with {name1} and {name2})"
                " cannot be combined with --untrimmed-output or --untrimmed-paired-output"
            )
        if args.discard_untrimmed:
            untrimmed = untrimmed2 = None
        else:
            untrimmed = untrimmed2 = 'unknown'
    else:
        untrimmed, untrimmed2 = open2(args.untrimmed_output,
                                      args.untrimmed_paired_output)
        out, out2 = open2(args.output, args.paired_output)
        if out is None:
            out = default_outfile

    return OutputFiles(
        rest=rest_file,
        info=info_file,
        wildcard=wildcard,
        too_short=too_short,
        too_short2=too_short2,
        too_long=too_long,
        too_long2=too_long2,
        untrimmed=untrimmed,
        untrimmed2=untrimmed2,
        out=out,
        out2=out2,
        demultiplex=demultiplex or demultiplex_combinatorial,
        interleaved=interleaved,
        force_fasta=args.fasta,
    )