def fix_bases_mask(bases_mask,barcode_sequence):
    """Adjust input bases mask to match actual barcode sequence lengths

    Updates the bases mask string extracted from RunInfo.xml so that the
    index read masks correspond to the index barcode sequence lengths
    given e.g. in the SampleSheet.csv file.

    For example: if the bases mask is 'y101,I7,y101' (i.e. assigning 7
    cycles to the index read) but the barcode sequence is 'CGATGT' (i.e.
    only 6 bases) then the adjusted bases mask should be 'y101,I6n,y101'.

    Arguments:
      bases_mask: bases mask string e.g. 'y101,I7,y101','y250,I8,I8,y250'
      barcode_sequence: index barcode sequence e.g. 'CGATGT' (single
      index), 'TAAGGCGA-TAGATCGC' (dual index)

    Returns:
      Updated bases mask string.

    """
    # Split barcode sequence string into components
    indexes = barcode_sequence.split('-')
    # Check input reads
    reads = []
    i = 0
    for read in bases_mask.split(','):
        new_read = read
        if read.startswith('I'):
            input_index_length = int(read[1:])
            try:
                actual_index_length = len(indexes[i])
            except IndexError:
                # No barcode for this read
                actual_index_length = 0
            if actual_index_length > 0:
                new_read = "I%d" % actual_index_length
            else:
                new_read = ""
            if input_index_length > actual_index_length:
                # Actual index sequence is shorter so adjust
                # bases mask and pad with 'n's
                new_read = new_read + \
                           'n'*(input_index_length-actual_index_length)
            i += 1
        reads.append(new_read)
    # Assemble and return updated index tags
    return ','.join(reads)