function [align_length, align_position, n_errors, error_positions] = align_with_constraints_fast_left(seq, target, maximum_errors, error_cost, candidate_positions)
%#codegen
% Finds highest scoring alignment of seq and target that forces the
% alignment to begin from the first nucleotide on the left of seq.
% If candidate_positions begins with 0, we just scan the whole seq. So don't put in 0 if you want specific positions only.

        l_seq = length(seq);
        l_target = length(target);
        
        align_length = 0;
        n_errors = 0;
        best_match_pos = 0;
        score = 0;
        error_positions = zeros(maximum_errors,1);
        if candidate_positions(1) == 0
            match_positions = l_target:-1:1; 
        else
            match_positions = candidate_positions;
        end
        
        n_pos = numel(match_positions);
        
        for i_i=1:n_pos
            i = match_positions(i_i);
            
            max_length = min( l_target - i + 1, l_seq);
            if max_length < score                
                break;
            end
            
            [current_align_length, current_n_errors, current_error_positions] = align_with_constraints_fixed_left( seq(1:max_length), target(i:i+max_length-1), maximum_errors, error_cost);
            current_score = current_align_length - error_cost*current_n_errors;
            
            
            if current_score > score || (current_score==score && current_align_length > align_length)
                align_length = current_align_length;
                n_errors = current_n_errors;
                error_positions = current_error_positions;
                score = current_score;
                best_match_pos = i;
            end            
        end                
        align_position = [1, best_match_pos];                
        
%% Displays alignment of sequences
%         align_pipes = repmat('|',1,align_length);        
%         align_pipes(error_positions)=' ';
%         alignment = [seq(start: start + align_length -1); align_pipes ; target(best_match_index : best_match_index + align_length -1)];

end