def extract_sequence(region, sequence_file, fai):
    """
    Get a sequence region from an indexed sequence file.

    :param region: A tuple of the genomic coordinates with elements: name, pos, end.
    :param sequence_file: Open file to extract sequences from.
    :param fai: Dictionary of FAI records keyed by sequence name (see `read_fai_file()`).

    :return: Sequence in `seqFile` specified by `region`.
    """

    # Check FAI for region
    if region[0] not in fai:
        raise ValueError('Sequence {} is missing in the index'.format(region[0]))

    # Get coordinates and lengths
    chr_start = int(fai[region[0]][1])
    seq_len = int(fai[region[0]][2])
    line_len = int(fai[region[0]][3])

    region_pos = int(region[1])
    region_end = int(region[2])

    # Calculate psotions
    start_line = int(region_pos / seq_len)
    start_line_pos = region_pos % seq_len

    end_line = int(region_end / seq_len)
    end_line_pos = region_end % seq_len

    start_file_pos = chr_start + start_line * line_len + start_line_pos
    end_file_pos = chr_start + end_line * line_len + end_line_pos

    # Check file positions
    if start_file_pos < 0:
        raise ValueError('Region {0}:{1}-{2} attempts to seek before 0 in the sequence file'.format(*region))

    if end_file_pos < start_file_pos:
        raise ValueError(
            'Region {0}:{1}-{2} attempts to seek before the start position of its record in the '
            'sequence file'.format(*region)
        )

    # Read sequence
    sequence_file.seek(start_file_pos)

    return sequence_file.read(end_file_pos - start_file_pos).replace('\n', '')