def processSequence(key, description_rest, sequence, options, mask_regions=None):

    global global_outfile
    global global_file_id
    global global_written2file

    written = 0

    lsequence = len(sequence)

    if mask_regions:
        sequence, nmasked = maskSequence(sequence, mask_regions[key])
    else:
        nmasked = 0

    while written < lsequence:

        # size of current chunk
        chunk_size = min(
            options.chunk_size - global_written2file, lsequence - written)

        first_res = max(0, written)
        last_res = min(written + chunk_size + options.extend, lsequence)

        if options.loglevel >= 2:
            options.stdlog.write("# file_id=%i, chunk_size=%i, global_written2file=%i, written=%i, lsequence=%i, first_res=%i, last_res=%i\n" %
                                 (global_file_id, chunk_size, global_written2file, written, lsequence, first_res, last_res))

        # check if we need to open a new sequence file:
        if global_outfile is None:
            global_file_id += 1
            global_outfile = open(options.output_pattern % global_file_id, "w")

        offset_positive_strand = first_res
        offset_negative_strand = lsequence - last_res

        if options.format == "fasta":
            global_outfile.write(">%s_%i_%i %s\n%s\n" % (key,
                                                         offset_positive_strand, offset_negative_strand,
                                                         description_rest, sequence[first_res:last_res]))
        elif options.format == "ranges":
            global_outfile.write("%s\t%i\t%i\n" % (key, first_res, last_res))

        global_written2file += chunk_size

        # if we have reached the chunksize: close file
        if global_written2file >= options.chunk_size:
            global_outfile.close()
            global_outfile = None
            global_written2file = 0

        written += chunk_size

    return len(sequence), nmasked
dataset/ETHPy150Open CGATOxford/cgat/scripts/split_genomic_fasta_file.py/processSequence/original