Convert WgSim FASTQ files to RNF FASTQ files.

		Args:
			rnf_fastq_fo (file): File object of the target RNF file.
			fai_fo (file): File object of FAI index of the reference genome.
			genome_id (int): RNF genome ID.
			wgsim_fastq_1_fn (str): File name of the first WgSim FASTQ file.
			wgsim_fastq_2_fn (str): File name of the second WgSim FASTQ file.
			number_of_read_tuples (int): Expected number of read tuples (to estimate widths).

def recode_wgsim_reads(
        rnf_fastq_fo,
        fai_fo,
        genome_id,
        wgsim_fastq_1_fn,
        wgsim_fastq_2_fn=None,
        number_of_read_tuples=10**9,
    ):
        """Convert WgSim FASTQ files to RNF FASTQ files.

		Args:
			rnf_fastq_fo (file): File object of the target RNF file.
			fai_fo (file): File object of FAI index of the reference genome.
			genome_id (int): RNF genome ID.
			wgsim_fastq_1_fn (str): File name of the first WgSim FASTQ file.
			wgsim_fastq_2_fn (str): File name of the second WgSim FASTQ file.
			number_of_read_tuples (int): Expected number of read tuples (to estimate widths).
		"""

        wgsim_pattern = re.compile(
            '@(.*)_([0-9]+)_([0-9]+)_([0-9]+):([0-9]+):([0-9]+)_([0-9]+):([0-9]+):([0-9]+)_([0-9a-f]+)/([12])'
        )
        """
			WGSIM read name format

		1)  contig name (chromsome name)
		2)  start end 1 (one-based)
		3)  end end 2 (one-based)
		4)  number of errors end 1
		5)  number of substitutions end 1
		6)  number of indels end 1
		5)  number of errors end 2
		6)  number of substitutions end 2
		7)  number of indels end 2
		10) id
		11) pair
		"""

        fai_index = rnftools.utils.FaIdx(fai_fo)
        read_tuple_id_width = len(format(number_of_read_tuples, 'x'))

        last_read_tuple_name = None

        fq_creator = rnftools.rnfformat.FqCreator(
            fastq_fo=rnf_fastq_fo,
            read_tuple_id_width=read_tuple_id_width,
            genome_id_width=2,
            chr_id_width=fai_index.chr_id_width,
            coor_width=fai_index.coor_width,
            info_reads_in_tuple=True,
            info_simulator="wgsim",
        )

        reads_in_tuple = 2
        if wgsim_fastq_2_fn is None:
            reads_in_tuple = 1

        i = 0
        with open(wgsim_fastq_1_fn, "r+") as f_inp_1:
            if reads_in_tuple == 2:
                # todo: close file
                f_inp_2 = open(wgsim_fastq_2_fn)

            for line_a in f_inp_1:
                lines = [line_a.strip()]
                if reads_in_tuple == 2:
                    lines.append(f_inp_2.readline().strip())

                if i % 4 == 0:
                    segments = []
                    # bases=[]
                    # qualities=[]

                    m = wgsim_pattern.search(lines[0])
                    if m is None:
                        rnftools.utils.error(
                            "Read tuple '{}' was not generated by WgSim.".format(lines[0][1:]), program="RNFtools",
                            subprogram="MIShmash", exception=ValueError
                        )

                    contig_name = m.group(1)
                    start_1 = int(m.group(2))
                    end_2 = int(m.group(3))
                    errors_1 = int(m.group(4))
                    substitutions_1 = int(m.group(5))
                    indels_1 = int(m.group(6))
                    errors_2 = int(m.group(7))
                    substitutions_2 = int(m.group(8))
                    indels_2 = int(m.group(9))
                    read_tuple_id_w = int(m.group(10), 16)
                    pair = int(m.group(11))

                    chr_id = fai_index.dict_chr_ids[contig_name] if fai_index.dict_chr_ids != {} else "0"

                    if start_1 < end_2:
                        direction_1 = "F"
                        direction_2 = "R"
                    else:
                        direction_1 = "R"
                        direction_2 = "F"

                    segment1 = rnftools.rnfformat.Segment(
                        genome_id=genome_id,
                        chr_id=chr_id,
                        direction=direction_1,
                        left=start_1,
                        right=0,
                    )

                    segment2 = rnftools.rnfformat.Segment(
                        genome_id=genome_id,
                        chr_id=chr_id,
                        direction=direction_2,
                        left=0,
                        right=end_2,
                    )

                elif i % 4 == 1:
                    bases = lines[0]
                    if reads_in_tuple == 2:
                        bases2 = lines[1]

                elif i % 4 == 2:
                    pass

                elif i % 4 == 3:
                    qualities = lines[0]
                    if reads_in_tuple == 2:
                        qualities2 = lines[1]

                    if reads_in_tuple == 1:
                        fq_creator.add_read(
                            read_tuple_id=i // 4 + 1,
                            bases=bases,
                            qualities=qualities,
                            segments=[segment1, segment2],
                        )
                    else:
                        fq_creator.add_read(
                            read_tuple_id=i // 4 + 1,
                            bases=bases,
                            qualities=qualities,
                            segments=[segment1],
                        )
                        fq_creator.add_read(
                            read_tuple_id=i // 4 + 1,
                            bases=bases2,
                            qualities=qualities2,
                            segments=[segment2],
                        )

                i += 1

        fq_creator.flush_read_tuple()