function [similar score sym] = TImrf2(emesh, selection, bait, varargin)
% [similar score sym] = TImrf2(emesh, selection, bait, varargin)
%
% Wrapper for JTImrf2.
% Finds correlated/anti-correlated embryos to bait in selection
%

    use_javafx = [];
    correlation = 1;
    top_no = 40;
    verbose = 0;
    clusters = 2;
    sym = [];
    sym_idx = [];
    sym_text = [];

    if ~isempty(varargin)
        
	    i = 1;
	    while i <= length(varargin),
		switch(varargin{i}),
		    case 'symidx'
			i = i+1;
			sym_idx = varargin{i};
		    case 'symtext'
			i = i+1;
			sym_text = varargin{i};
		    case 'correlation'
			i = i+1;
			correlation = varargin{i};
		    case 'javafx'
			% use java function specified instead of
			% JTImrf2.java
			i = i+1;
			use_javafx = varargin{i};
		    case 'top_no'
			i = i+1;
			top_no = varargin{i};
		    case 'verbose'
			i = i+1;
			verbose = varargin{i};
		     otherwise
			error('Argument %s not known', varargin{i});
		end
		i = i+1;
	    end
    end


    t_conn = fully_connected(emesh);    
    no_eval = size(selection,2);    
    h_all = zeros(no_eval, 2);
    
    j_mrf = exist('JTImrf2');
    if j_mrf == 8,
    	java_mrf = JTImrf2(t_conn);
    else
        error('Java MRF not found - check your classpath!\n');
    end
    
    if ~isempty(use_javafx),
       java_mrf = javaObject(use_javafx, t_conn );
       if correlation < 0,
           java_mrf.inverse(1);
       end
       j_mrf = 8;
    end

    if correlation < 0,
       java_mrf.inverse(1);
    end	    
    
    for i=1:no_eval,
        
    	[h] = java_mrf.twoMesh(clusters, bait, selection(:,i), verbose);
            
        if verbose > 2,
            if j_mrf == 8,
                sk1 = java_mrf.getLabels(1);
                sk2 = java_mrf.getLabels(2);
            end
            figure;
            subplot(2,2,1); displaymesh(bait, emesh.p_scale, emesh.t);
            subplot(2,2,2); displaymesh(sk1, emesh.p_scale, emesh.t, -1);
            subplot(2,2,3); displaymesh(selection(:,i), emesh.p_scale, emesh.t);
            subplot(2,2,4); displaymesh(sk2, emesh.p_scale, emesh.t, -1);
        end
        
        h_all(i,:) = [h, i];
        
        if verbose > 0,
            fprintf('Embryo %d = %.2f\n', i, h);
        end
        
    end
    
    h_sort = sortrows(h_all, 1);
    
    if top_no > no_eval,
        top_ret = no_eval;
    elseif top_no <= 0,
        top_ret = no_eval;
    else
        top_ret = top_no;
    end
    
    similar = h_sort(1:top_ret, 2);
    score = h_sort(1:top_ret, 1);
    
    if ~isempty(sym_idx), 
        sym = {};
        if ~isempty(sym_text),
            for i=1:length(similar),
                sym{i} = sym_text{sym_idx(similar(i))};
            end
        else
            sym = sym_idx(similar);    
        end
    end

    if verbose > 1,
        if iscell(sym),
            showmeshcollection(emesh, [], selection(:,similar), 'symbols', sym);
        else
            showmeshcollection(emesh, [], selection(:,similar));
        end
    end
end



% from find_restricted
function [t_conn] = fully_connected(emesh)

    for i=1:length(emesh.t)
        tnfr{i} = [];   
        tnf{i} = []; 
        for j=1:size(emesh.t,2)       
            [prow, pcol] = find(emesh.t==emesh.t(i,j));
            tnfr{i} = [tnfr{i}, prow(prow ~= i)'];
        end
        for j=1:length(emesh.tn{i})
           if length(find(tnfr{i} == emesh.tn{i}(j))) > 1,
            tnf{i} = [tnf{i}, emesh.tn{i}(j)];
          end
        end
    end

    t_conn=zeros(311,311);
    for i=1:length(tnf), 
        for j=1:length(tnf{i}), 
            t_conn(i,tnf{i}(j)) = 1; 
            t_conn(i,i) = 1; 
        end;
    end

    t_conn = logical(t_conn);
end
