function OUT = k12_perm_plot(L1,L2,sims,D,s)

%K12_PERM compares two point populations L1,L2 by randlomly permuting their labels
%and computing K12-values for a range D of distance values. P-values are then computed, 
%reflecting significant attraction and/or repulsion between these patterns.

%NOTE 1: By convention, low P-Values here correspond to significant ATTRACTION between 
%        patterns.
%NOTE 2: Ties are treated neutrally, adding .5 rather 1 to rankings.

%Written by: TONY E. SMITH, 12/2/01

% INPUTS:
%     (i)   L1   = locations of pop1
%     (ii)  L2   = locations of pop2
%     (iii) sims = number of random shifts
%     (iv)  D    = vector of distance values at which to measure k12-values.
%     (v)   s    = optional specification of random seed. Default seed = 23311.
%                  s = 1 if random seed is to be used.
%                  s = 5_digit positive integer to be used as seed 


% OUTPUTS:  A structure OUT with:
%             OUT.pval = Vector of P-Values at each distance in D 
%             OUT.seed = Random seed used
%             OUT.S1 = Randomly relabeled version of L1
%             OUT.S2 = Randomly relabeled version of L2

% SCREEN OUTPUT:  A plot of P-Values against distances in D.


% Compute Initial Seed

if nargin == 4
   
   s = 23311; %forest seed (default)   
    
   %s =      ; %myrtles seed
   
elseif s == 1
   
   s = round(100000*rand(1))
   
else
   
   s = s;
   
end

rand('seed',s);


% Call K12_PERM

PVal = k12_perm(L1,L2,sims,D,s);

%Create random label example for output

loc = [L1;L2];

n = length(loc);

n1 = length(L1);

n2 = n - n1;

%random permutation of pops

list = randperm(n);

I1 = list(1:n1);

I2 = list(n1+1:n);

LL1 = loc(I1,:);

LL2 = loc(I2,:);

%Make Outputs

OUT.pval = PVal;

OUT.seed = s;

OUT.S1 = LL1;

OUT.S2 = LL2;



%SCREEN OUTPUT: Plot of PVal against D

plot(D,PVal,'k.','MarkerSize',20);

hold on;

plot(D,PVal,'k-','LineWidth',2);

hold on;

SIG = .05*ones(length(D),1);

plot(D,SIG,'r--');

plot(D,1 - SIG,'r--');


xlabel('Radius','Fontsize',12, 'FontWeight', 'bold');

ylabel('attraction                   P-Value                    repulsion',...
   'Fontsize',12, 'FontWeight', 'bold');


