function [model_info] = get_infer_using_model(alignments_file_base_name, original_model, n_chunk)

model_type = class(original_model);


if ~exist('n_chunk','var')
    n_chunk = 100;
end

%% load genomic segments
[genV, genD, genJ] = load_Mus_TCR_genomic_segments();


%% figure out alignments files
alignments_file_indices = 1:100;

for f=alignments_file_indices

    fname = [alignments_file_base_name '.alignments.' num2str(f) '.mat'];
    if exist(fname,'file')
        alignments_files{f} = fname;
    else
        break;
    end
end

alignments_file_base_name
alignments_files{:}

basename = alignments_file_base_name;


    %% Calculate loglikelihoods
    model = original_model;
    model.validate_model(); % See if model is valid - whatever that means.

    do_smoothing = true;

    if exist('model_settings','var')
        model = model.settings(model_settings);
    end

    load(alignments_files{1},'alignment_parameters');
    other_model_settings.read_length = alignment_parameters.read_length;

    other_model_settings.max_assignments = 5000;

    other_model_settings.high_error_region = 0;
    other_model_settings.min_V_length = 15;

    force_all_alleles = false;
    probability_threshold_factor = 0.0001;
    other_model_settings.negative_excess_deletions_max = 0;


    model = model.settings(other_model_settings);
    no_error = false;
    ignore_deep_error = false;
    READ_LENGTH_CORRECTION = 0;

    counter = model.initialize_counter();
    initial_assigns = model.initialize_assigns(counter);

    n_assignments = [];
    loglikelihoods = [];
    log_generation_probabilities = [];
    assignment_entropies = [];
    allele_entropies = [];
    event_entropies = [];
    assignment_entropies_no_errors = [];
    mean_errors = [];

    MaxLL.Vgene = [];
    MaxLL.Dgene = [];
    MaxLL.Jgene = [];
    MaxLL.insVD = [];
    MaxLL.insDJ = [];
    MaxLL.cutV = [];
    MaxLL.cutJ = [];
    MaxLL.cutDl = [];
    MaxLL.cutDr = [];
    MaxLL.V_length = [];
    MaxLL.J_length = [];
    MaxLL.D_length = [];
    MaxLL.errors = [];



    for f=1:length(alignments_files)
        %Load Alignments
        if f>1
            clear('V','D','J','Sequence');
        end
        alignments_file = alignments_files{f}
        load(alignments_file,'V', 'D', 'J','Sequence');

        N = size(V,1)
        N_p = ceil(N/n_chunk);

        dummy_counter = model.initialize_counter();
        counters = repmat(dummy_counter, N_p ,1);
        %all_counters = repmat(dummy_counter,N,1);

        myV = cell(N_p,1);
        myD = cell(N_p,1);
        myJ = cell(N_p,1);
        mySequence = cell(N_p,1);
        for k=1:N_p
            myV{k} = V((k-1)*n_chunk +1 : min(k*n_chunk, N));
            myD{k} = D((k-1)*n_chunk +1 : min(k*n_chunk, N));
            myJ{k} = J((k-1)*n_chunk +1 : min(k*n_chunk, N));
            mySequence{k} = Sequence((k-1)*n_chunk +1 : min(k*n_chunk, N));
        end

        cc = ceil(n_chunk/5);
        %h_assigns = zeros(N,1);
        my_n_assignments = cell(N_p,1);
        my_loglikelihoods = cell(N_p,1);
        my_logPgens = cell(N_p,1);
        my_assignment_entropies = cell(N_p,1);
        my_assignment_entropies_no_errors = cell(N_p,1);
        my_allele_entropies = cell(N_p,1);
        my_event_entropies = cell(N_p,1);
        my_mean_errors = cell(N_p,1);

        my_Vgene = cell(N_p,1);
        my_Dgene = cell(N_p,1);
        my_Jgene = cell(N_p,1);
        my_insVD = cell(N_p,1);
        my_insDJ = cell(N_p,1);
        my_cutV = cell(N_p,1);
        my_cutJ = cell(N_p,1);
        my_cutDl = cell(N_p,1);
        my_cutDr = cell(N_p,1);
        my_V_length = cell(N_p,1);
        my_J_length = cell(N_p,1);
        my_D_length = cell(N_p,1);
        my_errors = cell(N_p,1);



        disp('Starting Loop over sequences');
        parfor k=1:N_p
            warning('off','MATLAB:structOnObject');
            my_n_assignments{k} = zeros(length(myV{k}),1);
            my_loglikelihoods{k} = zeros(length(myV{k}),1);
            my_logPgens{k} = zeros(length(myV{k}),1);
            my_assignment_entropies{k} = zeros(length(myV{k}),1);
            my_assignment_entropies_no_errors{k} = zeros(length(myV{k}),1);
            my_allele_entropies{k} = zeros(length(myV{k}),1);
            my_event_entropies{k} = zeros(length(myV{k}),1);

            my_mean_errors{k} = zeros(length(myV{k}),1);

            my_Vgene{k} = zeros(length(myV{k}),1);
            my_Dgene{k} = zeros(length(myV{k}),1);
            my_Jgene{k} = zeros(length(myV{k}),1);
            my_insVD{k} = zeros(length(myV{k}),1);
            my_insDJ{k} = zeros(length(myV{k}),1);
            my_cutV{k} = zeros(length(myV{k}),1);
            my_cutJ{k} = zeros(length(myV{k}),1);
            my_cutDl{k} = zeros(length(myV{k}),1);
            my_cutDr{k} = zeros(length(myV{k}),1);
            my_V_length{k} = zeros(length(myV{k}),1);
            my_J_length{k} = zeros(length(myV{k}),1);
            my_D_length{k} = zeros(length(myV{k}),1);
            my_errors{k} = zeros(length(myV{k}),1);


            for i=1:length(myV{k})
                if mod(i,cc)==0, disp([num2str(i + (k-1)*n_chunk) '*']); end;
                % now explore space of all possible (sufficiently probable)
                % assignments and update the expectation values.
                assigns = calculate_assignment_probabilities(model, mySequence{k}{i}, myV{k}(i),myD{k}(i),myJ{k}(i), genV, genD, genJ, initial_assigns, probability_threshold_factor, no_error, ignore_deep_error, do_smoothing, force_all_alleles, READ_LENGTH_CORRECTION);
                my_n_assignments{k}(i) = assigns.n_assignments;
                my_loglikelihoods{k}(i) = log(assigns.likelihood);
                my_logPgens{k}(i) = log(assigns.generation_probability);

                if assigns.n_assignments > 0
                    p_ass = assigns.proba(1:assigns.n_assignments)./assigns.likelihood;
                    p_ass_nz = p_ass(p_ass>0);
                    my_assignment_entropies{k}(i) = -dot(p_ass_nz,log(p_ass_nz));

                    %[~,max_p_ass_i] = max(p_ass);
                    max_p_ass_i = assigns.max_proba_index;
                    my_Vgene{k}(i) = assigns.V(max_p_ass_i);
                    my_Dgene{k}(i) = assigns.DJ(max_p_ass_i,1);
                    my_Jgene{k}(i) = assigns.DJ(max_p_ass_i,2);
                    my_insVD{k}(i) = assigns.insVD(max_p_ass_i);
                    my_insDJ{k}(i) = assigns.insDJ(max_p_ass_i);
                    my_cutV{k}(i) = assigns.cutV_given_V(max_p_ass_i,1);
                    my_cutJ{k}(i) = assigns.cutJ_given_J(max_p_ass_i,1);
                    my_cutDl{k}(i) = assigns.cutDlcutDr_given_D(max_p_ass_i,1);
                    my_cutDr{k}(i) = assigns.cutDlcutDr_given_D(max_p_ass_i,2);
                    my_V_length{k}(i) = assigns.V_align_length(max_p_ass_i);
                    my_J_length{k}(i) = assigns.J_align_length(max_p_ass_i);
                    my_D_length{k}(i) = assigns.D_align_length(max_p_ass_i);
                    my_errors{k}(i) = assigns.error(max_p_ass_i);

                    my_mean_errors{k}(i) = dot(p_ass,assigns.error(1:assigns.n_assignments));


                    if assigns.generation_probability > 0
                        p_gen = assigns.event_probability(1:assigns.n_assignments)./assigns.generation_probability;
                        p_gen = p_gen(p_gen > 0);
                        my_assignment_entropies_no_errors{k}(i) = -dot(p_gen,log(p_gen));
                    else
                        my_assignment_entropies_no_errors{k}(i) = NaN;
                    end

                    if assigns.likelihood > 0
                        [counters(k), delta_counter] = model.update_counter(assigns, counters(k));
                        my_allele_entropies{k}(i) = entropy(delta_counter.nPVallele_given_gene) - entropy(delta_counter.nPV) + entropy(delta_counter.nPDallele_given_gene) - entropy(sum(delta_counter.nPDJ,2));
                    end

                    my_event_entropies{k}(i) = my_assignment_entropies_no_errors{k}(i) - my_allele_entropies{k}(i);

                end


            end

        end
        counter = model.sum_counters([counter; counters]);
        for k=1:N_p
            n_assignments = [n_assignments, my_n_assignments{k}'];
            loglikelihoods = [loglikelihoods, my_loglikelihoods{k}'];
            log_generation_probabilities = [log_generation_probabilities, my_logPgens{k}'];
            assignment_entropies = [assignment_entropies, my_assignment_entropies{k}'];
            assignment_entropies_no_errors = [assignment_entropies_no_errors, my_assignment_entropies_no_errors{k}'];
            allele_entropies = [allele_entropies, my_allele_entropies{k}'];
            event_entropies = [event_entropies, my_event_entropies{k}'];

            mean_errors = [mean_errors, my_mean_errors{k}'];

            MaxLL.Vgene = [MaxLL.Vgene, my_Vgene{k}'];
            MaxLL.Dgene = [MaxLL.Dgene, my_Dgene{k}'];
            MaxLL.Jgene = [MaxLL.Jgene, my_Jgene{k}'];
            MaxLL.insVD = [MaxLL.insVD, my_insVD{k}'];
            MaxLL.insDJ = [MaxLL.insDJ, my_insDJ{k}'];
            MaxLL.cutV = [MaxLL.cutV, my_cutV{k}'];
            MaxLL.cutJ = [MaxLL.cutJ, my_cutJ{k}'];
            MaxLL.cutDl = [MaxLL.cutDl, my_cutDl{k}'];
            MaxLL.cutDr = [MaxLL.cutDr, my_cutDr{k}'];
            MaxLL.V_length = [MaxLL.V_length, my_V_length{k}'];
            MaxLL.J_length = [MaxLL.J_length, my_J_length{k}'];
            MaxLL.D_length = [MaxLL.D_length, my_D_length{k}'];
            MaxLL.errors = [MaxLL.errors, my_errors{k}'];

        end
        %hist(h_assigns,30);
    end


    %%% Normalize the expectation values calculated and save to file.
        model = model.get_model_from_counter(counter);
        model = model.calculate_assignment_entropies();

    sequence_repertoire_entropy = model.S_total - mean(event_entropies(n_assignments>0));
    
    
    model_info.model = model;
    model_info.counter = counter;
    model_info.n_assignments = n_assignments;
    model_info.MaxLL = MaxLL;
    model_info.mean_errors = mean_errors;
    model_info.loglikelihoods = loglikelihoods;
    model_info.log_generation_probabilities = log_generation_probabilities;
    model_info.event_entropies = event_entropies;
    model_info.assignment_entropies = assignment_entropies;
    model_info.assignment_entropies_no_errors = assignment_entropies_no_errors;
    model_info.allele_entropies = allele_entropies;
    model_info.sequence_repertoire_entropy = sequence_repertoire_entropy;

    %save(out_filename, 'model','counter','n_assignments','MaxLL','mean_errors','loglikelihoods','log_generation_probabilities','event_entropies','assignment_entropies','assignment_entropies_no_errors','allele_entropies','sequence_repertoire_entropy');


end
