def extract_barcode_region(read_sequence, adapter_template,
    barcode_set_index, alignment_stop_ref, qcat_config):
    """
    Extracts the region from the read that is expected to contain
    the barcode sequence.
    :param read_sequence: Sequence of the read
    :type read_sequence: str
    :param adapter_template: Adapter layout with masked barcode region
    :type adapter_template: AdapterLayout
    :param barcode_set_index:
    :param alignment_stop_ref: Position on the read that the last bp of the
    adapter template is aligned to
    :type alignment_stop_ref: int
    :param qcat_config: qcatConfig object
    :type qcat_config: qcatConfig
    :return: Part of the read that will be used to identify the barcode
    :rtype str
    """
    adapter_length = adapter_template.get_adapter_length()
    barcode_end = adapter_template.get_barcode_end(barcode_set_index)
    barcode_length = adapter_template.get_barcode_length(barcode_set_index)
    barcode_end_ref = alignment_stop_ref - (adapter_length - barcode_end) + 1
    barcode_start_ref = barcode_end_ref - barcode_length
    barcode_start_ref -= min(qcat_config.extracted_barcode_extension,
        barcode_start_ref)
    barcode_end_ref += min(qcat_config.extracted_barcode_extension, len(
        read_sequence) - barcode_end_ref)
    return read_sequence[barcode_start_ref:barcode_end_ref + 1]
