def compute_alignment_matrix(seq_x, seq_y, scoring_matrix, global_flag = True):
    """
    Function to compute the alignment matrix for two sequences given
    those sequences and the scoring matrix. Global flag dictates whether
    a global or local alignment should be computed.
    Returns a matrix.
    """
    
    len_x = len(seq_x) + 1
    len_y = len(seq_y) + 1
    
    # first create an empty grid of the right dimensions
    align_matrix = [[0 for dummy_col in range(len_y)] for dummy_row in range(len_x)]
    
    # global flag allows negative scores
    if global_flag:
        # fill out leftmost column with incrementing dash score
        for row in range(1, len_x ):
            align_matrix[row][0] = align_matrix[row - 1][0] + \
                                   scoring_matrix[seq_x[row - 1]]["-"]
        
        # fill out uppermost row with increment dash score
        for col in range(1, len_y):
            align_matrix[0][col] = align_matrix[0][col - 1] + \
                                   scoring_matrix["-"][seq_y[col - 1]]
 
    # iteratively fill out the rest of the matrix
    for row in range(1, len_x):
        for col in range(1, len_y):
            align_matrix[row][col] = max(align_matrix[row - 1][col - 1] + 
                                         scoring_matrix[seq_x[row - 1]][seq_y[col - 1]],
                                         align_matrix[row - 1][col] + 
                                         scoring_matrix[seq_x[row - 1]]["-"],
                                         align_matrix[row][col - 1] + 
                                         scoring_matrix["-"][seq_y[col - 1]])
            if not global_flag:
                # must be all positive or 0
                align_matrix[row][col] = max(align_matrix[row][col], 0)
    
    return align_matrix