def write_fasta(
    init_fasta, info_frags, output=DEFAULT_NEW_GENOME_NAME, junction=False
):

    """Convert an info_frags.txt file into a fasta file given a reference.
    Optionally adds junction sequences to reflect the possibly missing base
    pairs between two newly joined scaffolds.
    """

    init_genome = {
        record.id: record.seq for record in SeqIO.parse(init_fasta, "fasta")
    }
    my_new_records = []
    with open(info_frags, "r") as info_frags_handle:
        current_seq = ""
        current_id = None
        previous_contig = None
        for line in info_frags_handle:
            if line.startswith(">"):
                previous_contig = None
                if current_id is not None:
                    new_record = SeqRecord(
                        current_seq, id=current_id, description=""
                    )
                    my_new_records.append(new_record)
                current_seq = ""
                current_id = str(line[1:])
            elif line.startswith("init_contig"):
                previous_contig = None
            else:
                (init_contig, _, orientation, pos_start, pos_end) = str(
                    line[:-1]
                ).split("\t")

                start = int(pos_start)
                end = int(pos_end)
                ori = int(orientation)

                assert start < end
                assert ori in {-1, 1}

                seq_to_add = init_genome[init_contig][start:end]
                if ori == 1:
                    current_seq += seq_to_add
                elif ori == -1:
                    current_seq += seq_to_add.reverse_complement()

                if junction and previous_contig not in {init_contig, None}:
                    error_was_raised = False
                    try:
                        extra_seq = Seq(junction, IUPAC.ambiguous_dna)
                        current_seq = extra_seq + current_seq
                    except TypeError:
                        if not error_was_raised:
                            print("Invalid junction sequence")
                            error_was_raised = True
                previous_contig = init_contig

        new_record = SeqRecord(current_seq, id=current_id, description="")
        my_new_records.append(new_record)
    SeqIO.write(my_new_records, output, "fasta")
Convert an info_frags.txt file into a fasta file given a reference.
    Optionally adds junction sequences to reflect the possibly missing base
    pairs between two newly joined scaffolds.